diff --git a/packages/core/src/modules/dids/domain/key-type/__tests__/ed25519.test.ts b/packages/core/src/modules/dids/domain/key-type/__tests__/ed25519.test.ts index cd93ada9cd..c66b4fc7aa 100644 --- a/packages/core/src/modules/dids/domain/key-type/__tests__/ed25519.test.ts +++ b/packages/core/src/modules/dids/domain/key-type/__tests__/ed25519.test.ts @@ -52,7 +52,10 @@ describe('ed25519', () => { }) it('supports Ed25519VerificationKey2018 verification method type', () => { - expect(keyDidEd25519.supportedVerificationMethodTypes).toMatchObject(['Ed25519VerificationKey2018']) + expect(keyDidEd25519.supportedVerificationMethodTypes).toMatchObject([ + 'Ed25519VerificationKey2018', + 'Ed25519VerificationKey2020', + ]) }) it('returns key for Ed25519VerificationKey2018 verification method', () => { @@ -63,6 +66,22 @@ describe('ed25519', () => { expect(key.fingerprint).toBe(TEST_ED25519_FINGERPRINT) }) + it('returns key for Ed25519VerificationKey2020 verification method', () => { + const verificationMethod = JsonTransformer.fromJSON( + { + id: 'did:example:123', + type: 'Ed25519VerificationKey2020', + controller: 'did:example:123', + publicKeyMultibase: 'z6MkkBWg1AnNxxWiq77gJDeHsLhGN6JV9Y3d6WiTifUs1sZi', + }, + VerificationMethod + ) + + const key = keyDidEd25519.getKeyFromVerificationMethod(verificationMethod) + + expect(key.publicKeyBase58).toBe('6jFdQvXwdR2FicGycegT2F9GYX2djeoGQVoXtPWr6enL') + }) + it('throws an error if an invalid verification method is passed', () => { const verificationMethod = JsonTransformer.fromJSON(didKeyEd25519Fixture.verificationMethod[0], VerificationMethod) diff --git a/packages/core/src/modules/dids/domain/key-type/bls12381g2.ts b/packages/core/src/modules/dids/domain/key-type/bls12381g2.ts index a17d20130a..f7cc4b2a6f 100644 --- a/packages/core/src/modules/dids/domain/key-type/bls12381g2.ts +++ b/packages/core/src/modules/dids/domain/key-type/bls12381g2.ts @@ -4,7 +4,7 @@ import type { KeyDidMapping } from './keyDidMapping' import { KeyType } from '../../../../crypto' import { Key } from '../../../../crypto/Key' -const VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020 = 'Bls12381G2Key2020' +export const VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020 = 'Bls12381G2Key2020' export function getBls12381g2VerificationMethod(did: string, key: Key) { return { diff --git a/packages/core/src/modules/dids/domain/key-type/ed25519.ts b/packages/core/src/modules/dids/domain/key-type/ed25519.ts index eb360c72fb..4098d230b5 100644 --- a/packages/core/src/modules/dids/domain/key-type/ed25519.ts +++ b/packages/core/src/modules/dids/domain/key-type/ed25519.ts @@ -6,7 +6,8 @@ import { convertPublicKeyToX25519 } from '@stablelib/ed25519' import { KeyType } from '../../../../crypto' import { Key } from '../../../../crypto/Key' -const VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018 = 'Ed25519VerificationKey2018' +export const VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018 = 'Ed25519VerificationKey2018' +export const VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020 = 'Ed25519VerificationKey2020' export function getEd25519VerificationMethod({ key, id, controller }: { id: string; key: Key; controller: string }) { return { @@ -18,19 +19,28 @@ export function getEd25519VerificationMethod({ key, id, controller }: { id: stri } export const keyDidEd25519: KeyDidMapping = { - supportedVerificationMethodTypes: [VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018], + supportedVerificationMethodTypes: [ + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018, + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020, + ], getVerificationMethods: (did, key) => [ getEd25519VerificationMethod({ id: `${did}#${key.fingerprint}`, key, controller: did }), ], getKeyFromVerificationMethod: (verificationMethod: VerificationMethod) => { if ( - verificationMethod.type !== VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018 || - !verificationMethod.publicKeyBase58 + verificationMethod.type === VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018 && + verificationMethod.publicKeyBase58 + ) { + return Key.fromPublicKeyBase58(verificationMethod.publicKeyBase58, KeyType.Ed25519) + } + if ( + verificationMethod.type === VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020 && + verificationMethod.publicKeyMultibase ) { - throw new Error('Invalid verification method passed') + return Key.fromFingerprint(verificationMethod.publicKeyMultibase) } - return Key.fromPublicKeyBase58(verificationMethod.publicKeyBase58, KeyType.Ed25519) + throw new Error('Invalid verification method passed') }, } diff --git a/packages/core/src/modules/vc/SignatureSuiteRegistry.ts b/packages/core/src/modules/vc/SignatureSuiteRegistry.ts index 925fa3f334..b3ac5f4316 100644 --- a/packages/core/src/modules/vc/SignatureSuiteRegistry.ts +++ b/packages/core/src/modules/vc/SignatureSuiteRegistry.ts @@ -11,8 +11,8 @@ export const SignatureSuiteToken = Symbol('SignatureSuiteToken') export interface SuiteInfo { suiteClass: typeof LinkedDataSignature proofType: string - requiredKeyType: string - keyType: string + verificationMethodTypes: string[] + keyTypes: KeyType[] } @injectable() @@ -27,8 +27,12 @@ export class SignatureSuiteRegistry { return this.suiteMapping.map((x) => x.proofType) } + public getByVerificationMethodType(verificationMethodType: string) { + return this.suiteMapping.find((x) => x.verificationMethodTypes.includes(verificationMethodType)) + } + public getByKeyType(keyType: KeyType) { - return this.suiteMapping.find((x) => x.keyType === keyType) + return this.suiteMapping.find((x) => x.keyTypes.includes(keyType)) } public getByProofType(proofType: string) { @@ -40,4 +44,14 @@ export class SignatureSuiteRegistry { return suiteInfo } + + public getVerificationMethodTypesByProofType(proofType: string): string[] { + const suiteInfo = this.suiteMapping.find((suiteInfo) => suiteInfo.proofType === proofType) + + if (!suiteInfo) { + throw new AriesFrameworkError(`No verification method type found for proof type: ${proofType}`) + } + + return suiteInfo.verificationMethodTypes + } } diff --git a/packages/core/src/modules/vc/W3cCredentialService.ts b/packages/core/src/modules/vc/W3cCredentialService.ts index 4b9ccef0aa..18fa986542 100644 --- a/packages/core/src/modules/vc/W3cCredentialService.ts +++ b/packages/core/src/modules/vc/W3cCredentialService.ts @@ -63,7 +63,7 @@ export class W3cCredentialService { const signingKey = await this.getPublicKeyFromVerificationMethod(agentContext, options.verificationMethod) const suiteInfo = this.suiteRegistry.getByProofType(options.proofType) - if (signingKey.keyType !== suiteInfo.keyType) { + if (!suiteInfo.keyTypes.includes(signingKey.keyType)) { throw new AriesFrameworkError('The key type of the verification method does not match the suite') } @@ -169,7 +169,7 @@ export class W3cCredentialService { const signingKey = await this.getPublicKeyFromVerificationMethod(agentContext, options.verificationMethod) - if (signingKey.keyType !== suiteInfo.keyType) { + if (!suiteInfo.keyTypes.includes(signingKey.keyType)) { throw new AriesFrameworkError('The key type of the verification method does not match the suite') } @@ -379,6 +379,14 @@ export class W3cCredentialService { return result.map((record) => record.credential) } + public getVerificationMethodTypesByProofType(proofType: string): string[] { + return this.suiteRegistry.getByProofType(proofType).verificationMethodTypes + } + + public getKeyTypesByProofType(proofType: string): string[] { + return this.suiteRegistry.getByProofType(proofType).keyTypes + } + public async findCredentialRecordByQuery( agentContext: AgentContext, query: Query diff --git a/packages/core/src/modules/vc/W3cVcModule.ts b/packages/core/src/modules/vc/W3cVcModule.ts index 30d5eb3be9..64a8782501 100644 --- a/packages/core/src/modules/vc/W3cVcModule.ts +++ b/packages/core/src/modules/vc/W3cVcModule.ts @@ -1,6 +1,11 @@ import type { DependencyManager, Module } from '../../plugins' import { KeyType } from '../../crypto' +import { VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020 } from '../dids/domain/key-type/bls12381g2' +import { + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018, + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020, +} from '../dids/domain/key-type/ed25519' import { SignatureSuiteRegistry, SignatureSuiteToken } from './SignatureSuiteRegistry' import { W3cCredentialService } from './W3cCredentialService' @@ -19,22 +24,25 @@ export class W3cVcModule implements Module { dependencyManager.registerInstance(SignatureSuiteToken, { suiteClass: Ed25519Signature2018, proofType: 'Ed25519Signature2018', - requiredKeyType: 'Ed25519VerificationKey2018', - keyType: KeyType.Ed25519, + verificationMethodTypes: [ + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018, + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020, + ], + keyTypes: [KeyType.Ed25519], }) // This will be moved out of core into the bbs module dependencyManager.registerInstance(SignatureSuiteToken, { suiteClass: BbsBlsSignature2020, proofType: 'BbsBlsSignature2020', - requiredKeyType: 'BbsBlsSignatureProof2020', - keyType: KeyType.Bls12381g2, + verificationMethodTypes: [VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020], + keyTypes: [KeyType.Bls12381g2], }) dependencyManager.registerInstance(SignatureSuiteToken, { suiteClass: BbsBlsSignatureProof2020, proofType: 'BbsBlsSignatureProof2020', - requiredKeyType: 'BbsBlsSignatureProof2020', - keyType: KeyType.Bls12381g2, + verificationMethodTypes: [VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020], + keyTypes: [KeyType.Bls12381g2], }) } } diff --git a/packages/core/src/modules/vc/__tests__/W3cCredentialService.test.ts b/packages/core/src/modules/vc/__tests__/W3cCredentialService.test.ts index beccb899ef..6f81ce43c7 100644 --- a/packages/core/src/modules/vc/__tests__/W3cCredentialService.test.ts +++ b/packages/core/src/modules/vc/__tests__/W3cCredentialService.test.ts @@ -8,6 +8,11 @@ import { JsonTransformer } from '../../../utils/JsonTransformer' import { IndyWallet } from '../../../wallet/IndyWallet' import { WalletError } from '../../../wallet/error' import { DidKey, DidResolverService } from '../../dids' +import { VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020 } from '../../dids/domain/key-type/bls12381g2' +import { + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018, + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020, +} from '../../dids/domain/key-type/ed25519' import { SignatureSuiteRegistry } from '../SignatureSuiteRegistry' import { W3cCredentialService } from '../W3cCredentialService' import { orArrayToArray } from '../jsonldUtil' @@ -29,20 +34,24 @@ const signatureSuiteRegistry = new SignatureSuiteRegistry([ { suiteClass: Ed25519Signature2018, proofType: 'Ed25519Signature2018', - requiredKeyType: 'Ed25519VerificationKey2018', - keyType: KeyType.Ed25519, + + verificationMethodTypes: [ + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018, + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020, + ], + keyTypes: [KeyType.Ed25519], }, { suiteClass: BbsBlsSignature2020, proofType: 'BbsBlsSignature2020', - requiredKeyType: 'BbsBlsSignatureProof2020', - keyType: KeyType.Bls12381g2, + verificationMethodTypes: [VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020], + keyTypes: [KeyType.Bls12381g2], }, { suiteClass: BbsBlsSignatureProof2020, proofType: 'BbsBlsSignatureProof2020', - requiredKeyType: 'BbsBlsSignatureProof2020', - keyType: KeyType.Bls12381g2, + verificationMethodTypes: [VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020], + keyTypes: [KeyType.Bls12381g2], }, ]) @@ -94,6 +103,44 @@ describe('W3cCredentialService', () => { await wallet.delete() }) + describe('Utility methods', () => { + describe('getKeyTypesByProofType', () => { + it('should return the correct key types for Ed25519Signature2018 proof type', async () => { + const keyTypes = w3cCredentialService.getKeyTypesByProofType('Ed25519Signature2018') + expect(keyTypes).toEqual([KeyType.Ed25519]) + }) + it('should return the correct key types for BbsBlsSignature2020 proof type', async () => { + const keyTypes = w3cCredentialService.getKeyTypesByProofType('BbsBlsSignature2020') + expect(keyTypes).toEqual([KeyType.Bls12381g2]) + }) + it('should return the correct key types for BbsBlsSignatureProof2020 proof type', async () => { + const keyTypes = w3cCredentialService.getKeyTypesByProofType('BbsBlsSignatureProof2020') + expect(keyTypes).toEqual([KeyType.Bls12381g2]) + }) + }) + + describe('getVerificationMethodTypesByProofType', () => { + it('should return the correct key types for Ed25519Signature2018 proof type', async () => { + const verificationMethodTypes = + w3cCredentialService.getVerificationMethodTypesByProofType('Ed25519Signature2018') + expect(verificationMethodTypes).toEqual([ + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018, + VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020, + ]) + }) + it('should return the correct key types for BbsBlsSignature2020 proof type', async () => { + const verificationMethodTypes = + w3cCredentialService.getVerificationMethodTypesByProofType('BbsBlsSignature2020') + expect(verificationMethodTypes).toEqual([VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020]) + }) + it('should return the correct key types for BbsBlsSignatureProof2020 proof type', async () => { + const verificationMethodTypes = + w3cCredentialService.getVerificationMethodTypesByProofType('BbsBlsSignatureProof2020') + expect(verificationMethodTypes).toEqual([VERIFICATION_METHOD_TYPE_BLS12381G2_KEY_2020]) + }) + }) + }) + describe('Ed25519Signature2018', () => { let issuerDidKey: DidKey let verificationMethod: string diff --git a/packages/core/src/modules/vc/__tests__/W3cVcModule.test.ts b/packages/core/src/modules/vc/__tests__/W3cVcModule.test.ts index e60807c1b6..d48d1672e6 100644 --- a/packages/core/src/modules/vc/__tests__/W3cVcModule.test.ts +++ b/packages/core/src/modules/vc/__tests__/W3cVcModule.test.ts @@ -24,23 +24,23 @@ describe('W3cVcModule', () => { expect(dependencyManager.registerInstance).toHaveBeenCalledTimes(3) expect(dependencyManager.registerInstance).toHaveBeenCalledWith(SignatureSuiteToken, { suiteClass: Ed25519Signature2018, + verificationMethodTypes: ['Ed25519VerificationKey2018', 'Ed25519VerificationKey2020'], proofType: 'Ed25519Signature2018', - requiredKeyType: 'Ed25519VerificationKey2018', - keyType: KeyType.Ed25519, + keyTypes: [KeyType.Ed25519], }) expect(dependencyManager.registerInstance).toHaveBeenCalledWith(SignatureSuiteToken, { suiteClass: BbsBlsSignature2020, + verificationMethodTypes: ['Bls12381G2Key2020'], proofType: 'BbsBlsSignature2020', - requiredKeyType: 'BbsBlsSignatureProof2020', - keyType: KeyType.Bls12381g2, + keyTypes: [KeyType.Bls12381g2], }) expect(dependencyManager.registerInstance).toHaveBeenCalledWith(SignatureSuiteToken, { suiteClass: BbsBlsSignatureProof2020, proofType: 'BbsBlsSignatureProof2020', - requiredKeyType: 'BbsBlsSignatureProof2020', - keyType: KeyType.Bls12381g2, + verificationMethodTypes: ['Bls12381G2Key2020'], + keyTypes: [KeyType.Bls12381g2], }) }) })