Skip to content

Commit

Permalink
feat: add Multikey as supported vm type (#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbluhm committed Jan 31, 2024
1 parent 11d7096 commit 5562cb1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('ed25519', () => {
'Ed25519VerificationKey2018',
'Ed25519VerificationKey2020',
'JsonWebKey2020',
'Multikey',
])
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ describe('x25519', () => {
})

it('supports X25519KeyAgreementKey2019 verification method type', () => {
expect(keyDidX25519.supportedVerificationMethodTypes).toMatchObject(['X25519KeyAgreementKey2019', 'JsonWebKey2020'])
expect(keyDidX25519.supportedVerificationMethodTypes).toMatchObject([
'X25519KeyAgreementKey2019',
'JsonWebKey2020',
'Multikey',
])
})

it('returns key for X25519KeyAgreementKey2019 verification method', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/modules/dids/domain/key-type/ed25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import {
isEd25519VerificationKey2020,
isJsonWebKey2020,
getEd25519VerificationKey2018,
getKeyFromMultikey,
isMultikey,
VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018,
VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020,
VERIFICATION_METHOD_TYPE_JSON_WEB_KEY_2020,
VERIFICATION_METHOD_TYPE_MULTIKEY,
} from '../verificationMethod'

export { convertPublicKeyToX25519 } from '@stablelib/ed25519'
Expand All @@ -23,6 +26,7 @@ export const keyDidEd25519: KeyDidMapping = {
VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2018,
VERIFICATION_METHOD_TYPE_ED25519_VERIFICATION_KEY_2020,
VERIFICATION_METHOD_TYPE_JSON_WEB_KEY_2020,
VERIFICATION_METHOD_TYPE_MULTIKEY,
],
getVerificationMethods: (did, key) => [
getEd25519VerificationKey2018({ id: `${did}#${key.fingerprint}`, key, controller: did }),
Expand All @@ -40,6 +44,10 @@ export const keyDidEd25519: KeyDidMapping = {
return getKeyFromJsonWebKey2020(verificationMethod)
}

if (isMultikey(verificationMethod)) {
return getKeyFromMultikey(verificationMethod)
}

throw new AriesFrameworkError(
`Verification method with type '${verificationMethod.type}' not supported for key type '${KeyType.Ed25519}'`
)
Expand Down
19 changes: 17 additions & 2 deletions packages/core/src/modules/dids/domain/key-type/keyDidMapping.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { Key } from '../../../../crypto/Key'
import type { VerificationMethod } from '../verificationMethod'

import { KeyType } from '../../../../crypto/KeyType'
import { getJwkFromJson } from '../../../../crypto/jose/jwk'
import { AriesFrameworkError } from '../../../../error'
import {
VERIFICATION_METHOD_TYPE_MULTIKEY,
isMultikey,
type VerificationMethod,
getKeyFromMultikey,
} from '../verificationMethod'
import { isJsonWebKey2020, VERIFICATION_METHOD_TYPE_JSON_WEB_KEY_2020 } from '../verificationMethod/JsonWebKey2020'

import { keyDidBls12381g1 } from './bls12381g1'
Expand Down Expand Up @@ -67,7 +72,7 @@ export function getKeyDidMappingByKeyType(keyType: KeyType) {
return keyDid
}

export function getKeyFromVerificationMethod(verificationMethod: VerificationMethod) {
export function getKeyFromVerificationMethod(verificationMethod: VerificationMethod): Key {
// This is a special verification method, as it supports basically all key types.
if (isJsonWebKey2020(verificationMethod)) {
// TODO: move this validation to another place
Expand All @@ -80,6 +85,16 @@ export function getKeyFromVerificationMethod(verificationMethod: VerificationMet
return getJwkFromJson(verificationMethod.publicKeyJwk).key
}

if (isMultikey(verificationMethod)) {
if (!verificationMethod.publicKeyMultibase) {
throw new AriesFrameworkError(
`Missing publicKeyMultibase on verification method with type ${VERIFICATION_METHOD_TYPE_MULTIKEY}`
)
}

return getKeyFromMultikey(verificationMethod)
}

const keyDid = verificationMethodKeyDidMapping[verificationMethod.type]
if (!keyDid) {
throw new AriesFrameworkError(`Unsupported key did from verification method type '${verificationMethod.type}'`)
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/modules/dids/domain/key-type/x25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import {
getKeyFromJsonWebKey2020,
isJsonWebKey2020,
VERIFICATION_METHOD_TYPE_JSON_WEB_KEY_2020,
VERIFICATION_METHOD_TYPE_MULTIKEY,
isMultikey,
getKeyFromMultikey,
} from '../verificationMethod'

export const keyDidX25519: KeyDidMapping = {
supportedVerificationMethodTypes: [
VERIFICATION_METHOD_TYPE_X25519_KEY_AGREEMENT_KEY_2019,
VERIFICATION_METHOD_TYPE_JSON_WEB_KEY_2020,
VERIFICATION_METHOD_TYPE_MULTIKEY,
],
getVerificationMethods: (did, key) => [
getX25519KeyAgreementKey2019({ id: `${did}#${key.fingerprint}`, key, controller: did }),
Expand All @@ -30,6 +34,10 @@ export const keyDidX25519: KeyDidMapping = {
return getKeyFromX25519KeyAgreementKey2019(verificationMethod)
}

if (isMultikey(verificationMethod)) {
return getKeyFromMultikey(verificationMethod)
}

throw new AriesFrameworkError(
`Verification method with type '${verificationMethod.type}' not supported for key type '${KeyType.X25519}'`
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { VerificationMethod } from './VerificationMethod'

import { Key } from '../../../../crypto/Key'
import { AriesFrameworkError } from '../../../../error'

export const VERIFICATION_METHOD_TYPE_MULTIKEY = 'Multikey'

type GetMultikeyOptions = {
did: string
key: Key
verificationMethodId?: string
}

/**
* Get a Multikey verification method.
*/
export function getMultikey({ did, key, verificationMethodId }: GetMultikeyOptions) {
if (!verificationMethodId) {
verificationMethodId = `${did}#${key.fingerprint}`
}

return {
id: verificationMethodId,
type: VERIFICATION_METHOD_TYPE_MULTIKEY,
controller: did,
publicKeyMultibase: key.fingerprint,
}
}

/**
* Check whether a verification method is a Multikey verification method.
*/
export function isMultikey(
verificationMethod: VerificationMethod
): verificationMethod is VerificationMethod & { type: 'Multikey' } {
return verificationMethod.type === VERIFICATION_METHOD_TYPE_MULTIKEY
}

/**
* Get a key from a Multikey verification method.
*/
export function getKeyFromMultikey(verificationMethod: VerificationMethod & { type: 'Multikey' }) {
if (!verificationMethod.publicKeyMultibase) {
throw new AriesFrameworkError(
`Missing publicKeyMultibase on verification method with type ${VERIFICATION_METHOD_TYPE_MULTIKEY}`
)
}

return Key.fromFingerprint(verificationMethod.publicKeyMultibase)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './Ed25519VerificationKey2018'
export * from './Ed25519VerificationKey2020'
export * from './JsonWebKey2020'
export * from './X25519KeyAgreementKey2019'
export * from './Multikey'

0 comments on commit 5562cb1

Please sign in to comment.