Skip to content

Commit 2362e68

Browse files
sb-andersonholiman
authored andcommitted
accounts/usbwallet: support dynamic tx (#30180)
Adds support non-legacy transaction-signing using ledger --------- Co-authored-by: Martin Holst Swende <[email protected]>
1 parent d71a8a0 commit 2362e68

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

accounts/usbwallet/ledger.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,22 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
338338
return common.Address{}, nil, err
339339
}
340340
} else {
341-
if txrlp, err = rlp.EncodeToBytes([]interface{}{tx.Nonce(), tx.GasPrice(), tx.Gas(), tx.To(), tx.Value(), tx.Data(), chainID, big.NewInt(0), big.NewInt(0)}); err != nil {
342-
return common.Address{}, nil, err
341+
if tx.Type() == types.DynamicFeeTxType {
342+
if txrlp, err = rlp.EncodeToBytes([]interface{}{chainID, tx.Nonce(), tx.GasTipCap(), tx.GasFeeCap(), tx.Gas(), tx.To(), tx.Value(), tx.Data(), tx.AccessList()}); err != nil {
343+
return common.Address{}, nil, err
344+
}
345+
// append type to transaction
346+
txrlp = append([]byte{tx.Type()}, txrlp...)
347+
} else if tx.Type() == types.AccessListTxType {
348+
if txrlp, err = rlp.EncodeToBytes([]interface{}{chainID, tx.Nonce(), tx.GasPrice(), tx.Gas(), tx.To(), tx.Value(), tx.Data(), tx.AccessList()}); err != nil {
349+
return common.Address{}, nil, err
350+
}
351+
// append type to transaction
352+
txrlp = append([]byte{tx.Type()}, txrlp...)
353+
} else if tx.Type() == types.LegacyTxType {
354+
if txrlp, err = rlp.EncodeToBytes([]interface{}{tx.Nonce(), tx.GasPrice(), tx.Gas(), tx.To(), tx.Value(), tx.Data(), chainID, big.NewInt(0), big.NewInt(0)}); err != nil {
355+
return common.Address{}, nil, err
356+
}
343357
}
344358
}
345359
payload := append(path, txrlp...)
@@ -353,7 +367,9 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
353367
// Chunk size selection to mitigate an underlying RLP deserialization issue on the ledger app.
354368
// https://github.com/LedgerHQ/app-ethereum/issues/409
355369
chunk := 255
356-
for ; len(payload)%chunk <= ledgerEip155Size; chunk-- {
370+
if tx.Type() == types.LegacyTxType {
371+
for ; len(payload)%chunk <= ledgerEip155Size; chunk-- {
372+
}
357373
}
358374

359375
for len(payload) > 0 {
@@ -381,8 +397,11 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
381397
if chainID == nil {
382398
signer = new(types.HomesteadSigner)
383399
} else {
384-
signer = types.NewEIP155Signer(chainID)
385-
signature[64] -= byte(chainID.Uint64()*2 + 35)
400+
signer = types.LatestSignerForChainID(chainID)
401+
// For non-legacy transactions, V is 0 or 1, no need to subtract here.
402+
if tx.Type() == types.LegacyTxType {
403+
signature[64] -= byte(chainID.Uint64()*2 + 35)
404+
}
386405
}
387406
signed, err := tx.WithSignature(signer, signature)
388407
if err != nil {

0 commit comments

Comments
 (0)