github.com/MikyChow/arbitrum-go-ethereum@v0.0.0-20230306102812-078da49636de/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  	"github.com/MikyChow/arbitrum-go-ethereum/accounts"
    23  	"github.com/MikyChow/arbitrum-go-ethereum/core/types"
    24  	"github.com/MikyChow/arbitrum-go-ethereum/crypto"
    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 keystore 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(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
    80  }
    81  
    82  // signHash attempts to sign the given hash with
    83  // the given account. If the wallet does not wrap this particular account, an
    84  // error is returned to avoid account leakage (even though in theory we may be
    85  // able to sign via our shared keystore backend).
    86  func (w *keystoreWallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
    87  	// Make sure the requested account is contained within
    88  	if !w.Contains(account) {
    89  		return nil, accounts.ErrUnknownAccount
    90  	}
    91  	// Account seems valid, request the keystore to sign
    92  	return w.keystore.SignHash(account, hash)
    93  }
    94  
    95  // SignData signs keccak256(data). The mimetype parameter describes the type of data being signed.
    96  func (w *keystoreWallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
    97  	return w.signHash(account, crypto.Keccak256(data))
    98  }
    99  
   100  // SignDataWithPassphrase signs keccak256(data). The mimetype parameter describes the type of data being signed.
   101  func (w *keystoreWallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
   102  	// Make sure the requested account is contained within
   103  	if !w.Contains(account) {
   104  		return nil, accounts.ErrUnknownAccount
   105  	}
   106  	// Account seems valid, request the keystore to sign
   107  	return w.keystore.SignHashWithPassphrase(account, passphrase, crypto.Keccak256(data))
   108  }
   109  
   110  // SignText implements accounts.Wallet, attempting to sign the hash of
   111  // the given text with the given account.
   112  func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
   113  	return w.signHash(account, accounts.TextHash(text))
   114  }
   115  
   116  // SignTextWithPassphrase implements accounts.Wallet, attempting to sign the
   117  // hash of the given text with the given account using passphrase as extra authentication.
   118  func (w *keystoreWallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
   119  	// Make sure the requested account is contained within
   120  	if !w.Contains(account) {
   121  		return nil, accounts.ErrUnknownAccount
   122  	}
   123  	// Account seems valid, request the keystore to sign
   124  	return w.keystore.SignHashWithPassphrase(account, passphrase, accounts.TextHash(text))
   125  }
   126  
   127  // SignTx implements accounts.Wallet, attempting to sign the given transaction
   128  // with the given account. If the wallet does not wrap this particular account,
   129  // an error is returned to avoid account leakage (even though in theory we may
   130  // be able to sign via our shared keystore backend).
   131  func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   132  	// Make sure the requested account is contained within
   133  	if !w.Contains(account) {
   134  		return nil, accounts.ErrUnknownAccount
   135  	}
   136  	// Account seems valid, request the keystore to sign
   137  	return w.keystore.SignTx(account, tx, chainID)
   138  }
   139  
   140  // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
   141  // transaction with the given account using passphrase as extra authentication.
   142  func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
   143  	// Make sure the requested account is contained within
   144  	if !w.Contains(account) {
   145  		return nil, accounts.ErrUnknownAccount
   146  	}
   147  	// Account seems valid, request the keystore to sign
   148  	return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
   149  }