key.go 4.5 KB

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