Skip to content

Commit

Permalink
feat: Experiments with KeyStore IdentityStore
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Mar 5, 2020
1 parent 27e4ab2 commit cca8825
Show file tree
Hide file tree
Showing 7 changed files with 1,487 additions and 29 deletions.
41 changes: 41 additions & 0 deletions examples/send-vc/identity-store.ts
@@ -0,0 +1,41 @@
import { AbstractIdentityStore, SerializedIdentity, Identity, Key } from 'daf-core'
import Debug from 'debug'
const debug = Debug('daf:orm:identity-store')

export class IdentityStore extends AbstractIdentityStore {
async get(did: string) {
const identity = await Identity.findOne(did, { relations: ['keys'] })
if (!identity) throw Error('Identity not found')
return identity
}

async delete(did: string) {
const identity = await Identity.findOne(did)
if (!identity) throw Error('Identity not found')
debug('Deleting', did)
await identity.remove()
return true
}

async set(did: string, serializedIdentity: SerializedIdentity) {
const identity = new Identity()
identity.did = serializedIdentity.did
identity.controllerKeyId = serializedIdentity.controllerKeyId
identity.keys = []

for (const sKey of serializedIdentity.keys) {
const key = new Key()
key.kid = sKey.kid
identity.keys.push(key)
}
await identity.save()

debug('Saving', did)
return true
}

async listDids() {
const identities = await Identity.find()
return identities.map(identity => identity.did)
}
}
19 changes: 18 additions & 1 deletion examples/send-vc/index.ts
@@ -1,9 +1,24 @@
import { AbstractIdentity, EventTypes, Message } from 'daf-core'
import { AbstractIdentity, EventTypes, Message, Key, Identity } from 'daf-core'
import { ActionSendJWT } from 'daf-did-comm'
import { ActionSignW3cVc } from 'daf-w3c'
import { core } from './setup'
import { createConnection } from 'typeorm'

async function main() {
await createConnection({
// "type": "sqlite",
// "database": "database.sqlite",
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'simonas',
password: '',
database: 'simonas',
synchronize: true,
logging: true,
entities: [Key, Identity],
})

// Getting existing identity or creating a new one
let identity: AbstractIdentity
const identities = await core.identityManager.getIdentities()
Expand All @@ -14,6 +29,8 @@ async function main() {
identity = await core.identityManager.createIdentity(identityProviders[0].type)
}

console.log(identity.did)

// Sign verifiable credential
const vcJwt = await core.handleAction({
type: 'action.sign.w3c.vc',
Expand Down
31 changes: 31 additions & 0 deletions examples/send-vc/key-store.ts
@@ -0,0 +1,31 @@
import { AbstractKeyStore, SerializedKey, Key } from 'daf-core'

import Debug from 'debug'
const debug = Debug('daf:orm:key-store')

export default class KeyStore extends AbstractKeyStore {
async get(kid: string) {
const key = await Key.findOne(kid)
if (!key) throw Error('Key not found')
return key
}

async delete(kid: string) {
const key = await Key.findOne(kid)
if (!key) throw Error('Key not found')
debug('Deleting key', kid)
await key.remove()
return true
}

async set(kid: string, serializedKey: SerializedKey) {
const key = new Key()
key.kid = kid
key.privateKeyHex = serializedKey.privateKeyHex
key.publicKeyHex = serializedKey.publicKeyHex
key.type = serializedKey.type
debug('Saving key', kid)
await key.save()
return true
}
}
4 changes: 4 additions & 0 deletions examples/send-vc/package.json
Expand Up @@ -17,7 +17,11 @@
"daf-resolver": "../../packages/daf-resolver",
"daf-trust-graph": "../../packages/daf-trust-graph",
"daf-w3c": "../../packages/daf-w3c",
"pg": "^7.18.2",
"reflect-metadata": "^0.1.13",
"sqlite3": "^4.1.1",
"ts-node": "^8.5.4",
"typeorm": "^0.2.24",
"typescript": "^3.7.3"
},
"license": "Apache-2.0"
Expand Down
12 changes: 8 additions & 4 deletions examples/send-vc/setup.ts
Expand Up @@ -2,16 +2,20 @@
import { IdentityProvider } from 'daf-ethr-did'

// Storing serialized key pairs in the file system
import { KeyStore } from 'daf-fs'
const keyStore = new KeyStore('./key-store.json')
// import { KeyStore } from 'daf-fs'
// const keyStore = new KeyStore('./key-store.json')
import KeyStore from './key-store'
const keyStore = new KeyStore()

// KeyManagementSystem is responsible for managing encryption and signing keys
import { KeyManagementSystem } from 'daf-libsodium'
const kms = new KeyManagementSystem(keyStore)

// Storing serialized identities in the file system
import { IdentityStore } from 'daf-fs'
const identityStore = new IdentityStore('./identity-store.json')
// import { IdentityStore } from 'daf-fs'
// const identityStore = new IdentityStore('./identity-store.json')
import { IdentityStore } from './identity-store'
const identityStore = new IdentityStore()

// Infura is being used to access Ethereum blockchain. https://infura.io
const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c'
Expand Down
34 changes: 34 additions & 0 deletions examples/send-vc/tsconfig.json
@@ -0,0 +1,34 @@
{
"compilerOptions": {
"preserveConstEnums": true,
"sourceMap": true,
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"downlevelIteration": true,
"declarationMap": true,
"declaration": true,
"composite": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"exclude": ["**/__tests__/**/*", "**/build/**/*"],
"references": [
{ "path": "../../packages/daf-core" },
{ "path": "../../packages/daf-data-store" },
{ "path": "../../packages/daf-debug" },
{ "path": "../../packages/daf-did-comm" },
{ "path": "../../packages/daf-did-jwt" },
{ "path": "../../packages/daf-ethr-did" },
{ "path": "../../packages/daf-fs" },
{ "path": "../../packages/daf-libsodium" },
{ "path": "../../packages/daf-node-sqlite3" },
{ "path": "../../packages/daf-resolver" },
{ "path": "../../packages/daf-resolver-universal" },
{ "path": "../../packages/daf-selective-disclosure" },
{ "path": "../../packages/daf-trust-graph" },
{ "path": "../../packages/daf-url" },
{ "path": "../../packages/daf-w3c" }
]
}

0 comments on commit cca8825

Please sign in to comment.