From b07894fa84948dcb5a6190baefd89444c1e41577 Mon Sep 17 00:00:00 2001 From: Michael Avoyan Date: Wed, 22 May 2024 16:31:35 +0300 Subject: [PATCH] v0.8.31 - fix VCLDidJwk population --- packages/sample-server/package.json | 2 +- packages/sdk/package.json | 2 +- packages/sdk/src/api/entities/VCLDidJwk.ts | 15 +++++++++ packages/sdk/src/api/entities/VCLPublicJwk.ts | 2 +- packages/sdk/test/entities/VCLDidJwk.test.ts | 33 +++++++++++++++++++ .../resources/valid/DidJwkMocks.ts | 20 +++++++---- 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 packages/sdk/test/entities/VCLDidJwk.test.ts diff --git a/packages/sample-server/package.json b/packages/sample-server/package.json index 897d299..36c1fad 100644 --- a/packages/sample-server/package.json +++ b/packages/sample-server/package.json @@ -19,7 +19,7 @@ "@fastify/autoload": "~5.7.1", "env-var": "~7.3.0", "fastify": "~4.15.0", - "@velocitycareerlabs/vnf-nodejs-wallet-sdk": "^0.8.30" + "@velocitycareerlabs/vnf-nodejs-wallet-sdk": "^0.8.31" }, "devDependencies": { "@jest/globals": "~29.5.0", diff --git a/packages/sdk/package.json b/packages/sdk/package.json index e4103f7..01c371a 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@velocitycareerlabs/vnf-nodejs-wallet-sdk", - "version": "0.8.30", + "version": "0.8.31", "description": "VNF Wallet SDK Nodejs", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/sdk/src/api/entities/VCLDidJwk.ts b/packages/sdk/src/api/entities/VCLDidJwk.ts index 7fbaf5d..c23c23f 100644 --- a/packages/sdk/src/api/entities/VCLDidJwk.ts +++ b/packages/sdk/src/api/entities/VCLDidJwk.ts @@ -2,6 +2,7 @@ import { JWK, base64url, exportJWK } from "jose"; import VCLPublicJwk from "./VCLPublicJwk"; import { KeyObject, KeyPairKeyObjectResult } from "crypto"; import canonicalize from "canonicalize"; +import { Dictionary } from "../VCLTypes"; export default class VCLDidJwk { constructor( @@ -17,6 +18,7 @@ export default class VCLDidJwk { static readonly KeyDid = "did"; static readonly KeyKid = "kid"; static readonly KeyKeyId = "keyId"; + static readonly KeyPublicJwk = "publicJwk"; static Utils = class { static generateDidJwk = async (ecKey: KeyPairKeyObjectResult) => { const publicJwk = await exportJWK(ecKey.publicKey); @@ -29,4 +31,17 @@ export default class VCLDidJwk { return `${this.generateDidJwk(ecKey)}${VCLDidJwk.DidJwkSuffix}`; }; }; + + public static fromString(didJwkStr: string): VCLDidJwk { + return VCLDidJwk.fromJSON(JSON.parse(didJwkStr)); + } + + public static fromJSON(didJwkJson: Dictionary): VCLDidJwk { + return new VCLDidJwk( + didJwkJson[VCLDidJwk.KeyDid], + VCLPublicJwk.fromJSON(didJwkJson[VCLDidJwk.KeyPublicJwk]), + didJwkJson[VCLDidJwk.KeyKid], + didJwkJson[VCLDidJwk.KeyKeyId] + ); + } } diff --git a/packages/sdk/src/api/entities/VCLPublicJwk.ts b/packages/sdk/src/api/entities/VCLPublicJwk.ts index 4ba9091..68359da 100644 --- a/packages/sdk/src/api/entities/VCLPublicJwk.ts +++ b/packages/sdk/src/api/entities/VCLPublicJwk.ts @@ -8,7 +8,7 @@ enum Format { } export default class VCLPublicJwk { - constructor(public valueStr: string, public valueJson: Dictionary) {} + private constructor(public valueStr: string, public valueJson: Dictionary) {} static readonly Format = Format; get curve(): string { return this.valueJson["crv"]; diff --git a/packages/sdk/test/entities/VCLDidJwk.test.ts b/packages/sdk/test/entities/VCLDidJwk.test.ts new file mode 100644 index 0000000..de8e3c0 --- /dev/null +++ b/packages/sdk/test/entities/VCLDidJwk.test.ts @@ -0,0 +1,33 @@ +import VCLDidJwk from "../../src/api/entities/VCLDidJwk"; +import { DidJwkMocks } from "../infrastructure/resources/valid/DidJwkMocks"; + +describe("VCLDidJwk Tests", () => { + let subject: VCLDidJwk; + const expectedDid = "did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6InI5ZnlhNTJJbG1UbzN5YlMwd19HZWZlUV9SWDJFSF9ISm1TV3FZWU8ySlkiLCJ5IjoicFFUUmE3R2txYzVrajZvZGVNcXBnVjVUNExqYlphNEY1S1R1MkpEclduYyJ9"; + const expectedKid = "did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6InI5ZnlhNTJJbG1UbzN5YlMwd19HZWZlUV9SWDJFSF9ISm1TV3FZWU8ySlkiLCJ5IjoicFFUUmE3R2txYzVrajZvZGVNcXBnVjVUNExqYlphNEY1S1R1MkpEclduYyJ9#0"; + const expectedKeyId = "6630f0a67b097c289711f583"; + const expectedPublicJwk = { + "kty": "EC", + "crv": "P-256", + "y": "pQTRa7Gkqc5kj6odeMqpgV5T4LjbZa4F5KTu2JDrWnc", + "x": "r9fya52IlmTo3ybS0w_GefeQ_RX2EH_HJmSWqYYO2JY" + } + + test("testPublicJwkFromStr", () => { + subject = VCLDidJwk.fromString(DidJwkMocks.DidJwkStr); + + expect(subject.did).toBe(expectedDid); + expect(subject.kid).toBe(expectedKid); + expect(subject.keyId).toBe(expectedKeyId); + expect(subject.publicJwk.valueJson).toStrictEqual(expectedPublicJwk); + }); + + test("testPublicJwkFromJson", () => { + subject = VCLDidJwk.fromJSON(DidJwkMocks.DidJwkJson); + + expect(subject.did).toBe(expectedDid); + expect(subject.kid).toBe(expectedKid); + expect(subject.keyId).toBe(expectedKeyId); + expect(subject.publicJwk.valueJson).toStrictEqual(expectedPublicJwk); + }); +}); diff --git a/packages/sdk/test/infrastructure/resources/valid/DidJwkMocks.ts b/packages/sdk/test/infrastructure/resources/valid/DidJwkMocks.ts index 7e44096..bb9d73a 100644 --- a/packages/sdk/test/infrastructure/resources/valid/DidJwkMocks.ts +++ b/packages/sdk/test/infrastructure/resources/valid/DidJwkMocks.ts @@ -1,12 +1,20 @@ import { VCLDidJwk, VCLPublicJwk } from "../../../../src"; class DidJwkMocks { - static readonly DidJwk = new VCLDidJwk( - "did:jwk:eyJrdHkiOiJFQyIsInVzZSI6InNpZyIsImNydiI6InNlY3AyNTZrMSIsImtpZCI6IjNkODdhZGFmLWQ0ZmEtNDBkZS1iNGYzLTExNGFhOGZmOTMyOCIsIngiOiJvZThGN1ZPWmtOZGpnUTNLdHVmenlwRjBkTWh2QjZVanpYQVRVQ1d2NlRjIiwieSI6IjRQNFZJRFJtYWM2ZlJFY0hkR2tDdVRqdDJMSnNoYVZ2WWpjMGVVZEdpaHcifQ", - VCLPublicJwk.fromString("{\"kty\":\"EC\",\"use\":\"sig\",\"crv\":\"secp256k1\",\"kid\":\"3d87adaf-d4fa-40de-b4f3-114aa8ff9328\",\"x\":\"oe8F7VOZkNdjgQ3KtufzypF0dMhvB6UjzXATUCWv6Tc\",\"y\":\"4P4VIDRmac6fREcHdGkCuTjt2LJshaVvYjc0eUdGihw\"}"), - "did:jwk:eyJrdHkiOiJFQyIsInVzZSI6InNpZyIsImNydiI6InNlY3AyNTZrMSIsImtpZCI6IjNkODdhZGFmLWQ0ZmEtNDBkZS1iNGYzLTExNGFhOGZmOTMyOCIsIngiOiJvZThGN1ZPWmtOZGpnUTNLdHVmenlwRjBkTWh2QjZVanpYQVRVQ1d2NlRjIiwieSI6IjRQNFZJRFJtYWM2ZlJFY0hkR2tDdVRqdDJMSnNoYVZ2WWpjMGVVZEdpaHcifQ#0", - "3d87adaf-d4fa-40de-b4f3-114aa8ff9328" - ) + static readonly DidJwkStr = `{ "did": "did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6InI5ZnlhNTJJbG1UbzN5YlMwd19HZWZlUV9SWDJFSF9ISm1TV3FZWU8ySlkiLCJ5IjoicFFUUmE3R2txYzVrajZvZGVNcXBnVjVUNExqYlphNEY1S1R1MkpEclduYyJ9", "kid": "did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6InI5ZnlhNTJJbG1UbzN5YlMwd19HZWZlUV9SWDJFSF9ISm1TV3FZWU8ySlkiLCJ5IjoicFFUUmE3R2txYzVrajZvZGVNcXBnVjVUNExqYlphNEY1S1R1MkpEclduYyJ9#0", "keyId": "6630f0a67b097c289711f583", "publicJwk": { "kty": "EC", "crv": "P-256", "y": "pQTRa7Gkqc5kj6odeMqpgV5T4LjbZa4F5KTu2JDrWnc", "x": "r9fya52IlmTo3ybS0w_GefeQ_RX2EH_HJmSWqYYO2JY" } }` + static readonly DidJwkJson = + { + "did": "did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6InI5ZnlhNTJJbG1UbzN5YlMwd19HZWZlUV9SWDJFSF9ISm1TV3FZWU8ySlkiLCJ5IjoicFFUUmE3R2txYzVrajZvZGVNcXBnVjVUNExqYlphNEY1S1R1MkpEclduYyJ9", + "kid": "did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6InI5ZnlhNTJJbG1UbzN5YlMwd19HZWZlUV9SWDJFSF9ISm1TV3FZWU8ySlkiLCJ5IjoicFFUUmE3R2txYzVrajZvZGVNcXBnVjVUNExqYlphNEY1S1R1MkpEclduYyJ9#0", + "keyId": "6630f0a67b097c289711f583", + "publicJwk": { + "kty": "EC", + "crv": "P-256", + "y": "pQTRa7Gkqc5kj6odeMqpgV5T4LjbZa4F5KTu2JDrWnc", + "x": "r9fya52IlmTo3ybS0w_GefeQ_RX2EH_HJmSWqYYO2JY" + } + } + static readonly DidJwk = VCLDidJwk.fromJSON(DidJwkMocks.DidJwkJson) } export { DidJwkMocks } \ No newline at end of file