github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/accounts/keystore/keystore_wallet.go (about)

     1  package keystore
     2  
     3  import (
     4  	"math/big"
     5  
     6  	quickchain "github.com/quickchainproject/quickchain"
     7  	"github.com/quickchainproject/quickchain/accounts"
     8  	"github.com/quickchainproject/quickchain/core/types"
     9  )
    10  
    11  // keystoreWallet implements the accounts.Wallet interface for the original
    12  // keystore.
    13  type keystoreWallet struct {
    14  	account  accounts.Account // Single account contained in this wallet
    15  	keystore *KeyStore        // Keystore where the account originates from
    16  }
    17  
    18  // URL implements accounts.Wallet, returning the URL of the account within.
    19  func (w *keystoreWallet) URL() accounts.URL {
    20  	return w.account.URL
    21  }
    22  
    23  // Status implements accounts.Wallet, returning whether the account held by the
    24  // keystore wallet is unlocked or not.
    25  func (w *keystoreWallet) Status() (string, error) {
    26  	w.keystore.mu.RLock()
    27  	defer w.keystore.mu.RUnlock()
    28  
    29  	if _, ok := w.keystore.unlocked[w.account.Address]; ok {
    30  		return "Unlocked", nil
    31  	}
    32  	return "Locked", nil
    33  }
    34  
    35  // Open implements accounts.Wallet, but is a noop for plain wallets since there
    36  // is no connection or decryption step necessary to access the list of accounts.
    37  func (w *keystoreWallet) Open(passphrase string) error { return nil }
    38  
    39  // Close implements accounts.Wallet, but is a noop for plain wallets since is no
    40  // meaningful open operation.
    41  func (w *keystoreWallet) Close() error { return nil }
    42  
    43  // Accounts implements accounts.Wallet, returning an account list consisting of
    44  // a single account that the plain kestore wallet contains.
    45  func (w *keystoreWallet) Accounts() []accounts.Account {
    46  	return []accounts.Account{w.account}
    47  }
    48  
    49  // Contains implements accounts.Wallet, returning whether a particular account is
    50  // or is not wrapped by this wallet instance.
    51  func (w *keystoreWallet) Contains(account accounts.Account) bool {
    52  	return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL)
    53  }
    54  
    55  // Derive implements accounts.Wallet, but is a noop for plain wallets since there
    56  // is no notion of hierarchical account derivation for plain keystore accounts.
    57  func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
    58  	return accounts.Account{}, accounts.ErrNotSupported
    59  }
    60  
    61  // SelfDerive implements accounts.Wallet, but is a noop for plain wallets since
    62  // there is no notion of hierarchical account derivation for plain keystore accounts.
    63  func (w *keystoreWallet) SelfDerive(base accounts.DerivationPath, chain quickchain.ChainStateReader) {}
    64  
    65  // SignHash implements accounts.Wallet, attempting to sign the given hash with
    66  // the given account. If the wallet does not wrap this particular account, an
    67  // error is returned to avoid account leakage (even though in theory we may be
    68  // able to sign via our shared keystore backend).
    69  func (w *keystoreWallet) SignHash(account accounts.Account, hash []byte) ([]byte, error) {
    70  	// Make sure the requested account is contained within
    71  	if account.Address != w.account.Address {
    72  		return nil, accounts.ErrUnknownAccount
    73  	}
    74  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
    75  		return nil, accounts.ErrUnknownAccount
    76  	}
    77  	// Account seems valid, request the keystore to sign
    78  	return w.keystore.SignHash(account, hash)
    79  }
    80  
    81  // SignTx implements accounts.Wallet, attempting to sign the given transaction
    82  // with the given account. If the wallet does not wrap this particular account,
    83  // an error is returned to avoid account leakage (even though in theory we may
    84  // be able to sign via our shared keystore backend).
    85  func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
    86  	// Make sure the requested account is contained within
    87  	if account.Address != w.account.Address {
    88  		return nil, accounts.ErrUnknownAccount
    89  	}
    90  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
    91  		return nil, accounts.ErrUnknownAccount
    92  	}
    93  	// Account seems valid, request the keystore to sign
    94  	return w.keystore.SignTx(account, tx, chainID)
    95  }
    96  
    97  // SignHashWithPassphrase implements accounts.Wallet, attempting to sign the
    98  // given hash with the given account using passphrase as extra authentication.
    99  func (w *keystoreWallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) {
   100  	// Make sure the requested account is contained within
   101  	if account.Address != w.account.Address {
   102  		return nil, accounts.ErrUnknownAccount
   103  	}
   104  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   105  		return nil, accounts.ErrUnknownAccount
   106  	}
   107  	// Account seems valid, request the keystore to sign
   108  	return w.keystore.SignHashWithPassphrase(account, passphrase, hash)
   109  }
   110  
   111  // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
   112  // transaction with the given account using passphrase as extra authentication.
   113  func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   114  	// Make sure the requested account is contained within
   115  	if account.Address != w.account.Address {
   116  		return nil, accounts.ErrUnknownAccount
   117  	}
   118  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   119  		return nil, accounts.ErrUnknownAccount
   120  	}
   121  	// Account seems valid, request the keystore to sign
   122  	return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
   123  }