From 72c1b8c35bffcb3c31064366c580e1cfa3925912 Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Thu, 6 Jun 2024 15:20:28 +0300 Subject: [PATCH] add DID toJson function --- package-lock.json | 4 ++-- package.json | 2 +- src/did/did.ts | 4 ++++ tests/did.test.ts | 12 ++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b955d52..fcfc979 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@iden3/js-iden3-core", - "version": "1.3.1", + "version": "1.3.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@iden3/js-iden3-core", - "version": "1.3.1", + "version": "1.3.2", "license": "AGPL-3.0", "devDependencies": { "@iden3/eslint-config": "https://github.com/iden3/eslint-config", diff --git a/package.json b/package.json index 39f0bf8..01469c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@iden3/js-iden3-core", - "version": "1.3.1", + "version": "1.3.2", "description": "Low level API to create and manipulate iden3 Claims.", "source": "./src/index.ts", "types": "dist/types/index.d.ts", diff --git a/src/did/did.ts b/src/did/did.ts index 7d4a1aa..8201f1b 100644 --- a/src/did/did.ts +++ b/src/did/did.ts @@ -91,6 +91,10 @@ export class DID { return buff.join(''); } + toJSON() { + return this.string(); + } + static parse(s: string): DID { const parser = new Parser(s); diff --git a/tests/did.test.ts b/tests/did.test.ts index d47f289..fdca2b3 100644 --- a/tests/did.test.ts +++ b/tests/did.test.ts @@ -97,6 +97,18 @@ describe('DID tests', () => { expect('did:iden3:readonly:tJ93RwaVfE1PEMxd5rpZZuPtLCwbEaDCrNBhAy8HM').toBe(did2.string()); }); + it('Test DID to JSON', () => { + const didStr = 'did:polygonid:linea:sepolia:32232vGknSaJHfCBffnbzHzYYy6FvHDkK9QL4SFAq6'; + const did = DID.parse(didStr); + const field1Val = 'field1'; + const objWithDid = { + field1: field1Val, + did + }; + const stringify = JSON.stringify(objWithDid); + expect(stringify).toBe(`{"field1":"${field1Val}","did":"${didStr}"}`); + }); + describe('TestDID_PolygonID_Types', () => { const testCases = [ {