github.com/abschain-develop/go-abs@v2.0.3+incompatible/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/abschain-develop/go-abs"
    23  	"github.com/abschain-develop/go-abs/accounts"
    24  	"github.com/abschain-develop/go-abs/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 there
    56  // is no 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 !w.Contains(account) {
    88  		return nil, accounts.ErrUnknownAccount
    89  	}
    90  	// Account seems valid, request the keystore to sign
    91  	return w.keystore.SignHash(account, hash)
    92  }
    93  
    94  // SignTx implements accounts.Wallet, attempting to sign the given transaction
    95  // with the given account. If the wallet does not wrap this particular account,
    96  // an error is returned to avoid account leakage (even though in theory we may
    97  // be able to sign via our shared keystore backend).
    98  func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
    99  	// Make sure the requested account is contained within
   100  	if !w.Contains(account) {
   101  		return nil, accounts.ErrUnknownAccount
   102  	}
   103  	// Account seems valid, request the keystore to sign
   104  	return w.keystore.SignTx(account, tx, chainID)
   105  }
   106  
   107  // SignHashWithPassphrase implements accounts.Wallet, attempting to sign the
   108  // given hash with the given account using passphrase as extra authentication.
   109  func (w *keystoreWallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) {
   110  	// Make sure the requested account is contained within
   111  	if !w.Contains(account) {
   112  		return nil, accounts.ErrUnknownAccount
   113  	}
   114  	// Account seems valid, request the keystore to sign
   115  	return w.keystore.SignHashWithPassphrase(account, passphrase, hash)
   116  }
   117  
   118  // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
   119  // transaction with the given account using passphrase as extra authentication.
   120  func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   121  	// Make sure the requested account is contained within
   122  	if !w.Contains(account) {
   123  		return nil, accounts.ErrUnknownAccount
   124  	}
   125  	// Account seems valid, request the keystore to sign
   126  	return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
   127  }