forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic_key.go
145 lines (129 loc) · 3.44 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
package crypto
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/btcec"
"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) 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("'%X' is not a valid ed25519 signature for message: %X", signature, 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.ParseDERSignature(signature.Signature, btcec.S256())
if err != nil {
return fmt.Errorf("could not parse DER signature for secp256k1 key: %v", err)
}
if sig.Verify(msg, pub) {
return nil
}
return fmt.Errorf("'%X' is not a valid secp256k1 signature for message: %X", signature, msg)
default:
return fmt.Errorf("invalid curve type")
}
}
func (p PublicKey) Address() Address {
switch p.CurveType {
case CurveTypeEd25519:
addr, _ := AddressFromBytes(tmhash.Sum(p.PublicKey))
return addr
case CurveTypeSecp256k1:
sha := sha256.New()
sha.Write(p.PublicKey[:])
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) 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) Encode() []byte {
encoded := make([]byte, PublicKeyLength(p.CurveType)+1)
encoded[0] = p.CurveType.Byte()
copy(encoded[1:], p.PublicKey)
return encoded
}