Skip to content

Commit e7def66

Browse files
committed
core/types: add Transaction.From for deposit txs (#571)
1 parent 033a5b1 commit e7def66

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

core/types/deposit_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/ethereum/go-ethereum/accounts/abi"
2424
"github.com/ethereum/go-ethereum/common"
25+
"github.com/stretchr/testify/require"
2526
)
2627

2728
var (
@@ -93,3 +94,18 @@ func FuzzUnpackIntoDeposit(f *testing.F) {
9394
}
9495
})
9596
}
97+
98+
func TestDepositTxFrom(t *testing.T) {
99+
from := common.HexToAddress("0x1234567890123456789012345678901234567890")
100+
dtx := &DepositTx{From: from}
101+
tx := NewTx(dtx)
102+
require.Equal(t, from, tx.From())
103+
104+
dtxwn := &depositTxWithNonce{DepositTx: *dtx}
105+
tx = NewTx(dtxwn)
106+
require.Equal(t, from, tx.From())
107+
108+
dftx := new(DynamicFeeTx)
109+
tx = NewTx(dftx)
110+
require.Panics(t, func() { tx.From() })
111+
}

core/types/deposit_tx.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (tx *DepositTx) gasTipCap() *big.Int { return new(big.Int) }
7777
func (tx *DepositTx) gasPrice() *big.Int { return new(big.Int) }
7878
func (tx *DepositTx) value() *big.Int { return tx.Value }
7979
func (tx *DepositTx) nonce() uint64 { return 0 }
80+
func (tx *DepositTx) from() common.Address { return tx.From }
8081
func (tx *DepositTx) to() *common.Address { return tx.To }
8182
func (tx *DepositTx) isSystemTx() bool { return tx.IsSystemTransaction }
8283

@@ -105,3 +106,14 @@ func (tx *DepositTx) encode(b *bytes.Buffer) error {
105106
func (tx *DepositTx) decode(input []byte) error {
106107
return rlp.DecodeBytes(input, tx)
107108
}
109+
110+
// From is an OP-Stack addition to the Transaction type to easily get a deposit
111+
// transaction sender address.
112+
// It can be difficult to create a correct signer just to extract the From field
113+
// from a deposit transaction if the chain ID is not known.
114+
func (tx *Transaction) From() common.Address {
115+
if tx.Type() != DepositTxType {
116+
panic("From() called on non-deposit transaction")
117+
}
118+
return tx.inner.(interface{ from() common.Address }).from()
119+
}

0 commit comments

Comments
 (0)