Skip to content

Commit

Permalink
feat: Using DIDComm for encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Feb 10, 2020
1 parent c384159 commit 02fefa9
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 6 deletions.
57 changes: 57 additions & 0 deletions packages/daf-cli/src/identity-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ program
.option('-d, --delete', 'Delete identity')
.option('-s, --service', 'Add service endpoint')
.option('-p, --publicKey', 'Add public key')
.option('--encrypt', 'Encrypt data to a recipient DID')
.option('--decrypt', 'Decrypt data')
.action(async cmd => {
if (cmd.types) {
const list = await core.identityManager.getIdentityProviderTypes()
Expand Down Expand Up @@ -136,4 +138,59 @@ program
console.error(e)
}
}

if (cmd.encrypt) {
try {
const identities = await core.identityManager.getIdentities()
const answers = await inquirer.prompt([
{
type: 'list',
name: 'did',
choices: identities.map(item => item.did),
message: 'Select DID',
},
{
type: 'text',
name: 'to',
message: 'Recipient DID',
},
{
type: 'text',
name: 'message',
message: 'Message',
},
])

const identity = await core.identityManager.getIdentity(answers.did)
const result = await identity.encrypt(answers.to, answers.message)
console.log('Success:', result)
} catch (e) {
console.error(e)
}
}

if (cmd.decrypt) {
try {
const identities = await core.identityManager.getIdentities()
const answers = await inquirer.prompt([
{
type: 'list',
name: 'did',
choices: identities.map(item => item.did),
message: 'Select DID',
},
{
type: 'text',
name: 'message',
message: 'Encrypted message',
},
])

const identity = await core.identityManager.getIdentity(answers.did)
const result = await identity.decrypt(answers.message)
console.log('Success:', result)
} catch (e) {
console.error(e)
}
}
})
4 changes: 2 additions & 2 deletions packages/daf-core/src/identity/abstract-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export abstract class AbstractIdentity {
abstract did: string
abstract didDoc(): Promise<DIDDocument | null>
abstract signer(keyId?: string): Signer
abstract encrypt(to: string, data: string | Uint8Array): Promise<any>
abstract decrypt(encrypted: any): Promise<string>
abstract encrypt(to: string, data: string): Promise<string>
abstract decrypt(encrypted: string): Promise<string>
}

type AbstractIdentityClass = typeof AbstractIdentity
Expand Down
38 changes: 37 additions & 1 deletion packages/daf-ethr-did-fs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/daf-ethr-did-fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"build": "tsc"
},
"dependencies": {
"DIDComm-js": "github:decentralized-identity/DIDComm-js",
"base64url": "^3.0.1",
"daf-core": "^1.4.1",
"daf-resolver": "^1.1.0",
Expand Down
34 changes: 31 additions & 3 deletions packages/daf-ethr-did-fs/src/ethr-identity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { AbstractIdentity, Resolver } from 'daf-core'
import { SimpleSigner } from 'did-jwt'
import { Key } from './identity-provider'
import { DIDComm } from 'DIDComm-js'
const didcomm = new DIDComm()

export class EthrIdentity extends AbstractIdentity {
public readonly did: string
Expand All @@ -18,13 +20,39 @@ export class EthrIdentity extends AbstractIdentity {

public signer(keyId?: string) {
const key = this.keys.find(item => item.type === 'Secp256k1')
if (!key) throw Error('[ethr-identity] Key not found')
if (!key) throw Error('Key not found')
return SimpleSigner(key.privateKey)
}

async didDoc() {
return this.resolver.resolve(this.did)
}
async encrypt(): Promise<any> {}
async decrypt(): Promise<any> {}
async encrypt(to: string, data: string): Promise<any> {
const didDoc = await this.resolver.resolve(to)
const publicKey = didDoc?.publicKey.find(item => item.type == 'Ed25519VerificationKey2018')
if (publicKey?.publicKeyHex) {
await didcomm.ready
return await didcomm.pack_anon_msg_for_recipients(data, [
Uint8Array.from(Buffer.from(publicKey.publicKeyHex, 'hex')),
])
} else {
return Promise.reject('Encryption public key not found for ' + to)
}
}
async decrypt(encrypted: string): Promise<string> {
const key = this.keys.find(item => item.type === 'Ed25519')
if (!key) throw Error('Encryption key not found for ' + this.did)
await didcomm.ready
try {
const unpackMessage = await didcomm.unpackMessage(encrypted, {
keyType: 'ed25519',
publicKey: Uint8Array.from(Buffer.from(key.publicKey, 'hex')),
privateKey: Uint8Array.from(Buffer.from(key.privateKey, 'hex')),
})

return unpackMessage.message
} catch (e) {
return Promise.reject('Error: ' + e.message)
}
}
}

0 comments on commit 02fefa9

Please sign in to comment.