account_manager.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "sync"
  32. "time"
  33. "github.com/ethereum/go-ethereum/crypto"
  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. unlockMilliseconds time.Duration
  44. mutex sync.RWMutex
  45. }
  46. func NewAccountManager(keyStore crypto.KeyStore2, unlockMilliseconds time.Duration) AccountManager {
  47. keysMap := make(map[string]crypto.Key)
  48. am := &AccountManager{
  49. keyStore: keyStore,
  50. unlockedKeys: keysMap,
  51. unlockMilliseconds: unlockMilliseconds,
  52. }
  53. return *am
  54. }
  55. func (am AccountManager) DeleteAccount(address []byte, auth string) error {
  56. return am.keyStore.DeleteKey(address, auth)
  57. }
  58. func (am *AccountManager) Sign(fromAccount *Account, toSign []byte) (signature []byte, err error) {
  59. am.mutex.RLock()
  60. unlockedKey := am.unlockedKeys[string(fromAccount.Address)]
  61. am.mutex.RUnlock()
  62. if unlockedKey.Address == nil {
  63. return nil, ErrLocked
  64. }
  65. signature, err = crypto.Sign(toSign, unlockedKey.PrivateKey)
  66. return signature, err
  67. }
  68. func (am *AccountManager) SignLocked(fromAccount *Account, keyAuth string, toSign []byte) (signature []byte, err error) {
  69. key, err := am.keyStore.GetKey(fromAccount.Address, keyAuth)
  70. if err != nil {
  71. return nil, err
  72. }
  73. am.mutex.RLock()
  74. am.unlockedKeys[string(fromAccount.Address)] = *key
  75. am.mutex.RUnlock()
  76. go unlockLater(am, fromAccount.Address)
  77. signature, err = crypto.Sign(toSign, key.PrivateKey)
  78. return signature, err
  79. }
  80. func (am AccountManager) NewAccount(auth string) (*Account, error) {
  81. key, err := am.keyStore.GenerateNewKey(crand.Reader, auth)
  82. if err != nil {
  83. return nil, err
  84. }
  85. ua := &Account{
  86. Address: key.Address,
  87. }
  88. return ua, err
  89. }
  90. func (am *AccountManager) Accounts() ([]Account, error) {
  91. addresses, err := am.keyStore.GetKeyAddresses()
  92. if err != nil {
  93. return nil, err
  94. }
  95. accounts := make([]Account, len(addresses))
  96. for i, addr := range addresses {
  97. accounts[i] = Account{
  98. Address: addr,
  99. }
  100. }
  101. return accounts, err
  102. }
  103. func unlockLater(am *AccountManager, addr []byte) {
  104. select {
  105. case <-time.After(time.Millisecond * am.unlockMilliseconds):
  106. }
  107. am.mutex.RLock()
  108. // TODO: how do we know the key is actually gone from memory?
  109. delete(am.unlockedKeys, string(addr))
  110. am.mutex.RUnlock()
  111. }