Skip to content

Commit

Permalink
feat: CLI config
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed May 13, 2020
1 parent b9a4ebc commit 5a3391e
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 55 deletions.
79 changes: 79 additions & 0 deletions packages/daf-cli/default/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var fs = require('fs')

module.exports = {
identityProviders: [
{
package: 'daf-ethr-did',
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + process.env.DAF_INFURA_ID,
gas: 10001,
ttl: 60 * 60 * 24 * 30 * 12 + 1,
},
{
package: 'daf-ethr-did',
network: 'ropsten',
rpcUrl: 'https://ropsten.infura.io/v3/' + process.env.DAF_INFURA_ID,
},
{
package: 'daf-ethr-did',
network: 'mainnet',
rpcUrl: 'https://mainnet.infura.io/v3/' + process.env.DAF_INFURA_ID,
},
{
package: 'daf-ethr-did',
network: 'private',
rpcUrl: 'http://localhost:8545/',
registry: '0x05cc574b19a3c11308f761b3d7263bd8608bc532'
},
{
package: 'daf-elem-did',
network: 'ropsten',
apiUrl: 'https://element-did.com/api/v1/sidetree',
},
],
ethrDidNetworks: [
{
name: 'mainnet',
rpcUrl: 'https://mainnet.infura.io/v3/' + process.env.DAF_INFURA_ID
},
{
name: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + process.env.DAF_INFURA_ID
},
{
name: 'ropsten',
rpcUrl: 'https://ropsten.infura.io/v3/' + process.env.DAF_INFURA_ID
},
{
name: 'kovan',
rpcUrl: 'https://kovan.infura.io/v3/' + process.env.DAF_INFURA_ID
},
{
name: 'goerli',
rpcUrl: 'https://goerli.infura.io/v3/' + process.env.DAF_INFURA_ID
},
{
name: 'private',
rpcUrl: 'http://localhost:8545/',
registry: '0x05cc574b19a3c11308f761b3d7263bd8608bc532'
}
],
// https://typeorm.io/#/connection-options
database: {
type: 'sqlite',
synchronize: !fs.existsSync(process.env.DAF_DATA_STORE),
database: process.env.DAF_DATA_STORE,
logging: process.env.DAF_DEBUG_DB === 'true' ? true : false,
migrationsRun: true,
},
graphql: {
apiKey: process.env.DAF_GRAPHQL_API_KEY,
resolvers: {
IdentityManager: true,
TrustGraph: false,
DIDComm: true,
W3c: true,
Sdr: true,
}
}
}
2 changes: 2 additions & 0 deletions packages/daf-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"debug": "^4.1.1",
"dotenv": "^8.2.0",
"graphql": "^14.5.8",
"graphql-tools": "^5.0.0",
"inquirer": "^7.0.0",
"lodash.merge": "^4.6.2",
"qrcode-terminal": "^0.12.0",
Expand All @@ -50,6 +51,7 @@
"files": [
"bin/**/*",
"build/**/*",
"default/**/*",
"src/**/*",
"README.md",
"LICENSE"
Expand Down
44 changes: 44 additions & 0 deletions packages/daf-cli/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ConnectionOptions } from 'typeorm'
const fs = require('fs')

export interface Configuration {
identityProviders: {
package: 'daf-ethr-did' | 'daf-elem-did'
network: string
rpcUrl?: string
apiUrl?: string
gas?: number
ttl?: number
registry?: string
}[],
ethrDidNetworks: {
name: string
rpcUrl: string
registry?: string
}[],
database: ConnectionOptions,
graphql: {
apiKey?: string
resolvers: {
IdentityManager: boolean
TrustGraph: boolean
DIDComm: boolean
W3c: boolean
Sdr: boolean
}
}
}

const defaultPath = process.env.HOME + '/.daf/'
const configFile = process.env.DAF_CONFIG || defaultPath + 'config.js'


export const getConfiguration = (): Configuration => {
if (!fs.existsSync(configFile)) {
console.log('Config file does not exist. Creating: ' + configFile)
const contents = fs.readFileSync(__dirname + '/../default/config.js')
fs.writeFileSync(configFile, contents)
}
const configuration: Configuration = require(configFile)
return configuration
}
88 changes: 70 additions & 18 deletions packages/daf-cli/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,84 @@ import { SdrGql } from 'daf-selective-disclosure'
import merge from 'lodash.merge'
import { agent } from './setup'
import { listen } from './services'
import { makeExecutableSchema, mergeSchemas } from 'graphql-tools'
import { getConfiguration } from './config'

program
.command('graphql')
.description('GraphQL server')
.option('-p, --port <port>', 'Port')
.option('-l, --listen', 'Listen for new messages')
.option('-i, --interval <seconds>', 'Poll for new messages with interval of <seconds>')
.action(async cmd => {
await agent
const { graphql } = getConfiguration()
const schemas = [
makeExecutableSchema({
typeDefs: Gql.baseTypeDefs + Gql.Core.typeDefs,
resolvers: Gql.Core.resolvers
})
]

if (graphql.resolvers.IdentityManager) {
schemas.push(
makeExecutableSchema({
typeDefs: Gql.baseTypeDefs + Gql.IdentityManager.typeDefs,
resolvers: Gql.IdentityManager.resolvers
})
)
}

if (graphql.resolvers.TrustGraph) {
schemas.push(
makeExecutableSchema({
typeDefs: Gql.baseTypeDefs + TrustGraphGql.typeDefs,
resolvers: TrustGraphGql.resolvers
})
)
}

if (graphql.resolvers.DIDComm) {
schemas.push(
makeExecutableSchema({
typeDefs: Gql.baseTypeDefs + DIDCommGql.typeDefs,
resolvers: DIDCommGql.resolvers
})
)
}

if (graphql.resolvers.W3c) {
schemas.push(
makeExecutableSchema({
typeDefs: Gql.baseTypeDefs + W3cGql.typeDefs,
resolvers: W3cGql.resolvers
})
)
}

if (graphql.resolvers.Sdr) {
schemas.push(
makeExecutableSchema({
typeDefs: Gql.baseTypeDefs + SdrGql.typeDefs,
resolvers: SdrGql.resolvers
})
)
}

const schema = mergeSchemas({ schemas })

const server = new ApolloServer({
typeDefs: [
Gql.baseTypeDefs,
Gql.Core.typeDefs,
Gql.IdentityManager.typeDefs,
TrustGraphGql.typeDefs,
DIDCommGql.typeDefs,
W3cGql.typeDefs,
SdrGql.typeDefs,
],
resolvers: merge(
Gql.Core.resolvers,
Gql.IdentityManager.resolvers,
TrustGraphGql.resolvers,
DIDCommGql.resolvers,
W3cGql.resolvers,
SdrGql.resolvers,
),
context: async () => ({ agent: (await agent) }),
schema,
context: async ({ req }) => {
if (graphql.apiKey) {
const token = req.headers.authorization || ''
if (token !== 'Bearer ' + graphql.apiKey) {
throw Error('Auth error')
}
}

return { agent: (await agent) }
},
introspection: true,
})
// await core.setupServices()
Expand Down
65 changes: 34 additions & 31 deletions packages/daf-cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ import { SdrActionHandler, SdrMessageHandler } from 'daf-selective-disclosure'
import { TrustGraphActionHandler, TrustGraphServiceController } from 'daf-trust-graph'
import { DIDCommActionHandler, DIDCommMessageHandler } from 'daf-did-comm'
import { UrlMessageHandler } from 'daf-url'
import { createConnection } from 'typeorm'
import { createConnection, ConnectionOptions } from 'typeorm'
import { migrations } from './migrations'
const fs = require('fs')
import ws from 'ws'
import { config } from 'dotenv'
import { getConfiguration } from './config'

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

const writeDefaultConfig = async () => {
const writeDefaultEnv = async () => {
if (!fs.existsSync(defaultPath)) {
fs.mkdirSync(defaultPath)
}

if (!fs.existsSync(envFile)) {
console.log('Configuration file does not exist. Creating: ' + envFile)
console.log('Environment file does not exist. Creating: ' + envFile)
let env = 'DAF_DATA_STORE=' + defaultPath + 'database-v2.sqlite'
env += '\nDAF_DEBUG_DB=false'
env += '\nDAF_SECRET_KEY=' + (await SecretBox.createSecretKey())
Expand All @@ -39,14 +40,13 @@ const writeDefaultConfig = async () => {
}

const setupAgent = async (): Promise<Daf.Agent> => {
await writeDefaultConfig()
await writeDefaultEnv()
config({ path: envFile })

const infuraProjectId = process.env.DAF_INFURA_ID
const configuration = getConfiguration()

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

if (process.env.DAF_UNIVERSAL_RESOLVER_URL) {
Expand All @@ -59,35 +59,38 @@ const setupAgent = async (): Promise<Daf.Agent> => {
if (process.env.DAF_TG_WSURI) TrustGraphServiceController.defaultWsUri = process.env.DAF_TG_WSURI
TrustGraphServiceController.webSocketImpl = ws

const synchronize = !fs.existsSync(process.env.DAF_DATA_STORE)

const dbConnection = createConnection({
type: 'sqlite',
migrationsRun: true,
synchronize,
database: process.env.DAF_DATA_STORE,
logging: process.env.DAF_DEBUG_DB === 'true' ? true : false,
...configuration.database,
entities: [...Daf.Entities],
migrations: [...Daf.migrations, ...migrations],
})

const identityProviders = [
new EthrDid.IdentityProvider({
identityStore: new Daf.IdentityStore('rinkeby-ethr', dbConnection),
kms: new KeyManagementSystem(new Daf.KeyStore(dbConnection, new SecretBox(process.env.DAF_SECRET_KEY))),
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
gas: 10001,
ttl: 60 * 60 * 24 * 30 * 12 + 1,
}),

new ElemDid.IdentityProvider({
identityStore: new Daf.IdentityStore('elem-did', dbConnection),
kms: new KeyManagementSystem(new Daf.KeyStore(dbConnection, new SecretBox(process.env.DAF_SECRET_KEY))),
apiUrl: 'https://element-did.com/api/v1/sidetree',
network: 'ropsten'
}),
]
const identityProviders: Daf.AbstractIdentityProvider[] = []

for (const identityProviderConfig of configuration.identityProviders) {
switch(identityProviderConfig.package) {
case 'daf-ethr-did':
identityProviders.push(new EthrDid.IdentityProvider({
identityStore: new Daf.IdentityStore(identityProviderConfig.package + identityProviderConfig.network, dbConnection),
kms: new KeyManagementSystem(new Daf.KeyStore(dbConnection, new SecretBox(process.env.DAF_SECRET_KEY))),
network: identityProviderConfig.network,
rpcUrl: identityProviderConfig.rpcUrl,
gas: identityProviderConfig.gas,
ttl: identityProviderConfig.ttl,
registry: identityProviderConfig.registry
}))
break
case 'daf-elem-did':
identityProviders.push(new ElemDid.IdentityProvider({
identityStore: new Daf.IdentityStore(identityProviderConfig.package + identityProviderConfig.network, dbConnection),
kms: new KeyManagementSystem(new Daf.KeyStore(dbConnection, new SecretBox(process.env.DAF_SECRET_KEY))),
apiUrl: identityProviderConfig.apiUrl,
network: identityProviderConfig.network
}))
break
}
}

const serviceControllers = [TrustGraphServiceController]

const messageHandler = new UrlMessageHandler()
Expand Down
7 changes: 7 additions & 0 deletions packages/daf-core/src/graphql/graphql-base-type-defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ export const baseTypeDefs = `
provider: String
}
type Message
type Presentation
type Credential
type Claim
scalar Object
scalar Date
`
Loading

0 comments on commit 5a3391e

Please sign in to comment.