-
Notifications
You must be signed in to change notification settings - Fork 816
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
316d6c0
Fix hardware wallet signing example in tx documentation
vedant-asati 1796818
Complete example update
acolytec3 8538ddd
remove signature from unsigned tx
vedant-asati e0875f3
fix ledger example
vedant-asati 17507d5
clean up example
acolytec3 4f2443e
lint
acolytec3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Common, Sepolia } from '@ethereumjs/common' | ||
import { RLP } from '@ethereumjs/rlp' | ||
import { | ||
type FeeMarketEIP1559TxData, | ||
type LegacyTxData, | ||
createFeeMarket1559Tx, | ||
createLegacyTx, | ||
} from '@ethereumjs/tx' | ||
import { bytesToHex } from '@ethereumjs/util' | ||
import Eth from '@ledgerhq/hw-app-eth' | ||
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid' | ||
|
||
const transport = await TransportNodeHid.default.open() | ||
const eth = new Eth.default(transport) | ||
const common = new Common({ chain: Sepolia }) | ||
|
||
// Signing with the first key of the derivation path | ||
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', | ||
accessList: [], | ||
type: '0x02', | ||
} | ||
|
||
const run = async () => { | ||
// Signing a legacy tx | ||
const tx1 = createLegacyTx(legacyTxData, { common }) | ||
const unsignedTx1 = tx1.getMessageToSign() | ||
// Ledger signTransaction API expects it to be serialized | ||
// Ledger returns unprefixed hex strings without 0x for v, r, s values | ||
const { v, r, s } = await eth.signTransaction( | ||
bip32Path, | ||
bytesToHex(RLP.encode(unsignedTx1)).slice(2), | ||
null, | ||
) | ||
const signedTx1 = tx1.addSignature(BigInt(`0x${v}`), BigInt(`0x${r}`), BigInt(`0x${s}`)) | ||
const from = signedTx1.getSenderAddress().toString() | ||
console.log(`signedTx: ${bytesToHex(tx1.serialize())}\nfrom: ${from}`) | ||
|
||
// Signing a 1559 tx | ||
const tx2 = createFeeMarket1559Tx(eip1559TxData, { common }) | ||
// Ledger returns unprefixed hex strings without 0x for v, r, s values | ||
const unsignedTx2 = tx2.getMessageToSign() | ||
const { v2, r2, s2 } = await eth.signTransaction( | ||
bip32Path, | ||
bytesToHex(unsignedTx2).slice(2), | ||
null, | ||
) | ||
const signedTx2 = tx2.addSignature(BigInt(`0x${v2}`), BigInt(`0x${r2}`), BigInt(`0x${s2}`)) | ||
const from2 = signedTx2.getSenderAddress().toString() | ||
console.log(`signedTx: ${bytesToHex(tx2.serialize())}\nfrom: ${from2}`) | ||
} | ||
|
||
run() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not edited by this PR but the note of "Buffer" means that these edits are in very historic parts of the README and that those are also incorrect (we now use
Uint8Array
instead ofBuffer
)