Skip to content

Commit

Permalink
Obtain DID and verkey from agency instead of hardcoding them in creat…
Browse files Browse the repository at this point in the history
…eVcxAgent (#500)

Signed-off-by: Miroslav Kovar <[email protected]>

Co-authored-by: Miroslav Kovar <[email protected]>
  • Loading branch information
mirgee and mirgee authored Jun 20, 2022
1 parent 2494bb5 commit 5469f22
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
5 changes: 3 additions & 2 deletions agents/node/vcxagent-core/src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ const {
shutdownVcx
} = require('@hyperledger/node-vcx-wrapper')
const { createStorageService } = require('./storage/storage-service')
const { waitUntilAgencyIsReady } = require('./common')
const { waitUntilAgencyIsReady, getAgencyConfig } = require('./common')

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

await waitUntilAgencyIsReady(agencyUrl, logger)
const agencyConfig = await getAgencyConfig(agencyUrl, logger)

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

async function getAgencyConfig (agencyUrl, logger) {
let agencyDid, agencyVerkey
const agencyInfoPath = `${agencyUrl}/agency`
logger.info(`Obtaining agency DID and verkey info from ${agencyInfoPath}`)
try {
const { data } = await axios.get(agencyInfoPath)
agencyDid = data.DID
agencyVerkey = data.verKey
if (!agencyDid || !agencyVerkey) {
throw Error(`Agency returned unexpected DID and verkey format: ${JSON.stringify(data)}`)
}
} catch (err) {
logger.warn(`Failed to obtain DID and verkey from agency with error ${err}. Defaults will be used.`)
agencyDid = 'VsKV7grR1BUE29mG2Fm2kX'
agencyVerkey = 'Hezce2UWMZ3wUhVkh2LfKSs8nDzWwzs2Win7EzNN3YaR'
}
return {
agency_endpoint: agencyUrl,
agency_did: agencyDid,
agency_verkey: agencyVerkey
}
}

async function pollFunction (fn, actionDescription, logger, attemptsThreshold = 10, timeoutMs = 2000) {
let { result, isFinished } = await fn()
let attempts = 1
Expand Down Expand Up @@ -68,3 +91,4 @@ function getRandomInt (min, max) {
module.exports.waitUntilAgencyIsReady = waitUntilAgencyIsReady
module.exports.pollFunction = pollFunction
module.exports.getSampleSchemaData = getSampleSchemaData
module.exports.getAgencyConfig = getAgencyConfig
13 changes: 4 additions & 9 deletions agents/node/vcxagent-core/src/utils/vcx-workflows.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
createWallet, openMainWallet, closeMainWallet,
configureIssuerWallet
} = require('@hyperledger/node-vcx-wrapper')
const axios = require('axios')

async function initRustApiAndLogger (logLevel) {
const rustApi = initRustAPI()
Expand All @@ -18,16 +19,16 @@ async function initRustapi (logLevel = 'vcx=error', num_threads = 4) {
await initThreadpool({ num_threads })
}

async function provisionAgentInAgency (agentName, genesisPath, agencyUrl, seed, walletExtraConfigs, logger) {
async function provisionAgentInAgency (agentName, genesisPath, agencyConfig, seed, walletExtraConfigs, logger) {
logger.info('Provisioning cloud agent')
if (!agentName) {
throw Error('agentName not specified')
}
if (!genesisPath) {
throw Error('genesisPath not specified')
}
if (!agencyUrl) {
throw Error('agencyUrl not specified')
if (!agencyConfig) {
throw Error('agencyConfig not specified')
}
if (!seed) {
throw Error('seed not specified')
Expand All @@ -49,12 +50,6 @@ async function provisionAgentInAgency (agentName, genesisPath, agencyUrl, seed,
}
logger.info(`Using wallet config ${JSON.stringify(walletConfig)}`)

let agencyConfig = {
agency_endpoint: agencyUrl,
agency_did: 'VsKV7grR1BUE29mG2Fm2kX',
agency_verkey: 'Hezce2UWMZ3wUhVkh2LfKSs8nDzWwzs2Win7EzNN3YaR'
}

logger.debug(`Creating wallet with config: ${JSON.stringify(walletConfig, null, 2)}`)
await createWallet(walletConfig)
logger.debug(`Opening wallet with config: ${JSON.stringify(walletConfig, null, 2)}`)
Expand Down
4 changes: 2 additions & 2 deletions wrappers/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5469f22

Please sign in to comment.