Skip to content

Commit

Permalink
Remove dependence of aries module on VcxStateType (#314)
Browse files Browse the repository at this point in the history
* Modify numeric representation of aries protocols states (breaking)
* Reject on FFI error in getState() and updateState() (breaking)
* Remove dependence of aries module on VcxStateType (refactoring)
* Move agency pool test (refactoring)
* Make state type generic on typescript wrapper level (refactoring)

Signed-off-by: Miroslav Kovar <[email protected]>
  • Loading branch information
mirgee authored Jul 19, 2021
1 parent 3c2165d commit 6d46888
Show file tree
Hide file tree
Showing 57 changed files with 674 additions and 673 deletions.
2 changes: 1 addition & 1 deletion agents/node/vcxagent-core/demo/alice.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async function runAlice (options) {

function _validateMsgs (msgs) {
logger.debug(`Validating messages:\n${JSON.stringify(msgs, null, 2)}`)
assert(msgs.length === 5)
assert(msgs.length === 4)
assert(msgs[0].uid)
assert(msgs[0].statusCode)
assert(msgs[0].decryptedMsg)
Expand Down
8 changes: 4 additions & 4 deletions agents/node/vcxagent-core/demo/faber.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { StateType, ProofState, Proof } = require('@hyperledger/node-vcx-wrapper')
const { VerifierStateType, ProofState, Proof } = require('@hyperledger/node-vcx-wrapper')
const sleepPromise = require('sleep-promise')
const { runScript } = require('./script-common')
const { testTailsUrl } = require('../src/common')
Expand Down Expand Up @@ -88,12 +88,12 @@ async function runFaber (options) {
let proofProtocolState = await vcxProof.updateStateV2(connectionToAlice)
logger.debug(`vcxProof = ${JSON.stringify(vcxProof)}`)
logger.debug(`proofState = ${proofProtocolState}`)
while (proofProtocolState !== StateType.Accepted) { // even if revoked credential was used, state should in final state be StateType.Accepted
while (![VerifierStateType.Finished, VerifierStateType.Failed].includes(proofProtocolState)) {
await sleepPromise(2000)
proofProtocolState = await vcxProof.updateStateV2(connectionToAlice)
logger.info(`proofState=${proofProtocolState}`)
if (proofProtocolState === StateType.None) {
logger.error(`Faber proof protocol state is ${StateType.None} which an error has ocurred.`)
if (proofProtocolState === VerifierStateType.Failed) {
logger.error(`Faber proof protocol state is ${3} which an error has ocurred.`)
logger.error(`Serialized proof state = ${JSON.stringify(await vcxProof.serialize())}`)
process.exit(-1)
}
Expand Down
4 changes: 3 additions & 1 deletion agents/node/vcxagent-core/src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async function createVcxAgent ({ agentName, genesisPath, agencyUrl, seed, usePos
await storageService.saveAgentProvision(agentProvision)
}
const agentProvision = await storageService.loadAgentProvision()
const issuerDid = agentProvision.issuerConfig["institution_did"]

async function agentInitVcx () {
logger.info(`Initializing ${agentName} vcx session.`)
Expand Down Expand Up @@ -93,7 +94,8 @@ async function createVcxAgent ({ agentName, genesisPath, agencyUrl, seed, usePos
loadCredDef: storageService.loadCredentialDefinition,
saveIssuerCredential: storageService.saveCredIssuer,
loadIssuerCredential: storageService.loadCredIssuer,
listIssuerCredentialIds: storageService.listCredIssuerKeys
listIssuerCredentialIds: storageService.listCredIssuerKeys,
issuerDid
})
const serviceCredHolder = createServiceCredHolder({
logger,
Expand Down
4 changes: 2 additions & 2 deletions agents/node/vcxagent-core/src/services/service-connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { getMessagesForConnection } = require('../utils/messages')
const {
updateMessages,
Connection,
StateType
ConnectionStateType
} = require('@hyperledger/node-vcx-wrapper')
const { pollFunction } = require('../common')

Expand Down Expand Up @@ -50,7 +50,7 @@ module.exports.createServiceConnections = function createServiceConnections ({ l

async function _progressConnectionToAcceptedState (connection, attemptsThreshold, timeoutMs) {
async function progressToAcceptedState () {
if (await connection.updateState() !== StateType.Accepted) {
if (await connection.updateState() !== ConnectionStateType.Finished) {
return { result: undefined, isFinished: false }
} else {
return { result: null, isFinished: true }
Expand Down
5 changes: 3 additions & 2 deletions agents/node/vcxagent-core/src/services/service-cred-holder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {filterOffersByCredentialName} = require('../utils/credentials')
const { filterOffersByAttr } = require('../utils/credentials')
const { filterOffersBySchema } = require('../utils/credentials')
const {
StateType,
HolderStateType,
Credential
} = require('@hyperledger/node-vcx-wrapper')
const { pollFunction } = require('../common')
Expand All @@ -11,6 +11,7 @@ module.exports.createServiceCredHolder = function createServiceCredHolder ({ log
async function _getOffers (connection, filter, attemptsThreshold, timeoutMs) {
async function findSomeCredOffer () {
let offers = await Credential.getOffers(connection)
logger.info(`Offers: ${offers}`)
if (filter && filter.schemaIdRegex) {
offers = filterOffersBySchema(offers, filter.schemaIdRegex)
}
Expand Down Expand Up @@ -49,7 +50,7 @@ module.exports.createServiceCredHolder = function createServiceCredHolder ({ log
async function waitForCredential (connectionId, holderCredentialId, attemptsThreshold = 10, timeoutMs = 2000) {
const connection = await loadConnection(connectionId)
const credential = await loadHolderCredential(holderCredentialId)
await _progressCredentialToState(credential, connection, StateType.Accepted, attemptsThreshold, timeoutMs)
await _progressCredentialToState(credential, connection, HolderStateType.Finished, attemptsThreshold, timeoutMs)
logger.info('Credential has been received.')
await saveHolderCredential(holderCredentialId, credential)
return getCredentialData(holderCredentialId)
Expand Down
11 changes: 6 additions & 5 deletions agents/node/vcxagent-core/src/services/service-cred-issuer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const {
StateType,
IssuerStateType,
IssuerCredential
} = require('@hyperledger/node-vcx-wrapper')
const { pollFunction } = require('../common')

module.exports.createServiceCredIssuer = function createServiceCredIssuer ({ logger, loadConnection, loadCredDef, saveIssuerCredential, loadIssuerCredential, listIssuerCredentialIds }) {
module.exports.createServiceCredIssuer = function createServiceCredIssuer ({ logger, loadConnection, loadCredDef, saveIssuerCredential, loadIssuerCredential, listIssuerCredentialIds, issuerDid }) {
async function sendOffer (issuerCredId, connectionId, credDefId, schemaAttrs) {
const connection = await loadConnection(connectionId)
const credDef = await loadCredDef(credDefId)
Expand All @@ -14,7 +14,8 @@ module.exports.createServiceCredIssuer = function createServiceCredIssuer ({ log
sourceId: 'alice_degree',
credDefHandle: credDef.handle,
credentialName: 'cred',
price: '0'
price: '0',
issuerDid
})
logger.info(`Per issuer credential ${issuerCredId}, sending cred offer to connection ${connectionId}`)
await issuerCred.sendOffer(connection)
Expand All @@ -36,7 +37,7 @@ module.exports.createServiceCredIssuer = function createServiceCredIssuer ({ log
const issuerCred = await loadIssuerCredential(issuerCredId)
const connection = await loadConnection(connectionId)
logger.debug('Going to wait until credential request is received.')
await _progressIssuerCredentialToState(issuerCred, connection, StateType.RequestReceived, 10, 2000)
await _progressIssuerCredentialToState(issuerCred, connection, IssuerStateType.RequestReceived, 10, 2000)
await saveIssuerCredential(issuerCredId, issuerCred)
}

Expand All @@ -45,7 +46,7 @@ module.exports.createServiceCredIssuer = function createServiceCredIssuer ({ log
const connection = await loadConnection(connectionId)
const issuerCred = await loadIssuerCredential(issuerCredId)
logger.info('Going to wait until counterparty accepts the credential.')
await _progressIssuerCredentialToState(issuerCred, connection, StateType.Accepted, 10, 2000)
await _progressIssuerCredentialToState(issuerCred, connection, IssuerStateType.Finished, 10, 2000)
await saveIssuerCredential(issuerCredId, issuerCred)
}

Expand Down
6 changes: 3 additions & 3 deletions agents/node/vcxagent-core/src/services/service-prover.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { pollFunction } = require('../common')
const { holderSelectCredentialsForProof } = require('../utils/proofs')
const {
DisclosedProof,
StateType
ProverStateType
} = require('@hyperledger/node-vcx-wrapper')

module.exports.createServiceProver = function createServiceProver ({ logger, loadConnection, saveDisclosedProof, loadDisclosedProof, listDislosedProofIds }) {
Expand Down Expand Up @@ -35,7 +35,7 @@ module.exports.createServiceProver = function createServiceProver ({ logger, loa
}
}

const [error, proofRequests] = await pollFunction(findSomeRequests, 'Get credential offer', logger, attemptsThreshold, timeoutMs)
const [error, proofRequests] = await pollFunction(findSomeRequests, 'Get proof request', logger, attemptsThreshold, timeoutMs)
if (error) {
throw Error(`Couldn't find any proof request. ${error}`)
}
Expand Down Expand Up @@ -84,7 +84,7 @@ module.exports.createServiceProver = function createServiceProver ({ logger, loa
await sendDisclosedProof(disclosedProofId, connectionId)
const disclosedProof = await loadDisclosedProof(disclosedProofId)
const connection = await loadConnection(connectionId)
await _progressProofToState(disclosedProof, connection, [StateType.Accepted, StateType.None])
await _progressProofToState(disclosedProof, connection, [ProverStateType.PresentationPreparationFailed, ProverStateType.PresentationSent])
const state = await disclosedProof.getState()
await saveDisclosedProof(disclosedProofId, disclosedProof)
return state
Expand Down
3 changes: 2 additions & 1 deletion agents/node/vcxagent-core/src/utils/vcx-workflows.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ async function provisionAgentInAgency (agentName, genesisPath, agencyUrl, seed,
logger.debug(`Configuring issuer's wallet with seed: ${seed}`)
const issuerConfig = JSON.parse(await configureIssuerWallet(seed))
issuerConfig.institution_name = agentName
logger.debug(`Configured issuer wallet with config: ${JSON.stringify(issuerConfig, null, 2)}`)
logger.debug(`Provisioning agent with config: ${JSON.stringify(agencyConfig, null, 2)}`)
agencyConfig = JSON.parse(await provisionCloudAgent(agencyConfig))
logger.debug(`Provisined agent with config: ${JSON.stringify(agencyConfig, null, 2)}`)
logger.debug(`Provisioned agent with config: ${JSON.stringify(agencyConfig, null, 2)}`)
await closeMainWallet()

return { agencyConfig, issuerConfig, walletConfig }
Expand Down
10 changes: 5 additions & 5 deletions agents/node/vcxagent-core/test/distribute-tails.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const axios = require('axios')
const { buildRevocationDetails } = require('../src')
const { createPairedAliceAndFaber } = require('./utils/utils')
const { initRustapi } = require('../src/index')
const { StateType } = require('@hyperledger/node-vcx-wrapper')
const { IssuerStateType, HolderStateType, VerifierStateType, ProverStateType } = require('@hyperledger/node-vcx-wrapper')
const uuid = require('uuid')
const sleep = require('sleep-promise')
const fs = require('fs')
Expand All @@ -27,9 +27,9 @@ describe('test tails distribution', () => {
const tailsUrl = `http://127.0.0.1:${port}/${tailsUrlId}`
await faber.sendCredentialOffer(buildRevocationDetails({ supportRevocation: true, tailsFile: `${__dirname}/tmp/faber/tails`, tailsUrl, maxCreds: 5 }))
await alice.acceptCredentialOffer()
await faber.updateStateCredentialV2(StateType.RequestReceived)
await faber.updateStateCredentialV2(IssuerStateType.RequestReceived)
await faber.sendCredential()
await alice.updateStateCredentialV2(StateType.Accepted)
await alice.updateStateCredentialV2(HolderStateType.Finished)

const faberTailsHash = await faber.getTailsHash()
const app = express()
Expand All @@ -47,8 +47,8 @@ describe('test tails distribution', () => {

const request = await faber.requestProofFromAlice()
await alice.sendHolderProof(JSON.parse(request), revRegId => aliceTailsFileDir)
await faber.updateStateVerifierProofV2(StateType.Accepted)
await alice.updateStateHolderProofV2(StateType.Accepted)
await faber.updateStateVerifierProofV2(VerifierStateType.Finished)
await alice.updateStateHolderProofV2(ProverStateType.Finished)
} catch (err) {
console.error(`err = ${err.message} stack = ${err.stack}`)
if (server) {
Expand Down
1 change: 0 additions & 1 deletion agents/node/vcxagent-core/test/feature-discovery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ beforeAll(async () => {
})

describe('send ping, get ping', () => {

it('Faber should send credential to Alice', async () => {
try {
const { alice, faber } = await createPairedAliceAndFaber()
Expand Down
4 changes: 2 additions & 2 deletions agents/node/vcxagent-core/test/sign-messaging.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env jest */
require('jest')
const {createPairedAliceAndFaber} = require('./utils/utils')
const {initRustapi} = require('../src/index')
const { createPairedAliceAndFaber } = require('./utils/utils')
const { initRustapi } = require('../src/index')
const sleep = require('sleep-promise')

beforeAll(async () => {
Expand Down
11 changes: 5 additions & 6 deletions agents/node/vcxagent-core/test/update-state-v2.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-env jest */
require('jest')
const { shutdownVcx } = require('@hyperledger/node-vcx-wrapper')
const { createPairedAliceAndFaber } = require('./utils/utils')
const { initRustapi } = require('../src/index')
const { StateType } = require('@hyperledger/node-vcx-wrapper')
const { IssuerStateType, HolderStateType, ProverStateType, VerifierStateType } = require('@hyperledger/node-vcx-wrapper')
const sleep = require('sleep-promise')

beforeAll(async () => {
Expand All @@ -19,14 +18,14 @@ describe('test update state', () => {
await faber.sendCredentialOffer()
await alice.acceptCredentialOffer()

await faber.updateStateCredentialV2(StateType.RequestReceived)
await faber.updateStateCredentialV2(IssuerStateType.RequestReceived)
await faber.sendCredential()
await alice.updateStateCredentialV2(StateType.Accepted)
await alice.updateStateCredentialV2(HolderStateType.Finished)

const request = await faber.requestProofFromAlice()
await alice.sendHolderProof(JSON.parse(request))
await faber.updateStateVerifierProofV2(StateType.Accepted)
await alice.updateStateHolderProofV2(StateType.Accepted)
await faber.updateStateVerifierProofV2(VerifierStateType.Finished)
await alice.updateStateHolderProofV2(ProverStateType.Finished)
} catch (err) {
console.error(`err = ${err.message} stack = ${err.stack}`)
await sleep(2000)
Expand Down
6 changes: 3 additions & 3 deletions agents/node/vcxagent-core/test/utils/alice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env jest */
const { createVcxAgent } = require('../../src/index')
const { StateType } = require('@hyperledger/node-vcx-wrapper')
const { ConnectionStateType, ProverStateType } = require('@hyperledger/node-vcx-wrapper')

module.exports.createAlice = async function createAlice () {
const agentName = `alice-${Math.floor(new Date() / 1000)}`
Expand All @@ -26,7 +26,7 @@ module.exports.createAlice = async function createAlice () {

await vcxAgent.serviceConnections.inviteeConnectionAcceptFromInvitation(connectionId, invite)
const connection = await vcxAgent.serviceConnections.getVcxConnection(connectionId)
expect(await connection.getState()).toBe(StateType.OfferSent)
expect(await connection.getState()).toBe(ConnectionStateType.Requested)

await vcxAgent.agentShutdownVcx()
}
Expand Down Expand Up @@ -56,7 +56,7 @@ module.exports.createAlice = async function createAlice () {
const { selectedCreds } = await vcxAgent.serviceProver.selectCredentials(disclosedProofId, mapRevRegId)
const selfAttestedAttrs = { attribute_3: 'Smith' }
await vcxAgent.serviceProver.generateProof(disclosedProofId, selectedCreds, selfAttestedAttrs)
expect(await vcxAgent.serviceProver.sendDisclosedProof(disclosedProofId, connectionId)).toBe(StateType.OfferSent)
expect(await vcxAgent.serviceProver.sendDisclosedProof(disclosedProofId, connectionId)).toBe(ProverStateType.PresentationSent)

await vcxAgent.agentShutdownVcx()
}
Expand Down
10 changes: 5 additions & 5 deletions agents/node/vcxagent-core/test/utils/faber.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env jest */
const { buildRevocationDetails } = require('../../src')
const { createVcxAgent, getSampleSchemaData } = require('../../src')
const { StateType } = require('@hyperledger/node-vcx-wrapper')
const { ConnectionStateType, IssuerStateType, VerifierStateType } = require('@hyperledger/node-vcx-wrapper')
const { getAliceSchemaAttrs, getFaberCredDefName, getFaberProofData } = require('./data')

module.exports.createFaber = async function createFaber () {
Expand Down Expand Up @@ -30,7 +30,7 @@ module.exports.createFaber = async function createFaber () {
const invite = await vcxAgent.serviceConnections.inviterConnectionCreate(connectionId, undefined)
logger.info(`Faber generated invite:\n${invite}`)
const connection = await vcxAgent.serviceConnections.getVcxConnection(connectionId)
expect(await connection.getState()).toBe(StateType.Initialized)
expect(await connection.getState()).toBe(ConnectionStateType.Invited)

await vcxAgent.agentShutdownVcx()

Expand All @@ -41,7 +41,7 @@ module.exports.createFaber = async function createFaber () {
logger.info('Faber is going to generate invite')
await vcxAgent.agentInitVcx()

expect(await vcxAgent.serviceConnections.connectionUpdate(connectionId)).toBe(StateType.RequestReceived)
expect(await vcxAgent.serviceConnections.connectionUpdate(connectionId)).toBe(ConnectionStateType.Responded)

await vcxAgent.agentShutdownVcx()
}
Expand Down Expand Up @@ -90,7 +90,7 @@ module.exports.createFaber = async function createFaber () {
await vcxAgent.agentInitVcx()

logger.info('Issuer sending credential')
expect(await vcxAgent.serviceCredIssuer.sendCredential(issuerCredId, connectionId)).toBe(StateType.Accepted)
expect(await vcxAgent.serviceCredIssuer.sendCredential(issuerCredId, connectionId)).toBe(IssuerStateType.Finished)
logger.info('Credential sent')

await vcxAgent.agentShutdownVcx()
Expand All @@ -105,7 +105,7 @@ module.exports.createFaber = async function createFaber () {
await vcxAgent.serviceVerifier.createProof(proofId, proofData)
logger.info(`Faber is sending proof request to connection ${connectionId}`)
const { state, proofRequestMessage } = await vcxAgent.serviceVerifier.sendProofRequest(connectionId, proofId)
expect(state).toBe(StateType.OfferSent)
expect(state).toBe(VerifierStateType.PresentationRequestSent)
await vcxAgent.agentShutdownVcx()
return proofRequestMessage
}
Expand Down
8 changes: 4 additions & 4 deletions agents/node/vcxagent-core/test/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { createFaber } = require('./faber')
const { createAlice } = require('./alice')
const { StateType } = require('@hyperledger/node-vcx-wrapper')
const { ConnectionStateType } = require('@hyperledger/node-vcx-wrapper')

module.exports.createPairedAliceAndFaber = async function createPairedAliceAndFaber () {
const alice = await createAlice()
const faber = await createFaber()
const invite = await faber.createInvite()
await alice.acceptInvite(invite)
await faber.updateConnection(StateType.RequestReceived)
await alice.updateConnection(StateType.Accepted)
await faber.updateConnection(StateType.Accepted)
await faber.updateConnection(ConnectionStateType.Responded)
await alice.updateConnection(ConnectionStateType.Finished)
await faber.updateConnection(ConnectionStateType.Finished)
return { alice, faber }
}
8 changes: 1 addition & 7 deletions libvcx/src/api_lib/api_c/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::api_lib::api_handle::connection;
use crate::api_lib::utils_c;
use crate::api_lib::utils_c::cstring::CStringUtils;
use crate::api_lib::utils_c::runtime::execute;
use crate::aries::messages::a2a::A2AMessage;
use crate::error::prelude::*;
use crate::libindy;
use crate::utils::error;
Expand Down Expand Up @@ -548,13 +547,8 @@ pub extern fn vcx_connection_update_state_with_message(command_handle: CommandHa
return VcxError::from(VcxErrorKind::InvalidConnectionHandle).into();
}

let message: A2AMessage = match serde_json::from_str(&message) {
Ok(x) => x,
Err(_) => return VcxError::from(VcxErrorKind::InvalidJson).into(),
};

execute(move || {
let result = update_state_with_message(connection_handle, message);
let result = update_state_with_message(connection_handle, &message);

let rc = match result {
Ok(x) => {
Expand Down
Loading

0 comments on commit 6d46888

Please sign in to comment.