Skip to content

Commit

Permalink
Use state enums on nodejs layer
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslavkovar@protonmail.com>
  • Loading branch information
mirgee committed Jul 7, 2021
1 parent 12af876 commit c3b1e6a
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ members = [
debug = true
panic = 'unwind'
incremental = false

[profile.dev]
split-debuginfo = "unpacked"
6 changes: 3 additions & 3 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,11 +88,11 @@ async function runFaber (options) {
let proofProtocolState = await vcxProof.updateStateV2(connectionToAlice)
logger.debug(`vcxProof = ${JSON.stringify(vcxProof)}`)
logger.debug(`proofState = ${proofProtocolState}`)
while (![2, 3].includes(proofProtocolState)) { // 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 === 3) {
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: 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
InviterStateType
} = 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() !== 4) {
if (await connection.updateState() !== InviterStateType.Completed) {
return { result: undefined, isFinished: false }
} else {
return { result: null, isFinished: true }
Expand Down
6 changes: 3 additions & 3 deletions agents/node/vcxagent-core/src/services/service-cred-issuer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {
StateType,
IssuerStateType,
IssuerCredential
} = require('@hyperledger/node-vcx-wrapper')
const { pollFunction } = require('../common')
Expand Down Expand Up @@ -37,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, 2, 10, 2000)
await _progressIssuerCredentialToState(issuerCred, connection, IssuerStateType.RequestReceived, 10, 2000)
await saveIssuerCredential(issuerCredId, issuerCred)
}

Expand All @@ -46,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, 4, 10, 2000)
await _progressIssuerCredentialToState(issuerCred, connection, IssuerStateType.Finished, 10, 2000)
await saveIssuerCredential(issuerCredId, issuerCred)
}

Expand Down
4 changes: 2 additions & 2 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 @@ -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, [2, 3])
await _progressProofToState(disclosedProof, connection, [ProverStateType.PresentationPreparationFailed, ProverStateType.PresentationSent])
const state = await disclosedProof.getState()
await saveDisclosedProof(disclosedProofId, disclosedProof)
return state
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 { InviteeStateType, 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(InviteeStateType.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 { InviterStateType, 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(InviterStateType.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(InviterStateType.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
49 changes: 49 additions & 0 deletions wrappers/node/src/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,55 @@ export enum StateType {
Rejected = 9,
}

export enum InviterStateType {
Null = 0,
Invited = 1,
Requested = 2,
Responded = 3,
Completed = 4
}

export enum InviteeStateType {
Null = 0,
Invited = 1,
Requested = 2,
Responded = 3,
Completed = 4
}

export enum HolderStateType {
OfferReceived = 0,
RequestSent = 1,
Finished = 2,
Failed = 3,
}

export enum IssuerStateType {
Initial = 0,
OfferSent = 1,
RequestReceived = 2,
CredentialSent = 3,
Finished = 4,
Failed = 5,
}

export enum ProverStateType {
Initial = 0,
PresentationPrepared = 1,
PresentationPreparationFailed = 2,
PresentationSent = 3,
Finished = 4,
Failed = 5,
}

export enum VerifierStateType {
Initial = 0,
PresentationRequestSent = 1,
Finished = 2,
Failed = 3,
}


export interface IInitVCXOptions {
libVCXPath?: string;
}
Expand Down

0 comments on commit c3b1e6a

Please sign in to comment.