github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/accounts/keystore/keystore_wallet.go (about)

     1  // Copyright 2018 Wanchain Foundation Ltd
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package keystore
    19  
    20  import (
    21  	"math/big"
    22  
    23  	ethereum "github.com/wanchain/go-wanchain"
    24  	"github.com/wanchain/go-wanchain/accounts"
    25  	"github.com/wanchain/go-wanchain/common"
    26  	"github.com/wanchain/go-wanchain/core/types"
    27  )
    28  
    29  // keystoreWallet implements the accounts.Wallet interface for the original
    30  // keystore.
    31  type keystoreWallet struct {
    32  	account  accounts.Account // Single account contained in this wallet
    33  	keystore *KeyStore        // Keystore where the account originates from
    34  }
    35  
    36  // URL implements accounts.Wallet, returning the URL of the account within.
    37  func (w *keystoreWallet) URL() accounts.URL {
    38  	return w.account.URL
    39  }
    40  
    41  // Status implements accounts.Wallet, returning whether the account held by the
    42  // keystore wallet is unlocked or not.
    43  func (w *keystoreWallet) Status() (string, error) {
    44  	w.keystore.mu.RLock()
    45  	defer w.keystore.mu.RUnlock()
    46  
    47  	if _, ok := w.keystore.unlocked[w.account.Address]; ok {
    48  		return "Unlocked", nil
    49  	}
    50  	return "Locked", nil
    51  }
    52  
    53  // Open implements accounts.Wallet, but is a noop for plain wallets since there
    54  // is no connection or decryption step necessary to access the list of accounts.
    55  func (w *keystoreWallet) Open(passphrase string) error { return nil }
    56  
    57  // Close implements accounts.Wallet, but is a noop for plain wallets since is no
    58  // meaningful open operation.
    59  func (w *keystoreWallet) Close() error { return nil }
    60  
    61  // Accounts implements accounts.Wallet, returning an account list consisting of
    62  // a single account that the plain kestore wallet contains.
    63  func (w *keystoreWallet) Accounts() []accounts.Account {
    64  	return []accounts.Account{w.account}
    65  }
    66  
    67  // Contains implements accounts.Wallet, returning whether a particular account is
    68  // or is not wrapped by this wallet instance.
    69  func (w *keystoreWallet) Contains(account accounts.Account) bool {
    70  	return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL)
    71  }
    72  
    73  // Derive implements accounts.Wallet, but is a noop for plain wallets since there
    74  // is no notion of hierarchical account derivation for plain keystore accounts.
    75  func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
    76  	return accounts.Account{}, accounts.ErrNotSupported
    77  }
    78  
    79  // SelfDerive implements accounts.Wallet, but is a noop for plain wallets since
    80  // there is no notion of hierarchical account derivation for plain keystore accounts.
    81  func (w *keystoreWallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) {}
    82  
    83  // SignHash implements accounts.Wallet, attempting 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 account.Address != w.account.Address {
    90  		return nil, accounts.ErrUnknownAccount
    91  	}
    92  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
    93  		return nil, accounts.ErrUnknownAccount
    94  	}
    95  	// Account seems valid, request the keystore to sign
    96  	return w.keystore.SignHash(account, hash)
    97  }
    98  
    99  // SignTx implements accounts.Wallet, attempting to sign the given transaction
   100  // with the given account. If the wallet does not wrap this particular account,
   101  // an error is returned to avoid account leakage (even though in theory we may
   102  // be able to sign via our shared keystore backend).
   103  func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   104  	// Make sure the requested account is contained within
   105  	if account.Address != w.account.Address {
   106  		return nil, accounts.ErrUnknownAccount
   107  	}
   108  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   109  		return nil, accounts.ErrUnknownAccount
   110  	}
   111  	// Account seems valid, request the keystore to sign
   112  	return w.keystore.SignTx(account, tx, chainID)
   113  }
   114  
   115  func (w *keystoreWallet) ComputeOTAPPKeys(account accounts.Account, AX, AY, BX, BY string) ([]string, 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  
   124  	// Account seems valid, request the keystore to process
   125  	return w.keystore.ComputeOTAPPKeys(account, AX, AY, BX, BY)
   126  }
   127  
   128  // SignHashWithPassphrase implements accounts.Wallet, attempting to sign the
   129  // given hash with the given account using passphrase as extra authentication.
   130  func (w *keystoreWallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) {
   131  	// Make sure the requested account is contained within
   132  	if account.Address != w.account.Address {
   133  		return nil, accounts.ErrUnknownAccount
   134  	}
   135  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   136  		return nil, accounts.ErrUnknownAccount
   137  	}
   138  	// Account seems valid, request the keystore to sign
   139  	return w.keystore.SignHashWithPassphrase(account, passphrase, hash)
   140  }
   141  
   142  // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
   143  // transaction with the given account using passphrase as extra authentication.
   144  func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   145  	// Make sure the requested account is contained within
   146  	if account.Address != w.account.Address {
   147  		return nil, accounts.ErrUnknownAccount
   148  	}
   149  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   150  		return nil, accounts.ErrUnknownAccount
   151  	}
   152  	// Account seems valid, request the keystore to sign
   153  	return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
   154  }
   155  
   156  // GetWanAddress represents the wallet to retrieve corresponding wanchain public address for a specific ordinary account/address
   157  func (w *keystoreWallet) GetWanAddress(account accounts.Account) (common.WAddress, error) {
   158  	// Make sure the requested account is contained within
   159  	if account.Address != w.account.Address {
   160  		return common.WAddress{}, accounts.ErrUnknownAccount
   161  	}
   162  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   163  		return common.WAddress{}, accounts.ErrUnknownAccount
   164  	}
   165  	// Account seems valid, request the keystore to retrieve
   166  	return w.keystore.GetWanAddress(account)
   167  }