github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/keystore/wallet.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar 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-aigar 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-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package keystore 19 20 import ( 21 "math/big" 22 23 ethereum "github.com/AigarNetwork/aigar" 24 "github.com/AigarNetwork/aigar/accounts" 25 "github.com/AigarNetwork/aigar/core/types" 26 "github.com/AigarNetwork/aigar/crypto" 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 there 58 // is no 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(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) { 82 } 83 84 // signHash attempts to sign the given hash with 85 // the given account. If the wallet does not wrap this particular account, an 86 // error is returned to avoid account leakage (even though in theory we may be 87 // able to sign via our shared keystore backend). 88 func (w *keystoreWallet) signHash(account accounts.Account, hash []byte) ([]byte, error) { 89 // Make sure the requested account is contained within 90 if !w.Contains(account) { 91 return nil, accounts.ErrUnknownAccount 92 } 93 // Account seems valid, request the keystore to sign 94 return w.keystore.SignHash(account, hash) 95 } 96 97 // SignData signs keccak256(data). The mimetype parameter describes the type of data being signed 98 func (w *keystoreWallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) { 99 return w.signHash(account, crypto.Keccak256(data)) 100 } 101 102 // SignDataWithPassphrase signs keccak256(data). The mimetype parameter describes the type of data being signed 103 func (w *keystoreWallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) { 104 // Make sure the requested account is contained within 105 if !w.Contains(account) { 106 return nil, accounts.ErrUnknownAccount 107 } 108 // Account seems valid, request the keystore to sign 109 return w.keystore.SignHashWithPassphrase(account, passphrase, crypto.Keccak256(data)) 110 } 111 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 // given hash 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 }