account_manager.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @authors
  16. * Gustav Simonsson <gustav.simonsson@gmail.com>
  17. * @date 2015
  18. *
  19. */
  20. /*
  21. This abstracts part of a user's interaction with an account she controls.
  22. It's not an abstraction of core Ethereum accounts data type / logic -
  23. for that see the core processing code of blocks / txs.
  24. Currently this is pretty much a passthrough to the KeyStore2 interface,
  25. and accounts persistence is derived from stored keys' addresses
  26. */
  27. package accounts
  28. import (
  29. "crypto/ecdsa"
  30. crand "crypto/rand"
  31. "errors"
  32. "os"
  33. "sync"
  34. "time"
  35. "github.com/ethereum/go-ethereum/common"
  36. "github.com/ethereum/go-ethereum/crypto"
  37. )
  38. var (
  39. ErrLocked = errors.New("account is locked")
  40. ErrNoKeys = errors.New("no keys in store")
  41. )
  42. type Account struct {
  43. Address common.Address
  44. }
  45. type Manager struct {
  46. keyStore crypto.KeyStore2
  47. unlocked map[common.Address]*unlocked
  48. mutex sync.RWMutex
  49. }
  50. type unlocked struct {
  51. *crypto.Key
  52. abort chan struct{}
  53. }
  54. func NewManager(keyStore crypto.KeyStore2) *Manager {
  55. return &Manager{
  56. keyStore: keyStore,
  57. unlocked: make(map[common.Address]*unlocked),
  58. }
  59. }
  60. func (am *Manager) HasAccount(addr common.Address) bool {
  61. accounts, _ := am.Accounts()
  62. for _, acct := range accounts {
  63. if acct.Address == addr {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. func (am *Manager) DeleteAccount(address common.Address, auth string) error {
  70. return am.keyStore.DeleteKey(address, auth)
  71. }
  72. func (am *Manager) Sign(a Account, toSign []byte) (signature []byte, err error) {
  73. am.mutex.RLock()
  74. unlockedKey, found := am.unlocked[a.Address]
  75. am.mutex.RUnlock()
  76. if !found {
  77. return nil, ErrLocked
  78. }
  79. signature, err = crypto.Sign(toSign, unlockedKey.PrivateKey)
  80. return signature, err
  81. }
  82. // unlock indefinitely
  83. func (am *Manager) Unlock(addr common.Address, keyAuth string) error {
  84. return am.TimedUnlock(addr, keyAuth, 0)
  85. }
  86. // Unlock unlocks the account with the given address. The account
  87. // stays unlocked for the duration of timeout
  88. // it timeout is 0 the account is unlocked for the entire session
  89. func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time.Duration) error {
  90. key, err := am.keyStore.GetKey(addr, keyAuth)
  91. if err != nil {
  92. return err
  93. }
  94. var u *unlocked
  95. am.mutex.Lock()
  96. defer am.mutex.Unlock()
  97. var found bool
  98. u, found = am.unlocked[addr]
  99. if found {
  100. // terminate dropLater for this key to avoid unexpected drops.
  101. if u.abort != nil {
  102. close(u.abort)
  103. }
  104. }
  105. if timeout > 0 {
  106. u = &unlocked{Key: key, abort: make(chan struct{})}
  107. go am.expire(addr, u, timeout)
  108. } else {
  109. u = &unlocked{Key: key}
  110. }
  111. am.unlocked[addr] = u
  112. return nil
  113. }
  114. func (am *Manager) expire(addr common.Address, u *unlocked, timeout time.Duration) {
  115. t := time.NewTimer(timeout)
  116. defer t.Stop()
  117. select {
  118. case <-u.abort:
  119. // just quit
  120. case <-t.C:
  121. am.mutex.Lock()
  122. // only drop if it's still the same key instance that dropLater
  123. // was launched with. we can check that using pointer equality
  124. // because the map stores a new pointer every time the key is
  125. // unlocked.
  126. if am.unlocked[addr] == u {
  127. zeroKey(u.PrivateKey)
  128. delete(am.unlocked, addr)
  129. }
  130. am.mutex.Unlock()
  131. }
  132. }
  133. func (am *Manager) NewAccount(auth string) (Account, error) {
  134. key, err := am.keyStore.GenerateNewKey(crand.Reader, auth)
  135. if err != nil {
  136. return Account{}, err
  137. }
  138. return Account{Address: key.Address}, nil
  139. }
  140. func (am *Manager) Accounts() ([]Account, error) {
  141. addresses, err := am.keyStore.GetKeyAddresses()
  142. if os.IsNotExist(err) {
  143. return nil, ErrNoKeys
  144. } else if err != nil {
  145. return nil, err
  146. }
  147. accounts := make([]Account, len(addresses))
  148. for i, addr := range addresses {
  149. accounts[i] = Account{
  150. Address: addr,
  151. }
  152. }
  153. return accounts, err
  154. }
  155. // zeroKey zeroes a private key in memory.
  156. func zeroKey(k *ecdsa.PrivateKey) {
  157. b := k.D.Bits()
  158. for i := range b {
  159. b[i] = 0
  160. }
  161. }
  162. // USE WITH CAUTION = this will save an unencrypted private key on disk
  163. // no cli or js interface
  164. func (am *Manager) Export(path string, addr common.Address, keyAuth string) error {
  165. key, err := am.keyStore.GetKey(addr, keyAuth)
  166. if err != nil {
  167. return err
  168. }
  169. return crypto.SaveECDSA(path, key.PrivateKey)
  170. }
  171. func (am *Manager) Import(path string, keyAuth string) (Account, error) {
  172. privateKeyECDSA, err := crypto.LoadECDSA(path)
  173. if err != nil {
  174. return Account{}, err
  175. }
  176. key := crypto.NewKeyFromECDSA(privateKeyECDSA)
  177. if err = am.keyStore.StoreKey(key, keyAuth); err != nil {
  178. return Account{}, err
  179. }
  180. return Account{Address: key.Address}, nil
  181. }
  182. func (am *Manager) ImportPreSaleKey(keyJSON []byte, password string) (acc Account, err error) {
  183. var key *crypto.Key
  184. key, err = crypto.ImportPreSaleKey(am.keyStore, keyJSON, password)
  185. if err != nil {
  186. return
  187. }
  188. if err = am.keyStore.StoreKey(key, password); err != nil {
  189. return
  190. }
  191. return Account{Address: key.Address}, nil
  192. }