Skip to content

Commit

Permalink
Allow picking a key when creating credential.
Browse files Browse the repository at this point in the history
Use `keyRef` if supplied to pick signing key in `createVerifiablePresentation` and `createVerifiableCredential`
  • Loading branch information
jasny committed Jul 20, 2023
1 parent 6981e68 commit 178df94
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
25 changes: 23 additions & 2 deletions packages/credential-w3c/src/__tests__/action-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ const mockIdentifiers: IIdentifier[] = [
controllerKeyId: 'kid2',
keys: [
{
kid: 'kid2',
kid: 'kid2a',
publicKeyHex: 'pub',
type: 'Ed25519',
kms: 'mock',
},
{
kid: 'kid2b',
publicKeyHex: 'pub',
type: 'Secp256k1',
kms: 'mock',
Expand Down Expand Up @@ -98,8 +104,18 @@ let agent = {
} as any as TAgent<IResolver & IDIDManager & IKeyManager & ICredentialPlugin & IDataStore>

describe('@veramo/credential-w3c', () => {
const keyManagerSign = agent.keyManagerSign as
| jest.Mock<(args: { algorithm: string; keyRef: string}) => Promise<string>>

beforeEach(() => {
keyManagerSign.mockClear()
});

test.each(mockIdentifiers)('handles createVerifiableCredential', async (mockIdentifier) => {
expect.assertions(3)
expect.assertions(6)

const keyRef = mockIdentifier.keys[1]?.kid // Second key or undefined
const expectedKey = mockIdentifier.keys[mockIdentifier.keys.length - 1]

agent.didManagerGet = jest.fn(async (args): Promise<IIdentifier> => mockIdentifier)
const context = { agent }
Expand All @@ -126,13 +142,18 @@ describe('@veramo/credential-w3c', () => {
credential,
save: false,
proofFormat: 'jwt',
keyRef,
},
context,
)
// TODO Update these after refactoring did-jwt-vc
expect(context.agent.didManagerGet).toBeCalledWith({ did: mockIdentifier.did })
expect(context.agent.dataStoreSaveVerifiableCredential).not.toBeCalled()
expect(vc.id).toEqual('vc1')

expect(keyManagerSign).toBeCalled()
expect(keyManagerSign.mock.calls[0][0].keyRef).toEqual(expectedKey.kid)
expect(keyManagerSign.mock.calls[0][0].algorithm).toEqual(expectedKey.type === 'Ed25519' ? 'EdDSA' : 'ES256K')
})

test.each(mockIdentifiers)('handles createVerifiablePresentation', async (mockIdentifier) => {
Expand Down
22 changes: 16 additions & 6 deletions packages/credential-w3c/src/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ export class CredentialPlugin implements IAgentPlugin {
} catch (e) {
throw new Error('invalid_argument: presentation.holder must be a DID managed by this agent')
}
//FIXME: `args` should allow picking a key or key type
const key = identifier.keys.find((k) => k.type === 'Secp256k1' || k.type === 'Ed25519' || k.type === 'Secp256r1')
if (!key) throw Error('key_not_found: No signing key for ' + identifier.did)
const key = pickSigningKey(identifier, keyRef)

let verifiablePresentation: VerifiablePresentation

Expand Down Expand Up @@ -237,9 +235,7 @@ export class CredentialPlugin implements IAgentPlugin {
)
}
} else {
//FIXME: `args` should allow picking a key or key type
const key = identifier.keys.find((k) => k.type === 'Secp256k1' || k.type === 'Ed25519' || k.type === 'Secp256r1')
if (!key) throw Error('No signing key for ' + identifier.did)
const key = pickSigningKey(identifier, keyRef)

debug('Signing VC with', identifier.did)
let alg = 'ES256K'
Expand Down Expand Up @@ -480,6 +476,20 @@ export class CredentialPlugin implements IAgentPlugin {
}
}

function pickSigningKey(identifier: IIdentifier, keyRef?: string): IKey {
let key: IKey | undefined

if (!keyRef) {
key = identifier.keys.find((k) => k.type === 'Secp256k1' || k.type === 'Ed25519' || k.type === 'Secp256r1')
if (!key) throw Error('key_not_found: No signing key for ' + identifier.did)
} else {
key = identifier.keys.find((k) => k.kid === keyRef)
if (!key) throw Error('key_not_found: No signing key for ' + identifier.did + ' with kid ' + keyRef)
}

return key as IKey
}

function wrapSigner(
context: IAgentContext<Pick<IKeyManager, 'keyManagerSign'>>,
key: IKey,
Expand Down

0 comments on commit 178df94

Please sign in to comment.