Skip to content

Commit

Permalink
feat: Data-store upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Mar 9, 2020
1 parent 41a60fc commit c4c0810
Show file tree
Hide file tree
Showing 32 changed files with 11,277 additions and 1,596 deletions.
50 changes: 42 additions & 8 deletions examples/send-vc/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,49 @@ const main = async () => {
// console.dir(res, { depth: 10 })

// const msg = new Message({raw: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1ODI2MTk4NzUsImF1ZCI6ImRpZDpldGhyOnJpbmtlYnk6MHgzYzM1N2JhNDU4OTMzYTE5YzFkZjFjN2Y2YjQ3M2IzMzAyYmJiZTYxIiwidGFnIjoieHl6LTEyMyIsInZwIjp7IkBjb250ZXh0IjpbImh0dHBzOi8vd3d3LnczLm9yZy8yMDE4L2NyZWRlbnRpYWxzL3YxIl0sInR5cGUiOlsiVmVyaWZpYWJsZVByZXNlbnRhdGlvbiJdLCJ2ZXJpZmlhYmxlQ3JlZGVudGlhbCI6WyJleUowZVhBaU9pSktWMVFpTENKaGJHY2lPaUpGVXpJMU5rc3RVaUo5LmV5SnBZWFFpT2pFMU9ESTJNVGsyTnpZc0luTjFZaUk2SW1ScFpEcGxkR2h5T25KcGJtdGxZbms2TUhnell6TTFOMkpoTkRVNE9UTXpZVEU1WXpGa1pqRmpOMlkyWWpRM00ySXpNekF5WW1KaVpUWXhJaXdpZG1NaU9uc2lRR052Ym5SbGVIUWlPbHNpYUhSMGNITTZMeTkzZDNjdWR6TXViM0puTHpJd01UZ3ZZM0psWkdWdWRHbGhiSE12ZGpFaVhTd2lkSGx3WlNJNld5SldaWEpwWm1saFlteGxRM0psWkdWdWRHbGhiQ0pkTENKamNtVmtaVzUwYVdGc1UzVmlhbVZqZENJNmV5SnVZVzFsSWpvaVFXeHBZMlVpZlgwc0ltbHpjeUk2SW1ScFpEcGxkR2h5T25KcGJtdGxZbms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YzFkZjFjN2Y2YjQ3M2IzMzAyYmJiZTYxIiwidmMiOnsiQGNvbnRleHQiOlsiaHR0cHM6Ly93d3cudzMub3JnLzIwMTgvY3JlZGVudGlhbHMvdjEiXSwidHlwZSI6WyJWZXJpZmlhYmxlQ3JlZGVudGlhbCJdLCJjcmVkZW50aWFsU3ViamVjdCI6eyJuYW1lIjoiQWxpY2UifX0sImlzcyI6ImRpZDpldGhyOnJpbmtlYnk6MHgzYzM1N2JhNDU4OTMzYTE5YzFkZjFjN2Y2YjQ3M2IzMzAyYmJiZTYxIn0.IGF1LFOc4_PcGVeq7Yw7OGz4Gj7xXZK6p8bP9CSEIXz7mNFPM0v0nuevTZ47a0I8XgLfCFNkUrIIscjH8MFx_wE',
// })
// const validated = await core.validateMessage(msg)
// // console.dir({validated}, {depth: 10})
// await validated.save()
// console.log('after ----------')
// console.dir({ validated }, { depth: 10 })
//

// const hash = '77f02854ccbf36b2d670cb62efc8807e3f76e8536279ca515a1d3abe564ab0b7d191579740d39e8a9a661fcd1bb7dafe4cface430081f4a08833f14d8e18392c'

// const credential = await Credential.findOne(hash, {
// relations: ['messages', 'messages.from', 'messages.to']
// })

// console.log(credential.messages)

// const i1 = new Identity()
// i1.did = 'did:example:3'

const i2 = new Identity()
i2.did = 'did:example:2'

// const m1 = new Message()
// m1.type = 'vp'
// m1.raw = '333'
// m1.from = i1
// m1.to = [i2]

// await m1.save()

const messages = await Message.find({
relations: ['from', 'to'],
where: {
to: {
did: 'did:example:2',
},
},
})
const validated = await core.validateMessage(msg)
// console.dir({validated}, {depth: 10})
await validated.save()
console.log('after ----------')
console.dir({ validated }, { depth: 10 })

console.dir({ messages }, { depth: 10 })
}

main().catch(console.log)
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"docs": "ts-node ./scripts/generate-docs.ts && api-documenter markdown -i ./tmpapi -o ./docs/api && rm -rf ./tmpapi",
"test": "jest --config=jest.json",
"test:watch": "jest --config=jest.json --watch",
"daf": "./packages/daf-cli/bin/daf.js"
"daf": "./packages/daf-cli/bin/daf.js",
"build-clean": "rimraf ./packages/*/build ./packages/*/node_modules ./packages/*/tsconfig.tsbuildinfo"
},
"workspaces": [
"packages/*"
Expand Down Expand Up @@ -42,5 +43,8 @@
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"rimraf": "^3.0.2"
}
}
3 changes: 2 additions & 1 deletion packages/daf-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"daf-ethr-did": "^2.3.17",
"daf-fs": "^2.3.16",
"daf-libsodium": "^2.3.16",
"daf-node-sqlite3": "^2.3.16",
"daf-resolver": "^2.3.15",
"daf-resolver-universal": "^2.3.16",
"daf-selective-disclosure": "^2.3.16",
Expand All @@ -36,6 +35,8 @@
"inquirer": "^7.0.0",
"lodash.merge": "^4.6.2",
"qrcode-terminal": "^0.12.0",
"sqlite3": "^4.1.1",
"typeorm": "^0.2.24",
"ws": "^7.2.0"
},
"devDependencies": {
Expand Down
12 changes: 7 additions & 5 deletions packages/daf-cli/src/credential.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Daf from 'daf-core'
import * as W3c from 'daf-w3c'
import * as DIDComm from 'daf-did-comm'
import { core, dataStore } from './setup'
import { core, dataStore, initializeDb } from './setup'
import program from 'commander'
import inquirer from 'inquirer'
import qrcode from 'qrcode-terminal'
Expand All @@ -12,6 +12,8 @@ program
.option('-s, --send', 'Send')
.option('-q, --qrcode', 'Show qrcode')
.action(async cmd => {
await initializeDb()

const identities = await core.identityManager.getIdentities()
if (identities.length === 0) {
console.error('No dids')
Expand Down Expand Up @@ -62,7 +64,6 @@ program

const jwt = await core.handleAction(signAction)

await dataStore.initialize()
if (!cmd.send) {
await core.validateMessage(new Daf.Message({ raw: jwt, meta: { type: 'cli' } }))
} else {
Expand Down Expand Up @@ -93,6 +94,8 @@ program
.option('-s, --send', 'Send')
.option('-q, --qrcode', 'Show qrcode')
.action(async cmd => {
await initializeDb()

const myIdentities = await core.identityManager.getIdentities()
if (myIdentities.length === 0) {
console.error('No dids')
Expand All @@ -104,7 +107,7 @@ program
const identities = [
{
name: 'Enter manualy',
value: false,
value: 'manual',
},
]
for (const did of dids) {
Expand Down Expand Up @@ -136,7 +139,7 @@ program
},
])

if (!answers.aud) {
if (answers.aud === 'manual') {
const audAnswer = await inquirer.prompt([
{
type: 'input',
Expand Down Expand Up @@ -206,7 +209,6 @@ program

const jwt = await core.handleAction(signAction)

await dataStore.initialize()
if (!cmd.send) {
await core.validateMessage(new Daf.Message({ raw: jwt, meta: { type: 'cli' } }))
} else {
Expand Down
4 changes: 3 additions & 1 deletion packages/daf-cli/src/data-explorer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { core, dataStore } from './setup'
import { core, dataStore, initializeDb } from './setup'
import program from 'commander'
import inquirer from 'inquirer'
import { formatDistanceToNow } from 'date-fns'
Expand All @@ -10,6 +10,8 @@ program
.option('-i, --identities', 'List known identities')
.option('-m, --messages', 'List messages')
.action(async cmd => {
await initializeDb()

if (cmd.identities) {
const dids = await dataStore.allIdentities()
if (dids.length === 0) {
Expand Down
8 changes: 4 additions & 4 deletions packages/daf-cli/src/msg.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import * as Daf from 'daf-core'
import { core, dataStore } from './setup'
import { core, dataStore, initializeDb } from './setup'
import program from 'commander'

program
.command('msg <raw>')
.description('Handle raw message (JWT)')
.action(async raw => {
try {
await dataStore.initialize()
const result = await core.validateMessage(
await initializeDb()
const message = await core.validateMessage(
new Daf.Message({
raw,
meta: { type: 'cli' },
}),
)
console.log(result)
console.log(message)
} catch (e) {
console.error(e)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/daf-cli/src/sdr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Daf from 'daf-core'
import * as DIDComm from 'daf-did-comm'
import * as SD from 'daf-selective-disclosure'
import { core, dataStore } from './setup'
import { core, dataStore, initializeDb } from './setup'
import program from 'commander'
import inquirer from 'inquirer'
import qrcode from 'qrcode-terminal'
Expand All @@ -12,6 +12,8 @@ program
.option('-s, --send', 'Send')
.option('-q, --qrcode', 'Show qrcode')
.action(async cmd => {
await initializeDb()

const identities = await core.identityManager.getIdentities()
if (identities.length === 0) {
console.error('No dids')
Expand Down Expand Up @@ -92,7 +94,6 @@ program

const jwt = await core.handleAction(signAction)

await dataStore.initialize()
if (!cmd.send) {
await core.validateMessage(new Daf.Message({ raw: jwt, meta: { type: 'cli' } }))
} else if (answers.sub !== '') {
Expand Down
4 changes: 2 additions & 2 deletions packages/daf-cli/src/services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventTypes, Message } from 'daf-core'
import { core, dataStore } from './setup'
import { core, dataStore, initializeDb } from './setup'
import program from 'commander'
import { setInterval } from 'timers'

Expand All @@ -12,7 +12,7 @@ program
})

export const listen = async (pollSeconds?: number) => {
await dataStore.initialize()
await initializeDb()

core.on(EventTypes.validatedMessage, async (msg: Message) => {
console.log('New message type:', msg.type)
Expand Down
24 changes: 20 additions & 4 deletions packages/daf-cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import * as TG from 'daf-trust-graph'
import * as DBG from 'daf-debug'
import * as DIDComm from 'daf-did-comm'
import * as URL from 'daf-url'
import { createConnection } from 'typeorm'

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

Expand Down Expand Up @@ -81,10 +81,26 @@ 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: defaultPath + 'database-v2.sqlite',
synchronize: false,
logging: true,
entities: [
Daf.Key,
Daf.Identity,
Daf.Message,
Daf.MessageMetaData,
Daf.Credential,
Daf.Presentation,
Daf.Claim,
],
})
}
export const dataStore = new DataStore()

core.on(Daf.EventTypes.validatedMessage, async (message: Daf.Message) => {
debug('New message %O', message)
await dataStore.saveMessage(message)
await message.save()
})
1 change: 0 additions & 1 deletion packages/daf-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
{ "path": "../daf-ethr-did" },
{ "path": "../daf-fs" },
{ "path": "../daf-libsodium" },
{ "path": "../daf-node-sqlite3" },
{ "path": "../daf-resolver" },
{ "path": "../daf-resolver-universal" },
{ "path": "../daf-selective-disclosure" },
Expand Down
4 changes: 2 additions & 2 deletions packages/daf-core/src/graphql/graphql-base-type-defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const baseTypeDefs = `
}
type MessageMetaData {
id: ID!
type: String!
id: String
data: String
value: String
}
`
Loading

0 comments on commit c4c0810

Please sign in to comment.