gitee.com/liu-zhao234568/cntest@v1.0.0/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 "gitee.com/liu-zhao234568/cntest"
    23  	"gitee.com/liu-zhao234568/cntest/accounts"
    24  	"gitee.com/liu-zhao234568/cntest/core/types"
    25  	"gitee.com/liu-zhao234568/cntest/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 keystore 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  // SignText implements accounts.Wallet, attempting to sign the hash of
   112  // the given text with the given account.
   113  func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
   114  	return w.signHash(account, accounts.TextHash(text))
   115  }
   116  
   117  // SignTextWithPassphrase implements accounts.Wallet, attempting to sign the
   118  // hash of the given text with the given account using passphrase as extra authentication.
   119  func (w *keystoreWallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
   120  	// Make sure the requested account is contained within
   121  	if !w.Contains(account) {
   122  		return nil, accounts.ErrUnknownAccount
   123  	}
   124  	// Account seems valid, request the keystore to sign
   125  	return w.keystore.SignHashWithPassphrase(account, passphrase, accounts.TextHash(text))
   126  }
   127  
   128  // SignTx implements accounts.Wallet, attempting to sign the given transaction
   129  // with the given account. If the wallet does not wrap this particular account,
   130  // an error is returned to avoid account leakage (even though in theory we may
   131  // be able to sign via our shared keystore backend).
   132  func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   133  	// Make sure the requested account is contained within
   134  	if !w.Contains(account) {
   135  		return nil, accounts.ErrUnknownAccount
   136  	}
   137  	// Account seems valid, request the keystore to sign
   138  	return w.keystore.SignTx(account, tx, chainID)
   139  }
   140  
   141  // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
   142  // transaction with the given account using passphrase as extra authentication.
   143  func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   144  	// Make sure the requested account is contained within
   145  	if !w.Contains(account) {
   146  		return nil, accounts.ErrUnknownAccount
   147  	}
   148  	// Account seems valid, request the keystore to sign
   149  	return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
   150  }