Skip to content

Commit

Permalink
fix: Updated examples: pass dbConnection to Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Apr 9, 2020
1 parent e3dfc2f commit d84872d
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 239 deletions.
33 changes: 14 additions & 19 deletions examples/expressjs-ethr/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { JwtMessageHandler } from 'daf-did-jwt'
import * as EthrDid from 'daf-ethr-did'
import * as DafLibSodium from 'daf-libsodium'
import { W3cMessageHandler, W3cActionHandler } from 'daf-w3c'
import { SdrMessageHandler, SdrActionHandler} from 'daf-selective-disclosure'
import { DIDCommMessageHandler, DIDCommActionHandler} from 'daf-did-comm'
import { SdrMessageHandler, SdrActionHandler } from 'daf-selective-disclosure'
import { DIDCommMessageHandler, DIDCommActionHandler } from 'daf-did-comm'
import { UrlMessageHandler } from 'daf-url'
import { createConnection } from 'typeorm'
import { DataStore } from 'daf-data-store'
Expand All @@ -25,10 +25,18 @@ if (process.env.DAF_UNIVERSAL_RESOLVER_URL) {
})
}

const dbConnection = createConnection({
type: 'sqlite',
database: './database.sqlite',
synchronize: true,
logging: false,
entities: [...Daf.Entities],
})

const identityProviders = [
new EthrDid.IdentityProvider({
identityStore: new Daf.IdentityStore('rinkeby-ethr'),
kms: new DafLibSodium.KeyManagementSystem(new Daf.KeyStore()),
identityStore: new Daf.IdentityStore('rinkeby-ethr', dbConnection),
kms: new DafLibSodium.KeyManagementSystem(new Daf.KeyStore(dbConnection)),
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
}),
Expand All @@ -43,26 +51,13 @@ messageHandler
.setNext(new SdrMessageHandler())

const actionHandler = new DIDCommActionHandler()
actionHandler
.setNext(new W3cActionHandler())
.setNext(new SdrActionHandler())
actionHandler.setNext(new W3cActionHandler()).setNext(new SdrActionHandler())

export const agent = new Daf.Agent({
dbConnection,
identityProviders,
serviceControllers,
didResolver,
messageHandler,
actionHandler,
})

export const initializeDb = async () => {
await createConnection({
type: 'sqlite',
database: './database.sqlite',
synchronize: true,
logging: false,
entities: [...Daf.Entities],
})
}

export const dataStore = new DataStore()
12 changes: 5 additions & 7 deletions examples/expressjs-ethr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ 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 { agent, dataStore, initializeDb } from './framework'
import { agent } 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 initializeDb()
const identity = await getIdentity()

const messagingEndpoint = '/handle-message'
Expand Down Expand Up @@ -69,8 +68,7 @@ async function main() {
req.session.views = req.session.views ? req.session.views + 1 : 1

const { did, views } = req.session
const name = await dataStore.shortId(did)
res.render('home', { did, views, name })
res.render('home', { did, views })
})

app.get('/history', requireLogin, async (req, res) => {
Expand All @@ -80,8 +78,9 @@ async function main() {
req.session.views = req.session.views ? req.session.views + 1 : 1

const { did, views } = req.session
const name = await dataStore.shortId(did)
const messages = await dataStore.findMessages({ sender: did })
const messages = await (await agent.dbConnection)
.getRepository(Daf.Message)
.find({ where: { from: did } })
console.log(messages)
res.render('history', { did, views, name, messages })
})
Expand Down Expand Up @@ -120,7 +119,6 @@ async function main() {
req.session.views = req.session.views ? req.session.views + 1 : 1

const { did, views } = req.session
const name = await dataStore.shortId(did)

// Sign verifiable credential
const nameJwt = await agent.handleAction({
Expand Down
11 changes: 4 additions & 7 deletions examples/expressjs-ethr/views/history.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
<h2>DID</h2>
<p>{{did}}</p>

<h2>Name</h2>
<p>{{name}}</p>

<table class="table table-hover">
<thead>
<tr>
<th scope="col">Timestamp</th>
<th scope="col">Issuance Date</th>
<th scope="col">Type</th>
<th scope="col">Sender</th>
<th scope="col">Receiver</th>
Expand All @@ -18,10 +15,10 @@
<tbody>
{{#each messages}}
<tr>
<td>{{timestamp}}</th>
<td>{{issuanceDate}}</th>
<td>{{type}}</th>
<td>{{sender.did}}</th>
<td>{{receiver.did}}</th>
<td>{{from.did}}</th>
<td>{{to.did}}</th>
</tr>
{{~/each}}
</tbody>
Expand Down
3 changes: 0 additions & 3 deletions examples/expressjs-ethr/views/home.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@

<h2>DID</h2>
<p>{{did}}</p>

<h2>Name</h2>
<p>{{name}}</p>
47 changes: 24 additions & 23 deletions examples/expressjs-ethr/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -613,51 +613,51 @@ cross-fetch@^3.0.4:
node-fetch "2.6.0"
whatwg-fetch "3.0.0"

daf-core@../../packages/daf-core, daf-core@^4.0.0-beta.15+00f0d2f:
version "4.0.0-beta.15"
daf-core@../../packages/daf-core, daf-core@^4.0.0-beta.39+63dd12d:
version "4.0.0-beta.39"
dependencies:
blakejs "^1.1.0"
debug "^4.1.1"
events "^3.0.0"
typeorm "^0.2.24"

daf-data-store@../../packages/daf-data-store, daf-data-store@^4.0.0-beta.15+00f0d2f:
version "4.0.0-beta.15"
daf-data-store@../../packages/daf-data-store, daf-data-store@^4.0.0-beta.39+63dd12d:
version "4.0.0-beta.39"
dependencies:
blakejs "^1.1.0"
daf-core "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
sql-bricks-sqlite "^0.1.0"

daf-did-comm@../../packages/daf-did-comm:
version "4.0.0-beta.15"
version "4.0.0-beta.39"
dependencies:
daf-core "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
uuid "^3.3.3"

daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^4.0.0-beta.15+00f0d2f:
version "4.0.0-beta.15"
daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^4.0.0-beta.39+63dd12d:
version "4.0.0-beta.39"
dependencies:
daf-core "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
did-jwt "^4.0.0"
did-resolver "^1.1.0"

daf-ethr-did@../../packages/daf-ethr-did:
version "4.0.0-beta.15"
version "4.0.0-beta.39"
dependencies:
daf-core "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
ethjs-provider-signer "^0.1.4"
ethr-did "^1.1.0"
js-sha3 "^0.8.0"

daf-libsodium@../../packages/daf-libsodium:
version "4.0.0-beta.15"
version "4.0.0-beta.39"
dependencies:
base-58 "^0.0.1"
daf-core "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
did-jwt "^4.0.0"
elliptic "^6.5.2"
Expand All @@ -680,28 +680,29 @@ daf-resolver@../../packages/daf-resolver:
web-did-resolver "^1.2.0"

daf-selective-disclosure@../../packages/daf-selective-disclosure:
version "4.0.0-beta.15"
version "4.0.0-beta.41"
dependencies:
blakejs "^1.1.0"
daf-core "^4.0.0-beta.15+00f0d2f"
daf-data-store "^4.0.0-beta.15+00f0d2f"
daf-did-jwt "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
daf-data-store "^4.0.0-beta.39+63dd12d"
daf-did-jwt "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
did-jwt "^4.0.0"
typeorm "^0.2.24"

daf-url@../../packages/daf-url:
version "4.0.0-beta.15"
version "4.0.0-beta.39"
dependencies:
daf-core "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
url-parse "^1.4.7"

daf-w3c@../../packages/daf-w3c:
version "4.0.0-beta.15"
version "4.0.0-beta.39"
dependencies:
blakejs "^1.1.0"
daf-core "^4.0.0-beta.15+00f0d2f"
daf-did-jwt "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
daf-did-jwt "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
did-jwt-vc "^0.1.3"
did-resolver "^1.1.0"
Expand Down
21 changes: 10 additions & 11 deletions examples/id-hub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c'
let didResolver = new DafResolver({ infuraProjectId })

const messageHandler = new JwtMessageHandler()
messageHandler
.setNext(new W3cMessageHandler())
.setNext(new SdrMessageHandler())
messageHandler.setNext(new W3cMessageHandler()).setNext(new SdrMessageHandler())

const dbConnection = createConnection({
type: 'sqlite',
database: './database.sqlite',
synchronize: true,
logging: false,
entities: [...Daf.Entities],
})

export const agent = new Daf.Agent({
dbConnection,
identityProviders: [],
serviceControllers: [],
didResolver,
Expand All @@ -43,14 +50,6 @@ agent.on(Daf.EventTypes.savedMessage, async (message: Daf.Message) => {
})

const main = async () => {
const c = await createConnection({
type: 'sqlite',
database: './database.sqlite',
synchronize: true,
logging: false,
entities: [...Daf.Entities],
})

const info = await server.listen()
console.log(`🚀 Server ready at ${info.url}`)
}
Expand Down
35 changes: 18 additions & 17 deletions examples/id-hub/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -861,28 +861,28 @@ cross-fetch@^3.0.4:
node-fetch "2.6.0"
whatwg-fetch "3.0.0"

daf-core@../../packages/daf-core, daf-core@^4.0.0-beta.15+00f0d2f:
version "4.0.0-beta.15"
daf-core@../../packages/daf-core, daf-core@^4.0.0-beta.39+63dd12d:
version "4.0.0-beta.39"
dependencies:
blakejs "^1.1.0"
debug "^4.1.1"
events "^3.0.0"
typeorm "^0.2.24"

daf-data-store@^4.0.0-beta.15+00f0d2f:
version "4.0.0-beta.15"
resolved "https://registry.yarnpkg.com/daf-data-store/-/daf-data-store-4.0.0-beta.15.tgz#47bdb81fcec83c6a18c20299f2326922ce02697a"
integrity sha512-5vyyYzdqYq4nC841xoVm62AKom6z1YPulJUBo28P95Y2JPUjOmN2+LBnMd4ZPc73Kt3ix0kUCUdZmgebZL+Nhg==
daf-data-store@^4.0.0-beta.39+63dd12d:
version "4.0.0-beta.39"
resolved "https://registry.yarnpkg.com/daf-data-store/-/daf-data-store-4.0.0-beta.39.tgz#e328aa8b5583a07a2e279f5e3da0df65721b31ff"
integrity sha512-DaOeBUZo5l9Dlg2vrFLuxCMMqZGofkFK7q72tmOMdTDUkKfP0BtacB4wmr5PeTSDOtJosKcHI3uVAHI4Ksq0Wg==
dependencies:
blakejs "^1.1.0"
daf-core "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
sql-bricks-sqlite "^0.1.0"

daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^4.0.0-beta.15+00f0d2f:
version "4.0.0-beta.15"
daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^4.0.0-beta.39+63dd12d:
version "4.0.0-beta.39"
dependencies:
daf-core "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
did-jwt "^4.0.0"
did-resolver "^1.1.0"
Expand All @@ -897,21 +897,22 @@ daf-resolver@../../packages/daf-resolver:
web-did-resolver "^1.2.0"

daf-selective-disclosure@../../packages/daf-selective-disclosure:
version "4.0.0-beta.15"
version "4.0.0-beta.41"
dependencies:
blakejs "^1.1.0"
daf-core "^4.0.0-beta.15+00f0d2f"
daf-data-store "^4.0.0-beta.15+00f0d2f"
daf-did-jwt "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
daf-data-store "^4.0.0-beta.39+63dd12d"
daf-did-jwt "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
did-jwt "^4.0.0"
typeorm "^0.2.24"

daf-w3c@../../packages/daf-w3c:
version "4.0.0-beta.15"
version "4.0.0-beta.39"
dependencies:
blakejs "^1.1.0"
daf-core "^4.0.0-beta.15+00f0d2f"
daf-did-jwt "^4.0.0-beta.15+00f0d2f"
daf-core "^4.0.0-beta.39+63dd12d"
daf-did-jwt "^4.0.0-beta.39+63dd12d"
debug "^4.1.1"
did-jwt-vc "^0.1.3"
did-resolver "^1.1.0"
Expand Down
3 changes: 2 additions & 1 deletion examples/react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ const App: React.FC = () => {

try {
const credentialSubject: any = {}
;(credentialSubject['id'] = receiver), (credentialSubject[claimType] = claimValue)
credentialSubject['id'] = receiver
credentialSubject[claimType] = claimValue

const credential = await agent.handleAction({
type: W3c.ActionTypes.signCredentialJwt,
Expand Down
Loading

0 comments on commit d84872d

Please sign in to comment.