key.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. package crypto
  21. import (
  22. "bytes"
  23. "crypto/ecdsa"
  24. "encoding/hex"
  25. "encoding/json"
  26. "io"
  27. "code.google.com/p/go-uuid/uuid"
  28. "github.com/ethereum/go-ethereum/common"
  29. )
  30. const (
  31. version = 3
  32. )
  33. type Key struct {
  34. Id uuid.UUID // Version 4 "random" for unique id not derived from key data
  35. // to simplify lookups we also store the address
  36. Address common.Address
  37. // we only store privkey as pubkey/address can be derived from it
  38. // privkey in this struct is always in plaintext
  39. PrivateKey *ecdsa.PrivateKey
  40. }
  41. type plainKeyJSON struct {
  42. Address string `json:"address"`
  43. PrivateKey string `json:"privatekey"`
  44. Id string `json:"id"`
  45. Version int `json:"version"`
  46. }
  47. type encryptedKeyJSONV3 struct {
  48. Address string `json:"address"`
  49. Crypto cryptoJSON
  50. Id string `json:"id"`
  51. Version int `json:"version"`
  52. }
  53. type encryptedKeyJSONV1 struct {
  54. Address string `json:"address"`
  55. Crypto cryptoJSON
  56. Id string `json:"id"`
  57. Version string `json:"version"`
  58. }
  59. type cryptoJSON struct {
  60. Cipher string `json:"cipher"`
  61. CipherText string `json:"ciphertext"`
  62. CipherParams cipherparamsJSON `json:"cipherparams"`
  63. KDF string `json:"kdf"`
  64. KDFParams map[string]interface{} `json:"kdfparams"`
  65. MAC string `json:"mac"`
  66. }
  67. type cipherparamsJSON struct {
  68. IV string `json:"iv"`
  69. }
  70. type scryptParamsJSON struct {
  71. N int `json:"n"`
  72. R int `json:"r"`
  73. P int `json:"p"`
  74. DkLen int `json:"dklen"`
  75. Salt string `json:"salt"`
  76. }
  77. func (k *Key) MarshalJSON() (j []byte, err error) {
  78. jStruct := plainKeyJSON{
  79. hex.EncodeToString(k.Address[:]),
  80. hex.EncodeToString(FromECDSA(k.PrivateKey)),
  81. k.Id.String(),
  82. version,
  83. }
  84. j, err = json.Marshal(jStruct)
  85. return j, err
  86. }
  87. func (k *Key) UnmarshalJSON(j []byte) (err error) {
  88. keyJSON := new(plainKeyJSON)
  89. err = json.Unmarshal(j, &keyJSON)
  90. if err != nil {
  91. return err
  92. }
  93. u := new(uuid.UUID)
  94. *u = uuid.Parse(keyJSON.Id)
  95. k.Id = *u
  96. addr, err := hex.DecodeString(keyJSON.Address)
  97. if err != nil {
  98. return err
  99. }
  100. privkey, err := hex.DecodeString(keyJSON.PrivateKey)
  101. if err != nil {
  102. return err
  103. }
  104. k.Address = common.BytesToAddress(addr)
  105. k.PrivateKey = ToECDSA(privkey)
  106. return nil
  107. }
  108. func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {
  109. id := uuid.NewRandom()
  110. key := &Key{
  111. Id: id,
  112. Address: PubkeyToAddress(privateKeyECDSA.PublicKey),
  113. PrivateKey: privateKeyECDSA,
  114. }
  115. return key
  116. }
  117. func NewKey(rand io.Reader) *Key {
  118. randBytes := make([]byte, 64)
  119. _, err := rand.Read(randBytes)
  120. if err != nil {
  121. panic("key generation: could not read from random source: " + err.Error())
  122. }
  123. reader := bytes.NewReader(randBytes)
  124. privateKeyECDSA, err := ecdsa.GenerateKey(S256(), reader)
  125. if err != nil {
  126. panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
  127. }
  128. return NewKeyFromECDSA(privateKeyECDSA)
  129. }