gitlab.com/lightnet1/evrynet-node@v1.1.0/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 "gitlab.com/lightnet1/evrynet-node/accounts" 27 "gitlab.com/lightnet1/evrynet-node/accounts/keystore" 28 "gitlab.com/lightnet1/evrynet-node/common" 29 "gitlab.com/lightnet1/evrynet-node/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 // ProviderSignTxPassphrase signs the transaction if the private key matching the 151 // given address can be decrypted with the given passphrase. 152 func (ks *KeyStore) ProviderSignTxPassphrase(account *Account, passphrase string, tx *Transaction, chainID *BigInt) (*Transaction, error) { 153 if chainID == nil { // Null passed from mobile app 154 chainID = new(BigInt) 155 } 156 signed, err := ks.keystore.ProviderSignTxWithPassphrase(account.account, passphrase, tx.tx, chainID.bigint) 157 if err != nil { 158 return nil, err 159 } 160 return &Transaction{signed}, nil 161 } 162 163 // Unlock unlocks the given account indefinitely. 164 func (ks *KeyStore) Unlock(account *Account, passphrase string) error { 165 return ks.keystore.TimedUnlock(account.account, passphrase, 0) 166 } 167 168 // Lock removes the private key with the given address from memory. 169 func (ks *KeyStore) Lock(address *Address) error { 170 return ks.keystore.Lock(address.address) 171 } 172 173 // TimedUnlock unlocks the given account with the passphrase. The account stays 174 // unlocked for the duration of timeout (nanoseconds). A timeout of 0 unlocks the 175 // account until the program exits. The account must match a unique key file. 176 // 177 // If the account address is already unlocked for a duration, TimedUnlock extends or 178 // shortens the active unlock timeout. If the address was previously unlocked 179 // indefinitely the timeout is not altered. 180 func (ks *KeyStore) TimedUnlock(account *Account, passphrase string, timeout int64) error { 181 return ks.keystore.TimedUnlock(account.account, passphrase, time.Duration(timeout)) 182 } 183 184 // NewAccount generates a new key and stores it into the key directory, 185 // encrypting it with the passphrase. 186 func (ks *KeyStore) NewAccount(passphrase string) (*Account, error) { 187 account, err := ks.keystore.NewAccount(passphrase) 188 if err != nil { 189 return nil, err 190 } 191 return &Account{account}, nil 192 } 193 194 // UpdateAccount changes the passphrase of an existing account. 195 func (ks *KeyStore) UpdateAccount(account *Account, passphrase, newPassphrase string) error { 196 return ks.keystore.Update(account.account, passphrase, newPassphrase) 197 } 198 199 // ExportKey exports as a JSON key, encrypted with newPassphrase. 200 func (ks *KeyStore) ExportKey(account *Account, passphrase, newPassphrase string) (key []byte, _ error) { 201 return ks.keystore.Export(account.account, passphrase, newPassphrase) 202 } 203 204 // ImportKey stores the given encrypted JSON key into the key directory. 205 func (ks *KeyStore) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (account *Account, _ error) { 206 acc, err := ks.keystore.Import(common.CopyBytes(keyJSON), passphrase, newPassphrase) 207 if err != nil { 208 return nil, err 209 } 210 return &Account{acc}, nil 211 } 212 213 // ImportECDSAKey stores the given encrypted JSON key into the key directory. 214 func (ks *KeyStore) ImportECDSAKey(key []byte, passphrase string) (account *Account, _ error) { 215 privkey, err := crypto.ToECDSA(common.CopyBytes(key)) 216 if err != nil { 217 return nil, err 218 } 219 acc, err := ks.keystore.ImportECDSA(privkey, passphrase) 220 if err != nil { 221 return nil, err 222 } 223 return &Account{acc}, nil 224 } 225 226 // ImportPreSaleKey decrypts the given Evrynet presale wallet and stores 227 // a key file in the key directory. The key file is encrypted with the same passphrase. 228 func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (ccount *Account, _ error) { 229 account, err := ks.keystore.ImportPreSaleKey(common.CopyBytes(keyJSON), passphrase) 230 if err != nil { 231 return nil, err 232 } 233 return &Account{account}, nil 234 }