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