-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17fe219
commit 9b545be
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
packages/daf-core/src/identity/abstract-key-management-system.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |