Skip to content

Commit

Permalink
v0.8.31 - fix VCLDidJwk population
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelavoyan committed May 22, 2024
1 parent 63287f1 commit b07894f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/sample-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 15 additions & 0 deletions packages/sdk/src/api/entities/VCLDidJwk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand All @@ -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<any>): VCLDidJwk {
return new VCLDidJwk(
didJwkJson[VCLDidJwk.KeyDid],
VCLPublicJwk.fromJSON(didJwkJson[VCLDidJwk.KeyPublicJwk]),
didJwkJson[VCLDidJwk.KeyKid],
didJwkJson[VCLDidJwk.KeyKeyId]
);
}
}
2 changes: 1 addition & 1 deletion packages/sdk/src/api/entities/VCLPublicJwk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum Format {
}

export default class VCLPublicJwk {
constructor(public valueStr: string, public valueJson: Dictionary<any>) {}
private constructor(public valueStr: string, public valueJson: Dictionary<any>) {}
static readonly Format = Format;
get curve(): string {
return this.valueJson["crv"];
Expand Down
33 changes: 33 additions & 0 deletions packages/sdk/test/entities/VCLDidJwk.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
20 changes: 14 additions & 6 deletions packages/sdk/test/infrastructure/resources/valid/DidJwkMocks.ts
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit b07894f

Please sign in to comment.