Skip to content

Commit 0d6d06b

Browse files
MariusVanDerWijdenenriquefynn
authored andcommitted
mobile: use bind.NewKeyedTransactor instead of duplicating (ethereum#20888)
It's better to reuse the existing code to create a keyed transactor than to rewrite the logic again.
1 parent ae78b30 commit 0d6d06b

File tree

1 file changed

+2
-18
lines changed

1 file changed

+2
-18
lines changed

mobile/bind.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package geth
2020

2121
import (
22-
"errors"
2322
"math/big"
2423
"strings"
2524

@@ -28,7 +27,6 @@ import (
2827
"github.com/ethereum/go-ethereum/accounts/keystore"
2928
"github.com/ethereum/go-ethereum/common"
3029
"github.com/ethereum/go-ethereum/core/types"
31-
"github.com/ethereum/go-ethereum/crypto"
3230
)
3331

3432
// Signer is an interface defining the callback when a contract requires a
@@ -82,28 +80,14 @@ func NewTransactOpts() *TransactOpts {
8280
return new(TransactOpts)
8381
}
8482

85-
// NewKeyedTransactor is a utility method to easily create a transaction signer
83+
// NewKeyedTransactOpts is a utility method to easily create a transaction signer
8684
// from a single private key.
8785
func NewKeyedTransactOpts(keyJson []byte, passphrase string) (*TransactOpts, error) {
8886
key, err := keystore.DecryptKey(keyJson, passphrase)
8987
if err != nil {
9088
return nil, err
9189
}
92-
keyAddr := crypto.PubkeyToAddress(key.PrivateKey.PublicKey)
93-
opts := bind.TransactOpts{
94-
From: keyAddr,
95-
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
96-
if address != keyAddr {
97-
return nil, errors.New("not authorized to sign this account")
98-
}
99-
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key.PrivateKey)
100-
if err != nil {
101-
return nil, err
102-
}
103-
return tx.WithSignature(signer, signature)
104-
},
105-
}
106-
return &TransactOpts{opts}, nil
90+
return &TransactOpts{*bind.NewKeyedTransactor(key.PrivateKey)}, nil
10791
}
10892

10993
func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} }

0 commit comments

Comments
 (0)