Skip to content

Commit

Permalink
feat(selective-disclosure): support multiple key types for generating…
Browse files Browse the repository at this point in the history
… requests

fixes #946
  • Loading branch information
mirceanis committed Aug 4, 2023
1 parent ecdb2a2 commit 723e120
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 6 deletions.
81 changes: 80 additions & 1 deletion __tests__/shared/handleSdrMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default (testContext: {
afterAll(testContext.tearDown)

it('should create identifier', async () => {
identifier = await agent.didManagerCreate({ kms: 'local', provider: 'did:ethr' })
identifier = await agent.didManagerCreate({ kms: 'local', provider: 'did:key' })
expect(identifier).toHaveProperty('did')
})

Expand Down Expand Up @@ -82,6 +82,85 @@ export default (testContext: {
expect(message.raw).toEqual(JWT)
})

it('should create and handle an SDR message with Ed25519', async () => {
const sdrIssuer = await agent.didManagerCreate({ provider: 'did:key', options: { keyType: 'Ed25519' } })
const req = await agent.createSelectiveDisclosureRequest({
data: {
issuer: sdrIssuer.did,
tag: 'sdr-one',
claims: [
{
reason: 'We need it',
claimType: 'name',
essential: true,
},
],
},
})

const message = await agent.handleMessage({
raw: req,
save: false,
})

expect(message.raw).toEqual(req)
})

it('should create and handle an SDR message with Secp256k1', async () => {
const sdrIssuer = await agent.didManagerCreate({
provider: 'did:ethr',
})
const req = await agent.createSelectiveDisclosureRequest({
data: {
issuer: sdrIssuer.did,
tag: 'sdr-one',
claims: [
{
reason: 'We need it',
claimType: 'name',
essential: true,
},
],
},
})

const message = await agent.handleMessage({
raw: req,
save: false,
})

expect(message.raw).toEqual(req)
})

it('should create and handle an SDR message with Secp256r1', async () => {
const sdrIssuer = await agent.didManagerCreate({
provider: 'did:jwk',
options: {
keyType: 'Secp256r1',
}
})
const req = await agent.createSelectiveDisclosureRequest({
data: {
issuer: sdrIssuer.did,
tag: 'sdr-one',
claims: [
{
reason: 'We need it',
claimType: 'name',
essential: true,
},
],
},
})

const message = await agent.handleMessage({
raw: req,
save: false,
})

expect(message.raw).toEqual(req)
})

it('should be able to find the request message', async () => {
const messages = await agent.dataStoreORMGetMessages()

Expand Down
40 changes: 35 additions & 5 deletions packages/selective-disclosure/src/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IKey,
IKeyManager,
TClaimsColumns,
TKeyType,
VerifiableCredential,
VerifiablePresentation,
} from '@veramo/core-types'
Expand All @@ -25,7 +26,22 @@ import {
import schema from './plugin.schema.json' assert { type: 'json' }
import { createJWT } from 'did-jwt'
import Debug from 'debug'
import { asArray, bytesToBase64, computeEntryHash, decodeCredentialToObject, extractIssuer, } from '@veramo/utils'
import {
asArray,
bytesToBase64,
computeEntryHash,
decodeCredentialToObject,
extractIssuer,
} from '@veramo/utils'

const KEY_ALG_MAPPING: Record<TKeyType, string | null> = {
Secp256k1: 'ES256K',
Secp256r1: 'ES256',
Ed25519: 'EdDSA',
X25519: null,
Bls12381G1: null,
Bls12381G2: null,
} as const

/**
* This class adds support for creating
Expand Down Expand Up @@ -73,8 +89,20 @@ export class SelectiveDisclosure implements IAgentPlugin {
delete data.issuer
Debug('veramo:selective-disclosure:create-sdr')('Signing SDR with', identifier.did)

const key = identifier.keys.find((k: IKey) => k.type === 'Secp256k1')
const key = identifier.keys.find((k: IKey) => {
return (
Object.keys(KEY_ALG_MAPPING).includes(k.type) &&
KEY_ALG_MAPPING[k.type] &&
k.meta?.algorithms?.includes(KEY_ALG_MAPPING[k.type] ?? 'unsupported')
)
})

if (!key) throw Error('Signing key not found')

const algorithm = KEY_ALG_MAPPING[key?.type ?? '']

if (!algorithm) throw Error('Unsupported key type')

const signer = (data: string | Uint8Array) => {
let dataString, encoding: 'base64' | undefined
if (typeof data === 'string') {
Expand All @@ -83,7 +111,7 @@ export class SelectiveDisclosure implements IAgentPlugin {
} else {
;(dataString = bytesToBase64(data)), (encoding = 'base64')
}
return context.agent.keyManagerSign({ keyRef: key.kid, data: dataString, encoding })
return context.agent.keyManagerSign({ keyRef: key.kid, data: dataString, encoding, algorithm })
}
const jwt = await createJWT(
{
Expand All @@ -92,7 +120,7 @@ export class SelectiveDisclosure implements IAgentPlugin {
},
{
signer,
alg: 'ES256K',
alg: algorithm,
issuer: identifier.did,
},
)
Expand Down Expand Up @@ -198,7 +226,9 @@ export class SelectiveDisclosure implements IAgentPlugin {

if (
credentialRequest.issuers &&
!credentialRequest.issuers.map((i) => i.did).includes(extractIssuer(credential, { removeParameters: true }))
!credentialRequest.issuers
.map((i) => i.did)
.includes(extractIssuer(credential, { removeParameters: true }))
) {
return false
}
Expand Down

0 comments on commit 723e120

Please sign in to comment.