Skip to content

Commit 7e3d163

Browse files
authored
tx: fix example types (ethereumjs#3992)
* tx: fix example types * chore: undo refactor * util: properly refactor isHexString * chore: remove typeast * chore: revert to hex starts with * util: string instead of PrefixedHexString
1 parent cb48132 commit 7e3d163

File tree

6 files changed

+17
-12
lines changed

6 files changed

+17
-12
lines changed

packages/tx/examples/accessListTx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
2+
import type { AccessList2930TxData } from '@ethereumjs/tx'
23
import { createAccessList2930Tx } from '@ethereumjs/tx'
34
import { bytesToHex } from '@ethereumjs/util'
45

56
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Berlin })
67

7-
const txData = {
8+
const txData: AccessList2930TxData = {
89
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
910
gasLimit: '0x02625a00',
1011
gasPrice: '0x01',

packages/tx/examples/blobTx.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
2+
import type { BlobEIP4844TxData } from '@ethereumjs/tx'
23
import { createBlob4844Tx } from '@ethereumjs/tx'
34
import { bytesToHex } from '@ethereumjs/util'
45
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
@@ -13,12 +14,12 @@ const main = async () => {
1314
customCrypto: { kzg },
1415
})
1516

16-
const txData = {
17+
const txData: BlobEIP4844TxData = {
1718
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
1819
gasLimit: '0x02625a00',
1920
maxPriorityFeePerGas: '0x01',
2021
maxFeePerGas: '0xff',
21-
maxFeePerDataGas: '0xfff',
22+
maxFeePerBlobGas: '0xfff',
2223
nonce: '0x00',
2324
to: '0xcccccccccccccccccccccccccccccccccccccccc',
2425
value: '0x0186a0',

packages/tx/examples/legacyTx.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
2+
import type { LegacyTxData } from '@ethereumjs/tx'
23
import { createLegacyTx } from '@ethereumjs/tx'
34
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
45

5-
const txParams = {
6+
const txData: LegacyTxData = {
67
nonce: '0x0',
78
gasPrice: '0x09184e72a000',
89
gasLimit: '0x2710',
@@ -12,7 +13,7 @@ const txParams = {
1213
}
1314

1415
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
15-
const tx = createLegacyTx(txParams, { common })
16+
const tx = createLegacyTx(txData, { common })
1617

1718
const privateKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109')
1819

packages/tx/examples/londonTx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
2+
import type { FeeMarketEIP1559TxData } from '@ethereumjs/tx'
23
import { createFeeMarket1559Tx } from '@ethereumjs/tx'
34
import { bytesToHex } from '@ethereumjs/util'
45

56
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
67

7-
const txData = {
8+
const txData: FeeMarketEIP1559TxData = {
89
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
910
gasLimit: '0x02625a00',
1011
maxPriorityFeePerGas: '0x01',

packages/tx/examples/transactions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// Install the dependencies and run `npx tsx examples/transactions.ts`
44

55
import { createLegacyTx, createLegacyTxFromBytesArray } from '@ethereumjs/tx'
6-
import { bytesToHex, hexToBytes, toBytes } from '@ethereumjs/util'
6+
import type { PrefixedHexString } from '@ethereumjs/util'
7+
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
78

89
// We create an unsigned transaction.
910
// Notice we don't set the `to` field because we are creating a new contract.
@@ -38,7 +39,7 @@ console.log('--------------------')
3839
// If you got it directly from the network it will be rlp encoded.
3940
// You can decode it with the rlp module.
4041
// After that you should have something like:
41-
const rawTx = [
42+
const rawTx: PrefixedHexString[] = [
4243
'0x',
4344
'0x09184e72a000',
4445
'0x2710',
@@ -50,7 +51,7 @@ const rawTx = [
5051
'0x5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13',
5152
]
5253

53-
const tx2 = createLegacyTxFromBytesArray(rawTx.map(toBytes)) // This is also a mainnet transaction
54+
const tx2 = createLegacyTxFromBytesArray(rawTx.map(hexToBytes)) // This is also a mainnet transaction
5455

5556
// So assuming that you were able to parse the transaction, we will now get the sender's address.
5657

packages/util/src/bytes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ export const bytesToUnprefixedHex = _bytesToUnprefixedHex
2424
* @returns {Uint8Array} The converted bytes
2525
* @throws If the input is not a valid 0x-prefixed hex string
2626
*/
27-
export const hexToBytes = (hex: string) => {
27+
export const hexToBytes = (hex: string): Uint8Array => {
2828
if (!hex.startsWith('0x')) throw EthereumJSErrorWithoutCode('input string must be 0x prefixed')
2929
return nobleH2B(padToEven(stripHexPrefix(hex)))
3030
}
3131

32-
export const unprefixedHexToBytes = (hex: string) => {
32+
export const unprefixedHexToBytes = (hex: string): Uint8Array => {
3333
if (hex.startsWith('0x')) throw EthereumJSErrorWithoutCode('input string cannot be 0x prefixed')
3434
return nobleH2B(padToEven(hex))
3535
}
3636

3737
export const bytesToHex = (bytes: Uint8Array): PrefixedHexString => {
3838
if (bytes === undefined || bytes.length === 0) return '0x'
3939
const unprefixedHex = bytesToUnprefixedHex(bytes)
40-
return ('0x' + unprefixedHex) as PrefixedHexString
40+
return `0x${unprefixedHex}`
4141
}
4242

4343
// BigInt cache for the numbers 0 - 256*256-1 (two-byte bytes)

0 commit comments

Comments
 (0)