github.com/yihuang/erigon@v1.9.7/accounts/keystore/wallet.go (about)

     1  // Copyright 2017 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 keystore
    18  
    19  import (
    20  	"math/big"
    21  
    22  	ethereum "github.com/ethereum/go-ethereum"
    23  	"github.com/ethereum/go-ethereum/accounts"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/crypto"
    26  )
    27  
    28  // keystoreWallet implements the accounts.Wallet interface for the original
    29  // keystore.
    30  type keystoreWallet struct {
    31  	account  accounts.Account // Single account contained in this wallet
    32  	keystore *KeyStore        // Keystore where the account originates from
    33  }
    34  
    35  // URL implements accounts.Wallet, returning the URL of the account within.
    36  func (w *keystoreWallet) URL() accounts.URL {
    37  	return w.account.URL
    38  }
    39  
    40  // Status implements accounts.Wallet, returning whether the account held by the
    41  // keystore wallet is unlocked or not.
    42  func (w *keystoreWallet) Status() (string, error) {
    43  	w.keystore.mu.RLock()
    44  	defer w.keystore.mu.RUnlock()
    45  
    46  	if _, ok := w.keystore.unlocked[w.account.Address]; ok {
    47  		return "Unlocked", nil
    48  	}
    49  	return "Locked", nil
    50  }
    51  
    52  // Open implements accounts.Wallet, but is a noop for plain wallets since there
    53  // is no connection or decryption step necessary to access the list of accounts.
    54  func (w *keystoreWallet) Open(passphrase string) error { return nil }
    55  
    56  // Close implements accounts.Wallet, but is a noop for plain wallets since there
    57  // is no meaningful open operation.
    58  func (w *keystoreWallet) Close() error { return nil }
    59  
    60  // Accounts implements accounts.Wallet, returning an account list consisting of
    61  // a single account that the plain kestore wallet contains.
    62  func (w *keystoreWallet) Accounts() []accounts.Account {
    63  	return []accounts.Account{w.account}
    64  }
    65  
    66  // Contains implements accounts.Wallet, returning whether a particular account is
    67  // or is not wrapped by this wallet instance.
    68  func (w *keystoreWallet) Contains(account accounts.Account) bool {
    69  	return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL)
    70  }
    71  
    72  // Derive implements accounts.Wallet, but is a noop for plain wallets since there
    73  // is no notion of hierarchical account derivation for plain keystore accounts.
    74  func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
    75  	return accounts.Account{}, accounts.ErrNotSupported
    76  }
    77  
    78  // SelfDerive implements accounts.Wallet, but is a noop for plain wallets since
    79  // there is no notion of hierarchical account derivation for plain keystore accounts.
    80  func (w *keystoreWallet) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
    81  }
    82  
    83  // signHash attempts to sign the given hash with
    84  // the given account. If the wallet does not wrap this particular account, an
    85  // error is returned to avoid account leakage (even though in theory we may be
    86  // able to sign via our shared keystore backend).
    87  func (w *keystoreWallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
    88  	// Make sure the requested account is contained within
    89  	if !w.Contains(account) {
    90  		return nil, accounts.ErrUnknownAccount
    91  	}
    92  	// Account seems valid, request the keystore to sign
    93  	return w.keystore.SignHash(account, hash)
    94  }
    95  
    96  // SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
    97  func (w *keystoreWallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
    98  	return w.signHash(account, crypto.Keccak256(data))
    99  }
   100  
   101  // SignDataWithPassphrase signs keccak256(data). The mimetype parameter describes the type of data being signed
   102  func (w *keystoreWallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
   103  	// Make sure the requested account is contained within
   104  	if !w.Contains(account) {
   105  		return nil, accounts.ErrUnknownAccount
   106  	}
   107  	// Account seems valid, request the keystore to sign
   108  	return w.keystore.SignHashWithPassphrase(account, passphrase, crypto.Keccak256(data))
   109  }
   110  
   111  func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
   112  	return w.signHash(account, accounts.TextHash(text))
   113  }
   114  
   115  // SignTextWithPassphrase implements accounts.Wallet, attempting to sign the
   116  // given hash with the given account using passphrase as extra authentication.
   117  func (w *keystoreWallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
   118  	// Make sure the requested account is contained within
   119  	if !w.Contains(account) {
   120  		return nil, accounts.ErrUnknownAccount
   121  	}
   122  	// Account seems valid, request the keystore to sign
   123  	return w.keystore.SignHashWithPassphrase(account, passphrase, accounts.TextHash(text))
   124  }
   125  
   126  // SignTx implements accounts.Wallet, attempting to sign the given transaction
   127  // with the given account. If the wallet does not wrap this particular account,
   128  // an error is returned to avoid account leakage (even though in theory we may
   129  // be able to sign via our shared keystore backend).
   130  func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   131  	// Make sure the requested account is contained within
   132  	if !w.Contains(account) {
   133  		return nil, accounts.ErrUnknownAccount
   134  	}
   135  	// Account seems valid, request the keystore to sign
   136  	return w.keystore.SignTx(account, tx, chainID)
   137  }
   138  
   139  // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
   140  // transaction with the given account using passphrase as extra authentication.
   141  func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   142  	// Make sure the requested account is contained within
   143  	if !w.Contains(account) {
   144  		return nil, accounts.ErrUnknownAccount
   145  	}
   146  	// Account seems valid, request the keystore to sign
   147  	return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
   148  }