Skip to content

Commit dc3a1bd

Browse files
committed
Add a test for type 4 transaction, refactoring
Signed-off-by: dan437 <80175477+dan437@users.noreply.github.com>
1 parent 52cfb54 commit dc3a1bd

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/utils.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { arrayify, hexlify } from '@ethersproject/bytes';
2+
import { keccak256 } from '@ethersproject/keccak256';
13
import { ChainId, NetworkType } from '@metamask/controller-utils';
24
import {
35
type TransactionMeta,
@@ -374,6 +376,13 @@ describe('src/utils.js', () => {
374376
utils.getTxHash('0x0302b75dfb9fd9eb34056af0');
375377
}).toThrow('unsupported transaction type: 3');
376378
});
379+
380+
it('computes hash for type 4 transaction', () => {
381+
const type4TxHex = '0x04deadbeef';
382+
const expectedHash = hexlify(keccak256(arrayify(type4TxHex)));
383+
const txHash = utils.getTxHash(type4TxHex);
384+
expect(txHash).toBe(expectedHash);
385+
});
377386
});
378387

379388
describe('getReturnTxHashAsap', () => {

src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ export const incrementNonceInHex = (nonceInHex: string): string => {
224224
return hexlify(Number(nonceInDec) + 1);
225225
};
226226

227+
const isType4Transaction = (signedTxHex: string) => {
228+
return typeof signedTxHex === 'string' && signedTxHex.startsWith('0x04');
229+
};
230+
227231
export const getTxHash = (signedTxHex: any) => {
228232
if (!signedTxHex) {
229233
return '';
@@ -232,7 +236,7 @@ export const getTxHash = (signedTxHex: any) => {
232236
const parsed = parse(signedTxHex);
233237
return parsed?.hash ?? '';
234238
} catch (error) {
235-
if (typeof signedTxHex === 'string' && signedTxHex.startsWith('0x04')) {
239+
if (isType4Transaction(signedTxHex)) {
236240
return hexlify(keccak256(arrayify(signedTxHex)));
237241
}
238242
throw error;

0 commit comments

Comments
 (0)