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

Refine the search algorithm for locating verification method references #510

Merged
merged 18 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.14.0

### Minor Changes

- Refine the search algorithm for locating verification method references.

### Patch Changes

- Updated dependencies
- @docknetwork/dock-blockchain-modules@0.20.0
- @docknetwork/dock-blockchain-api@0.16.0
- @docknetwork/credential-sdk@0.31.0

## 0.13.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.13.0",
"version": "0.14.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.30.0",
"@docknetwork/dock-blockchain-api": "0.15.0",
"@docknetwork/dock-blockchain-modules": "0.19.0"
"@docknetwork/credential-sdk": "0.31.0",
"@docknetwork/dock-blockchain-api": "0.16.0",
"@docknetwork/dock-blockchain-modules": "0.20.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.25.0

### Minor Changes

- Refine the search algorithm for locating verification method references.

### Patch Changes

- Updated dependencies
- @docknetwork/[email protected]

## 0.24.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.24.0",
"version": "0.25.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.30.0"
"@docknetwork/credential-sdk": "0.31.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.22.0

### Minor Changes

- Refine the search algorithm for locating verification method references.

### Patch Changes

- Updated dependencies
- @docknetwork/[email protected]

## 0.21.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.21.0",
"version": "0.22.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.30.0"
"@docknetwork/credential-sdk": "0.31.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.24.0",
"@docknetwork/cheqd-blockchain-api": "0.25.0",
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
14 changes: 10 additions & 4 deletions packages/cheqd-blockchain-modules/src/did/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import {
import { TypedUUID } from '@docknetwork/credential-sdk/types/generic';
import { createInternalCheqdModule } from '../common';

function parseDocument(document) {
function createOrUpdateDidDocument(document) {
return DIDDocument.from(document).toCheqd(this.types.DidDocument);
}
function deactivateDidDocument(id) {
return new CheqdDeactivateDidDocument(
this.types.Did.from(id),
TypedUUID.random(),
);
}

const methods = {
createDidDocument: parseDocument,
updateDidDocument: parseDocument,
deactivateDidDocument: (id) => new CheqdDeactivateDidDocument(id, TypedUUID.random()),
createDidDocument: createOrUpdateDidDocument,
updateDidDocument: createOrUpdateDidDocument,
deactivateDidDocument,
};

export class CheqdDIDModuleInternal extends createInternalCheqdModule(methods) {
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.31.0

### Minor Changes

- Refine the search algorithm for locating verification method references.

## 0.30.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.30.0",
"version": "0.31.0",
"license": "MIT",
"type": "module",
"files": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { withQualifier, TypedTuple, TypedNumber } from '../../generic';
import {
withQualifier,
TypedTuple,
TypedNumber,
createConverter,
} from '../../generic';
import {
NamespaceDid,
CheqdNamespaceDid,
CheqdTestnetDid,
CheqdMainnetDid,
DockNamespaceDid,
} from '../onchain/typed-did';

export class VerificationMethodRef extends withQualifier(TypedTuple) {
Expand Down Expand Up @@ -50,10 +56,27 @@ export class CheqdVerificationMethodRef extends VerificationMethodRef {
static Classes = [CheqdNamespaceDid, TypedNumber];
}

export class DockVerificartionMethodRef extends VerificationMethodRef {
static Classes = [DockNamespaceDid, TypedNumber];
}

export class CheqdTestnetVerificationMethodRef extends CheqdVerificationMethodRef {
static Classes = [CheqdTestnetDid, TypedNumber];
}

export class CheqdMainnetVerificationMethodRef extends CheqdVerificationMethodRef {
static Classes = [CheqdMainnetDid, TypedNumber];
}

/**
* Retrieve all possible stringified representations of DIDs that can be derived from the provided Dock verification method reference.
*
* @param {*} verMethodRef
* @returns {Array<string>}
*/
export const possibleVerificationMethodRefs = createConverter(
String,
DockVerificartionMethodRef,
CheqdTestnetVerificationMethodRef,
CheqdMainnetVerificationMethodRef,
);
14 changes: 14 additions & 0 deletions packages/credential-sdk/src/types/did/onchain/typed-did/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
TypedEnum,
TypedTuple,
TypedUUID,
createConverter,
withQualifier,
} from '../../../generic';
import { CheqdDid, CheqdMainnetDid, CheqdTestnetDid } from './cheqd-did';
Expand Down Expand Up @@ -239,5 +240,18 @@ DidOrDidMethodKeySignature.bindVariants(
DidMethodKeySignature,
);

/**
* Retrieve all possible stringified representations of DIDs that can be derived from the provided Dock DID.
*
* @param {*} did
* @returns {Array<string>}
*/
export const possibleDids = createConverter(
String,
DockDid,
CheqdTestnetDid,
CheqdMainnetDid,
);

export { DockDidValue, DidMethodKeyPublicKey };
export * from './cheqd-did';
19 changes: 19 additions & 0 deletions packages/credential-sdk/src/types/generic/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,22 @@ export class ByteArray extends TypedBytes {}
export class ArrayOfByteArrays extends TypedArray {
static Class = TypedBytes;
}

export const createConverter = (finalize, parserClass, ...parsers) => (item) => {
const res = [item];
try {
const parsedDid = parserClass.from(item);

for (const parser of parsers) {
try {
res.push(parser.from(parsedDid));
} catch {
// Error is intentionally ignored
}
}
} catch {
// Error is intentionally ignored
}

return res.map(finalize);
};
7 changes: 6 additions & 1 deletion packages/credential-sdk/src/vc/CredentialIssuancePurpose.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import jsonld from 'jsonld';
import jsigs from 'jsonld-signatures';
import { expandJSONLD } from './helpers';
import { Bls12381BBDT16MacProofDockName } from './crypto/constants';
import { possibleDids } from '../types/did/onchain/typed-did';

const { AssertionProofPurpose } = jsigs.purposes;

Expand Down Expand Up @@ -85,7 +86,11 @@ export default class CredentialIssuancePurpose extends AssertionProofPurpose {
throw new Error('Credential issuer is required.');
}

if (result.controller.id !== issuer[0]['@id']) {
if (
possibleDids(issuer[0]['@id']).every(
(did) => did !== result.controller.id,
)
) {
throw new Error(
'Credential issuer must match the verification method controller.',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { u8aToU8a } from '../../../utils/bytes';
import { withExtendedStaticProperties } from '../../../utils/inheritance';
import CustomLinkedDataSignature from './CustomLinkedDataSignature';
import { deepClone } from '../../../utils/misc';
import { possibleVerificationMethodRefs } from '../../../types/did/document/verification-method-ref';

const SUITE_CONTEXT_URL = 'https://www.w3.org/2018/credentials/v1';

Expand Down Expand Up @@ -543,21 +544,23 @@ export default withExtendedStaticProperties(
if (!verificationMethod) {
throw new Error('No "verificationMethod" found in proof.');
}

// Note: `expansionMap` is intentionally not passed; we can safely drop
// properties here and must allow for it
const result = await jsonld.frame(
verificationMethod,
{
'@context': jsigs.SECURITY_CONTEXT_URL,
'@embed': '@always',
id: verificationMethod,
id: possibleVerificationMethodRefs(verificationMethod),
},
{
documentLoader,
compactToRelative: false,
expandContext: jsigs.SECURITY_CONTEXT_URL,
},
);

if (!result) {
throw new Error(`Verification method ${verificationMethod} not found.`);
}
Expand Down
49 changes: 4 additions & 45 deletions packages/credential-sdk/src/vc/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ 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';
import { possibleVerificationMethodRefs } from '../types/did/document/verification-method-ref';

/**
* @typedef {object} KeyDoc The Options to use in the function createUser.
Expand Down Expand Up @@ -87,9 +78,6 @@ export async function getSuiteFromKeyDoc(keyDoc, useProofValue, options) {
case Ed255192020VerKeyName:
Cls = Ed25519Signature2020;
break;
/* case Sr25519VerKeyName:
Cls = Sr25519Signature2020;
break; */
case Bls12381BBSDockVerKeyName:
Cls = Bls12381BBSSignatureDock2022;
break;
Expand Down Expand Up @@ -147,43 +135,14 @@ export function potentialToArray(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.
* 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 const verMethodRefsEqual = (ref, keyId) => ref === keyId
|| possibleVerificationMethodRefs(ref).some((id) => id === keyId);

export function getKeyFromDIDDocument(didDocument, didUrl) {
// Ensure not already a key doc
Expand Down
Loading
Loading