Skip to content

Commit

Permalink
Unify vcxagent-core api style
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 9, 2020
1 parent 7c292df commit aafc86f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions agents/node/vcxagent-core/demo/alice.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ async function runAlice (options) {
}
logger.info('Connection to alice was Accepted!')

await vcxAgent.serviceCredHolder.waitForCredentialOfferAndAcceptAndProgress({ connectionId, holderCredentialId })
await vcxAgent.serviceCredHolder.waitForCredentialOfferAndAcceptAndProgress(connectionId, holderCredentialId)

const proofRequest = (await vcxAgent.serviceProver.waitForProofRequests({ connectionId }))[0]
const proofRequest = (await vcxAgent.serviceProver.waitForProofRequests(connectionId))[0]
if (!proofRequest) {
throw Error('No proof request found.')
}
Expand Down
3 changes: 2 additions & 1 deletion agents/node/vcxagent-core/demo/faber.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ async function runFaber (options) {
})

logger.info('Faber is going to send credential offer')
await vcxAgent.serviceCredIssuer.sendOfferAndCredential({ issuerCredId, connectionId, credDefId, schemaAttrs: getAliceSchemaAttrs() })
const schemaAttrs = getAliceSchemaAttrs()
await vcxAgent.serviceCredIssuer.sendOfferAndCredential(issuerCredId, connectionId, credDefId, schemaAttrs)
if (options.revocation) {
await vcxAgent.serviceCredIssuer.revokeCredential(issuerCredId)
}
Expand Down
12 changes: 6 additions & 6 deletions agents/node/vcxagent-core/src/agent/service-cred-holder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports.createServiceCredHolder = function createServiceCredHolder (logge
return offers
}

async function _progressCredentialToState (credential, connection, credentialStateTarget, attemptsThreshold, timeoutMs) {
async function _progressCredentialToState (credential, connection, credentialStateTarget, attemptsThreshold = 10, timeoutMs = 2000) {
async function progressToAcceptedState () {
if (await credential.updateStateV2(connection) !== credentialStateTarget) {
return { result: undefined, isFinished: false }
Expand All @@ -48,7 +48,7 @@ module.exports.createServiceCredHolder = function createServiceCredHolder (logge
}
}

async function waitForCredential (connectionId, holderCredentialId, attemptsThreshold, timeoutMs) {
async function waitForCredential (connectionId, holderCredentialId, attemptsThreshold = 10, timeoutMs = 2000) {
const connSerializedBefore = await loadConnection(connectionId)
const connection = await Connection.deserialize(connSerializedBefore)

Expand Down Expand Up @@ -93,7 +93,7 @@ module.exports.createServiceCredHolder = function createServiceCredHolder (logge
return credential
}

async function waitForCredentialOffer (connectionId, credOfferFilter, attemptsThreshold, timeoutMs) {
async function waitForCredentialOffer (connectionId, credOfferFilter = null, attemptsThreshold = 10, timeoutMs = 2000) {
logger.info('Going to try fetch credential offer and receive credential.')
const connSerializedBefore = await loadConnection(connectionId)
const connection = await Connection.deserialize(connSerializedBefore)
Expand All @@ -107,14 +107,14 @@ module.exports.createServiceCredHolder = function createServiceCredHolder (logge
return pickedOffer
}

async function waitForCredentialOfferAndAccept ({ connectionId, holderCredentialId, credOfferFilter, attemptsThreshold = 10, timeoutMs = 2000 }) {
async function waitForCredentialOfferAndAccept (connectionId, holderCredentialId, credOfferFilter = null, attemptsThreshold = 10, timeoutMs = 2000) {
const pickedOffer = await waitForCredentialOffer(connectionId, credOfferFilter, attemptsThreshold, timeoutMs)
return createCredentialFromOfferAndSendRequest(connectionId, holderCredentialId, pickedOffer)
}

async function waitForCredentialOfferAndAcceptAndProgress ({ connectionId, holderCredentialId, credOfferFilter, attemptsThreshold = 10, timeoutMs = 2000 }) {
async function waitForCredentialOfferAndAcceptAndProgress (connectionId, holderCredentialId, credOfferFilter = null, attemptsThreshold = 10, timeoutMs = 2000) {
logger.info('Going to try fetch credential offer and receive credential.')
await waitForCredentialOfferAndAccept({ connectionId, holderCredentialId, credOfferFilter, attemptsThreshold, timeoutMs })
await waitForCredentialOfferAndAccept(connectionId, holderCredentialId, credOfferFilter, attemptsThreshold, timeoutMs)
return waitForCredential(connectionId, holderCredentialId, attemptsThreshold, timeoutMs)
}

Expand Down
18 changes: 9 additions & 9 deletions agents/node/vcxagent-core/src/agent/service-cred-issuer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { pollFunction } = require('../common')

module.exports.createServiceCredIssuer = function createServiceCredIssuer (logger, loadConnection, loadCredDef, storeIssuerCredential, loadIssuerCredential, listIssuerCredentialIds) {
// Creates issuer credential, sends offer
async function sendOffer ({ issuerCredId, connectionId, credDefId, schemaAttrs }) {
async function sendOffer (issuerCredId, connectionId, credDefId, schemaAttrs) {
logger.debug(`sendOffer >> issuerCredId=${issuerCredId} schemaAttrs=${schemaAttrs} credDefId=${credDefId} connectionId=${connectionId}`)
const serConnection = await loadConnection(connectionId)
const credDefSerialized = await loadCredDef(credDefId)
Expand Down Expand Up @@ -45,7 +45,7 @@ module.exports.createServiceCredIssuer = function createServiceCredIssuer (logge
}

// Assuming issuer credential is in state Requested, tries to send the actual credential
async function sendCredential ({ issuerCredId, connectionId }) {
async function sendCredential (issuerCredId, connectionId) {
logger.debug('sendCredential >> ')
const serConnection = await loadConnection(connectionId)
const connection = await Connection.deserialize(serConnection)
Expand All @@ -65,9 +65,9 @@ module.exports.createServiceCredIssuer = function createServiceCredIssuer (logge
}

// Creates issuer credential, sends offer and waits to receive credential request
async function sendOfferAndWaitForCredRequest ({ issuerCredId, connectionId, credDefId, schemaAttrs }) {
async function sendOfferAndWaitForCredRequest (issuerCredId, connectionId, credDefId, schemaAttrs) {
logger.debug('sendOfferAndWaitForCredRequest >> ')
const issuerCred = await sendOffer({ issuerCredId, schemaAttrs, credDefId, connectionId })
const issuerCred = await sendOffer(issuerCredId, connectionId, credDefId, schemaAttrs)

logger.debug('Going to wait until credential request is received.')
const serConnection = await loadConnection(connectionId)
Expand All @@ -81,9 +81,9 @@ module.exports.createServiceCredIssuer = function createServiceCredIssuer (logge
}

// Assuming issuer credential is in state "Requested", sends credential and wait to receive Ack
async function sendCredentialAndProgress ({ issuerCredId, connectionId }) {
async function sendCredentialAndProgress (issuerCredId, connectionId) {
logger.debug('sendCredentialAndProgress >> ')
await sendCredential({ issuerCredId, connectionId })
await sendCredential(issuerCredId, connectionId)

const serConnection = await loadConnection(connectionId)
const connection = await Connection.deserialize(serConnection)
Expand All @@ -101,10 +101,10 @@ module.exports.createServiceCredIssuer = function createServiceCredIssuer (logge
}

// Creates issuer credential, sends offer and waits to receive credential request, then sends credential
async function sendOfferAndCredential ({ issuerCredId, connectionId, credDefId, schemaAttrs }) {
async function sendOfferAndCredential (issuerCredId, connectionId, credDefId, schemaAttrs) {
logger.debug('sendOfferAndCredential >> ')
await sendOfferAndWaitForCredRequest({ issuerCredId, connectionId, credDefId, schemaAttrs })
await sendCredentialAndProgress({ issuerCredId, connectionId })
await sendOfferAndWaitForCredRequest(issuerCredId, connectionId, credDefId, schemaAttrs)
await sendCredentialAndProgress(issuerCredId, connectionId)
}

// Assuming the credential has been issued, tries to revoke the credential on ledger
Expand Down
2 changes: 1 addition & 1 deletion agents/node/vcxagent-core/src/agent/service-prover.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports.createServiceProver = function createServiceProver (logger, loadC
return proofRequests
}

async function waitForProofRequests ({ connectionId, attemptsThreshold = 10, timeoutMs = 2000 }) {
async function waitForProofRequests (connectionId, attemptsThreshold = 10, timeoutMs = 2000) {
const connSerializedBefore = await loadConnection(connectionId)
const connection = await Connection.deserialize(connSerializedBefore)

Expand Down
2 changes: 1 addition & 1 deletion agents/node/vcxagent-core/src/agent/service-verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
const sleep = require('sleep-promise')

module.exports.createServiceVerifier = function createServiceVerifier (logger, loadConnection, storeProof, loadProof, listProofIds) {
async function createProof ({ proofId, proofData }) {
async function createProof (proofId, proofData) {
logger.info(`Verifier creating proof ${proofId}, proofData=${JSON.stringify(proofData)}`)
await sleep(1000)
const proof = await Proof.create(proofData)
Expand Down
2 changes: 1 addition & 1 deletion agents/node/vcxagent-core/test/utils/alice.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports.createAlice = async function createAlice () {
await vcxAgent.agentInitVcx()

logger.info('Alice accepting creadential offer')
await vcxAgent.serviceCredHolder.waitForCredentialOfferAndAccept({ connectionId, holderCredentialId })
await vcxAgent.serviceCredHolder.waitForCredentialOfferAndAccept(connectionId, holderCredentialId)

await vcxAgent.agentShutdownVcx()
}
Expand Down
6 changes: 3 additions & 3 deletions agents/node/vcxagent-core/test/utils/faber.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports.createFaber = async function createFaber () {
logger.info('Faber sending credential to Alice')
const schemaAttrs = getAliceSchemaAttrs()
const credDefId = getFaberCredDefName()
await vcxAgent.serviceCredIssuer.sendOffer({ issuerCredId, connectionId, credDefId, schemaAttrs })
await vcxAgent.serviceCredIssuer.sendOffer(issuerCredId, connectionId, credDefId, schemaAttrs)

await vcxAgent.agentShutdownVcx()
}
Expand Down Expand Up @@ -95,7 +95,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(StateType.Accepted)
logger.info('Credential sent')

await vcxAgent.agentShutdownVcx()
Expand All @@ -106,7 +106,7 @@ module.exports.createFaber = async function createFaber () {
await vcxAgent.agentInitVcx()
const issuerDid = vcxAgent.getInstitutionDid()
const proofData = getFaberProofData(issuerDid, proofId)
await vcxAgent.serviceVerifier.createProof({ proofId, proofData })
await vcxAgent.serviceVerifier.createProof(proofId, proofData)
const { state, proofRequestMessage } = await vcxAgent.serviceVerifier.sendProofRequest(connectionId, proofId)
expect(state).toBe(StateType.OfferSent)
await vcxAgent.agentShutdownVcx()
Expand Down

0 comments on commit aafc86f

Please sign in to comment.