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
160 lines (143 loc) · 4.08 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
package crypto
import (
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/tendermint/tendermint/crypto/tmhash"
hex "github.com/tmthrgd/go-hex"
"golang.org/x/crypto/ed25519"
)
const (
MaxPublicKeyLength = btcec.PubKeyBytesLenCompressed
PublicKeyFixedWidthEncodingLength = MaxPublicKeyLength + 1
)
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) IsSet() bool {
return p.CurveType != CurveTypeUnset && p.IsValid()
}
func (p PublicKey) MarshalJSON() ([]byte, error) {
jStruct := PublicKeyJSON{
CurveType: p.CurveType.String(),
PublicKey: hex.EncodeUpperToString(p.PublicKey),
}
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.PublicKey = 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.PublicKey)
}
func (p PublicKey) Verify(msg []byte, signature *Signature) error {
switch p.CurveType {
case CurveTypeUnset:
return fmt.Errorf("public key is unset")
case CurveTypeEd25519:
if ed25519.Verify(p.PublicKey.Bytes(), msg, signature.Signature) {
return nil
}
return fmt.Errorf("signature '%X' is not a valid ed25519 signature for message: %s",
signature.Signature, string(msg))
case CurveTypeSecp256k1:
pub, err := btcec.ParsePubKey(p.PublicKey, btcec.S256())
if err != nil {
return fmt.Errorf("could not parse secp256k1 public key: %v", err)
}
sig, err := btcec.ParseSignature(signature.Signature, btcec.S256())
if err != nil {
return fmt.Errorf("could not parse DER signature for secp256k1 key: %v", err)
}
if sig.Verify(Keccak256(msg), pub) {
return nil
}
return fmt.Errorf("signature '%X' is not a valid secp256k1 signature for message: %s",
signature.Signature, string(msg))
default:
return fmt.Errorf("invalid curve type")
}
}
func (p PublicKey) GetAddress() Address {
switch p.CurveType {
case CurveTypeEd25519:
addr, _ := AddressFromBytes(tmhash.Sum(p.PublicKey))
return addr
case CurveTypeSecp256k1:
pub, err := btcec.ParsePubKey(p.PublicKey.Bytes(), btcec.S256())
if err != nil {
return Address{}
}
hash := Keccak256(pub.SerializeUncompressed()[1:])
addr, _ := AddressFromBytes(hash[len(hash)-20:])
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) String() string {
return hex.EncodeUpperToString(p.PublicKey)
}
// Produces a binary encoding of the CurveType byte plus
// the public key for padded to a fixed width on the right
func (p PublicKey) EncodeFixedWidth() []byte {
encoded := make([]byte, PublicKeyFixedWidthEncodingLength)
encoded[0] = p.CurveType.Byte()
copy(encoded[1:], p.PublicKey)
return encoded
}
func DecodePublicKeyFixedWidth(bs []byte) (PublicKey, error) {
const errHeader = "DecodePublicKeyFixedWidth():"
if len(bs) != PublicKeyFixedWidthEncodingLength {
return PublicKey{}, fmt.Errorf("%s expected exactly %d bytes but got %d bytes",
errHeader, PublicKeyFixedWidthEncodingLength, len(bs))
}
curveType := CurveType(bs[0])
return PublicKeyFromBytes(bs[1:PublicKeyLength(curveType)+1], curveType)
}