forked from coinbase/mesh-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keys.go
279 lines (242 loc) · 6.96 KB
/
keys.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keys
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"encoding/hex"
"fmt"
"math/big"
"github.com/btcsuite/btcd/btcec"
"github.com/coinbase/kryptology/pkg/signatures/schnorr/mina"
"github.com/TheArcadiaGroup/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/types"
)
// PrivKeyBytesLen are 32-bytes for all supported curvetypes
const PrivKeyBytesLen = 32
func privateKeyValid(privateKey []byte) error {
// We will need to add a switch statement here if we add support
// for CurveTypes that have a different private key length than
// PrivKeyBytesLen.
if len(privateKey) != PrivKeyBytesLen {
return fmt.Errorf(
"expected %d bytes for private key but got %d: %w",
PrivKeyBytesLen,
len(privateKey),
ErrPrivKeyLengthInvalid,
)
}
if asserter.BytesArrayZero(privateKey) {
return ErrPrivKeyZero
}
return nil
}
// ImportPrivateKey returns a Keypair from a hex-encoded privkey string
func ImportPrivateKey(privKeyHex string, curve types.CurveType) (*KeyPair, error) {
privKey, err := hex.DecodeString(privKeyHex)
if err != nil {
return nil, fmt.Errorf("failed to decode private key hex: %w", ErrPrivKeyUndecodable)
}
// We check the parsed private key length to ensure we don't panic (most
// crypto libraries panic with incorrect private key lengths instead of
// throwing an error).
if err := privateKeyValid(privKey); err != nil {
return nil, fmt.Errorf("private key is invalid: %w", err)
}
var keyPair *KeyPair
switch curve {
case types.Secp256k1:
rawPrivKey, rawPubKey := btcec.PrivKeyFromBytes(btcec.S256(), privKey)
pubKey := &types.PublicKey{
Bytes: rawPubKey.SerializeCompressed(),
CurveType: curve,
}
keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.Serialize(),
}
case types.Edwards25519:
rawPrivKey := ed25519.NewKeyFromSeed(privKey)
pubKey := &types.PublicKey{
Bytes: rawPrivKey.Public().(ed25519.PublicKey),
CurveType: curve,
}
keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.Seed(),
}
case types.Secp256r1:
crv := elliptic.P256()
x, y := crv.ScalarBaseMult(privKey)
// IsOnCurve will return false for the point at infinity (0, 0)
// See:
// https://github.com/golang/go/blob/3298300ddf45a0792b4d8ea5e05f0fbceec4c9f9/src/crypto/elliptic/elliptic.go#L24
if !crv.IsOnCurve(x, y) {
return nil, ErrPubKeyNotOnCurve
}
rawPubKey := ecdsa.PublicKey{X: x, Y: y, Curve: crv}
rawPrivKey := ecdsa.PrivateKey{
PublicKey: rawPubKey,
D: new(big.Int).SetBytes(privKey),
}
pubKey := &types.PublicKey{
Bytes: elliptic.Marshal(crv, rawPubKey.X, rawPubKey.Y),
CurveType: curve,
}
keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.D.Bytes(),
}
case types.Pallas:
rawPrivKey := &mina.SecretKey{}
_ = rawPrivKey.UnmarshalBinary(privKey)
pubKey, _ := rawPrivKey.GetPublicKey().MarshalBinary()
priKeyBytes, _ := rawPrivKey.MarshalBinary()
keyPair = &KeyPair{
PublicKey: &types.PublicKey{
Bytes: pubKey,
CurveType: curve,
},
PrivateKey: priKeyBytes,
}
default:
return nil, fmt.Errorf(
"curve type %s is invalid: %w",
types.PrintStruct(curve),
ErrCurveTypeNotSupported,
)
}
// We test for validity before returning
// the new KeyPair.
if err := keyPair.IsValid(); err != nil {
return nil, fmt.Errorf("key pair is invalid: %w", err)
}
return keyPair, nil
}
// GenerateKeypair returns a Keypair of a specified CurveType
func GenerateKeypair(curve types.CurveType) (*KeyPair, error) {
var keyPair *KeyPair
switch curve {
case types.Secp256k1:
rawPrivKey, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return nil, fmt.Errorf(
"failed to generate private key for secp256k1 curve type: %w",
err,
)
}
rawPubKey := rawPrivKey.PubKey()
pubKey := &types.PublicKey{
Bytes: rawPubKey.SerializeCompressed(),
CurveType: curve,
}
keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.Serialize(),
}
case types.Edwards25519:
rawPubKey, rawPrivKey, err := ed25519.GenerateKey(nil)
if err != nil {
return nil, fmt.Errorf(
"failed to generate key pair for edwards25519 curve type: %w",
err,
)
}
pubKey := &types.PublicKey{
Bytes: rawPubKey,
CurveType: curve,
}
keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.Seed(),
}
case types.Secp256r1:
crv := elliptic.P256()
rawPrivKey, err := ecdsa.GenerateKey(crv, rand.Reader)
if err != nil {
return nil, fmt.Errorf(
"failed to generate private key for secp256r1 curve type: %w",
err,
)
}
rawPubKey := rawPrivKey.PublicKey
pubKey := &types.PublicKey{
Bytes: elliptic.Marshal(crv, rawPubKey.X, rawPubKey.Y),
CurveType: curve,
}
keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKey.D.Bytes(),
}
case types.Pallas:
rawPubKey, rawPrivKey, err := mina.NewKeys()
rawPubKeyBytes, _ := rawPubKey.MarshalBinary()
rawPrivKeyBytes, _ := rawPrivKey.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("failed to generate key pair for pallas curve type: %w", err)
}
pubKey := &types.PublicKey{
Bytes: rawPubKeyBytes,
CurveType: curve,
}
keyPair = &KeyPair{
PublicKey: pubKey,
PrivateKey: rawPrivKeyBytes,
}
default:
return nil, fmt.Errorf(
"curve type %s is invalid: %w",
types.PrintStruct(curve),
ErrCurveTypeNotSupported,
)
}
// We test for validity before returning
// the new KeyPair.
if err := keyPair.IsValid(); err != nil {
return nil, fmt.Errorf("key pair is invalid: %w", err)
}
return keyPair, nil
}
// IsValid checks the validity of a KeyPair.
func (k *KeyPair) IsValid() error {
if err := asserter.PublicKey(k.PublicKey); err != nil {
return fmt.Errorf("public key is invalid: %w", err)
}
if err := privateKeyValid(k.PrivateKey); err != nil {
return fmt.Errorf("private key is invalid: %w", err)
}
return nil
}
// Signer returns the constructs a Signer
// for the KeyPair.
func (k *KeyPair) Signer() (Signer, error) {
switch k.PublicKey.CurveType {
case types.Secp256k1:
return &SignerSecp256k1{k}, nil
case types.Edwards25519:
return &SignerEdwards25519{k}, nil
case types.Secp256r1:
return &SignerSecp256r1{k}, nil
case types.Pallas:
return &SignerPallas{k}, nil
default:
return nil, fmt.Errorf(
"curve type %s is invalid: %w",
types.PrintStruct(k.PublicKey.CurveType),
ErrCurveTypeNotSupported,
)
}
}