forked from streamingfast/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
193 lines (157 loc) · 3.87 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
package solana
import (
"crypto"
"crypto/ed25519"
crypto_rand "crypto/rand"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"filippo.io/edwards25519"
"github.com/mr-tron/base58"
)
const MAX_SEED_LENGTH = 32
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 []uint8
err = json.Unmarshal(content, &values)
if err != nil {
return nil, fmt.Errorf("decode keygen file: %w", err)
}
return values, nil
}
func (k PrivateKey) String() string {
return base58.Encode(k)
}
func NewRandomPrivateKey() (PublicKey, PrivateKey, error) {
pub, priv, err := ed25519.GenerateKey(crypto_rand.Reader)
if err != nil {
return PublicKey{}, nil, err
}
var publicKey PublicKey
copy(publicKey[:], pub)
return publicKey, 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
}
type PublicKey [32]byte
func PublicKeyFromBytes(in []byte) (out PublicKey) {
byteCount := len(in)
if byteCount == 0 {
return
}
max := 32
if byteCount < max {
max = byteCount
}
copy(out[:], in[0:max])
return
}
func (p PublicKey) ToSlice() []byte {
return p[:]
}
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) != 32 {
return out, fmt.Errorf("invalid length, expected 32, got %d", len(val))
}
copy(out[:], val)
return
}
func PublicKeyFindProgramAddress(path [][]byte, programId PublicKey) (PublicKey, byte, error) {
nonce := byte(255)
for {
seedsWithNonce := append(path, []byte{nonce})
key, err := createProgramAddress(seedsWithNonce, programId)
if err == nil {
return key, nonce, nil
}
nonce -= 1
if nonce == 0 {
return PublicKey{}, 0x00, fmt.Errorf("unable to find a viable program address nonce")
}
}
}
func createProgramAddress(seeds [][]byte, programId PublicKey) (PublicKey, error) {
buf := []byte{}
for _, seed := range seeds {
if len(seed) > MAX_SEED_LENGTH {
return PublicKey{}, fmt.Errorf("max seed length exceeded")
}
buf = append(buf, seed...)
}
buf = append(buf, programId[:]...)
buf = append(buf, []byte("ProgramDerivedAddress")...)
pkey := sha256.Sum256(buf)
if _, err := new(edwards25519.Point).SetBytes(pkey[:]); err == nil {
return PublicKey{}, fmt.Errorf("invalid seeds, address must fall off the curve")
}
return pkey, nil
}
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
}
var zeroPublicKey = PublicKey{}
func (p PublicKey) IsZero() bool {
return p == zeroPublicKey
}
func (p PublicKey) String() string {
return base58.Encode(p[:])
}