forked from hyperledger-labs/go-perun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
transactor.go
72 lines (64 loc) 路 2.59 KB
/
transactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2020 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hd
import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
)
// Transactor can be used to make TransactOpts for accounts stored in a HD wallet.
type Transactor struct {
Wallet accounts.Wallet
Signer types.Signer
}
type hashSigner interface {
SignHash(account accounts.Account, hash []byte) ([]byte, error)
}
// NewTransactor returns a TransactOpts for the given account. It errors if the account is
// not contained in the wallet used for initializing transactor backend.
func (t *Transactor) NewTransactor(account accounts.Account) (*bind.TransactOpts, error) {
if !t.Wallet.Contains(account) {
return nil, errors.New("account not found in wallet")
}
return &bind.TransactOpts{
From: account.Address,
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != account.Address {
return nil, errors.New("not authorized to sign this account")
}
hs, ok := t.Wallet.(hashSigner)
if !ok {
// signer.Hash returns the hash of the tx according to the chain
// configuration but the accounts.Wallet interface only contains methods
// SignData and SignText, which also do hashing. So there's no way to
// properly sign a transaction with a Wallet that doesn't have the
// SignHash method, if the tx needs to be signed according to EIP155
// rules. So in this case, use the Wallet's SignTx method.
return t.Wallet.SignTx(account, tx, tx.ChainId())
}
signature, err := hs.SignHash(account, t.Signer.Hash(tx).Bytes())
if err != nil {
return nil, err
}
return tx.WithSignature(t.Signer, signature)
},
}, nil
}
// NewTransactor returns a backend that can make TransactOpts for accounts
// contained in the given ethereum wallet.
func NewTransactor(w accounts.Wallet, signer types.Signer) *Transactor {
return &Transactor{Wallet: w, Signer: signer}
}