diff --git a/examples/cactus-example-discounted-asset-trade/transaction-indy.ts b/examples/cactus-example-discounted-asset-trade/transaction-indy.ts index 28d93c9689..58de50fd1b 100644 --- a/examples/cactus-example-discounted-asset-trade/transaction-indy.ts +++ b/examples/cactus-example-discounted-asset-trade/transaction-indy.ts @@ -74,12 +74,13 @@ function sendRequest( const args = { args: arg_request }; - verifierFactory - .getVerifier("3PfTJw8g") - .sendSyncRequest(contract, method, args) - .then((result) => { - return resolve(result as ISendRequestResultV1>); - }); + const verifier = await verifierFactory.getVerifier( + "3PfTJw8g", + "legacy-socketio", + ); + verifier.sendSyncRequest(contract, method, args).then((result) => { + return resolve(result as ISendRequestResultV1>); + }); } catch (err) { logger.error(err); return reject(err); diff --git a/examples/cactus-example-electricity-trade/package.json b/examples/cactus-example-electricity-trade/package.json index e54e1a232b..d1059a81a5 100644 --- a/examples/cactus-example-electricity-trade/package.json +++ b/examples/cactus-example-electricity-trade/package.json @@ -21,7 +21,6 @@ "@hyperledger/cactus-plugin-keychain-memory": "2.0.0-alpha.2", "@hyperledger/cactus-plugin-ledger-connector-ethereum": "2.0.0-alpha.2", "@hyperledger/cactus-plugin-ledger-connector-sawtooth": "2.0.0-alpha.2", - "@hyperledger/cactus-verifier-client": "2.0.0-alpha.2", "@types/node": "14.18.54", "body-parser": "1.20.2", "cookie-parser": "1.4.6", diff --git a/examples/cactus-example-tcs-huawei/BalanceManagement.ts b/examples/cactus-example-tcs-huawei/BalanceManagement.ts index 4fc12061ed..0834d49aaa 100644 --- a/examples/cactus-example-tcs-huawei/BalanceManagement.ts +++ b/examples/cactus-example-tcs-huawei/BalanceManagement.ts @@ -36,7 +36,7 @@ export class BalanceManagement { } getBalance(account: string): Promise { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { // for LedgerOperation // const execData = {"referedAddress": account}; // const ledgerOperation: LedgerOperation = new LedgerOperation("getNumericBalance", "", execData); @@ -48,8 +48,11 @@ export class BalanceManagement { // const method = "default"; // const args = {"method": {type: "web3Eth", command: "getBalance"}, "args": {"args": [account]}}; - this.verifierFactory - .getVerifier("84jUisrs") + const verifier = await this.verifierFactory.getVerifier( + "84jUisrs", + "legacy-socketio", + ); + verifier .sendSyncRequest(contract, method, args) .then((result) => { const res1 = result as ISendRequestResultV1; diff --git a/examples/cactus-example-tcs-huawei/BusinessLogicTcsElectricityTrade.ts b/examples/cactus-example-tcs-huawei/BusinessLogicTcsElectricityTrade.ts index 5de75beda0..354f7c2c23 100644 --- a/examples/cactus-example-tcs-huawei/BusinessLogicTcsElectricityTrade.ts +++ b/examples/cactus-example-tcs-huawei/BusinessLogicTcsElectricityTrade.ts @@ -81,18 +81,20 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase { const options = { filterKey: config.electricityTradeInfo.tcsHuawei.filterKey, }; - const verifierTcs = routesVerifierFactory.getVerifier( - useValidator["validatorID"][0], - ); - verifierTcs.startMonitor( - "BusinessLogicTcsElectricityTrade", - options, - routesTransactionManagement, - ); + routesVerifierFactory + .getVerifier(useValidator["validatorID"][0]) + .then((verifierTcs) => { + verifierTcs.startMonitor( + "BusinessLogicTcsElectricityTrade", + options, + routesTransactionManagement, + ); + }); + logger.debug("getVerifierTcs"); } - remittanceTransaction(transactionSubset: object) { + async remittanceTransaction(transactionSubset: object) { logger.debug( `called remittanceTransaction(), accountInfo = ${json2str( transactionSubset, @@ -128,7 +130,7 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase { routesTransactionManagement.getValidatorToUse(this.businessLogicID), ); // const verifierEthereum = routesTransactionManagement.getVerifier(useValidator['validatorID'][1]); - const verifierEthereum = routesVerifierFactory.getVerifier( + const verifierEthereum = await routesVerifierFactory.getVerifier( useValidator["validatorID"][1], ); verifierEthereum.startMonitor( diff --git a/examples/cactus-example-tcs-huawei/TransactionEthereum.ts b/examples/cactus-example-tcs-huawei/TransactionEthereum.ts index bc8d44f8bb..df6967d154 100644 --- a/examples/cactus-example-tcs-huawei/TransactionEthereum.ts +++ b/examples/cactus-example-tcs-huawei/TransactionEthereum.ts @@ -98,56 +98,55 @@ function getNewNonce(fromAddress: string): Promise<{ txnCountHex: string }> { const args = { args: { args: [fromAddress] } }; logger.debug(`##getNewNonce(A): call validator#getNonce()`); - xVerifierFactory - .getVerifier("84jUisrs") - .sendSyncRequest(contract, method, args) - .then((result) => { - // logger.debug(`##getNewNonce(A): result: ${JSON.stringify(result)}`); - - const res1 = result as ISendRequestResultV1<{ - readonly nonce: number; - readonly nonceHex: string; - }>; - - let txnCount: number = res1.data.nonce; - let txnCountHex: string = res1.data.nonceHex; - - const latestNonce = getLatestNonce(fromAddress); - // logger.debug(`##getNewNonce(B): fromAddress: ${fromAddress}, txnCount: ${txnCount}, latestNonce: ${latestNonce}`); - if (txnCount <= latestNonce) { - // nonce correction - txnCount = latestNonce + 1; - logger.debug( - `##getNewNonce(C): Adjust txnCount, fromAddress: ${fromAddress}, txnCount: ${txnCount}, latestNonce: ${latestNonce}`, - ); - - const method = { type: "function", command: "toHex" }; - const args = { args: { args: [txnCount] } }; - - logger.debug(`##getNewNonce(D): call validator#toHex()`); - xVerifierFactory - .getVerifier("84jUisrs") - .sendSyncRequest(contract, method, args) - .then((result) => { - const res2 = result as ISendRequestResultV1<{ - readonly hexStr: string; - }>; - txnCountHex = res2.data.hexStr; - logger.debug(`##getNewNonce(E): txnCountHex: ${txnCountHex}`); - - // logger.debug(`##getNewNonce(F) _nonce: ${txnCount}, latestNonce: ${latestNonce}`); - setLatestNonce(fromAddress, txnCount); - - return resolve({ txnCountHex: txnCountHex }); - }); - } else { + const verifier = await this.verifierFactory.getVerifier( + "84jUisrs", + "legacy-socketio", + ); + + verifier.sendSyncRequest(contract, method, args).then((result) => { + // logger.debug(`##getNewNonce(A): result: ${JSON.stringify(result)}`); + + const res1 = result as ISendRequestResultV1<{ + readonly nonce: number; + readonly nonceHex: string; + }>; + + let txnCount: number = res1.data.nonce; + let txnCountHex: string = res1.data.nonceHex; + + const latestNonce = getLatestNonce(fromAddress); + // logger.debug(`##getNewNonce(B): fromAddress: ${fromAddress}, txnCount: ${txnCount}, latestNonce: ${latestNonce}`); + if (txnCount <= latestNonce) { + // nonce correction + txnCount = latestNonce + 1; + logger.debug( + `##getNewNonce(C): Adjust txnCount, fromAddress: ${fromAddress}, txnCount: ${txnCount}, latestNonce: ${latestNonce}`, + ); + + const method = { type: "function", command: "toHex" }; + const args = { args: { args: [txnCount] } }; + + logger.debug(`##getNewNonce(D): call validator#toHex()`); + verifier.sendSyncRequest(contract, method, args).then((result) => { + const res2 = result as ISendRequestResultV1<{ + readonly hexStr: string; + }>; + txnCountHex = res2.data.hexStr; + logger.debug(`##getNewNonce(E): txnCountHex: ${txnCountHex}`); + // logger.debug(`##getNewNonce(F) _nonce: ${txnCount}, latestNonce: ${latestNonce}`); setLatestNonce(fromAddress, txnCount); - logger.debug(`##getNewNonce(G): txnCountHex: ${txnCountHex}`); return resolve({ txnCountHex: txnCountHex }); - } - }); + }); + } else { + // logger.debug(`##getNewNonce(F) _nonce: ${txnCount}, latestNonce: ${latestNonce}`); + setLatestNonce(fromAddress, txnCount); + + logger.debug(`##getNewNonce(G): txnCountHex: ${txnCountHex}`); + return resolve({ txnCountHex: txnCountHex }); + } + }); } catch (err) { logger.error(err); return reject(err); diff --git a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/api-client/verifier-integration-with-besu-connector.test.ts b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/api-client/verifier-integration-with-besu-connector.test.ts index d59459f52a..50afcda617 100644 --- a/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/api-client/verifier-integration-with-besu-connector.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/api-client/verifier-integration-with-besu-connector.test.ts @@ -128,7 +128,7 @@ describe("Verifier integration with besu connector tests", () => { }); const listenOptions: IListenOptions = { - hostname: "localhost", + hostname: "127.0.0.1", port: 0, server, }; diff --git a/packages/cactus-test-plugin-ledger-connector-ethereum/src/test/typescript/integration/api-client/verifier-integration-with-ethereum-connector.test.ts b/packages/cactus-test-plugin-ledger-connector-ethereum/src/test/typescript/integration/api-client/verifier-integration-with-ethereum-connector.test.ts index 37cfbf3170..01e78c8a11 100644 --- a/packages/cactus-test-plugin-ledger-connector-ethereum/src/test/typescript/integration/api-client/verifier-integration-with-ethereum-connector.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-ethereum/src/test/typescript/integration/api-client/verifier-integration-with-ethereum-connector.test.ts @@ -179,14 +179,14 @@ describe("Verifier integration with ethereum connector tests", () => { // Helper Functions ////////////////////////////////// - function monitorAndGetBlock( + async function monitorAndGetBlock( options: Record = {}, ): Promise> { + const appId = "testMonitor"; + const sut = await globalVerifierFactory.getVerifier(ethereumValidatorId); + return new Promise>( (resolve, reject) => { - const appId = "testMonitor"; - const sut = globalVerifierFactory.getVerifier(ethereumValidatorId); - const monitor: IVerifierEventListener = { onEvent(ledgerEvent: LedgerEvent): void { try { @@ -222,8 +222,8 @@ describe("Verifier integration with ethereum connector tests", () => { // Tests ////////////////////////////////// - test("Verifier of EthereumApiClient is created by VerifierFactory", () => { - const sut = globalVerifierFactory.getVerifier(ethereumValidatorId); + test("Verifier of EthereumApiClient is created by VerifierFactory", async () => { + const sut = await globalVerifierFactory.getVerifier(ethereumValidatorId); expect(sut.ledgerApi.className).toEqual("EthereumApiClient"); }); @@ -236,7 +236,7 @@ describe("Verifier integration with ethereum connector tests", () => { beforeAll(async () => { // Setup verifier - verifier = globalVerifierFactory.getVerifier( + verifier = await globalVerifierFactory.getVerifier( ethereumValidatorId, "ETH_1X", ); @@ -508,9 +508,13 @@ describe("Verifier integration with ethereum connector tests", () => { const method = { type: "web3Eth", command: "getBalance" }; const args = { args: [WHALE_ACCOUNT_ADDRESS] }; - const results = (await globalVerifierFactory - .getVerifier(ethereumValidatorId) - .sendSyncRequest(contract, method, args)) as ISendRequestResultV1; + const verifier = + await globalVerifierFactory.getVerifier(ethereumValidatorId); + const results = (await verifier.sendSyncRequest( + contract, + method, + args, + )) as ISendRequestResultV1; expect(results.status).toEqual(200); expect(results.data.length).toBeGreaterThan(0); }); @@ -525,7 +529,8 @@ describe("Verifier integration with ethereum connector tests", () => { const correctArgs: any = { args: [WHALE_ACCOUNT_ADDRESS], }; - const verifier = globalVerifierFactory.getVerifier(ethereumValidatorId); + const verifier = + await globalVerifierFactory.getVerifier(ethereumValidatorId); // Sanity check if correct parameters work const resultCorrect = (await verifier.sendSyncRequest( @@ -565,9 +570,9 @@ describe("Verifier integration with ethereum connector tests", () => { const args = {}; try { - await globalVerifierFactory - .getVerifier(ethereumValidatorId) - .sendSyncRequest(contract, method, args); + const verifier = + await globalVerifierFactory.getVerifier(ethereumValidatorId); + await verifier.sendSyncRequest(contract, method, args); fail("Expected sendSyncRequest call to fail but it succeeded."); } catch (error) { console.log("sendSyncRequest failed as expected"); diff --git a/packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/api-client/verifier-integration-with-quorum-connector.test.ts b/packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/api-client/verifier-integration-with-quorum-connector.test.ts index bf8a063c84..5477f16665 100644 --- a/packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/api-client/verifier-integration-with-quorum-connector.test.ts +++ b/packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/api-client/verifier-integration-with-quorum-connector.test.ts @@ -198,14 +198,14 @@ describe("Verifier integration with quorum connector tests", () => { // Helper Functions ////////////////////////////////// - function monitorAndGetBlock( + async function monitorAndGetBlock( options: Record = {}, ): Promise> { + const appId = "testMonitor"; + const sut = await globalVerifierFactory.getVerifier(quorumValidatorId); + return new Promise>( (resolve, reject) => { - const appId = "testMonitor"; - const sut = globalVerifierFactory.getVerifier(quorumValidatorId); - const monitor: IVerifierEventListener = { onEvent(ledgerEvent: LedgerEvent): void { try { @@ -241,8 +241,8 @@ describe("Verifier integration with quorum connector tests", () => { // Tests ////////////////////////////////// - test("Verifier of QuorumApiClient is created by VerifierFactory", () => { - const sut = globalVerifierFactory.getVerifier(quorumValidatorId); + test("Verifier of QuorumApiClient is created by VerifierFactory", async () => { + const sut = await globalVerifierFactory.getVerifier(quorumValidatorId); expect(sut.ledgerApi.className).toEqual("QuorumApiClient"); }); @@ -255,7 +255,7 @@ describe("Verifier integration with quorum connector tests", () => { beforeAll(async () => { // Setup verifier - verifier = globalVerifierFactory.getVerifier( + verifier = await globalVerifierFactory.getVerifier( quorumValidatorId, "QUORUM_2X", ); @@ -543,11 +543,12 @@ describe("Verifier integration with quorum connector tests", () => { const method = { type: "web3Eth", command: "getBalance" }; const args = { args: [connectionProfile.quorum.member2.accountAddress] }; - const results = (await globalVerifierFactory - .getVerifier(quorumValidatorId) - .sendSyncRequest(contract, method, args)) as ISendRequestResultV1< - Array - >; + const verifier = await globalVerifierFactory.getVerifier(quorumValidatorId); + const results = (await verifier.sendSyncRequest( + contract, + method, + args, + )) as ISendRequestResultV1>; expect(results.status).toEqual(200); expect(results.data.length).toBeGreaterThan(0); }); @@ -562,7 +563,7 @@ describe("Verifier integration with quorum connector tests", () => { const correctArgs: Record = { args: [connectionProfile.quorum.member2.accountAddress], }; - const verifier = globalVerifierFactory.getVerifier(quorumValidatorId); + const verifier = await globalVerifierFactory.getVerifier(quorumValidatorId); // Sanity check if correct parameters work const resultCorrect = (await verifier.sendSyncRequest( @@ -601,13 +602,12 @@ describe("Verifier integration with quorum connector tests", () => { const method = { type: "web3Eth", command: "foo" }; const args = {}; - const results = (await globalVerifierFactory - .getVerifier(quorumValidatorId) - .sendSyncRequest( - contract, - method, - args, - )) as ISendRequestResultV1; + const verifier = await globalVerifierFactory.getVerifier(quorumValidatorId); + const results = (await verifier.sendSyncRequest( + contract, + method, + args, + )) as ISendRequestResultV1; expect(results).toBeTruthy(); expect(results.status).toEqual(504); diff --git a/packages/cactus-test-verifier-client/src/main/typescript/verifier-with-go-eth-stress-check.ts b/packages/cactus-test-verifier-client/src/main/typescript/verifier-with-go-eth-stress-check.ts index cd4a575809..cff6be76ce 100644 --- a/packages/cactus-test-verifier-client/src/main/typescript/verifier-with-go-eth-stress-check.ts +++ b/packages/cactus-test-verifier-client/src/main/typescript/verifier-with-go-eth-stress-check.ts @@ -139,7 +139,7 @@ async function setupEnvironment(): Promise { sutLogLevel, ); - verifier = verifierFactory.getVerifier(ethereumValidatorId, "ETH_1X"); + verifier = await verifierFactory.getVerifier(ethereumValidatorId, "ETH_1X"); // Clear the stress log file writeFileSync(STRESS_LOG_FILENAME, ""); diff --git a/packages/cactus-verifier-client/README.md b/packages/cactus-verifier-client/README.md index c4a514d724..77c8fd862b 100644 --- a/packages/cactus-verifier-client/README.md +++ b/packages/cactus-verifier-client/README.md @@ -20,6 +20,8 @@ This package provides `Verifier` and `VerifierFactory` components that can be us ## VerifierFactory - Used to create single verifier per ledger based on pre-defined configuration. - See [verifier-factory.test.ts](../cactus-verifier-client/src/test/typescript/unit/verifier-factory.test.ts) for unit tests. +- **In order to use `VerifierFactory` or `getValidatorApiClient` you must manually install the connector package that provides given ledger ApiClient!** + - Example: if your project uses ethereum and corda validators, you must install `cactus-plugin-ledger-connector-ethereum` and `cactus-plugin-ledger-connector-corda `. See table above for validator to package mapping. ### Usage ``` typescript @@ -53,7 +55,7 @@ const verifierFactory = new VerifierFactory(ledgerPluginInfo); // Get ApiClient to validator with ID "myBesuValidatorId" // Second argument will determine type of returned Verifier (BesuApiClient in this case) -const myBesu: Verifier = sut.getVerifier("myBesuValidatorId", "BESU_1X")) +const myBesu: Verifier = await sut.getVerifier("myBesuValidatorId", "BESU_1X")) // Second argument can be ignored for backward-compatibility // It will return Verifier<(union of all supported ApiClients)> diff --git a/packages/cactus-verifier-client/package.json b/packages/cactus-verifier-client/package.json index 8fef9ba4e8..bef5a4ccd3 100644 --- a/packages/cactus-verifier-client/package.json +++ b/packages/cactus-verifier-client/package.json @@ -42,9 +42,11 @@ "build": "tsc" }, "dependencies": { - "@hyperledger/cactus-api-client": "2.0.0-alpha.2", "@hyperledger/cactus-common": "2.0.0-alpha.2", - "@hyperledger/cactus-core-api": "2.0.0-alpha.2", + "@hyperledger/cactus-core-api": "2.0.0-alpha.2" + }, + "devDependencies": { + "@hyperledger/cactus-api-client": "2.0.0-alpha.2", "@hyperledger/cactus-plugin-ledger-connector-besu": "2.0.0-alpha.2", "@hyperledger/cactus-plugin-ledger-connector-corda": "2.0.0-alpha.2", "@hyperledger/cactus-plugin-ledger-connector-ethereum": "2.0.0-alpha.2", @@ -56,9 +58,46 @@ "jest-extended": "4.0.1", "rxjs": "7.8.1" }, + "peerDependencies": { + "@hyperledger/cactus-api-client": "2.x", + "@hyperledger/cactus-plugin-ledger-connector-besu": "2.x", + "@hyperledger/cactus-plugin-ledger-connector-corda": "2.x", + "@hyperledger/cactus-plugin-ledger-connector-ethereum": "2.x", + "@hyperledger/cactus-plugin-ledger-connector-fabric": "2.x", + "@hyperledger/cactus-plugin-ledger-connector-iroha": "2.x", + "@hyperledger/cactus-plugin-ledger-connector-iroha2": "2.x", + "@hyperledger/cactus-plugin-ledger-connector-quorum": "2.x", + "@hyperledger/cactus-plugin-ledger-connector-sawtooth": "2.x" + }, + "peerDependenciesMeta": { + "@hyperledger/cactus-plugin-ledger-connector-besu": { + "optional": true + }, + "@hyperledger/cactus-plugin-ledger-connector-corda": { + "optional": true + }, + "@hyperledger/cactus-plugin-ledger-connector-ethereum": { + "optional": true + }, + "@hyperledger/cactus-plugin-ledger-connector-fabric": { + "optional": true + }, + "@hyperledger/cactus-plugin-ledger-connector-iroha": { + "optional": true + }, + "@hyperledger/cactus-plugin-ledger-connector-iroha2": { + "optional": true + }, + "@hyperledger/cactus-plugin-ledger-connector-quorum": { + "optional": true + }, + "@hyperledger/cactus-plugin-ledger-connector-sawtooth": { + "optional": true + } + }, "engines": { "node": ">=10", - "npm": ">=6" + "npm": ">=8" }, "publishConfig": { "access": "public" diff --git a/packages/cactus-verifier-client/src/main/typescript/get-validator-api-client.ts b/packages/cactus-verifier-client/src/main/typescript/get-validator-api-client.ts index 673d91e58a..0c356a6f31 100644 --- a/packages/cactus-verifier-client/src/main/typescript/get-validator-api-client.ts +++ b/packages/cactus-verifier-client/src/main/typescript/get-validator-api-client.ts @@ -1,51 +1,43 @@ /* - * Copyright 2020-2022 Hyperledger Cactus Contributors + * Copyright 2020-2023 Hyperledger Cactus Contributors * SPDX-License-Identifier: Apache-2.0 * * get-validator-api-client.ts */ -import { +import type { SocketIOApiClient, SocketIOApiClientOptions, } from "@hyperledger/cactus-api-client"; - -import { +import type { BesuApiClient, BesuApiClientOptions, } from "@hyperledger/cactus-plugin-ledger-connector-besu"; - -import { +import type { QuorumApiClient, QuorumApiClientOptions, } from "@hyperledger/cactus-plugin-ledger-connector-quorum"; - -import { +import type { EthereumApiClient, EthereumApiClientOptions, } from "@hyperledger/cactus-plugin-ledger-connector-ethereum"; - -import { +import type { CordaApiClient, CordaApiClientOptions, } from "@hyperledger/cactus-plugin-ledger-connector-corda"; - -import { +import type { IrohaApiClient, IrohaApiClientOptions, } from "@hyperledger/cactus-plugin-ledger-connector-iroha"; - -import { +import type { Iroha2ApiClient, Iroha2ApiClientOptions, } from "@hyperledger/cactus-plugin-ledger-connector-iroha2"; - -import { +import type { FabricApiClient, FabricApiClientOptions, } from "@hyperledger/cactus-plugin-ledger-connector-fabric"; - -import { +import type { SawtoothApiClient, SawtoothApiClientOptions, } from "@hyperledger/cactus-plugin-ledger-connector-sawtooth"; @@ -105,33 +97,53 @@ export type ClientApiConfig = { * @param validatorType: what kind of validator to create, must be defined in ClientApiConfig. * @param options: Configuration for given ApiClients, depends on validatorType supplied earlier * @returns Api coresponding to validatorType requested in arguments. - * - * @todo Use dynamic import to save space and not require all the ApiClient packages. */ -export function getValidatorApiClient( +export async function getValidatorApiClient( validatorType: K, options: ClientApiConfig[K]["in"], -): ClientApiConfig[K]["out"] { +): Promise { switch (validatorType) { case "legacy-socketio": - return new SocketIOApiClient(options as SocketIOApiClientOptions); + // TODO - replace with dynamic imports once ESM is supported + const apiClientPackage = require("@hyperledger/cactus-api-client"); + return new apiClientPackage.SocketIOApiClient( + options as SocketIOApiClientOptions, + ); case "BESU_1X": case "BESU_2X": - return new BesuApiClient(options as BesuApiClientOptions); + const besuPackage = require("@hyperledger/cactus-plugin-ledger-connector-besu"); + return new besuPackage.BesuApiClient(options as BesuApiClientOptions); case "QUORUM_2X": - return new QuorumApiClient(options as QuorumApiClientOptions); + const quorumPackage = require("@hyperledger/cactus-plugin-ledger-connector-quorum"); + return new quorumPackage.QuorumApiClient( + options as QuorumApiClientOptions, + ); case "ETH_1X": - return new EthereumApiClient(options as EthereumApiClientOptions); + const ethereumPackage = require("@hyperledger/cactus-plugin-ledger-connector-ethereum"); + return new ethereumPackage.EthereumApiClient( + options as EthereumApiClientOptions, + ); case "CORDA_4X": - return new CordaApiClient(options as CordaApiClientOptions); + const cordaPackage = require("@hyperledger/cactus-plugin-ledger-connector-corda"); + return new cordaPackage.CordaApiClient(options as CordaApiClientOptions); case "IROHA_1X": - return new IrohaApiClient(options as IrohaApiClientOptions); + const irohaPackage = require("@hyperledger/cactus-plugin-ledger-connector-iroha"); + return new irohaPackage.IrohaApiClient(options as IrohaApiClientOptions); case "IROHA_2X": - return new Iroha2ApiClient(options as CordaApiClientOptions); + const iroha2Package = require("@hyperledger/cactus-plugin-ledger-connector-iroha2"); + return new iroha2Package.Iroha2ApiClient( + options as Iroha2ApiClientOptions, + ); case "FABRIC_2X": - return new FabricApiClient(options as FabricApiClientOptions); + const fabricPackage = require("@hyperledger/cactus-plugin-ledger-connector-fabric"); + return new fabricPackage.FabricApiClient( + options as FabricApiClientOptions, + ); case "SAWTOOTH_1X": - return new SawtoothApiClient(options as SawtoothApiClientOptions); + const sawtoothPackage = require("@hyperledger/cactus-plugin-ledger-connector-sawtooth"); + return new sawtoothPackage.SawtoothApiClient( + options as SawtoothApiClientOptions, + ); default: // Will not compile if any ClientApiConfig key was not handled by this switch const _: never = validatorType; diff --git a/packages/cactus-verifier-client/src/main/typescript/verifier-factory.ts b/packages/cactus-verifier-client/src/main/typescript/verifier-factory.ts index cfb6dc2e6c..3be55f2edf 100644 --- a/packages/cactus-verifier-client/src/main/typescript/verifier-factory.ts +++ b/packages/cactus-verifier-client/src/main/typescript/verifier-factory.ts @@ -1,5 +1,5 @@ /* - * Copyright 2020-2022 Hyperledger Cactus Contributors + * Copyright 2020-2023 Hyperledger Cactus Contributors * SPDX-License-Identifier: Apache-2.0 * * verifier-factory.ts @@ -133,10 +133,10 @@ export class VerifierFactory { * @param type: optional parameter, will determine the return type. * @returns Verifier or Verifier if type argument was not provided. */ - getVerifier( + async getVerifier( validatorId: string, type?: K, - ): Verifier { + ): Promise> { const validatorConfig = this.getValidatorConfigEntryOrThrow(validatorId); // Assert ClientApi types @@ -156,7 +156,7 @@ export class VerifierFactory { } else { this.log.info(`No Verifier for Validator ${validatorId} - create new.`); - const clientApi = getValidatorApiClient( + const clientApi = await getValidatorApiClient( validatorConfig.validatorType, validatorConfig as unknown as ClientApiConfig[K]["in"], ); diff --git a/packages/cactus-verifier-client/src/main/typescript/verifier.ts b/packages/cactus-verifier-client/src/main/typescript/verifier.ts index 3b1aefa5a6..07a78507d5 100644 --- a/packages/cactus-verifier-client/src/main/typescript/verifier.ts +++ b/packages/cactus-verifier-client/src/main/typescript/verifier.ts @@ -1,18 +1,17 @@ /* - * Copyright 2020-2021 Hyperledger Cactus Contributors + * Copyright 2020-2023 Hyperledger Cactus Contributors * SPDX-License-Identifier: Apache-2.0 * - * Verifier.ts + * verifier.ts */ -import { Subscription } from "rxjs"; +import type { Subscription } from "rxjs"; import { Logger, LogLevelDesc, LoggerProvider, } from "@hyperledger/cactus-common"; - import { ISocketApiClient, IVerifier, @@ -34,9 +33,6 @@ type BlockTypeFromSocketApi = T extends ISocketApiClient /** * Extends ledger connector ApiClient with additional monitoring methods (using callbacks, instead of reactive). * - * @remarks - * Migrated from cmd-socketio-server for merging the codebases. - * * @todo Don't throw exception for not supported operations, don't include these methods at all (if possible) */ export class Verifier> diff --git a/packages/cactus-verifier-client/src/test/typescript/unit/get-validator-api-client.test.ts b/packages/cactus-verifier-client/src/test/typescript/unit/get-validator-api-client.test.ts index 77fde4e9a6..4293eb0835 100644 --- a/packages/cactus-verifier-client/src/test/typescript/unit/get-validator-api-client.test.ts +++ b/packages/cactus-verifier-client/src/test/typescript/unit/get-validator-api-client.test.ts @@ -4,24 +4,17 @@ import "jest-extended"; import { getValidatorApiClient } from "../../../main/typescript/get-validator-api-client"; -import { - SocketIOApiClient, - SocketIOApiClientOptions, -} from "@hyperledger/cactus-api-client"; +import { SocketIOApiClientOptions } from "@hyperledger/cactus-api-client"; +import { BesuApiClientOptions } from "@hyperledger/cactus-plugin-ledger-connector-besu"; -import { - BesuApiClient, - BesuApiClientOptions, -} from "@hyperledger/cactus-plugin-ledger-connector-besu"; - -test("Create legacy socketio client", () => { +test("Create legacy socketio client", async () => { const clientOptions: SocketIOApiClientOptions = { validatorID: "someValId", validatorURL: "invalid-url123asd", validatorKeyValue: "xxxxxxxxxxxxx", }; - const clientApi: SocketIOApiClient = getValidatorApiClient( + const clientApi = await getValidatorApiClient( "legacy-socketio", clientOptions, ); @@ -33,24 +26,16 @@ test("Create legacy socketio client", () => { clientApi.close(); }); -test("Create besu client", () => { +test("Create besu client", async () => { const clientOptions = new BesuApiClientOptions({ basePath: "foo" }); // BESU 1 - const clientApi1: BesuApiClient = getValidatorApiClient( - "BESU_1X", - clientOptions, - ); - + const clientApi1 = await getValidatorApiClient("BESU_1X", clientOptions); expect(clientApi1.className).toEqual("BesuApiClient"); expect(clientApi1.options).toEqual(clientOptions); // BESU 2 - const clientApi2: BesuApiClient = getValidatorApiClient( - "BESU_2X", - clientOptions, - ); - + const clientApi2 = await getValidatorApiClient("BESU_2X", clientOptions); expect(clientApi2.className).toEqual("BesuApiClient"); expect(clientApi2.options).toEqual(clientOptions); }); diff --git a/packages/cactus-verifier-client/src/test/typescript/unit/verifier-factory.test.ts b/packages/cactus-verifier-client/src/test/typescript/unit/verifier-factory.test.ts index 6d85b4626c..1a1c8d37cf 100644 --- a/packages/cactus-verifier-client/src/test/typescript/unit/verifier-factory.test.ts +++ b/packages/cactus-verifier-client/src/test/typescript/unit/verifier-factory.test.ts @@ -8,7 +8,6 @@ import { VerifierFactory, VerifierFactoryConfig, } from "../../../main/typescript/verifier-factory"; -import { BesuApiClient } from "@hyperledger/cactus-plugin-ledger-connector-besu"; describe("Constructor Tests", () => { test("Basic construction", () => { @@ -100,27 +99,41 @@ describe("getVerifier Tests", () => { sut = new VerifierFactory(ledgerPluginInfo); }); - test("Throws when requesting validator not defined in config", () => { - expect(() => sut.getVerifier("missingValidatorId")).toThrow(); + test("Throws when requesting validator not defined in config", async () => { + try { + await sut.getVerifier("missingValidatorId"); + expect(1).toBe("getVerifier with invalid Id should throw an error!"); + } catch (error) { + console.log("getVerifier with invalid Id throw error as expected."); + } }); - test("Throws when requested client type differs from configured type", () => { - expect(() => - sut.getVerifier("myBesuValidatorId", "legacy-socketio"), - ).toThrow(); + test("Throws when requested client type differs from configured type", async () => { + try { + await sut.getVerifier("myBesuValidatorId", "legacy-socketio"); + expect(1).toBe( + "getVerifier with invalid verifier type should throw an error!", + ); + } catch (error) { + console.log("getVerifier with invalid type throw error as expected."); + } // even though the same clientApi is used for both BESU_1X and BESU_2X this should fail // client code should not depend on internal implementation detail. - expect(() => sut.getVerifier("myBesuValidatorId", "BESU_1X")).toThrow(); + try { + await sut.getVerifier("myBesuValidatorId", "BESU_1X"); + expect(1).toBe( + "getVerifier with invalid verifier type should throw an error!", + ); + } catch (error) { + console.log("getVerifier with invalid type throw error as expected."); + } }); - test("Creates a legacy socketio client", () => { + test("Creates a legacy socketio client", async () => { const validatorId = "mySocketSawtoothValidatorId"; - const client: Verifier = sut.getVerifier( - validatorId, - "legacy-socketio", - ); + const client = await sut.getVerifier(validatorId, "legacy-socketio"); expect(client.verifierID).toEqual(validatorId); expect(client.ledgerApi.className).toEqual("SocketIOApiClient"); @@ -129,25 +142,22 @@ describe("getVerifier Tests", () => { client.ledgerApi.close(); }); - test("Creates a open-api based client", () => { + test("Creates a open-api based client", async () => { const validatorId = "myBesuValidatorId"; - const client: Verifier = sut.getVerifier( - validatorId, - "BESU_2X", - ); + const client = await sut.getVerifier(validatorId, "BESU_2X"); expect(client.verifierID).toEqual(validatorId); expect(client.ledgerApi.className).toEqual("BesuApiClient"); expect(client.ledgerApi.options.basePath).toEqual("myBesuPath"); }); - test("Creates correct api client without explicit type specification", () => { + test("Creates correct api client without explicit type specification", async () => { const validatorId = "mySocketSawtoothValidatorId"; - const client: Verifier = sut.getVerifier( + const client = (await sut.getVerifier( validatorId, - ) as Verifier; + )) as Verifier; expect(client.verifierID).toEqual(validatorId); expect(client.ledgerApi.className).toEqual("SocketIOApiClient"); @@ -156,20 +166,14 @@ describe("getVerifier Tests", () => { client.ledgerApi.close(); }); - test("Factory reuses already created verifiers", () => { + test("Factory reuses already created verifiers", async () => { const validatorId = "mySocketSawtoothValidatorId"; - const client: Verifier = sut.getVerifier( - validatorId, - "legacy-socketio", - ); + const client = await sut.getVerifier(validatorId, "legacy-socketio"); expect(client.verifierID).toEqual(validatorId); expect(sut["verifierMap"].size).toBe(1); - const anotherClient: Verifier = sut.getVerifier( - validatorId, - "legacy-socketio", - ); + const anotherClient = await sut.getVerifier(validatorId, "legacy-socketio"); expect(anotherClient.verifierID).toEqual(validatorId); expect(sut["verifierMap"].size).toBe(1); // No new verifier added diff --git a/yarn.lock b/yarn.lock index 634645b8e4..b708462a44 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7029,7 +7029,6 @@ __metadata: "@hyperledger/cactus-plugin-keychain-memory": 2.0.0-alpha.2 "@hyperledger/cactus-plugin-ledger-connector-ethereum": 2.0.0-alpha.2 "@hyperledger/cactus-plugin-ledger-connector-sawtooth": 2.0.0-alpha.2 - "@hyperledger/cactus-verifier-client": 2.0.0-alpha.2 "@types/escape-html": 1.0.1 "@types/express": 4.17.19 "@types/node": 14.18.54 @@ -8285,6 +8284,33 @@ __metadata: "@hyperledger/cactus-plugin-ledger-connector-sawtooth": 2.0.0-alpha.2 jest-extended: 4.0.1 rxjs: 7.8.1 + peerDependencies: + "@hyperledger/cactus-api-client": 2.x + "@hyperledger/cactus-plugin-ledger-connector-besu": 2.x + "@hyperledger/cactus-plugin-ledger-connector-corda": 2.x + "@hyperledger/cactus-plugin-ledger-connector-ethereum": 2.x + "@hyperledger/cactus-plugin-ledger-connector-fabric": 2.x + "@hyperledger/cactus-plugin-ledger-connector-iroha": 2.x + "@hyperledger/cactus-plugin-ledger-connector-iroha2": 2.x + "@hyperledger/cactus-plugin-ledger-connector-quorum": 2.x + "@hyperledger/cactus-plugin-ledger-connector-sawtooth": 2.x + peerDependenciesMeta: + "@hyperledger/cactus-plugin-ledger-connector-besu": + optional: true + "@hyperledger/cactus-plugin-ledger-connector-corda": + optional: true + "@hyperledger/cactus-plugin-ledger-connector-ethereum": + optional: true + "@hyperledger/cactus-plugin-ledger-connector-fabric": + optional: true + "@hyperledger/cactus-plugin-ledger-connector-iroha": + optional: true + "@hyperledger/cactus-plugin-ledger-connector-iroha2": + optional: true + "@hyperledger/cactus-plugin-ledger-connector-quorum": + optional: true + "@hyperledger/cactus-plugin-ledger-connector-sawtooth": + optional: true languageName: unknown linkType: soft