account_manager.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. crand "crypto/rand"
  30. "errors"
  31. "github.com/ethereum/go-ethereum/crypto"
  32. "sync"
  33. "time"
  34. )
  35. var ErrLocked = errors.New("account is locked; please request passphrase")
  36. // TODO: better name for this struct?
  37. type Account struct {
  38. Address []byte
  39. }
  40. type AccountManager struct {
  41. keyStore crypto.KeyStore2
  42. unlockedKeys map[string]crypto.Key
  43. unlockedMilliSeconds int
  44. mutex sync.Mutex
  45. }
  46. func NewAccountManager(keyStore crypto.KeyStore2, unlockMilliSeconds int) AccountManager {
  47. keysMap := make(map[string]crypto.Key)
  48. am := &AccountManager{
  49. keyStore: keyStore,
  50. unlockedKeys: keysMap,
  51. unlockedMilliSeconds: unlockMilliSeconds,
  52. mutex: sync.Mutex{}, // for accessing unlockedKeys map
  53. }
  54. return *am
  55. }
  56. func (am AccountManager) DeleteAccount(address []byte, auth string) error {
  57. return am.keyStore.DeleteKey(address, auth)
  58. }
  59. func (am *AccountManager) Sign(fromAccount *Account, toSign []byte) (signature []byte, err error) {
  60. am.mutex.Lock()
  61. unlockedKey := am.unlockedKeys[string(fromAccount.Address)]
  62. am.mutex.Unlock()
  63. if unlockedKey.Address == nil {
  64. return nil, ErrLocked
  65. }
  66. signature, err = crypto.Sign(toSign, unlockedKey.PrivateKey)
  67. return signature, err
  68. }
  69. func (am *AccountManager) SignLocked(fromAccount *Account, keyAuth string, toSign []byte) (signature []byte, err error) {
  70. key, err := am.keyStore.GetKey(fromAccount.Address, keyAuth)
  71. if err != nil {
  72. return nil, err
  73. }
  74. am.mutex.Lock()
  75. am.unlockedKeys[string(fromAccount.Address)] = *key
  76. am.mutex.Unlock()
  77. go unlockLater(am, fromAccount.Address)
  78. signature, err = crypto.Sign(toSign, key.PrivateKey)
  79. return signature, err
  80. }
  81. func (am AccountManager) NewAccount(auth string) (*Account, error) {
  82. key, err := am.keyStore.GenerateNewKey(crand.Reader, auth)
  83. if err != nil {
  84. return nil, err
  85. }
  86. ua := &Account{
  87. Address: key.Address,
  88. }
  89. return ua, err
  90. }
  91. func (am *AccountManager) Accounts() ([]Account, error) {
  92. addresses, err := am.keyStore.GetKeyAddresses()
  93. if err != nil {
  94. return nil, err
  95. }
  96. accounts := make([]Account, len(addresses))
  97. for i, addr := range addresses {
  98. accounts[i] = Account{
  99. Address: addr,
  100. }
  101. }
  102. return accounts, err
  103. }
  104. func unlockLater(am *AccountManager, addr []byte) {
  105. time.Sleep(time.Millisecond * time.Duration(am.unlockedMilliSeconds))
  106. am.mutex.Lock()
  107. // TODO: how do we know the key is actually gone from memory?
  108. delete(am.unlockedKeys, string(addr))
  109. am.mutex.Unlock()
  110. }