Skip to content

Commit

Permalink
chore: Experimental stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Feb 13, 2020
1 parent 17fe219 commit 9b545be
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/daf-core/src/identity/abstract-identity-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SerializedKey } from './abstract-key-management-system'

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

export abstract class AbstractIdentityStore {
abstract set(did: string, serializedIdentity: SerializedIdentity): Promise<boolean>
abstract get(did: string): Promise<SerializedKey>
abstract delete(did: string): Promise<boolean>
abstract listDids(): Promise<string[]>
}
22 changes: 22 additions & 0 deletions packages/daf-core/src/identity/abstract-key-management-system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type KeyType = 'Ed25519' | 'Secp256k1'

export interface SerializedKey {
type: KeyType
publicKeyHex: string
privateKeyHex?: string
kid: string
meta?: string
}

export abstract class AbstractKey {
abstract serialized: SerializedKey
abstract encrypt(to: SerializedKey, data: string): Promise<string>
abstract decrypt(encrypted: string): Promise<string>
abstract sign(data: string): Promise<string>
}

export abstract class AbstractKeyManagementSystem {
abstract createKey(type: KeyType): Promise<AbstractKey>
abstract getKey(kid: string): Promise<AbstractKey>
abstract deleteKey(kid: string): Promise<boolean>
}
6 changes: 6 additions & 0 deletions packages/daf-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ export { AbstractActionHandler } from './action/action-handler'
export { IdentityManager } from './identity/identity-manager'
export { AbstractIdentity } from './identity/abstract-identity'
export { AbstractIdentityProvider, IdentityProviderDerived } from './identity/abstract-identity-provider'
export {
AbstractKeyManagementSystem,
AbstractKey,
KeyType,
SerializedKey,
} from './identity/abstract-key-management-system'
export { AbstractMessageValidator } from './message/abstract-message-validator'
export { Message } from './message/message'
export { ServiceManager, LastMessageTimestampForInstance, ServiceEventTypes } from './service/service-manager'
Expand Down
32 changes: 32 additions & 0 deletions packages/daf-ethr-did-fs/src/action-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Core, AbstractActionHandler, Types } from 'daf-core'

export const ActionTypes = {
addPublicKey: 'action.createAndAddPublicKey',
addDelegate: 'action.addDelegate',
}

export class ActionHandler extends AbstractActionHandler {
public async handleAction(action: Types.Action, core: Core) {
if (action.type === ActionTypes.addPublicKey) {
/// NR 1
const did = action.data.did
const keyType = action.data.keyType

// create keypair

// update did document
const identity = await core.identityManager.getIdentity(did)
const kms = identity.kms
const key = kms.create(keyType)

try {
const result = await identity.controller.addPublicKey(key.serialized, { gas: action.data.gas })
// update serialized identity
identity.serialized.keys.push(key.serialized)
const x = await identity.identityProvider.updateIdentity(identity.did, identity.serialized)
} catch (e) {}
}

return super.handleAction(action, core)
}
}
42 changes: 42 additions & 0 deletions packages/daf-ethr-did-fs/src/key-management-system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { AbstractKeyManagementSystem, KeyType, AbstractKey, SerializedKey } from 'daf-core'
import * as sodium from 'libsodium-wrappers'

export class Key extends AbstractKey {
async encrypt(to: SerializedKey, data: string) {
return ''
}

async decrypt(data: string) {
return ''
}

async sign(data: string) {
return ''
}
}
export class KeyManagementSystem extends AbstractKeyManagementSystem {
private fileName: string

constructor(options: { fileName: string }) {
super()
this.fileName = options.fileName
}

async createKey(type: KeyType) {
let serializedKey: SerializedKey

switch (type) {
case 'Ed25519':
await sodium.ready
const keyPair = sodium.crypto_sign_keypair()
serializedKey = {
type,
kid: Buffer.from(keyPair.publicKey).toString('hex'),
publicKeyHex: Buffer.from(keyPair.publicKey).toString('hex'),
privateKeyHex: Buffer.from(keyPair.privateKey).toString('hex'),
}

break
}
}
}

0 comments on commit 9b545be

Please sign in to comment.