Skip to content

Commit dc2f64a

Browse files
committed
chore: prefer numberToHex
1 parent 8518908 commit dc2f64a

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

src/utils/transaction/serializeTransaction.ts

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ import {
4848
} from '../blob/toBlobSidecars.js'
4949
import { type ConcatHexErrorType, concatHex } from '../data/concat.js'
5050
import { trim } from '../data/trim.js'
51-
import { type ToHexErrorType, bytesToHex, toHex } from '../encoding/toHex.js'
51+
import {
52+
bytesToHex,
53+
numberToHex,
54+
type NumberToHexErrorType,
55+
} from '../encoding/toHex.js'
5256
import { type ToRlpErrorType, toRlp } from '../encoding/toRlp.js'
5357

5458
import {
@@ -142,7 +146,7 @@ type SerializeTransactionEIP7702ErrorType =
142146
| SerializeAuthorizationListErrorType
143147
| ConcatHexErrorType
144148
| InvalidLegacyVErrorType
145-
| ToHexErrorType
149+
| NumberToHexErrorType
146150
| ToRlpErrorType
147151
| SerializeAccessListErrorType
148152
| ErrorType
@@ -173,13 +177,13 @@ function serializeTransactionEIP7702(
173177
return concatHex([
174178
'0x04',
175179
toRlp([
176-
toHex(chainId),
177-
nonce ? toHex(nonce) : '0x',
178-
maxPriorityFeePerGas ? toHex(maxPriorityFeePerGas) : '0x',
179-
maxFeePerGas ? toHex(maxFeePerGas) : '0x',
180-
gas ? toHex(gas) : '0x',
180+
numberToHex(chainId),
181+
nonce ? numberToHex(nonce) : '0x',
182+
maxPriorityFeePerGas ? numberToHex(maxPriorityFeePerGas) : '0x',
183+
maxFeePerGas ? numberToHex(maxFeePerGas) : '0x',
184+
gas ? numberToHex(gas) : '0x',
181185
to ?? '0x',
182-
value ? toHex(value) : '0x',
186+
value ? numberToHex(value) : '0x',
183187
data ?? '0x',
184188
serializedAccessList,
185189
serializedAuthorizationList,
@@ -196,7 +200,7 @@ type SerializeTransactionEIP4844ErrorType =
196200
| ToBlobSidecarsErrorType
197201
| ConcatHexErrorType
198202
| InvalidLegacyVErrorType
199-
| ToHexErrorType
203+
| NumberToHexErrorType
200204
| ToRlpErrorType
201205
| SerializeAccessListErrorType
202206
| ErrorType
@@ -252,16 +256,16 @@ function serializeTransactionEIP4844(
252256
const serializedAccessList = serializeAccessList(accessList)
253257

254258
const serializedTransaction = [
255-
toHex(chainId),
256-
nonce ? toHex(nonce) : '0x',
257-
maxPriorityFeePerGas ? toHex(maxPriorityFeePerGas) : '0x',
258-
maxFeePerGas ? toHex(maxFeePerGas) : '0x',
259-
gas ? toHex(gas) : '0x',
259+
numberToHex(chainId),
260+
nonce ? numberToHex(nonce) : '0x',
261+
maxPriorityFeePerGas ? numberToHex(maxPriorityFeePerGas) : '0x',
262+
maxFeePerGas ? numberToHex(maxFeePerGas) : '0x',
263+
gas ? numberToHex(gas) : '0x',
260264
to ?? '0x',
261-
value ? toHex(value) : '0x',
265+
value ? numberToHex(value) : '0x',
262266
data ?? '0x',
263267
serializedAccessList,
264-
maxFeePerBlobGas ? toHex(maxFeePerBlobGas) : '0x',
268+
maxFeePerBlobGas ? numberToHex(maxFeePerBlobGas) : '0x',
265269
blobVersionedHashes ?? [],
266270
...toYParitySignatureArray(transaction, signature),
267271
] as const
@@ -291,7 +295,7 @@ type SerializeTransactionEIP1559ErrorType =
291295
| AssertTransactionEIP1559ErrorType
292296
| ConcatHexErrorType
293297
| InvalidLegacyVErrorType
294-
| ToHexErrorType
298+
| NumberToHexErrorType
295299
| ToRlpErrorType
296300
| SerializeAccessListErrorType
297301
| ErrorType
@@ -317,13 +321,13 @@ function serializeTransactionEIP1559(
317321
const serializedAccessList = serializeAccessList(accessList)
318322

319323
const serializedTransaction = [
320-
toHex(chainId),
321-
nonce ? toHex(nonce) : '0x',
322-
maxPriorityFeePerGas ? toHex(maxPriorityFeePerGas) : '0x',
323-
maxFeePerGas ? toHex(maxFeePerGas) : '0x',
324-
gas ? toHex(gas) : '0x',
324+
numberToHex(chainId),
325+
nonce ? numberToHex(nonce) : '0x',
326+
maxPriorityFeePerGas ? numberToHex(maxPriorityFeePerGas) : '0x',
327+
maxFeePerGas ? numberToHex(maxFeePerGas) : '0x',
328+
gas ? numberToHex(gas) : '0x',
325329
to ?? '0x',
326-
value ? toHex(value) : '0x',
330+
value ? numberToHex(value) : '0x',
327331
data ?? '0x',
328332
serializedAccessList,
329333
...toYParitySignatureArray(transaction, signature),
@@ -339,7 +343,7 @@ type SerializeTransactionEIP2930ErrorType =
339343
| AssertTransactionEIP2930ErrorType
340344
| ConcatHexErrorType
341345
| InvalidLegacyVErrorType
342-
| ToHexErrorType
346+
| NumberToHexErrorType
343347
| ToRlpErrorType
344348
| SerializeAccessListErrorType
345349
| ErrorType
@@ -356,12 +360,12 @@ function serializeTransactionEIP2930(
356360
const serializedAccessList = serializeAccessList(accessList)
357361

358362
const serializedTransaction = [
359-
toHex(chainId),
360-
nonce ? toHex(nonce) : '0x',
361-
gasPrice ? toHex(gasPrice) : '0x',
362-
gas ? toHex(gas) : '0x',
363+
numberToHex(chainId),
364+
nonce ? numberToHex(nonce) : '0x',
365+
gasPrice ? numberToHex(gasPrice) : '0x',
366+
gas ? numberToHex(gas) : '0x',
363367
to ?? '0x',
364-
value ? toHex(value) : '0x',
368+
value ? numberToHex(value) : '0x',
365369
data ?? '0x',
366370
serializedAccessList,
367371
...toYParitySignatureArray(transaction, signature),
@@ -376,7 +380,7 @@ function serializeTransactionEIP2930(
376380
type SerializeTransactionLegacyErrorType =
377381
| AssertTransactionLegacyErrorType
378382
| InvalidLegacyVErrorType
379-
| ToHexErrorType
383+
| NumberToHexErrorType
380384
| ToRlpErrorType
381385
| ErrorType
382386

@@ -389,11 +393,11 @@ function serializeTransactionLegacy(
389393
assertTransactionLegacy(transaction)
390394

391395
let serializedTransaction = [
392-
nonce ? toHex(nonce) : '0x',
393-
gasPrice ? toHex(gasPrice) : '0x',
394-
gas ? toHex(gas) : '0x',
396+
nonce ? numberToHex(nonce) : '0x',
397+
gasPrice ? numberToHex(gasPrice) : '0x',
398+
gas ? numberToHex(gas) : '0x',
395399
to ?? '0x',
396-
value ? toHex(value) : '0x',
400+
value ? numberToHex(value) : '0x',
397401
data ?? '0x',
398402
]
399403

@@ -421,14 +425,14 @@ function serializeTransactionLegacy(
421425

422426
serializedTransaction = [
423427
...serializedTransaction,
424-
toHex(v),
428+
numberToHex(v),
425429
r === '0x00' ? '0x' : r,
426430
s === '0x00' ? '0x' : s,
427431
]
428432
} else if (chainId > 0) {
429433
serializedTransaction = [
430434
...serializedTransaction,
431-
toHex(chainId),
435+
numberToHex(chainId),
432436
'0x',
433437
'0x',
434438
]
@@ -452,11 +456,11 @@ export function toYParitySignatureArray(
452456
const s = trim(signature.s)
453457

454458
const yParity_ = (() => {
455-
if (typeof yParity === 'number') return yParity ? toHex(1) : '0x'
459+
if (typeof yParity === 'number') return yParity ? numberToHex(1) : '0x'
456460
if (v === 0n) return '0x'
457-
if (v === 1n) return toHex(1)
461+
if (v === 1n) return numberToHex(1)
458462

459-
return v === 27n ? '0x' : toHex(1)
463+
return v === 27n ? '0x' : numberToHex(1)
460464
})()
461465

462466
return [yParity_, r === '0x00' ? '0x' : r, s === '0x00' ? '0x' : s]

0 commit comments

Comments
 (0)