github.com/luckypickle/go-ethereum-vet@v1.14.2/mobile/accounts.go (about) 1 // Copyright 2016 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 // Contains all the wrappers from the accounts package to support client side key 18 // management on mobile platforms. 19 20 package geth 21 22 import ( 23 "errors" 24 "time" 25 26 "github.com/luckypickle/go-ethereum-vet/accounts" 27 "github.com/luckypickle/go-ethereum-vet/accounts/keystore" 28 "github.com/luckypickle/go-ethereum-vet/common" 29 "github.com/luckypickle/go-ethereum-vet/crypto" 30 ) 31 32 const ( 33 // StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB 34 // memory and taking approximately 1s CPU time on a modern processor. 35 StandardScryptN = int(keystore.StandardScryptN) 36 37 // StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB 38 // memory and taking approximately 1s CPU time on a modern processor. 39 StandardScryptP = int(keystore.StandardScryptP) 40 41 // LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB 42 // memory and taking approximately 100ms CPU time on a modern processor. 43 LightScryptN = int(keystore.LightScryptN) 44 45 // LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB 46 // memory and taking approximately 100ms CPU time on a modern processor. 47 LightScryptP = int(keystore.LightScryptP) 48 ) 49 50 // Account represents a stored key. 51 type Account struct{ account accounts.Account } 52 53 // Accounts represents a slice of accounts. 54 type Accounts struct{ accounts []accounts.Account } 55 56 // Size returns the number of accounts in the slice. 57 func (a *Accounts) Size() int { 58 return len(a.accounts) 59 } 60 61 // Get returns the account at the given index from the slice. 62 func (a *Accounts) Get(index int) (account *Account, _ error) { 63 if index < 0 || index >= len(a.accounts) { 64 return nil, errors.New("index out of bounds") 65 } 66 return &Account{a.accounts[index]}, nil 67 } 68 69 // Set sets the account at the given index in the slice. 70 func (a *Accounts) Set(index int, account *Account) error { 71 if index < 0 || index >= len(a.accounts) { 72 return errors.New("index out of bounds") 73 } 74 a.accounts[index] = account.account 75 return nil 76 } 77 78 // GetAddress retrieves the address associated with the account. 79 func (a *Account) GetAddress() *Address { 80 return &Address{a.account.Address} 81 } 82 83 // GetURL retrieves the canonical URL of the account. 84 func (a *Account) GetURL() string { 85 return a.account.URL.String() 86 } 87 88 // KeyStore manages a key storage directory on disk. 89 type KeyStore struct{ keystore *keystore.KeyStore } 90 91 // NewKeyStore creates a keystore for the given directory. 92 func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore { 93 return &KeyStore{keystore: keystore.NewKeyStore(keydir, scryptN, scryptP)} 94 } 95 96 // HasAddress reports whether a key with the given address is present. 97 func (ks *KeyStore) HasAddress(address *Address) bool { 98 return ks.keystore.HasAddress(address.address) 99 } 100 101 // GetAccounts returns all key files present in the directory. 102 func (ks *KeyStore) GetAccounts() *Accounts { 103 return &Accounts{ks.keystore.Accounts()} 104 } 105 106 // DeleteAccount deletes the key matched by account if the passphrase is correct. 107 // If a contains no filename, the address must match a unique key. 108 func (ks *KeyStore) DeleteAccount(account *Account, passphrase string) error { 109 return ks.keystore.Delete(account.account, passphrase) 110 } 111 112 // SignHash calculates a ECDSA signature for the given hash. The produced signature 113 // is in the [R || S || V] format where V is 0 or 1. 114 func (ks *KeyStore) SignHash(address *Address, hash []byte) (signature []byte, _ error) { 115 return ks.keystore.SignHash(accounts.Account{Address: address.address}, common.CopyBytes(hash)) 116 } 117 118 // SignTx signs the given transaction with the requested account. 119 func (ks *KeyStore) SignTx(account *Account, tx *Transaction, chainID *BigInt) (*Transaction, error) { 120 if chainID == nil { // Null passed from mobile app 121 chainID = new(BigInt) 122 } 123 signed, err := ks.keystore.SignTx(account.account, tx.tx, chainID.bigint) 124 if err != nil { 125 return nil, err 126 } 127 return &Transaction{signed}, nil 128 } 129 130 // SignHashPassphrase signs hash if the private key matching the given address can 131 // be decrypted with the given passphrase. The produced signature is in the 132 // [R || S || V] format where V is 0 or 1. 133 func (ks *KeyStore) SignHashPassphrase(account *Account, passphrase string, hash []byte) (signature []byte, _ error) { 134 return ks.keystore.SignHashWithPassphrase(account.account, passphrase, common.CopyBytes(hash)) 135 } 136 137 // SignTxPassphrase signs the transaction if the private key matching the 138 // given address can be decrypted with the given passphrase. 139 func (ks *KeyStore) SignTxPassphrase(account *Account, passphrase string, tx *Transaction, chainID *BigInt) (*Transaction, error) { 140 if chainID == nil { // Null passed from mobile app 141 chainID = new(BigInt) 142 } 143 signed, err := ks.keystore.SignTxWithPassphrase(account.account, passphrase, tx.tx, chainID.bigint) 144 if err != nil { 145 return nil, err 146 } 147 return &Transaction{signed}, nil 148 } 149 150 // Unlock unlocks the given account indefinitely. 151 func (ks *KeyStore) Unlock(account *Account, passphrase string) error { 152 return ks.keystore.TimedUnlock(account.account, passphrase, 0) 153 } 154 155 // Lock removes the private key with the given address from memory. 156 func (ks *KeyStore) Lock(address *Address) error { 157 return ks.keystore.Lock(address.address) 158 } 159 160 // TimedUnlock unlocks the given account with the passphrase. The account stays 161 // unlocked for the duration of timeout (nanoseconds). A timeout of 0 unlocks the 162 // account until the program exits. The account must match a unique key file. 163 // 164 // If the account address is already unlocked for a duration, TimedUnlock extends or 165 // shortens the active unlock timeout. If the address was previously unlocked 166 // indefinitely the timeout is not altered. 167 func (ks *KeyStore) TimedUnlock(account *Account, passphrase string, timeout int64) error { 168 return ks.keystore.TimedUnlock(account.account, passphrase, time.Duration(timeout)) 169 } 170 171 // NewAccount generates a new key and stores it into the key directory, 172 // encrypting it with the passphrase. 173 func (ks *KeyStore) NewAccount(passphrase string) (*Account, error) { 174 account, err := ks.keystore.NewAccount(passphrase) 175 if err != nil { 176 return nil, err 177 } 178 return &Account{account}, nil 179 } 180 181 // UpdateAccount changes the passphrase of an existing account. 182 func (ks *KeyStore) UpdateAccount(account *Account, passphrase, newPassphrase string) error { 183 return ks.keystore.Update(account.account, passphrase, newPassphrase) 184 } 185 186 // ExportKey exports as a JSON key, encrypted with newPassphrase. 187 func (ks *KeyStore) ExportKey(account *Account, passphrase, newPassphrase string) (key []byte, _ error) { 188 return ks.keystore.Export(account.account, passphrase, newPassphrase) 189 } 190 191 // ImportKey stores the given encrypted JSON key into the key directory. 192 func (ks *KeyStore) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (account *Account, _ error) { 193 acc, err := ks.keystore.Import(common.CopyBytes(keyJSON), passphrase, newPassphrase) 194 if err != nil { 195 return nil, err 196 } 197 return &Account{acc}, nil 198 } 199 200 // ImportECDSAKey stores the given encrypted JSON key into the key directory. 201 func (ks *KeyStore) ImportECDSAKey(key []byte, passphrase string) (account *Account, _ error) { 202 privkey, err := crypto.ToECDSA(common.CopyBytes(key)) 203 if err != nil { 204 return nil, err 205 } 206 acc, err := ks.keystore.ImportECDSA(privkey, passphrase) 207 if err != nil { 208 return nil, err 209 } 210 return &Account{acc}, nil 211 } 212 213 // ImportPreSaleKey decrypts the given Ethereum presale wallet and stores 214 // a key file in the key directory. The key file is encrypted with the same passphrase. 215 func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (ccount *Account, _ error) { 216 account, err := ks.keystore.ImportPreSaleKey(common.CopyBytes(keyJSON), passphrase) 217 if err != nil { 218 return nil, err 219 } 220 return &Account{account}, nil 221 }