Skip to content

Commit

Permalink
feat: Renamed Core to Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Mar 31, 2020
1 parent 094cb23 commit f2c79b6
Show file tree
Hide file tree
Showing 47 changed files with 224 additions and 226 deletions.
2 changes: 1 addition & 1 deletion examples/expressjs-ethr/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ actionHandler
.setNext(new W3cActionHandler())
.setNext(new SdrActionHandler())

export const core = new Daf.Core({
export const agent = new Daf.Agent({
identityProviders,
serviceControllers,
didResolver,
Expand Down
10 changes: 5 additions & 5 deletions examples/expressjs-ethr/src/identity.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import * as Daf from 'daf-core'
import { core } from './framework'
import { agent } from './framework'

export const getIdentity = async () => {
// Get of create new identity
let identity: Daf.AbstractIdentity
const identities = await core.identityManager.getIdentities()
const identities = await agent.identityManager.getIdentities()
if (identities.length > 0) {
identity = identities[0]
} else {
const identityProviders = await core.identityManager.getIdentityProviders()
identity = await core.identityManager.createIdentity(identityProviders[0].type)
const identityProviders = await agent.identityManager.getIdentityProviders()
identity = await agent.identityManager.createIdentity(identityProviders[0].type)
}

return identity
}

export const setServiceEndpoint = async (identity: Daf.AbstractIdentity, serviceEndpoint: string) => {
// Check if DID Document contains current serviceEndpoint
const didDoc = await core.didResolver.resolve(identity.did)
const didDoc = await agent.didResolver.resolve(identity.did)
const exists = didDoc?.service?.find(item => item.serviceEndpoint === serviceEndpoint)
if (!exists) {
try {
Expand Down
16 changes: 8 additions & 8 deletions examples/expressjs-ethr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Daf from 'daf-core'
import * as SD from 'daf-selective-disclosure'
import * as W3C from 'daf-w3c'
import { app, server, io, sessionStore } from './server'
import { core, dataStore, initializeDb } from './framework'
import { agent, dataStore, initializeDb } from './framework'
import { getIdentity, setServiceEndpoint } from './identity'

if (!process.env.HOST) throw Error('Environment variable HOST not set')
Expand All @@ -24,14 +24,14 @@ async function main() {
app.post(messagingEndpoint, express.text({ type: '*/*' }), async (req, res) => {
try {
// This will trigger Daf.EventTypes.validatedMessage
const result = await core.handleMessage({ raw: req.body })
const result = await agent.handleMessage({ raw: req.body })
res.json({ id: result.id })
} catch (e) {
res.send(e.message)
}
})

core.on(Daf.EventTypes.savedMessage, async (message: Daf.Message) => {
agent.on(Daf.EventTypes.savedMessage, async (message: Daf.Message) => {
if (message.type === W3C.MessageTypes.vp && message.threadId) {
// TODO check for required vcs

Expand Down Expand Up @@ -93,7 +93,7 @@ async function main() {
req.session.views = req.session.views ? req.session.views + 1 : 1

// Sign Selective Disclosure Request
const jwt = await core.handleAction({
const jwt = await agent.handleAction({
type: SD.ActionTypes.signSdr,
did: identity.did,
data: {
Expand Down Expand Up @@ -123,7 +123,7 @@ async function main() {
const name = await dataStore.shortId(did)

// Sign verifiable credential
const nameJwt = await core.handleAction({
const nameJwt = await agent.handleAction({
type: W3C.ActionTypes.signVc,
did: identity.did,
data: {
Expand All @@ -138,7 +138,7 @@ async function main() {
},
} as W3C.ActionSignW3cVc)

const kwcJwt = await core.handleAction({
const kwcJwt = await agent.handleAction({
type: W3C.ActionTypes.signVc,
did: identity.did,
data: {
Expand All @@ -153,7 +153,7 @@ async function main() {
},
} as W3C.ActionSignW3cVc)

const vpJwt = await core.handleAction({
const vpJwt = await agent.handleAction({
type: W3C.ActionTypes.signVp,
did: identity.did,
data: {
Expand Down Expand Up @@ -186,7 +186,7 @@ async function main() {

app.get('/public-profile', async (req, res) => {
// Sign verifiable presentation
const jwt = await core.handleAction({
const jwt = await agent.handleAction({
type: W3C.ActionTypes.signVc,
did: identity.did,
data: {
Expand Down
6 changes: 3 additions & 3 deletions examples/id-hub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ messageHandler
.setNext(new W3cMessageHandler())
.setNext(new SdrMessageHandler())

export const core = new Daf.Core({
export const agent = new Daf.Agent({
identityProviders: [],
serviceControllers: [],
didResolver,
Expand All @@ -32,12 +32,12 @@ const server = new ApolloServer({
throw Error('Auth error')
}

return { core }
return { agent }
},
introspection: true,
})

core.on(Daf.EventTypes.savedMessage, async (message: Daf.Message) => {
agent.on(Daf.EventTypes.savedMessage, async (message: Daf.Message) => {
// Add your business logic here
console.log(message)
})
Expand Down
12 changes: 6 additions & 6 deletions examples/react-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react'
import * as W3c from 'daf-w3c'
import * as TG from 'daf-trust-graph'
import { core } from './setup'
import { agent } from './setup'
const {
BaseStyles,
Box,
Expand Down Expand Up @@ -33,13 +33,13 @@ const App: React.FC = () => {
const [identities, setIdentities] = useState([{ identityProviderType: '', did: '' }])

useEffect(() => {
core.identityManager.getIdentityProviders().then((providers: any) => {
agent.identityManager.getIdentityProviders().then((providers: any) => {
setIdentityProviders(providers)
})
}, [])

const updateIdentityList = () => {
core.identityManager.getIdentities().then((identities: any) => {
agent.identityManager.getIdentities().then((identities: any) => {
setIdentities(identities)
if (identities.length > 0) setActiveDid(identities[0].did)
})
Expand All @@ -57,7 +57,7 @@ const App: React.FC = () => {
const credentialSubject: any = {}
credentialSubject[claimType] = claimValue

const credential = await core.handleAction({
const credential = await agent.handleAction({
type: W3c.ActionTypes.signVc,
did: activeDid,
data: {
Expand All @@ -73,7 +73,7 @@ const App: React.FC = () => {
console.log(credential)

// Send credential using TrustGraph
await core.handleAction({
await agent.handleAction({
type: TG.ActionTypes.sendJwt,
data: {
from: activeDid,
Expand Down Expand Up @@ -110,7 +110,7 @@ const App: React.FC = () => {
mr={3}
key={identityProvider.type}
onClick={async () => {
await core.identityManager.createIdentity(identityProvider.type)
await agent.identityManager.createIdentity(identityProvider.type)
updateIdentityList()
}}
>
Expand Down
2 changes: 1 addition & 1 deletion examples/react-app/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const identityProviders: Daf.AbstractIdentityProvider[] = [
// identityProviders.push(new MM.IdentityProvider())
// }

export const core = new Daf.Core({
export const agent = new Daf.Agent({
identityProviders,
serviceControllers: [],
didResolver,
Expand Down
56 changes: 27 additions & 29 deletions examples/react-graphql/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as Daf from 'daf-core'
import * as DS from 'daf-data-store'
import * as DidJwt from 'daf-did-jwt'
import * as W3c from 'daf-w3c'
import * as SD from 'daf-selective-disclosure'
import * as TG from 'daf-trust-graph'
import * as URL from 'daf-url'
import { JwtMessageHandler } from 'daf-did-jwt'
import { W3cMessageHandler, W3cActionHandler, W3cGql } from 'daf-w3c'
import { SdrMessageHandler, SdrActionHandler, SdrGql} from 'daf-selective-disclosure'
import { TrustGraphServiceController, TrustGraphGql, TrustGraphActionHandler} from 'daf-trust-graph'
import { UrlMessageHandler } from 'daf-url'
import * as DafEthrDid from 'daf-ethr-did'
import * as DafLibSodium from 'daf-libsodium'
import { DafResolver } from 'daf-resolver'
Expand All @@ -13,7 +13,7 @@ import merge from 'lodash.merge'
import ws from 'ws'
import { createConnection } from 'typeorm'

TG.ServiceController.webSocketImpl = ws
TrustGraphServiceController.webSocketImpl = ws
const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c'

let didResolver = new DafResolver({ infuraProjectId })
Expand All @@ -26,24 +26,24 @@ const identityProviders = [
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
}),
]
const serviceControllers = [TG.ServiceController]
const serviceControllers = [TrustGraphServiceController]

const messageValidator = new URL.MessageValidator()
messageValidator
.setNext(new DidJwt.MessageValidator())
.setNext(new W3c.MessageValidator())
.setNext(new SD.MessageValidator())
const messageHandler = new UrlMessageHandler()
messageHandler
.setNext(new JwtMessageHandler())
.setNext(new W3cMessageHandler())
.setNext(new SdrMessageHandler())

const actionHandler = new TG.ActionHandler()
const actionHandler = new TrustGraphActionHandler()
actionHandler
.setNext(new W3c.ActionHandler())
.setNext(new SD.ActionHandler())
.setNext(new W3cActionHandler())
.setNext(new SdrActionHandler())

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

Expand All @@ -52,20 +52,18 @@ const dataStore = new DS.DataStore()
const server = new ApolloServer({
typeDefs: [
Daf.Gql.baseTypeDefs,
DS.Gql.typeDefs,
Daf.Gql.Core.typeDefs,
Daf.Gql.IdentityManager.typeDefs,
TG.Gql.typeDefs,
W3c.Gql.typeDefs,
SD.Gql.typeDefs,
TrustGraphGql.typeDefs,
W3cGql.typeDefs,
SdrGql.typeDefs,
],
resolvers: merge(
Daf.Gql.Core.resolvers,
Daf.Gql.IdentityManager.resolvers,
DS.Gql.resolvers,
TG.Gql.resolvers,
W3c.Gql.resolvers,
SD.Gql.resolvers,
TrustGraphGql.resolvers,
W3cGql.resolvers,
SdrGql.resolvers,
),
context: ({ req }) => {
// Authorization is out of scope for this example,
Expand All @@ -75,12 +73,12 @@ const server = new ApolloServer({
throw Error('Auth error')
}

return { core, dataStore }
return { agent, dataStore }
},
introspection: true,
})

core.on(Daf.EventTypes.validatedMessage, async (message: Daf.Message) => {
agent.on(Daf.EventTypes.validatedMessage, async (message: Daf.Message) => {
await message.save()
})

Expand All @@ -95,8 +93,8 @@ const main = async () => {
],
})

await core.setupServices()
await core.listen()
await agent.setupServices()
await agent.listen()
const info = await server.listen()
console.log(`🚀 Server ready at ${info.url}`)
}
Expand Down
12 changes: 6 additions & 6 deletions examples/rest-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const port = process.env.PORT || 8080
import { Entities } from 'daf-core'
import { createConnection } from 'typeorm'

import { core } from './setup'
import { agent } from './setup'

app.get('/identities', async (req, res) => {
const identities = await core.identityManager.getIdentities()
const identities = await agent.identityManager.getIdentities()
res.json(identities.map(identity => identity.did))
})

app.get('/providers', async (req, res) => {
const providers = await core.identityManager.getIdentityProviders()
const providers = await agent.identityManager.getIdentityProviders()
res.json(
providers.map(provider => {
provider.type, provider.description
Expand All @@ -22,7 +22,7 @@ app.get('/providers', async (req, res) => {

app.post('/create-identity', express.json(), async (req, res) => {
try {
const identity = await core.identityManager.createIdentity(req.body.type)
const identity = await agent.identityManager.createIdentity(req.body.type)
res.json({ did: identity.did })
} catch (e) {
res.status(404)
Expand All @@ -32,7 +32,7 @@ app.post('/create-identity', express.json(), async (req, res) => {

app.post('/handle-action', express.json(), async (req, res) => {
try {
const result = await core.handleAction(req.body)
const result = await agent.handleAction(req.body)
res.json({ result })
} catch (e) {
res.status(404)
Expand All @@ -43,7 +43,7 @@ app.post('/handle-action', express.json(), async (req, res) => {
// This endpoint would be published in did doc as a serviceEndpoint
app.post('/handle-message', express.text({ type: '*/*' }), async (req, res) => {
try {
const message = await core.handleMessage({ raw: req.body })
const message = await agent.handleMessage({ raw: req.body })

// add your business logic here

Expand Down
2 changes: 1 addition & 1 deletion examples/rest-api/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c'

const didResolver = new DafResolver({ infuraProjectId })

export const core = new Daf.Core({
export const agent = new Daf.Agent({
identityProviders: [
new DafEthr.IdentityProvider({
kms: new DafLibSodium.KeyManagementSystem(new Daf.KeyStore()),
Expand Down
Loading

0 comments on commit f2c79b6

Please sign in to comment.