Skip to content

Commit

Permalink
Use keyRef if supplied to pick signing key in `createVerifiablePres…
Browse files Browse the repository at this point in the history
…entation` and `createVerifiableCredential`
  • Loading branch information
jasny committed Jul 20, 2023
1 parent 6981e68 commit eff1863
Showing 1 changed file with 16 additions and 6 deletions.
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

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
}

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

0 comments on commit eff1863

Please sign in to comment.