Skip to content

Commit

Permalink
add isValidNonce logic inside prepareForSignOperation
Browse files Browse the repository at this point in the history
  • Loading branch information
chabroA committed Oct 23, 2023
1 parent 5bb7c4e commit 2dd4c8e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
35 changes: 24 additions & 11 deletions libs/coin-evm/src/__tests__/unit/prepareTransaction.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "../fixtures/prepareTransaction.fixtures";
import { GasOptions } from "../../types";
import * as nftAPI from "../../api/nft";
import { DEFAULT_NONCE } from "../../createTransaction";

describe("EVM Family", () => {
describe("prepareTransaction.ts", () => {
Expand Down Expand Up @@ -629,7 +630,12 @@ describe("EVM Family", () => {

describe("if transaction nonce is not already valid", () => {
it("should not change a coin transaction", async () => {
expect(await prepareForSignOperation({ account, transaction })).toEqual({
expect(
await prepareForSignOperation({
account,
transaction: { ...transaction, nonce: DEFAULT_NONCE },
}),
).toEqual({
...transaction,
nonce: 10,
});
Expand All @@ -638,20 +644,28 @@ describe("EVM Family", () => {
});

it("should update a token transaction with the correct recipient", async () => {
expect(await prepareForSignOperation({ account, transaction: tokenTransaction })).toEqual(
{
...tokenTransaction,
amount: new BigNumber(0),
recipient: tokenAccount.token.contractAddress,
nonce: 10,
},
);
expect(
await prepareForSignOperation({
account,
transaction: { ...tokenTransaction, nonce: DEFAULT_NONCE },
}),
).toEqual({
...tokenTransaction,
amount: new BigNumber(0),
recipient: tokenAccount.token.contractAddress,
nonce: 10,
});

expect(mockedNodeApi.getTransactionCount).toHaveBeenCalledTimes(1);
});

it("should update an NFT transaction with the correct recipient", async () => {
expect(await prepareForSignOperation({ account, transaction: nftTransaction })).toEqual({
expect(
await prepareForSignOperation({
account,
transaction: { ...nftTransaction, nonce: DEFAULT_NONCE },
}),
).toEqual({
...nftTransaction,
amount: new BigNumber(0),
recipient: nftTransaction.nft.contract,
Expand All @@ -668,7 +682,6 @@ describe("EVM Family", () => {
await prepareForSignOperation({
account,
transaction: { ...transaction, nonce: 1 },
isValidNonce: true,
}),
).toEqual({
...transaction,
Expand Down
22 changes: 10 additions & 12 deletions libs/coin-evm/src/prepareTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { isEqual } from "lodash";
import BigNumber from "bignumber.js";
import { Account, TokenAccount } from "@ledgerhq/types-live";
import { findSubAccountById } from "@ledgerhq/coin-framework/account/index";
import { getAdditionalLayer2Fees, getEstimatedFees, isNftTransaction } from "./logic";
import { EvmNftTransaction, Transaction as EvmTransaction, FeeData, Strategy } from "./types";
import { getTransactionData, getTypedTransaction } from "./transaction";
import { validateRecipient } from "./getTransactionStatus";
import { Account, TokenAccount } from "@ledgerhq/types-live";
import BigNumber from "bignumber.js";
import { isEqual } from "lodash";
import { getNftCollectionMetadata } from "./api/nft";
import { getNodeApi } from "./api/node/index";
import { DEFAULT_NONCE } from "./createTransaction";
import { validateRecipient } from "./getTransactionStatus";
import { getAdditionalLayer2Fees, getEstimatedFees, isNftTransaction } from "./logic";
import { getTransactionData, getTypedTransaction } from "./transaction";
import { EvmNftTransaction, Transaction as EvmTransaction, FeeData, Strategy } from "./types";

/**
* Prepare basic coin transactions or smart contract interactions (other than live ERC20 transfers)
Expand Down Expand Up @@ -227,18 +228,15 @@ export const prepareTransaction = async (
export const prepareForSignOperation = async ({
account,
transaction,
/**
* specify if the nonce of the transaction should be used or replaced by the
* one returned by the node
*/
isValidNonce = false,
}: {
account: Account;
transaction: EvmTransaction;
isValidNonce?: boolean;
}): Promise<EvmTransaction> => {
const nodeApi = getNodeApi(account.currency);

const isValidNonce = transaction.nonce !== DEFAULT_NONCE;

const nonce = isValidNonce
? transaction.nonce
: await nodeApi.getTransactionCount(account.currency, account.freshAddress);
Expand Down
2 changes: 0 additions & 2 deletions libs/coin-evm/src/signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { EvmAddress, EvmSignature, EvmSigner } from "./signer";
import { prepareForSignOperation } from "./prepareTransaction";
import { getSerializedTransaction } from "./transaction";
import { Transaction } from "./types";
import { DEFAULT_NONCE } from "./createTransaction";

/**
* Transforms the ECDSA signature paremeter v hexadecimal string received
Expand Down Expand Up @@ -64,7 +63,6 @@ export const buildSignOperation =
const preparedTransaction = await prepareForSignOperation({
account,
transaction,
isValidNonce: transaction.nonce !== DEFAULT_NONCE,
});
const serializedTxHexString = getSerializedTransaction(preparedTransaction).slice(2); // Remove 0x prefix

Expand Down

0 comments on commit 2dd4c8e

Please sign in to comment.