|
| 1 | +// Copyright 2018 The go-ethereum Authors |
| 2 | +// This file is part of go-ethereum. |
| 3 | +// |
| 4 | +// go-ethereum is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU 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 | +// go-ethereum 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 General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package external |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math/big" |
| 22 | + "sync" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum" |
| 25 | + "github.com/ethereum/go-ethereum/accounts" |
| 26 | + "github.com/ethereum/go-ethereum/common" |
| 27 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 28 | + "github.com/ethereum/go-ethereum/core/types" |
| 29 | + "github.com/ethereum/go-ethereum/crypto" |
| 30 | + "github.com/ethereum/go-ethereum/event" |
| 31 | + "github.com/ethereum/go-ethereum/internal/ethapi" |
| 32 | + "github.com/ethereum/go-ethereum/log" |
| 33 | + "github.com/ethereum/go-ethereum/rpc" |
| 34 | + "github.com/ethereum/go-ethereum/signer/core" |
| 35 | +) |
| 36 | + |
| 37 | +type ExternalBackend struct { |
| 38 | + signers []accounts.Wallet |
| 39 | +} |
| 40 | + |
| 41 | +func (eb *ExternalBackend) Wallets() []accounts.Wallet { |
| 42 | + return eb.signers |
| 43 | +} |
| 44 | + |
| 45 | +func NewExternalBackend(endpoint string) (*ExternalBackend, error) { |
| 46 | + signer, err := NewExternalSigner(endpoint) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + return &ExternalBackend{ |
| 51 | + signers: []accounts.Wallet{signer}, |
| 52 | + }, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (eb *ExternalBackend) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription { |
| 56 | + return event.NewSubscription(func(quit <-chan struct{}) error { |
| 57 | + <-quit |
| 58 | + return nil |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +// ExternalSigner provides an API to interact with an external signer (clef) |
| 63 | +// It proxies request to the external signer while forwarding relevant |
| 64 | +// request headers |
| 65 | +type ExternalSigner struct { |
| 66 | + client *rpc.Client |
| 67 | + endpoint string |
| 68 | + status string |
| 69 | + cacheMu sync.RWMutex |
| 70 | + cache []accounts.Account |
| 71 | +} |
| 72 | + |
| 73 | +func NewExternalSigner(endpoint string) (*ExternalSigner, error) { |
| 74 | + client, err := rpc.Dial(endpoint) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + extsigner := &ExternalSigner{ |
| 79 | + client: client, |
| 80 | + endpoint: endpoint, |
| 81 | + } |
| 82 | + // Check if reachable |
| 83 | + version, err := extsigner.pingVersion() |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + extsigner.status = fmt.Sprintf("ok [version=%v]", version) |
| 88 | + return extsigner, nil |
| 89 | +} |
| 90 | + |
| 91 | +func (api *ExternalSigner) URL() accounts.URL { |
| 92 | + return accounts.URL{ |
| 93 | + Scheme: "extapi", |
| 94 | + Path: api.endpoint, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (api *ExternalSigner) Status() (string, error) { |
| 99 | + return api.status, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (api *ExternalSigner) Open(passphrase string) error { |
| 103 | + return fmt.Errorf("operation not supported on external signers") |
| 104 | +} |
| 105 | + |
| 106 | +func (api *ExternalSigner) Close() error { |
| 107 | + return fmt.Errorf("operation not supported on external signers") |
| 108 | +} |
| 109 | + |
| 110 | +func (api *ExternalSigner) Accounts() []accounts.Account { |
| 111 | + var accnts []accounts.Account |
| 112 | + res, err := api.listAccounts() |
| 113 | + if err != nil { |
| 114 | + log.Error("account listing failed", "error", err) |
| 115 | + return accnts |
| 116 | + } |
| 117 | + for _, addr := range res { |
| 118 | + accnts = append(accnts, accounts.Account{ |
| 119 | + URL: accounts.URL{ |
| 120 | + Scheme: "extapi", |
| 121 | + Path: api.endpoint, |
| 122 | + }, |
| 123 | + Address: addr, |
| 124 | + }) |
| 125 | + } |
| 126 | + api.cacheMu.Lock() |
| 127 | + api.cache = accnts |
| 128 | + api.cacheMu.Unlock() |
| 129 | + return accnts |
| 130 | +} |
| 131 | + |
| 132 | +func (api *ExternalSigner) Contains(account accounts.Account) bool { |
| 133 | + api.cacheMu.RLock() |
| 134 | + defer api.cacheMu.RUnlock() |
| 135 | + for _, a := range api.cache { |
| 136 | + if a.Address == account.Address && (account.URL == (accounts.URL{}) || account.URL == api.URL()) { |
| 137 | + return true |
| 138 | + } |
| 139 | + } |
| 140 | + return false |
| 141 | +} |
| 142 | + |
| 143 | +func (api *ExternalSigner) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) { |
| 144 | + return accounts.Account{}, fmt.Errorf("operation not supported on external signers") |
| 145 | +} |
| 146 | + |
| 147 | +func (api *ExternalSigner) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) { |
| 148 | + log.Error("operation SelfDerive not supported on external signers") |
| 149 | +} |
| 150 | + |
| 151 | +func (api *ExternalSigner) signHash(account accounts.Account, hash []byte) ([]byte, error) { |
| 152 | + return []byte{}, fmt.Errorf("operation not supported on external signers") |
| 153 | +} |
| 154 | + |
| 155 | +// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed |
| 156 | +func (api *ExternalSigner) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) { |
| 157 | + // TODO! Replace this with a call to clef SignData with correct mime-type for Clique, once we |
| 158 | + // have that in place |
| 159 | + return api.signHash(account, crypto.Keccak256(data)) |
| 160 | +} |
| 161 | + |
| 162 | +func (api *ExternalSigner) SignText(account accounts.Account, text []byte) ([]byte, error) { |
| 163 | + return api.signHash(account, accounts.TextHash(text)) |
| 164 | +} |
| 165 | + |
| 166 | +func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { |
| 167 | + res := ethapi.SignTransactionResult{} |
| 168 | + to := common.NewMixedcaseAddress(*tx.To()) |
| 169 | + data := hexutil.Bytes(tx.Data()) |
| 170 | + args := &core.SendTxArgs{ |
| 171 | + Data: &data, |
| 172 | + Nonce: hexutil.Uint64(tx.Nonce()), |
| 173 | + Value: hexutil.Big(*tx.Value()), |
| 174 | + Gas: hexutil.Uint64(tx.Gas()), |
| 175 | + GasPrice: hexutil.Big(*tx.GasPrice()), |
| 176 | + To: &to, |
| 177 | + From: common.NewMixedcaseAddress(account.Address), |
| 178 | + } |
| 179 | + |
| 180 | + if err := api.client.Call(&res, "account_signTransaction", args); err != nil { |
| 181 | + return nil, err |
| 182 | + } |
| 183 | + return res.Tx, nil |
| 184 | +} |
| 185 | + |
| 186 | +func (api *ExternalSigner) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) { |
| 187 | + return []byte{}, fmt.Errorf("operation not supported on external signers") |
| 188 | +} |
| 189 | + |
| 190 | +func (api *ExternalSigner) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { |
| 191 | + return nil, fmt.Errorf("operation not supported on external signers") |
| 192 | +} |
| 193 | + |
| 194 | +func (api *ExternalSigner) listAccounts() ([]common.Address, error) { |
| 195 | + var res []common.Address |
| 196 | + if err := api.client.Call(&res, "account_list"); err != nil { |
| 197 | + return nil, err |
| 198 | + } |
| 199 | + return res, nil |
| 200 | +} |
| 201 | + |
| 202 | +func (api *ExternalSigner) signCliqueBlock(a common.Address, rlpBlock hexutil.Bytes) (hexutil.Bytes, error) { |
| 203 | + var sig hexutil.Bytes |
| 204 | + if err := api.client.Call(&sig, "account_signData", "application/clique", a, rlpBlock); err != nil { |
| 205 | + return nil, err |
| 206 | + } |
| 207 | + if sig[64] != 27 && sig[64] != 28 { |
| 208 | + return nil, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)") |
| 209 | + } |
| 210 | + sig[64] -= 27 // Transform V from 27/28 to 0/1 for Clique use |
| 211 | + return sig, nil |
| 212 | +} |
| 213 | + |
| 214 | +func (api *ExternalSigner) pingVersion() (string, error) { |
| 215 | + var v string |
| 216 | + if err := api.client.Call(&v, "account_version"); err != nil { |
| 217 | + return "", err |
| 218 | + } |
| 219 | + return v, nil |
| 220 | +} |
0 commit comments