Skip to content

Commit b1abbe5

Browse files
committed
⬆️ Upgrade to go-ethereum 1.9.25
Since go-ethereum 1.9.25, method TransactOpts.Sign no longer takes as input a types.Signer object. Therefore, we let our TransactOpts factories embed the signer into the TransactOpts object. Signed-off-by: Matthias Geihs <matthias@perun.network>
1 parent 6bb5f58 commit b1abbe5

File tree

9 files changed

+141
-68
lines changed

9 files changed

+141
-68
lines changed

backend/ethereum/channel/funder_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ethereum/go-ethereum/accounts"
2525
"github.com/ethereum/go-ethereum/accounts/abi/bind"
2626
"github.com/ethereum/go-ethereum/common"
27+
"github.com/ethereum/go-ethereum/core/types"
2728
"github.com/pkg/errors"
2829
"github.com/stretchr/testify/assert"
2930
"github.com/stretchr/testify/require"
@@ -330,7 +331,7 @@ func newNFunders(
330331
simBackend.FundAddress(ctx, deployAccount.Address)
331332
tokenAcc := &ksWallet.NewRandomAccount(rng).(*keystore.Account).Account
332333
simBackend.FundAddress(ctx, tokenAcc.Address)
333-
cb := ethchannel.NewContractBackend(simBackend, keystore.NewTransactor(*ksWallet))
334+
cb := ethchannel.NewContractBackend(simBackend, keystore.NewTransactor(*ksWallet, types.NewEIP155Signer(big.NewInt(1337))))
334335

335336
// Deploy ETHAssetholder
336337
assetAddr1, err := ethchannel.DeployETHAssetholder(ctx, cb, deployAccount.Address, *deployAccount)

backend/ethereum/channel/test/setup.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ package test
1616

1717
import (
1818
"context"
19+
"math/big"
1920
"math/rand"
2021
"testing"
2122
"time"
2223

2324
"github.com/ethereum/go-ethereum/accounts"
2425
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/core/types"
2527
"github.com/stretchr/testify/require"
2628

2729
ethchannel "perun.network/go-perun/backend/ethereum/channel"
@@ -63,7 +65,7 @@ func NewSimSetup(rng *rand.Rand) *SimSetup {
6365
defer cancel()
6466
simBackend.FundAddress(ctx, txAccount.Account.Address)
6567

66-
contractBackend := ethchannel.NewContractBackend(simBackend, keystore.NewTransactor(*ksWallet))
68+
contractBackend := ethchannel.NewContractBackend(simBackend, keystore.NewTransactor(*ksWallet, types.NewEIP155Signer(big.NewInt(1337))))
6769

6870
return &SimSetup{
6971
SimBackend: simBackend,
@@ -102,7 +104,7 @@ func NewSetup(t *testing.T, rng *rand.Rand, n int) *Setup {
102104
s.Parts[i] = s.Accs[i].Address()
103105
s.SimBackend.FundAddress(ctx, s.Accs[i].Account.Address)
104106
s.Recvs[i] = ksWallet.NewRandomAccount(rng).Address().(*ethwallet.Address)
105-
cb := ethchannel.NewContractBackend(s.SimBackend, keystore.NewTransactor(*ksWallet))
107+
cb := ethchannel.NewContractBackend(s.SimBackend, keystore.NewTransactor(*ksWallet, types.NewEIP155Signer(big.NewInt(1337))))
106108
accounts := map[ethchannel.Asset]accounts.Account{asset: s.Accs[i].Account}
107109
depositors := map[ethchannel.Asset]ethchannel.Depositor{asset: new(ethchannel.ETHDepositor)}
108110
s.Funders[i] = ethchannel.NewFunder(cb, accounts, depositors)

backend/ethereum/channel/test/transactor.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,26 @@ import (
3131

3232
// TransactorSetup holds the setup for running generic tests on a transactor implementation.
3333
type TransactorSetup struct {
34+
Signer types.Signer
35+
ChainID int64
3436
Tr channel.Transactor
3537
ValidAcc accounts.Account // wallet should contain key corresponding to this account.
3638
MissingAcc accounts.Account // wallet should not contain key corresponding to this account.
3739
}
3840

39-
// GenericLegacyTransactorTest tests that a transactor works with Frontier and
40-
// Homestead signers.
41-
func GenericLegacyTransactorTest(t *testing.T, rng *rand.Rand, s TransactorSetup) {
42-
// Both can only deal with chainID == 0.
43-
GenericSignerTest(t, rng, s, &types.FrontierSigner{}, 0)
44-
GenericSignerTest(t, rng, s, &types.HomesteadSigner{}, 0)
45-
}
46-
47-
// GenericEIP155TransactorTest tests that a transactor works with EIP155 signers.
48-
func GenericEIP155TransactorTest(t *testing.T, rng *rand.Rand, s TransactorSetup) {
49-
id := rng.Int63() // Any id will work.
50-
GenericSignerTest(t, rng, s, types.NewEIP155Signer(big.NewInt(id)), id)
51-
}
52-
5341
// GenericSignerTest tests that a transactor produces the correct signatures
5442
// for the passed signer.
55-
func GenericSignerTest(t *testing.T, rng *rand.Rand, setup TransactorSetup, signer types.Signer, chainID int64) {
43+
func GenericSignerTest(t *testing.T, rng *rand.Rand, setup TransactorSetup) {
44+
signer := setup.Signer
45+
chainID := setup.ChainID
5646
data := make([]byte, rng.Int31n(100)+1)
5747
rng.Read(data)
5848

5949
t.Run("happy", func(t *testing.T) {
6050
transactOpts, err := setup.Tr.NewTransactor(setup.ValidAcc)
6151
require.NoError(t, err)
6252
rawTx := types.NewTransaction(uint64(1), common.Address{}, big.NewInt(1), uint64(1), big.NewInt(1), data)
63-
signedTx, err := transactOpts.Signer(signer, setup.ValidAcc.Address, rawTx)
53+
signedTx, err := transactOpts.Signer(setup.ValidAcc.Address, rawTx)
6454
assert.NoError(t, err)
6555
require.NotNil(t, signedTx)
6656

@@ -83,7 +73,7 @@ func GenericSignerTest(t *testing.T, rng *rand.Rand, setup TransactorSetup, sign
8373
require.NoError(t, err)
8474

8575
rawTx := types.NewTransaction(uint64(1), common.Address{}, big.NewInt(1), uint64(1), big.NewInt(1), data)
86-
_, err = transactOpts.Signer(signer, setup.MissingAcc.Address, rawTx)
76+
_, err = transactOpts.Signer(setup.MissingAcc.Address, rawTx)
8777
assert.Error(t, err)
8878
})
8979
}

backend/ethereum/wallet/hd/transactor.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
// Transactor can be used to make TransactOpts for accounts stored in a HD wallet.
2626
type Transactor struct {
2727
Wallet accounts.Wallet
28+
Signer types.Signer
2829
}
2930

3031
type hashSigner interface {
@@ -39,7 +40,7 @@ func (t *Transactor) NewTransactor(account accounts.Account) (*bind.TransactOpts
3940
}
4041
return &bind.TransactOpts{
4142
From: account.Address,
42-
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
43+
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
4344
if address != account.Address {
4445
return nil, errors.New("not authorized to sign this account")
4546
}
@@ -55,17 +56,17 @@ func (t *Transactor) NewTransactor(account accounts.Account) (*bind.TransactOpts
5556
return t.Wallet.SignTx(account, tx, tx.ChainId())
5657
}
5758

58-
signature, err := hs.SignHash(account, signer.Hash(tx).Bytes())
59+
signature, err := hs.SignHash(account, t.Signer.Hash(tx).Bytes())
5960
if err != nil {
6061
return nil, err
6162
}
62-
return tx.WithSignature(signer, signature)
63+
return tx.WithSignature(t.Signer, signature)
6364
},
6465
}, nil
6566
}
6667

6768
// NewTransactor returns a backend that can make TransactOpts for accounts
6869
// contained in the given ethereum wallet.
69-
func NewTransactor(w accounts.Wallet) *Transactor {
70-
return &Transactor{Wallet: w}
70+
func NewTransactor(w accounts.Wallet, signer types.Signer) *Transactor {
71+
return &Transactor{Wallet: w, Signer: signer}
7172
}

backend/ethereum/wallet/hd/transactor_test.go

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/core/types"
2525
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
26-
"github.com/stretchr/testify/assert"
2726
"github.com/stretchr/testify/require"
2827

2928
"perun.network/go-perun/backend/ethereum/channel/test"
@@ -38,24 +37,58 @@ const missingAddr = "0x1"
3837
// noSignHash is used to hide the SignHash function of the embedded wallet.
3938
type noSignHash struct {
4039
*hdwallet.Wallet
41-
SignTxCalled bool
4240
}
4341

4442
func TestTransactor(t *testing.T) {
4543
rng := pkgtest.Prng(t)
46-
// The normal transactor should be able to deal with all signers.
47-
s := newTransactorSetup(t, rng, false)
48-
test.GenericLegacyTransactorTest(t, rng, s)
49-
test.GenericEIP155TransactorTest(t, rng, s)
44+
chainID := rng.Int63()
5045

51-
// The noSignHasher should only work with Frontier and Homestead.
52-
s = newTransactorSetup(t, rng, true)
53-
test.GenericLegacyTransactorTest(t, rng, s)
54-
assert.True(t, s.Tr.(*hd.Transactor).Wallet.(*noSignHash).SignTxCalled)
46+
tests := []struct {
47+
title string
48+
signer types.Signer
49+
chainID int64
50+
hideSignHash bool
51+
}{
52+
{
53+
title: "FrontierSigner",
54+
signer: &types.FrontierSigner{},
55+
chainID: 0,
56+
},
57+
{
58+
title: "HomesteadSigner",
59+
signer: &types.HomesteadSigner{},
60+
chainID: 0,
61+
},
62+
{
63+
title: "FrontierSigner (hideSignHash)",
64+
signer: &types.FrontierSigner{},
65+
chainID: 0,
66+
hideSignHash: true,
67+
},
68+
{
69+
title: "HomesteadSigner (hideSignHash)",
70+
signer: &types.HomesteadSigner{},
71+
chainID: 0,
72+
hideSignHash: true,
73+
},
74+
{
75+
title: "EIP155Signer",
76+
signer: types.NewEIP155Signer(big.NewInt(chainID)),
77+
chainID: chainID,
78+
},
79+
}
80+
81+
for _, _t := range tests {
82+
_t := _t
83+
t.Run(_t.title, func(t *testing.T) {
84+
s := newTransactorSetup(t, rng, _t.hideSignHash, _t.signer, _t.chainID)
85+
test.GenericSignerTest(t, rng, s)
86+
})
87+
}
5588
}
5689

5790
// nolint:interfacer // rand.Rand is preferred over io.Reader here.
58-
func newTransactorSetup(t *testing.T, prng *rand.Rand, hideSignHash bool) test.TransactorSetup {
91+
func newTransactorSetup(t *testing.T, prng *rand.Rand, hideSignHash bool, signer types.Signer, chainID int64) test.TransactorSetup {
5992
walletSeed := make([]byte, 20)
6093
prng.Read(walletSeed)
6194
mnemonic, err := hdwallet.NewMnemonicFromEntropy(walletSeed)
@@ -67,7 +100,7 @@ func newTransactorSetup(t *testing.T, prng *rand.Rand, hideSignHash bool) test.T
67100

68101
var wrappedWallet accounts.Wallet = rawHDWallet
69102
if hideSignHash {
70-
wrappedWallet = &noSignHash{rawHDWallet, false}
103+
wrappedWallet = &noSignHash{rawHDWallet}
71104
}
72105
hdWallet, err := hd.NewWallet(wrappedWallet, hd.DefaultRootDerivationPath.String(), 0)
73106
require.NoError(t, err)
@@ -78,7 +111,9 @@ func newTransactorSetup(t *testing.T, prng *rand.Rand, hideSignHash bool) test.T
78111
require.NotNil(t, validAcc)
79112

80113
return test.TransactorSetup{
81-
Tr: hd.NewTransactor(hdWallet.Wallet()),
114+
Signer: signer,
115+
ChainID: chainID,
116+
Tr: hd.NewTransactor(hdWallet.Wallet(), signer),
82117
ValidAcc: accounts.Account{Address: wallet.AsEthAddr(validAcc.Address())},
83118
MissingAcc: accounts.Account{Address: common.HexToAddress(missingAddr)},
84119
}
@@ -87,9 +122,3 @@ func newTransactorSetup(t *testing.T, prng *rand.Rand, hideSignHash bool) test.T
87122
// SignHash hides the public SignHash.
88123
func (*noSignHash) SignHash() {
89124
}
90-
91-
// SignTx calls SignTx of the embedded wallet and sets SignTxCalled to true.
92-
func (n *noSignHash) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
93-
n.SignTxCalled = true
94-
return n.Wallet.SignTx(account, tx, chainID)
95-
}

backend/ethereum/wallet/keystore/transactor.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ import (
1818
"github.com/ethereum/go-ethereum/accounts"
1919
"github.com/ethereum/go-ethereum/accounts/abi/bind"
2020
"github.com/ethereum/go-ethereum/accounts/keystore"
21+
"github.com/ethereum/go-ethereum/common"
22+
"github.com/ethereum/go-ethereum/core/types"
2123
"github.com/pkg/errors"
2224
)
2325

2426
// Transactor can be used to make TransactOpts for accounts stored in a keystore.
2527
type Transactor struct {
26-
Ks *keystore.KeyStore
28+
Ks *keystore.KeyStore
29+
Signer types.Signer
2730
}
2831

2932
// NewTransactor returns a TransactOpts for the given account. It errors if the account is
@@ -32,11 +35,23 @@ func (t *Transactor) NewTransactor(account accounts.Account) (*bind.TransactOpts
3235
if !t.Ks.HasAddress(account.Address) {
3336
return nil, errors.New("the wallet does not contain the keys for the given account")
3437
}
35-
tr, err := bind.NewKeyStoreTransactor(t.Ks, account)
36-
return tr, errors.WithStack(err)
38+
return &bind.TransactOpts{
39+
From: account.Address,
40+
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
41+
keystore, signer := t.Ks, t.Signer
42+
if address != account.Address {
43+
return nil, bind.ErrNotAuthorized
44+
}
45+
signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
46+
if err != nil {
47+
return nil, err
48+
}
49+
return tx.WithSignature(signer, signature)
50+
},
51+
}, nil
3752
}
3853

3954
// NewTransactor returns a backend that can make TransactOpts for accounts contained in the given keystore.
40-
func NewTransactor(w Wallet) *Transactor {
41-
return &Transactor{Ks: w.Ks}
55+
func NewTransactor(w Wallet, s types.Signer) *Transactor {
56+
return &Transactor{Ks: w.Ks, Signer: s}
4257
}

backend/ethereum/wallet/keystore/transactor_test.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
package keystore_test
1616

1717
import (
18+
"math/big"
1819
"math/rand"
1920
"testing"
2021

2122
"github.com/ethereum/go-ethereum/accounts"
2223
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/core/types"
2325
"github.com/stretchr/testify/require"
2426

25-
ethchanneltest "perun.network/go-perun/backend/ethereum/channel/test"
27+
"perun.network/go-perun/backend/ethereum/channel/test"
2628
"perun.network/go-perun/backend/ethereum/wallet"
2729
"perun.network/go-perun/backend/ethereum/wallet/keystore"
2830
_ "perun.network/go-perun/backend/ethereum/wallet/test"
@@ -34,18 +36,48 @@ import (
3436
const randomAddr = "0x1"
3537

3638
func TestTxOptsBackend(t *testing.T) {
37-
prng := pkgtest.Prng(t)
38-
s := newTransactorSetup(t, prng)
39-
ethchanneltest.GenericEIP155TransactorTest(t, prng, s)
40-
ethchanneltest.GenericLegacyTransactorTest(t, prng, s)
39+
rng := pkgtest.Prng(t)
40+
chainID := rng.Int63()
41+
42+
tests := []struct {
43+
title string
44+
signer types.Signer
45+
chainID int64
46+
}{
47+
{
48+
title: "FrontierSigner",
49+
signer: &types.FrontierSigner{},
50+
chainID: 0,
51+
},
52+
{
53+
title: "HomesteadSigner",
54+
signer: &types.HomesteadSigner{},
55+
chainID: 0,
56+
},
57+
{
58+
title: "EIP155Signer",
59+
signer: types.NewEIP155Signer(big.NewInt(chainID)),
60+
chainID: chainID,
61+
},
62+
}
63+
64+
for _, _t := range tests {
65+
_t := _t
66+
t.Run(_t.title, func(t *testing.T) {
67+
s := newTransactorSetup(t, rng, _t.signer, _t.chainID)
68+
test.GenericSignerTest(t, rng, s)
69+
})
70+
}
4171
}
4272

43-
func newTransactorSetup(t require.TestingT, prng *rand.Rand) ethchanneltest.TransactorSetup {
73+
func newTransactorSetup(t require.TestingT, prng *rand.Rand, signer types.Signer, chainID int64) test.TransactorSetup {
4474
ksWallet, ok := wallettest.RandomWallet().(*keystore.Wallet)
4575
require.Truef(t, ok, "random wallet in wallettest should be a keystore wallet")
4676
acc := wallettest.NewRandomAccount(prng)
47-
return ethchanneltest.TransactorSetup{
48-
Tr: keystore.NewTransactor(*ksWallet),
77+
return test.TransactorSetup{
78+
Signer: signer,
79+
ChainID: chainID,
80+
Tr: keystore.NewTransactor(*ksWallet, signer),
4981
ValidAcc: accounts.Account{Address: wallet.AsEthAddr(acc.Address())},
5082
MissingAcc: accounts.Account{Address: common.HexToAddress(randomAddr)},
5183
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ module perun.network/go-perun
33
go 1.15
44

55
require (
6-
github.com/ethereum/go-ethereum v1.9.23
6+
github.com/ethereum/go-ethereum v1.9.25
77
github.com/miguelmota/go-ethereum-hdwallet v0.0.0-20200123000308-a60dcd172b4c
88
github.com/pkg/errors v0.9.1
99
github.com/sirupsen/logrus v1.7.0
1010
github.com/stretchr/testify v1.6.1
1111
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca
12-
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
13-
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
12+
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9
13+
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
1414
)

0 commit comments

Comments
 (0)