Skip to content

Commit

Permalink
feat: Ngrok support in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Sep 15, 2020
1 parent 6d30cff commit 56c464d
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 57 deletions.
49 changes: 49 additions & 0 deletions packages/daf-cli/default/client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
agent:
$require: daf-core#Agent
$args:
- plugins:
- $require: daf-rest#AgentRestClient
$args:
- url: http://localhost:3332
enabledMethods:
- keyManagerGetKeyManagementSystems
- keyManagerCreateKey
- keyManagerGetKey
- keyManagerDeleteKey
- keyManagerImportKey
- keyManagerEncryptJWE
- keyManagerDecryptJWE
- keyManagerSignJWT
- keyManagerSignEthTX
- identityManagerGetProviders
- identityManagerGetIdentities
- identityManagerGetIdentity
- identityManagerCreateIdentity
- identityManagerGetOrCreateIdentity
- identityManagerImportIdentity
- identityManagerDeleteIdentity
- identityManagerAddKey
- identityManagerRemoveKey
- identityManagerAddService
- identityManagerRemoveService
- resolveDid
- dataStoreSaveMessage
- dataStoreSaveVerifiableCredential
- dataStoreSaveVerifiablePresentation
- dataStoreORMGetIdentities
- dataStoreORMGetIdentitiesCount
- dataStoreORMGetMessages
- dataStoreORMGetMessagesCount
- dataStoreORMGetVerifiableCredentialsByClaims
- dataStoreORMGetVerifiableCredentialsByClaimsCount
- dataStoreORMGetVerifiableCredentials
- dataStoreORMGetVerifiableCredentialsCount
- dataStoreORMGetVerifiablePresentations
- dataStoreORMGetVerifiablePresentationsCount
- handleMessage
- sendMessageDIDCommAlpha1
- createVerifiablePresentation
- createVerifiableCredential
- createSelectiveDisclosureRequest
- getVerifiableCredentialsForSdr
- validatePresentationAgainstSdr
2 changes: 2 additions & 0 deletions packages/daf-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
"inquirer": "^7.0.0",
"jsonpointer": "^4.1.0",
"lodash.merge": "^4.6.2",
"ngrok": "^3.3.0",
"pg": "^8.2.0",
"qrcode-terminal": "^0.12.0",
"sqlite3": "^5.0.0",
"typeorm": "0.2.24",
"url-parse": "^1.4.7",
"ws": "^7.2.0",
"yaml": "^1.10.0"
},
Expand Down
128 changes: 74 additions & 54 deletions packages/daf-cli/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import express from 'express'
import program from 'commander'
import ngrok from 'ngrok'
import parse from 'url-parse'
import { AgentRouter } from 'daf-express'
import { getAgent } from './setup'

Expand All @@ -8,7 +10,9 @@ program
.description('Launch OpenAPI server')
.option('--port <port>', 'Port', '3332')
.option('--hostname <string>', 'Server hostname', 'localhost')
.option('--https <boolean>', 'Use https instead of http', true)
.option('--ngrok', 'Open ngrok tunnel')
.option('--ngrokSubdomain <string>', 'ngrok subdomain')
.option('--ngrokRegion <string>', 'ngrok region')
.option('--createDefaultIdentity <boolean>', 'Should the agent create default web did', true)
.option('--messagingServiceEndpoint <path>', 'Path of the messaging service endpoint', '/messaging')
.option(
Expand All @@ -31,70 +35,86 @@ program

app.use('/', agentRouter)

if (options.createDefaultIdentity) {
let serverIdentity = await agent.identityManagerGetOrCreateIdentity({
provider: 'did:web',
alias: options.hostname,
})
console.log('Created:', serverIdentity.did)
app.listen(options.port, async () => {
console.log(`🚀 Agent server ready at http://localhost:${options.port}`)
console.log('🧩 Available methods', JSON.stringify(agent.availableMethods()))
console.log('🛠 Exposed methods', JSON.stringify(exposedMethods))

if (options.messagingServiceEndpoint) {
const serviceEndpoint =
(options.https ? 'https://' : 'http://') + options.hostname + options.messagingServiceEndpoint
let hostname = options.hostname

await agent.identityManagerAddService({
did: serverIdentity.did,
service: {
id: 'msg',
type: 'Messaging',
serviceEndpoint,
},
})
console.log('Added endpoint', serviceEndpoint)
let baseUrl = 'http://' + hostname + ':' + options.port

app.post(serviceEndpoint, express.text({ type: '*/*' }), async (req, res) => {
try {
const message = await agent.handleMessage({ raw: req.body, save: true })
res.json({ id: message.id })
} catch (e) {
console.log(e)
res.send(e.message)
}
if (options.ngrok) {
baseUrl = await ngrok.connect({
addr: options.port,
subdomain: options.ngrokSubdomain,
region: options.ngrokRegion,
})
hostname = parse(baseUrl).hostname
}

app.get('/.well-known/did.json', async (req, res) => {
serverIdentity = await agent.identityManagerGetOrCreateIdentity({
if (options.createDefaultIdentity) {
let serverIdentity = await agent.identityManagerGetOrCreateIdentity({
provider: 'did:web',
alias: options.hostname,
alias: hostname,
})
console.log('🆔', serverIdentity.did)

if (options.messagingServiceEndpoint) {
const serviceEndpoint = 'https://' + hostname + options.messagingServiceEndpoint

await agent.identityManagerAddService({
did: serverIdentity.did,
service: {
id: serverIdentity.did + '#msg',
type: 'Messaging',
description: 'Handles incoming POST messages',
serviceEndpoint,
},
})
console.log('📨 Messaging endpoint', serviceEndpoint)

const didDoc = {
'@context': 'https://w3id.org/did/v1',
id: serverIdentity.did,
publicKey: serverIdentity.keys.map((key) => ({
id: serverIdentity.did + '#' + key.kid,
type: key.type === 'Secp256k1' ? 'Secp256k1VerificationKey2018' : 'Ed25519VerificationKey2018',
owner: serverIdentity.did,
publicKeyHex: key.publicKeyHex,
})),
authentication: serverIdentity.keys.map((key) => ({
type:
key.type === 'Secp256k1'
? 'Secp256k1SignatureAuthentication2018'
: 'Ed25519SignatureAuthentication2018',
publicKey: serverIdentity.did + '#' + key.kid,
})),
service: serverIdentity.services,
app.post(options.messagingServiceEndpoint, express.text({ type: '*/*' }), async (req, res) => {
try {
const message = await agent.handleMessage({ raw: req.body, save: true })
console.log('Received message', message.type, message.id)
res.json({ id: message.id })
} catch (e) {
console.log(e)
res.send(e.message)
}
})
}

res.json(didDoc)
})
}
const didDocEndpoint = '/.well-known/did.json'
app.get(didDocEndpoint, async (req, res) => {
serverIdentity = await agent.identityManagerGetOrCreateIdentity({
provider: 'did:web',
alias: hostname,
})

app.listen(options.port, () => {
console.log(`🚀 Server ready at http://${options.hostname}:${options.port}`)
console.log('Enabled agent methods', JSON.stringify(agent.availableMethods()))
console.log('Exposed methods', JSON.stringify(exposedMethods))
const didDoc = {
'@context': 'https://w3id.org/did/v1',
id: serverIdentity.did,
publicKey: serverIdentity.keys.map((key) => ({
id: serverIdentity.did + '#' + key.kid,
type: key.type === 'Secp256k1' ? 'Secp256k1VerificationKey2018' : 'Ed25519VerificationKey2018',
owner: serverIdentity.did,
publicKeyHex: key.publicKeyHex,
})),
authentication: serverIdentity.keys.map((key) => ({
type:
key.type === 'Secp256k1'
? 'Secp256k1SignatureAuthentication2018'
: 'Ed25519SignatureAuthentication2018',
publicKey: serverIdentity.did + '#' + key.kid,
})),
service: serverIdentity.services,
}

res.json(didDoc)
})
console.log('📋 DID Document ' + baseUrl + didDocEndpoint)
}
})
})
Loading

0 comments on commit 56c464d

Please sign in to comment.