Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable auto-conversion on verification method reference comparison #509

Merged
merged 15 commits into from
Jan 24, 2025
Merged
13 changes: 13 additions & 0 deletions examples/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# @docknetwork/sdk-examples

## 0.13.0

### Minor Changes

- Enable auto-conversion on verification method comparison

### Patch Changes

- Updated dependencies
- @docknetwork/[email protected]
- @docknetwork/[email protected]
- @docknetwork/[email protected]

## 0.12.0

### Minor Changes
Expand Down
8 changes: 4 additions & 4 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@docknetwork/sdk-examples",
"private": true,
"type": "module",
"version": "0.12.0",
"version": "0.13.0",
"scripts": {
"bbs-dock-example": "babel-node ./bbs-dock.js",
"claim-deduction-example": "babel-node ./claim-deduction.js",
Expand All @@ -19,9 +19,9 @@
"lint": "eslint \"*.js\""
},
"dependencies": {
"@docknetwork/credential-sdk": "0.29.0",
"@docknetwork/dock-blockchain-api": "0.14.0",
"@docknetwork/dock-blockchain-modules": "0.18.0"
"@docknetwork/credential-sdk": "0.30.0",
"@docknetwork/dock-blockchain-api": "0.15.0",
"@docknetwork/dock-blockchain-modules": "0.19.0"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/cheqd-blockchain-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/cheqd-blockchain-api

## 0.24.0

### Minor Changes

- Enable auto-conversion on verification method comparison

### Patch Changes

- Updated dependencies
- @docknetwork/[email protected]

## 0.23.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/cheqd-blockchain-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/cheqd-blockchain-api",
"version": "0.23.0",
"version": "0.24.0",
"license": "MIT",
"main": "./dist/esm/index.js",
"type": "module",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@cheqd/sdk": "cjs",
"@docknetwork/credential-sdk": "0.29.0"
"@docknetwork/credential-sdk": "0.30.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand Down
11 changes: 11 additions & 0 deletions packages/cheqd-blockchain-modules/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/cheqd-blockchain-modules

## 0.21.0

### Minor Changes

- Enable auto-conversion on verification method comparison

### Patch Changes

- Updated dependencies
- @docknetwork/[email protected]

## 0.20.0

### Minor Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/cheqd-blockchain-modules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/cheqd-blockchain-modules",
"version": "0.20.0",
"version": "0.21.0",
"type": "module",
"license": "MIT",
"main": "./dist/esm/index.js",
Expand Down Expand Up @@ -33,7 +33,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@docknetwork/credential-sdk": "0.29.0"
"@docknetwork/credential-sdk": "0.30.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand All @@ -42,7 +42,7 @@
"@babel/plugin-syntax-import-attributes": "^7.25.6",
"@babel/plugin-transform-modules-commonjs": "^7.24.1",
"@babel/preset-env": "^7.24.3",
"@docknetwork/cheqd-blockchain-api": "0.23.0",
"@docknetwork/cheqd-blockchain-api": "0.24.0",
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/credential-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @docknetwork/credential-sdk

## 0.30.0

### Minor Changes

- Enable auto-conversion on verification method comparison

## 0.29.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/credential-sdk",
"version": "0.29.0",
"version": "0.30.0",
"license": "MIT",
"type": "module",
"files": [
Expand Down
53 changes: 52 additions & 1 deletion packages/credential-sdk/src/vc/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ import {
} from './crypto/constants';
import Bls12381BBDT16MACDock2024 from './crypto/Bls12381BBDT16MACDock2024';
import { DidKeypair } from '../keypairs';
import {
CheqdMainnetVerificationMethodRef,
CheqdTestnetVerificationMethodRef,
CheqdVerificationMethodRef,
VerificationMethodRef,
} from '../types/did/document/verification-method-ref';
import {
CheqdMainnetDid,
CheqdTestnetDid,
} from '../types/did/onchain/typed-did';

/**
* @typedef {object} KeyDoc The Options to use in the function createUser.
Expand Down Expand Up @@ -135,6 +145,46 @@ export function potentialToArray(a) {
return a ? (Array.isArray(a) ? a : [a]) : [];
}

/**
* Compares a verification method reference with a key ID to determine if they are equivalent.
* The function checks if the reference might start with `did:dock:`, and converts it to a `did:cheqd` format for comparison.
*
* @param {string} ref - The verification method reference to compare.
* @param {string} keyId - The key ID to compare against.
* @returns {boolean} - Returns `true` if the references are equivalent, `false` otherwise.
*/
export const verMethodRefsEqual = (ref, keyId) => {
// If the reference matches the key ID directly, return true.
if (ref === keyId) {
return true;
}

try {
// Parse the key ID into a Cheqd verification method reference object.
let parsedKeyId = CheqdVerificationMethodRef.from(keyId);
const {
did: {
value: { constructor: CheqdDid },
},
} = parsedKeyId;

// Check if the parsed key ID corresponds to the Cheqd Testnet or Mainnet and convert accordingly.
if (CheqdDid === CheqdTestnetDid) {
parsedKeyId = CheqdTestnetVerificationMethodRef.from(keyId);
} else if (CheqdDid === CheqdMainnetDid) {
parsedKeyId = CheqdMainnetVerificationMethodRef.from(keyId);
} else {
throw new Error(`Unknown DID type: \`${CheqdDid.name}\``);
}

// Compare the parsed reference with the provided reference using `eq`.
return parsedKeyId.eq(VerificationMethodRef.from(ref));
} catch (err) {
// If an error occurs during parsing or comparison, assume the references are not equivalent.
return false;
}
};

export function getKeyFromDIDDocument(didDocument, didUrl) {
// Ensure not already a key doc
if (
Expand All @@ -151,7 +201,8 @@ export function getKeyFromDIDDocument(didDocument, didUrl) {
...potentialToArray(didDocument.keyAgreement),
...potentialToArray(didDocument.publicKey),
];
return possibleKeys.filter((key) => key.id === didUrl)[0];

return possibleKeys.find((key) => verMethodRefsEqual(didUrl, key.id));
}

/**
Expand Down
23 changes: 23 additions & 0 deletions packages/credential-sdk/tests/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
DockDidValue,
CheqdTestnetDidValue,
CheqdMainnetDidValue,
DockDid,
} from "../src/types/did";
import {
CheqdMainnetDIDDocument,
CheqdTestnetDIDDocument,
DIDDocument,
VerificationMethodRef,
} from "../src/types/did/document";
import {
PublicKeyEd25519,
Expand All @@ -20,6 +22,8 @@ import {
} from "../src/types";
import { TypedUUID } from "../src/types/generic";
import { hexToU8a, maybeToCheqdPayloadOrJSON } from "../src/utils";
import { verMethodRefsEqual } from "../src/vc";
import { CheqdTestnetVerificationMethodRef } from "../src/types/did/document/verification-method-ref";

const RANDOM_PKS = [
"0xa1aa6a2058dd190e284a64e72adaf4e16a9ae9fbf0673d7575924e6aca3b21dc",
Expand Down Expand Up @@ -129,6 +133,25 @@ describe("DID document workflow", () => {
const CAP_INV = new VerificationRelationship().setCapabilityInvocation();
const KEY_AGR = new VerificationRelationship().setKeyAgreement();

test(`\`verMethodRefsEqual\` method conversion works`, () => {
const dockVerMethod = new VerificationMethodRef(DockDid.random(), 3);
const cheqdVerMethod =
CheqdTestnetVerificationMethodRef.from(dockVerMethod);

expect(
verMethodRefsEqual(String(dockVerMethod), String(cheqdVerMethod))
).toBe(true);
expect(
verMethodRefsEqual(
String(dockVerMethod),
String(new VerificationMethodRef(CheqdTestnetDid.random(), 3))
)
).toBe(false);
expect(
verMethodRefsEqual(String(dockVerMethod), String(dockVerMethod))
).toBe(true);
});

test(`\`DIDDocument.from\` works`, () => {
const doc = {
"@context": ["test"],
Expand Down
11 changes: 11 additions & 0 deletions packages/dock-blockchain-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/dock-blockchain-api

## 0.15.0

### Minor Changes

- Enable auto-conversion on verification method comparison

### Patch Changes

- Updated dependencies
- @docknetwork/[email protected]

## 0.14.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/dock-blockchain-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/dock-blockchain-api",
"version": "0.14.0",
"version": "0.15.0",
"license": "MIT",
"main": "./dist/esm/index.js",
"type": "module",
Expand Down Expand Up @@ -89,7 +89,7 @@
"@polkadot/api": "10.12.4"
},
"dependencies": {
"@docknetwork/credential-sdk": "0.29.0",
"@docknetwork/credential-sdk": "0.30.0",
"@docknetwork/node-types": "^0.17.0",
"@juanelas/base64": "^1.0.5",
"@polkadot/api": "10.12.4",
Expand Down
11 changes: 11 additions & 0 deletions packages/dock-blockchain-modules/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/dock-blockchain-modules

## 0.19.0

### Minor Changes

- Enable auto-conversion on verification method comparison

### Patch Changes

- Updated dependencies
- @docknetwork/[email protected]

## 0.18.0

### Minor Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/dock-blockchain-modules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/dock-blockchain-modules",
"version": "0.18.0",
"version": "0.19.0",
"license": "MIT",
"type": "module",
"main": "./dist/esm/index.js",
Expand Down Expand Up @@ -33,7 +33,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@docknetwork/credential-sdk": "0.29.0"
"@docknetwork/credential-sdk": "0.30.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand All @@ -42,7 +42,7 @@
"@babel/plugin-syntax-import-attributes": "^7.25.6",
"@babel/plugin-transform-modules-commonjs": "^7.24.1",
"@babel/preset-env": "^7.24.3",
"@docknetwork/dock-blockchain-api": "0.14.0",
"@docknetwork/dock-blockchain-api": "0.15.0",
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
Loading