Skip to content

Commit

Permalink
feat: Create and send VP from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Nov 27, 2019
1 parent 7cedec1 commit c7e1360
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 46 deletions.
4 changes: 2 additions & 2 deletions packages/daf-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ npm -g i daf-cli
- [x] Verifiable Credentials
- [x] Create Verifiable Credential
- [x] Send Verifiable Credential (DIDComm / TrustGraph)
- [ ] Create Verifiable Presentation
- [ ] Send Verifiable Presentation (DIDComm / TrustGraph)
- [x] Create Verifiable Presentation
- [x] Send Verifiable Presentation (DIDComm / TrustGraph)
- [x] Data Store Explorer
- [x] List known identities
- [x] List messages
Expand Down
208 changes: 170 additions & 38 deletions packages/daf-cli/src/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,190 @@ import qrcode from 'qrcode-terminal'

program
.command('credential')
.description('Manage W3C Verifiable Credentials')
.option('-c, --create', 'Create new credential')
.description('Create W3C Verifiable 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,
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',
},
{
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,
},
{
type: 'input',
name: 'claimType',
message: 'Claim Type',
default: 'name',
},
}

const jwt = await core.handleAction(signAction)

await dataStore.initialize()
if (!cmd.send) {
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)
}
})

program
.command('presentation')
.description('Create W3C Verifiable Presentation')
.option('-s, --send', 'Send')
.option('-q, --qrcode', 'Show qrcode')
.action(async cmd => {
const myDids = await core.identityManager.listDids()
if (myDids.length === 0) {
console.error('No dids')
process.exit()
}

const dids = await dataStore.allIdentities()

const identities = [
{
name: 'Enter manualy',
value: false,
},
]
for (const did of dids) {
const shortId = await dataStore.shortId(did.did)
identities.push({
value: did.did,
name: `${did.did} - ${shortId}`,
})
}

let aud = null
const answers = await inquirer.prompt([
{
type: 'list',
name: 'iss',
choices: myDids,
message: 'Issuer DID',
},
{
type: 'list',
name: 'aud',
message: 'Audience DID',
choices: identities,
},
])

if (!answers.aud) {
const audAnswer = await inquirer.prompt([
{
type: 'input',
name: 'claimValue',
message: 'Claim Value',
default: 'Alice',
name: 'aud',
message: 'Enter audience DID',
},
])
aud = audAnswer.aud
}

const credentials = await dataStore.findCredentials({ sub: answers.iss })
const list: any = []
if (credentials.length > 0) {
for (const credential of credentials) {
const fields = await dataStore.credentialsFieldsForClaimHash(credential.hash)
const issuer = await dataStore.shortId(credential.iss.did)
const claims = []
for (const field of fields) {
claims.push(field.type + ' = ' + field.value)
}
list.push({
name: claims.join(', ') + ' | Issuer: ' + issuer,
value: credential.jwt,
})
}

const credentialSubject: any = {}
const type: string = answers.claimType
credentialSubject[type] = answers.claimValue
let addMoreCredentials = true
const verifiableCredential = []

while (addMoreCredentials) {
const answers2 = await inquirer.prompt([
{
type: 'list',
name: 'credential',
choices: list,
message: 'Select credential',
},
{
type: 'list',
name: 'addMore',
message: 'Add another credential?',
choices: [
{ name: 'Yes', value: true },
{ name: 'No', value: false },
],
},
])
verifiableCredential.push(answers2.credential)
addMoreCredentials = answers2.addMore
}

const signAction: W3c.ActionSignW3cVc = {
type: W3c.ActionTypes.signVc,
const signAction: W3c.ActionSignW3cVp = {
type: W3c.ActionTypes.signVp,
did: answers.iss,
data: {
sub: answers.sub,
vc: {
aud: aud,
vp: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
credentialSubject,
verifiableCredential,
},
},
}
Expand All @@ -73,7 +205,7 @@ program
type: DIDComm.ActionTypes.sendJwt,
data: {
from: answers.iss,
to: answers.sub,
to: answers.aud,
jwt,
},
}
Expand Down
9 changes: 3 additions & 6 deletions packages/daf-cli/src/data-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ program
for (const did of dids) {
const shortId = await dataStore.shortId(did.did)
identities.push({
did: did.did,
shortId,
value: did.did,
name: `${did.did} - ${shortId}`,
})
}

const answers = await inquirer.prompt([
{
type: 'list',
name: 'sub',
choices: identities.map(identity => ({
value: identity.did,
name: `${identity.did} - ${identity.shortId}`,
})),
choices: identities,
message: 'Identity',
},
{
Expand Down

0 comments on commit c7e1360

Please sign in to comment.