diff --git a/packages/cactus-plugin-ledger-connector-ethereum/src/main/typescript/plugin-ledger-connector-ethereum.ts b/packages/cactus-plugin-ledger-connector-ethereum/src/main/typescript/plugin-ledger-connector-ethereum.ts index 551baf07fb..1b170a18f3 100644 --- a/packages/cactus-plugin-ledger-connector-ethereum/src/main/typescript/plugin-ledger-connector-ethereum.ts +++ b/packages/cactus-plugin-ledger-connector-ethereum/src/main/typescript/plugin-ledger-connector-ethereum.ts @@ -943,6 +943,22 @@ export class PluginLedgerConnectorEthereum ): Promise { Checks.truthy(req, "deployContract() request arg"); + // Validate the keys in the request object + const validKeys = [ + "web3SigningCredential", + "contract", + "constructorArgs", + "gasConfig", + "value", + ]; + const extraKeys = Object.keys(req).filter( + (key) => !validKeys.includes(key), + ); + + if (extraKeys.length > 0) { + throw new Error(`Invalid parameters: ${extraKeys.join(", ")}`); + } + if (isWeb3SigningCredentialNone(req.web3SigningCredential)) { throw new Error(`Cannot deploy contract with pre-signed TX`); } diff --git a/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-contract-deploy-and-invoke-using-json-object-v1.test.ts b/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-contract-deploy-and-invoke-using-json-object-v1.test.ts index 8c6815ad9a..d691d463e8 100644 --- a/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-contract-deploy-and-invoke-using-json-object-v1.test.ts +++ b/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-contract-deploy-and-invoke-using-json-object-v1.test.ts @@ -24,6 +24,7 @@ import { LogLevelDesc, IListenOptions, Servers, + LoggerProvider, } from "@hyperledger/cactus-common"; import { PluginRegistry } from "@hyperledger/cactus-core"; import { Configuration, Constants } from "@hyperledger/cactus-core-api"; @@ -50,6 +51,12 @@ const containerImageName = "ghcr.io/hyperledger/cacti-geth-all-in-one"; const containerImageVersion = "2023-07-27-2a8c48ed6"; describe("Ethereum contract deploy and invoke using keychain tests", () => { + const logLevel: LogLevelDesc = "info"; + const log = LoggerProvider.getOrCreate({ + label: "geth-contract-deploy-and-invoke-using-json-object-v1.test.ts", + level: logLevel, + }); + const keychainEntryKey = uuidV4(); let testEthAccount: { address: HexString; @@ -227,24 +234,23 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { }); test("deployContract without contractJSON should fail", async () => { - try { - await apiClient.deployContract({ + await expect( + apiClient.deployContract({ contract: {} as ContractJsonDefinition, web3SigningCredential: { ethAccount: WHALE_ACCOUNT_ADDRESS, secret: "", type: Web3SigningCredentialType.GethKeychainPassword, }, - }); - fail("Expected deployContract call to fail but it succeeded."); - } catch (error) { - console.log("deployContract failed as expected"); - } + }), + ).rejects.toThrow(); + + log.info("deployContract failed as expected"); }); test("deployContract with additional parameters should fail", async () => { - try { - await apiClient.deployContract({ + await expect( + apiClient.deployContract({ contract: { contractJSON: HelloWorldContractJson, }, @@ -255,11 +261,10 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { }, gas: 1000000, fake: 4, - } as DeployContractV1Request); - fail("Expected deployContract call to fail but it succeeded."); - } catch (error) { - console.log("deployContract failed as expected"); - } + } as DeployContractV1Request), + ).rejects.toThrow(); + + log.info("deployContract failed as expected"); }); ////////////////////////////////// @@ -285,8 +290,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { expect(setNameOut).toBeTruthy(); expect(setNameOut.data).toBeTruthy(); - try { - await apiClient.invokeContractV1({ + await expect( + apiClient.invokeContractV1({ contract: { contractJSON: HelloWorldContractJson, contractAddress, @@ -299,11 +304,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { secret: "", type: Web3SigningCredentialType.GethKeychainPassword, }, - }); - fail("Expected getContractInfoKeychain call to fail but it succeeded."); - } catch (error) { - expect(error).toBeTruthy(); - } + }), + ).rejects.toBeTruthy(); const getNameOut = await apiClient.invokeContractV1({ contract: { @@ -366,8 +368,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { expect(setNameOut).toBeTruthy(); expect(setNameOut.data).toBeTruthy(); - try { - await apiClient.invokeContractV1({ + await expect( + apiClient.invokeContractV1({ contract: { contractJSON: HelloWorldContractJson, contractAddress, @@ -383,11 +385,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { secret: testEthAccount.privateKey, type: Web3SigningCredentialType.PrivateKeyHex, }, - }); - fail("Expected getContractInfoKeychain call to fail but it succeeded."); - } catch (error) { - expect(error).toBeTruthy(); - } + }), + ).rejects.toBeTruthy(); const invokeGetNameOut = await apiClient.invokeContractV1({ contract: { @@ -410,8 +409,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { }); test("invokeContractV1 without methodName should fail", async () => { - try { - await apiClient.invokeContractV1({ + await expect( + apiClient.invokeContractV1({ contract: { contractJSON: HelloWorldContractJson, contractAddress, @@ -423,12 +422,9 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { secret: "", type: Web3SigningCredentialType.GethKeychainPassword, }, - } as InvokeContractV1Request); - fail( - "Expected deployContractSolBytecodeV1 call to fail but it succeeded.", - ); - } catch (error) { - console.log("deployContractSolBytecodeV1 failed as expected"); - } + } as InvokeContractV1Request), + ).rejects.toThrow(); + + log.info("deployContractSolBytecodeV1 failed as expected"); }); }); diff --git a/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-contract-deploy-and-invoke-using-keychain-v1.test.ts b/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-contract-deploy-and-invoke-using-keychain-v1.test.ts index 70f4f4dda7..fd8d193fd1 100644 --- a/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-contract-deploy-and-invoke-using-keychain-v1.test.ts +++ b/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-contract-deploy-and-invoke-using-keychain-v1.test.ts @@ -24,6 +24,7 @@ import { LogLevelDesc, IListenOptions, Servers, + LoggerProvider, } from "@hyperledger/cactus-common"; import { PluginRegistry } from "@hyperledger/cactus-core"; import { Configuration, Constants } from "@hyperledger/cactus-core-api"; @@ -54,6 +55,11 @@ const containerImageName = "ghcr.io/hyperledger/cacti-geth-all-in-one"; const containerImageVersion = "2023-07-27-2a8c48ed6"; describe("Ethereum contract deploy and invoke using keychain tests", () => { + const log = LoggerProvider.getOrCreate({ + label: "geth-contract-deploy-and-invoke-using-keychain-v1.test.ts", + level: testLogLevel, + }); + const keychainEntryKey = uuidV4(); let testEthAccount: { address: HexString; @@ -230,8 +236,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { }); test("deployContract without contractName should fail", async () => { - try { - await apiClient.deployContract({ + await expect( + apiClient.deployContract({ contract: { keychainId: keychainPlugin.getKeychainId(), } as ContractKeychainDefinition, @@ -240,16 +246,15 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { secret: "", type: Web3SigningCredentialType.GethKeychainPassword, }, - }); - fail("Expected deployContract call to fail but it succeeded."); - } catch (error) { - console.log("deployContract failed as expected"); - } + }), + ).rejects.toThrow(); + + log.info("deployContract failed as expected"); }); test("deployContract with additional parameters should fail", async () => { - try { - await apiClient.deployContract({ + await expect( + apiClient.deployContract({ contract: { contractName: HelloWorldContractJson.contractName, keychainId: keychainPlugin.getKeychainId(), @@ -261,11 +266,10 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { }, gas: 1000000, fake: 4, - } as DeployContractV1Request); - fail("Expected deployContract call to fail but it succeeded."); - } catch (error) { - console.log("deployContract failed as expected"); - } + } as DeployContractV1Request), + ).rejects.toThrow(); + + log.info("deployContract failed as expected"); }); ////////////////////////////////// @@ -291,8 +295,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { expect(setNameOut).toBeTruthy(); expect(setNameOut.data).toBeTruthy(); - try { - await apiClient.invokeContractV1({ + await expect( + apiClient.invokeContractV1({ contract: { contractName: HelloWorldContractJson.contractName, keychainId: keychainPlugin.getKeychainId(), @@ -305,11 +309,10 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { secret: "", type: Web3SigningCredentialType.GethKeychainPassword, }, - }); - fail("Expected invokeContractV1 call to fail but it succeeded."); - } catch (error) { - expect(error).toBeTruthy(); - } + }), + ).rejects.toThrow(); + + log.info("invokeContractV1 failed as expected"); const getNameOut = await apiClient.invokeContractV1({ contract: { @@ -385,16 +388,15 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { }); test("runTransactionV1 without transaction config should fail", async () => { - try { - await apiClient.runTransactionV1({ + await expect( + apiClient.runTransactionV1({ web3SigningCredential: { type: Web3SigningCredentialType.None, }, - } as RunTransactionRequest); - fail("Expected runTransactionV1 call to fail but it succeeded."); - } catch (error) { - console.log("runTransactionV1 failed as expected"); - } + } as RunTransactionRequest), + ).rejects.toThrow(); + + log.info("runTransactionV1 failed as expected"); }); test("invoke Web3SigningCredentialType.PrivateKeyHex", async () => { @@ -420,8 +422,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { expect(setNameOut).toBeTruthy(); expect(setNameOut.data).toBeTruthy(); - try { - await apiClient.invokeContractV1({ + await expect( + apiClient.invokeContractV1({ contract: { contractName: HelloWorldContractJson.contractName, keychainId: keychainPlugin.getKeychainId(), @@ -437,11 +439,10 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { secret: testEthAccount.privateKey, type: Web3SigningCredentialType.PrivateKeyHex, }, - }); - fail("Expected invokeContractV1 call to fail but it succeeded."); - } catch (error) { - expect(error).toBeTruthy(); - } + }), + ).rejects.toThrow(); + + log.info("invokeContractV1 failed as expected"); const invokeGetNameOut = await apiClient.invokeContractV1({ contract: { @@ -490,8 +491,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { expect(setNameOut).toBeTruthy(); expect(setNameOut.data).toBeTruthy(); - try { - await apiClient.invokeContractV1({ + await expect( + apiClient.invokeContractV1({ contract: { contractName: HelloWorldContractJson.contractName, keychainId: keychainPlugin.getKeychainId(), @@ -507,11 +508,10 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { secret: "", type: Web3SigningCredentialType.GethKeychainPassword, }, - }); - fail("Expected invokeContractV1 call to fail but it succeeded."); - } catch (error) { - expect(error).toBeTruthy(); - } + }), + ).rejects.toThrow(); + + log.info("invokeContractV1 failed as expected"); const invokeGetNameOut = await apiClient.invokeContractV1({ contract: { @@ -530,8 +530,8 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { }); test("invokeContractV1 without methodName should fail", async () => { - try { - await apiClient.invokeContractV1({ + await expect( + apiClient.invokeContractV1({ contract: { contractName: HelloWorldContractJson.contractName, keychainId: keychainPlugin.getKeychainId(), @@ -543,11 +543,10 @@ describe("Ethereum contract deploy and invoke using keychain tests", () => { secret: "", type: Web3SigningCredentialType.GethKeychainPassword, }, - } as InvokeContractV1Request); - fail("Expected invokeContractV1 call to fail but it succeeded."); - } catch (error) { - console.log("invokeContractV1 failed as expected"); - } + } as InvokeContractV1Request), + ).rejects.toThrow(); + + log.info("invokeContractV1 failed as expected"); }); // @todo - move to separate test suite diff --git a/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-invoke-web3-method-v1.test.ts b/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-invoke-web3-method-v1.test.ts index 5cf120279a..e46e27cbb8 100644 --- a/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-invoke-web3-method-v1.test.ts +++ b/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-invoke-web3-method-v1.test.ts @@ -167,43 +167,37 @@ describe("invokeRawWeb3EthMethod Tests", () => { }); test("invokeRawWeb3EthMethod with missing arg throws error (getBlock)", async () => { - try { - const connectorResponse = connector.invokeRawWeb3EthMethod({ - methodName: "getBlock", - }); - - await connectorResponse; - fail("Calling getBlock with missing argument should throw an error"); - } catch (err) { - expect(err).toBeTruthy(); - } + // Should "missing arg" mean no method name is provided? Because the only required parameter is methodName + // Have taken out methodName from the invocation of the method as I'm guessing that is what this test if supposed to check for? + // It will also fail if methodName is an empty string. + // Or should we just delete this test ? + await expect( + // @ts-expect-error: the script fails otherwise + connector.invokeRawWeb3EthMethod(), + ).rejects.toBeTruthy(); }); test("invokeRawWeb3EthMethod with invalid arg throws error (getBlock)", async () => { - try { - const connectorResponse = connector.invokeRawWeb3EthMethod({ + await expect( + connector.invokeRawWeb3EthMethod({ methodName: "getBlock", params: ["foo"], - }); + }), + ).rejects.toThrow(); - await connectorResponse; - fail("Calling getBlock with argument should throw an error"); - } catch (err) { - expect(err).toBeTruthy(); - } + log.info( + "Calling getBlock with an invalid argument threw an error as expected", + ); }); test("invokeRawWeb3EthMethod with non existing method throws error", async () => { - try { - const connectorResponse = connector.invokeRawWeb3EthMethod({ + await expect( + connector.invokeRawWeb3EthMethod({ methodName: "foo", params: ["foo"], - }); + }), + ).rejects.toThrow(); - await connectorResponse; - fail("Calling non existing method should throw an error"); - } catch (err) { - expect(err).toBeTruthy(); - } + log.info("Calling non-existing method threw an error as expected"); }); }); diff --git a/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-transact-and-gas-fees.test.ts b/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-transact-and-gas-fees.test.ts index 4b8cadff0e..830efe8eab 100644 --- a/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-transact-and-gas-fees.test.ts +++ b/packages/cactus-plugin-ledger-connector-ethereum/src/test/typescript/integration/geth-transact-and-gas-fees.test.ts @@ -22,6 +22,7 @@ import { LogLevelDesc, IListenOptions, Servers, + LoggerProvider, } from "@hyperledger/cactus-common"; import { PluginRegistry } from "@hyperledger/cactus-core"; import { Configuration, Constants } from "@hyperledger/cactus-core-api"; @@ -41,6 +42,11 @@ const containerImageName = "ghcr.io/hyperledger/cacti-geth-all-in-one"; const containerImageVersion = "2023-07-27-2a8c48ed6"; describe("Running ethereum transactions with different gas configurations", () => { + const log = LoggerProvider.getOrCreate({ + label: "geth-contract-deploy-and-invoke-using-keychain-v1.test.ts", + level: testLogLevel, + }); + let web3: InstanceType, addressInfo, address: string, @@ -133,8 +139,8 @@ describe("Running ethereum transactions with different gas configurations", () = web3.utils.toWei(2, "gwei"), ); - try { - await apiClient.runTransactionV1({ + await expect( + apiClient.runTransactionV1({ web3SigningCredential: { ethAccount: WHALE_ACCOUNT_ADDRESS, secret: "", @@ -149,13 +155,10 @@ describe("Running ethereum transactions with different gas configurations", () = maxFeePerGas: maxFee, }, }, - }); - fail( - "Expected runTransactionV1 with mixed config to fail but it succeeded.", - ); - } catch (error) { - console.log("runTransactionV1 with mixed config failed as expected"); - } + }), + ).rejects.toThrow(); + + log.info("runTransactionV1 with mixed config failed as expected"); const balance = await web3.eth.getBalance(testEthAccount.address); expect(balance.toString()).toEqual("0");