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

fix(utils): correctly extract publicKeyHex from [Ed/X]25519 2020 keys #1076

Merged
merged 3 commits into from
Nov 28, 2022
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
220 changes: 220 additions & 0 deletions packages/did-comm/src/__tests__/packing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import { DIDComm } from "../didcomm"
import {
createAgent,
IDIDManager,
IIdentifier,
IKeyManager,
IResolver,
TAgent,
} from '../../../core/src'
import { DIDManager, MemoryDIDStore } from '../../../did-manager/src'
import { KeyManager, MemoryKeyStore, MemoryPrivateKeyStore } from '../../../key-manager/src'
import { KeyManagementSystem } from '../../../kms-local/src'
import { getDidKeyResolver, KeyDIDProvider } from '../../../did-provider-key/src'
import { DIDResolverPlugin, getUniversalResolver } from '../../../did-resolver/src'
import { DIDDocument, Resolver } from 'did-resolver'

const multiBaseDoc = {
"@context": [
"https://www.w3.org/ns/did/v1",
"https://w3id.org/security/suites/jws-2020/v1"
],
"controller": "did:web:portcullis.1keep.com",
"id": "did:web:portcullis.1keep.com",
"keyAgreement": [
"did:web:portcullis.1keep.com#1"
],
"service": [
{
"accept": [
"didcomm/v2"
],
"id": "#didcomm",
"routingKeys": [],
"serviceEndpoint": "https://portcullis.1keep.com/didcomm",
"type": "DIDCommMessaging"
}
],
"verificationMethod": [
{
"controller": "did:web:portcullis.1keep.com",
"id": "did:web:portcullis.1keep.com#0",
"publicKeyMultibase": "z8FRmkyRH9xAsLCk51yXN2Qy6uq4eN4iAesa3v3Hv889v",
"type": "Ed25519VerificationKey2020"
},
{
"controller": "did:web:portcullis.1keep.com",
"id": "did:web:portcullis.1keep.com#1",
"publicKeyMultibase": "z7bNjXnt3EXhpy7UdzZUANa2CsBFpzKGFXsgvkBd9iHGo",
"type": "X25519KeyAgreementKey2020"
}
]
}

const base58Doc = {
"@context": [
"https://www.w3.org/ns/did/v1",
"https://w3id.org/security/suites/jws-2020/v1"
],
"authentication": [
"did:web:verifiable.ink#0"
],
"controller": "did:web:verifiable.ink",
"id": "did:web:verifiable.ink",
"keyAgreement": [
"did:web:verifiable.ink#1"
],
"service": [
{
"accept": [
"didcomm/v2"
],
"id": "did:web:verifiable.ink#didcomm",
"routingKeys": [],
"serviceEndpoint": "https://verifiable.ink/didcomm",
"type": "DIDCommMessaging"
}
],
"verificationMethod": [
{
"controller": "did:web:verifiable.ink",
"id": "did:web:verifiable.ink#0",
"publicKeyBase58": "JDTRH3N5xMFxRnDzn4NdZc1NPUm9w9Y4ZSehnscCKqBw",
"type": "Ed25519VerificationKey2018"
},
{
"controller": "did:web:verifiable.ink",
"id": "did:web:verifiable.ink#1",
"publicKeyBase58": "Djiwiqccx8kupbtbkMYioj3F4eL9GUcsPJ9LUmNJFN7n",
"type": "X25519KeyAgreementKey2019"
}
]
}

const hexDoc = {
"@context": "https://w3id.org/did/v1",
"id": "did:web:iiw-demo.herokuapp.com",
"verificationMethod": [
{
"id": "did:web:iiw-demo.herokuapp.com#6eb094077b42b299c8ab5e1981e3dba7e9e331c26acdde4c6bd434dcb4bf856e",
"type": "Ed25519VerificationKey2018",
"controller": "did:web:iiw-demo.herokuapp.com",
"publicKeyHex": "6eb094077b42b299c8ab5e1981e3dba7e9e331c26acdde4c6bd434dcb4bf856e"
},
{
"id": "did:web:iiw-demo.herokuapp.com#6e5db42d8d42bca9eee16fef45d41cd3cfaa52fa726ade05055f7d09dcfbf669",
"type": "X25519KeyAgreementKey2019",
"controller": "did:web:iiw-demo.herokuapp.com",
"publicKeyHex": "6e5db42d8d42bca9eee16fef45d41cd3cfaa52fa726ade05055f7d09dcfbf669"
}
],
"authentication": [
"did:web:iiw-demo.herokuapp.com#6eb094077b42b299c8ab5e1981e3dba7e9e331c26acdde4c6bd434dcb4bf856e"
],
"assertionMethod": [
"did:web:iiw-demo.herokuapp.com#6eb094077b42b299c8ab5e1981e3dba7e9e331c26acdde4c6bd434dcb4bf856e"
],
"keyAgreement": [
"did:web:iiw-demo.herokuapp.com#6eb094077b42b299c8ab5e1981e3dba7e9e331c26acdde4c6bd434dcb4bf856e",
"did:web:iiw-demo.herokuapp.com#6e5db42d8d42bca9eee16fef45d41cd3cfaa52fa726ade05055f7d09dcfbf669"
],
"service": [
{
"id": "did:web:iiw-demo.herokuapp.com#msg",
"type": "Messaging",
"serviceEndpoint": "https://iiw-demo.herokuapp.com/messaging",
"description": "Handles incoming POST messages"
},
{
"id": "did:web:iiw-demo.herokuapp.com#msg-didcomm",
"type": "DIDCommMessaging",
"serviceEndpoint": "https://iiw-demo.herokuapp.com/messaging",
"description": "Handles incoming DIDComm messages"
}
]
}

describe('didComm', () => {
let didKeyIdentifier: IIdentifier
let agent: TAgent<IResolver & IKeyManager & IDIDManager>

beforeAll(async () => {
agent = createAgent({
plugins: [
new KeyManager({
store: new MemoryKeyStore(),
kms: {
local: new KeyManagementSystem(new MemoryPrivateKeyStore()),
},
}),
new DIDManager({
providers: {
'did:key': new KeyDIDProvider({ defaultKms: 'local' }),
},
store: new MemoryDIDStore(),
defaultProvider: 'did:key',
}),
new DIDResolverPlugin({
resolver: new Resolver({
...getDidKeyResolver(),
'fake': async (did: string) => {
let doc: DIDDocument
if (did === "did:fake:base58") {
doc = base58Doc
} else if (did === "did:fake:multibase") {
doc = multiBaseDoc
} else if (did === "did:fake:hex") {
doc = hexDoc
} else {
throw new Error("Bad didUrl for fake resolver: " + did)
}

// DIDResolutionResult
return {
didResolutionMetadata: {},
didDocument: doc,
didDocumentMetadata: {}
}
}
})
}),
new DIDComm()
],
})
didKeyIdentifier = await agent.didManagerCreate()
})

const testMessage = (receiverDID: string) => {
return {
type: 'test',
from: didKeyIdentifier.did,
to: receiverDID,
id: 'test',
body: { hello: 'world' },
}
}

it('should pack message for public key as base58', async () => {
const packedMessage = await agent.packDIDCommMessage({
message: testMessage("did:fake:base58"),
packing: 'authcrypt'
})
expect(packedMessage).toBeDefined()
})

it('should pack message for public key as multibase', async () => {
const packedMessage = await agent.packDIDCommMessage({
message: testMessage("did:fake:multibase"),
packing: 'authcrypt'
})
expect(packedMessage).toBeDefined()
})

it('should pack message for public key as hex', async () => {
const packedMessage = await agent.packDIDCommMessage({
message: testMessage("did:fake:hex"),
packing: 'authcrypt'
})
expect(packedMessage).toBeDefined()
})
})
4 changes: 2 additions & 2 deletions packages/utils/src/did-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ export async function dereferenceDidKeys(
export function extractPublicKeyHex(pk: _ExtendedVerificationMethod, convert: boolean = false): string {
let keyBytes = extractPublicKeyBytes(pk)
if (convert) {
if (['Ed25519', 'Ed25519VerificationKey2018'].includes(pk.type)) {
if (['Ed25519', 'Ed25519VerificationKey2018', 'Ed25519VerificationKey2020'].includes(pk.type)) {
keyBytes = convertPublicKeyToX25519(keyBytes)
} else if (!['X25519', 'X25519KeyAgreementKey2019'].includes(pk.type)) {
} else if (!['X25519', 'X25519KeyAgreementKey2019', 'X25519KeyAgreementKey2020'].includes(pk.type)) {
return ''
}
}
Expand Down