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

feat: add Multikey as supported vm type #1720

Merged
merged 4 commits into from
Jan 31, 2024
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
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have this in keyDidMapping (a higher level), so i don't know when it'll reach this, but i see we have the same duplication for JsonWebKey2020 so it's okay for now.

We can refactor it a bit later on

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fingerprint was supposed to give a fingerprint of the key, and it also is a multibase encoding. Might be good to add a toMultibase and fromMultibase options and also support e.g. base64 encoding as described in the di spec. But fine for now

}
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'
Loading