-
Notifications
You must be signed in to change notification settings - Fork 4
/
keys.go
460 lines (384 loc) · 10.8 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2021 github.com/gagliardetto
// This file has been modified by github.com/gagliardetto
//
// Copyright 2020 dfuse Platform 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 solana
import (
"crypto"
"crypto/ed25519"
crypto_rand "crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"io/ioutil"
"math"
"filippo.io/edwards25519"
"github.com/mr-tron/base58"
)
type PrivateKey []byte
func MustPrivateKeyFromBase58(in string) PrivateKey {
out, err := PrivateKeyFromBase58(in)
if err != nil {
panic(err)
}
return out
}
func PrivateKeyFromBase58(privkey string) (PrivateKey, error) {
res, err := base58.Decode(privkey)
if err != nil {
return nil, err
}
return res, nil
}
func PrivateKeyFromSolanaKeygenFile(file string) (PrivateKey, error) {
content, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("read keygen file: %w", err)
}
var values []byte
err = json.Unmarshal(content, &values)
if err != nil {
return nil, fmt.Errorf("decode keygen file: %w", err)
}
return PrivateKey([]byte(values)), nil
}
func (k PrivateKey) String() string {
return base58.Encode(k)
}
func NewRandomPrivateKey() (PrivateKey, error) {
pub, priv, err := ed25519.GenerateKey(crypto_rand.Reader)
if err != nil {
return nil, err
}
var publicKey PublicKey
copy(publicKey[:], pub)
return PrivateKey(priv), nil
}
func (k PrivateKey) Sign(payload []byte) (Signature, error) {
p := ed25519.PrivateKey(k)
signData, err := p.Sign(crypto_rand.Reader, payload, crypto.Hash(0))
if err != nil {
return Signature{}, err
}
var signature Signature
copy(signature[:], signData)
return signature, err
}
func (k PrivateKey) PublicKey() PublicKey {
p := ed25519.PrivateKey(k)
pub := p.Public().(ed25519.PublicKey)
var publicKey PublicKey
copy(publicKey[:], pub)
return publicKey
}
// PK is a convenience alias for PublicKey
type PK = PublicKey
func (p PublicKey) Verify(message []byte, signature Signature) bool {
pub := ed25519.PublicKey(p[:])
return ed25519.Verify(pub, message, signature[:])
}
type PublicKey [PublicKeyLength]byte
func PublicKeyFromBytes(in []byte) (out PublicKey) {
byteCount := len(in)
if byteCount == 0 {
return
}
max := PublicKeyLength
if byteCount < max {
max = byteCount
}
copy(out[:], in[0:max])
return
}
// MPK is a convenience alias for MustPublicKeyFromBase58
func MPK(in string) PublicKey {
return MustPublicKeyFromBase58(in)
}
func MustPublicKeyFromBase58(in string) PublicKey {
out, err := PublicKeyFromBase58(in)
if err != nil {
panic(err)
}
return out
}
func PublicKeyFromBase58(in string) (out PublicKey, err error) {
val, err := base58.Decode(in)
if err != nil {
return out, fmt.Errorf("decode: %w", err)
}
if len(val) != PublicKeyLength {
return out, fmt.Errorf("invalid length, expected %v, got %d", PublicKeyLength, len(val))
}
copy(out[:], val)
return
}
func (p PublicKey) MarshalText() ([]byte, error) {
return []byte(base58.Encode(p[:])), nil
}
func (p *PublicKey) UnmarshalText(data []byte) error {
return p.Set(string(data))
}
func (p PublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(base58.Encode(p[:]))
}
func (p *PublicKey) UnmarshalJSON(data []byte) (err error) {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*p, err = PublicKeyFromBase58(s)
if err != nil {
return fmt.Errorf("invalid public key %q: %w", s, err)
}
return
}
func (p PublicKey) Equals(pb PublicKey) bool {
return p == pb
}
// ToPointer returns a pointer to the pubkey.
func (p PublicKey) ToPointer() *PublicKey {
return &p
}
func (p PublicKey) Bytes() []byte {
return []byte(p[:])
}
// Check if a `Pubkey` is on the ed25519 curve.
func (p PublicKey) IsOnCurve() bool {
return IsOnCurve(p[:])
}
var zeroPublicKey = PublicKey{}
// IsZero returns whether the public key is zero.
// NOTE: the System Program public key is also zero.
func (p PublicKey) IsZero() bool {
return p == zeroPublicKey
}
func (p *PublicKey) Set(s string) (err error) {
*p, err = PublicKeyFromBase58(s)
if err != nil {
return fmt.Errorf("invalid public key %s: %w", s, err)
}
return
}
func (p PublicKey) String() string {
return base58.Encode(p[:])
}
// Short returns a shortened pubkey string,
// only including the first n chars, ellipsis, and the last n characters.
// NOTE: this is ONLY for visual representation for humans,
// and cannot be used for anything else.
func (p PublicKey) Short(n int) string {
return formatShortPubkey(n, p)
}
func formatShortPubkey(n int, pubkey PublicKey) string {
str := pubkey.String()
if n > (len(str)/2)-1 {
n = (len(str) / 2) - 1
}
if n < 2 {
n = 2
}
return str[:n] + "..." + str[len(str)-n:]
}
type PublicKeySlice []PublicKey
// UniqueAppend appends the provided pubkey only if it is not
// already present in the slice.
// Returns true when the provided pubkey wasn't already present.
func (slice *PublicKeySlice) UniqueAppend(pubkey PublicKey) bool {
if !slice.Has(pubkey) {
slice.Append(pubkey)
return true
}
return false
}
func (slice *PublicKeySlice) Append(pubkeys ...PublicKey) {
*slice = append(*slice, pubkeys...)
}
func (slice PublicKeySlice) Has(pubkey PublicKey) bool {
for _, key := range slice {
if key.Equals(pubkey) {
return true
}
}
return false
}
// Split splits the slice into chunks of the specified size.
func (slice PublicKeySlice) Split(chunkSize int) []PublicKeySlice {
divided := make([]PublicKeySlice, 0)
if len(slice) == 0 || chunkSize < 1 {
return divided
}
if len(slice) == 1 {
return append(divided, slice)
}
for i := 0; i < len(slice); i += chunkSize {
end := i + chunkSize
if end > len(slice) {
end = len(slice)
}
divided = append(divided, slice[i:end])
}
return divided
}
// GetAddedRemovedPubkeys accepts two slices of pubkeys (`previous` and `next`), and returns
// two slices:
// - `added` is the slice of pubkeys that are present in `next` but NOT present in `previous`.
// - `removed` is the slice of pubkeys that are present in `previous` but are NOT present in `next`.
func GetAddedRemovedPubkeys(previous PublicKeySlice, next PublicKeySlice) (added PublicKeySlice, removed PublicKeySlice) {
added = make(PublicKeySlice, 0)
removed = make(PublicKeySlice, 0)
for _, prev := range previous {
if !next.Has(prev) {
removed = append(removed, prev)
}
}
for _, nx := range next {
if !previous.Has(nx) {
added = append(added, nx)
}
}
return
}
var nativeProgramIDs = PublicKeySlice{
BPFLoaderProgramID,
BPFLoaderDeprecatedProgramID,
FeatureProgramID,
ConfigProgramID,
StakeProgramID,
VoteProgramID,
Secp256k1ProgramID,
SystemProgramID,
SysVarClockPubkey,
SysVarEpochSchedulePubkey,
SysVarFeesPubkey,
SysVarInstructionsPubkey,
SysVarRecentBlockHashesPubkey,
SysVarRentPubkey,
SysVarRewardsPubkey,
SysVarSlotHashesPubkey,
SysVarSlotHistoryPubkey,
SysVarStakeHistoryPubkey,
}
// https://github.com/solana-labs/solana/blob/216983c50e0a618facc39aa07472ba6d23f1b33a/sdk/program/src/pubkey.rs#L372
func isNativeProgramID(key PublicKey) bool {
return nativeProgramIDs.Has(key)
}
const (
/// Number of bytes in a pubkey.
PublicKeyLength = 32
// Maximum length of derived pubkey seed.
MaxSeedLength = 32
// Maximum number of seeds.
MaxSeeds = 16
/// Number of bytes in a signature.
SignatureLength = 64
// // Maximum string length of a base58 encoded pubkey.
// MaxBase58Length = 44
)
// Ported from https://github.com/solana-labs/solana/blob/216983c50e0a618facc39aa07472ba6d23f1b33a/sdk/program/src/pubkey.rs#L159
func CreateWithSeed(base PublicKey, seed string, owner PublicKey) (PublicKey, error) {
if len(seed) > MaxSeedLength {
return PublicKey{}, errors.New("Max seed length exceeded")
}
// let owner = owner.as_ref();
// if owner.len() >= PDA_MARKER.len() {
// let slice = &owner[owner.len() - PDA_MARKER.len()..];
// if slice == PDA_MARKER {
// return Err(PubkeyError::IllegalOwner);
// }
// }
b := make([]byte, 0, 64+len(seed))
b = append(b, base[:]...)
b = append(b, seed[:]...)
b = append(b, owner[:]...)
hash := sha256.Sum256(b)
return PublicKeyFromBytes(hash[:]), nil
}
const PDA_MARKER = "ProgramDerivedAddress"
var ErrMaxSeedLengthExceeded = errors.New("Max seed length exceeded")
// Create a program address.
// Ported from https://github.com/solana-labs/solana/blob/216983c50e0a618facc39aa07472ba6d23f1b33a/sdk/program/src/pubkey.rs#L204
func CreateProgramAddress(seeds [][]byte, programID PublicKey) (PublicKey, error) {
if len(seeds) > MaxSeeds {
return PublicKey{}, ErrMaxSeedLengthExceeded
}
for _, seed := range seeds {
if len(seed) > MaxSeedLength {
return PublicKey{}, ErrMaxSeedLengthExceeded
}
}
buf := []byte{}
for _, seed := range seeds {
buf = append(buf, seed...)
}
buf = append(buf, programID[:]...)
buf = append(buf, []byte(PDA_MARKER)...)
hash := sha256.Sum256(buf)
if IsOnCurve(hash[:]) {
return PublicKey{}, errors.New("invalid seeds; address must fall off the curve")
}
return PublicKeyFromBytes(hash[:]), nil
}
// Check if the provided `b` is on the ed25519 curve.
func IsOnCurve(b []byte) bool {
_, err := new(edwards25519.Point).SetBytes(b)
isOnCurve := err == nil
return isOnCurve
}
// Find a valid program address and its corresponding bump seed.
func FindProgramAddress(seed [][]byte, programID PublicKey) (PublicKey, uint8, error) {
var address PublicKey
var err error
bumpSeed := uint8(math.MaxUint8)
for bumpSeed != 0 {
address, err = CreateProgramAddress(append(seed, []byte{byte(bumpSeed)}), programID)
if err == nil {
return address, bumpSeed, nil
}
bumpSeed--
}
return PublicKey{}, bumpSeed, errors.New("unable to find a valid program address")
}
func FindAssociatedTokenAddress(
wallet PublicKey,
mint PublicKey,
) (PublicKey, uint8, error) {
return findAssociatedTokenAddressAndBumpSeed(
wallet,
mint,
SPLAssociatedTokenAccountProgramID,
)
}
func findAssociatedTokenAddressAndBumpSeed(
walletAddress PublicKey,
splTokenMintAddress PublicKey,
programID PublicKey,
) (PublicKey, uint8, error) {
return FindProgramAddress([][]byte{
walletAddress[:],
TokenProgramID[:],
splTokenMintAddress[:],
},
programID,
)
}
// FindTokenMetadataAddress returns the token metadata program-derived address given a SPL token mint address.
func FindTokenMetadataAddress(mint PublicKey) (PublicKey, uint8, error) {
seed := [][]byte{
[]byte("metadata"),
TokenMetadataProgramID[:],
mint[:],
}
return FindProgramAddress(seed, TokenMetadataProgramID)
}