Skip to content

Commit e171bf7

Browse files
committed
core/types: remove redundant SignECDSA wrappers, rename to SignTx
1 parent bbc4ea4 commit e171bf7

16 files changed

Lines changed: 58 additions & 99 deletions

‎accounts/abi/bind/util_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestWaitDeployed(t *testing.T) {
6060

6161
// Create the transaction.
6262
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
63-
tx, _ = tx.SignECDSA(types.HomesteadSigner{}, testKey)
63+
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
6464

6565
// Wait for it to get mined in the background.
6666
var (

‎core/bench_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
8383
toaddr := common.Address{}
8484
data := make([]byte, nbytes)
8585
gas := IntrinsicGas(data, false, false)
86-
tx, _ := types.NewTransaction(gen.TxNonce(benchRootAddr), toaddr, big.NewInt(1), gas, nil, data).SignECDSA(types.HomesteadSigner{}, benchRootKey)
86+
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(benchRootAddr), toaddr, big.NewInt(1), gas, nil, data), types.HomesteadSigner{}, benchRootKey)
8787
gen.AddTx(tx)
8888
}
8989
}
@@ -123,7 +123,7 @@ func genTxRing(naccounts int) func(int, *BlockGen) {
123123
nil,
124124
nil,
125125
)
126-
tx, _ = tx.SignECDSA(types.HomesteadSigner{}, ringKeys[from])
126+
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, ringKeys[from])
127127
gen.AddTx(tx)
128128
from = to
129129
}

‎core/blockchain_test.go‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ func TestFastVsFullChains(t *testing.T) {
719719
// If the block number is multiple of 3, send a few bonus transactions to the miner
720720
if i%3 == 2 {
721721
for j := 0; j < i%4+1; j++ {
722-
tx, err := types.NewTransaction(block.TxNonce(address), common.Address{0x00}, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key)
722+
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{0x00}, big.NewInt(1000), params.TxGas, nil, nil), signer, key)
723723
if err != nil {
724724
panic(err)
725725
}
@@ -883,8 +883,8 @@ func TestChainTxReorgs(t *testing.T) {
883883
// Create two transactions shared between the chains:
884884
// - postponed: transaction included at a later block in the forked chain
885885
// - swapped: transaction included at the same block number in the forked chain
886-
postponed, _ := types.NewTransaction(0, addr1, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key1)
887-
swapped, _ := types.NewTransaction(1, addr1, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key1)
886+
postponed, _ := types.SignTx(types.NewTransaction(0, addr1, big.NewInt(1000), params.TxGas, nil, nil), signer, key1)
887+
swapped, _ := types.SignTx(types.NewTransaction(1, addr1, big.NewInt(1000), params.TxGas, nil, nil), signer, key1)
888888

889889
// Create two transactions that will be dropped by the forked chain:
890890
// - pastDrop: transaction dropped retroactively from a past block
@@ -900,13 +900,13 @@ func TestChainTxReorgs(t *testing.T) {
900900
chain, _ := GenerateChain(params.TestChainConfig, genesis, db, 3, func(i int, gen *BlockGen) {
901901
switch i {
902902
case 0:
903-
pastDrop, _ = types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key2)
903+
pastDrop, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key2)
904904

905905
gen.AddTx(pastDrop) // This transaction will be dropped in the fork from below the split point
906906
gen.AddTx(postponed) // This transaction will be postponed till block #3 in the fork
907907

908908
case 2:
909-
freshDrop, _ = types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key2)
909+
freshDrop, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key2)
910910

911911
gen.AddTx(freshDrop) // This transaction will be dropped in the fork from exactly at the split point
912912
gen.AddTx(swapped) // This transaction will be swapped out at the exact height
@@ -925,18 +925,18 @@ func TestChainTxReorgs(t *testing.T) {
925925
chain, _ = GenerateChain(params.TestChainConfig, genesis, db, 5, func(i int, gen *BlockGen) {
926926
switch i {
927927
case 0:
928-
pastAdd, _ = types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key3)
928+
pastAdd, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key3)
929929
gen.AddTx(pastAdd) // This transaction needs to be injected during reorg
930930

931931
case 2:
932932
gen.AddTx(postponed) // This transaction was postponed from block #1 in the original chain
933933
gen.AddTx(swapped) // This transaction was swapped from the exact current spot in the original chain
934934

935-
freshAdd, _ = types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key3)
935+
freshAdd, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key3)
936936
gen.AddTx(freshAdd) // This transaction will be added exactly at reorg time
937937

938938
case 3:
939-
futureAdd, _ = types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key3)
939+
futureAdd, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key3)
940940
gen.AddTx(futureAdd) // This transaction will be added after a full reorg
941941
}
942942
})
@@ -995,7 +995,7 @@ func TestLogReorgs(t *testing.T) {
995995
subs := evmux.Subscribe(RemovedLogsEvent{})
996996
chain, _ := GenerateChain(params.TestChainConfig, genesis, db, 2, func(i int, gen *BlockGen) {
997997
if i == 1 {
998-
tx, err := types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), big.NewInt(1000000), new(big.Int), code).SignECDSA(signer, key1)
998+
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), big.NewInt(1000000), new(big.Int), code), signer, key1)
999999
if err != nil {
10001000
t.Fatalf("failed to create tx: %v", err)
10011001
}
@@ -1035,7 +1035,7 @@ func TestReorgSideEvent(t *testing.T) {
10351035
}
10361036

10371037
replacementBlocks, _ := GenerateChain(params.TestChainConfig, genesis, db, 4, func(i int, gen *BlockGen) {
1038-
tx, err := types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), big.NewInt(1000000), new(big.Int), nil).SignECDSA(signer, key1)
1038+
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), big.NewInt(1000000), new(big.Int), nil), signer, key1)
10391039
if i == 2 {
10401040
gen.OffsetTime(-1)
10411041
}
@@ -1152,7 +1152,7 @@ func TestEIP155Transition(t *testing.T) {
11521152
tx *types.Transaction
11531153
err error
11541154
basicTx = func(signer types.Signer) (*types.Transaction, error) {
1155-
return types.NewTransaction(block.TxNonce(address), common.Address{}, new(big.Int), big.NewInt(21000), new(big.Int), nil).SignECDSA(signer, key)
1155+
return types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{}, new(big.Int), big.NewInt(21000), new(big.Int), nil), signer, key)
11561156
}
11571157
)
11581158
switch i {
@@ -1215,7 +1215,7 @@ func TestEIP155Transition(t *testing.T) {
12151215
tx *types.Transaction
12161216
err error
12171217
basicTx = func(signer types.Signer) (*types.Transaction, error) {
1218-
return types.NewTransaction(block.TxNonce(address), common.Address{}, new(big.Int), big.NewInt(21000), new(big.Int), nil).SignECDSA(signer, key)
1218+
return types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{}, new(big.Int), big.NewInt(21000), new(big.Int), nil), signer, key)
12191219
}
12201220
)
12211221
switch i {
@@ -1260,11 +1260,11 @@ func TestEIP161AccountRemoval(t *testing.T) {
12601260
)
12611261
switch i {
12621262
case 0:
1263-
tx, err = types.NewTransaction(block.TxNonce(address), theAddr, new(big.Int), big.NewInt(21000), new(big.Int), nil).SignECDSA(signer, key)
1263+
tx, err = types.SignTx(types.NewTransaction(block.TxNonce(address), theAddr, new(big.Int), big.NewInt(21000), new(big.Int), nil), signer, key)
12641264
case 1:
1265-
tx, err = types.NewTransaction(block.TxNonce(address), theAddr, new(big.Int), big.NewInt(21000), new(big.Int), nil).SignECDSA(signer, key)
1265+
tx, err = types.SignTx(types.NewTransaction(block.TxNonce(address), theAddr, new(big.Int), big.NewInt(21000), new(big.Int), nil), signer, key)
12661266
case 2:
1267-
tx, err = types.NewTransaction(block.TxNonce(address), theAddr, new(big.Int), big.NewInt(21000), new(big.Int), nil).SignECDSA(signer, key)
1267+
tx, err = types.SignTx(types.NewTransaction(block.TxNonce(address), theAddr, new(big.Int), big.NewInt(21000), new(big.Int), nil), signer, key)
12681268
}
12691269
if err != nil {
12701270
t.Fatal(err)

‎core/chain_makers_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ func ExampleGenerateChain() {
5555
switch i {
5656
case 0:
5757
// In block 1, addr1 sends addr2 some ether.
58-
tx, _ := types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(signer, key1)
58+
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil), signer, key1)
5959
gen.AddTx(tx)
6060
case 1:
6161
// In block 2, addr1 sends some more ether to addr2.
6262
// addr2 passes it on to addr3.
63-
tx1, _ := types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key1)
64-
tx2, _ := types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, key2)
63+
tx1, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key1)
64+
tx2, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key2)
6565
gen.AddTx(tx1)
6666
gen.AddTx(tx2)
6767
case 2:

‎core/tx_pool_test.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
)
3333

3434
func transaction(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
35-
tx, _ := types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, big.NewInt(1), nil).SignECDSA(types.HomesteadSigner{}, key)
35+
tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, big.NewInt(1), nil), types.HomesteadSigner{}, key)
3636
return tx
3737
}
3838

@@ -238,7 +238,7 @@ func TestRemoveTx(t *testing.T) {
238238
func TestNegativeValue(t *testing.T) {
239239
pool, key := setupTxPool()
240240

241-
tx, _ := types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil).SignECDSA(types.HomesteadSigner{}, key)
241+
tx, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil), types.HomesteadSigner{}, key)
242242
from, _ := deriveSender(tx)
243243
currentState, _ := pool.currentState()
244244
currentState.AddBalance(from, big.NewInt(1))
@@ -287,9 +287,9 @@ func TestTransactionDoubleNonce(t *testing.T) {
287287
resetState()
288288

289289
signer := types.HomesteadSigner{}
290-
tx1, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(100000), big.NewInt(1), nil).SignECDSA(signer, key)
291-
tx2, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(2), nil).SignECDSA(signer, key)
292-
tx3, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(1), nil).SignECDSA(signer, key)
290+
tx1, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(100000), big.NewInt(1), nil), signer, key)
291+
tx2, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(2), nil), signer, key)
292+
tx3, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(1), nil), signer, key)
293293

294294
// Add the first two transaction, ensure higher priced stays only
295295
if err := pool.add(tx1); err != nil {

‎core/types/transaction.go‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package types
1818

1919
import (
2020
"container/heap"
21-
"crypto/ecdsa"
2221
"encoding/json"
2322
"errors"
2423
"fmt"
@@ -293,14 +292,6 @@ func (tx *Transaction) AsMessage(s Signer) (Message, error) {
293292
return msg, err
294293
}
295294

296-
// SignECDSA signs the transaction using the given signer and private key
297-
//
298-
// XXX This only makes for a nice API: NewTx(...).SignECDSA(signer, prv). Should
299-
// we keep this?
300-
func (tx *Transaction) SignECDSA(signer Signer, prv *ecdsa.PrivateKey) (*Transaction, error) {
301-
return signer.SignECDSA(tx, prv)
302-
}
303-
304295
// WithSignature returns a new transaction with the given signature.
305296
// This signature needs to be formatted as described in the yellow paper (v+27).
306297
func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, error) {

‎core/types/transaction_signing.go‎

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func MakeSigner(config *params.ChainConfig, blockNumber *big.Int) Signer {
5050
return signer
5151
}
5252

53-
// SignECDSA signs the transaction using the given signer and private key
54-
func SignECDSA(s Signer, tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error) {
53+
// SignTx signs the transaction using the given signer and private key
54+
func SignTx(tx *Transaction, s Signer, prv *ecdsa.PrivateKey) (*Transaction, error) {
5555
h := s.Hash(tx)
5656
sig, err := crypto.Sign(h[:], prv)
5757
if err != nil {
@@ -96,9 +96,8 @@ type Signer interface {
9696
Hash(tx *Transaction) common.Hash
9797
// PubilcKey returns the public key derived from the signature
9898
PublicKey(tx *Transaction) ([]byte, error)
99-
// SignECDSA signs the transaction with the given and returns a copy of the tx
100-
SignECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error)
101-
// WithSignature returns a copy of the transaction with the given signature
99+
// WithSignature returns a copy of the transaction with the given signature.
100+
// The signature must be encoded in [R || S || V] format where V is 0 or 1.
102101
WithSignature(tx *Transaction, sig []byte) (*Transaction, error)
103102
// Checks for equality on the signers
104103
Equal(Signer) bool
@@ -124,10 +123,6 @@ func (s EIP155Signer) Equal(s2 Signer) bool {
124123
return ok && eip155.chainId.Cmp(s.chainId) == 0
125124
}
126125

127-
func (s EIP155Signer) SignECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error) {
128-
return SignECDSA(s, tx, prv)
129-
}
130-
131126
func (s EIP155Signer) PublicKey(tx *Transaction) ([]byte, error) {
132127
// if the transaction is not protected fall back to homestead signer
133128
if !tx.Protected() {
@@ -193,15 +188,6 @@ func (s EIP155Signer) Hash(tx *Transaction) common.Hash {
193188
})
194189
}
195190

196-
func (s EIP155Signer) SigECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error) {
197-
h := s.Hash(tx)
198-
sig, err := crypto.Sign(h[:], prv)
199-
if err != nil {
200-
return nil, err
201-
}
202-
return s.WithSignature(tx, sig)
203-
}
204-
205191
// HomesteadTransaction implements TransactionInterface using the
206192
// homestead rules.
207193
type HomesteadSigner struct{ FrontierSigner }
@@ -224,15 +210,6 @@ func (hs HomesteadSigner) WithSignature(tx *Transaction, sig []byte) (*Transacti
224210
return cpy, nil
225211
}
226212

227-
func (hs HomesteadSigner) SignECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error) {
228-
h := hs.Hash(tx)
229-
sig, err := crypto.Sign(h[:], prv)
230-
if err != nil {
231-
return nil, err
232-
}
233-
return hs.WithSignature(tx, sig)
234-
}
235-
236213
func (hs HomesteadSigner) PublicKey(tx *Transaction) ([]byte, error) {
237214
if tx.data.V.BitLen() > 8 {
238215
return nil, ErrInvalidSig
@@ -280,15 +257,6 @@ func (fs FrontierSigner) WithSignature(tx *Transaction, sig []byte) (*Transactio
280257
return cpy, nil
281258
}
282259

283-
func (fs FrontierSigner) SignECDSA(tx *Transaction, prv *ecdsa.PrivateKey) (*Transaction, error) {
284-
h := fs.Hash(tx)
285-
sig, err := crypto.Sign(h[:], prv)
286-
if err != nil {
287-
return nil, err
288-
}
289-
return fs.WithSignature(tx, sig)
290-
}
291-
292260
// Hash returns the hash to be sned by the sender.
293261
// It does not uniquely identify the transaction.
294262
func (fs FrontierSigner) Hash(tx *Transaction) common.Hash {

‎core/types/transaction_signing_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestEIP155Signing(t *testing.T) {
3030
addr := crypto.PubkeyToAddress(key.PublicKey)
3131

3232
signer := NewEIP155Signer(big.NewInt(18))
33-
tx, err := NewTransaction(0, addr, new(big.Int), new(big.Int), new(big.Int), nil).SignECDSA(signer, key)
33+
tx, err := SignTx(NewTransaction(0, addr, new(big.Int), new(big.Int), new(big.Int), nil), signer, key)
3434
if err != nil {
3535
t.Fatal(err)
3636
}
@@ -49,7 +49,7 @@ func TestEIP155ChainId(t *testing.T) {
4949
addr := crypto.PubkeyToAddress(key.PublicKey)
5050

5151
signer := NewEIP155Signer(big.NewInt(18))
52-
tx, err := NewTransaction(0, addr, new(big.Int), new(big.Int), new(big.Int), nil).SignECDSA(signer, key)
52+
tx, err := SignTx(NewTransaction(0, addr, new(big.Int), new(big.Int), new(big.Int), nil), signer, key)
5353
if err != nil {
5454
t.Fatal(err)
5555
}
@@ -62,7 +62,7 @@ func TestEIP155ChainId(t *testing.T) {
6262
}
6363

6464
tx = NewTransaction(0, addr, new(big.Int), new(big.Int), new(big.Int), nil)
65-
tx, err = tx.SignECDSA(HomesteadSigner{}, key)
65+
tx, err = SignTx(tx, HomesteadSigner{}, key)
6666
if err != nil {
6767
t.Fatal(err)
6868
}
@@ -121,7 +121,7 @@ func TestChainId(t *testing.T) {
121121
tx := NewTransaction(0, common.Address{}, new(big.Int), new(big.Int), new(big.Int), nil)
122122

123123
var err error
124-
tx, err = tx.SignECDSA(NewEIP155Signer(big.NewInt(1)), key)
124+
tx, err = SignTx(tx, NewEIP155Signer(big.NewInt(1)), key)
125125
if err != nil {
126126
t.Fatal(err)
127127
}

‎core/types/transaction_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestTransactionPriceNonceSort(t *testing.T) {
138138
for start, key := range keys {
139139
addr := crypto.PubkeyToAddress(key.PublicKey)
140140
for i := 0; i < 25; i++ {
141-
tx, _ := NewTransaction(uint64(start+i), common.Address{}, big.NewInt(100), big.NewInt(100), big.NewInt(int64(start+i)), nil).SignECDSA(signer, key)
141+
tx, _ := SignTx(NewTransaction(uint64(start+i), common.Address{}, big.NewInt(100), big.NewInt(100), big.NewInt(int64(start+i)), nil), signer, key)
142142
groups[addr] = append(groups[addr], tx)
143143
}
144144
}

‎eth/downloader/downloader_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (dl *downloadTester) makeChain(n int, seed byte, parent *types.Block, paren
119119
// If the block number is multiple of 3, send a bonus transaction to the miner
120120
if parent == dl.genesis && i%3 == 0 {
121121
signer := types.MakeSigner(params.TestChainConfig, block.Number())
122-
tx, err := types.NewTransaction(block.TxNonce(testAddress), common.Address{seed}, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(signer, testKey)
122+
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddress), common.Address{seed}, big.NewInt(1000), params.TxGas, nil, nil), signer, testKey)
123123
if err != nil {
124124
panic(err)
125125
}

0 commit comments

Comments
 (0)