account_manager.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "github.com/ethereum/go-ethereum/crypto"
  31. "github.com/ethereum/go-ethereum/crypto/secp256k1"
  32. )
  33. // TODO: better name for this struct?
  34. type UserAccount struct {
  35. Addr []byte
  36. }
  37. type AccountManager struct {
  38. keyStore crypto.KeyStore2
  39. }
  40. // TODO: get key by addr - modify KeyStore2 GetKey to work with addr
  41. // TODO: pass through passphrase for APIs which require access to private key?
  42. func NewAccountManager(keyStore crypto.KeyStore2) AccountManager {
  43. am := &AccountManager{
  44. keyStore: keyStore,
  45. }
  46. return *am
  47. }
  48. func (am *AccountManager) Sign(fromAddr []byte, keyAuth string, toSign []byte) (signature []byte, err error) {
  49. key, err := am.keyStore.GetKey(fromAddr, keyAuth)
  50. if err != nil {
  51. return nil, err
  52. }
  53. privKey := crypto.FromECDSA(key.PrivateKey)
  54. // TODO: what is second value?
  55. signature, err = secp256k1.Sign(toSign, privKey)
  56. return signature, err
  57. }
  58. func (am AccountManager) NewAccount(auth string) (*UserAccount, error) {
  59. key, err := am.keyStore.GenerateNewKey(crand.Reader, auth)
  60. if err != nil {
  61. return nil, err
  62. }
  63. ua := &UserAccount{
  64. Addr: key.Address,
  65. }
  66. return ua, err
  67. }
  68. // set of accounts == set of keys in given key store
  69. // TODO: do we need persistence of accounts as well?
  70. func (am *AccountManager) Accounts() ([]UserAccount, error) {
  71. addresses, err := am.keyStore.GetKeyAddresses()
  72. if err != nil {
  73. return nil, err
  74. }
  75. accounts := make([]UserAccount, len(addresses))
  76. for i, addr := range addresses {
  77. ua := &UserAccount{
  78. Addr: addr,
  79. }
  80. accounts[i] = *ua
  81. }
  82. return accounts, err
  83. }