accounts.go 7.7 KB

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