accounts.go 8.3 KB

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