github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/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 ) 28 29 const ( 30 // StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB 31 // memory and taking approximately 1s CPU time on a modern processor. 32 StandardScryptN = int(accounts.StandardScryptN) 33 34 // StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB 35 // memory and taking approximately 1s CPU time on a modern processor. 36 StandardScryptP = int(accounts.StandardScryptP) 37 38 // LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB 39 // memory and taking approximately 100ms CPU time on a modern processor. 40 LightScryptN = int(accounts.LightScryptN) 41 42 // LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB 43 // memory and taking approximately 100ms CPU time on a modern processor. 44 LightScryptP = int(accounts.LightScryptP) 45 ) 46 47 // Account represents a stored key. 48 type Account struct{ account accounts.Account } 49 50 // Accounts represents a slice of accounts. 51 type Accounts struct{ accounts []accounts.Account } 52 53 // Size returns the number of accounts in the slice. 54 func (a *Accounts) Size() int { 55 return len(a.accounts) 56 } 57 58 // Get returns the account at the given index from the slice. 59 func (a *Accounts) Get(index int) (account *Account, _ error) { 60 if index < 0 || index >= len(a.accounts) { 61 return nil, errors.New("index out of bounds") 62 } 63 return &Account{a.accounts[index]}, nil 64 } 65 66 // Set sets the account at the given index in the slice. 67 func (a *Accounts) Set(index int, account *Account) error { 68 if index < 0 || index >= len(a.accounts) { 69 return errors.New("index out of bounds") 70 } 71 a.accounts[index] = account.account 72 return nil 73 } 74 75 // GetAddress retrieves the address associated with the account. 76 func (a *Account) GetAddress() *Address { 77 return &Address{a.account.Address} 78 } 79 80 // GetFile retrieves the path of the file containing the account key. 81 func (a *Account) GetFile() string { 82 return a.account.File 83 } 84 85 // AccountManager manages a key storage directory on disk. 86 type AccountManager struct{ manager *accounts.Manager } 87 88 // NewAccountManager creates a manager for the given directory. 89 func NewAccountManager(keydir string, scryptN, scryptP int) *AccountManager { 90 return &AccountManager{manager: accounts.NewManager(keydir, scryptN, scryptP)} 91 } 92 93 // HasAddress reports whether a key with the given address is present. 94 func (am *AccountManager) HasAddress(address *Address) bool { 95 return am.manager.HasAddress(address.address) 96 } 97 98 // GetAccounts returns all key files present in the directory. 99 func (am *AccountManager) GetAccounts() *Accounts { 100 return &Accounts{am.manager.Accounts()} 101 } 102 103 // DeleteAccount deletes the key matched by account if the passphrase is correct. 104 // If a contains no filename, the address must match a unique key. 105 func (am *AccountManager) DeleteAccount(account *Account, passphrase string) error { 106 return am.manager.Delete(accounts.Account{ 107 Address: account.account.Address, 108 File: account.account.File, 109 }, passphrase) 110 } 111 112 // Sign 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 (am *AccountManager) Sign(address *Address, hash []byte) (signature []byte, _ error) { 115 return am.manager.Sign(address.address, hash) 116 } 117 118 // SignPassphrase signs hash if the private key matching the given address can 119 // be decrypted with the given passphrase. The produced signature is in the 120 // [R || S || V] format where V is 0 or 1. 121 func (am *AccountManager) SignPassphrase(account *Account, passphrase string, hash []byte) (signature []byte, _ error) { 122 return am.manager.SignWithPassphrase(account.account, passphrase, hash) 123 } 124 125 // Unlock unlocks the given account indefinitely. 126 func (am *AccountManager) Unlock(account *Account, passphrase string) error { 127 return am.manager.TimedUnlock(account.account, passphrase, 0) 128 } 129 130 // Lock removes the private key with the given address from memory. 131 func (am *AccountManager) Lock(address *Address) error { 132 return am.manager.Lock(address.address) 133 } 134 135 // TimedUnlock unlocks the given account with the passphrase. The account stays 136 // unlocked for the duration of timeout (nanoseconds). A timeout of 0 unlocks the 137 // account until the program exits. The account must match a unique key file. 138 // 139 // If the account address is already unlocked for a duration, TimedUnlock extends or 140 // shortens the active unlock timeout. If the address was previously unlocked 141 // indefinitely the timeout is not altered. 142 func (am *AccountManager) TimedUnlock(account *Account, passphrase string, timeout int64) error { 143 return am.manager.TimedUnlock(account.account, passphrase, time.Duration(timeout)) 144 } 145 146 // NewAccount generates a new key and stores it into the key directory, 147 // encrypting it with the passphrase. 148 func (am *AccountManager) NewAccount(passphrase string) (*Account, error) { 149 account, err := am.manager.NewAccount(passphrase) 150 if err != nil { 151 return nil, err 152 } 153 return &Account{account}, nil 154 } 155 156 // ExportKey exports as a JSON key, encrypted with newPassphrase. 157 func (am *AccountManager) ExportKey(account *Account, passphrase, newPassphrase string) (key []byte, _ error) { 158 return am.manager.Export(account.account, passphrase, newPassphrase) 159 } 160 161 // ImportKey stores the given encrypted JSON key into the key directory. 162 func (am *AccountManager) ImportKey(keyJSON []byte, passphrase, newPassphrase string) (account *Account, _ error) { 163 acc, err := am.manager.Import(keyJSON, passphrase, newPassphrase) 164 if err != nil { 165 return nil, err 166 } 167 return &Account{acc}, nil 168 } 169 170 // UpdateAccount changes the passphrase of an existing account. 171 func (am *AccountManager) UpdateAccount(account *Account, passphrase, newPassphrase string) error { 172 return am.manager.Update(account.account, passphrase, newPassphrase) 173 } 174 175 // ImportPreSaleKey decrypts the given Ethereum presale wallet and stores 176 // a key file in the key directory. The key file is encrypted with the same passphrase. 177 func (am *AccountManager) ImportPreSaleKey(keyJSON []byte, passphrase string) (ccount *Account, _ error) { 178 account, err := am.manager.ImportPreSaleKey(keyJSON, passphrase) 179 if err != nil { 180 return nil, err 181 } 182 return &Account{account}, nil 183 }