key.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "code.google.com/p/go-uuid/uuid"
  24. "crypto/ecdsa"
  25. "crypto/elliptic"
  26. "encoding/json"
  27. "io"
  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 []byte
  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. Id []byte
  39. Address []byte
  40. PrivateKey []byte
  41. }
  42. type cipherJSON struct {
  43. Salt []byte
  44. IV []byte
  45. CipherText []byte
  46. }
  47. type encryptedKeyJSON struct {
  48. Id []byte
  49. Address []byte
  50. Crypto cipherJSON
  51. }
  52. func (k *Key) MarshalJSON() (j []byte, err error) {
  53. jStruct := plainKeyJSON{
  54. k.Id,
  55. k.Address,
  56. FromECDSA(k.PrivateKey),
  57. }
  58. j, err = json.Marshal(jStruct)
  59. return j, err
  60. }
  61. func (k *Key) UnmarshalJSON(j []byte) (err error) {
  62. keyJSON := new(plainKeyJSON)
  63. err = json.Unmarshal(j, &keyJSON)
  64. if err != nil {
  65. return err
  66. }
  67. u := new(uuid.UUID)
  68. *u = keyJSON.Id
  69. k.Id = *u
  70. k.Address = keyJSON.Address
  71. k.PrivateKey = ToECDSA(keyJSON.PrivateKey)
  72. return err
  73. }
  74. func NewKey(rand io.Reader) *Key {
  75. randBytes := make([]byte, 32)
  76. _, err := rand.Read(randBytes)
  77. if err != nil {
  78. panic("key generation: could not read from random source: " + err.Error())
  79. }
  80. reader := bytes.NewReader(randBytes)
  81. _, x, y, err := elliptic.GenerateKey(S256(), reader)
  82. if err != nil {
  83. panic("key generation: elliptic.GenerateKey failed: " + err.Error())
  84. }
  85. privateKeyMarshalled := elliptic.Marshal(S256(), x, y)
  86. privateKeyECDSA := ToECDSA(privateKeyMarshalled)
  87. id := uuid.NewRandom()
  88. key := &Key{
  89. Id: id,
  90. Address: pubkeyToAddress(privateKeyECDSA.PublicKey),
  91. PrivateKey: privateKeyECDSA,
  92. }
  93. return key
  94. }