accounts.go 6.8 KB

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