From 94d246c8881edfad2cf617d67340bf4ae60b6579 Mon Sep 17 00:00:00 2001 From: danielailie Date: Thu, 30 Jan 2025 13:49:07 +0200 Subject: [PATCH] Update tests to use account.signTransaction --- src/abi/interaction.local.net.spec.ts | 121 ++++----- src/abi/interaction.spec.ts | 50 ++-- src/abi/smartContract.local.net.spec.ts | 120 ++++----- src/abi/smartContract.spec.ts | 35 ++- .../smartContractResults.local.net.spec.ts | 38 ++- src/accounts/account.spec.ts | 5 +- src/core/message.spec.ts | 19 +- src/core/transaction.spec.ts | 233 ++++++++---------- .../accountAwaiter.dev.net.spec.ts | 13 +- .../apiNetworkProvider.dev.net.spec.ts | 30 +-- .../proxyNetworkProvider.dev.net.spec.ts | 31 +-- src/proto/serializer.spec.ts | 54 ++-- .../relayedTransactionsFactory.spec.ts | 72 +++--- src/testutils/utils.ts | 15 +- 14 files changed, 346 insertions(+), 490 deletions(-) diff --git a/src/abi/interaction.local.net.spec.ts b/src/abi/interaction.local.net.spec.ts index 7ef19a40..7169a517 100644 --- a/src/abi/interaction.local.net.spec.ts +++ b/src/abi/interaction.local.net.spec.ts @@ -1,8 +1,8 @@ import BigNumber from "bignumber.js"; import { assert } from "chai"; import { promises } from "fs"; +import { Account } from "../accounts"; import { Transaction } from "../core/transaction"; -import { TransactionComputer } from "../core/transactionComputer"; import { TransactionsFactoryConfig } from "../core/transactionsFactoryConfig"; import { TransactionWatcher } from "../core/transactionWatcher"; import { @@ -10,19 +10,18 @@ import { SmartContractTransactionsFactory, SmartContractTransactionsOutcomeParser, } from "../smartContracts"; -import { loadAbiRegistry, loadTestWallets, prepareDeployment, TestWallet } from "../testutils"; +import { loadAbiRegistry, prepareDeployment } from "../testutils"; import { createLocalnetProvider } from "../testutils/networkProviders"; +import { getTestWalletsPath } from "../testutils/utils"; import { Interaction } from "./interaction"; import { SmartContract } from "./smartContract"; import { ManagedDecimalValue } from "./typesystem"; - describe("test smart contract interactor", function () { let provider = createLocalnetProvider(); - let alice: TestWallet; - const transactionComputer = new TransactionComputer(); + let alice: Account; before(async function () { - ({ alice } = await loadTestWallets()); + alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); }); it("should interact with 'answer' (local testnet) using the SmartContractTransactionsFactory", async function () { @@ -31,7 +30,6 @@ describe("test smart contract interactor", function () { let abiRegistry = await loadAbiRegistry("src/testdata/answer.abi.json"); let network = await provider.getNetworkConfig(); - await alice.sync(provider); const config = new TransactionsFactoryConfig({ chainID: network.chainID }); const factory = new SmartContractTransactionsFactory({ @@ -45,14 +43,12 @@ describe("test smart contract interactor", function () { bytecode: bytecode, gasLimit: 3000000n, }); - deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); + deployTransaction.nonce = BigInt(alice.nonce.valueOf()); - deployTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), - ); + deployTransaction.signature = alice.signTransaction(deployTransaction); - const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); - alice.account.incrementNonce(); + const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); + alice.incrementNonce(); const transactionCompletionAwaiter = new TransactionWatcher({ getTransaction: async (hash: string) => { @@ -89,12 +85,10 @@ describe("test smart contract interactor", function () { function: "getUltimateAnswer", gasLimit: 3000000n, }); - transaction.nonce = BigInt(alice.account.nonce.valueOf()); - transaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(transaction)), - ); + transaction.nonce = BigInt(alice.nonce.valueOf()); + transaction.signature = alice.signTransaction(transaction); - alice.account.incrementNonce(); + alice.incrementNonce(); await provider.sendTransaction(transaction); @@ -104,12 +98,10 @@ describe("test smart contract interactor", function () { function: "getUltimateAnswer", gasLimit: 3000000n, }); - transaction.nonce = BigInt(alice.account.nonce.valueOf()); - transaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(transaction)), - ); + transaction.nonce = BigInt(alice.nonce.valueOf()); + transaction.signature = alice.signTransaction(transaction); - alice.account.incrementNonce(); + alice.incrementNonce(); const executeTxHash = await provider.sendTransaction(transaction); transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(executeTxHash); @@ -132,7 +124,6 @@ describe("test smart contract interactor", function () { }); let network = await provider.getNetworkConfig(); - await alice.sync(provider); // Deploy the contract let deployTransaction = await prepareDeployment({ @@ -159,7 +150,7 @@ describe("test smart contract interactor", function () { // returnEgld() let returnEgldTransaction = returnEgldInteraction .withSender(alice.address) - .useThenIncrementNonceOf(alice.account) + .useThenIncrementNonceOf(alice) .buildTransaction(); let additionInteraction = contract.methods @@ -172,7 +163,7 @@ describe("test smart contract interactor", function () { // addition() let additionTransaction = additionInteraction .withSender(alice.address) - .useThenIncrementNonceOf(alice.account) + .useThenIncrementNonceOf(alice) .buildTransaction(); // log @@ -186,7 +177,7 @@ describe("test smart contract interactor", function () { // mdLn() let mdLnTransaction = mdLnInteraction .withSender(alice.address) - .useThenIncrementNonceOf(alice.account) + .useThenIncrementNonceOf(alice) .buildTransaction(); let additionVarInteraction = contract.methods @@ -202,7 +193,7 @@ describe("test smart contract interactor", function () { // addition() let additionVarTransaction = additionVarInteraction .withSender(alice.address) - .useThenIncrementNonceOf(alice.account) + .useThenIncrementNonceOf(alice) .buildTransaction(); let lnVarInteraction = contract.methods @@ -215,7 +206,7 @@ describe("test smart contract interactor", function () { // managed_decimal_ln_var() let lnVarTransaction = lnVarInteraction .withSender(alice.address) - .useThenIncrementNonceOf(alice.account) + .useThenIncrementNonceOf(alice) .buildTransaction(); // returnEgld() @@ -266,7 +257,6 @@ describe("test smart contract interactor", function () { let abiRegistry = await loadAbiRegistry("src/testdata/counter.abi.json"); let network = await provider.getNetworkConfig(); - await alice.sync(provider); const config = new TransactionsFactoryConfig({ chainID: network.chainID }); const factory = new SmartContractTransactionsFactory({ @@ -281,14 +271,12 @@ describe("test smart contract interactor", function () { bytecode: bytecode, gasLimit: 3000000n, }); - deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); + deployTransaction.nonce = BigInt(alice.nonce.valueOf()); - deployTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), - ); + deployTransaction.signature = alice.signTransaction(deployTransaction); - const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); - alice.account.incrementNonce(); + const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); + alice.incrementNonce(); const transactionCompletionAwaiter = new TransactionWatcher({ getTransaction: async (hash: string) => { @@ -312,13 +300,11 @@ describe("test smart contract interactor", function () { function: "increment", gasLimit: 3000000n, }); - incrementTransaction.nonce = BigInt(alice.account.nonce.valueOf()); + incrementTransaction.nonce = BigInt(alice.nonce.valueOf()); - incrementTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(incrementTransaction)), - ); + incrementTransaction.signature = alice.signTransaction(incrementTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); // Query "get()" const query = queryController.createQuery({ @@ -341,19 +327,15 @@ describe("test smart contract interactor", function () { function: "decrement", gasLimit: 3000000n, }); - decrementTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - decrementTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(decrementTransaction)), - ); + decrementTransaction.nonce = alice.nonce; + decrementTransaction.signature = alice.signTransaction(decrementTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); await provider.sendTransaction(decrementTransaction); - decrementTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - decrementTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(decrementTransaction)), - ); + decrementTransaction.nonce = alice.nonce; + decrementTransaction.signature = alice.signTransaction(decrementTransaction); const decrementTxHash = await provider.sendTransaction(decrementTransaction); transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(decrementTxHash); @@ -367,7 +349,6 @@ describe("test smart contract interactor", function () { let parser = new SmartContractTransactionsOutcomeParser({ abi: abiRegistry }); let network = await provider.getNetworkConfig(); - await alice.sync(provider); const config = new TransactionsFactoryConfig({ chainID: network.chainID }); const factory = new SmartContractTransactionsFactory({ @@ -382,14 +363,12 @@ describe("test smart contract interactor", function () { bytecode: bytecode, gasLimit: 100000000n, }); - deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); + deployTransaction.nonce = alice.nonce; - deployTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), - ); + deployTransaction.signature = alice.signTransaction(deployTransaction); - const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); - alice.account.incrementNonce(); + const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); + alice.incrementNonce(); const transactionCompletionAwaiter = new TransactionWatcher({ getTransaction: async (hash: string) => { @@ -409,12 +388,10 @@ describe("test smart contract interactor", function () { arguments: ["lucky", "EGLD", 1, null, null, 1, null, null], gasLimit: 30000000n, }); - startTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - startTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(startTransaction)), - ); + startTransaction.nonce = alice.nonce; + startTransaction.signature = alice.signTransaction(startTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); const startTxHash = await provider.sendTransaction(startTransaction); transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(startTxHash); @@ -429,12 +406,10 @@ describe("test smart contract interactor", function () { arguments: ["lucky"], gasLimit: 5000000n, }); - lotteryStatusTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - lotteryStatusTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(lotteryStatusTransaction)), - ); + lotteryStatusTransaction.nonce = alice.nonce; + lotteryStatusTransaction.signature = alice.signTransaction(lotteryStatusTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); const statusTxHash = await provider.sendTransaction(lotteryStatusTransaction); transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(statusTxHash); @@ -450,12 +425,10 @@ describe("test smart contract interactor", function () { arguments: ["lucky"], gasLimit: 5000000n, }); - lotteryInfoTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - lotteryInfoTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(lotteryInfoTransaction)), - ); + lotteryInfoTransaction.nonce = alice.nonce; + lotteryInfoTransaction.signature = alice.signTransaction(lotteryInfoTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); const infoTxHash = await provider.sendTransaction(lotteryInfoTransaction); transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(infoTxHash); @@ -477,10 +450,10 @@ describe("test smart contract interactor", function () { }); }); - async function signTransaction(options: { transaction: Transaction; wallet: TestWallet }) { + async function signTransaction(options: { transaction: Transaction; wallet: Account }) { const transaction = options.transaction; const wallet = options.wallet; - transaction.signature = await wallet.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = wallet.signTransaction(transaction); } }); diff --git a/src/abi/interaction.spec.ts b/src/abi/interaction.spec.ts index edfe365a..9c2e2c59 100644 --- a/src/abi/interaction.spec.ts +++ b/src/abi/interaction.spec.ts @@ -1,18 +1,13 @@ import BigNumber from "bignumber.js"; import { assert } from "chai"; +import { Account } from "../accounts"; import { Address } from "../core/address"; import { SmartContractQueryResponse } from "../core/smartContractQuery"; import { Token, TokenTransfer } from "../core/tokens"; import { Transaction } from "../core/transaction"; -import { TransactionComputer } from "../core/transactionComputer"; import { SmartContractController } from "../smartContracts"; -import { - loadAbiRegistry, - loadTestWallets, - MockNetworkProvider, - setupUnitTestWatcherTimeouts, - TestWallet, -} from "../testutils"; +import { loadAbiRegistry, MockNetworkProvider, setupUnitTestWatcherTimeouts } from "../testutils"; +import { getTestWalletsPath } from "../testutils/utils"; import { ContractFunction } from "./function"; import { Interaction } from "./interaction"; import { SmartContract } from "./smartContract"; @@ -21,11 +16,10 @@ import { BigUIntValue, BytesValue, OptionalValue, OptionValue, TokenIdentifierVa describe("test smart contract interactor", function () { let dummyAddress = new Address("erd1qqqqqqqqqqqqqpgqak8zt22wl2ph4tswtyc39namqx6ysa2sd8ss4xmlj3"); let provider = new MockNetworkProvider(); - let alice: TestWallet; - const transactionComputer = new TransactionComputer(); + let alice: Account; before(async function () { - ({ alice } = await loadTestWallets()); + alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); }); it("should set transaction fields", async function () { @@ -249,7 +243,7 @@ describe("test smart contract interactor", function () { // Execute, do not wait for execution let transaction = interaction.withSender(alice.address).withNonce(0n).buildTransaction(); transaction.sender = alice.address; - transaction.signature = await alice.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = alice.signTransaction(transaction); let hash = await provider.sendTransaction(transaction); assert.equal(transaction.nonce, 0n); assert.equal(transaction.data.toString(), "getUltimateAnswer"); @@ -257,7 +251,7 @@ describe("test smart contract interactor", function () { transaction = interaction.withNonce(1n).buildTransaction(); transaction.sender = alice.address; - transaction.signature = await alice.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = alice.signTransaction(transaction); hash = await provider.sendTransaction(transaction); assert.equal(transaction.nonce, 1n); assert.equal(hash, "ad513ce7c5d371d30e48f073326899766736eac1ac231d847d45bc3facbcb496"); @@ -265,7 +259,7 @@ describe("test smart contract interactor", function () { // Execute, and wait for execution transaction = interaction.withNonce(2n).buildTransaction(); transaction.sender = alice.address; - transaction.signature = await alice.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = alice.signTransaction(transaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@2bs", "getUltimateAnswer"); hash = await provider.sendTransaction(transaction); let responseExecute = await controller.awaitCompletedExecute(hash); @@ -314,9 +308,7 @@ describe("test smart contract interactor", function () { .withChainID("mock") .buildTransaction(); - incrementTransaction.signature = await alice.signer.sign( - transactionComputer.computeBytesForSigning(incrementTransaction), - ); + incrementTransaction.signature = alice.signTransaction(incrementTransaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@08", "increment"); let hash = await provider.sendTransaction(incrementTransaction); let responseExecute = await controller.awaitCompletedExecute(hash); @@ -330,22 +322,16 @@ describe("test smart contract interactor", function () { .withChainID("mock") .buildTransaction(); - decrementTransaction.signature = await alice.signer.sign( - transactionComputer.computeBytesForSigning(decrementTransaction), - ); + decrementTransaction.signature = alice.signTransaction(decrementTransaction); await provider.sendTransaction(decrementTransaction); // Decrement #2 decrementTransaction = decrementInteraction.withNonce(16n).buildTransaction(); - decrementTransaction.signature = await alice.signer.sign( - transactionComputer.computeBytesForSigning(decrementTransaction), - ); + decrementTransaction.signature = alice.signTransaction(decrementTransaction); await provider.sendTransaction(decrementTransaction); // Decrement #3 decrementTransaction = decrementInteraction.withNonce(17n).buildTransaction(); - decrementTransaction.signature = await alice.signer.sign( - transactionComputer.computeBytesForSigning(decrementTransaction), - ); + decrementTransaction.signature = alice.signTransaction(decrementTransaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@05", "decrement"); hash = await provider.sendTransaction(decrementTransaction); responseExecute = await controller.awaitCompletedExecute(hash); @@ -386,9 +372,7 @@ describe("test smart contract interactor", function () { .withChainID("mock") .buildTransaction(); - startTransaction.signature = await alice.signer.sign( - transactionComputer.computeBytesForSigning(startTransaction), - ); + startTransaction.signature = alice.signTransaction(startTransaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b", "start"); let hash = await provider.sendTransaction(startTransaction); @@ -405,9 +389,7 @@ describe("test smart contract interactor", function () { .withChainID("mock") .buildTransaction(); - statusTransaction.signature = await alice.signer.sign( - transactionComputer.computeBytesForSigning(statusTransaction), - ); + statusTransaction.signature = alice.signTransaction(statusTransaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult("@6f6b@01", "status"); hash = await provider.sendTransaction(startTransaction); @@ -425,9 +407,7 @@ describe("test smart contract interactor", function () { .withChainID("mock") .buildTransaction(); - getLotteryInfoTransaction.signature = await alice.signer.sign( - transactionComputer.computeBytesForSigning(getLotteryInfoTransaction), - ); + getLotteryInfoTransaction.signature = alice.signTransaction(getLotteryInfoTransaction); provider.mockGetTransactionWithAnyHashAsNotarizedWithOneResult( "@6f6b@0000000b6c75636b792d746f6b656e000000010100000000000000005fc2b9dbffffffff00000001640000000a140ec80fa7ee88000000", "getLotteryInfo", diff --git a/src/abi/smartContract.local.net.spec.ts b/src/abi/smartContract.local.net.spec.ts index 336a0b81..cec28ed1 100644 --- a/src/abi/smartContract.local.net.spec.ts +++ b/src/abi/smartContract.local.net.spec.ts @@ -1,7 +1,7 @@ import { assert } from "chai"; import { promises } from "fs"; +import { Account } from "../accounts"; import { Logger } from "../core/logger"; -import { TransactionComputer } from "../core/transactionComputer"; import { TransactionsFactoryConfig } from "../core/transactionsFactoryConfig"; import { TransactionWatcher } from "../core/transactionWatcher"; import { @@ -9,9 +9,8 @@ import { SmartContractTransactionsFactory, SmartContractTransactionsOutcomeParser, } from "../smartContracts"; -import { stringifyBigIntJSON } from "../testutils"; import { createLocalnetProvider } from "../testutils/networkProviders"; -import { loadTestWallets, TestWallet } from "../testutils/wallets"; +import { getTestWalletsPath, stringifyBigIntJSON } from "../testutils/utils"; import { decodeUnsignedNumber } from "./codec"; import { SmartContract } from "./smartContract"; import { @@ -25,14 +24,15 @@ import { } from "./typesystem"; describe("test on local testnet", function () { - let alice: TestWallet, bob: TestWallet, carol: TestWallet; + let alice: Account, bob: Account, carol: Account; let provider = createLocalnetProvider(); let watcher: TransactionWatcher; let parser: SmartContractTransactionsOutcomeParser; - const transactionComputer = new TransactionComputer(); before(async function () { - ({ alice, bob, carol } = await loadTestWallets()); + alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); + bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); + carol = await Account.newFromPem(`${getTestWalletsPath()}/carol.pem`); watcher = new TransactionWatcher({ getTransaction: async (hash: string) => { @@ -49,7 +49,6 @@ describe("test on local testnet", function () { TransactionWatcher.DefaultTimeout = 50000; let network = await provider.getNetworkConfig(); - await alice.sync(provider); const config = new TransactionsFactoryConfig({ chainID: network.chainID }); const factory = new SmartContractTransactionsFactory({ config: config }); @@ -60,26 +59,22 @@ describe("test on local testnet", function () { bytecode: bytecode, gasLimit: 4000000n, }); - deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); + deployTransaction.nonce = BigInt(alice.nonce.valueOf()); - deployTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), - ); + deployTransaction.signature = alice.signTransaction(deployTransaction); - const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); - alice.account.incrementNonce(); + const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); + alice.incrementNonce(); const smartContractCallTransaction = factory.createTransactionForExecute(alice.address, { contract: contractAddress, function: "increment", gasLimit: 4000000n, }); - smartContractCallTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - smartContractCallTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(smartContractCallTransaction)), - ); + smartContractCallTransaction.nonce = alice.nonce; + smartContractCallTransaction.signature = alice.signTransaction(smartContractCallTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); const simulateOne = factory.createTransactionForExecute(alice.address, { function: "increment", @@ -87,12 +82,10 @@ describe("test on local testnet", function () { gasLimit: 200000n, }); - simulateOne.nonce = BigInt(alice.account.nonce.valueOf()); - simulateOne.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(simulateOne)), - ); + simulateOne.nonce = alice.nonce; + simulateOne.signature = alice.signTransaction(simulateOne); - alice.account.incrementNonce(); + alice.incrementNonce(); const simulateTwo = factory.createTransactionForExecute(alice.address, { function: "foobar", @@ -100,12 +93,10 @@ describe("test on local testnet", function () { gasLimit: 700000n, }); - simulateTwo.nonce = BigInt(alice.account.nonce.valueOf()); - simulateTwo.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(simulateTwo)), - ); + simulateTwo.nonce = alice.nonce; + simulateTwo.signature = alice.signTransaction(simulateTwo); - alice.account.incrementNonce(); + alice.incrementNonce(); // Broadcast & execute const deployTxHash = await provider.sendTransaction(deployTransaction); @@ -133,7 +124,6 @@ describe("test on local testnet", function () { TransactionWatcher.DefaultTimeout = 50000; let network = await provider.getNetworkConfig(); - await alice.sync(provider); const config = new TransactionsFactoryConfig({ chainID: network.chainID }); const factory = new SmartContractTransactionsFactory({ config: config }); @@ -144,38 +134,32 @@ describe("test on local testnet", function () { bytecode: bytecode, gasLimit: 3000000n, }); - deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); + deployTransaction.nonce = alice.nonce; - deployTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), - ); + deployTransaction.signature = alice.signTransaction(deployTransaction); - const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); - alice.account.incrementNonce(); + const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); + alice.incrementNonce(); const firstScCallTransaction = factory.createTransactionForExecute(alice.address, { contract: contractAddress, function: "increment", gasLimit: 3000000n, }); - firstScCallTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - firstScCallTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(firstScCallTransaction)), - ); + firstScCallTransaction.nonce = alice.nonce; + firstScCallTransaction.signature = alice.signTransaction(firstScCallTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); const secondScCallTransaction = factory.createTransactionForExecute(alice.address, { contract: contractAddress, function: "increment", gasLimit: 3000000n, }); - secondScCallTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - secondScCallTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(secondScCallTransaction)), - ); + secondScCallTransaction.nonce = alice.nonce; + secondScCallTransaction.signature = alice.signTransaction(secondScCallTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); // Broadcast & execute const deployTxHash = await provider.sendTransaction(deployTransaction); @@ -210,7 +194,6 @@ describe("test on local testnet", function () { TransactionWatcher.DefaultTimeout = 50000; let network = await provider.getNetworkConfig(); - await alice.sync(provider); const config = new TransactionsFactoryConfig({ chainID: network.chainID }); const factory = new SmartContractTransactionsFactory({ config: config }); @@ -222,13 +205,11 @@ describe("test on local testnet", function () { gasLimit: 50000000n, arguments: [new U32Value(10000)], }); - deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - deployTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), - ); + deployTransaction.nonce = alice.nonce; + deployTransaction.signature = alice.signTransaction(deployTransaction); - const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); - alice.account.incrementNonce(); + const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); + alice.incrementNonce(); const transactionMintBob = factory.createTransactionForExecute(alice.address, { contract: contractAddress, @@ -236,12 +217,10 @@ describe("test on local testnet", function () { gasLimit: 9000000n, arguments: [new AddressValue(bob.address), new U32Value(1000)], }); - transactionMintBob.nonce = BigInt(alice.account.nonce.valueOf()); - transactionMintBob.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(transactionMintBob)), - ); + transactionMintBob.nonce = alice.nonce; + transactionMintBob.signature = alice.signTransaction(transactionMintBob); - alice.account.incrementNonce(); + alice.incrementNonce(); const transactionMintCarol = factory.createTransactionForExecute(alice.address, { contract: contractAddress, @@ -249,12 +228,10 @@ describe("test on local testnet", function () { gasLimit: 9000000n, arguments: [new AddressValue(carol.address), new U32Value(1500)], }); - transactionMintCarol.nonce = BigInt(alice.account.nonce.valueOf()); - transactionMintCarol.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(transactionMintCarol)), - ); + transactionMintCarol.nonce = alice.nonce; + transactionMintCarol.signature = alice.signTransaction(transactionMintCarol); - alice.account.incrementNonce(); + alice.incrementNonce(); // Broadcast & execute const deployTxHash = await provider.sendTransaction(deployTransaction); @@ -312,7 +289,6 @@ describe("test on local testnet", function () { TransactionWatcher.DefaultTimeout = 50000; let network = await provider.getNetworkConfig(); - await alice.sync(provider); const config = new TransactionsFactoryConfig({ chainID: network.chainID }); const factory = new SmartContractTransactionsFactory({ config: config }); @@ -323,14 +299,12 @@ describe("test on local testnet", function () { bytecode: bytecode, gasLimit: 50000000n, }); - deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); + deployTransaction.nonce = alice.nonce; - deployTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), - ); + deployTransaction.signature = alice.signTransaction(deployTransaction); - const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); - alice.account.incrementNonce(); + const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); + alice.incrementNonce(); const startTransaction = factory.createTransactionForExecute(alice.address, { contract: contractAddress, @@ -348,12 +322,10 @@ describe("test on local testnet", function () { OptionalValue.newMissing(), ], }); - startTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - startTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(startTransaction)), - ); + startTransaction.nonce = alice.nonce; + startTransaction.signature = alice.signTransaction(startTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); // Broadcast & execute const deployTx = await provider.sendTransaction(deployTransaction); diff --git a/src/abi/smartContract.spec.ts b/src/abi/smartContract.spec.ts index c8f26409..c7418262 100644 --- a/src/abi/smartContract.spec.ts +++ b/src/abi/smartContract.spec.ts @@ -1,14 +1,14 @@ import { assert } from "chai"; +import { Account } from "../accounts"; import { Address } from "../core/address"; import { TransactionComputer } from "../core/transactionComputer"; import { TransactionStatus } from "../core/transactionStatus"; import { TransactionWatcher } from "../core/transactionWatcher"; import { - loadTestWallets, + getTestWalletsPath, MarkCompleted, MockNetworkProvider, setupUnitTestWatcherTimeouts, - TestWallet, Wait, } from "../testutils"; import { Code } from "./code"; @@ -20,11 +20,11 @@ import { BytesValue } from "./typesystem/bytes"; describe("test contract", () => { let provider = new MockNetworkProvider(); let chainID = "test"; - let alice: TestWallet; + let alice: Account; const computer = new TransactionComputer(); before(async function () { - ({ alice } = await loadTestWallets()); + alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); }); it("should compute contract address", async () => { @@ -52,16 +52,11 @@ describe("test contract", () => { deployer: alice.address, }); - provider.mockUpdateAccount(alice.address, (account) => { - account.nonce = 42n; - }); - - await alice.sync(provider); - deployTransaction.nonce = alice.account.nonce; + deployTransaction.nonce = alice.nonce; assert.equal(deployTransaction.data.toString(), "01020304@0500@0100"); assert.equal(deployTransaction.gasLimit, 1000000n); - assert.equal(deployTransaction.nonce, 42n); + assert.equal(deployTransaction.nonce, alice.nonce); // Compute & set the contract address contract.setAddress(SmartContract.computeAddress(alice.address, 42n)); @@ -71,7 +66,7 @@ describe("test contract", () => { ); // Sign the transaction - deployTransaction.signature = await alice.signer.sign(computer.computeBytesForSigning(deployTransaction)); + deployTransaction.signature = alice.signTransaction(deployTransaction); // Now let's broadcast the deploy transaction, and wait for its execution. let hash = await provider.sendTransaction(deployTransaction); @@ -119,10 +114,9 @@ describe("test contract", () => { caller: alice.address, }); - await alice.sync(provider); - callTransactionOne.nonce = alice.account.nonce; - alice.account.incrementNonce(); - callTransactionTwo.nonce = alice.account.nonce; + callTransactionOne.nonce = alice.nonce; + alice.incrementNonce(); + callTransactionTwo.nonce = alice.nonce; assert.equal(callTransactionOne.nonce, 42n); assert.equal(callTransactionOne.data.toString(), "helloEarth@05@0123"); @@ -132,8 +126,8 @@ describe("test contract", () => { assert.equal(callTransactionTwo.gasLimit, 1500000n); // Sign transactions, broadcast them - callTransactionOne.signature = await alice.signer.sign(computer.computeBytesForSigning(callTransactionOne)); - callTransactionTwo.signature = await alice.signer.sign(computer.computeBytesForSigning(callTransactionTwo)); + callTransactionOne.signature = alice.signTransaction(callTransactionOne); + callTransactionTwo.signature = alice.signTransaction(callTransactionTwo); let hashOne = await provider.sendTransaction(callTransactionOne); let hashTwo = await provider.sendTransaction(callTransactionTwo); @@ -179,15 +173,14 @@ describe("test contract", () => { account.nonce = 42n; }); - await alice.sync(provider); - deployTransaction.nonce = alice.account.nonce; + deployTransaction.nonce = alice.nonce; assert.equal(deployTransaction.data.toString(), "upgradeContract@01020304@0100"); assert.equal(deployTransaction.gasLimit, 1000000n); assert.equal(deployTransaction.nonce, 42n); // Sign the transaction - deployTransaction.signature = await alice.signer.sign(computer.computeBytesForSigning(deployTransaction)); + deployTransaction.signature = alice.signTransaction(deployTransaction); // Now let's broadcast the deploy transaction, and wait for its execution. let hash = await provider.sendTransaction(deployTransaction); diff --git a/src/abi/smartContractResults.local.net.spec.ts b/src/abi/smartContractResults.local.net.spec.ts index b681f19b..e78bfd1f 100644 --- a/src/abi/smartContractResults.local.net.spec.ts +++ b/src/abi/smartContractResults.local.net.spec.ts @@ -1,23 +1,22 @@ import { assert } from "chai"; import { promises } from "fs"; -import { TransactionComputer } from "../core/transactionComputer"; +import { Account } from "../accounts"; import { TransactionsFactoryConfig } from "../core/transactionsFactoryConfig"; import { TransactionWatcher } from "../core/transactionWatcher"; import { SmartContractTransactionsFactory, SmartContractTransactionsOutcomeParser } from "../smartContracts"; -import { loadTestWallets, prepareDeployment, TestWallet } from "../testutils"; +import { getTestWalletsPath, prepareDeployment } from "../testutils"; import { createLocalnetProvider } from "../testutils/networkProviders"; import { ContractFunction } from "./function"; import { SmartContract } from "./smartContract"; describe("fetch transactions from local testnet", function () { - let alice: TestWallet; + let alice: Account; let provider = createLocalnetProvider(); let watcher: TransactionWatcher; let parser: SmartContractTransactionsOutcomeParser; - const transactionComputer = new TransactionComputer(); before(async function () { - ({ alice } = await loadTestWallets()); + alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); watcher = new TransactionWatcher({ getTransaction: async (hash: string) => { return await provider.getTransaction(hash); @@ -34,7 +33,6 @@ describe("fetch transactions from local testnet", function () { TransactionWatcher.DefaultTimeout = 50000; let network = await provider.getNetworkConfig(); - await alice.sync(provider); // Deploy let contract = new SmartContract({}); @@ -56,12 +54,10 @@ describe("fetch transactions from local testnet", function () { caller: alice.address, }); - transactionIncrement.nonce = alice.account.nonce; - transactionIncrement.signature = await alice.signer.sign( - transactionComputer.computeBytesForSigning(transactionIncrement), - ); + transactionIncrement.nonce = alice.nonce; + transactionIncrement.signature = alice.signTransaction(transactionIncrement); - alice.account.incrementNonce(); + alice.incrementNonce(); // Broadcast & execute const txHashDeploy = await provider.sendTransaction(transactionDeploy); @@ -87,7 +83,6 @@ describe("fetch transactions from local testnet", function () { TransactionWatcher.DefaultTimeout = 50000; let network = await provider.getNetworkConfig(); - await alice.sync(provider); const config = new TransactionsFactoryConfig({ chainID: network.chainID }); const factory = new SmartContractTransactionsFactory({ config: config }); @@ -98,26 +93,21 @@ describe("fetch transactions from local testnet", function () { bytecode: bytecode, gasLimit: 3000000n, }); - deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); + deployTransaction.nonce = alice.nonce; + deployTransaction.signature = alice.signTransaction(deployTransaction); - deployTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), - ); - - const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); - alice.account.incrementNonce(); + const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); + alice.incrementNonce(); const smartContractCallTransaction = factory.createTransactionForExecute(alice.address, { contract: contractAddress, function: "increment", gasLimit: 3000000n, }); - smartContractCallTransaction.nonce = BigInt(alice.account.nonce.valueOf()); - smartContractCallTransaction.signature = await alice.signer.sign( - Buffer.from(transactionComputer.computeBytesForSigning(smartContractCallTransaction)), - ); + smartContractCallTransaction.nonce = alice.nonce; + smartContractCallTransaction.signature = alice.signTransaction(smartContractCallTransaction); - alice.account.incrementNonce(); + alice.incrementNonce(); // Broadcast & execute const deployTxHash = await provider.sendTransaction(deployTransaction); diff --git a/src/accounts/account.spec.ts b/src/accounts/account.spec.ts index fa4c1992..85366309 100644 --- a/src/accounts/account.spec.ts +++ b/src/accounts/account.spec.ts @@ -1,14 +1,13 @@ import { assert } from "chai"; -import { resolve } from "path"; import { Address, Message, Transaction } from "../core"; +import { getTestWalletsPath } from "../testutils/utils"; import { KeyPair, UserSecretKey } from "../wallet"; import { Account } from "./account"; describe("test account methods", function () { - const testWallets = resolve(__dirname, "..", "testdata", "testwallets"); const DUMMY_MNEMONIC = "moral volcano peasant pass circle pen over picture flat shop clap goat never lyrics gather prepare woman film husband gravity behind test tiger improve"; - const alice = `${testWallets}/alice.pem`; + const alice = `${getTestWalletsPath()}/alice.pem`; it("should create account from pem file", async function () { const account = await Account.newFromPem(alice); diff --git a/src/core/message.spec.ts b/src/core/message.spec.ts index 8fb62d5c..2cb0776e 100644 --- a/src/core/message.spec.ts +++ b/src/core/message.spec.ts @@ -1,15 +1,16 @@ import { assert } from "chai"; -import { TestWallet, loadTestWallets } from "../testutils"; +import { Account } from "../accounts"; +import { getTestWalletsPath } from "../testutils/utils"; import { UserVerifier } from "../wallet"; import { DEFAULT_MESSAGE_VERSION, SDK_JS_SIGNER, UNKNOWN_SIGNER } from "./constants"; import { Message, MessageComputer } from "./message"; describe("test message", () => { - let alice: TestWallet; + let alice: Account; const messageComputer = new MessageComputer(); before(async function () { - ({ alice } = await loadTestWallets()); + alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); }); it("should test message compute bytes for signing", async () => { @@ -32,10 +33,10 @@ describe("test message", () => { const message = new Message({ data: data, - address: alice.getAddress(), + address: alice.address, }); - message.signature = await alice.signer.sign(messageComputer.computeBytesForSigning(message)); + message.signature = alice.signMessage(message); assert.equal( Buffer.from(message.signature).toString("hex"), @@ -53,13 +54,13 @@ describe("test message", () => { }); const unpackedMessage = messageComputer.unpackMessage(packedMessage); - assert.deepEqual(unpackedMessage.address, alice.getAddress()); + assert.deepEqual(unpackedMessage.address, alice.address); assert.deepEqual(unpackedMessage.data, message.data); assert.deepEqual(unpackedMessage.signature, message.signature); assert.deepEqual(unpackedMessage.version, message.version); assert.deepEqual(unpackedMessage.signer, message.signer); - const verifier = UserVerifier.fromAddress(alice.getAddress()); + const verifier = UserVerifier.fromAddress(alice.address); const isValid = verifier.verify( Buffer.from(messageComputer.computeBytesForVerifying(unpackedMessage)), Buffer.from(unpackedMessage.signature!), @@ -78,7 +79,7 @@ describe("test message", () => { }; const message = messageComputer.unpackMessage(legacyMessage); - assert.deepEqual(message.address, alice.getAddress()); + assert.deepEqual(message.address, alice.address); assert.deepEqual(Buffer.from(message.data).toString(), "this is a test message"); assert.deepEqual( Buffer.from(message.signature!).toString("hex"), @@ -97,7 +98,7 @@ describe("test message", () => { }; const message = messageComputer.unpackMessage(packedMessage); - assert.deepEqual(message.address, alice.getAddress()); + assert.deepEqual(message.address, alice.address); assert.deepEqual(Buffer.from(message.data).toString(), "this is a test message"); assert.deepEqual( Buffer.from(message.signature!).toString("hex"), diff --git a/src/core/transaction.spec.ts b/src/core/transaction.spec.ts index dfd6819b..40f48a4d 100644 --- a/src/core/transaction.spec.ts +++ b/src/core/transaction.spec.ts @@ -1,7 +1,8 @@ import { Buffer } from "buffer"; import { assert } from "chai"; +import { Account } from "../accounts"; import { ProtoSerializer } from "../proto"; -import { TestWallet, loadTestWallets } from "../testutils"; +import { getTestWalletsPath } from "../testutils/utils"; import { UserPublicKey, UserVerifier } from "../wallet"; import { Address } from "./address"; import { MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS, TRANSACTION_OPTIONS_DEFAULT } from "./constants"; @@ -10,7 +11,9 @@ import { Transaction } from "./transaction"; import { TransactionComputer } from "./transactionComputer"; describe("test transaction", async () => { - let wallets: Record; + let alice: Account; + let bob: Account; + let carol: Account; const minGasLimit = 50000; const minGasPrice = 1000000000; @@ -24,14 +27,16 @@ describe("test transaction", async () => { }; before(async function () { - wallets = await loadTestWallets(); + alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); + bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); + carol = await Account.newFromPem(`${getTestWalletsPath()}/carol.pem`); }); it("should serialize transaction for signing (without data)", async () => { const transaction = new Transaction({ chainID: networkConfig.chainID, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, value: 0n, version: 2, @@ -50,8 +55,8 @@ describe("test transaction", async () => { it("should serialize transaction for signing (with data)", async () => { const transaction = new Transaction({ chainID: networkConfig.chainID, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 70000n, value: 1000000000000000000n, version: 2, @@ -72,16 +77,14 @@ describe("test transaction", async () => { const transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: BigInt(minGasLimit), chainID: "local-testnet", }); - transaction.signature = await wallets.alice.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); assert.equal( Buffer.from(transaction.signature).toString("hex"), @@ -97,17 +100,15 @@ describe("test transaction", async () => { const transaction = new Transaction({ nonce: 90n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: 80000n, data: Buffer.from("hello"), chainID: "local-testnet", }); - transaction.signature = await wallets.alice.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); assert.equal( Buffer.from(transaction.signature).toString("hex"), @@ -122,8 +123,8 @@ describe("test transaction", async () => { it("should sign transaction (with usernames)", async () => { const transaction = new Transaction({ chainID: "T", - sender: wallets.carol.address, - receiver: wallets.alice.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, value: 1000000000000000000n, version: 2, @@ -132,9 +133,7 @@ describe("test transaction", async () => { receiverUsername: "alice", }); - transaction.signature = await wallets.carol.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); assert.equal( Buffer.from(transaction.signature).toString("hex"), @@ -145,8 +144,8 @@ describe("test transaction", async () => { it("should compute hash", async () => { const transaction = new Transaction({ chainID: networkConfig.chainID, - sender: wallets.alice.address, - receiver: wallets.alice.address, + sender: alice.address, + receiver: alice.address, gasLimit: 100000n, value: 1000000000000n, version: 2, @@ -167,8 +166,8 @@ describe("test transaction", async () => { it("should compute hash (with usernames)", async () => { const transaction = new Transaction({ chainID: networkConfig.chainID, - sender: wallets.alice.address, - receiver: wallets.alice.address, + sender: alice.address, + receiver: alice.address, gasLimit: 100000n, value: 1000000000000n, version: 2, @@ -192,8 +191,8 @@ describe("test transaction", async () => { const transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: BigInt(minGasLimit), chainID: "local-testnet", @@ -211,17 +210,15 @@ describe("test transaction", async () => { const transaction = new Transaction({ nonce: 91n, value: 10000000000000000000n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: 100000n, data: Buffer.from("for the book"), chainID: "local-testnet", }); - transaction.signature = await wallets.alice.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); assert.equal( Buffer.from(transaction.signature).toString("hex"), @@ -237,17 +234,15 @@ describe("test transaction", async () => { const transaction = new Transaction({ nonce: 92n, value: BigInt("123456789000000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: 100000n, data: Buffer.from("for the spaceship"), chainID: "local-testnet", }); - transaction.signature = await wallets.alice.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); assert.equal( Buffer.from(transaction.signature).toString("hex"), @@ -263,8 +258,8 @@ describe("test transaction", async () => { const transaction = new Transaction({ nonce: 0n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: 80000n, data: Buffer.from("hello"), @@ -272,9 +267,7 @@ describe("test transaction", async () => { version: 1, }); - transaction.signature = await wallets.alice.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); assert.equal( Buffer.from(transaction.signature).toString("hex"), @@ -290,16 +283,14 @@ describe("test transaction", async () => { const transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: BigInt(minGasLimit), chainID: "local-testnet", }); - transaction.signature = await wallets.alice.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); assert.equal( Buffer.from(transaction.signature).toString("hex"), @@ -318,16 +309,14 @@ describe("test transaction", async () => { const transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: BigInt(minGasLimit), chainID: "local-testnet", }); - transaction.signature = await wallets.alice.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); assert.equal( Buffer.from(transaction.signature).toString("hex"), @@ -354,9 +343,7 @@ describe("test transaction", async () => { chainID: "T", }); - transaction.signature = await wallets.carol.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); assert.equal( Buffer.from(transaction.signature).toString("hex"), @@ -369,12 +356,10 @@ describe("test transaction", async () => { }); it("should sign & compute hash (guarded transaction)", async () => { - const alice = wallets.alice; - const transaction = new Transaction({ chainID: "local-testnet", sender: alice.address, - receiver: wallets.bob.address, + receiver: bob.address, gasLimit: 150000n, gasPrice: 1000000000n, data: new Uint8Array(Buffer.from("test data field")), @@ -385,7 +370,7 @@ describe("test transaction", async () => { guardian: Address.newFromBech32("erd1x23lzn8483xs2su4fak0r0dqx6w38enpmmqf2yrkylwq7mfnvyhsxqw57y"), }); transaction.guardianSignature = new Uint8Array(64); - transaction.signature = await alice.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = alice.signTransaction(transaction); const serializer = new ProtoSerializer(); const buffer = serializer.serializeTransaction(transaction); @@ -403,8 +388,8 @@ describe("test transaction", async () => { const transaction = new Transaction({ nonce: 92n, value: BigInt("123456789000000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: BigInt(minGasLimit), chainID: "local-testnet", @@ -417,8 +402,8 @@ describe("test transaction", async () => { it("computes fee", async () => { const transaction = new Transaction({ chainID: "D", - sender: wallets.alice.address, - receiver: wallets.alice.address, + sender: alice.address, + receiver: alice.address, gasLimit: 50000n, gasPrice: BigInt(minGasPrice), }); @@ -430,8 +415,8 @@ describe("test transaction", async () => { it("computes fee, but should throw `NotEnoughGas` error", async () => { const transaction = new Transaction({ chainID: networkConfig.chainID, - sender: wallets.alice.address, - receiver: wallets.alice.address, + sender: alice.address, + receiver: alice.address, gasLimit: 50000n, data: Buffer.from("toolittlegaslimit"), }); @@ -445,8 +430,8 @@ describe("test transaction", async () => { let transaction = new Transaction({ nonce: 92n, value: BigInt("123456789000000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, data: Buffer.from("testdata"), gasPrice: BigInt(minGasPrice), gasLimit: BigInt(minGasLimit + 12010), @@ -460,8 +445,8 @@ describe("test transaction", async () => { it("computes fee (with data field)", async () => { const transaction = new Transaction({ chainID: networkConfig.chainID, - sender: wallets.alice.address, - receiver: wallets.alice.address, + sender: alice.address, + receiver: alice.address, gasLimit: 50000n + 12010n, gasPrice: BigInt(minGasPrice), data: Buffer.from("testdata"), @@ -472,12 +457,12 @@ describe("test transaction", async () => { }); it("should convert transaction to plain object and back", () => { - const sender = wallets.alice.address; + const sender = alice.address; const transaction = new Transaction({ nonce: 90n, value: 123456789000000000000000000000n, sender: sender, - receiver: wallets.bob.address, + receiver: bob.address, senderUsername: "alice", receiverUsername: "bob", gasPrice: BigInt(minGasPrice), @@ -494,8 +479,8 @@ describe("test transaction", async () => { it("should handle large values", () => { const tx1 = new Transaction({ value: 123456789000000000000000000000n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, chainID: "local-testnet", }); @@ -503,8 +488,8 @@ describe("test transaction", async () => { const tx2 = new Transaction({ value: 123456789000000000000000000000n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, chainID: "local-testnet", }); @@ -512,8 +497,8 @@ describe("test transaction", async () => { const tx3 = new Transaction({ value: BigInt("123456789000000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, chainID: "local-testnet", }); @@ -524,8 +509,8 @@ describe("test transaction", async () => { let transaction = new Transaction({ nonce: 90n, value: BigInt("1000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: 80000n, data: Buffer.from("hello"), @@ -538,8 +523,8 @@ describe("test transaction", async () => { transaction = new Transaction({ nonce: 90n, value: BigInt("1000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: 80000n, data: Buffer.from("hello"), @@ -552,8 +537,8 @@ describe("test transaction", async () => { transaction = new Transaction({ nonce: 90n, value: BigInt("1000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: 80000n, data: Buffer.from("hello"), @@ -566,8 +551,8 @@ describe("test transaction", async () => { transaction = new Transaction({ nonce: 90n, value: BigInt("1000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), gasLimit: 80000n, data: Buffer.from("hello"), @@ -580,10 +565,10 @@ describe("test transaction", async () => { transaction = new Transaction({ nonce: 90n, value: BigInt("1000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), - guardian: wallets.bob.address, + guardian: bob.address, gasLimit: 80000n, data: Buffer.from("hello"), chainID: "local-testnet", @@ -595,19 +580,17 @@ describe("test transaction", async () => { transaction = new Transaction({ nonce: 90n, value: BigInt("1000000000000000000"), - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasPrice: BigInt(minGasPrice), - guardian: wallets.bob.address, + guardian: bob.address, gasLimit: 80000n, data: Buffer.from("hello"), chainID: "local-testnet", version: 2, options: 2, }); - transaction.signature = await wallets.alice.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); transaction.guardianSignature = transaction.signature; assert.isTrue(transaction.isGuardedTransaction()); }); @@ -616,8 +599,8 @@ describe("test transaction", async () => { let transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, gasPrice: 1000000000n, chainID: "integration tests chain ID", @@ -625,11 +608,11 @@ describe("test transaction", async () => { options: 1, }); - transaction.signature = await wallets.alice.signer.sign(transactionComputer.computeHashForSigning(transaction)); + transaction.signature = alice.signTransaction(transaction); assert.equal( - "f0c81f2393b1ec5972c813f817bae8daa00ade91c6f75ea604ab6a4d2797aca4378d783023ff98f1a02717fe4f24240cdfba0b674ee9abb18042203d713bc70a", Buffer.from(transaction.signature).toString("hex"), + "f0c81f2393b1ec5972c813f817bae8daa00ade91c6f75ea604ab6a4d2797aca4378d783023ff98f1a02717fe4f24240cdfba0b674ee9abb18042203d713bc70a", ); }); @@ -637,25 +620,25 @@ describe("test transaction", async () => { let transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, chainID: "localnet", }); - transactionComputer.applyGuardian(transaction, wallets.carol.address); + transactionComputer.applyGuardian(transaction, carol.address); assert.equal(transaction.version, 2); assert.equal(transaction.options, 2); - assert.equal(transaction.guardian, wallets.carol.address); + assert.equal(transaction.guardian, carol.address); }); it("should apply guardian with options set for hash signing", async () => { let transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, chainID: "localnet", version: 1, @@ -665,7 +648,7 @@ describe("test transaction", async () => { assert.equal(transaction.version, 2); assert.equal(transaction.options, 1); - transactionComputer.applyGuardian(transaction, wallets.carol.address); + transactionComputer.applyGuardian(transaction, carol.address); assert.equal(transaction.version, 2); assert.equal(transaction.options, 3); }); @@ -673,12 +656,12 @@ describe("test transaction", async () => { it("should ensure transaction is valid", async () => { let transaction = new Transaction({ sender: Address.empty(), - receiver: wallets.bob.address, + receiver: bob.address, gasLimit: 50000n, chainID: "", }); - transaction.sender = wallets.alice.address; + transaction.sender = alice.address; assert.throws(() => { transactionComputer.computeBytesForSigning(transaction); @@ -700,24 +683,22 @@ describe("test transaction", async () => { it("should compute bytes to verify transaction signature", async () => { let transaction = new Transaction({ - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, chainID: "D", nonce: 7n, }); - transaction.signature = await wallets.alice.signer.sign( - transactionComputer.computeBytesForSigning(transaction), - ); + transaction.signature = alice.signTransaction(transaction); - const userVerifier = new UserVerifier(new UserPublicKey(wallets.alice.address.getPublicKey())); + const userVerifier = new UserVerifier(new UserPublicKey(alice.address.getPublicKey())); const isSignedByAlice = userVerifier.verify( transactionComputer.computeBytesForVerifying(transaction), transaction.signature, ); - const wrongVerifier = new UserVerifier(new UserPublicKey(wallets.bob.address.getPublicKey())); + const wrongVerifier = new UserVerifier(new UserPublicKey(bob.address.getPublicKey())); const isSignedByBob = wrongVerifier.verify( transactionComputer.computeBytesForVerifying(transaction), transaction.signature, @@ -729,8 +710,8 @@ describe("test transaction", async () => { it("should compute bytes to verify transaction signature (signed by hash)", async () => { let transaction = new Transaction({ - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, chainID: "D", nonce: 7n, @@ -738,15 +719,15 @@ describe("test transaction", async () => { transactionComputer.applyOptionsForHashSigning(transaction); - transaction.signature = await wallets.alice.signer.sign(transactionComputer.computeHashForSigning(transaction)); + transaction.signature = alice.signTransaction(transaction); - const userVerifier = new UserVerifier(new UserPublicKey(wallets.alice.address.getPublicKey())); + const userVerifier = new UserVerifier(new UserPublicKey(alice.address.getPublicKey())); const isSignedByAlice = userVerifier.verify( transactionComputer.computeBytesForVerifying(transaction), transaction.signature, ); - const wrongVerifier = new UserVerifier(new UserPublicKey(wallets.bob.address.getPublicKey())); + const wrongVerifier = new UserVerifier(new UserPublicKey(bob.address.getPublicKey())); const isSignedByBob = wrongVerifier.verify( transactionComputer.computeBytesForVerifying(transaction), transaction.signature, @@ -800,9 +781,9 @@ describe("test transaction", async () => { it("should serialize transaction with relayer", async () => { const transaction = new Transaction({ chainID: networkConfig.chainID, - sender: wallets.alice.address, - receiver: wallets.alice.address, - relayer: wallets.bob.address, + sender: alice.address, + receiver: alice.address, + relayer: bob.address, gasLimit: 50000n, value: 0n, version: 2, @@ -821,8 +802,8 @@ describe("test transaction", async () => { it("should test relayed v3", async () => { const transaction = new Transaction({ chainID: networkConfig.chainID, - sender: wallets.alice.address, - receiver: wallets.alice.address, + sender: alice.address, + receiver: alice.address, senderUsername: "alice", receiverUsername: "bob", gasLimit: 80000n, @@ -833,7 +814,7 @@ describe("test transaction", async () => { }); assert.isFalse(transactionComputer.isRelayedV3Transaction(transaction)); - transaction.relayer = wallets.carol.address; + transaction.relayer = carol.address; assert.isTrue(transactionComputer.isRelayedV3Transaction(transaction)); }); }); diff --git a/src/networkProviders/accountAwaiter.dev.net.spec.ts b/src/networkProviders/accountAwaiter.dev.net.spec.ts index 9a9e40e6..0569de46 100644 --- a/src/networkProviders/accountAwaiter.dev.net.spec.ts +++ b/src/networkProviders/accountAwaiter.dev.net.spec.ts @@ -1,8 +1,8 @@ import { assert } from "chai"; -import { Address, Transaction, TransactionComputer } from "../core"; +import { Account } from "../accounts"; +import { Address, Transaction } from "../core"; import { MarkCompleted, MockNetworkProvider, Wait } from "../testutils/mockNetworkProvider"; -import { createAccountBalance } from "../testutils/utils"; -import { loadTestWallet } from "../testutils/wallets"; +import { createAccountBalance, getTestWalletsPath } from "../testutils/utils"; import { AccountAwaiter } from "./accountAwaiter"; import { AccountOnNetwork } from "./accounts"; import { ApiNetworkProvider } from "./apiNetworkProvider"; @@ -41,13 +41,12 @@ describe("AccountAwaiter Tests", () => { it("should await for account balance increase on the network", async function () { this.timeout(20000); - const alice = await loadTestWallet("alice"); - const aliceAddress = alice.getAddress(); + const alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); + const aliceAddress = alice.address; const frank = Address.newFromBech32("erd1kdl46yctawygtwg2k462307dmz2v55c605737dp3zkxh04sct7asqylhyv"); const api = new ApiNetworkProvider("https://devnet-api.multiversx.com"); const watcher = new AccountAwaiter({ fetcher: api }); - const txComputer = new TransactionComputer(); const value = 100_000n; // Create and sign the transaction @@ -59,7 +58,7 @@ describe("AccountAwaiter Tests", () => { value, }); transaction.nonce = (await api.getAccount(aliceAddress)).nonce; - transaction.signature = await alice.signer.sign(txComputer.computeBytesForSigning(transaction)); + transaction.signature = alice.signTransaction(transaction); const initialBalance = (await api.getAccount(frank)).balance; diff --git a/src/networkProviders/apiNetworkProvider.dev.net.spec.ts b/src/networkProviders/apiNetworkProvider.dev.net.spec.ts index 74a61d75..60efbd68 100644 --- a/src/networkProviders/apiNetworkProvider.dev.net.spec.ts +++ b/src/networkProviders/apiNetworkProvider.dev.net.spec.ts @@ -1,19 +1,11 @@ import { assert, expect } from "chai"; -import { - Address, - SmartContractQuery, - Token, - Transaction, - TransactionComputer, - TransactionOnNetwork, - TransactionStatus, -} from "../core"; -import { loadTestWallet } from "../testutils/wallets"; +import { Account } from "../accounts"; +import { Address, SmartContractQuery, Token, Transaction, TransactionOnNetwork, TransactionStatus } from "../core"; +import { getTestWalletsPath } from "../testutils/utils"; import { ApiNetworkProvider } from "./apiNetworkProvider"; describe("ApiNetworkProvider Tests", function () { const apiProvider = new ApiNetworkProvider("https://devnet-api.multiversx.com"); - const transactionComputer = new TransactionComputer(); it("should fetch network configuration", async () => { const result = await apiProvider.getNetworkConfig(); @@ -284,7 +276,7 @@ describe("ApiNetworkProvider Tests", function () { }); it("should simulate transaction", async () => { - const bob = await loadTestWallet("bob"); + const bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); let transaction = new Transaction({ sender: bob.address, receiver: bob.address, @@ -297,7 +289,7 @@ describe("ApiNetworkProvider Tests", function () { let txOnNetwork = await apiProvider.simulateTransaction(transaction); assert.deepEqual(txOnNetwork.status, new TransactionStatus("success")); - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); txOnNetwork = await apiProvider.simulateTransaction(transaction, true); transaction = new Transaction({ @@ -321,7 +313,7 @@ describe("ApiNetworkProvider Tests", function () { assert.equal(txOnNetwork.smartContractResults[0].receiver.toBech32(), bob.address.toBech32()); assert.deepEqual(txOnNetwork.smartContractResults[0].data, "@6f6b"); - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); txOnNetwork = await apiProvider.simulateTransaction(transaction, true); assert.deepEqual(txOnNetwork.status, new TransactionStatus("success")); @@ -335,7 +327,7 @@ describe("ApiNetworkProvider Tests", function () { }); it("should estimate transaction cost", async function () { - const bob = await loadTestWallet("bob"); + const bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); const transaction = new Transaction({ sender: bob.address, receiver: bob.address, @@ -344,14 +336,14 @@ describe("ApiNetworkProvider Tests", function () { data: new Uint8Array(Buffer.from("test transaction")), }); transaction.nonce = (await apiProvider.getAccount(bob.address)).nonce; - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); const response = await apiProvider.estimateTransactionCost(transaction); assert.equal(response.gasLimit, 74000); }); it("should send and await for completed transaction", async function () { this.timeout(30000); - const bob = await loadTestWallet("bob"); + const bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); let transaction = new Transaction({ sender: bob.address, receiver: bob.address, @@ -360,7 +352,7 @@ describe("ApiNetworkProvider Tests", function () { }); const nonce = (await apiProvider.getAccount(bob.address)).nonce; transaction.nonce = nonce; - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); let hash = await apiProvider.sendTransaction(transaction); let transactionOnNetwork = await apiProvider.awaitTransactionCompleted(hash); assert.isTrue(transactionOnNetwork.status.isCompleted()); @@ -373,7 +365,7 @@ describe("ApiNetworkProvider Tests", function () { data: new Uint8Array(Buffer.from("dummy@05")), }); transaction.nonce = nonce + 1n; - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); const condition = (txOnNetwork: TransactionOnNetwork) => !txOnNetwork.status.isSuccessful(); hash = await apiProvider.sendTransaction(transaction); diff --git a/src/networkProviders/proxyNetworkProvider.dev.net.spec.ts b/src/networkProviders/proxyNetworkProvider.dev.net.spec.ts index d5eb6609..de66a18a 100644 --- a/src/networkProviders/proxyNetworkProvider.dev.net.spec.ts +++ b/src/networkProviders/proxyNetworkProvider.dev.net.spec.ts @@ -1,19 +1,11 @@ import { assert, expect } from "chai"; -import { - Address, - SmartContractQuery, - Token, - Transaction, - TransactionComputer, - TransactionOnNetwork, - TransactionStatus, -} from "../core"; -import { loadTestWallet } from "../testutils/wallets"; +import { Account } from "../accounts"; +import { Address, SmartContractQuery, Token, Transaction, TransactionOnNetwork, TransactionStatus } from "../core"; +import { getTestWalletsPath } from "../testutils/utils"; import { ProxyNetworkProvider } from "./proxyNetworkProvider"; describe("ProxyNetworkProvider Tests", function () { const proxy = new ProxyNetworkProvider("https://devnet-gateway.multiversx.com"); - const transactionComputer = new TransactionComputer(); it("should fetch network configuration", async () => { const result = await proxy.getNetworkConfig(); @@ -284,8 +276,7 @@ describe("ProxyNetworkProvider Tests", function () { }); it("should simulate transaction", async () => { - const bob = await loadTestWallet("bob"); - + const bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); let transaction = new Transaction({ sender: bob.address, receiver: bob.address, @@ -298,7 +289,7 @@ describe("ProxyNetworkProvider Tests", function () { let txOnNetwork = await proxy.simulateTransaction(transaction); assert.deepEqual(txOnNetwork.status, new TransactionStatus("success")); - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); txOnNetwork = await proxy.simulateTransaction(transaction); transaction = new Transaction({ @@ -322,7 +313,7 @@ describe("ProxyNetworkProvider Tests", function () { assert.equal(txOnNetwork.smartContractResults[0].receiver.toBech32(), bob.address.toBech32()); assert.equal(txOnNetwork.smartContractResults[0].data, "@6f6b"); - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); txOnNetwork = await proxy.simulateTransaction(transaction); assert.deepEqual(txOnNetwork.status, new TransactionStatus("success")); @@ -336,7 +327,7 @@ describe("ProxyNetworkProvider Tests", function () { }); it("should estimate transaction cost", async function () { - const bob = await loadTestWallet("bob"); + const bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); const transaction = new Transaction({ sender: bob.address, receiver: bob.address, @@ -345,14 +336,14 @@ describe("ProxyNetworkProvider Tests", function () { data: new Uint8Array(Buffer.from("test transaction")), }); transaction.nonce = (await proxy.getAccount(bob.address)).nonce; - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); const response = await proxy.estimateTransactionCost(transaction); assert.equal(response.gasLimit, 74000); }); it("should send and await for completed transaction", async function () { this.timeout(50000); - const bob = await loadTestWallet("bob"); + const bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); let transaction = new Transaction({ sender: bob.address, receiver: bob.address, @@ -361,7 +352,7 @@ describe("ProxyNetworkProvider Tests", function () { }); const nonce = (await proxy.getAccount(bob.address)).nonce; transaction.nonce = nonce; - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); let hash = await proxy.sendTransaction(transaction); let transactionOnNetwork = await proxy.awaitTransactionCompleted(hash); assert.isTrue(transactionOnNetwork.status.isCompleted()); @@ -374,7 +365,7 @@ describe("ProxyNetworkProvider Tests", function () { data: new Uint8Array(Buffer.from("dummy@05")), }); transaction.nonce = nonce + 1n; - transaction.signature = await bob.signer.sign(transactionComputer.computeBytesForSigning(transaction)); + transaction.signature = bob.signTransaction(transaction); const condition = (txOnNetwork: TransactionOnNetwork) => !txOnNetwork.status.isSuccessful(); hash = await proxy.sendTransaction(transaction); diff --git a/src/proto/serializer.spec.ts b/src/proto/serializer.spec.ts index 6a8ccce8..729c22ec 100644 --- a/src/proto/serializer.spec.ts +++ b/src/proto/serializer.spec.ts @@ -1,28 +1,31 @@ import { assert } from "chai"; +import { Account } from "../accounts"; import { Address, TokenTransfer, Transaction } from "../core"; -import { loadTestWallets, TestWallet } from "../testutils"; +import { getTestWalletsPath } from "../testutils"; import { ProtoSerializer } from "./serializer"; describe("serialize transactions", () => { - let wallets: Record; let serializer = new ProtoSerializer(); - + let alice: Account; + let bob: Account; + let carol: Account; before(async function () { - wallets = await loadTestWallets(); + alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); + bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); + carol = await Account.newFromPem(`${getTestWalletsPath()}/carol.pem`); }); it("with no data, no value", async () => { let transaction = new Transaction({ nonce: 89n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 50000n, chainID: "local-testnet", }); - const signer = wallets.alice.signer; - transaction.applySignature(await signer.sign(transaction.serializeForSigning())); + transaction.signature = alice.signTransaction(transaction); let buffer = serializer.serializeTransaction(transaction); assert.equal( @@ -35,15 +38,14 @@ describe("serialize transactions", () => { let transaction = new Transaction({ nonce: 90n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 80000n, data: Buffer.from("hello"), chainID: "local-testnet", }); - const signer = wallets.alice.signer; - transaction.applySignature(await signer.sign(transaction.serializeForSigning())); + transaction.signature = alice.signTransaction(transaction); let buffer = serializer.serializeTransaction(transaction); assert.equal( @@ -56,15 +58,14 @@ describe("serialize transactions", () => { let transaction = new Transaction({ nonce: 91n, value: TokenTransfer.newFromNativeAmount(10000000000000000000n).amount, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 100000n, data: Buffer.from("for the book"), chainID: "local-testnet", }); - const signer = wallets.alice.signer; - transaction.applySignature(await signer.sign(transaction.serializeForSigning())); + transaction.signature = alice.signTransaction(transaction); let buffer = serializer.serializeTransaction(transaction); assert.equal( @@ -77,15 +78,14 @@ describe("serialize transactions", () => { let transaction = new Transaction({ nonce: 92n, value: 123456789000000000000000000000n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 100000n, data: Buffer.from("for the spaceship"), chainID: "local-testnet", }); - const signer = wallets.alice.signer; - transaction.applySignature(await signer.sign(transaction.serializeForSigning())); + transaction.signature = alice.signTransaction(transaction); let buffer = serializer.serializeTransaction(transaction); assert.equal( @@ -98,20 +98,15 @@ describe("serialize transactions", () => { let transaction = new Transaction({ nonce: 0n, value: 0n, - sender: wallets.alice.address, - receiver: wallets.bob.address, + sender: alice.address, + receiver: bob.address, gasLimit: 80000n, data: Buffer.from("hello"), chainID: "local-testnet", version: 1, }); - transaction.applySignature( - Buffer.from( - "dfa3e9f2fdec60dcb353bac3b3435b4a2ff251e7e98eaf8620f46c731fc70c8ba5615fd4e208b05e75fe0f7dc44b7a99567e29f94fcd91efac7e67b182cd2a04", - "hex", - ), - ); + transaction.signature = alice.signTransaction(transaction); let buffer = serializer.serializeTransaction(transaction); assert.equal( @@ -132,8 +127,7 @@ describe("serialize transactions", () => { chainID: "T", }); - const signer = wallets.carol.signer; - transaction.applySignature(await signer.sign(transaction.serializeForSigning())); + transaction.signature = carol.signTransaction(transaction); const buffer = serializer.serializeTransaction(transaction); assert.equal( diff --git a/src/relayed/relayedTransactionsFactory.spec.ts b/src/relayed/relayedTransactionsFactory.spec.ts index 3535b8a7..9f23543b 100644 --- a/src/relayed/relayedTransactionsFactory.spec.ts +++ b/src/relayed/relayedTransactionsFactory.spec.ts @@ -1,16 +1,20 @@ import { assert } from "chai"; -import { Address, Transaction, TransactionComputer, TransactionsFactoryConfig } from "../core"; -import { TestWallet, loadTestWallets } from "../testutils"; +import { Account } from "../accounts"; +import { Address, Transaction, TransactionsFactoryConfig } from "../core"; +import { getTestWalletsPath } from "../testutils"; import { RelayedTransactionsFactory } from "./relayedTransactionsFactory"; describe("test relayed transactions factory", function () { const config = new TransactionsFactoryConfig({ chainID: "T" }); const factory = new RelayedTransactionsFactory({ config: config }); - const transactionComputer = new TransactionComputer(); - let alice: TestWallet, bob: TestWallet, carol: TestWallet, grace: TestWallet, frank: TestWallet; + let alice: Account, bob: Account, carol: Account, grace: Account, frank: Account; before(async function () { - ({ alice, bob, carol, grace, frank } = await loadTestWallets()); + alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); + bob = await Account.newFromPem(`${getTestWalletsPath()}/bob.pem`); + carol = await Account.newFromPem(`${getTestWalletsPath()}/carol.pem`); + grace = await Account.newFromPem(`${getTestWalletsPath()}/grace.pem`); + frank = await Account.newFromPem(`${getTestWalletsPath()}/frank.pem`); }); it("should throw exception when creating relayed v1 transaction with invalid inner transaction", async function () { @@ -46,16 +50,14 @@ describe("test relayed transactions factory", function () { nonce: 198n, }); - const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction); - innerTransaction.signature = await bob.signer.sign(serializedInnerTransaction); + innerTransaction.signature = bob.signTransaction(innerTransaction); - const relayedTransaction = factory.createRelayedV1Transaction(alice.getAddress(), { + const relayedTransaction = factory.createRelayedV1Transaction(alice.address, { innerTransaction: innerTransaction, }); relayedTransaction.nonce = 2627n; - const serializedRelayedTransaction = transactionComputer.computeBytesForSigning(relayedTransaction); - relayedTransaction.signature = await alice.signer.sign(serializedRelayedTransaction); + relayedTransaction.signature = alice.signTransaction(relayedTransaction); assert.equal( Buffer.from(relayedTransaction.data).toString(), @@ -79,16 +81,14 @@ describe("test relayed transactions factory", function () { value: 1000000000000000000n, }); - const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction); - innerTransaction.signature = await carol.signer.sign(serializedInnerTransaction); + innerTransaction.signature = carol.signTransaction(innerTransaction); - const relayedTransaction = factory.createRelayedV1Transaction(frank.getAddress(), { + const relayedTransaction = factory.createRelayedV1Transaction(frank.address, { innerTransaction: innerTransaction, }); relayedTransaction.nonce = 715n; - const serializedRelayedTransaction = transactionComputer.computeBytesForSigning(relayedTransaction); - relayedTransaction.signature = await frank.signer.sign(serializedRelayedTransaction); + relayedTransaction.signature = frank.signTransaction(relayedTransaction); assert.equal( Buffer.from(relayedTransaction.data).toString(), @@ -112,16 +112,14 @@ describe("test relayed transactions factory", function () { value: 1999999000000000000000000n, }); - const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction); - innerTransaction.signature = await carol.signer.sign(serializedInnerTransaction); + innerTransaction.signature = carol.signTransaction(innerTransaction); - const relayedTransaction = factory.createRelayedV1Transaction(frank.getAddress(), { + const relayedTransaction = factory.createRelayedV1Transaction(frank.address, { innerTransaction: innerTransaction, }); relayedTransaction.nonce = 715n; - const serializedRelayedTransaction = transactionComputer.computeBytesForSigning(relayedTransaction); - relayedTransaction.signature = await frank.signer.sign(serializedRelayedTransaction); + relayedTransaction.signature = frank.signTransaction(relayedTransaction); assert.equal( Buffer.from(relayedTransaction.data).toString(), @@ -146,17 +144,15 @@ describe("test relayed transactions factory", function () { guardian: grace.address, }); - const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction); - innerTransaction.signature = await bob.signer.sign(serializedInnerTransaction); - innerTransaction.guardianSignature = await grace.signer.sign(serializedInnerTransaction); + innerTransaction.signature = bob.signTransaction(innerTransaction); + innerTransaction.guardianSignature = grace.signTransaction(innerTransaction); - const relayedTransaction = factory.createRelayedV1Transaction(alice.getAddress(), { + const relayedTransaction = factory.createRelayedV1Transaction(alice.address, { innerTransaction: innerTransaction, }); relayedTransaction.nonce = 2627n; - const serializedRelayedTransaction = transactionComputer.computeBytesForSigning(relayedTransaction); - relayedTransaction.signature = await alice.signer.sign(serializedRelayedTransaction); + relayedTransaction.signature = alice.signTransaction(relayedTransaction); assert.equal( Buffer.from(relayedTransaction.data).toString(), @@ -181,20 +177,18 @@ describe("test relayed transactions factory", function () { guardian: grace.address, }); - const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction); - innerTransaction.signature = await bob.signer.sign(serializedInnerTransaction); - innerTransaction.guardianSignature = await grace.signer.sign(serializedInnerTransaction); + innerTransaction.signature = bob.signTransaction(innerTransaction); + innerTransaction.guardianSignature = grace.signTransaction(innerTransaction); - const relayedTransaction = factory.createRelayedV1Transaction(alice.getAddress(), { + const relayedTransaction = factory.createRelayedV1Transaction(alice.address, { innerTransaction: innerTransaction, }); relayedTransaction.nonce = 2627n; relayedTransaction.options = 2; relayedTransaction.guardian = frank.address; - const serializedRelayedTransaction = transactionComputer.computeBytesForSigning(relayedTransaction); - relayedTransaction.signature = await alice.signer.sign(serializedRelayedTransaction); - relayedTransaction.guardianSignature = await frank.signer.sign(serializedRelayedTransaction); + relayedTransaction.signature = alice.signTransaction(relayedTransaction); + relayedTransaction.guardianSignature = frank.signTransaction(relayedTransaction); assert.equal( Buffer.from(relayedTransaction.data).toString(), @@ -215,7 +209,7 @@ describe("test relayed transactions factory", function () { }); assert.throws(() => { - factory.createRelayedV2Transaction(carol.getAddress(), { + factory.createRelayedV2Transaction(carol.address, { innerTransaction: innerTransaction, innerTransactionGasLimit: 50000n, }), @@ -225,7 +219,7 @@ describe("test relayed transactions factory", function () { innerTransaction.gasLimit = 0n; assert.throws(() => { - factory.createRelayedV2Transaction(carol.getAddress(), { + factory.createRelayedV2Transaction(carol.address, { innerTransaction: innerTransaction, innerTransactionGasLimit: 50000n, }), @@ -245,17 +239,15 @@ describe("test relayed transactions factory", function () { options: 0, }); - const serializedInnerTransaction = transactionComputer.computeBytesForSigning(innerTransaction); - innerTransaction.signature = await bob.signer.sign(serializedInnerTransaction); + innerTransaction.signature = bob.signTransaction(innerTransaction); - const relayedTransaction = factory.createRelayedV2Transaction(alice.getAddress(), { + const relayedTransaction = factory.createRelayedV2Transaction(alice.address, { innerTransaction: innerTransaction, innerTransactionGasLimit: 60000000n, }); relayedTransaction.nonce = 37n; - const serializedRelayedTransaction = transactionComputer.computeBytesForSigning(relayedTransaction); - relayedTransaction.signature = await alice.signer.sign(serializedRelayedTransaction); + relayedTransaction.signature = alice.signTransaction(relayedTransaction); assert.equal(relayedTransaction.version, 2); assert.equal(relayedTransaction.options, 0); diff --git a/src/testutils/utils.ts b/src/testutils/utils.ts index 9a6f16ad..a97e895a 100644 --- a/src/testutils/utils.ts +++ b/src/testutils/utils.ts @@ -1,14 +1,14 @@ import * as fs from "fs"; import { PathLike } from "fs"; +import { resolve } from "path"; import { AbiRegistry, Code, SmartContract, TypedValue } from "../abi"; +import { Account } from "../accounts"; import { Transaction } from "../core/transaction"; -import { TransactionComputer } from "../core/transactionComputer"; import { TransactionWatcher } from "../core/transactionWatcher"; import { getAxios } from "../core/utils"; -import { TestWallet } from "./wallets"; export async function prepareDeployment(obj: { - deployer: TestWallet; + deployer: Account; contract: SmartContract; codePath: string; initArguments: TypedValue[]; @@ -26,13 +26,12 @@ export async function prepareDeployment(obj: { deployer: deployer.address, }); - const computer = new TransactionComputer(); - let nonce = deployer.account.getNonceThenIncrement(); + let nonce = deployer.getNonceThenIncrement(); let contractAddress = SmartContract.computeAddress(deployer.address, nonce); transaction.nonce = nonce; transaction.sender = deployer.address; contract.setAddress(contractAddress); - transaction.signature = await deployer.signer.sign(computer.computeBytesForSigning(transaction)); + transaction.signature = deployer.signTransaction(transaction); return transaction; } @@ -99,8 +98,8 @@ export function b64ToHex(value: string): string { return Buffer.from(value, "base64").toString("hex"); } -export function importJsonBig(value: string): string { - return Buffer.from(value, "base64").toString("hex"); +export function getTestWalletsPath(): string { + return resolve(__dirname, "..", "testdata", "testwallets"); } export const stringifyBigIntJSON = (jsonString: any): any => {