Skip to content

Commit

Permalink
feat: Breaking. New did management interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Feb 7, 2020
1 parent dafcac2 commit c384159
Show file tree
Hide file tree
Showing 21 changed files with 603 additions and 367 deletions.
13 changes: 10 additions & 3 deletions examples/expressjs-ethr/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DafUniversalResolver } from 'daf-resolver-universal'

import * as Daf from 'daf-core'
import * as DidJwt from 'daf-did-jwt'
import { EthrDidFsController } from 'daf-ethr-did-fs'
import { IdentityProvider } from 'daf-ethr-did-fs'

import * as W3c from 'daf-w3c'
import * as SD from 'daf-selective-disclosure'
Expand Down Expand Up @@ -43,7 +43,14 @@ if (process.env.DAF_UNIVERSAL_RESOLVER_URL) {
})
}

const identityControllers = [new EthrDidFsController(identityStoreFilename)]
const identityProviders = [
new IdentityProvider({
fileName: identityStoreFilename,
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
resolver: didResolver,
}),
]
const serviceControllers = [TG.ServiceController]

const messageValidator = new DBG.MessageValidator()
Expand All @@ -62,7 +69,7 @@ actionHandler
.setNext(new SD.ActionHandler())

export const core = new Daf.Core({
identityControllers,
identityProviders,
serviceControllers,
didResolver,
messageValidator,
Expand Down
17 changes: 8 additions & 9 deletions examples/expressjs-ethr/src/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ io.on('connection', function(socket) {
async function main() {
await dataStore.initialize()

// Get of create new issuer
let issuer: Daf.Issuer
const issuers = await core.identityManager.listIssuers()
if (issuers.length > 0) {
issuer = issuers[0]
// Get of create new identity
let identity: Daf.AbstractIdentity
const identities = await core.identityManager.getIdentities()
if (identities.length > 0) {
identity = identities[0]
} else {
const types = await core.identityManager.listTypes()
const did = await core.identityManager.create(types[0])
issuer = await core.identityManager.issuer(did)
const identityProviders = await core.identityManager.getIdentityProviderTypes()
identity = await core.identityManager.createIdentity(identityProviders[0].type)
}

app.get('/', async function(req, res) {
Expand All @@ -83,7 +82,7 @@ async function main() {
if (!did) {
const signAction: SD.ActionSignSdr = {
type: SD.ActionTypes.signSdr,
did: issuer.did,
did: identity.did,
data: {
tag: req.sessionID,
claims: [
Expand Down
20 changes: 11 additions & 9 deletions examples/react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ const App: React.FC = () => {
const [receiver, setReceiver] = useState('did:web:uport.me')
const [claimType, setClaimType] = useState('name')
const [claimValue, setClaimValue] = useState('Alice')
const [controllerTypes, setControllerTypes] = useState([''])
const [identities, setIdentities] = useState([{ type: '', did: '' }])
const [identityProviders, setIdentityProviders] = useState([{ type: '', description: '' }])
const [identities, setIdentities] = useState([{ identityProviderType: '', did: '' }])

useEffect(() => {
setControllerTypes(core.identityManager.listTypes())
core.identityManager.getIdentityProviderTypes().then((providers: any) => {
setIdentityProviders(providers)
})
}, [])

const updateIdentityList = () => {
core.identityManager.listIssuers().then(identities => {
core.identityManager.getIdentities().then((identities: any) => {
setIdentities(identities)
if (identities.length > 0) setActiveDid(identities[0].did)
})
Expand Down Expand Up @@ -101,18 +103,18 @@ const App: React.FC = () => {
<Card width={'auto'} mx={'auto'}>
<Heading>Send Verifiable Credential</Heading>

{controllerTypes.map(type => (
{identityProviders.map(identityProvider => (
<Button.Outline
mt={3}
mb={3}
mr={3}
key={type}
key={identityProvider.type}
onClick={async () => {
await core.identityManager.create(type)
await core.identityManager.createIdentity(identityProvider.type)
updateIdentityList()
}}
>
Create {type} DID
Create {identityProvider.type} DID
</Button.Outline>
))}

Expand All @@ -125,7 +127,7 @@ const App: React.FC = () => {
name="selecedDid"
required
onChange={(e: any) => setActiveDid(identity.did)}
label={`${identity.did} (${identity.type})`}
label={`${identity.did} (${identity.identityProviderType})`}
value={identity.did}
checked={activeDid === identity.did}
/>
Expand Down
8 changes: 5 additions & 3 deletions examples/react-app/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ messageValidator.setNext(new DidJwt.MessageValidator()).setNext(new W3c.MessageV
const actionHandler = new DBG.ActionHandler()
actionHandler.setNext(new TG.ActionHandler()).setNext(new W3c.ActionHandler())

const identityControllers: Daf.IdentityController[] = [new EthrDidLocalStorageController()]
const identityProviders: Daf.AbstractIdentityProvider[] = [
// new EthrDidLocalStorageController()
]

if (typeof window.ethereum !== 'undefined' && window.ethereum.isMetaMask) {
EthrDidMetamaskController.snapId = 'http://localhost:8082/package.json'
identityControllers.push(new EthrDidMetamaskController())
// identityProviders.push(new EthrDidMetamaskController())
}

export const core = new Daf.Core({
identityControllers,
identityProviders,
serviceControllers: [],
didResolver: new DafUniversalResolver({ url: 'https://uniresolver.io/1.0/identifiers/' }),
messageValidator,
Expand Down
16 changes: 12 additions & 4 deletions examples/react-graphql/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@ import * as TG from 'daf-trust-graph'
import * as DBG from 'daf-debug'
import * as URL from 'daf-url'
import { NodeSqlite3 } from 'daf-node-sqlite3'
import { EthrDidFsController } from 'daf-ethr-did-fs'
import { IdentityProvider } from 'daf-ethr-did-fs'
import { DafResolver } from 'daf-resolver'
import { ApolloServer } from 'apollo-server'
import merge from 'lodash.merge'
import ws from 'ws'

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

let didResolver = new DafResolver({ infuraProjectId: '5ffc47f65c4042ce847ef66a3fa70d4c' })
let didResolver = new DafResolver({ infuraProjectId })

const identityControllers = [new EthrDidFsController('./identity-store.json')]
const identityProviders = [
new IdentityProvider({
fileName: './identity-store.json',
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
resolver: didResolver,
}),
]
const serviceControllers = [TG.ServiceController]

const messageValidator = new DBG.MessageValidator()
Expand All @@ -34,7 +42,7 @@ actionHandler
.setNext(new SD.ActionHandler())

export const core = new Daf.Core({
identityControllers,
identityProviders,
serviceControllers,
didResolver,
messageValidator,
Expand Down
32 changes: 21 additions & 11 deletions examples/send-vc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,40 @@ messageValidator.setNext(new DidJwt.MessageValidator()).setNext(new W3c.MessageV
const actionHandler = new DBG.ActionHandler()
actionHandler.setNext(new TG.ActionHandler()).setNext(new W3c.ActionHandler())

const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c'

const didResolver = new DafUniversalResolver({ url: 'https://uniresolver.io/1.0/identifiers/' })

export const core = new Daf.Core({
identityControllers: [new Ethr.EthrDidFsController('./identity-store.json')],
identityProviders: [
new Ethr.IdentityProvider({
fileName: './identity-store.json',
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
resolver: didResolver,
}),
],
serviceControllers: [],
didResolver: new DafUniversalResolver({ url: 'https://uniresolver.io/1.0/identifiers/' }),
didResolver,
messageValidator,
actionHandler,
})

async function main() {
// Get or create new issuer
let issuer: Daf.Issuer
const issuers = await core.identityManager.listIssuers()
if (issuers.length > 0) {
issuer = issuers[0]
let identity: Daf.AbstractIdentity
const identities = await core.identityManager.getIdentities()
if (identities.length > 0) {
identity = identities[0]
} else {
const types = core.identityManager.listTypes()
const did = await core.identityManager.create(types[0])
issuer = await core.identityManager.issuer(did)
const identityProviders = core.identityManager.getIdentityProviderTypes()
identity = await core.identityManager.createIdentity(identityProviders[0].type)
}

// Sign credential
const credential = await core.handleAction({
type: W3c.ActionTypes.signVc,
did: issuer.did,
did: identity.did,
data: {
sub: 'did:web:uport.me',
vc: {
Expand All @@ -55,7 +65,7 @@ async function main() {
await core.handleAction({
type: TG.ActionTypes.sendJwt,
data: {
from: issuer.did,
from: identity.did,
to: 'did:web:uport.me',
jwt: credential,
},
Expand Down
65 changes: 65 additions & 0 deletions packages/daf-cli/src/identity-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ program
.option('-t, --types', 'List available identity controller types')
.option('-c, --create', 'Create identity')
.option('-d, --delete', 'Delete identity')
.option('-s, --service', 'Add service endpoint')
.option('-p, --publicKey', 'Add public key')
.action(async cmd => {
if (cmd.types) {
const list = await core.identityManager.getIdentityProviderTypes()
Expand Down Expand Up @@ -71,4 +73,67 @@ program
console.error(e)
}
}

if (cmd.service) {
try {
const identities = await core.identityManager.getIdentities()
const answers = await inquirer.prompt([
{
type: 'list',
name: 'did',
choices: identities.map(item => item.did),
message: 'Select DID',
},
{
type: 'text',
name: 'type',
message: 'Service type',
default: 'Messaging',
},
{
type: 'text',
name: 'endpoint',
message: 'Endpoint',
},
])

const identity = await core.identityManager.getIdentity(answers.did)
const provider = await core.identityManager.getIdentityProvider(identity.identityProviderType)
const result = await provider.addService(identity.did, {
type: answers.type,
serviceEndpoint: answers.endpoint,
id: '',
})
console.log('Success:', result)
} catch (e) {
console.error(e)
}
}

if (cmd.publicKey) {
try {
const identities = await core.identityManager.getIdentities()
const answers = await inquirer.prompt([
{
type: 'list',
name: 'did',
choices: identities.map(item => item.did),
message: 'Select DID',
},
{
type: 'list',
name: 'type',
choices: ['Ed25519', 'Secp256k1'],
message: 'Type',
},
])

const identity = await core.identityManager.getIdentity(answers.did)
const provider = await core.identityManager.getIdentityProvider(identity.identityProviderType)
const result = await provider.addPublicKey(identity.did, answers.type)
console.log('Success:', result)
} catch (e) {
console.error(e)
}
}
})
9 changes: 7 additions & 2 deletions packages/daf-cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const debug = Debug('daf:cli')

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

const identityStoreFilename = process.env.DAF_IDENTITY_STORE ?? defaultPath + '/identity-store-jwk.json'
const dataStoreFilename = process.env.DAF_DATA_STORE ?? defaultPath + '/data-store-cli.sqlite3'
const infuraProjectId = process.env.DAF_INFURA_ID ?? '5ffc47f65c4042ce847ef66a3fa70d4c'

Expand Down Expand Up @@ -49,11 +48,17 @@ TG.ServiceController.webSocketImpl = ws

const identityProviders = [
new EthrDidFs.IdentityProvider({
fileName: identityStoreFilename,
fileName: defaultPath + 'rinkeby-identity-store.json',
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
resolver: didResolver,
}),
new EthrDidFs.IdentityProvider({
fileName: defaultPath + 'kovan-identity-store.json',
network: 'kovan',
rpcUrl: 'https://kovan.infura.io/v3/' + infuraProjectId,
resolver: didResolver,
}),
]
const serviceControllers = [TG.ServiceController]

Expand Down
2 changes: 1 addition & 1 deletion packages/daf-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { AbstractActionHandler } from './action/action-handler'
export { EncryptionKeyManager, KeyPair } from './encryption-manager'
export { IdentityManager } from './identity/identity-manager'
export { AbstractIdentity } from './identity/abstract-identity'
export { AbstractIdentityProvider } from './identity/abstract-identity-provider'
export { AbstractIdentityProvider, IdentityProviderDerived } from './identity/abstract-identity-provider'
export { AbstractMessageValidator } from './message/abstract-message-validator'
export { Message } from './message/message'
export { ServiceManager, LastMessageTimestampForInstance, ServiceEventTypes } from './service/service-manager'
Expand Down
Loading

0 comments on commit c384159

Please sign in to comment.