Skip to content

Commit

Permalink
feat: daf-cli credential sending, receiving
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Nov 14, 2019
1 parent 4e35414 commit 7ca187e
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 5 deletions.
34 changes: 34 additions & 0 deletions packages/daf-cli/README.md
Expand Up @@ -35,7 +35,41 @@ Options:
-d, --delete <did> Delete identity
-h, --help output usage information
```
### W3C Credentials

```
Usage: daf credential [options]
Manage W3C Verifiable Credentials
Options:
-c, --create Create new credential
-s, --send Send
-q, --qrcode Show qrcode
-r, --receiver <did> Credential subject
-h, --help output usage information
```

### Services

Listen for new messages

```
daf listen
```

#### Using custom TGE


Send:
```
DAF_TG_URI=https://mouro.eu.ngrok.io/graphql daf credential -c -s
```

Receive:
```
DEBUG=* DAF_TG_URI=https://mouro.eu.ngrok.io/graphql DAF_TG_WSURI=wss://mouro.eu.ngrok.io/graphql daf listen
```

### DID Document resolver

Expand Down
29 changes: 27 additions & 2 deletions packages/daf-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/daf-cli/package.json
Expand Up @@ -12,6 +12,7 @@
"watch": "tsc --watch"
},
"dependencies": {
"@types/ws": "^6.0.3",
"commander": "^4.0.1",
"console-table-printer": "^1.1.15",
"daf-core": "^0.0.21",
Expand All @@ -28,7 +29,9 @@
"daf-trust-graph": "^0.0.21",
"daf-w3c": "^0.0.21",
"debug": "^4.1.1",
"inquirer": "^7.0.0"
"inquirer": "^7.0.0",
"qrcode-terminal": "^0.12.0",
"ws": "^7.2.0"
},
"devDependencies": {
"@types/debug": "^4.1.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/daf-cli/src/cli.ts
@@ -1,9 +1,10 @@
import program from 'commander'
import './identity-manager'
import './did-resolver'
import './credential'
import './services'

program.parse(process.argv)

if (!process.argv.slice(2).length) {
program.outputHelp()
}
96 changes: 96 additions & 0 deletions packages/daf-cli/src/credential.ts
@@ -0,0 +1,96 @@
import * as W3c from 'daf-w3c'
import * as DIDComm from 'daf-did-comm'
import { core, dataStore } from './setup'
import program from 'commander'
import inquirer from 'inquirer'
import qrcode from 'qrcode-terminal'

program
.command('credential')
.description('Manage W3C Verifiable Credentials')
.option('-c, --create', 'Create new credential')
.option('-s, --send', 'Send')
.option('-q, --qrcode', 'Show qrcode')
.option('-r, --receiver <did>', 'Credential subject')
.action(async (cmd) => {
if (cmd.create) {
const myDids = await core.identityManager.listDids()
if (myDids.length === 0) {
console.error('No dids')
process.exit()
}
const answers = await inquirer.prompt([
{
type: 'list',
name: 'iss',
choices: myDids,
message: 'Issuer DID'
},
{
type: 'input',
name: 'sub',
message: 'Subject DID',
default: cmd.receiver
},
{
type: 'input',
name: 'claimType',
message: 'Claim Type',
default: 'name'
},
{
type: 'input',
name: 'claimValue',
message: 'Claim Value',
default: 'Alice'
},
])

const credentialSubject: any = {}
const type: string = answers.claimType
credentialSubject[ type ] = answers.claimValue

const signAction: W3c.ActionSignW3cVc = {
type: W3c.ActionTypes.signVc,
did: answers.iss,
data: {
sub: answers.sub,
vc: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
credentialSubject
},
}
}

const jwt = await core.handleAction(signAction)

if (!cmd.send) {
await dataStore.initialize()
await core.onRawMessage({raw: jwt})
} else {

const sendAction: DIDComm.ActionSendJWT = {
type: DIDComm.ActionTypes.sendJwt,
data: {
from: answers.iss,
to: answers.sub,
jwt
}
}
try {
const result = await core.handleAction(sendAction)
console.log('Sent:', result)
} catch (e) {
console.error(e)
}
}

if (cmd.qrcode) {
qrcode.generate(jwt)
}

}

})

20 changes: 20 additions & 0 deletions packages/daf-cli/src/services.ts
@@ -0,0 +1,20 @@
import { EventTypes, Types } from 'daf-core'
import { core, dataStore } from './setup'
import program from 'commander'

program
.command('listen')
.description('Receive new messages and listen for new ones')
.action(async (did) => {
await dataStore.initialize()

core.on(EventTypes.validatedMessage, (type, msg: Types.ValidatedMessage) => {
console.log('New message type:', msg.type)
})

await core.startServices()
await core.syncServices(
await dataStore.latestMessageTimestamps()
)
})

3 changes: 2 additions & 1 deletion packages/daf-cli/src/setup.ts
Expand Up @@ -14,7 +14,7 @@ import { SodiumFsEncryptionKeyManager } from 'daf-sodium-fs'

import { NodeSqlite3 } from 'daf-node-sqlite3'
import { DataStore } from 'daf-data-store'

import ws from 'ws'

import Debug from 'debug'
const debug = Debug('main')
Expand Down Expand Up @@ -76,6 +76,7 @@ const serviceControllersWithConfig = [
config: {
uri: process.env.DAF_TG_URI,
wsUri: process.env.DAF_TG_WSURI,
webSocketImpl: ws,
},
},
]
Expand Down
4 changes: 4 additions & 0 deletions packages/daf-cli/types/qrcode-terminal/index.d.ts
@@ -0,0 +1,4 @@
declare module 'qrcode-terminal'{
export const generate: (text: string, options?: any) => void

}

0 comments on commit 7ca187e

Please sign in to comment.