Skip to content

Commit

Permalink
fix(kms-local): replace buggy didcomm clone with did jwt implementati…
Browse files Browse the repository at this point in the history
…on (#548)

* fix(kms-local): replace buggy didcomm-js clone with anon encryption from `did-jwt`
* remove the `libsodium-wrappers` dependency and replace it with `@stablelib/*`.
This should make it easier to work with multiple JS environments using the same code.

fixes #538

* chore(kms-local-react-native): remove the redundant `kms-local-react-native` module.

BREAKING CHANGE: `@veramo/kms-local-react-native` is no more. On react-native, please use `@veramo/kms-local` instead, combined with `@ethersproject/shims`
  • Loading branch information
mirceanis authored Jun 7, 2021
1 parent c6f8a02 commit 9dea353
Show file tree
Hide file tree
Showing 24 changed files with 265 additions and 2,255 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.words": [
"Decrypter",
"Encrypter",
"Keypair",
"arrayify",
"ethersproject"
]
Expand Down
91 changes: 49 additions & 42 deletions __tests__/shared/keyManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TKeyType } from '@veramo/core'
import { IKey, TKeyType } from '@veramo/core'
import { TAgent, IDIDManager, IKeyManager, IAgentOptions } from '../../packages/core/src'
import { ICredentialIssuer } from '@veramo/credential-w3c/src'
import { serialize } from '@ethersproject/transactions'

type ConfiguredAgent = TAgent<IDIDManager & IKeyManager>
Expand Down Expand Up @@ -51,7 +50,21 @@ export default (testContext: {
expect(key.type).toEqual('Ed25519')
})

it('should create X25519 key', async () => {
const key = await agent.keyManagerCreate({
kms: 'local',
type: 'X25519',
})

expect(key).toHaveProperty('kid')
expect(key).toHaveProperty('publicKeyHex')
expect(key).not.toHaveProperty('privateKeyHex')
expect(key.kms).toEqual('local')
expect(key.type).toEqual('X25519')
})

it('should throw an error for unsupported kms', async () => {
expect.assertions(1)
await expect(
agent.keyManagerCreate({
kms: 'foobar',
Expand All @@ -61,6 +74,7 @@ export default (testContext: {
})

it('should throw an error for unsupported key type', async () => {
expect.assertions(1)
await expect(
agent.keyManagerCreate({
kms: 'local',
Expand Down Expand Up @@ -88,6 +102,7 @@ export default (testContext: {
expect(key.meta).toEqual({
foo: 'bar',
bar: 'baz',
algorithms: ['ES256K', 'ES256K-R', 'eth_signTransaction', 'eth_signTypedData', 'eth_signMessage'],
})
})

Expand Down Expand Up @@ -131,27 +146,21 @@ export default (testContext: {
})

it('should import key', async () => {
const key = await agent.keyManagerCreate({
const fullKey: IKey = {
kid: '04dd467afb12bdb797303e7f3f0c8cd0ba80d518dc4e339e0e2eb8f2d99a9415cac537854a30d31a854b7af0b4fcb54c3954047390fa9500d3cc2e15a3e09017bb',
kms: 'local',
type: 'Secp256k1',
meta: {
foo: 'bar',
},
})

const fullKey = await agent.keyManagerGet({
kid: key.kid,
})

await agent.keyManagerDelete({
kid: key.kid,
})
publicKeyHex:
'04dd467afb12bdb797303e7f3f0c8cd0ba80d518dc4e339e0e2eb8f2d99a9415cac537854a30d31a854b7af0b4fcb54c3954047390fa9500d3cc2e15a3e09017bb',
privateKeyHex: 'e63886b5ba367dc2aff9acea6d955ee7c39115f12eaf2aa6b1a2eaa852036668',
meta: { foo: 'bar' },
}

const result = await agent.keyManagerImport(fullKey)
expect(result).toEqual(true)

const key2 = await agent.keyManagerGet({
kid: key.kid,
kid: fullKey.kid,
})

expect(key2).toEqual(fullKey)
Expand Down Expand Up @@ -191,41 +200,39 @@ export default (testContext: {
expect(typeof rawTx).toEqual('string')
})

it.todo('Should Encrypt/Decrypt')
// it('Should Encrypt/Decrypt', async () => {
// const message = 'foo bar'

// const senderKey = await agent.keyManagerCreate({
// kms: 'local',
// type: 'Ed25519',
// })
// it.todo('Should Encrypt/Decrypt')
it('Should Encrypt/Decrypt', async () => {
const message = 'foo bar'

// const recipientKey = await agent.keyManagerCreate({
// kms: 'local',
// type: 'Ed25519',
// })
const senderKey = await agent.keyManagerCreate({
kms: 'local',
type: 'Ed25519',
})

// const encrypted = await agent.keyManagerEncryptJWE({
// kid: senderKey.kid,
// to: recipientKey,
// data: message
// })
const recipientKey = await agent.keyManagerCreate({
kms: 'local',
type: 'Ed25519',
})

// expect(typeof encrypted).toEqual('string')
const encrypted = await agent.keyManagerEncryptJWE({
kid: senderKey.kid,
to: recipientKey,
data: message,
})

// const decrypted = await agent.keyManagerDecryptJWE({
// kid: recipientKey.kid,
// data: encrypted
// })
expect(typeof encrypted).toEqual('string')

// expect(decrypted).toEqual(message)
const decrypted = await agent.keyManagerDecryptJWE({
kid: recipientKey.kid,
data: encrypted,
})

// })
expect(decrypted).toEqual(message)
})

describe('using Secp256k1 testvectors', () => {
const importedKey = {
kid:
'04155ee0cbefeecd80de63a62b4ed8f0f97ac22a58f76a265903b9acab79bf018c7037e2bd897812170c92a4c978d6a10481491a37299d74c4bd412a111a4ac875',
kid: '04155ee0cbefeecd80de63a62b4ed8f0f97ac22a58f76a265903b9acab79bf018c7037e2bd897812170c92a4c978d6a10481491a37299d74c4bd412a111a4ac875',
kms: 'local',
type: <TKeyType>'Secp256k1',
publicKeyHex:
Expand Down
2 changes: 1 addition & 1 deletion __tests__/shared/resolveDid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TAgent, IResolver, ValidationError, IAgentOptions } from '../../packages/core/src'
import { TAgent, IResolver, IAgentOptions } from '../../packages/core/src'

type ConfiguredAgent = TAgent<IResolver>

Expand Down
6 changes: 4 additions & 2 deletions packages/core/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@
"type": "string",
"enum": [
"Ed25519",
"Secp256k1"
"Secp256k1",
"X25519"
],
"description": "Cryptographic key type"
},
Expand Down Expand Up @@ -807,7 +808,8 @@
"type": "string",
"enum": [
"Ed25519",
"Secp256k1"
"Secp256k1",
"X25519"
],
"description": "Cryptographic key type"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/IIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface IIdentifier {
* Cryptographic key type
* @public
*/
export type TKeyType = 'Ed25519' | 'Secp256k1'
export type TKeyType = 'Ed25519' | 'Secp256k1' | 'X25519'

/**
* Cryptographic key
Expand Down
3 changes: 2 additions & 1 deletion packages/data-store/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@
"type": "string",
"enum": [
"Ed25519",
"Secp256k1"
"Secp256k1",
"X25519"
],
"description": "Cryptographic key type"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/data-store/src/entities/key.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { KeyMetadata } from '@veramo/core'
import { KeyMetadata, TKeyType } from '@veramo/core'
import { Entity, Column, PrimaryColumn, BaseEntity, ManyToOne } from 'typeorm'
import { Identifier } from './identifier'

export type KeyType = 'Ed25519' | 'Secp256k1'
export type KeyType = TKeyType

@Entity('key')
export class Key extends BaseEntity {
Expand Down
7 changes: 4 additions & 3 deletions packages/key-manager/src/key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class KeyManager implements IAgentPlugin {
keyManagerGet: this.keyManagerGet.bind(this),
keyManagerDelete: this.keyManagerDelete.bind(this),
keyManagerImport: this.keyManagerImport.bind(this),
keyManagerEncryptJWE: this.keyManagerDecryptJWE.bind(this),
keyManagerEncryptJWE: this.keyManagerEncryptJWE.bind(this),
keyManagerDecryptJWE: this.keyManagerDecryptJWE.bind(this),
keyManagerSignJWT: this.keyManagerSignJWT.bind(this),
keyManagerSignEthTX: this.keyManagerSignEthTX.bind(this),
Expand All @@ -65,8 +65,8 @@ export class KeyManager implements IAgentPlugin {
const kms = this.getKms(args.kms)
const partialKey = await kms.createKey({ type: args.type, meta: args.meta })
const key: IKey = { ...partialKey, kms: args.kms }
if (args.meta) {
key.meta = args.meta
if (args.meta || key.meta) {
key.meta = {...args.meta, ...key.meta}
}
await this.store.import(key)
if (key.privateKeyHex) {
Expand All @@ -90,6 +90,7 @@ export class KeyManager implements IAgentPlugin {

/** {@inheritDoc @veramo/core#IKeyManager.keyManagerImport} */
async keyManagerImport(key: IKey): Promise<boolean> {
//FIXME: check proper key properties and ask the actual KMS to import and fill in the missing meta data
return this.store.import(key)
}

Expand Down
28 changes: 0 additions & 28 deletions packages/kms-local-react-native/CHANGELOG.md

This file was deleted.

Loading

0 comments on commit 9dea353

Please sign in to comment.