Skip to content

Commit

Permalink
feat: Agent dbConnection constructor option
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Apr 9, 2020
1 parent 9c2d0f6 commit e3dfc2f
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 102 deletions.
29 changes: 15 additions & 14 deletions docs/Docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,31 @@ DAF can be used by using Typescript API directly, or by using remote GraphQL api
## Typescript

```typescript
// Setting up the database connection
import { Entities } from 'daf-core'
import { createConnection } from 'typeorm'
// https://typeorm.io/#/connection-options
const dbConnection = createConnection({
type: 'sqlite',
database: 'database.sqlite',
synchronize: true,
entities: [...Entities],
})

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

// Storing key pairs in the database
import { KeyStore } from 'daf-core'
const keyStore = new KeyStore()
const keyStore = new KeyStore(dbConnection)

// KeyManagementSystem is responsible for managing encryption and signing keys
import { KeyManagementSystem } from 'daf-libsodium'
const kms = new KeyManagementSystem(keyStore)

// Storing managed identities in the database
import { IdentityStore } from 'daf-core'
const identityStore = new IdentityStore('unique-store-name')
const identityStore = new IdentityStore('unique-store-name', dbConnection)

// Infura is being used to access Ethereum blockchain. https://infura.io
const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c'
Expand Down Expand Up @@ -62,22 +73,12 @@ actionHandler.setNext(new SdrActionHandler()).setNext(new DIDCommActionHandler()
import { Agent } from 'daf-core'
// we need defaultIdentityProvider = 'rinkeby-ethr-did'
const agent = new Agent({
dbConnection,
didResolver,
identityProviders: [rinkebyIdentityProvider],
actionHandler,
messageHandler,
})

// Setting up the database connection
import { Entities } from 'daf-core'
import { createConnection } from 'typeorm'
// https://typeorm.io/#/connection-options
await createConnection({
type: 'sqlite',
database: 'database.sqlite',
synchronize: true,
entities: [...Entities],
})
```

## GraphQL Server
Expand Down Expand Up @@ -368,7 +369,7 @@ query claims($input: ClaimsInput) {

```typescript
import { findCredentialsForSdr } from 'daf-selective-disclosure'
const result = await findCredentialsForSdr(sdr, 'did:example:1234')
const result = await findCredentialsForSdr(agent.dbConnection, sdr, 'did:example:1234')

console.log(result)
```
Expand Down
8 changes: 1 addition & 7 deletions packages/daf-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ import './data-explorer'
import './graphql'
import './sdr'
import './msg'
import { initializeDb } from './setup'

const main = async () => {
await initializeDb()
program.parse(process.argv)
}

if (!process.argv.slice(2).length) {
program.outputHelp()
} else {
main().catch(e => console.log(e.message))
program.parse(process.argv)
}
22 changes: 11 additions & 11 deletions packages/daf-cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ if (process.env.DAF_TG_URI) TrustGraphServiceController.defaultUri = process.env
if (process.env.DAF_TG_WSURI) TrustGraphServiceController.defaultWsUri = process.env.DAF_TG_WSURI
TrustGraphServiceController.webSocketImpl = ws

const dbConnection = createConnection({
type: 'sqlite',
database: dataStoreFilename,
synchronize: true,
logging: process.env.DEBUG_DAF_DB ? true : 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 @@ -67,20 +75,12 @@ actionHandler
.setNext(new SdrActionHandler())

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

export const initializeDb = async () => {
await createConnection({
type: 'sqlite',
database: dataStoreFilename,
synchronize: true,
logging: process.env.DEBUG_DAF_DB ? true : false,
entities: [...Daf.Entities],
})
}
export const dataStore = new DataStore()
8 changes: 6 additions & 2 deletions packages/daf-core/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { IdentityManager } from './identity/identity-manager'
import { AbstractIdentityProvider } from './identity/abstract-identity-provider'
import { ServiceManager, LastMessageTimestampForInstance, ServiceEventTypes } from './service/service-manager'
import { ServiceControllerDerived } from './service/abstract-service-controller'
import { MessageHandler, unsupportedMessageTypeError } from './message/abstract-message-handler'
import { MessageHandler } from './message/abstract-message-handler'
import { ActionHandler } from './action/action-handler'
import { Action } from './types'
import { Message, MetaData } from './entities/message'
import { Connection } from 'typeorm'

import Debug from 'debug'
const debug = Debug('daf:agent')
Expand All @@ -23,6 +24,7 @@ export interface Resolver {
}

interface Config {
dbConnection?: Promise<Connection>
didResolver: Resolver
identityProviders: AbstractIdentityProvider[]
serviceControllers?: ServiceControllerDerived[]
Expand All @@ -31,6 +33,7 @@ interface Config {
}

export class Agent extends EventEmitter {
readonly dbConnection: Promise<Connection>
public identityManager: IdentityManager
public didResolver: Resolver
private serviceManager: ServiceManager
Expand All @@ -39,6 +42,7 @@ export class Agent extends EventEmitter {

constructor(config: Config) {
super()
this.dbConnection = config.dbConnection || null

this.identityManager = new IdentityManager({
identityProviders: config.identityProviders,
Expand Down Expand Up @@ -110,7 +114,7 @@ export class Agent extends EventEmitter {

debug('Validated message %o', message)
if (save) {
await message.save()
await (await this.dbConnection).getRepository(Message).save(message)
debug('Emitting event', EventTypes.savedMessage)
this.emit(EventTypes.savedMessage, message)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/daf-core/src/entities/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Message } from './message'
import { Presentation } from './presentation'
import { Credential } from './credential'
import { Claim } from './claim'
import { Connection } from 'typeorm'

@Entity()
export class Identity extends BaseEntity {
Expand Down Expand Up @@ -77,8 +78,8 @@ export class Identity extends BaseEntity {
*
* @param where
*/
async getLatestClaimValue(where: { type: string }): Promise<String> {
const claim = await Claim.findOne({
async getLatestClaimValue(dbConnection: Promise<Connection>, where: { type: string }): Promise<String> {
const claim = await (await dbConnection).getRepository(Claim).findOne({
where: {
...where,
subject: this.did,
Expand Down
Loading

0 comments on commit e3dfc2f

Please sign in to comment.