Skip to content

Commit

Permalink
fix: Express example
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Nov 28, 2019
1 parent 12f8115 commit 1c33310
Show file tree
Hide file tree
Showing 11 changed files with 2,928 additions and 3,281 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.swp
*~
/examples/*/node_modules/
/examples/*/build/

/node_modules
website/node_modules
Expand Down
3,208 changes: 0 additions & 3,208 deletions examples/expressjs/package-lock.json

This file was deleted.

29 changes: 15 additions & 14 deletions examples/expressjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "ts-node src/web-server",
"watch": "tsc -b --watch",
"start": "node build/web-server",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"apollo-server-express": "^2.9.9",
"daf-core": "0.0.22",
"daf-data-store": "0.0.22",
"daf-debug": "0.0.22",
"daf-did-comm": "0.0.22",
"daf-did-jwt": "0.0.22",
"daf-ethr-did-fs": "0.0.22",
"daf-node-sqlite3": "0.0.22",
"daf-resolver": "0.0.22",
"daf-resolver-universal": "0.0.22",
"daf-selective-disclosure": "0.0.22",
"daf-sodium-fs": "0.0.22",
"daf-trust-graph": "0.0.22",
"daf-w3c": "0.0.22",
"daf-core": "../../packages/daf-core",
"daf-data-store": "../../packages/daf-data-store",
"daf-debug": "../../packages/daf-debug",
"daf-did-comm": "../../packages/daf-did-comm",
"daf-did-jwt": "../../packages/daf-did-jwt",
"daf-ethr-did-fs": "../../packages/daf-ethr-did-fs",
"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",
"daf-sodium-fs": "../../packages/daf-sodium-fs",
"daf-trust-graph": "../../packages/daf-trust-graph",
"daf-w3c": "../../packages/daf-w3c",
"debug": "^4.1.1",
"express": "^4.17.1",
"lodash.merge": "^4.6.2",
Expand Down
26 changes: 10 additions & 16 deletions examples/expressjs/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import ws from 'ws'
import Debug from 'debug'
const debug = Debug('main')

const defaultPath = process.env.HOME + '/.daf'
const defaultPath = __dirname + '/.daf'

const identityStoreFilename = process.env.DAF_IDENTITY_STORE ?? defaultPath + '/identity-store.json'
const dataStoreFilename = process.env.DAF_DATA_STORE ?? defaultPath + '/data-store.sqlite3'
Expand All @@ -35,28 +35,23 @@ if (!process.env.DAF_IDENTITY_STORE || process.env.DAF_DATA_STORE || process.env

// DID Document Resolver
let didResolver: Daf.Resolver = new DafResolver({
infuraProjectId
infuraProjectId,
})

if (process.env.DAF_UNIVERSAL_RESOLVER_URL) {
didResolver = new DafUniversalResolver({
url: process.env.DAF_UNIVERSAL_RESOLVER_URL
url: process.env.DAF_UNIVERSAL_RESOLVER_URL,
})
}

const identityControllers = [new EthrDidFsController(identityStoreFilename)]

const messageValidator = new DBG.MessageValidator()
messageValidator
.setNext(new DIDComm.MessageValidator())
.setNext(
new DidJwt.MessageValidator({
payloadValidators: [
new W3c.PayloadValidator(),
new SD.PayloadValidator(),
],
}),
)
messageValidator.setNext(new DIDComm.MessageValidator()).setNext(
new DidJwt.MessageValidator({
payloadValidators: [new W3c.PayloadValidator(), new SD.PayloadValidator()],
}),
)

const actionHandler = new DBG.ActionHandler()
actionHandler
Expand Down Expand Up @@ -89,13 +84,12 @@ export const core = new Daf.Core({
didResolver,
messageValidator,
actionHandler,
encryptionKeyManager
encryptionKeyManager,
})

const db = new NodeSqlite3(dataStoreFilename)
export const dataStore = new DataStore(db)


const { ApolloServer } = require('apollo-server-express')
import merge from 'lodash.merge'

Expand All @@ -118,4 +112,4 @@ export const apolloServer = new ApolloServer({
context: () => ({ dataStore, core }),
introspection: true,
graphqlPath: '/graphql',
})
})
18 changes: 7 additions & 11 deletions examples/expressjs/src/web-did-doc.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { KeyPair } from 'daf-core'

export const webDidDocFromEthrDid = (
ethrDid: string,
hostname: string,
keyPair: KeyPair,
) => ({
export const webDidDocFromEthrDid = (ethereumAddress: string, hostname: string, keyPair: KeyPair) => ({
'@context': 'https://w3id.org/did/v1',
id: `did:web:${hostname}`,
publicKey: [
{
id: `did:web:${hostname}#owner`,
type: 'Secp256k1VerificationKey2018',
owner: `did:web:${hostname}`,
ethereumAddress: ethrDid.slice(9),
ethereumAddress,
},
{
id: `did:web:${hostname}#enc`,
Expand All @@ -28,11 +24,11 @@ export const webDidDocFromEthrDid = (
},
],
service: [
{
id: `did:web:${hostname}#messages`,
type: 'MessagingService',
serviceEndpoint: `https://${hostname}/didcomm`,
},
// {
// id: `did:web:${hostname}#messages`,
// type: 'MessagingService',
// serviceEndpoint: `https://${hostname}/didcomm`,
// },
// {
// id: `did:web:${hostname}#trustgraph`,
// type: 'TrustGraph',
Expand Down
32 changes: 10 additions & 22 deletions examples/expressjs/src/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ const bodyParser = require('body-parser')
import { core, dataStore, apolloServer } from './setup'
import { webDidDocFromEthrDid } from './web-did-doc'


import Debug from 'debug'
Debug.enable('*')
const debug = Debug('main')

core.on(
Daf.EventTypes.validatedMessage,
async (eventType: string, message: Daf.Types.ValidatedMessage) => {
debug('New message %s', message.hash)
debug('Meta %O', message.meta)
await dataStore.saveMessage(message)
},
)
core.on(Daf.EventTypes.validatedMessage, async (eventType: string, message: Daf.Types.ValidatedMessage) => {
debug('New message %s', message.hash)
debug('Meta %O', message.meta)
await dataStore.saveMessage(message)
})

let hostname: string

Expand All @@ -37,9 +33,7 @@ async function main() {
// Get of create new encryption keyPair
let keyPair: Daf.KeyPair
if (core.encryptionKeyManager) {
const existingKeyPair = await core.encryptionKeyManager.getKeyPairForDid(
issuer.did,
)
const existingKeyPair = await core.encryptionKeyManager.getKeyPairForDid(issuer.did)

if (!existingKeyPair) {
keyPair = await core.encryptionKeyManager.createKeyPairForDid(issuer.did)
Expand All @@ -54,16 +48,11 @@ async function main() {

app.get('/', async (req, res) => {
const messages = await dataStore.findMessages({})
res.send(
'Messages:<br/>' +
messages.map(
(message: any) => `${message.type} - ${message.hash}<br/>`,
),
)
res.send('Messages:<br/>' + messages.map((message: any) => `${message.type} - ${message.hash}<br/>`))
})

app.get('/.well-known/did.json', (req, res) =>
res.send(webDidDocFromEthrDid(issuer.did, hostname, keyPair)),
res.send(webDidDocFromEthrDid(issuer.ethereumAddress ? issuer.ethereumAddress : '', hostname, keyPair)),
)

app.post('/didcomm', async (req, res) => {
Expand All @@ -88,7 +77,7 @@ async function main() {
debug(`Listening on port ${port}!`)
const url = await ngrok.connect({
addr: port,
// subdomain: 'custom',
// subdomain: 'someservice',
// region: 'eu',
})
debug(`URL: ${url}`)
Expand All @@ -97,9 +86,8 @@ async function main() {
hostname = url.slice(8)
debug(`did:web:${hostname}`)


await core.startServices()

await core.syncServices(await dataStore.latestMessageTimestamps())
})
}

Expand Down
25 changes: 22 additions & 3 deletions examples/expressjs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,31 @@
"strict": true,
"sourceMap": true,
"target": "es5",
"rootDir": "./src",
"outDir": "./build",
"moduleResolution": "node",
"esModuleInterop": true,
"downlevelIteration": true,
"allowJs": true,
"downlevelIteration": true
},
"include": ["./src/**/*"],
"exclude": ["node_modules"]
"exclude": ["node_modules"],
"references": [
{ "path": "../../packages/daf-cli" },
{ "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-fs" },
{ "path": "../../packages/daf-ethr-did-react-native" },
{ "path": "../../packages/daf-node-sqlite3" },
{ "path": "../../packages/daf-node-sqlite3" },
{ "path": "../../packages/daf-random" },
{ "path": "../../packages/daf-resolver" },
{ "path": "../../packages/daf-resolver-universal" },
{ "path": "../../packages/daf-selective-disclosure" },
{ "path": "../../packages/daf-sodium-fs" },
{ "path": "../../packages/daf-trust-graph" },
{ "path": "../../packages/daf-w3c" }
]
}
Loading

0 comments on commit 1c33310

Please sign in to comment.