Skip to content

Commit

Permalink
Remove notion of protocol_type from vcxagent, improve proof generation
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Oct 10, 2020
1 parent ecffaaf commit 2697ac5
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 23,449 deletions.
23,350 changes: 0 additions & 23,350 deletions agents/node/vcxagent-cli/package-lock.json

This file was deleted.

13 changes: 1 addition & 12 deletions agents/node/vcxagent-cli/vcxclient-cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { runInteractive } = require('./vcxclient-interactive')
const { runScript } = require('./script-common')
const { allowedProtocolTypes } = require('@absaoss/vcxagent-core')

const optionDefinitions = [
{
Expand All @@ -9,12 +8,6 @@ const optionDefinitions = [
type: Boolean,
description: 'Display this usage guide.'
},
{
name: 'protocolType',
type: String,
description: 'Protocol type. Possible values: "1.0" "2.0" "3.0" "4.0". Default is 4.0',
defaultValue: '4.0'
},
{
name: 'postgresql',
type: Boolean,
Expand Down Expand Up @@ -56,11 +49,7 @@ const usage = [
}
]

function areOptionsValid (options) {
if (!(allowedProtocolTypes.includes(options.protocolType))) {
console.error(`Unknown protocol type ${options.protocolType}. Only ${JSON.stringify(allowedProtocolTypes)} are allowed.`)
return false
}
function areOptionsValid (_options) {
return true
}

Expand Down
7 changes: 3 additions & 4 deletions agents/node/vcxagent-cli/vcxclient-interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ const readlineSync = require('readline-sync')
const { initRustapi, getSampleSchemaData, createVcxAgent } = require('@absaoss/vcxagent-core')
const logger = require('./logger')('VCX Client')

async function createInteractiveClient (agentName, seed, acceptTaa, protocolType, rustLogLevel) {
logger.info(`Creating interactive client ${agentName} seed=${seed} protocolType=${protocolType}`)
async function createInteractiveClient (agentName, seed, acceptTaa, rustLogLevel) {
logger.info(`Creating interactive client ${agentName} seed=${seed}`)

await initRustapi(rustLogLevel)

const ariesAgent = await createVcxAgent({
agentName,
protocolType,
agencyUrl: 'http://localhost:8080',
seed,
webhookUrl: `http://localhost:7209/notifications/${agentName}`,
Expand Down Expand Up @@ -93,7 +92,7 @@ async function createInteractiveClient (agentName, seed, acceptTaa, protocolType
async function runInteractive (options) {
logger.debug(`Going to build interactive client using options ${JSON.stringify(options)}`)
const agentName = options.name || readlineSync.question('Enter agent\'s name:\n')
await createInteractiveClient(agentName, options.seed, options.acceptTaa, options.protocolType, options.RUST_LOG)
await createInteractiveClient(agentName, options.seed, options.acceptTaa, options.RUST_LOG)
}

module.exports.runInteractive = runInteractive
24 changes: 8 additions & 16 deletions agents/node/vcxagent-core/demo/alice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const readlineSync = require('readline-sync')
const sleepPromise = require('sleep-promise')
const { initRustapi, allowedProtocolTypes } = require('../src/index')
const { initRustapi } = require('../src/index')
const { createVcxAgent } = require('../src/index')
const logger = require('./logger')('Alice')
const { runScript } = require('./script-common')
Expand All @@ -10,6 +10,8 @@ const isPortReachable = require('is-port-reachable')
const url = require('url')
const { extractProofRequestAttachement } = require('../src/utils/proofs')

const mapRevRegIdToTailsFile = (_revRegId) => '/tmp/tails'

async function getInvitationString (fetchInviteUrl) {
let invitationString
if (fetchInviteUrl) {
Expand Down Expand Up @@ -45,7 +47,6 @@ async function runAlice (options) {
const disclosedProofId = 'alice-proof'
const vcxAgent = await createVcxAgent({
agentName,
protocolType: options.protocolType,
agencyUrl: 'http://localhost:8080',
seed: '000000000000000000000000Trustee1',
usePostgresWallet: false,
Expand All @@ -64,16 +65,17 @@ async function runAlice (options) {

await vcxAgent.serviceCredHolder.waitForCredentialOfferAndAcceptAndProgress(connectionId, holderCredentialId)

const proofRequest = (await vcxAgent.serviceProver.waitForProofRequests(connectionId))[0]
if (!proofRequest) {
const proofRequests = await vcxAgent.serviceProver.waitForProofRequests(connectionId)
if (proofRequests.length === 0) {
throw Error('No proof request found.')
}
const proofRequest = proofRequests[0]

await vcxAgent.serviceProver.buildDisclosedProof(disclosedProofId, proofRequest)
const requestInfo = extractProofRequestAttachement(proofRequest)
logger.debug(`Proof request presentation attachment ${JSON.stringify(requestInfo, null, 2)}`)

const selectedCreds = await vcxAgent.serviceProver.selectCredentials(disclosedProofId)
const selectedCreds = await vcxAgent.serviceProver.selectCredentials(disclosedProofId, mapRevRegIdToTailsFile)
const selfAttestedAttrs = { attribute_3: 'Smith' }
await vcxAgent.serviceProver.generateProof(disclosedProofId, selectedCreds, selfAttestedAttrs)
await vcxAgent.serviceProver.sendDisclosedProofAndProgress(disclosedProofId, connectionId)
Expand All @@ -90,12 +92,6 @@ const optionDefinitions = [
type: Boolean,
description: 'Display this usage guide.'
},
{
name: 'protocolType',
type: String,
description: 'Protocol type. Possible values: "1.0" "2.0" "3.0" "4.0". Default is 4.0',
defaultValue: '4.0'
},
{
name: 'postgresql',
type: Boolean,
Expand All @@ -119,11 +115,7 @@ const usage = [
}
]

function areOptionsValid (options) {
if (!(allowedProtocolTypes.includes(options.protocolType))) {
console.error(`Unknown protocol type ${options.protocolType}. Only ${JSON.stringify(allowedProtocolTypes)} are allowed.`)
return false
}
function areOptionsValid (_options) {
return true
}

Expand Down
31 changes: 13 additions & 18 deletions agents/node/vcxagent-core/demo/faber.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const { StateType, ProofState, Proof } = require('@absaoss/node-vcx-wrapper')
const { initRustapi, protocolTypes } = require('../src/index')
const { createVcxAgent } = require('../src/index')
const sleepPromise = require('sleep-promise')
const { runScript } = require('./script-common')
const logger = require('./logger')('Faber')
const assert = require('assert')
const uuid = require('uuid')
const express = require('express')
const bodyParser = require('body-parser')
const { getSampleSchemaData } = require('../src')
const { getAliceSchemaAttrs, getFaberCredDefName, getFaberProofData } = require('../test/utils/data')
const { getFaberProofDataWithNonRevocation } = require('../test/utils/data')
const { createVcxAgent, initRustapi, getSampleSchemaData, buildRevocationDetails } = require('../src/index')
const { getAliceSchemaAttrs, getFaberCredDefName } = require('../test/utils/data')

const tailsFile = '/tmp/tails'

async function runFaber (options) {
logger.info(`Starting. Revocation enabled=${options.revocation}`)
Expand All @@ -25,7 +26,6 @@ async function runFaber (options) {
const agentName = `faber-${uuid.v4()}`
vcxAgent = await createVcxAgent({
agentName,
protocolType: options.protocolType,
agencyUrl: 'http://localhost:8080',
seed: '000000000000000000000000Trustee1',
usePostgresWallet: false,
Expand All @@ -39,7 +39,7 @@ async function runFaber (options) {
}

const schemaId = await vcxAgent.serviceLedgerSchema.createSchema(getSampleSchemaData())
await vcxAgent.serviceLedgerCredDef.createCredentialDefinition(schemaId, getFaberCredDefName())
await vcxAgent.serviceLedgerCredDef.createCredentialDefinition(schemaId, getFaberCredDefName(), buildRevocationDetails({ supportRevocation: true, tailsFile, maxCreds: 5 }))

const { connection: connectionToAlice } = await vcxAgent.serviceConnections.inviterConnectionCreateAndAccept(connectionId, (invitationString) => {
logger.info('\n\n**invite details**')
Expand Down Expand Up @@ -76,7 +76,7 @@ async function runFaber (options) {
}

logger.info('#19 Create a Proof object')
const vcxProof = await Proof.create(getFaberProofData(vcxAgent.getInstitutionDid(), proofId))
const vcxProof = await Proof.create(getFaberProofDataWithNonRevocation(vcxAgent.getInstitutionDid(), proofId))

logger.info('#20 Request proof of degree from alice')
await vcxProof.requestProof(connectionToAlice)
Expand All @@ -89,6 +89,11 @@ async function runFaber (options) {
await sleepPromise(2000)
proofProtocolState = await vcxProof.updateState()
logger.info(`proofState=${proofProtocolState}`)
if (proofProtocolState === StateType.None) {
logger.error(`Faber proof protocol state is ${StateType.None} which an error has ocurred.`)
logger.error(`Serialized proof state = ${JSON.stringify(await vcxProof.serialize())}`)
process.exit(-1)
}
}

logger.info('#27 Process the proof provided by alice.')
Expand Down Expand Up @@ -137,12 +142,6 @@ const optionDefinitions = [
type: Boolean,
description: 'Display this usage guide.'
},
{
name: 'protocolType',
type: String,
description: 'Protocol type. Possible values: "1.0" "2.0" "3.0" "4.0". Default is 4.0',
defaultValue: '4.0'
},
{
name: 'postgresql',
type: Boolean,
Expand Down Expand Up @@ -172,11 +171,7 @@ const usage = [
}
]

function areOptionsValid (options) {
if (!(Object.values(protocolTypes).includes(options.protocolType))) {
console.error(`Unknown protocol type ${options.protocolType}. Only ${JSON.stringify(Object.values(protocolTypes))} are allowed.`)
return false
}
function areOptionsValid (_options) {
return true
}
runScript(optionDefinitions, usage, areOptionsValid, runFaber)
3 changes: 1 addition & 2 deletions agents/node/vcxagent-core/src/agent/service-connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ module.exports.createServiceConnections = function createServiceConnections (log
}

async function signData (connectionId, dataBase64) {
const connSerializedBefore = await loadConnection(connectionId)
const connection = await Connection.deserialize(connSerializedBefore)
const connection = await loadConnection(connectionId)
var challengeBuffer = Buffer.from(dataBase64, 'base64')
let signatureBuffer
try {
Expand Down
2 changes: 0 additions & 2 deletions agents/node/vcxagent-core/src/agent/service-cred-issuer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const {
Connection,
StateType,
IssuerCredential
} = require('@absaoss/node-vcx-wrapper')
const { pollFunction } = require('../common')

module.exports.createServiceCredIssuer = function createServiceCredIssuer (logger, loadConnection, loadCredDef, storeIssuerCredential, loadIssuerCredential, listIssuerCredentialIds) {

async function sendOffer (issuerCredId, connectionId, credDefId, schemaAttrs) {
const connection = await loadConnection(connectionId)
const credDef = await loadCredDef(credDefId)
Expand Down
15 changes: 6 additions & 9 deletions agents/node/vcxagent-core/src/agent/service-ledger-creddef.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
const { CredentialDef } = require('@absaoss/node-vcx-wrapper')

module.exports.createServiceLedgerCredDef = function createServiceLedgerCredDef (logger, storeSchema, loadSchema, storeCredDef, loadCredDef, listCredDefIds) {
async function createCredentialDefinition (schemaId, credDefId) {
logger.info('Create a new credential definition on the ledger')
async function createCredentialDefinition (schemaId, credDefId, revocationDetails) {
const data = {
name: credDefId,
paymentHandle: 0,
revocationDetails: {
supportRevocation: true,
tailsFile: '/tmp/tails',
maxCreds: 5
},
schemaId: schemaId,
sourceId: 'testCredentialDefSourceId123'
revocationDetails,
schemaId,
sourceId: credDefId
}
logger.info(`Create a new credential definition on the ledger from input: ${JSON.stringify(data)}`)

const credDef = await CredentialDef.create(data)
await storeCredDef(credDefId, credDef)
logger.info(`Created credentialDefinition ${credDefId}.`)
Expand Down
4 changes: 2 additions & 2 deletions agents/node/vcxagent-core/src/agent/service-prover.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ module.exports.createServiceProver = function createServiceProver (logger, loadC
await storeDisclosedProof(disclosedProofId, disclosedProof)
}

async function selectCredentials (disclosedProofId) {
async function selectCredentials (disclosedProofId, mapRevRegIdToTailsFilePath) {
const disclosedProof = await loadDisclosedProof(disclosedProofId)
return holderSelectCredentialsForProof(disclosedProof, logger)
return holderSelectCredentialsForProof(disclosedProof, logger, mapRevRegIdToTailsFilePath)
}

async function generateProof (disclosedProofId, selectedCreds, selfAttestedAttrs) {
Expand Down
4 changes: 2 additions & 2 deletions agents/node/vcxagent-core/src/agent/vcx-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const {
const { createStorageService } = require('../state/storage-service')
const { waitUntilAgencyIsReady } = require('../common')

async function createVcxAgent ({ agentName, genesisPath, protocolType, agencyUrl, seed, usePostgresWallet, logger }) {
async function createVcxAgent ({ agentName, genesisPath, agencyUrl, seed, usePostgresWallet, logger }) {
genesisPath = genesisPath || `${__dirname}/../../resources/docker.txn`

await waitUntilAgencyIsReady(agencyUrl, logger)

const storageService = await createStorageService(agentName)
if (!await storageService.agentProvisionExists()) {
const agentProvision = await provisionAgentInAgency(agentName, genesisPath, protocolType, agencyUrl, seed, usePostgresWallet, logger)
const agentProvision = await provisionAgentInAgency(agentName, genesisPath, agencyUrl, seed, usePostgresWallet, logger)
await storageService.saveAgentProvision(agentProvision)
}
const agentProvision = await storageService.loadAgentProvision()
Expand Down
15 changes: 15 additions & 0 deletions agents/node/vcxagent-core/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ function getSampleSchemaData () {
}
}

module.exports.buildRevocationDetails = function buildRevocationDetails ({ supportRevocation, tailsFile, maxCreds }) {
if (supportRevocation === true) {
return {
supportRevocation,
tailsFile,
maxCreds
}
} else {
return {
supportRevocation: false
// tailsFile: '/tmp/tails', // todo: CredDefinition in node wrapper should not require this when revocation is disabled
}
}
}

function getRandomInt (min, max) {
min = Math.ceil(min)
max = Math.floor(max)
Expand Down
16 changes: 14 additions & 2 deletions agents/node/vcxagent-core/src/utils/proofs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
module.exports.holderSelectCredentialsForProof = async function holderSelectCredentialsForProof (holderProof, logger) {
/**
* Based on credentials in the wallet and proof request, this function builds data structure specifying how to
* build proof complying with the proof request.
* @param holderProof - deserialized libvcx Proof object
* @param logger - logger implementing debug and info functions
* @param mapRevRegIdToTailsFilePath - function receiving 1 argument - rev_reg_id, mapping it to a path pointing to
* file containing tails file for the given revocation registry.
* @returns {Promise<{attrs: {}}>}
*/
module.exports.holderSelectCredentialsForProof = async function holderSelectCredentialsForProof (holderProof, logger, mapRevRegIdToTailsFilePath) {
const resolvedCreds = await holderProof.getCredentials()
const selectedCreds = { attrs: {} }
logger.debug(`Resolved credentials for proof = ${JSON.stringify(resolvedCreds, null, 2)}`)
Expand All @@ -12,7 +21,10 @@ module.exports.holderSelectCredentialsForProof = async function holderSelectCred
selectedCreds.attrs[attrName] = {
credential: resolvedCreds.attrs[attrName][0]
}
selectedCreds.attrs[attrName].tails_file = '/tmp/tails'
const revRegId = resolvedCreds.attrs[attrName][0].cred_info.rev_reg_id
if (revRegId) {
selectedCreds.attrs[attrName].tails_file = await mapRevRegIdToTailsFilePath(revRegId)
}
} else {
logger.info(`No credential was resolved for requested attribute key ${attrName}, will have to be supplied via self-attested attributes.`)
}
Expand Down
18 changes: 3 additions & 15 deletions agents/node/vcxagent-core/src/utils/vcx-workflows.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ const os = require('os')
const extension = { darwin: '.dylib', linux: '.so', win32: '.dll' }
const libPath = { darwin: '/usr/local/lib/', linux: '/usr/lib/', win32: 'c:\\windows\\system32\\' }

module.exports.allowedProtocolTypes = ['1.0', '2.0', '3.0', '4.0']

module.exports.protocolTypes = {
v1: '1.0',
v2: '2.0',
v3: '3.0',
v4: '4.0'
}

function getLibraryPath (libraryName) {
const platform = os.platform()
const postfix = extension[platform.toLowerCase()] || extension.linux
Expand Down Expand Up @@ -45,16 +36,13 @@ async function initRustapi (logLevel = 'vcx=error') {
await initRustApiAndLogger(logLevel)
}

async function provisionAgentInAgency (agentName, genesisPath, protocolType, agencyUrl, seed, usePostgresWallet, logger) {
async function provisionAgentInAgency (agentName, genesisPath, agencyUrl, seed, usePostgresWallet, logger) {
if (!agentName) {
throw Error('agentName not specified')
}
if (!genesisPath) {
throw Error('genesisPath not specified')
}
if (!protocolType) {
throw Error('protocolType not specified')
}
if (!agencyUrl) {
throw Error('agencyUrl not specified')
}
Expand All @@ -68,8 +56,8 @@ async function provisionAgentInAgency (agentName, genesisPath, protocolType, age
wallet_name: agentName,
wallet_key: '123',
payment_method: 'null',
enterprise_seed: seed,
protocol_type: protocolType
protocol_type: '4.0',
enterprise_seed: seed
}
if (usePostgresWallet) {
logger.info('Will use PostreSQL wallet. Initializing plugin.')
Expand Down
Loading

0 comments on commit 2697ac5

Please sign in to comment.