Skip to content

Commit 23651e4

Browse files
authored
Merge pull request #58 from sei-protocol/tony/add-back-modules
Add back account/abi/bind
2 parents fe5b96d + 3583ad6 commit 23651e4

File tree

7 files changed

+2620
-0
lines changed

7 files changed

+2620
-0
lines changed

accounts/abi/bind/auth.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package bind
18+
19+
import (
20+
"context"
21+
"crypto/ecdsa"
22+
"errors"
23+
"io"
24+
"math/big"
25+
26+
"github.com/ethereum/go-ethereum/accounts"
27+
"github.com/ethereum/go-ethereum/accounts/external"
28+
"github.com/ethereum/go-ethereum/accounts/keystore"
29+
"github.com/ethereum/go-ethereum/common"
30+
"github.com/ethereum/go-ethereum/core/types"
31+
"github.com/ethereum/go-ethereum/crypto"
32+
"github.com/ethereum/go-ethereum/log"
33+
)
34+
35+
// ErrNoChainID is returned whenever the user failed to specify a chain id.
36+
var ErrNoChainID = errors.New("no chain id specified")
37+
38+
// ErrNotAuthorized is returned when an account is not properly unlocked.
39+
var ErrNotAuthorized = errors.New("not authorized to sign this account")
40+
41+
// NewTransactor is a utility method to easily create a transaction signer from
42+
// an encrypted json key stream and the associated passphrase.
43+
//
44+
// Deprecated: Use NewTransactorWithChainID instead.
45+
func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
46+
log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID")
47+
json, err := io.ReadAll(keyin)
48+
if err != nil {
49+
return nil, err
50+
}
51+
key, err := keystore.DecryptKey(json, passphrase)
52+
if err != nil {
53+
return nil, err
54+
}
55+
return NewKeyedTransactor(key.PrivateKey), nil
56+
}
57+
58+
// NewKeyStoreTransactor is a utility method to easily create a transaction signer from
59+
// a decrypted key from a keystore.
60+
//
61+
// Deprecated: Use NewKeyStoreTransactorWithChainID instead.
62+
func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
63+
log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID")
64+
signer := types.HomesteadSigner{}
65+
return &TransactOpts{
66+
From: account.Address,
67+
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
68+
if address != account.Address {
69+
return nil, ErrNotAuthorized
70+
}
71+
signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
72+
if err != nil {
73+
return nil, err
74+
}
75+
return tx.WithSignature(signer, signature)
76+
},
77+
Context: context.Background(),
78+
}, nil
79+
}
80+
81+
// NewKeyedTransactor is a utility method to easily create a transaction signer
82+
// from a single private key.
83+
//
84+
// Deprecated: Use NewKeyedTransactorWithChainID instead.
85+
func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
86+
log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID")
87+
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
88+
signer := types.HomesteadSigner{}
89+
return &TransactOpts{
90+
From: keyAddr,
91+
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
92+
if address != keyAddr {
93+
return nil, ErrNotAuthorized
94+
}
95+
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
96+
if err != nil {
97+
return nil, err
98+
}
99+
return tx.WithSignature(signer, signature)
100+
},
101+
Context: context.Background(),
102+
}
103+
}
104+
105+
// NewTransactorWithChainID is a utility method to easily create a transaction signer from
106+
// an encrypted json key stream and the associated passphrase.
107+
func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
108+
json, err := io.ReadAll(keyin)
109+
if err != nil {
110+
return nil, err
111+
}
112+
key, err := keystore.DecryptKey(json, passphrase)
113+
if err != nil {
114+
return nil, err
115+
}
116+
return NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
117+
}
118+
119+
// NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
120+
// an decrypted key from a keystore.
121+
func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
122+
if chainID == nil {
123+
return nil, ErrNoChainID
124+
}
125+
signer := types.LatestSignerForChainID(chainID)
126+
return &TransactOpts{
127+
From: account.Address,
128+
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
129+
if address != account.Address {
130+
return nil, ErrNotAuthorized
131+
}
132+
signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
133+
if err != nil {
134+
return nil, err
135+
}
136+
return tx.WithSignature(signer, signature)
137+
},
138+
Context: context.Background(),
139+
}, nil
140+
}
141+
142+
// NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
143+
// from a single private key.
144+
func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
145+
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
146+
if chainID == nil {
147+
return nil, ErrNoChainID
148+
}
149+
signer := types.LatestSignerForChainID(chainID)
150+
return &TransactOpts{
151+
From: keyAddr,
152+
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
153+
if address != keyAddr {
154+
return nil, ErrNotAuthorized
155+
}
156+
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
157+
if err != nil {
158+
return nil, err
159+
}
160+
return tx.WithSignature(signer, signature)
161+
},
162+
Context: context.Background(),
163+
}, nil
164+
}
165+
166+
// NewClefTransactor is a utility method to easily create a transaction signer
167+
// with a clef backend.
168+
func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
169+
return &TransactOpts{
170+
From: account.Address,
171+
Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
172+
if address != account.Address {
173+
return nil, ErrNotAuthorized
174+
}
175+
return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
176+
},
177+
Context: context.Background(),
178+
}
179+
}

accounts/abi/bind/backend.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright 2015 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package bind
18+
19+
import (
20+
"context"
21+
"errors"
22+
"math/big"
23+
24+
"github.com/ethereum/go-ethereum"
25+
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/core/types"
27+
)
28+
29+
var (
30+
// ErrNoCode is returned by call and transact operations for which the requested
31+
// recipient contract to operate on does not exist in the state db or does not
32+
// have any code associated with it (i.e. self-destructed).
33+
ErrNoCode = errors.New("no contract code at given address")
34+
35+
// ErrNoPendingState is raised when attempting to perform a pending state action
36+
// on a backend that doesn't implement PendingContractCaller.
37+
ErrNoPendingState = errors.New("backend does not support pending state")
38+
39+
// ErrNoBlockHashState is raised when attempting to perform a block hash action
40+
// on a backend that doesn't implement BlockHashContractCaller.
41+
ErrNoBlockHashState = errors.New("backend does not support block hash state")
42+
43+
// ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves
44+
// an empty contract behind.
45+
ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
46+
)
47+
48+
// ContractCaller defines the methods needed to allow operating with a contract on a read
49+
// only basis.
50+
type ContractCaller interface {
51+
// CodeAt returns the code of the given account. This is needed to differentiate
52+
// between contract internal errors and the local chain being out of sync.
53+
CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
54+
55+
// CallContract executes an Ethereum contract call with the specified data as the
56+
// input.
57+
CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
58+
}
59+
60+
// PendingContractCaller defines methods to perform contract calls on the pending state.
61+
// Call will try to discover this interface when access to the pending state is requested.
62+
// If the backend does not support the pending state, Call returns ErrNoPendingState.
63+
type PendingContractCaller interface {
64+
// PendingCodeAt returns the code of the given account in the pending state.
65+
PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error)
66+
67+
// PendingCallContract executes an Ethereum contract call against the pending state.
68+
PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
69+
}
70+
71+
// BlockHashContractCaller defines methods to perform contract calls on a specific block hash.
72+
// Call will try to discover this interface when access to a block by hash is requested.
73+
// If the backend does not support the block hash state, Call returns ErrNoBlockHashState.
74+
type BlockHashContractCaller interface {
75+
// CodeAtHash returns the code of the given account in the state at the specified block hash.
76+
CodeAtHash(ctx context.Context, contract common.Address, blockHash common.Hash) ([]byte, error)
77+
78+
// CallContractAtHash executes an Ethereum contract call against the state at the specified block hash.
79+
CallContractAtHash(ctx context.Context, call ethereum.CallMsg, blockHash common.Hash) ([]byte, error)
80+
}
81+
82+
// ContractTransactor defines the methods needed to allow operating with a contract
83+
// on a write only basis. Besides the transacting method, the remainder are helpers
84+
// used when the user does not provide some needed values, but rather leaves it up
85+
// to the transactor to decide.
86+
type ContractTransactor interface {
87+
// HeaderByNumber returns a block header from the current canonical chain. If
88+
// number is nil, the latest known header is returned.
89+
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
90+
91+
// PendingCodeAt returns the code of the given account in the pending state.
92+
PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
93+
94+
// PendingNonceAt retrieves the current pending nonce associated with an account.
95+
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
96+
97+
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
98+
// execution of a transaction.
99+
SuggestGasPrice(ctx context.Context) (*big.Int, error)
100+
101+
// SuggestGasTipCap retrieves the currently suggested 1559 priority fee to allow
102+
// a timely execution of a transaction.
103+
SuggestGasTipCap(ctx context.Context) (*big.Int, error)
104+
105+
// EstimateGas tries to estimate the gas needed to execute a specific
106+
// transaction based on the current pending state of the backend blockchain.
107+
// There is no guarantee that this is the true gas limit requirement as other
108+
// transactions may be added or removed by miners, but it should provide a basis
109+
// for setting a reasonable default.
110+
EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
111+
112+
// SendTransaction injects the transaction into the pending pool for execution.
113+
SendTransaction(ctx context.Context, tx *types.Transaction) error
114+
}
115+
116+
// ContractFilterer defines the methods needed to access log events using one-off
117+
// queries or continuous event subscriptions.
118+
type ContractFilterer interface {
119+
// FilterLogs executes a log filter operation, blocking during execution and
120+
// returning all the results in one batch.
121+
//
122+
// TODO(karalabe): Deprecate when the subscription one can return past data too.
123+
FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error)
124+
125+
// SubscribeFilterLogs creates a background log filtering operation, returning
126+
// a subscription immediately, which can be used to stream the found events.
127+
SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
128+
}
129+
130+
// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
131+
type DeployBackend interface {
132+
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
133+
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
134+
}
135+
136+
// ContractBackend defines the methods needed to work with contracts on a read-write basis.
137+
type ContractBackend interface {
138+
ContractCaller
139+
ContractTransactor
140+
ContractFilterer
141+
}

0 commit comments

Comments
 (0)