github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/accounts/keystore/keystore_wallet.go (about)

     1  package keystore
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/neatlab/neatio"
     7  	"github.com/neatlab/neatio/chain/accounts"
     8  	"github.com/neatlab/neatio/chain/core/types"
     9  )
    10  
    11  type keystoreWallet struct {
    12  	account  accounts.Account
    13  	keystore *KeyStore
    14  }
    15  
    16  func (w *keystoreWallet) URL() accounts.URL {
    17  	return w.account.URL
    18  }
    19  
    20  func (w *keystoreWallet) Status() (string, error) {
    21  	w.keystore.mu.RLock()
    22  	defer w.keystore.mu.RUnlock()
    23  
    24  	if _, ok := w.keystore.unlocked[w.account.Address]; ok {
    25  		return "Unlocked", nil
    26  	}
    27  	return "Locked", nil
    28  }
    29  
    30  func (w *keystoreWallet) Open(passphrase string) error { return nil }
    31  
    32  func (w *keystoreWallet) Close() error { return nil }
    33  
    34  func (w *keystoreWallet) Accounts() []accounts.Account {
    35  	return []accounts.Account{w.account}
    36  }
    37  
    38  func (w *keystoreWallet) Contains(account accounts.Account) bool {
    39  	return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL)
    40  }
    41  
    42  func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
    43  	return accounts.Account{}, accounts.ErrNotSupported
    44  }
    45  
    46  func (w *keystoreWallet) SelfDerive(base accounts.DerivationPath, chain neatio.ChainStateReader) {}
    47  
    48  func (w *keystoreWallet) SignHash(account accounts.Account, hash []byte) ([]byte, error) {
    49  
    50  	if account.Address != w.account.Address {
    51  		return nil, accounts.ErrUnknownAccount
    52  	}
    53  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
    54  		return nil, accounts.ErrUnknownAccount
    55  	}
    56  
    57  	return w.keystore.SignHash(account, hash)
    58  }
    59  
    60  func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
    61  
    62  	if account.Address != w.account.Address {
    63  		return nil, accounts.ErrUnknownAccount
    64  	}
    65  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
    66  		return nil, accounts.ErrUnknownAccount
    67  	}
    68  
    69  	return w.keystore.SignTx(account, tx, chainID)
    70  }
    71  
    72  func (w *keystoreWallet) SignTxWithAddress(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
    73  
    74  	if account.Address != w.account.Address {
    75  		return nil, accounts.ErrUnknownAccount
    76  	}
    77  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
    78  		return nil, accounts.ErrUnknownAccount
    79  	}
    80  
    81  	return w.keystore.SignTxWithAddress(account, tx, chainID)
    82  }
    83  
    84  func (w *keystoreWallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) {
    85  
    86  	if account.Address != w.account.Address {
    87  		return nil, accounts.ErrUnknownAccount
    88  	}
    89  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
    90  		return nil, accounts.ErrUnknownAccount
    91  	}
    92  
    93  	return w.keystore.SignHashWithPassphrase(account, passphrase, hash)
    94  }
    95  
    96  func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
    97  
    98  	if account.Address != w.account.Address {
    99  		return nil, accounts.ErrUnknownAccount
   100  	}
   101  	if account.URL != (accounts.URL{}) && account.URL != w.account.URL {
   102  		return nil, accounts.ErrUnknownAccount
   103  	}
   104  
   105  	return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
   106  }