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