This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
key.go
134 lines (116 loc) · 2.9 KB
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package keys
import (
"encoding/json"
"fmt"
"github.com/hyperledger/burrow/crypto"
"github.com/tmthrgd/go-hex"
)
type Key struct {
CurveType crypto.CurveType
Address crypto.Address
PublicKey crypto.PublicKey
PrivateKey crypto.PrivateKey
}
// json encodings - addresses should be hex encoded
type keyJSON struct {
CurveType string
Address string
PublicKey string
AddressHash string
PrivateKey privateKeyJSON
}
type privateKeyJSON struct {
Crypto string
Plain string `json:",omitempty"`
Salt []byte `json:",omitempty"`
Nonce []byte `json:",omitempty"`
CipherText []byte `json:",omitempty"`
}
func NewKey(typ crypto.CurveType) (*Key, error) {
privKey, err := crypto.GeneratePrivateKey(nil, typ)
if err != nil {
return nil, err
}
pubKey := privKey.GetPublicKey()
return &Key{
CurveType: typ,
PublicKey: *pubKey,
Address: pubKey.GetAddress(),
PrivateKey: privKey,
}, nil
}
func NewKeyFromPub(curveType crypto.CurveType, PubKeyBytes []byte) (*Key, error) {
pubKey, err := crypto.PublicKeyFromBytes(PubKeyBytes, curveType)
if err != nil {
return nil, err
}
return &Key{
CurveType: curveType,
PublicKey: *pubKey,
Address: pubKey.GetAddress(),
}, nil
}
func NewKeyFromPriv(curveType crypto.CurveType, PrivKeyBytes []byte) (*Key, error) {
privKey, err := crypto.PrivateKeyFromRawBytes(PrivKeyBytes, curveType)
if err != nil {
return nil, err
}
pubKey := privKey.GetPublicKey()
return &Key{
CurveType: curveType,
Address: pubKey.GetAddress(),
PublicKey: *pubKey,
PrivateKey: privKey,
}, nil
}
func (k *Key) Pubkey() []byte {
return k.PublicKey.PublicKey
}
func (k *Key) MarshalJSON() (j []byte, err error) {
jStruct := keyJSON{
CurveType: k.CurveType.String(),
Address: hex.EncodeUpperToString(k.Address[:]),
PublicKey: hex.EncodeUpperToString(k.Pubkey()),
AddressHash: k.PublicKey.AddressHashType(),
PrivateKey: privateKeyJSON{Crypto: CryptoNone, Plain: hex.EncodeUpperToString(k.PrivateKey.RawBytes())},
}
j, err = json.Marshal(jStruct)
return j, err
}
func (k *Key) UnmarshalJSON(j []byte) (err error) {
keyJ := new(keyJSON)
err = json.Unmarshal(j, &keyJ)
if err != nil {
return err
}
if len(keyJ.PrivateKey.Plain) == 0 {
return fmt.Errorf("no private key")
}
curveType, err := crypto.CurveTypeFromString(keyJ.CurveType)
if err != nil {
curveType = crypto.CurveTypeEd25519
}
privKey, err := hex.DecodeString(keyJ.PrivateKey.Plain)
if err != nil {
return err
}
k2, err := NewKeyFromPriv(curveType, privKey)
if err != nil {
return err
}
k.Address = k2.Address
k.CurveType = curveType
k.PublicKey = *k2.PrivateKey.GetPublicKey()
k.PrivateKey = k2.PrivateKey
return nil
}
// returns the address if valid, nil otherwise
func isValidKeyJson(j []byte) []byte {
j1 := new(keyJSON)
e1 := json.Unmarshal(j, &j1)
if e1 == nil {
addr, _ := hex.DecodeString(j1.Address)
return addr
}
return nil
}