Skip to content

Commit

Permalink
feat: Daf-rest & daf-express
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat authored and mirceanis committed Sep 7, 2020
1 parent babbfe6 commit 9c9a597
Show file tree
Hide file tree
Showing 23 changed files with 1,406 additions and 366 deletions.
54 changes: 27 additions & 27 deletions examples/remote-methods/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
import 'cross-fetch/polyfill'
import { Agent } from 'daf-core'
import { GraphQLAgentPlugin } from '../lib/daf-graphql'
import { RESTAgentPlugin } from '../lib/daf-rest'
import { DafGrpc } from '../lib/daf-grpc'
import { IAgent, IAgentIdentityManager } from 'daf-core'
import { IAgentResolve } from 'daf-resolver'
// import { GraphQLAgentPlugin } from '../lib/daf-graphql'
import { AgentRestClient } from 'daf-rest'
// import { DafGrpc } from '../lib/daf-grpc'
import { IAgentBase, IAgentIdentityManager, IAgentResolve } from 'daf-core'

export type ConfiguredAgent = IAgent & IAgentIdentityManager & IAgentResolve
export type ConfiguredAgent = IAgentBase & IAgentIdentityManager & IAgentResolve

const agent: ConfiguredAgent = new Agent({
plugins: [
new GraphQLAgentPlugin({
url: 'http://localhost:3000',
apiKey: 'example-token',
methods: [
// 'resolve',
'getIdentityProviders',
'getIdentities',
'getIdentity',
'createIdentity',
'deleteIdentity',
],
}),
new DafGrpc({
url: 'http://localhost:3001',
new AgentRestClient({
url: 'http://localhost:3002/agent',
methods: [
// 'signCredentialJwt',
'resolveDid',
'identityManagerGetProviders',
'identityManagerGetIdentities',
'identityManagerGetIdentity',
'identityManagerCreateIdentity',
],
}),
new RESTAgentPlugin({
url: 'http://localhost:3002/agent',
methods: ['resolve'],
}),
// new DafGrpc({
// url: 'http://localhost:3001',
// methods: [
// // 'signCredentialJwt',
// ],
// }),
// new RESTAgentPlugin({
// url: 'http://localhost:3002/agent',
// methods: ['resolve'],
// }),
],
})

async function main() {
const providers = await agent.getIdentityProviders()
const providers = await agent.identityManagerGetProviders()
console.log({ providers })

// const newIdentity = await agent.createIdentity({ identityProviderType: 'rinkeby-ethr-did' })
Expand All @@ -47,7 +45,9 @@ async function main() {
// const identity = await agent.getIdentity({ did: identities[0].did })
// console.log({ identity })

const doc = await agent.resolve({ did: 'did:ethr:rinkeby:0x79292ba5a516f04c3de11e8f06642c7bec16c490' })
const doc = await agent.resolveDid({
didUrl: 'did:ethr:rinkeby:0x79292ba5a516f04c3de11e8f06642c7bec16c490',
})
console.log(doc)
}

Expand Down
13 changes: 0 additions & 13 deletions examples/remote-methods/lib/daf-rest/methods.ts

This file was deleted.

5 changes: 5 additions & 0 deletions examples/remote-methods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"daf-did-comm": "../../packages/daf-did-comm",
"daf-did-jwt": "../../packages/daf-did-jwt",
"daf-ethr-did": "../../packages/daf-ethr-did",
"daf-express": "../../packages/daf-express",
"daf-libsodium": "../../packages/daf-libsodium",
"daf-resolver": "../../packages/daf-resolver",
"daf-rest": "../../packages/daf-rest",
"daf-w3c": "../../packages/daf-w3c",
"express": "^4.17.1",
"graphql": "^15.0.0",
Expand All @@ -29,5 +31,8 @@
"ts-node": "^8.10.2",
"typeorm": "^0.2.24",
"typescript": "^3.9.3"
},
"resolutions": {
"daf-rest": "../../packages/daf-rest"
}
}
11 changes: 9 additions & 2 deletions examples/remote-methods/server-rest-express/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express'
import { agent } from './setup'
import { AgentExpressMiddleware } from '../lib/daf-expressjs'
import { AgentExpressMiddleware } from 'daf-express'

const app = express()
app.use(express.json())
Expand All @@ -10,7 +10,14 @@ app.use(
agent,
prefix: '/agent',
// methods: agent.availableMethods(),
methods: ['resolve'],
methods: [
'resolveDid',
'identityManagerGetProviders',
'identityManagerGetIdentities',
'identityManagerGetIdentity',
'identityManagerCreateIdentity',
'identityManagerCreateIdentity',
],
}),
)

Expand Down
101 changes: 49 additions & 52 deletions examples/remote-methods/server-rest-express/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { Entities } from 'daf-core'
import {
Agent,
KeyManager,
MessageHandler,
IdentityManager,
IAgentBase,
IAgentIdentityManager,
IAgentResolve,
IAgentDataStore,
IAgentKeyManager,
IAgentHandleMessage,
} from 'daf-core'
import { DafResolver } from 'daf-resolver'
import { JwtMessageHandler } from 'daf-did-jwt'
import { W3c, IAgentW3c, W3cMessageHandler } from 'daf-w3c'
import { DIDComm, DIDCommMessageHandler, IAgentSendMessageDIDCommAlpha1 } from 'daf-did-comm'
import { EthrIdentityProvider } from 'daf-ethr-did'
import { KeyManagementSystem, SecretBox } from 'daf-libsodium'
import { Entities, KeyStore, IdentityStore, DataStore, DataStoreORM, IAgentDataStoreORM } from 'daf-typeorm'
import { createConnection } from 'typeorm'

// Create database connection
const dbConnection = createConnection({
type: 'sqlite',
database: 'database.sqlite',
Expand All @@ -10,68 +27,48 @@ const dbConnection = createConnection({
entities: Entities,
})

// We will be using 'did:ethr' identities
import { IdentityProvider } from 'daf-ethr-did'

// SecretBox will encrypt/decrypt private keys in key store
import { SecretBox } from 'daf-libsodium'

// Generate this by running `npx daf-cli crypto -s`
const secretKey = '29739248cad1bd1a0fc4d9b75cd4d2990de535baf5caadfdf8d8f86664aa830c'
const secretBox = new SecretBox(secretKey)

// Storing serialized key pairs in the file system
import { KeyStore } from 'daf-core'
const keyStore = new KeyStore(dbConnection, secretBox)

// 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-core'
const identityStore = new IdentityStore('rinkeby-ethr', dbConnection)

// Infura is being used to access Ethereum blockchain. https://infura.io
const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c'

// Injecting required dependencies, and specifying which blockchain to use and how to access it
const rinkebyIdentityProvider = new IdentityProvider({
kms,
identityStore,
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
})

// Using local DID Document resolver. It is being used internally to
/// validate messages and to get information about service endpoints
import { DafResolver, IAgentResolve } from 'daf-resolver'

// Setting up Message Validator Chain
import { IAgentHandleMessage, HandleMessage } from 'daf-core'
import { JwtMessageHandler } from 'daf-did-jwt'
import { W3cMessageHandler } from 'daf-w3c'

// Setting up Action Handler Chain
import { DIDComm, IAgentSendMessageDIDCommAlpha1 } from 'daf-did-comm'
import { W3c, IAgentW3c } from 'daf-w3c'

// Initializing the Core by injecting dependencies
import { Agent, IAgent, IAgentIdentityManager, IdentityManager } from 'daf-core'
export type ConfiguredAgent = IAgent &
type ConfiguredAgent = IAgentBase &
IAgentIdentityManager &
IAgentKeyManager &
IAgentDataStore &
IAgentDataStoreORM &
IAgentResolve &
IAgentHandleMessage &
IAgentSendMessageDIDCommAlpha1 &
IAgentW3c

export const agent: ConfiguredAgent = new Agent({
context: {
// authenticatedDid: 'did:example:3456'
},
plugins: [
new IdentityManager({ identityProviders: [rinkebyIdentityProvider] }),
new KeyManager({
store: new KeyStore(dbConnection, new SecretBox(secretKey)),
kms: {
local: new KeyManagementSystem(),
},
}),
new IdentityManager({
store: new IdentityStore(dbConnection),
defaultProvider: 'did:ethr:rinkeby',
providers: {
'did:ethr:rinkeby': new EthrIdentityProvider({
defaultKms: 'local',
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
gas: 1000001,
ttl: 60 * 60 * 24 * 30 * 12 + 1,
}),
},
}),
new DafResolver({ infuraProjectId }),
new HandleMessage({
dbConnection,
messageHandlers: [new JwtMessageHandler(), new W3cMessageHandler()],
new DataStore(dbConnection),
new DataStoreORM(dbConnection),
new MessageHandler({
messageHandlers: [new DIDCommMessageHandler(), new JwtMessageHandler(), new W3cMessageHandler()],
}),
new DIDComm(),
new W3c(),
Expand Down
73 changes: 73 additions & 0 deletions examples/remote-methods/yarn-error.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
Arguments:
/Users/simonas/.nvm/versions/node/v10.18.1/bin/node /Users/simonas/.yarn/bin/yarn.js

PATH:
/Users/simonas/.nvm/versions/node/v10.18.1/bin:/Users/simonas/.yarn/bin:/Users/simonas/.config/yarn/global/node_modules/.bin:/Users/simonas/.nvm/versions/node/v12.13.1/bin:/Users/simonas/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/simonas/.yarn/bin:/Users/simonas/.config/yarn/global/node_modules/.bin:/Users/simonas/.nvm/versions/node/v12.13.1/bin:/Users/simonas/.cargo/bin:/Users/simonas/Library/Android/sdk/tools:/Users/simonas/Library/Android/sdk/platform-tools:/Users/simonas/Library/Android/sdk/tools:/Users/simonas/Library/Android/sdk/platform-tools

Yarn version:
1.21.1

Node version:
10.18.1

Platform:
darwin x64

Trace:
Error: https://registry.yarnpkg.com/daf-rest: Not found
at Request.params.callback [as _callback] (/Users/simonas/.yarn/lib/cli.js:66947:18)
at Request.self.callback (/Users/simonas/.yarn/lib/cli.js:140665:22)
at Request.emit (events.js:198:13)
at Request.<anonymous> (/Users/simonas/.yarn/lib/cli.js:141637:10)
at Request.emit (events.js:198:13)
at IncomingMessage.<anonymous> (/Users/simonas/.yarn/lib/cli.js:141559:12)
at Object.onceWrapper (events.js:286:20)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

npm manifest:
{
"name": "remote-methods",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start:client": "DEBUG=daf:* ts-node client/index.ts",
"start:rest": "DEBUG=daf:* ts-node server-rest-express/index.ts",
"start:graphql": "DEBUG=daf:* ts-node server-graphql-apollo/index.ts",
"start:grpc": "DEBUG=daf:* ts-node server-grpc/index.ts"
},
"dependencies": {
"@types/express": "^4.17.6",
"apollo-server": "^2.14.1",
"cross-fetch": "^3.0.4",
"daf-core": "../../packages/daf-core",
"daf-did-comm": "../../packages/daf-did-comm",
"daf-did-jwt": "../../packages/daf-did-jwt",
"daf-ethr-did": "../../packages/daf-ethr-did",
"daf-express": "../../packages/daf-express",
"daf-libsodium": "../../packages/daf-libsodium",
"daf-resolver": "../../packages/daf-resolver",
"daf-rest": "../../packages/daf-rest",
"daf-w3c": "../../packages/daf-w3c",
"express": "^4.17.1",
"graphql": "^15.0.0",
"graphql-request": "^2.0.0",
"graphql-tag": "^2.10.3",
"graphql-tools": "^6.0.6",
"sqlite3": "^4.1.1",
"ts-node": "^8.10.2",
"typeorm": "^0.2.24",
"typescript": "^3.9.3"
},
"resolutions": {
"daf-rest": "^6.0.0"
}
}

yarn manifest:
No manifest

Lockfile:
No lockfile
Loading

0 comments on commit 9c9a597

Please sign in to comment.