Skip to content

Commit

Permalink
feat: IdentityStore and KeyStore in daf-core
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Mar 9, 2020
1 parent c4c0810 commit 238539c
Show file tree
Hide file tree
Showing 9 changed files with 673 additions and 132 deletions.
2 changes: 1 addition & 1 deletion examples/expressjs-ethr/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
secrets
database.sqlite
4 changes: 2 additions & 2 deletions examples/expressjs-ethr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
"daf-did-comm": "../../packages/daf-did-comm",
"daf-did-jwt": "../../packages/daf-did-jwt",
"daf-ethr-did": "../../packages/daf-ethr-did",
"daf-fs": "../../packages/daf-fs",
"daf-libsodium": "../../packages/daf-libsodium",
"daf-node-sqlite3": "../../packages/daf-node-sqlite3",
"daf-resolver": "../../packages/daf-resolver",
"daf-resolver-universal": "../../packages/daf-resolver-universal",
"daf-selective-disclosure": "../../packages/daf-selective-disclosure",
Expand All @@ -36,7 +34,9 @@
"http": "^0.0.0",
"lodash.merge": "^4.6.2",
"socket.io": "^2.3.0",
"sqlite3": "^4.1.1",
"ts-node": "^8.5.0",
"typeorm": "^0.2.24",
"typescript": "^3.7.2"
},
"devDependencies": {
Expand Down
38 changes: 22 additions & 16 deletions examples/expressjs-ethr/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,11 @@ import * as SD from 'daf-selective-disclosure'
import * as DBG from 'daf-debug'
import * as DIDComm from 'daf-did-comm'
import * as URL from 'daf-url'

import { NodeSqlite3 } from 'daf-node-sqlite3'
import { createConnection } from 'typeorm'
import { DataStore } from 'daf-data-store'

const defaultPath = __dirname + '/../secrets'

const dataStoreFilename = process.env.DAF_DATA_STORE ?? defaultPath + '/data-store.sqlite3'
const infuraProjectId = process.env.DAF_INFURA_ID ?? '5ffc47f65c4042ce847ef66a3fa70d4c'

if (!process.env.DAF_IDENTITY_STORE || process.env.DAF_DATA_STORE || process.env.DAF_ENCRYPTION_STORE) {
const fs = require('fs')
if (!fs.existsSync(defaultPath)) {
fs.mkdirSync(defaultPath)
}
}

// DID Document Resolver
let didResolver: Daf.Resolver = new DafResolver({
infuraProjectId,
Expand All @@ -41,8 +30,8 @@ if (process.env.DAF_UNIVERSAL_RESOLVER_URL) {

const identityProviders = [
new EthrDid.IdentityProvider({
identityStore: new DafFs.IdentityStore(defaultPath + '/rinkeby-identity-store.json'),
kms: new DafLibSodium.KeyManagementSystem(new DafFs.KeyStore(defaultPath + '/rinkeby-kms.json')),
identityStore: new Daf.IdentityStore(),
kms: new DafLibSodium.KeyManagementSystem(new Daf.KeyStore()),
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
}),
Expand Down Expand Up @@ -71,5 +60,22 @@ export const core = new Daf.Core({
actionHandler,
})

const db = new NodeSqlite3(dataStoreFilename)
export const dataStore = new DataStore(db)
export const initializeDb = async () => {
await createConnection({
type: 'sqlite',
database: './database.sqlite',
synchronize: true,
logging: false,
entities: [
Daf.Key,
Daf.Identity,
Daf.Message,
Daf.MessageMetaData,
Daf.Credential,
Daf.Presentation,
Daf.Claim,
],
})
}

export const dataStore = new DataStore()
10 changes: 5 additions & 5 deletions examples/expressjs-ethr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import * as Daf from 'daf-core'
import * as SD from 'daf-selective-disclosure'
import * as W3C from 'daf-w3c'
import { app, server, io, sessionStore } from './server'
import { core, dataStore } from './framework'
import { core, dataStore, initializeDb } from './framework'
import { getIdentity, setServiceEndpoint } from './identity'

if (!process.env.HOST) throw Error('Environment variable HOST not set')
if (!process.env.PORT) throw Error('Environment variable PORT not set')

async function main() {
await dataStore.initialize()
await initializeDb()
const identity = await getIdentity()

const messagingEndpoint = '/handle-message'
Expand All @@ -25,7 +25,7 @@ async function main() {
try {
// This will trigger Daf.EventTypes.validatedMessage
const result = await core.validateMessage(
new Daf.Message({ raw: req.body, meta: { type: 'serviceEndpoint', id: serviceEndpoint } }),
new Daf.Message({ raw: req.body, meta: { type: 'serviceEndpoint', value: serviceEndpoint } }),
)
res.json({ id: result.id })
} catch (e) {
Expand All @@ -40,13 +40,13 @@ async function main() {
// TODO check for required vcs

const sessionId = message.threadId
io.in(sessionId).emit('loggedin', { did: message.sender })
io.in(sessionId).emit('loggedin', { did: message.from?.did })

sessionStore.get(sessionId, (error, session) => {
if (error) throw Error(error)

if (session) {
session.did = message.sender
session.did = message.from?.did
sessionStore.set(sessionId, session)
}
})
Expand Down
1 change: 0 additions & 1 deletion examples/expressjs-ethr/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
{ "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" },
Expand Down
Loading

0 comments on commit 238539c

Please sign in to comment.