Skip to content

Commit

Permalink
feat: Daf-ethr-did-fs using experimental interface
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Feb 13, 2020
1 parent a2521e0 commit cecffd8
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 261 deletions.
17 changes: 15 additions & 2 deletions packages/daf-cli/src/identity-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,19 @@ program
])

const identity = await core.identityManager.getIdentity(answers.did)
const result = await identity.encrypt(answers.to, answers.message)
const key = await identity.keyByType('Ed25519')
const didDoc = await core.didResolver.resolve(answers.to)
const publicKey = didDoc?.publicKey.find(item => item.type == 'Ed25519VerificationKey2018')
if (!publicKey?.publicKeyHex) throw Error('Recipient does not have encryption publicKey')

const result = await key.encrypt(
{
type: 'Ed25519',
publicKeyHex: publicKey?.publicKeyHex,
kid: publicKey?.publicKeyHex,
},
answers.message,
)
console.log('Success:', result)
} catch (e) {
console.error(e)
Expand All @@ -187,7 +199,8 @@ program
])

const identity = await core.identityManager.getIdentity(answers.did)
const result = await identity.decrypt(answers.message)
const key = await identity.keyByType('Ed25519')
const result = await key.decrypt(answers.message)
console.log('Success:', result)
} catch (e) {
console.error(e)
Expand Down
9 changes: 2 additions & 7 deletions packages/daf-cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,12 @@ TG.ServiceController.webSocketImpl = ws

const identityProviders = [
new EthrDidFs.IdentityProvider({
fileName: defaultPath + 'rinkeby-identity-store.json',
store: new EthrDidFs.IdentityStore(defaultPath + '/rinkeby-identity-store.json'),
kms: new EthrDidFs.KeyManagementSystem(defaultPath + '/rinkeby-kms.json'),
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
resolver: didResolver,
}),
new EthrDidFs.IdentityProvider({
fileName: defaultPath + 'kovan-identity-store.json',
network: 'kovan',
rpcUrl: 'https://kovan.infura.io/v3/' + infuraProjectId,
resolver: didResolver,
}),
]
const serviceControllers = [TG.ServiceController]

Expand Down
9 changes: 8 additions & 1 deletion packages/daf-core/src/identity/abstract-identity-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { EventEmitter } from 'events'
import { AbstractIdentity, ServiceEndpoint } from './abstract-identity'
import { AbstractIdentity } from './abstract-identity'

export interface ServiceEndpoint {
id: string
type: string
serviceEndpoint: string
description?: string
}

export abstract class AbstractIdentityProvider extends EventEmitter {
abstract type: string
Expand Down
2 changes: 1 addition & 1 deletion packages/daf-core/src/identity/abstract-identity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SerializedKey } from './abstract-key-management-system'

export interface SerializedIdentity {
did: string
controller: SerializedKey
controllerKeyId: string
keys: SerializedKey[]
}

Expand Down
39 changes: 3 additions & 36 deletions packages/daf-core/src/identity/abstract-identity.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
export interface DIDDocument {
'@context': 'https://w3id.org/did/v1'
id: string
publicKey: PublicKey[]
service?: ServiceEndpoint[]
}
export interface PublicKey {
id: string
type: string
owner: string
ethereumAddress?: string
publicKeyBase64?: string
publicKeyBase58?: string
publicKeyHex?: string
publicKeyPem?: string
}
export interface ServiceEndpoint {
id: string
type: string
serviceEndpoint: string
description?: string
}

// Placeholder:
interface EcdsaSignature {
r: string
s: string
recoveryParam?: number
}
// Placeholder:
type Signer = (data: string) => Promise<EcdsaSignature | string>

import { AbstractKey, KeyType } from './abstract-key-management-system'
export abstract class AbstractIdentity {
abstract identityProviderType: string
abstract did: string
abstract didDoc(): Promise<DIDDocument | null>
abstract signer(keyId?: string): Signer
abstract encrypt(to: string, data: string): Promise<string>
abstract decrypt(encrypted: string): Promise<string>
abstract keyByType(type: KeyType): Promise<AbstractKey>
abstract keyById(id: string): Promise<AbstractKey>
}

type AbstractIdentityClass = typeof AbstractIdentity
Expand Down
16 changes: 15 additions & 1 deletion packages/daf-did-comm/src/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@ export class ActionHandler extends AbstractActionHandler {
data: data.jwt,
})
debug(dm)
body = await identity.encrypt(data.to, dm)

// TODO: move this to AbstractIdentity
const key = await identity.keyByType('Ed25519')
const publicKey = didDoc?.publicKey.find(item => item.type == 'Ed25519VerificationKey2018')
if (!publicKey?.publicKeyHex) throw Error('Recipient does not have encryption publicKey')

body = await key.encrypt(
{
type: 'Ed25519',
publicKeyHex: publicKey?.publicKeyHex,
kid: publicKey?.publicKeyHex,
},
dm,
)

debug('Encrypted:', body)
} catch (e) {
console.log(e)
Expand Down
3 changes: 2 additions & 1 deletion packages/daf-did-comm/src/message-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export class MessageValidator extends AbstractMessageValidator {
for (const identity of identities) {
let decrypted
try {
decrypted = await identity.decrypt(message.raw)
const key = await identity.keyByType('Ed25519')
decrypted = await key.decrypt(message.raw)
} catch (e) {}
if (decrypted) {
debug('Decrypted for %s', identity.did)
Expand Down
32 changes: 0 additions & 32 deletions packages/daf-ethr-did-fs/src/action-handler.ts

This file was deleted.

Loading

0 comments on commit cecffd8

Please sign in to comment.