From d1c5eee27f59b78d25456893e798d939eca0c78b Mon Sep 17 00:00:00 2001 From: Raul Glogovetan Date: Wed, 27 Dec 2023 16:54:15 +0200 Subject: [PATCH] Fix: `transactionObjectValidator` to ignore `nonce` --- .../transactionObjectValidator.test.ts | 38 +++++++++++++++++-- packages/@purser/core/src/constants.ts | 1 - packages/@purser/core/src/helpers.ts | 13 ++++--- packages/@purser/core/src/types.ts | 2 +- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/packages/@purser/core/__tests__/helpers/transactionObjectValidator.test.ts b/packages/@purser/core/__tests__/helpers/transactionObjectValidator.test.ts index 8f85895b..59037306 100644 --- a/packages/@purser/core/__tests__/helpers/transactionObjectValidator.test.ts +++ b/packages/@purser/core/__tests__/helpers/transactionObjectValidator.test.ts @@ -111,10 +111,6 @@ describe('`Core` Module', () => { 'chainId', TRANSACTION.CHAIN_ID, ); - expect(validatedTransactionObject).toHaveProperty( - 'nonce', - TRANSACTION.NONCE, - ); expect(validatedTransactionObject.value.toString()).toEqual( TRANSACTION.VALUE, ); @@ -164,5 +160,39 @@ describe('`Core` Module', () => { expect(addressValidator).not.toHaveBeenCalled(); expect(addressValidator).not.toHaveBeenCalledWith(to); }); + test('Validates nonce, only if one was provided', async () => { + transactionObjectValidator({ + gasPrice, + gasLimit, + chainId, + value, + inputData, + }); + /* + * Validates gas price and gas limit + */ + expect(bigNumberValidator).toHaveBeenCalled(); + expect(bigNumberValidator).toHaveBeenCalledWith(gasPrice); + expect(bigNumberValidator).toHaveBeenCalledWith(gasLimit); + /* + * Validates the chain Id + */ + expect(safeIntegerValidator).toHaveBeenCalled(); + expect(safeIntegerValidator).toHaveBeenCalledWith(chainId); + /* + * Validates the transaction value + */ + expect(bigNumberValidator).toHaveBeenCalled(); + expect(bigNumberValidator).toHaveBeenCalledWith(value); + /* + * Validates the transaction input data + */ + expect(hexSequenceValidator).toHaveBeenCalled(); + expect(hexSequenceValidator).toHaveBeenCalledWith(inputData); + /* + * Doesn't validates the nonce, since it wasn't provided + */ + expect(safeIntegerValidator).not.toHaveBeenCalledWith(nonce); + }); }); }); diff --git a/packages/@purser/core/src/constants.ts b/packages/@purser/core/src/constants.ts index dad54822..7a5ba823 100644 --- a/packages/@purser/core/src/constants.ts +++ b/packages/@purser/core/src/constants.ts @@ -101,7 +101,6 @@ export const TRANSACTION = { CHAIN_ID: 1, GAS_PRICE: '9000000000', // 9 Gwei GAS_LIMIT: '21000', - NONCE: 0, VALUE: '0', INPUT_DATA: '', }; diff --git a/packages/@purser/core/src/helpers.ts b/packages/@purser/core/src/helpers.ts index bf6f6106..4ec78ab8 100644 --- a/packages/@purser/core/src/helpers.ts +++ b/packages/@purser/core/src/helpers.ts @@ -211,7 +211,7 @@ export const transactionObjectValidator = ({ chainId = TRANSACTION.CHAIN_ID, gasPrice = bigNumber(TRANSACTION.GAS_PRICE), gasLimit = bigNumber(TRANSACTION.GAS_LIMIT), - nonce = TRANSACTION.NONCE, + nonce, to, value = bigNumber(TRANSACTION.VALUE), inputData = TRANSACTION.INPUT_DATA, @@ -228,10 +228,13 @@ export const transactionObjectValidator = ({ * Check if the chain id value is valid (a positive, safe integer) */ safeIntegerValidator(chainId); - /* - * Check if the nonce value is valid (a positive, safe integer) - */ - safeIntegerValidator(nonce); + if (nonce) { + /* + * Only check if the nonce value is valid (a positive, safe integer), + * if one was provided in the initial transaction object + */ + safeIntegerValidator(nonce); + } /* * Only check if the address (`to` prop) is in the correct * format, if one was provided in the initial transaction object diff --git a/packages/@purser/core/src/types.ts b/packages/@purser/core/src/types.ts index ddf4535a..f75640f6 100644 --- a/packages/@purser/core/src/types.ts +++ b/packages/@purser/core/src/types.ts @@ -33,7 +33,7 @@ export interface TransactionObjectType { chainId: number; gasPrice: ExtendedBN; gasLimit: ExtendedBN; - nonce: number; + nonce?: number; value: ExtendedBN; inputData: string; }