2020-11-25 11:25:32 -08:00
|
|
|
package solana
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/ed25519"
|
|
|
|
crypto_rand "crypto/rand"
|
2020-11-25 13:09:55 -08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-12-10 22:01:55 -08:00
|
|
|
"io/ioutil"
|
2020-11-25 11:25:32 -08:00
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PrivateKey []byte
|
|
|
|
|
2020-12-10 22:01:55 -08:00
|
|
|
func MustPrivateKeyFromBase58(in string) PrivateKey {
|
|
|
|
out, err := PrivateKeyFromBase58(in)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:25:32 -08:00
|
|
|
func PrivateKeyFromBase58(privkey string) (PrivateKey, error) {
|
|
|
|
res, err := base58.Decode(privkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:01:55 -08:00
|
|
|
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 PrivateKey([]byte(values)), nil
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:25:32 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:09:55 -08:00
|
|
|
func (k PrivateKey) Sign(payload []byte) (Signature, error) {
|
2020-11-25 11:25:32 -08:00
|
|
|
p := ed25519.PrivateKey(k)
|
2020-11-25 13:09:55 -08:00
|
|
|
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
|
2020-11-25 11:25:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k PrivateKey) PublicKey() PublicKey {
|
|
|
|
p := ed25519.PrivateKey(k)
|
|
|
|
pub := p.Public().(ed25519.PublicKey)
|
|
|
|
|
|
|
|
var publicKey PublicKey
|
|
|
|
copy(publicKey[:], pub)
|
|
|
|
|
|
|
|
return publicKey
|
|
|
|
}
|
2020-11-25 13:09:55 -08:00
|
|
|
|
|
|
|
type PublicKey [32]byte
|
|
|
|
|
|
|
|
func (p PublicKey) Equals(pb PublicKey) bool {
|
|
|
|
return p.String() == pb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-12-10 22:01:55 -08:00
|
|
|
return out, fmt.Errorf("decode: %w", err)
|
2020-11-25 13:09:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(val) != 32 {
|
2020-12-10 22:01:55 -08:00
|
|
|
return out, fmt.Errorf("invalid length, expected 32, got %d", len(val))
|
2020-11-25 13:09:55 -08:00
|
|
|
}
|
2020-12-10 22:01:55 -08:00
|
|
|
|
2020-11-25 13:09:55 -08:00
|
|
|
copy(out[:], val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PublicKey) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(base58.Encode(p[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PublicKey) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
var s string
|
2020-12-10 22:01:55 -08:00
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
2020-11-25 13:09:55 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-10 22:01:55 -08:00
|
|
|
*p, err = PublicKeyFromBase58(s)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid public key %q: %w", s, err)
|
2020-11-25 13:09:55 -08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PublicKey) String() string {
|
|
|
|
return base58.Encode(p[:])
|
|
|
|
}
|