Skip to content

Fix outdated hardware wallet signing example #4084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions packages/tx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,34 +445,57 @@ Here is an example of signing txs with `@ledgerhq/hw-app-eth` as of `v6.5.0`:

```ts
import { Chain, Common } from '@ethereumjs/common'
import { LegacyTransaction, FeeMarketEIP1559Transaction } from '@ethereumjs/tx'
import { createLegacyTx, createFeeMarket1559Tx } from '@ethereumjs/tx'
import { bytesToHex } from '@ethereumjs/util'
import { RLP } from '@ethereumjs/rlp'
import Eth from '@ledgerhq/hw-app-eth'

const eth = new Eth(transport)
const common = new Common({ chain: Chain.Sepolia })

let txData: any = { value: 1 }
let tx: LegacyTransaction | FeeMarketEIP1559Transaction
let unsignedTx: Uint8Array[] | Uint8Array
let signedTx: typeof tx
const bip32Path = "44'/60'/0'/0/0"

const legacyTxData: LegacyTxData = {
nonce: '0x0',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
}

const eip1559TxData: FeeMarketEIP1559TxData = {
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
gasLimit: '0x02625a00',
maxPriorityFeePerGas: '0x01',
maxFeePerGas: '0xff',
nonce: '0x00',
to: '0xcccccccccccccccccccccccccccccccccccccccc',
value: '0x0186a0',
v: '0x01',
r: '0xafb6e247b1c490e284053c87ab5f6b59e219d51f743f7a4d83e400782bc7e4b9',
s: '0x479a268e0e0acd4de3f1e28e4fac2a6b32a4195e8dfa9d19147abe8807aa6f64',
chainId: '0x01',
accessList: [],
type: '0x02',
}

const run = async () => {
// Signing a legacy tx
tx = LegacyTransaction.fromTxData(txData, { common })
tx = tx.getMessageToSign()
let tx = createLegacyTx(legacyTxData, { common })
unsignedTx = tx.getMessageToSign()
// ledger signTransaction API expects it to be serialized
let { v, r, s } = await eth.signTransaction(bip32Path, RLP.encode(tx))
let { v, r, s } = await eth.signTransaction(bip32Path, RLP.encode(unsignedTx))
tx.addSignature(v, r, s, true)
let from = tx.getSenderAddress().toString()
console.log(`signedTx: ${bytesToHex(tx.serialize())}\nfrom: ${from}`)

// Signing a 1559 tx
txData = { value: 1 }
tx = FeeMarketEIP1559Transaction.fromTxData(txData, { common })
tx = tx.getMessageToSign()
tx = createFeeMarket1559Tx(eip1559TxData, { common })
unsignedTx = tx.getMessageToSign()
;({ v, r, s } = await eth.signTransaction(bip32Path, unsignedTx)) // this syntax is: object destructuring - assignment without declaration
tx.addSignature(v, r, s)
from = tx.getSenderAddress().toString()
Expand Down
Loading