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
/
public_key.go
178 lines (158 loc) · 4.11 KB
/
public_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package crypto
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/btcec"
abci "github.com/tendermint/tendermint/abci/types"
tmCrypto "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tmthrgd/go-hex"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/ripemd160"
)
type PublicKeyJSON struct {
CurveType string
PublicKey string
}
// Returns the length in bytes of the public key
func PublicKeyLength(curveType CurveType) int {
switch curveType {
case CurveTypeEd25519:
return ed25519.PublicKeySize
case CurveTypeSecp256k1:
return btcec.PubKeyBytesLenCompressed
default:
// Other functions rely on this
return 0
}
}
func (p PublicKey) MarshalJSON() ([]byte, error) {
jStruct := PublicKeyJSON{
CurveType: p.CurveType.String(),
PublicKey: hex.EncodeUpperToString(p.Key),
}
txt, err := json.Marshal(jStruct)
return txt, err
}
func (p PublicKey) MarshalText() ([]byte, error) {
return p.MarshalJSON()
}
func (p *PublicKey) UnmarshalJSON(text []byte) error {
var jStruct PublicKeyJSON
err := json.Unmarshal(text, &jStruct)
if err != nil {
return err
}
CurveType, err := CurveTypeFromString(jStruct.CurveType)
if err != nil {
return err
}
bs, err := hex.DecodeString(jStruct.PublicKey)
if err != nil {
return err
}
p.CurveType = CurveType
p.Key = bs
return nil
}
func (p *PublicKey) UnmarshalText(text []byte) error {
return p.UnmarshalJSON(text)
}
func (p PublicKey) IsValid() bool {
publicKeyLength := PublicKeyLength(p.CurveType)
return publicKeyLength != 0 && publicKeyLength == len(p.Key)
}
func (p PublicKey) Verify(msg []byte, signature Signature) bool {
switch p.CurveType {
case CurveTypeEd25519:
return ed25519.Verify(p.Key, msg, signature)
case CurveTypeSecp256k1:
pub, err := btcec.ParsePubKey(p.Key, btcec.S256())
if err != nil {
return false
}
sig, err := btcec.ParseDERSignature(signature, btcec.S256())
if err != nil {
return false
}
return sig.Verify(msg, pub)
default:
panic(fmt.Sprintf("invalid curve type"))
}
}
func (p PublicKey) PublicKey() PublicKey {
return p
}
func (p PublicKey) Address() Address {
switch p.CurveType {
case CurveTypeEd25519:
addr, _ := AddressFromBytes(tmhash.Sum(p.Key))
return addr
case CurveTypeSecp256k1:
sha := sha256.New()
sha.Write(p.Key[:])
hash := ripemd160.New()
hash.Write(sha.Sum(nil))
addr, _ := AddressFromBytes(hash.Sum(nil))
return addr
default:
panic(fmt.Sprintf("unknown CurveType %d", p.CurveType))
}
}
func (p PublicKey) AddressHashType() string {
switch p.CurveType {
case CurveTypeEd25519:
return "go-crypto-0.5.0"
case CurveTypeSecp256k1:
return "btc"
default:
return ""
}
}
func (p PublicKey) RawBytes() []byte {
return p.Key[:]
}
// Return the ABCI PubKey. See Tendermint protobuf.go for the go-crypto conversion this is based on
func (p PublicKey) ABCIPubKey() abci.PubKey {
return abci.PubKey{
Type: p.CurveType.ABCIType(),
Data: p.RawBytes(),
}
}
func PublicKeyFromTendermintPubKey(pubKey tmCrypto.PubKey) (PublicKey, error) {
switch pk := pubKey.(type) {
case tmCrypto.PubKeyEd25519:
return PublicKeyFromBytes(pk[:], CurveTypeEd25519)
case tmCrypto.PubKeySecp256k1:
return PublicKeyFromBytes(pk[:], CurveTypeSecp256k1)
default:
return PublicKey{}, fmt.Errorf("unrecognised tendermint public key type: %v", pk)
}
}
func PublicKeyFromABCIPubKey(pubKey abci.PubKey) (PublicKey, error) {
switch pubKey.Type {
case CurveTypeEd25519.ABCIType():
return PublicKey{
CurveType: CurveTypeEd25519,
Key: pubKey.Data,
}, nil
case CurveTypeSecp256k1.ABCIType():
return PublicKey{
CurveType: CurveTypeEd25519,
Key: pubKey.Data,
}, nil
}
return PublicKey{}, fmt.Errorf("did not recognise ABCI PubKey type: %s", pubKey.Type)
}
func (p PublicKey) String() string {
return hex.EncodeUpperToString(p.Key)
}
// Produces a binary encoding of the CurveType byte plus
// the public key for padded to a fixed width on the right
func (p PublicKey) Encode() []byte {
encoded := make([]byte, PublicKeyLength(p.CurveType)+1)
encoded[0] = p.CurveType.Byte()
copy(encoded[1:], p.Key)
return encoded
}