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