diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c87b514455..51f2b1d165 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -781,9 +781,7 @@ jobs: FULL_BUILD_DISABLED: true JEST_TEST_PATTERN: extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/(unit|integration|benchmark)/.*/*.test.ts JEST_TEST_RUNNER_DISABLED: false - TAPE_TEST_PATTERN: >- - --files={./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/counterparty-htlc-endpoint.test.ts,./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/own-htlc-endpoint.test.ts,./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/refund.test.ts,./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/withdraw-counterparty-endpoint.test.ts} - TAPE_TEST_RUNNER_DISABLED: false + TAPE_TEST_RUNNER_DISABLED: true needs: build-dev runs-on: ubuntu-22.04 steps: diff --git a/.taprc b/.taprc index 024e28e54d..e914ecfb0b 100644 --- a/.taprc +++ b/.taprc @@ -16,10 +16,6 @@ files: - ./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v2.3.0-deploy-contract-from-json-json-object.test.ts - ./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/openapi/openapi-validation.test.ts - ./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v21.4.1-invoke-contract-json-object.test.ts - - ./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/counterparty-htlc-endpoint.test.ts - - ./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/own-htlc-endpoint.test.ts - - ./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/withdraw-counterparty-endpoint.test.ts - - ./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/refund.test.ts - ./packages/cactus-plugin-keychain-memory-wasm/src/test/typescript/unit/plugin-keychain-memory-wasm.test.ts - ./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/add-orgs.test.ts - ./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/obtain-profiles.test.ts diff --git a/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/counterparty-htlc-endpoint.test.ts b/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/counterparty-htlc-endpoint.test.ts index 2815325d73..bc582afa19 100644 --- a/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/counterparty-htlc-endpoint.test.ts +++ b/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/counterparty-htlc-endpoint.test.ts @@ -1,6 +1,5 @@ -import test, { Test } from "tape-promise/tape"; +import "jest-extended"; import { v4 as uuidv4 } from "uuid"; -import { AddressInfo } from "net"; import express from "express"; import bodyParser from "body-parser"; import http from "http"; @@ -13,10 +12,12 @@ import { OwnHTLCRequest, CounterpartyHTLCRequest, Configuration, + PluginHTLCCoordinatorBesu, } from "../../../../main/typescript/public-api"; import { IPluginHtlcEthBesuErc20Options, PluginFactoryHtlcEthBesuErc20, + PluginHtlcEthBesuErc20, } from "@hyperledger/cactus-plugin-htlc-eth-besu-erc20"; import { DefaultApi as BesuApi, @@ -42,43 +43,27 @@ import HashTimeLockJSON from "@hyperledger/cactus-plugin-htlc-eth-besu-erc20/src import TestTokenJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/Test_Token.json"; import DemoHelperJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/DemoHelpers.json"; -const logLevel: LogLevelDesc = "INFO"; -const estimatedGas = 6721975; -const expiration = 2147483648; -const receiver = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57"; -const hashLock = - "0x3c335ba7f06a8b01d0596589f73c19069e21c81e5013b91f408165d1bf623d32"; -const firstHighNetWorthAccount = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; -const privateKey = - "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"; -const connectorInstanceId = uuidv4(); -const web3SigningCredential: Web3SigningCredential = { - ethAccount: firstHighNetWorthAccount, - secret: privateKey, - type: Web3SigningCredentialType.PrivateKeyHex, -} as Web3SigningCredential; -const contractAddress = "0xCfEB869F69431e42cdB54A4F4f105C19C080A601"; - -const testCase = "Test own htlc endpoint"; - -test("BEFORE " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning did not throw OK"); - t.end(); -}); +describe("HTLC Coordinator Besu", () => { + const logLevel: LogLevelDesc = "INFO"; + const estimatedGas = 6721975; + const expiration = 2147483648; + const receiver = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57"; + const hashLock = + "0x3c335ba7f06a8b01d0596589f73c19069e21c81e5013b91f408165d1bf623d32"; + const privateKey = + "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"; -test(testCase, async (t: Test) => { - t.comment("Starting Besu Test Ledger"); - const besuTestLedger = new BesuTestLedger(); - await besuTestLedger.start(); + const firstHighNetWorthAccount = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; + const contractAddress = "0xCfEB869F69431e42cdB54A4F4f105C19C080A601"; - test.onFinish(async () => { - await besuTestLedger.stop(); - await besuTestLedger.destroy(); - }); + const connectorInstanceId = uuidv4(); + const pluginRegistry = new PluginRegistry({}); + const web3SigningCredential: Web3SigningCredential = { + ethAccount: firstHighNetWorthAccount, + secret: privateKey, + type: Web3SigningCredentialType.PrivateKeyHex, + } as Web3SigningCredential; - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); const keychainId = uuidv4(); const keychainPlugin = new PluginKeychainMemory({ instanceId: uuidv4(), @@ -91,222 +76,219 @@ test(testCase, async (t: Test) => { ]), logLevel, }); - await keychainPlugin.set( - DemoHelperJSON.contractName, - JSON.stringify(DemoHelperJSON), - ); - await keychainPlugin.set( - HashTimeLockJSON.contractName, - JSON.stringify(HashTimeLockJSON), - ); - - const factory = new PluginFactoryLedgerConnector({ - pluginImportType: PluginImportType.Local, - }); - - const pluginRegistry = new PluginRegistry({}); - const connector: PluginLedgerConnectorBesu = await factory.create({ - rpcApiHttpHost, - rpcApiWsHost, - logLevel, - instanceId: connectorInstanceId, - pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), - }); - - pluginRegistry.add(connector); - - const iPluginHtlcEthBesuErc20Options: IPluginHtlcEthBesuErc20Options = { - instanceId: uuidv4(), - keychainId: keychainId, - pluginRegistry, - }; - const pluginFactoryHtlcEthBesuErc20 = new PluginFactoryHtlcEthBesuErc20({ - pluginImportType: PluginImportType.Local, - }); - const pluginHtlcEthBesuErc20 = await pluginFactoryHtlcEthBesuErc20.create( - iPluginHtlcEthBesuErc20Options, - ); - pluginRegistry.add(pluginHtlcEthBesuErc20); - - const pluginOptions: IPluginHTLCCoordinatorBesuOptions = { - instanceId: uuidv4(), - logLevel, - pluginRegistry, - }; - const factoryHTLC = new PluginFactoryHTLCCoordinatorBesu({ - pluginImportType: PluginImportType.Local, - }); - const pluginHTLCCoordinatorBesu = await factoryHTLC.create(pluginOptions); - pluginRegistry.add(pluginHTLCCoordinatorBesu); const expressApp = express(); expressApp.use(bodyParser.json({ limit: "250mb" })); const server = http.createServer(expressApp); + const besuWsApi = new SocketIoServer(server, { + path: Constants.SocketIoConnectionPathV1, + }); + const listenOptions: IListenOptions = { hostname: "127.0.0.1", port: 0, server, }; - const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; - test.onFinish(async () => await Servers.shutdown(server)); - const { address, port } = addressInfo; - const apiHost = `http://${address}:${port}`; - - const configuration = new Configuration({ basePath: apiHost }); - const htlcCoordinatorBesuApiClient = new HtlcCoordinatorBesuApi( - configuration, - ); - await pluginHTLCCoordinatorBesu.getOrCreateWebServices(); - await pluginHTLCCoordinatorBesu.registerWebServices(expressApp); - const besuWsApi = new SocketIoServer(server, { - path: Constants.SocketIoConnectionPathV1, - }); - const besuConnectorConfiguration = new Configuration({ basePath: apiHost }); - const besuConnectorApi = new BesuApi(besuConnectorConfiguration); - - await connector.getOrCreateWebServices(); - await connector.registerWebServices(expressApp, besuWsApi as any); - - t.comment("Deploys TestToken via .json file on deployContract function"); - const deployOutToken = await connector.deployContract({ - contractName: TestTokenJSON.contractName, - contractAbi: TestTokenJSON.abi, - bytecode: TestTokenJSON.bytecode, - web3SigningCredential, - keychainId, - constructorArgs: ["100", "token", "2", "TKN"], - gas: estimatedGas, - }); - t.ok(deployOutToken, "deployContract() output is truthy OK"); - t.ok( - deployOutToken.transactionReceipt, - "deployContract() output.transactionReceipt is truthy OK", - ); - t.ok( - deployOutToken.transactionReceipt.contractAddress, - "deployContract() output.transactionReceipt.contractAddress is truthy OK", - ); - const tokenAddress = deployOutToken.transactionReceipt - .contractAddress as string; - - t.comment("Approve 10 Tokens to HashTimeLockAddress"); - const approveTokensOutput = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Send, - methodName: "approve", - params: [contractAddress, "10"], - gas: estimatedGas, + let besuTestLedger: BesuTestLedger; + let connector: PluginLedgerConnectorBesu; + let pluginHtlcEthBesuErc20: PluginHtlcEthBesuErc20; + let pluginHTLCCoordinatorBesu: PluginHTLCCoordinatorBesu; + let htlcCoordinatorBesuApiClient: HtlcCoordinatorBesuApi; + let besuConnectorApi: BesuApi; + let rpcApiHttpHost: string; + let rpcApiWsHost: string; + + beforeAll(async () => { + besuTestLedger = new BesuTestLedger(); + await besuTestLedger.start(); + + rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); + rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + + await keychainPlugin.set( + DemoHelperJSON.contractName, + JSON.stringify(DemoHelperJSON), + ); + await keychainPlugin.set( + HashTimeLockJSON.contractName, + JSON.stringify(HashTimeLockJSON), + ); + + const factory = new PluginFactoryLedgerConnector({ + pluginImportType: PluginImportType.Local, + }); + + connector = await factory.create({ + instanceId: connectorInstanceId, + rpcApiHttpHost, + rpcApiWsHost, + logLevel, + pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), + }); + pluginRegistry.add(connector); + + const iPluginHtlcEthBesuErc20Options: IPluginHtlcEthBesuErc20Options = { + instanceId: uuidv4(), + keychainId: keychainId, + pluginRegistry, + }; + + const factoryHtlc = new PluginFactoryHtlcEthBesuErc20({ + pluginImportType: PluginImportType.Local, + }); + + pluginHtlcEthBesuErc20 = await factoryHtlc.create( + iPluginHtlcEthBesuErc20Options, + ); + pluginRegistry.add(pluginHtlcEthBesuErc20); + + const pluginOptions: IPluginHTLCCoordinatorBesuOptions = { + instanceId: uuidv4(), + logLevel, + pluginRegistry, + }; + const factoryHTLC = new PluginFactoryHTLCCoordinatorBesu({ + pluginImportType: PluginImportType.Local, + }); + pluginHTLCCoordinatorBesu = await factoryHTLC.create(pluginOptions); + pluginRegistry.add(pluginHTLCCoordinatorBesu); + + await pluginHTLCCoordinatorBesu.getOrCreateWebServices(); + await pluginHTLCCoordinatorBesu.registerWebServices(expressApp); + + const { address, port } = await Servers.listen(listenOptions); + const apiHost = `http://${address}:${port}`; + + const configuration = new Configuration({ basePath: apiHost }); + htlcCoordinatorBesuApiClient = new HtlcCoordinatorBesuApi(configuration); + + const besuConnectorConfiguration = new Configuration({ basePath: apiHost }); + besuConnectorApi = new BesuApi(besuConnectorConfiguration); + + await connector.getOrCreateWebServices(); + await connector.registerWebServices(expressApp, besuWsApi); }); - t.equal( - approveTokensOutput.data.success, - true, - "approve() transactionReceipt.status is true OK", - ); - - t.comment("Get account balance"); - const responseBalance = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "balanceOf", - params: [firstHighNetWorthAccount], + + afterAll(async () => { + await besuTestLedger.stop(); + await besuTestLedger.destroy(); + await Servers.shutdown(server); }); - t.equal( - responseBalance.data.callOutput, - "100", - "balance of account is 100 OK", - ); - - t.comment("Get HashTimeLock contract and account allowance"); - const allowanceOutput = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "allowance", - params: [firstHighNetWorthAccount, contractAddress], + + afterAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).resolves.not.toThrow(); }); - t.equal(allowanceOutput.status, 200, "allowance status is 200 OK"); - t.equal(allowanceOutput.data.callOutput, "10", "allowance amount is 10 OK"); - t.comment("Create and initialize own HTLC"); - const ownHTLCRequest: OwnHTLCRequest = { - htlcPackage: HtlcPackage.BesuErc20, - connectorInstanceId, - keychainId, - constructorArgs: [], - web3SigningCredential, - inputAmount: 10, - outputAmount: 1, - expiration, - hashLock, - tokenAddress, - receiver, - outputNetwork: "BTC", - outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", - gas: estimatedGas, - }; + test("Deploys TestToken and performs HTLC operations", async () => { + // Deploy TestToken - const response = await htlcCoordinatorBesuApiClient.ownHtlcV1(ownHTLCRequest); - t.equal(response.status, 200, "response status is 200 OK"); - t.equal(response.data.success, true, "response success is true"); - t.ok( - response.data, - "pluginHTLCCoordinatorBesu.ownHtlcV1() output is truthy OK", - ); - t.ok( - response.data.out.transactionReceipt, - "pluginHTLCCoordinatorBesu.ownHtlcV1() output.transactionReceipt is truthy OK", - ); - - t.comment("Get HTLC id"); - const responseTxId = await besuConnectorApi.invokeContractV1({ - contractName: DemoHelperJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "getTxId", - params: [ - firstHighNetWorthAccount, - receiver, - 10, - hashLock, + const deployOutToken = await connector.deployContract({ + contractName: TestTokenJSON.contractName, + contractAbi: TestTokenJSON.abi, + bytecode: TestTokenJSON.bytecode, + web3SigningCredential, + keychainId, + constructorArgs: ["100", "token", "2", "TKN"], + gas: estimatedGas, + }); + expect(deployOutToken).toBeTruthy(); + expect(deployOutToken.transactionReceipt).toBeTruthy(); + expect(deployOutToken.transactionReceipt.contractAddress).toBeTruthy(); + + const tokenAddress = deployOutToken.transactionReceipt + .contractAddress as string; + + const approveTokensOutput = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Send, + methodName: "approve", + params: [contractAddress, "10"], + gas: estimatedGas, + }); + expect(approveTokensOutput.data.success).toBeTrue(); + + const responseBalance = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "balanceOf", + params: [firstHighNetWorthAccount], + }); + expect(responseBalance.data.callOutput).toEqual("100"); + + const allowanceOutput = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "allowance", + params: [firstHighNetWorthAccount, contractAddress], + }); + expect(allowanceOutput.status).toBe(200); + expect(allowanceOutput.data.callOutput).toEqual("10"); + + const ownHTLCRequest: OwnHTLCRequest = { + htlcPackage: HtlcPackage.BesuErc20, + connectorInstanceId, + keychainId, + constructorArgs: [], + web3SigningCredential, + inputAmount: 10, + outputAmount: 1, expiration, + hashLock, tokenAddress, - ], - gas: estimatedGas, - }); + receiver, + outputNetwork: "BTC", + outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", + gas: estimatedGas, + }; - t.comment("Get counterparty HTLC"); - const counterpartyHTLCRequest: CounterpartyHTLCRequest = { - htlcPackage: HtlcPackage.BesuErc20, - connectorInstanceId, - keychainId, - htlcId: responseTxId.data.callOutput, - web3SigningCredential, - gas: estimatedGas, - }; + const response = + await htlcCoordinatorBesuApiClient.ownHtlcV1(ownHTLCRequest); + expect(response.status).toBe(200); + expect(response.data.success).toBe(true); + expect(response.data).toBeTruthy(); + expect(response.data.out.transactionReceipt).toBeTruthy(); - const response2 = await htlcCoordinatorBesuApiClient.counterpartyHtlcV1( - counterpartyHTLCRequest, - ); - t.equal(response2.status, 200, "response status is 200 OK"); - const hstsHeader = response2.headers["strict-transport-security"]; - t.equal( - hstsHeader, - "max-age=31536000; includeSubDomains; preload", - "response header is max-age=31536000; includeSubDomains; preload OK", - ); - t.equal(response2.data.success, true, "response success is true"); - t.equal(response2.data.callOutput, "1", "the contract status is 1 - Active"); -}); + const responseTxId = await besuConnectorApi.invokeContractV1({ + contractName: DemoHelperJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "getTxId", + params: [ + firstHighNetWorthAccount, + receiver, + 10, + hashLock, + expiration, + tokenAddress, + ], + gas: estimatedGas, + }); -test("AFTER " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning did not throw OK"); - t.end(); + const counterpartyHTLCRequest: CounterpartyHTLCRequest = { + htlcPackage: HtlcPackage.BesuErc20, + connectorInstanceId, + keychainId, + htlcId: responseTxId.data.callOutput, + web3SigningCredential, + gas: estimatedGas, + }; + + const response2 = await htlcCoordinatorBesuApiClient.counterpartyHtlcV1( + counterpartyHTLCRequest, + ); + expect(response2.status).toBe(200); + const hstsHeader = response2.headers["strict-transport-security"]; + expect(hstsHeader).toBe("max-age=31536000; includeSubDomains; preload"); + expect(response2.data.success).toBe(true); + expect(response2.data.callOutput).toBe("1"); // Contract status is Active + }); }); diff --git a/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/own-htlc-endpoint.test.ts b/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/own-htlc-endpoint.test.ts index 417c89b208..7554fcb252 100644 --- a/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/own-htlc-endpoint.test.ts +++ b/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/own-htlc-endpoint.test.ts @@ -1,4 +1,4 @@ -import test, { Test } from "tape-promise/tape"; +import "jest-extended"; import { v4 as uuidv4 } from "uuid"; import { AddressInfo } from "net"; import express from "express"; @@ -41,43 +41,44 @@ import HashTimeLockJSON from "@hyperledger/cactus-plugin-htlc-eth-besu-erc20/src import TestTokenJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/Test_Token.json"; import DemoHelperJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/DemoHelpers.json"; -const logLevel: LogLevelDesc = "INFO"; -const estimatedGas = 6721975; -const expiration = 2147483648; -const receiver = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57"; -const hashLock = - "0x3c335ba7f06a8b01d0596589f73c19069e21c81e5013b91f408165d1bf623d32"; -const firstHighNetWorthAccount = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; -const privateKey = - "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"; -const connectorInstanceId = uuidv4(); -const web3SigningCredential: Web3SigningCredential = { - ethAccount: firstHighNetWorthAccount, - secret: privateKey, - type: Web3SigningCredentialType.PrivateKeyHex, -} as Web3SigningCredential; -const contractAddress = "0xCfEB869F69431e42cdB54A4F4f105C19C080A601"; +describe("PluginHtlcCoordinatorBesu", () => { + let rpcApiHttpHost: string; + let rpcApiWsHost: string; + let connector: PluginLedgerConnectorBesu; + let htlcCoordinatorBesuApi: HtlcCoordinatorBesuApi; + let besuConnectorApi: BesuApi; -const testCase = "Test own htlc endpoint"; + const logLevel: LogLevelDesc = "INFO"; + const estimatedGas = 6721975; + const expiration = 2147483648; + const receiver = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57"; + const hashLock = + "0x3c335ba7f06a8b01d0596589f73c19069e21c81e5013b91f408165d1bf623d32"; + const firstHighNetWorthAccount = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; + const privateKey = + "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"; + const connectorInstanceId = uuidv4(); + const web3SigningCredential: Web3SigningCredential = { + ethAccount: firstHighNetWorthAccount, + secret: privateKey, + type: Web3SigningCredentialType.PrivateKeyHex, + } as Web3SigningCredential; + const contractAddress = "0xCfEB869F69431e42cdB54A4F4f105C19C080A601"; -test("BEFORE " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning did not throw OK"); - t.end(); -}); - -test(testCase, async (t: Test) => { - t.comment("Starting Besu Test Ledger"); - const besuTestLedger = new BesuTestLedger(); - await besuTestLedger.start(); + const expressApp = express(); + expressApp.use(bodyParser.json({ limit: "250mb" })); + const server = http.createServer(expressApp); + const listenOptions: IListenOptions = { + hostname: "127.0.0.1", + port: 0, + server: server, + }; - test.onFinish(async () => { - await besuTestLedger.stop(); - await besuTestLedger.destroy(); + const besuWsApi = new SocketIoServer(server, { + path: Constants.SocketIoConnectionPathV1, }); - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + const besuTestLedger = new BesuTestLedger(); const keychainId = uuidv4(); const keychainPlugin = new PluginKeychainMemory({ instanceId: uuidv4(), @@ -104,166 +105,153 @@ test(testCase, async (t: Test) => { }); const pluginRegistry = new PluginRegistry({}); - const connector: PluginLedgerConnectorBesu = await factory.create({ - rpcApiHttpHost, - rpcApiWsHost, - logLevel, - instanceId: connectorInstanceId, - pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), - }); const iPluginHtlcEthBesuErc20Options: IPluginHtlcEthBesuErc20Options = { instanceId: uuidv4(), keychainId: keychainId, pluginRegistry, }; + const pluginFactoryHtlcEthBesuErc20 = new PluginFactoryHtlcEthBesuErc20({ pluginImportType: PluginImportType.Local, }); - const pluginHtlcEthBesuErc20 = await pluginFactoryHtlcEthBesuErc20.create( - iPluginHtlcEthBesuErc20Options, - ); - pluginRegistry.add(pluginHtlcEthBesuErc20); - pluginRegistry.add(connector); - const pluginOptions: IPluginHTLCCoordinatorBesuOptions = { - instanceId: uuidv4(), - logLevel, - pluginRegistry, - }; + beforeAll(async () => { + await pruneDockerAllIfGithubAction({ logLevel }); + }); + afterAll(async () => { + await pruneDockerAllIfGithubAction({ logLevel }); + }); + beforeAll(async () => { + await besuTestLedger.start(); + rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); + rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + }); + afterAll(async () => { + await besuTestLedger.stop(); + await besuTestLedger.destroy(); + }); - const factoryHTLC = new PluginFactoryHTLCCoordinatorBesu({ - pluginImportType: PluginImportType.Local, + afterAll(async () => { + await Servers.shutdown(server); }); - const pluginHTLCCoordinatorBesu = await factoryHTLC.create(pluginOptions); - pluginRegistry.add(pluginHTLCCoordinatorBesu); - const expressApp = express(); - expressApp.use(bodyParser.json({ limit: "250mb" })); - const server = http.createServer(expressApp); - const listenOptions: IListenOptions = { - hostname: "127.0.0.1", - port: 0, - server: server, - }; - const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; - test.onFinish(async () => await Servers.shutdown(server)); - const { address, port } = addressInfo; - const apiHost = `http://${address}:${port}`; + beforeAll(async () => { + connector = await factory.create({ + rpcApiHttpHost, + rpcApiWsHost, + logLevel, + instanceId: connectorInstanceId, + pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), + }); - const configuration = new Configuration({ basePath: apiHost }); - const htlcCoordinatorBesuApi = new HtlcCoordinatorBesuApi(configuration); - await pluginHTLCCoordinatorBesu.getOrCreateWebServices(); - await pluginHTLCCoordinatorBesu.registerWebServices(expressApp); + const pluginHtlcEthBesuErc20 = await pluginFactoryHtlcEthBesuErc20.create( + iPluginHtlcEthBesuErc20Options, + ); + pluginRegistry.add(pluginHtlcEthBesuErc20); - const besuWsApi = new SocketIoServer(server, { - path: Constants.SocketIoConnectionPathV1, - }); - const besuConnectorConfiguration = new Configuration({ basePath: apiHost }); - const besuConnectorApi = new BesuApi(besuConnectorConfiguration); + pluginRegistry.add(connector); + const pluginOptions: IPluginHTLCCoordinatorBesuOptions = { + instanceId: uuidv4(), + logLevel, + pluginRegistry, + }; - await connector.getOrCreateWebServices(); - await connector.registerWebServices(expressApp, besuWsApi as any); + const factoryHTLC = new PluginFactoryHTLCCoordinatorBesu({ + pluginImportType: PluginImportType.Local, + }); + const pluginHTLCCoordinatorBesu = await factoryHTLC.create(pluginOptions); + pluginRegistry.add(pluginHTLCCoordinatorBesu); - t.comment("Deploys TestToken via .json file on deployContract function"); - const deployOutToken = await connector.deployContract({ - contractName: TestTokenJSON.contractName, - contractAbi: TestTokenJSON.abi, - bytecode: TestTokenJSON.bytecode, - web3SigningCredential, - keychainId, - constructorArgs: ["100", "token", "2", "TKN"], - gas: estimatedGas, - }); - t.ok(deployOutToken, "deployContract() output is truthy OK"); - t.ok( - deployOutToken.transactionReceipt, - "deployContract() output.transactionReceipt is truthy OK", - ); - t.ok( - deployOutToken.transactionReceipt.contractAddress, - "deployContract() output.transactionReceipt.contractAddress is truthy OK", - ); - const tokenAddress = deployOutToken.transactionReceipt - .contractAddress as string; + const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; + const { address, port } = addressInfo; + const apiHost = `http://${address}:${port}`; - t.comment("Approve 10 Tokens to HashTimeLockAddress"); - const approveTokensOutput = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Send, - methodName: "approve", - params: [contractAddress, "10"], - gas: estimatedGas, - }); - t.equal( - approveTokensOutput.data.success, - true, - "approve() transactionReceipt.status is true OK", - ); + const configuration = new Configuration({ basePath: apiHost }); + htlcCoordinatorBesuApi = new HtlcCoordinatorBesuApi(configuration); + await pluginHTLCCoordinatorBesu.getOrCreateWebServices(); + await pluginHTLCCoordinatorBesu.registerWebServices(expressApp); - t.comment("Get account balance"); - const responseBalance = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "balanceOf", - params: [firstHighNetWorthAccount], - }); - t.equal( - responseBalance.data.callOutput, - "100", - "balance of account is 100 OK", - ); + const besuConnectorConfiguration = new Configuration({ basePath: apiHost }); + besuConnectorApi = new BesuApi(besuConnectorConfiguration); - t.comment("Get HashTimeLock contract and account allowance"); - const allowanceOutput = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "allowance", - params: [firstHighNetWorthAccount, contractAddress], + await connector.getOrCreateWebServices(); + await connector.registerWebServices(expressApp, besuWsApi as any); }); - t.equal(allowanceOutput.status, 200, "allowance status is 200 OK"); - t.equal(allowanceOutput.data.callOutput, "10", "allowance amount is 10 OK"); - t.comment("Create and initialize own HTLC"); - const ownHTLCRequest: OwnHTLCRequest = { - htlcPackage: HtlcPackage.BesuErc20, - connectorInstanceId, - keychainId, - constructorArgs: [], - web3SigningCredential, - inputAmount: 10, - outputAmount: 1, - expiration, - hashLock, - tokenAddress, - receiver, - outputNetwork: "BTC", - outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", - gas: estimatedGas, - }; + test("ownHtlcV1() endpoint", async () => { + const deployOutToken = await connector.deployContract({ + contractName: TestTokenJSON.contractName, + contractAbi: TestTokenJSON.abi, + bytecode: TestTokenJSON.bytecode, + web3SigningCredential, + keychainId, + constructorArgs: ["100", "token", "2", "TKN"], + gas: estimatedGas, + }); + expect(deployOutToken).toBeTruthy(); + expect(deployOutToken.transactionReceipt).toBeTruthy(); + expect(deployOutToken.transactionReceipt.contractAddress).toBeTruthy(); + const tokenAddress = deployOutToken.transactionReceipt + .contractAddress as string; - const response = await htlcCoordinatorBesuApi.ownHtlcV1(ownHTLCRequest); - t.equal(response.status, 200, "response status is 200 OK"); - t.equal(response.data.success, true, "response success is true"); - t.ok( - response.data, - "pluginHTLCCoordinatorBesu.ownHtlcV1() output is truthy OK", - ); - t.ok( - response.data.out.transactionReceipt, - "pluginHTLCCoordinatorBesu.ownHtlcV1() output.transactionReceipt is truthy OK", - ); - t.end(); -}); + const approveTokensOutput = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Send, + methodName: "approve", + params: [contractAddress, "10"], + gas: estimatedGas, + }); + expect(approveTokensOutput.data.success).toBeTrue(); + + const responseBalance = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "balanceOf", + params: [firstHighNetWorthAccount], + }); + + expect(responseBalance.data.callOutput).toEqual("100"); + + const allowanceOutput = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "allowance", + params: [firstHighNetWorthAccount, contractAddress], + }); -test("AFTER " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning did not throw OK"); - t.end(); + expect(allowanceOutput.status).toEqual(200); + expect(allowanceOutput.data.callOutput).toEqual("10"); + + const ownHTLCRequest: OwnHTLCRequest = { + htlcPackage: HtlcPackage.BesuErc20, + connectorInstanceId, + keychainId, + constructorArgs: [], + web3SigningCredential, + inputAmount: 10, + outputAmount: 1, + expiration, + hashLock, + tokenAddress, + receiver, + outputNetwork: "BTC", + outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", + gas: estimatedGas, + }; + + const response = await htlcCoordinatorBesuApi.ownHtlcV1(ownHTLCRequest); + + expect(response.status).toEqual(200); + expect(response.data).toBeTruthy(); + expect(response.data.success).toBeTrue(); + expect(response.data.out).toBeTruthy(); + expect(response.data.out.transactionReceipt).toBeTruthy(); + }); }); diff --git a/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/refund.test.ts b/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/refund.test.ts index 7343633d06..5fda8a2095 100644 --- a/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/refund.test.ts +++ b/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/refund.test.ts @@ -1,7 +1,5 @@ -import axios from "axios"; -import test, { Test } from "tape-promise/tape"; +import "jest-extended"; import { v4 as uuidv4 } from "uuid"; -import { AddressInfo } from "net"; import express from "express"; import bodyParser from "body-parser"; import http from "http"; @@ -13,9 +11,15 @@ import { HtlcPackage, OwnHTLCRequest, CounterpartyHTLCRequest, - WithdrawCounterpartyRequest, Configuration, + PluginHTLCCoordinatorBesu, + WithdrawCounterpartyRequest, } from "../../../../main/typescript/public-api"; +import { + IPluginHtlcEthBesuErc20Options, + PluginFactoryHtlcEthBesuErc20, + PluginHtlcEthBesuErc20, +} from "@hyperledger/cactus-plugin-htlc-eth-besu-erc20"; import { DefaultApi as BesuApi, EthContractInvocationType, @@ -24,14 +28,11 @@ import { Web3SigningCredentialType, Web3SigningCredential, } from "@hyperledger/cactus-plugin-ledger-connector-besu"; -import { - IPluginHtlcEthBesuErc20Options, - PluginFactoryHtlcEthBesuErc20, -} from "@hyperledger/cactus-plugin-htlc-eth-besu-erc20"; import { LogLevelDesc, IListenOptions, Servers, + LoggerProvider, } from "@hyperledger/cactus-common"; import { PluginRegistry } from "@hyperledger/cactus-core"; import { Constants, PluginImportType } from "@hyperledger/cactus-core-api"; @@ -40,50 +41,40 @@ import { pruneDockerAllIfGithubAction, } from "@hyperledger/cactus-test-tooling"; import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory"; - import HashTimeLockJSON from "@hyperledger/cactus-plugin-htlc-eth-besu-erc20/src/main/solidity/contracts/HashedTimeLockContract.json"; import TestTokenJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/Test_Token.json"; import DemoHelperJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/DemoHelpers.json"; +import axios from "axios"; -const logLevel: LogLevelDesc = "INFO"; -const estimatedGas = 6721975; -const expiration = 2147483648; -const incorrect_secret = - "0x4853485acd2bfc3c632026ee365279743af107a30492e3ceaa7aefc30c2a048a"; -const receiver = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57"; -const hashLock = - "0x3c335ba7f06a8b01d0596589f73c19069e21c81e5013b91f408165d1bf623d32"; -const firstHighNetWorthAccount = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; -const privateKey = - "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"; -const connectorInstanceId = uuidv4(); -const web3SigningCredential: Web3SigningCredential = { - ethAccount: firstHighNetWorthAccount, - secret: privateKey, - type: Web3SigningCredentialType.PrivateKeyHex, -} as Web3SigningCredential; -const contractAddress = "0xCfEB869F69431e42cdB54A4F4f105C19C080A601"; - -const testCase = "Test own htlc endpoint"; - -test("BEFORE " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning did not throw OK"); - t.end(); -}); +describe("HTLC Coordinator Besu", () => { + const logLevel: LogLevelDesc = "DEBUG"; + const log = LoggerProvider.getOrCreate({ + label: + "plugin-htlc-coordinator-besu-plugin-htlc-coordinator/refund.test.ts", + level: logLevel, + }); + const estimatedGas = 6721975; + const expiration = 2147483648; + const receiver = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57"; + const hashLock = + "0x3c335ba7f06a8b01d0596589f73c19069e21c81e5013b91f408165d1bf623d32"; + const privateKey = + "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"; -test(testCase, async (t: Test) => { - t.comment("Starting Besu Test Ledger"); - const besuTestLedger = new BesuTestLedger(); - await besuTestLedger.start(); + const incorrectSecret = + "0x4853485acd2bfc3c632026ee365279743af107a30492e3ceaa7aefc30c2a048a"; - test.onFinish(async () => { - await besuTestLedger.stop(); - await besuTestLedger.destroy(); - }); + const firstHighNetWorthAccount = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; + const contractAddress = "0xCfEB869F69431e42cdB54A4F4f105C19C080A601"; + + const connectorInstanceId = uuidv4(); + const pluginRegistry = new PluginRegistry({}); + const web3SigningCredential: Web3SigningCredential = { + ethAccount: firstHighNetWorthAccount, + secret: privateKey, + type: Web3SigningCredentialType.PrivateKeyHex, + } as Web3SigningCredential; - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); const keychainId = uuidv4(); const keychainPlugin = new PluginKeychainMemory({ instanceId: uuidv4(), @@ -96,269 +87,266 @@ test(testCase, async (t: Test) => { ]), logLevel, }); - keychainPlugin.set( - DemoHelperJSON.contractName, - JSON.stringify(DemoHelperJSON), - ); - keychainPlugin.set( - HashTimeLockJSON.contractName, - JSON.stringify(HashTimeLockJSON), - ); - - const factory = new PluginFactoryLedgerConnector({ - pluginImportType: PluginImportType.Local, - }); - - const pluginRegistry = new PluginRegistry({}); - const connector: PluginLedgerConnectorBesu = await factory.create({ - rpcApiHttpHost, - rpcApiWsHost, - logLevel, - instanceId: connectorInstanceId, - pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), - }); - pluginRegistry.add(connector); - - const iPluginHtlcEthBesuErc20Options: IPluginHtlcEthBesuErc20Options = { - instanceId: uuidv4(), - keychainId: keychainId, - pluginRegistry, - }; - const pluginFactoryHtlcEthBesuErc20 = new PluginFactoryHtlcEthBesuErc20({ - pluginImportType: PluginImportType.Local, - }); - const pluginHtlcEthBesuErc20 = await pluginFactoryHtlcEthBesuErc20.create( - iPluginHtlcEthBesuErc20Options, - ); - pluginRegistry.add(pluginHtlcEthBesuErc20); - - const pluginOptions: IPluginHTLCCoordinatorBesuOptions = { - instanceId: uuidv4(), - logLevel, - pluginRegistry, - }; - const factoryHTLC = new PluginFactoryHTLCCoordinatorBesu({ - pluginImportType: PluginImportType.Local, - }); - const pluginHTLCCoordinatorBesu = await factoryHTLC.create(pluginOptions); - pluginRegistry.add(pluginHTLCCoordinatorBesu); const expressApp = express(); expressApp.use(bodyParser.json({ limit: "250mb" })); const server = http.createServer(expressApp); + const besuWsApi = new SocketIoServer(server, { + path: Constants.SocketIoConnectionPathV1, + }); + const listenOptions: IListenOptions = { hostname: "127.0.0.1", port: 0, server, }; - const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; - test.onFinish(async () => await Servers.shutdown(server)); - const { address, port } = addressInfo; - const apiHost = `http://${address}:${port}`; - const configuration = new Configuration({ basePath: apiHost }); - const htlcCoordinatorBesuApi = new HtlcCoordinatorBesuApi(configuration); + let besuTestLedger: BesuTestLedger; + let connector: PluginLedgerConnectorBesu; + let pluginHtlcEthBesuErc20: PluginHtlcEthBesuErc20; + let pluginHTLCCoordinatorBesu: PluginHTLCCoordinatorBesu; + let htlcCoordinatorBesuApiClient: HtlcCoordinatorBesuApi; + let besuConnectorApi: BesuApi; + let rpcApiHttpHost: string; + let rpcApiWsHost: string; - await pluginHTLCCoordinatorBesu.getOrCreateWebServices(); - await pluginHTLCCoordinatorBesu.registerWebServices(expressApp); - const besuWsApi = new SocketIoServer(server, { - path: Constants.SocketIoConnectionPathV1, - }); - const besuConnectorConfiguration = new Configuration({ basePath: apiHost }); - const besuConnectorApi = new BesuApi(besuConnectorConfiguration); - - await connector.getOrCreateWebServices(); - await connector.registerWebServices(expressApp, besuWsApi as any); - - t.comment("Deploys TestToken via .json file on deployContract function"); - const deployOutToken = await connector.deployContract({ - contractName: TestTokenJSON.contractName, - contractAbi: TestTokenJSON.abi, - bytecode: TestTokenJSON.bytecode, - web3SigningCredential, - keychainId, - constructorArgs: ["100", "token", "2", "TKN"], - gas: estimatedGas, - }); - t.ok(deployOutToken, "deployContract() output is truthy OK"); - t.ok( - deployOutToken.transactionReceipt, - "deployContract() output.transactionReceipt is truthy OK", - ); - t.ok( - deployOutToken.transactionReceipt.contractAddress, - "deployContract() output.transactionReceipt.contractAddress is truthy OK", - ); - const tokenAddress = deployOutToken.transactionReceipt - .contractAddress as string; - - t.comment("Approve 10 Tokens to HashTimeLockAddress"); - const approveTokensOutput = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Send, - methodName: "approve", - params: [contractAddress, "10"], - gas: estimatedGas, + beforeAll(async () => { + besuTestLedger = new BesuTestLedger(); + await besuTestLedger.start(); + + rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); + rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + + await keychainPlugin.set( + DemoHelperJSON.contractName, + JSON.stringify(DemoHelperJSON), + ); + await keychainPlugin.set( + HashTimeLockJSON.contractName, + JSON.stringify(HashTimeLockJSON), + ); + + const factory = new PluginFactoryLedgerConnector({ + pluginImportType: PluginImportType.Local, + }); + + connector = await factory.create({ + instanceId: connectorInstanceId, + rpcApiHttpHost, + rpcApiWsHost, + logLevel, + pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), + }); + pluginRegistry.add(connector); + + const iPluginHtlcEthBesuErc20Options: IPluginHtlcEthBesuErc20Options = { + instanceId: uuidv4(), + keychainId: keychainId, + pluginRegistry, + }; + + const factoryHtlc = new PluginFactoryHtlcEthBesuErc20({ + pluginImportType: PluginImportType.Local, + }); + + pluginHtlcEthBesuErc20 = await factoryHtlc.create( + iPluginHtlcEthBesuErc20Options, + ); + pluginRegistry.add(pluginHtlcEthBesuErc20); + + const pluginOptions: IPluginHTLCCoordinatorBesuOptions = { + instanceId: uuidv4(), + logLevel, + pluginRegistry, + }; + const factoryHTLC = new PluginFactoryHTLCCoordinatorBesu({ + pluginImportType: PluginImportType.Local, + }); + pluginHTLCCoordinatorBesu = await factoryHTLC.create(pluginOptions); + pluginRegistry.add(pluginHTLCCoordinatorBesu); + + await pluginHTLCCoordinatorBesu.getOrCreateWebServices(); + await pluginHTLCCoordinatorBesu.registerWebServices(expressApp); + + const { address, port } = await Servers.listen(listenOptions); + const apiHost = `http://${address}:${port}`; + + const configuration = new Configuration({ basePath: apiHost }); + htlcCoordinatorBesuApiClient = new HtlcCoordinatorBesuApi(configuration); + + const besuConnectorConfiguration = new Configuration({ basePath: apiHost }); + besuConnectorApi = new BesuApi(besuConnectorConfiguration); + + await connector.getOrCreateWebServices(); + await connector.registerWebServices(expressApp, besuWsApi); }); - t.equal( - approveTokensOutput.data.success, - true, - "approve() transactionReceipt.status is true OK", - ); - - t.comment("Get account balance"); - const responseBalance = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "balanceOf", - params: [firstHighNetWorthAccount], + + afterAll(async () => { + await besuTestLedger.stop(); + await besuTestLedger.destroy(); + await Servers.shutdown(server); }); - t.equal( - responseBalance.data.callOutput, - "100", - "balance of account is 100 OK", - ); - - t.comment("Get HashTimeLock contract and account allowance"); - const allowanceOutput = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "allowance", - params: [firstHighNetWorthAccount, contractAddress], + + afterAll(async () => { + const pruning = pruneDockerAllIfGithubAction({ logLevel }); + await expect(pruning).resolves.not.toThrow(); }); - t.equal(allowanceOutput.status, 200, "allowance status is 200 OK"); - t.equal(allowanceOutput.data.callOutput, "10", "allowance amount is 10 OK"); - t.comment("Create and initialize own HTLC"); - const ownHTLCRequest: OwnHTLCRequest = { - htlcPackage: HtlcPackage.BesuErc20, - connectorInstanceId, - keychainId, - constructorArgs: [], - web3SigningCredential, - inputAmount: 10, - outputAmount: 1, - expiration, - hashLock, - tokenAddress, - receiver, - outputNetwork: "BTC", - outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", - gas: estimatedGas, - }; + test("withdrawCounterpartyV1() endpoint for refunds", async () => { + // Deploy TestToken - const response = await htlcCoordinatorBesuApi.ownHtlcV1(ownHTLCRequest); - t.equal(response.status, 200, "response status is 200 OK"); - t.equal(response.data.success, true, "response success is true"); - t.ok( - response.data, - "pluginHTLCCoordinatorBesu.ownHtlcV1() output is truthy OK", - ); - t.ok( - response.data.out.transactionReceipt, - "pluginHTLCCoordinatorBesu.ownHtlcV1() output.transactionReceipt is truthy OK", - ); - - t.comment("Get HTLC id"); - const responseTxId = await besuConnectorApi.invokeContractV1({ - contractName: DemoHelperJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "getTxId", - params: [ - firstHighNetWorthAccount, - receiver, - 10, - hashLock, + const deployOutToken = await connector.deployContract({ + contractName: TestTokenJSON.contractName, + contractAbi: TestTokenJSON.abi, + bytecode: TestTokenJSON.bytecode, + web3SigningCredential, + keychainId, + constructorArgs: ["100", "token", "2", "TKN"], + gas: estimatedGas, + }); + expect(deployOutToken).toBeTruthy(); + expect(deployOutToken.transactionReceipt).toBeTruthy(); + expect(deployOutToken.transactionReceipt.contractAddress).toBeTruthy(); + + const tokenAddress = deployOutToken.transactionReceipt + .contractAddress as string; + + const approveTokensOutput = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Send, + methodName: "approve", + params: [contractAddress, "10"], + gas: estimatedGas, + }); + expect(approveTokensOutput.data.success).toBeTrue(); + + const responseBalance = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "balanceOf", + params: [firstHighNetWorthAccount], + }); + expect(responseBalance.data.callOutput).toEqual("100"); + + const allowanceOutput = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "allowance", + params: [firstHighNetWorthAccount, contractAddress], + }); + expect(allowanceOutput.status).toBe(200); + expect(allowanceOutput.data.callOutput).toEqual("10"); + + const ownHTLCRequest: OwnHTLCRequest = { + htlcPackage: HtlcPackage.BesuErc20, + connectorInstanceId, + keychainId, + constructorArgs: [], + web3SigningCredential, + inputAmount: 10, + outputAmount: 1, expiration, + hashLock, tokenAddress, - ], - gas: estimatedGas, - }); + receiver, + outputNetwork: "BTC", + outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", + gas: estimatedGas, + }; - t.comment("Get counterparty HTLC"); - const counterpartyHTLCRequest: CounterpartyHTLCRequest = { - htlcPackage: HtlcPackage.BesuErc20, - connectorInstanceId, - keychainId, - htlcId: responseTxId.data.callOutput, - web3SigningCredential, - gas: estimatedGas, - }; + const response = + await htlcCoordinatorBesuApiClient.ownHtlcV1(ownHTLCRequest); + expect(response.status).toBe(200); + expect(response.data.success).toBe(true); + expect(response.data).toBeTruthy(); + expect(response.data.out.transactionReceipt).toBeTruthy(); - const response2 = await htlcCoordinatorBesuApi.counterpartyHtlcV1( - counterpartyHTLCRequest, - ); - t.equal(response2.status, 200, "response status is 200 OK"); - t.equal(response2.data.success, true, "response success is true"); - t.equal(response2.data.callOutput, "1", "the contract status is 1 - Active"); - - t.comment("Withdraw with incorrect secret"); - const withdrawCounterparty: WithdrawCounterpartyRequest = { - htlcPackage: HtlcPackage.BesuErc20, - connectorInstanceId, - keychainId, - web3SigningCredential, - htlcId: responseTxId.data.callOutput, - secret: incorrect_secret, - gas: estimatedGas, - }; + const responseTxId = await besuConnectorApi.invokeContractV1({ + contractName: DemoHelperJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "getTxId", + params: [ + firstHighNetWorthAccount, + receiver, + 10, + hashLock, + expiration, + tokenAddress, + ], + gas: estimatedGas, + }); - try { - await htlcCoordinatorBesuApi.withdrawCounterpartyV1(withdrawCounterparty); - } catch (exp: unknown) { - if (axios.isAxiosError(exp) && exp.response) { - const revertReason = exp.response.data.cause.receipt.revertReason; - const regExp = new RegExp(/0e494e56414c49445f5345435245540/); - const rejectMsg = "response === throws OK"; - t.match(revertReason, regExp, rejectMsg); - } else { - throw exp; - } - } + const counterpartyHTLCRequest: CounterpartyHTLCRequest = { + htlcPackage: HtlcPackage.BesuErc20, + connectorInstanceId, + keychainId, + htlcId: responseTxId.data.callOutput, + web3SigningCredential, + gas: estimatedGas, + }; - t.comment("Get balance of receiver account"); - const responseFinalBalanceReceiver = await connector.invokeContract({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "balanceOf", - params: [receiver], - }); - t.equal( - responseFinalBalanceReceiver.callOutput, - "0", - "balance of receiver account is 0 OK", - ); - - t.comment("Get balance of sender account"); - const responseFinalBalanceSender = await connector.invokeContract({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "balanceOf", - params: [firstHighNetWorthAccount], - }); - t.equal( - responseFinalBalanceSender.callOutput, - "90", - "balance of sender account is 90 OK", - ); + const response2 = await htlcCoordinatorBesuApiClient.counterpartyHtlcV1( + counterpartyHTLCRequest, + ); + expect(response2.status).toBe(200); + const hstsHeader = response2.headers["strict-transport-security"]; + expect(hstsHeader).toBe("max-age=31536000; includeSubDomains; preload"); + expect(response2.data.success).toBe(true); + expect(response2.data.callOutput).toBe("1"); // Contract status is Active - t.end(); -}); + const withdrawCounterparty: WithdrawCounterpartyRequest = { + htlcPackage: HtlcPackage.BesuErc20, + connectorInstanceId, + keychainId, + web3SigningCredential, + htlcId: responseTxId.data.callOutput, + secret: incorrectSecret, + gas: estimatedGas, + }; + + try { + await htlcCoordinatorBesuApiClient.withdrawCounterpartyV1( + withdrawCounterparty, + ); + } catch (exp: unknown) { + log.debug("Caught error as expected: %o", exp); + if (axios.isAxiosError(exp) && exp.response) { + log.debug("Caught response: %o", exp.response.data); + const revertReason = exp.response.data.cause.receipt.revertReason; + const regExp = new RegExp(/0e494e56414c49445f5345435245540/); + expect(revertReason).toMatch(regExp); + // t.match(revertReason, regExp, rejectMsg); + } else { + throw exp; + } + } -test("AFTER " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning did not throw OK"); - t.end(); + const responseFinalBalanceReceiver = await connector.invokeContract({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "balanceOf", + params: [receiver], + }); + expect(responseFinalBalanceReceiver.callOutput).toEqual("0"); + + const responseFinalBalanceSender = await connector.invokeContract({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "balanceOf", + params: [firstHighNetWorthAccount], + }); + expect(responseFinalBalanceSender.callOutput).toEqual("90"); + }); }); diff --git a/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/withdraw-counterparty-endpoint.test.ts b/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/withdraw-counterparty-endpoint.test.ts index db77265eec..0b215b620c 100644 --- a/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/withdraw-counterparty-endpoint.test.ts +++ b/extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/withdraw-counterparty-endpoint.test.ts @@ -1,10 +1,10 @@ -import test, { Test } from "tape-promise/tape"; +import "jest-extended"; import { v4 as uuidv4 } from "uuid"; import { AddressInfo } from "net"; import express from "express"; import bodyParser from "body-parser"; -import http from "http"; import { Server as SocketIoServer } from "socket.io"; +import http from "http"; import { encodeParameter } from "web3-eth-abi"; import { keccak256 } from "web3-utils"; import { @@ -13,9 +13,9 @@ import { IPluginHTLCCoordinatorBesuOptions, HtlcPackage, OwnHTLCRequest, + Configuration, CounterpartyHTLCRequest, WithdrawCounterpartyRequest, - Configuration, } from "../../../../main/typescript/public-api"; import { DefaultApi as BesuApi, @@ -41,51 +41,52 @@ import { pruneDockerAllIfGithubAction, } from "@hyperledger/cactus-test-tooling"; import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory"; - import HashTimeLockJSON from "@hyperledger/cactus-plugin-htlc-eth-besu-erc20/src/main/solidity/contracts/HashedTimeLockContract.json"; import TestTokenJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/Test_Token.json"; import DemoHelperJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/DemoHelpers.json"; -const logLevel: LogLevelDesc = "DEBUG"; -const estimatedGas = 6721975; -const expiration = 2147483648; -const secret = - "0x3853485acd2bfc3c632026ee365279743af107a30492e3ceaa7aefc30c2a048a"; -const receiver = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57"; -const firstHighNetWorthAccount = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; -const privateKey = - "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"; -const connectorInstanceId = uuidv4(); -const web3SigningCredential: Web3SigningCredential = { - ethAccount: firstHighNetWorthAccount, - secret: privateKey, - type: Web3SigningCredentialType.PrivateKeyHex, -} as Web3SigningCredential; -const contractAddress = "0xCfEB869F69431e42cdB54A4F4f105C19C080A601"; - -const testCase = "Test own htlc endpoint"; - -test("BEFORE " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning did not throw OK"); - t.end(); -}); +describe("PluginHtlcCoordinatorBesu", () => { + let rpcApiHttpHost: string; + let rpcApiWsHost: string; + let connector: PluginLedgerConnectorBesu; + let htlcCoordinatorBesuApi: HtlcCoordinatorBesuApi; + let besuConnectorApi: BesuApi; + + const logLevel: LogLevelDesc = "DEBUG"; + const estimatedGas = 6721975; + const expiration = 2147483648; + const secret = + "0x3853485acd2bfc3c632026ee365279743af107a30492e3ceaa7aefc30c2a048a"; + const receiver = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57"; + + const firstHighNetWorthAccount = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; + const privateKey = + "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"; + const connectorInstanceId = uuidv4(); + const web3SigningCredential: Web3SigningCredential = { + ethAccount: firstHighNetWorthAccount, + secret: privateKey, + type: Web3SigningCredentialType.PrivateKeyHex, + } as Web3SigningCredential; + const contractAddress = "0xCfEB869F69431e42cdB54A4F4f105C19C080A601"; -test(testCase, async (t: Test) => { const secretEthAbiEncoded = encodeParameter("uint256", secret); const hashLock = keccak256(secretEthAbiEncoded); - t.comment("Starting Besu Test Ledger"); - const besuTestLedger = new BesuTestLedger(); - await besuTestLedger.start(); + const expressApp = express(); + expressApp.use(bodyParser.json({ limit: "250mb" })); + const server = http.createServer(expressApp); + const listenOptions: IListenOptions = { + hostname: "127.0.0.1", + port: 0, + server: server, + }; - test.onFinish(async () => { - await besuTestLedger.stop(); - await besuTestLedger.destroy(); + const besuWsApi = new SocketIoServer(server, { + path: Constants.SocketIoConnectionPathV1, }); - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); + const besuTestLedger = new BesuTestLedger(); const keychainId = uuidv4(); const keychainPlugin = new PluginKeychainMemory({ instanceId: uuidv4(), @@ -112,246 +113,220 @@ test(testCase, async (t: Test) => { }); const pluginRegistry = new PluginRegistry({}); - const connector: PluginLedgerConnectorBesu = await factory.create({ - rpcApiHttpHost, - rpcApiWsHost, - logLevel, - instanceId: connectorInstanceId, - pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), - }); - pluginRegistry.add(connector); const iPluginHtlcEthBesuErc20Options: IPluginHtlcEthBesuErc20Options = { instanceId: uuidv4(), keychainId: keychainId, pluginRegistry, }; + const pluginFactoryHtlcEthBesuErc20 = new PluginFactoryHtlcEthBesuErc20({ pluginImportType: PluginImportType.Local, }); - const pluginHtlcEthBesuErc20 = await pluginFactoryHtlcEthBesuErc20.create( - iPluginHtlcEthBesuErc20Options, - ); - pluginRegistry.add(pluginHtlcEthBesuErc20); - const pluginOptions: IPluginHTLCCoordinatorBesuOptions = { - instanceId: uuidv4(), - logLevel, - pluginRegistry, - }; - const factoryHTLC = new PluginFactoryHTLCCoordinatorBesu({ - pluginImportType: PluginImportType.Local, + beforeAll(async () => { + await pruneDockerAllIfGithubAction({ logLevel }); }); - const pluginHTLCCoordinatorBesu = await factoryHTLC.create(pluginOptions); - pluginRegistry.add(pluginHTLCCoordinatorBesu); - - const expressApp = express(); - expressApp.use(bodyParser.json({ limit: "250mb" })); - const server = http.createServer(expressApp); - const listenOptions: IListenOptions = { - hostname: "127.0.0.1", - port: 0, - server, - }; - const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; - test.onFinish(async () => await Servers.shutdown(server)); - const { address, port } = addressInfo; - const apiHost = `http://${address}:${port}`; - - const configuration = new Configuration({ basePath: apiHost }); - const htlcCoordinatorBesuApi = new HtlcCoordinatorBesuApi(configuration); - - await pluginHTLCCoordinatorBesu.getOrCreateWebServices(); - await pluginHTLCCoordinatorBesu.registerWebServices(expressApp); - const besuWsApi = new SocketIoServer(server, { - path: Constants.SocketIoConnectionPathV1, + afterAll(async () => { + await pruneDockerAllIfGithubAction({ logLevel }); }); - const besuConnectorConfiguration = new Configuration({ basePath: apiHost }); - const besuConnectorApi = new BesuApi(besuConnectorConfiguration); - - await connector.getOrCreateWebServices(); - await connector.registerWebServices(expressApp, besuWsApi as any); - - t.comment("Deploys TestToken via .json file on deployContract function"); - const deployOutToken = await connector.deployContract({ - contractName: TestTokenJSON.contractName, - contractAbi: TestTokenJSON.abi, - bytecode: TestTokenJSON.bytecode, - web3SigningCredential, - keychainId, - constructorArgs: ["100", "token", "2", "TKN"], - gas: estimatedGas, + beforeAll(async () => { + await besuTestLedger.start(); + rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); + rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); }); - t.ok(deployOutToken, "deployContract() output is truthy OK"); - t.ok( - deployOutToken.transactionReceipt, - "deployContract() output.transactionReceipt is truthy OK", - ); - t.ok( - deployOutToken.transactionReceipt.contractAddress, - "deployContract() output.transactionReceipt.contractAddress is truthy OK", - ); - const tokenAddress = deployOutToken.transactionReceipt - .contractAddress as string; - - t.comment("Approve 10 Tokens to HashTimeLockAddress"); - const approveTokensOutput = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Send, - methodName: "approve", - params: [contractAddress, "10"], - gas: estimatedGas, + afterAll(async () => { + await besuTestLedger.stop(); + await besuTestLedger.destroy(); }); - t.equal( - approveTokensOutput.data.success, - true, - "approve() transactionReceipt.status is true OK", - ); - t.comment("Get account balance"); - const responseBalance = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "balanceOf", - params: [firstHighNetWorthAccount], + afterAll(async () => { + await Servers.shutdown(server); }); - t.equal( - responseBalance.data.callOutput, - "100", - "balance of account is 100 OK", - ); - t.comment("Get HashTimeLock contract and account allowance"); - const allowanceOutput = await besuConnectorApi.invokeContractV1({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "allowance", - params: [firstHighNetWorthAccount, contractAddress], + beforeAll(async () => { + connector = await factory.create({ + rpcApiHttpHost, + rpcApiWsHost, + logLevel, + instanceId: connectorInstanceId, + pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), + }); + + const pluginHtlcEthBesuErc20 = await pluginFactoryHtlcEthBesuErc20.create( + iPluginHtlcEthBesuErc20Options, + ); + pluginRegistry.add(pluginHtlcEthBesuErc20); + + pluginRegistry.add(connector); + const pluginOptions: IPluginHTLCCoordinatorBesuOptions = { + instanceId: uuidv4(), + logLevel, + pluginRegistry, + }; + + const factoryHTLC = new PluginFactoryHTLCCoordinatorBesu({ + pluginImportType: PluginImportType.Local, + }); + const pluginHTLCCoordinatorBesu = await factoryHTLC.create(pluginOptions); + pluginRegistry.add(pluginHTLCCoordinatorBesu); + + const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; + const { address, port } = addressInfo; + const apiHost = `http://${address}:${port}`; + + const configuration = new Configuration({ basePath: apiHost }); + htlcCoordinatorBesuApi = new HtlcCoordinatorBesuApi(configuration); + await pluginHTLCCoordinatorBesu.getOrCreateWebServices(); + await pluginHTLCCoordinatorBesu.registerWebServices(expressApp); + + const besuConnectorConfiguration = new Configuration({ basePath: apiHost }); + besuConnectorApi = new BesuApi(besuConnectorConfiguration); + + await connector.getOrCreateWebServices(); + await connector.registerWebServices(expressApp, besuWsApi); }); - t.equal(allowanceOutput.status, 200, "allowance status is 200 OK"); - t.equal(allowanceOutput.data.callOutput, "10", "allowance amount is 10 OK"); - t.comment("Create and initialize own HTLC"); - const ownHTLCRequest: OwnHTLCRequest = { - htlcPackage: HtlcPackage.BesuErc20, - connectorInstanceId, - keychainId, - constructorArgs: [], - web3SigningCredential, - inputAmount: 10, - outputAmount: 1, - expiration, - hashLock, - tokenAddress, - receiver, - outputNetwork: "BTC", - outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", - gas: estimatedGas, - }; + test("withdrawCounterpartyV1() endpoint", async () => { + const deployOutToken = await connector.deployContract({ + contractName: TestTokenJSON.contractName, + contractAbi: TestTokenJSON.abi, + bytecode: TestTokenJSON.bytecode, + web3SigningCredential, + keychainId, + constructorArgs: ["100", "token", "2", "TKN"], + gas: estimatedGas, + }); + expect(deployOutToken).toBeTruthy(); + expect(deployOutToken.transactionReceipt).toBeTruthy(); + expect(deployOutToken.transactionReceipt.contractAddress).toBeTruthy(); + const tokenAddress = deployOutToken.transactionReceipt + .contractAddress as string; - const response = await htlcCoordinatorBesuApi.ownHtlcV1(ownHTLCRequest); - t.equal(response.status, 200, "response status is 200 OK"); - t.equal(response.data.success, true, "response success is true"); - t.ok( - response.data, - "pluginHTLCCoordinatorBesu.ownHtlcV1() output is truthy OK", - ); - t.ok( - response.data.out.transactionReceipt, - "pluginHTLCCoordinatorBesu.ownHtlcV1() output.transactionReceipt is truthy OK", - ); + const approveTokensOutput = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Send, + methodName: "approve", + params: [contractAddress, "10"], + gas: estimatedGas, + }); + expect(approveTokensOutput.data.success).toBeTrue(); - t.comment("Get HTLC id"); - const responseTxId = await besuConnectorApi.invokeContractV1({ - contractName: DemoHelperJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "getTxId", - params: [ - firstHighNetWorthAccount, - receiver, - 10, - hashLock, + const responseBalance = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "balanceOf", + params: [firstHighNetWorthAccount], + }); + expect(responseBalance.data.callOutput).toEqual("100"); + + const allowanceOutput = await besuConnectorApi.invokeContractV1({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "allowance", + params: [firstHighNetWorthAccount, contractAddress], + }); + + expect(allowanceOutput.status).toEqual(200); + expect(allowanceOutput.data.callOutput).toEqual("10"); + + const ownHTLCRequest: OwnHTLCRequest = { + htlcPackage: HtlcPackage.BesuErc20, + connectorInstanceId, + keychainId, + constructorArgs: [], + web3SigningCredential, + inputAmount: 10, + outputAmount: 1, expiration, + hashLock, tokenAddress, - ], - gas: estimatedGas, - }); + receiver, + outputNetwork: "BTC", + outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", + gas: estimatedGas, + }; - t.comment("Get counterparty HTLC"); - const counterpartyHTLCRequest: CounterpartyHTLCRequest = { - htlcPackage: HtlcPackage.BesuErc20, - connectorInstanceId, - keychainId, - htlcId: responseTxId.data.callOutput, - web3SigningCredential, - gas: estimatedGas, - }; + const response = await htlcCoordinatorBesuApi.ownHtlcV1(ownHTLCRequest); + expect(response.status).toEqual(200); + expect(response.data).toBeTruthy(); + expect(response.data.success).toBeTrue(); + expect(response.data.out).toBeTruthy(); + expect(response.data.out.transactionReceipt).toBeTruthy(); - const response2 = await htlcCoordinatorBesuApi.counterpartyHtlcV1( - counterpartyHTLCRequest, - ); - t.equal(response2.status, 200, "response status is 200 OK"); - t.equal(response2.data.success, true, "response success is true"); - t.equal(response2.data.callOutput, "1", "the contract status is 1 - Active"); - - t.comment("Get counterparty HTLC"); - const withdrawCounterparty: WithdrawCounterpartyRequest = { - htlcPackage: HtlcPackage.BesuErc20, - connectorInstanceId, - keychainId, - web3SigningCredential, - htlcId: responseTxId.data.callOutput, - secret, - gas: estimatedGas, - }; + const responseTxId = await besuConnectorApi.invokeContractV1({ + contractName: DemoHelperJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "getTxId", + params: [ + firstHighNetWorthAccount, + receiver, + 10, + hashLock, + expiration, + tokenAddress, + ], + gas: estimatedGas, + }); - const response3 = - await htlcCoordinatorBesuApi.withdrawCounterpartyV1(withdrawCounterparty); - t.equal(response3.status, 200, "response status is 200 OK"); - t.equal(response3.data.success, true, "response success is true"); + const counterpartyHTLCRequest: CounterpartyHTLCRequest = { + htlcPackage: HtlcPackage.BesuErc20, + connectorInstanceId, + keychainId, + htlcId: responseTxId.data.callOutput, + web3SigningCredential, + gas: estimatedGas, + }; - t.comment("Get balance of sender account"); - const responseFinalBalanceSender = await connector.invokeContract({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "balanceOf", - params: [firstHighNetWorthAccount], - }); - t.equal( - responseFinalBalanceSender.callOutput, - "90", - "balance of sender account is 90 OK", - ); + const response2 = await htlcCoordinatorBesuApi.counterpartyHtlcV1( + counterpartyHTLCRequest, + ); + expect(response2.status).toBe(200); + expect(response2.data.success).toBe(true); + expect(response2.data.callOutput).toBe("1"); // Contract status is Active - t.comment("Get balance of receiver account"); - const responseFinalBalanceReceiver = await connector.invokeContract({ - contractName: TestTokenJSON.contractName, - keychainId, - signingCredential: web3SigningCredential, - invocationType: EthContractInvocationType.Call, - methodName: "balanceOf", - params: [receiver], - }); - t.equal( - responseFinalBalanceReceiver.callOutput, - "10", - "balance of receiver account is 10 OK", - ); - t.end(); -}); + const withdrawCounterparty: WithdrawCounterpartyRequest = { + htlcPackage: HtlcPackage.BesuErc20, + connectorInstanceId, + keychainId, + web3SigningCredential, + htlcId: responseTxId.data.callOutput, + secret, + gas: estimatedGas, + }; + + const response3 = + await htlcCoordinatorBesuApi.withdrawCounterpartyV1(withdrawCounterparty); + + expect(response3.status).toBe(200); + expect(response3.data.success).toBe(true); -test("AFTER " + testCase, async (t: Test) => { - const pruning = pruneDockerAllIfGithubAction({ logLevel }); - await t.doesNotReject(pruning, "Pruning did not throw OK"); - t.end(); + const responseFinalBalanceSender = await connector.invokeContract({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "balanceOf", + params: [firstHighNetWorthAccount], + }); + expect(responseFinalBalanceSender.callOutput).toEqual("90"); + + const responseFinalBalanceReceiver = await connector.invokeContract({ + contractName: TestTokenJSON.contractName, + keychainId, + signingCredential: web3SigningCredential, + invocationType: EthContractInvocationType.Call, + methodName: "balanceOf", + params: [receiver], + }); + expect(responseFinalBalanceReceiver.callOutput).toEqual("10"); + }); }); diff --git a/jest.config.js b/jest.config.js index 4202250a3e..fd30b8abf5 100644 --- a/jest.config.js +++ b/jest.config.js @@ -28,10 +28,6 @@ module.exports = { `./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/openapi/openapi-validation.test.ts`, `./packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v21.4.1-invoke-contract-json-object.test.ts`, `./packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/manual/geth-alchemy-integration-manual-check.test.ts`, - `./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/counterparty-htlc-endpoint.test.ts`, - `./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/own-htlc-endpoint.test.ts`, - `./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/withdraw-counterparty-endpoint.test.ts`, - `./extensions/cactus-plugin-htlc-coordinator-besu/src/test/typescript/integration/plugin-htlc-coordinator/refund.test.ts`, `./packages/cactus-plugin-keychain-memory-wasm/src/test/typescript/unit/plugin-keychain-memory-wasm.test.ts`, `./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/add-orgs.test.ts`, `./packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/obtain-profiles.test.ts`,