Skip to content

Commit

Permalink
fix(did-comm): compare compressed keys for secp256k1
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceanis committed Nov 11, 2021
1 parent 6b503a6 commit c6c28c8
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
2 changes: 1 addition & 1 deletion __tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ let dbConnection: Promise<Connection>
let databaseFile: string

const setup = async (options?: IAgentOptions): Promise<boolean> => {
databaseFile = options?.context?.databaseFile || 'local-database.sqlite'
databaseFile = options?.context?.databaseFile || `./tmp/local-database-${Math.random().toPrecision(5)}.sqlite`
dbConnection = createConnection({
name: options?.context?.['dbName'] || 'test',
type: 'sqlite',
Expand Down
2 changes: 1 addition & 1 deletion __tests__/localMemoryStoreAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import didManager from './shared/didManager'
import didCommPacking from './shared/didCommPacking'
import messageHandler from './shared/messageHandler'

const databaseFile = 'local-database2.sqlite'
const databaseFile = `./tmp/local-database2-${Math.random().toPrecision(5)}.sqlite`
const infuraProjectId = '3586660d179141e3801c3895de1c2eba'

let agent: TAgent<
Expand Down
2 changes: 1 addition & 1 deletion __tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import didWithFakeDidFlow from './shared/didCommWithFakeDidFlow'
import messageHandler from './shared/messageHandler'
import didDiscovery from './shared/didDiscovery'

const databaseFile = 'rest-database.sqlite'
const databaseFile = `./tmp/rest-database-${Math.random().toPrecision(5)}.sqlite`
const infuraProjectId = '3586660d179141e3801c3895de1c2eba'
const secretKey = '29739248cad1bd1a0fc4d9b75cd4d2990de535baf5caadfdf8d8f86664aa830c'
const port = 3002
Expand Down
37 changes: 24 additions & 13 deletions __tests__/shared/didCommWithEthrDidFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export default (testContext: {
})

afterAll(async () => {
await new Promise((resolve, reject) => didCommEndpointServer?.close(resolve))
try {
await new Promise((resolve, reject) => didCommEndpointServer?.close(resolve))
} catch (e) {
//nop
}
testContext.tearDown()
})

Expand All @@ -108,7 +112,9 @@ export default (testContext: {

const resolution = await agent.resolveDid({ didUrl: alice.did })

expect(resolution?.didDocument?.service?.[0].serviceEndpoint).toEqual(`http://localhost:${listeningPort}/foobar`)
expect(resolution?.didDocument?.service?.[0].serviceEndpoint).toEqual(
`http://localhost:${listeningPort}/foobar`,
)
})

it('should remove service from identifier', async () => {
Expand Down Expand Up @@ -168,7 +174,9 @@ export default (testContext: {

const resolution = await agent.resolveDid({ didUrl: alice.did })

expect(resolution?.didDocument?.service?.[0].serviceEndpoint).toEqual(`http://localhost:${listeningPort}/messaging`)
expect(resolution?.didDocument?.service?.[0].serviceEndpoint).toEqual(
`http://localhost:${listeningPort}/messaging`,
)
})

it('should add encryption key to receiver DID', async () => {
Expand All @@ -186,10 +194,12 @@ export default (testContext: {
const resolution = await agent.resolveDid({ didUrl: alice.did })
const expectedBase58Key = u8a.toString(u8a.fromString(newKey.publicKeyHex, 'base16'), 'base58btc')
expect(resolution?.didDocument?.verificationMethod?.[2].publicKeyBase58).toEqual(expectedBase58Key)
expect(resolution?.didDocument?.keyAgreement?.[0]).toEqual(resolution?.didDocument?.verificationMethod?.[2].id)
expect(resolution?.didDocument?.keyAgreement?.[0]).toEqual(
resolution?.didDocument?.verificationMethod?.[2].id,
)
})

it.skip('should send an anoncrypt message from bob to alice', async () => {
it('should send an anoncrypt message from bob to alice', async () => {
expect.assertions(3)

const message = {
Expand Down Expand Up @@ -233,8 +243,8 @@ export default (testContext: {
)
})

it.skip('should fail to send jws message from alice to bob', async () => {
expect.assertions(3)
it('should fail to send jws message from alice to bob', async () => {
expect.assertions(1)

const message = {
type: 'test',
Expand All @@ -247,12 +257,13 @@ export default (testContext: {
packing: 'jws',
message,
})
expect(agent.sendDIDCommMessage({
messageId: '456',
packedMessage,
recipientDidUrl: bob.did,
})).rejects.toThrowError(/^Error: Failed to send message/)

await expect(
agent.sendDIDCommMessage({
messageId: '456',
packedMessage,
recipientDidUrl: bob.did,
}),
).rejects.toThrowError(/^not_found: could not find DIDComm Messaging service in DID document for/)
})
})
}
4 changes: 2 additions & 2 deletions packages/data-store/src/__tests__/data-store-orm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ async function populateDB(agent: TAgent<IDataStore & IDataStoreORM>) {
await agent.dataStoreSaveMessage({ message: m4 })
}

describe('@veramo/data-store entities', () => {
describe('@veramo/data-store queries', () => {
let dbConnection: Promise<Connection>
const databaseFile = './test-db2.sqlite'
const databaseFile = './tmp/test-db2.sqlite'

function makeAgent(context?: Record<string, any>): TAgent<IDataStore & IDataStoreORM> {
//@ts-ignore
Expand Down
4 changes: 2 additions & 2 deletions packages/data-store/src/__tests__/entities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { Entities } from '../index'
import { blake2bHex } from 'blakejs'
import fs from 'fs'

describe('veramo core', () => {
describe('DB entities test', () => {
let connection: Connection
const databaseFile = './test-db.sqlite'
const databaseFile = './tmp/test-db.sqlite'

beforeAll(
async () =>
Expand Down
1 change: 1 addition & 0 deletions packages/did-comm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}
},
"dependencies": {
"@ethersproject/signing-key": "^5.5.0",
"@stablelib/ed25519": "^1.0.2",
"@veramo/core": "^3.0.0",
"@veramo/message-handler": "^3.0.0",
Expand Down
21 changes: 20 additions & 1 deletion packages/did-comm/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { convertPublicKeyToX25519, convertSecretKeyToX25519 } from '@stablelib/ed25519'
import { computePublicKey } from '@ethersproject/signing-key'
import {
DIDDocumentSection,
IAgentContext,
Expand Down Expand Up @@ -139,6 +140,19 @@ export function convertIdentifierEncryptionKeys(identifier: IIdentifier): IKey[]
.filter(isDefined)
}

export function compressIdentifierSecp256k1Keys(identifier: IIdentifier): IKey[] {
return identifier.keys
.map((key) => {
if (key.type === 'Secp256k1') {
const publicBytes = u8a.fromString(key.publicKeyHex, 'base16')
const compressedKey = computePublicKey(publicBytes, true).substring(2)
key.publicKeyHex = compressedKey
}
return key
})
.filter(isDefined)
}

export async function mapRecipientsToLocalKeys(
managedKeys: { recipient: any; kid: string; identifier: IIdentifier }[],
context: IAgentContext<IResolver>,
Expand Down Expand Up @@ -176,6 +190,8 @@ export async function mapIdentifierKeysToDoc(
let localKeys = identifier.keys.filter(isDefined)
if (section === 'keyAgreement') {
localKeys = convertIdentifierEncryptionKeys(identifier)
} else {
localKeys = compressIdentifierSecp256k1Keys(identifier)
}
// finally map the didDocument keys to the identifier keys by comparing `publicKeyHex`
const extendedKeys: _ExtendedIKey[] = keyAgreementKeys
Expand All @@ -193,7 +209,10 @@ export async function mapIdentifierKeysToDoc(
return extendedKeys
}

export async function resolveDidOrThrow(didUrl: string, context: IAgentContext<IResolver>): Promise<DIDDocument> {
export async function resolveDidOrThrow(
didUrl: string,
context: IAgentContext<IResolver>,
): Promise<DIDDocument> {
// TODO: add caching
const docResult = await context.agent.resolveDid({ didUrl: didUrl })
const err = docResult?.didResolutionMetadata?.error
Expand Down

0 comments on commit c6c28c8

Please sign in to comment.