Skip to content

Commit

Permalink
fix(did-comm): avoid double conversion for some keys while packing
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceanis committed Jul 5, 2021
1 parent 4fc587c commit 78321a9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
8 changes: 4 additions & 4 deletions __tests__/shared/didcomm.ts
Expand Up @@ -18,7 +18,7 @@ export default (testContext: {
agent = testContext.getAgent()

sender = await agent.didManagerImport({
did: 'did:key:z6MkgbqNU4uF9NKSz5BqJQ4XKVHuQZYcUZP8pXGsJC8nTHwo',
did: 'did:fake:z6MkgbqNU4uF9NKSz5BqJQ4XKVHuQZYcUZP8pXGsJC8nTHwo',
keys: [
{
type: 'Ed25519',
Expand All @@ -30,12 +30,12 @@ export default (testContext: {
},
],
services: [],
provider: 'did:key',
provider: 'did:fake',
alias: 'sender',
})

receiver = await agent.didManagerImport({
did: 'did:key:z6MkrPhffVLBZpxH7xvKNyD4sRVZeZsNTWJkLdHdgWbfgNu3',
did: 'did:fake:z6MkrPhffVLBZpxH7xvKNyD4sRVZeZsNTWJkLdHdgWbfgNu3',
keys: [
{
type: 'Ed25519',
Expand All @@ -47,7 +47,7 @@ export default (testContext: {
},
],
services: [],
provider: 'did:key',
provider: 'did:fake',
alias: 'receiver',
})
return true
Expand Down
13 changes: 1 addition & 12 deletions packages/did-comm/src/action-handler.ts
Expand Up @@ -276,18 +276,7 @@ export class DIDComm implements IAgentPlugin {

// 2.2 get public key bytes and key IDs for supported recipient keys
const recipients: { kid: string; publicKeyBytes: Uint8Array }[] = keyAgreementKeys
.map((pk) => {
const publicKeyHex = pk.publicKeyHex!
let publicKeyBytes = u8a.fromString(publicKeyHex, 'base16')
if (['Ed25519VerificationKey2018', 'Ed25519'].includes(pk.type)) {
publicKeyBytes = convertPublicKeyToX25519(publicKeyBytes)
} else if (!['X25519KeyAgreementKey2019', 'X25519'].includes(pk.type)) {
// other key agreement keys not supported
return null
}
const kid = pk.id
return { kid, publicKeyBytes }
})
.map((pk) => ({ kid: pk.id, publicKeyBytes: u8a.fromString(pk.publicKeyHex!, 'base16') }))
.filter(isDefined)

// 3. create Encrypter for each recipient
Expand Down
6 changes: 5 additions & 1 deletion packages/did-comm/src/utils.ts
Expand Up @@ -245,7 +245,11 @@ export async function dereferenceDidKeys(
.map((key) => {
const hexKey = convertToPublicKeyHex(key, convert)
const { publicKeyHex, publicKeyBase58, publicKeyBase64, publicKeyJwk, ...keyProps } = key
return { ...keyProps, publicKeyHex: hexKey }
const newKey = { ...keyProps, publicKeyHex: hexKey }
if (convert && 'Ed25519VerificationKey2018' === newKey.type) {
newKey.type = 'X25519KeyAgreementKey2019'
}
return newKey
})
.filter((key) => key.publicKeyHex.length > 0)
}
Expand Down

0 comments on commit 78321a9

Please sign in to comment.