account_manager.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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) Primary() (addr common.Address, err error) {
  70. addrs, err := am.keyStore.GetKeyAddresses()
  71. if os.IsNotExist(err) {
  72. return common.Address{}, ErrNoKeys
  73. } else if err != nil {
  74. return common.Address{}, err
  75. }
  76. if len(addrs) == 0 {
  77. return common.Address{}, ErrNoKeys
  78. }
  79. return addrs[0], nil
  80. }
  81. func (am *Manager) DeleteAccount(address common.Address, auth string) error {
  82. return am.keyStore.DeleteKey(address, auth)
  83. }
  84. func (am *Manager) Sign(a Account, toSign []byte) (signature []byte, err error) {
  85. am.mutex.RLock()
  86. unlockedKey, found := am.unlocked[a.Address]
  87. am.mutex.RUnlock()
  88. if !found {
  89. return nil, ErrLocked
  90. }
  91. signature, err = crypto.Sign(toSign, unlockedKey.PrivateKey)
  92. return signature, err
  93. }
  94. // unlock indefinitely
  95. func (am *Manager) Unlock(addr common.Address, keyAuth string) error {
  96. return am.TimedUnlock(addr, keyAuth, 0)
  97. }
  98. // Unlock unlocks the account with the given address. The account
  99. // stays unlocked for the duration of timeout
  100. // it timeout is 0 the account is unlocked for the entire session
  101. func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time.Duration) error {
  102. key, err := am.keyStore.GetKey(addr, keyAuth)
  103. if err != nil {
  104. return err
  105. }
  106. var u *unlocked
  107. am.mutex.Lock()
  108. defer am.mutex.Unlock()
  109. var found bool
  110. u, found = am.unlocked[addr]
  111. if found {
  112. // terminate dropLater for this key to avoid unexpected drops.
  113. if u.abort != nil {
  114. close(u.abort)
  115. }
  116. }
  117. if timeout > 0 {
  118. u = &unlocked{Key: key, abort: make(chan struct{})}
  119. go am.expire(addr, u, timeout)
  120. } else {
  121. u = &unlocked{Key: key}
  122. }
  123. am.unlocked[addr] = u
  124. return nil
  125. }
  126. func (am *Manager) expire(addr common.Address, u *unlocked, timeout time.Duration) {
  127. t := time.NewTimer(timeout)
  128. defer t.Stop()
  129. select {
  130. case <-u.abort:
  131. // just quit
  132. case <-t.C:
  133. am.mutex.Lock()
  134. // only drop if it's still the same key instance that dropLater
  135. // was launched with. we can check that using pointer equality
  136. // because the map stores a new pointer every time the key is
  137. // unlocked.
  138. if am.unlocked[addr] == u {
  139. zeroKey(u.PrivateKey)
  140. delete(am.unlocked, addr)
  141. }
  142. am.mutex.Unlock()
  143. }
  144. }
  145. func (am *Manager) NewAccount(auth string) (Account, error) {
  146. key, err := am.keyStore.GenerateNewKey(crand.Reader, auth)
  147. if err != nil {
  148. return Account{}, err
  149. }
  150. return Account{Address: key.Address}, nil
  151. }
  152. func (am *Manager) Accounts() ([]Account, error) {
  153. addresses, err := am.keyStore.GetKeyAddresses()
  154. if os.IsNotExist(err) {
  155. return nil, ErrNoKeys
  156. } else if err != nil {
  157. return nil, err
  158. }
  159. accounts := make([]Account, len(addresses))
  160. for i, addr := range addresses {
  161. accounts[i] = Account{
  162. Address: addr,
  163. }
  164. }
  165. return accounts, err
  166. }
  167. // zeroKey zeroes a private key in memory.
  168. func zeroKey(k *ecdsa.PrivateKey) {
  169. b := k.D.Bits()
  170. for i := range b {
  171. b[i] = 0
  172. }
  173. }
  174. // USE WITH CAUTION = this will save an unencrypted private key on disk
  175. // no cli or js interface
  176. func (am *Manager) Export(path string, addr common.Address, keyAuth string) error {
  177. key, err := am.keyStore.GetKey(addr, keyAuth)
  178. if err != nil {
  179. return err
  180. }
  181. return crypto.SaveECDSA(path, key.PrivateKey)
  182. }
  183. func (am *Manager) Import(path string, keyAuth string) (Account, error) {
  184. privateKeyECDSA, err := crypto.LoadECDSA(path)
  185. if err != nil {
  186. return Account{}, err
  187. }
  188. key := crypto.NewKeyFromECDSA(privateKeyECDSA)
  189. if err = am.keyStore.StoreKey(key, keyAuth); err != nil {
  190. return Account{}, err
  191. }
  192. return Account{Address: key.Address}, nil
  193. }
  194. func (am *Manager) ImportPreSaleKey(keyJSON []byte, password string) (acc Account, err error) {
  195. var key *crypto.Key
  196. key, err = crypto.ImportPreSaleKey(am.keyStore, keyJSON, password)
  197. if err != nil {
  198. return
  199. }
  200. if err = am.keyStore.StoreKey(key, password); err != nil {
  201. return
  202. }
  203. return Account{Address: key.Address}, nil
  204. }