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

feat: implement didcomm v2 packing/unpacking #575

Merged
merged 13 commits into from
Jun 26, 2021
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
2 changes: 2 additions & 0 deletions __tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import saveClaims from './shared/saveClaims'
import documentationExamples from './shared/documentationExamples'
import keyManager from './shared/keyManager'
import didManager from './shared/didManager'
import didComm from './shared/didcomm'
import messageHandler from './shared/messageHandler'

const databaseFile = 'local-database.sqlite'
Expand Down Expand Up @@ -171,4 +172,5 @@ describe('Local integration tests', () => {
keyManager(testContext)
didManager(testContext)
messageHandler(testContext)
didComm(testContext)
})
2 changes: 2 additions & 0 deletions __tests__/localMemoryStoreAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import saveClaims from './shared/saveClaims'
import documentationExamples from './shared/documentationExamples'
import keyManager from './shared/keyManager'
import didManager from './shared/didManager'
import didComm from './shared/didcomm'
import messageHandler from './shared/messageHandler'

const databaseFile = 'local-database2.sqlite'
Expand Down Expand Up @@ -164,4 +165,5 @@ describe('Local in-memory integration tests', () => {
keyManager(testContext)
didManager(testContext)
messageHandler(testContext)
didComm(testContext)
})
2 changes: 2 additions & 0 deletions __tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import webDidFlow from './shared/webDidFlow'
import documentationExamples from './shared/documentationExamples'
import keyManager from './shared/keyManager'
import didManager from './shared/didManager'
import didComm from './shared/didcomm'
import messageHandler from './shared/messageHandler'
import { getUniversalResolver } from '../packages/did-resolver/src/universal-resolver'

Expand Down Expand Up @@ -191,4 +192,5 @@ describe('REST integration tests', () => {
keyManager(testContext)
didManager(testContext)
messageHandler(testContext)
didComm(testContext)
})
133 changes: 133 additions & 0 deletions __tests__/shared/didcomm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { TAgent, IDIDManager, IKeyManager, IIdentifier, IResolver } from '../../packages/core/src'
import { IDIDComm } from '../../packages/did-comm/src'

type ConfiguredAgent = TAgent<IDIDManager & IKeyManager & IResolver & IDIDComm>

export default (testContext: {
getAgent: () => ConfiguredAgent
setup: () => Promise<boolean>
tearDown: () => Promise<boolean>
}) => {
describe('DID comm', () => {
let agent: ConfiguredAgent
let sender: IIdentifier
let receiver: IIdentifier

beforeAll(async () => {
await testContext.setup()
agent = testContext.getAgent()

sender = await agent.didManagerImport({
did: 'did:key:z6MkgbqNU4uF9NKSz5BqJQ4XKVHuQZYcUZP8pXGsJC8nTHwo',
keys: [
{
type: 'Ed25519',
kid: 'didcomm-senderKey-1',
publicKeyHex: '1fe9b397c196ab33549041b29cf93be29b9f2bdd27322f05844112fad97ff92a',
privateKeyHex:
'b57103882f7c66512dc96777cbafbeb2d48eca1e7a867f5a17a84e9a6740f7dc1fe9b397c196ab33549041b29cf93be29b9f2bdd27322f05844112fad97ff92a',
kms: 'local',
},
],
services: [],
provider: 'did:key',
alias: 'sender',
})

receiver = await agent.didManagerImport({
did: 'did:key:z6MkrPhffVLBZpxH7xvKNyD4sRVZeZsNTWJkLdHdgWbfgNu3',
keys: [
{
type: 'Ed25519',
kid: 'didcomm-receiverKey-1',
publicKeyHex: 'b162e405b6485eff8a57932429b192ec4de13c06813e9028a7cdadf0e2703636',
privateKeyHex:
'19ed9b6949cfd0f9a57e30f0927839a985fa699491886ebcdda6a954d869732ab162e405b6485eff8a57932429b192ec4de13c06813e9028a7cdadf0e2703636',
kms: 'local',
},
],
services: [],
provider: 'did:key',
alias: 'receiver',
})
return true
})
afterAll(testContext.tearDown)

it('should pack and unpack a plaintext message', async () => {
expect.assertions(1)
const message = {
type: 'test',
to: receiver.did,
id: 'test',
body: { hello: 'world' },
}
const packedMessage = await agent.packDIDCommMessage({
packing: 'none',
message,
})
const unpackedMessage = await agent.unpackDIDCommMessage(packedMessage)
expect(unpackedMessage).toEqual({
message: {
...message,
typ: 'application/didcomm-plain+json',
},
metaData: { packing: 'none' },
})
})

it('should pack and unpack a JWS message', async () => {
const message = {
type: 'test',
to: receiver.did,
from: sender.did,
id: 'test',
body: { hello: 'world' },
}
const packedMessage = await agent.packDIDCommMessage({
packing: 'jws',
message,
})
const unpackedMessage = await agent.unpackDIDCommMessage(packedMessage)
expect(unpackedMessage).toEqual({
message,
metaData: { packing: 'jws' },
})
})

it('should pack and unpack an anoncrypted message', async () => {
expect.assertions(2)
const message = {
type: 'test',
to: receiver.did,
id: 'test',
body: { hello: 'world' },
}
const packedMessage = await agent.packDIDCommMessage({
packing: 'anoncrypt',
message,
})
const unpackedMessage = await agent.unpackDIDCommMessage(packedMessage)
expect(unpackedMessage.message).toEqual(message)
expect(unpackedMessage.metaData).toEqual({ packing: 'anoncrypt' })
})

it('should pack and unpack an authcrypted message', async () => {
expect.assertions(2)
const message = {
type: 'test',
to: receiver.did,
from: sender.did,
id: 'test',
body: { hello: 'world' },
}
const packedMessage = await agent.packDIDCommMessage({
packing: 'authcrypt',
message,
})
const unpackedMessage = await agent.unpackDIDCommMessage(packedMessage)
expect(unpackedMessage.message).toEqual(message)
expect(unpackedMessage.metaData).toEqual({ packing: 'authcrypt' })
})
})
}
16 changes: 16 additions & 0 deletions __tests__/shared/documentationExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ export default (testContext: {

//DO NOT EDIT MANUALLY START

it('core-IResolver-getDIDComponentById example', async () => {
const did = 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190'
const didFragment = `${did}#controller`
const fragment = await agent.getDIDComponentById({
didDocument: (await agent.resolveDid({ didUrl: did }))?.didDocument,
didUrl: didFragment,
section: 'authentication',
})
expect(fragment).toEqual({
id: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190#controller',
type: 'EcdsaSecp256k1RecoveryMethod2020',
controller: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190',
blockchainAccountId: '0xb09B66026bA5909A7CFE99b76875431D2b8D5190@eip155:4',
})
})

it('core-IResolver-resolveDid example', async () => {
const doc = await agent.resolveDid({
didUrl: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test:watch": "yarn test --watch --verbose",
"veramo": "./packages/cli/bin/veramo.js",
"prettier": "prettier --write '{packages,__tests__, !build}/**/*.{ts,js,json,md,yml}'",
"build-clean": "rimraf ./packages/*/build ./packages/*/node_modules ./packages/*/tsconfig.tsbuildinfo && jest --clearCache",
"build-clean": "rimraf ./packages/*/build ./packages/*/api ./packages/*/node_modules ./packages/*/tsconfig.tsbuildinfo && jest --clearCache",
"publish:latest": "lerna publish --conventional-commits --include-merged-tags --create-release github --yes --dist-tag latest --registry https://registry.npmjs.org/:_authToken=${NPM_TOKEN}",
"publish:next": "lerna publish --conventional-prerelease --force-publish --canary --no-git-tag-version --include-merged-tags --preid next --pre-dist-tag next --yes --registry https://registry.npmjs.org/:_authToken=${NPM_TOKEN}",
"publish:unstable": "lerna publish --conventional-prerelease --force-publish --canary --no-git-tag-version --include-merged-tags --preid unstable --pre-dist-tag unstable --yes --registry https://registry.npmjs.org/:_authToken=${NPM_TOKEN}"
Expand Down
6 changes: 0 additions & 6 deletions packages/cli/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ function createSchema(generator: TJS.SchemaGenerator, symbol: string) {

const schema = generator.createSchema(fixedSymbol)

if (fixedSymbol === 'ICreateVerifiableCredentialArgs') {
//@ts-ignore
schema.definitions['W3CCredential']['properties']['credentialSubject']['additionalProperties'] = true
}
// console.dir({ fixedSymbol, schema }, {depth: 10})

const newSchema = {
components: {
schemas: schema.definitions,
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/explore/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import blessed, { Widgets } from 'blessed'
import { IMessage, VerifiableCredential } from '@veramo/core'
import { UniqueVerifiableCredential } from '@veramo/data-store'
import { shortDate, shortDid } from './utils'
import { ConfiguredAgent } from '../setup'
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/explore/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import blessed from 'blessed'
import { IMessage } from '@veramo/core'
import { ConfiguredAgent } from '../setup'
import { styles } from './styles'

Expand Down
67 changes: 65 additions & 2 deletions packages/core/src/types/IResolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { DIDResolutionOptions, DIDResolutionResult } from 'did-resolver'
import {
DIDDocument,
DIDResolutionOptions,
DIDResolutionResult,
ServiceEndpoint,
VerificationMethod,
} from 'did-resolver'
export { DIDDocument, DIDResolutionOptions, DIDResolutionResult } from 'did-resolver'
import { IPluginMethodMap } from './IAgent'

Expand Down Expand Up @@ -29,7 +35,7 @@ export interface ResolveDidArgs {
*/
export interface IResolver extends IPluginMethodMap {
/**
* Resolves DID and returns DID Document
* Resolves DID and returns DID Resolution Result
*
* @example
* ```typescript
Expand Down Expand Up @@ -59,4 +65,61 @@ export interface IResolver extends IPluginMethodMap {
* @public
*/
resolveDid(args: ResolveDidArgs): Promise<DIDResolutionResult>

/**
* Dereferences a DID URL fragment and returns the corresponding DID document entry.
*
* @example
* ```typescript
* const did = 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190'
* const didFragment = `${did}#controller`
* const fragment = await agent.getDIDComponentById({
* didDocument: (await agent.resolveDid({didUrl: did}))?.didDocument,
* didUrl: didFragment,
* section: 'authentication'
* })
* expect(fragment).toEqual({
* id: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190#controller',
* type: 'EcdsaSecp256k1RecoveryMethod2020',
* controller: 'did:ethr:rinkeby:0xb09b66026ba5909a7cfe99b76875431d2b8d5190',
* blockchainAccountId: '0xb09B66026bA5909A7CFE99b76875431D2b8D5190@eip155:4',
* })
* ```
*
* @param args.didDocument - the DID document from which to extract the fragment.
* This MUST be the document resolved by {@link resolveDid}
* @param args.didUrl - the DID URI that needs to be dereferenced
* @param args.section - Optional - the section of the DID Document to be used for dereferencing
*
* @returns a `Promise` containing the {@link did-resolver#VerificationMethod | VerificationMethod} or
* {@link did-resolver#ServiceEndpoint | ServiceEndpoint}
*
* @throws `not_found:...` in case the fragment is not displayed in the DID document
*
* @beta
*/
getDIDComponentById(args: {
/**
* the DID document from which to extract the fragment. This MUST be the document resolved by {@link resolveDid}
*/
didDocument: DIDDocument
/**
* The DID URI that refers to the subsection by #fragment. Example: did:example:identifier#controller
*/
didUrl: string
/**
* The section of the DID document where to search for the fragment. Example 'keyAgreement'
*/
section?: DIDDocumentSection
}): Promise<VerificationMethod | ServiceEndpoint>
}

export type DIDDocumentSection =
| 'verificationMethod'
| 'publicKey' //used for backward compatibility
| 'service'
| 'authentication'
| 'assertionMethod'
| 'keyAgreement'
| 'capabilityInvocation'
| 'capabilityDelegation'
Loading