key.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package crypto
  17. import (
  18. "bytes"
  19. "crypto/ecdsa"
  20. "encoding/hex"
  21. "encoding/json"
  22. "io"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/pborman/uuid"
  25. )
  26. const (
  27. version = 3
  28. )
  29. type Key struct {
  30. Id uuid.UUID // Version 4 "random" for unique id not derived from key data
  31. // to simplify lookups we also store the address
  32. Address common.Address
  33. // we only store privkey as pubkey/address can be derived from it
  34. // privkey in this struct is always in plaintext
  35. PrivateKey *ecdsa.PrivateKey
  36. }
  37. type plainKeyJSON struct {
  38. Address string `json:"address"`
  39. PrivateKey string `json:"privatekey"`
  40. Id string `json:"id"`
  41. Version int `json:"version"`
  42. }
  43. type encryptedKeyJSONV3 struct {
  44. Address string `json:"address"`
  45. Crypto cryptoJSON
  46. Id string `json:"id"`
  47. Version int `json:"version"`
  48. }
  49. type encryptedKeyJSONV1 struct {
  50. Address string `json:"address"`
  51. Crypto cryptoJSON
  52. Id string `json:"id"`
  53. Version string `json:"version"`
  54. }
  55. type cryptoJSON struct {
  56. Cipher string `json:"cipher"`
  57. CipherText string `json:"ciphertext"`
  58. CipherParams cipherparamsJSON `json:"cipherparams"`
  59. KDF string `json:"kdf"`
  60. KDFParams map[string]interface{} `json:"kdfparams"`
  61. MAC string `json:"mac"`
  62. }
  63. type cipherparamsJSON struct {
  64. IV string `json:"iv"`
  65. }
  66. type scryptParamsJSON struct {
  67. N int `json:"n"`
  68. R int `json:"r"`
  69. P int `json:"p"`
  70. DkLen int `json:"dklen"`
  71. Salt string `json:"salt"`
  72. }
  73. func (k *Key) MarshalJSON() (j []byte, err error) {
  74. jStruct := plainKeyJSON{
  75. hex.EncodeToString(k.Address[:]),
  76. hex.EncodeToString(FromECDSA(k.PrivateKey)),
  77. k.Id.String(),
  78. version,
  79. }
  80. j, err = json.Marshal(jStruct)
  81. return j, err
  82. }
  83. func (k *Key) UnmarshalJSON(j []byte) (err error) {
  84. keyJSON := new(plainKeyJSON)
  85. err = json.Unmarshal(j, &keyJSON)
  86. if err != nil {
  87. return err
  88. }
  89. u := new(uuid.UUID)
  90. *u = uuid.Parse(keyJSON.Id)
  91. k.Id = *u
  92. addr, err := hex.DecodeString(keyJSON.Address)
  93. if err != nil {
  94. return err
  95. }
  96. privkey, err := hex.DecodeString(keyJSON.PrivateKey)
  97. if err != nil {
  98. return err
  99. }
  100. k.Address = common.BytesToAddress(addr)
  101. k.PrivateKey = ToECDSA(privkey)
  102. return nil
  103. }
  104. func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {
  105. id := uuid.NewRandom()
  106. key := &Key{
  107. Id: id,
  108. Address: PubkeyToAddress(privateKeyECDSA.PublicKey),
  109. PrivateKey: privateKeyECDSA,
  110. }
  111. return key
  112. }
  113. func NewKey(rand io.Reader) *Key {
  114. randBytes := make([]byte, 64)
  115. _, err := rand.Read(randBytes)
  116. if err != nil {
  117. panic("key generation: could not read from random source: " + err.Error())
  118. }
  119. reader := bytes.NewReader(randBytes)
  120. privateKeyECDSA, err := ecdsa.GenerateKey(S256(), reader)
  121. if err != nil {
  122. panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
  123. }
  124. return NewKeyFromECDSA(privateKeyECDSA)
  125. }