solana-go/nativetypes.go

180 lines
2.8 KiB
Go
Raw Normal View History

2020-07-31 09:17:36 -07:00
package solana
import (
"encoding/json"
"errors"
"fmt"
2020-07-31 09:17:36 -07:00
"strconv"
"github.com/mr-tron/base58"
)
type Padding []byte
type Hash PublicKey
///
type Signature [32]byte
func SignatureFromBase58(in string) (out Signature, err error) {
val, err := base58.Decode(in)
if err != nil {
return
}
if len(val) != 64 {
err = fmt.Errorf("invalid length, expected 64, got %d", len(val))
return
}
copy(out[:], val)
return
}
func (p Signature) MarshalJSON() ([]byte, error) {
return json.Marshal(base58.Encode(p[:]))
}
func (p *Signature) UnmarshalJSON(data []byte) (err error) {
var s string
err = json.Unmarshal(data, &s)
if err != nil {
return
}
dat, err := base58.Decode(s)
if err != nil {
return err
}
if len(dat) != 64 {
return errors.New("invalid data length for public key")
}
target := Signature{}
copy(target[:], dat)
*p = target
return
}
func (p Signature) String() string {
return base58.Encode(p[:])
}
///
type PublicKey [32]byte
func PublicKeyFromBase58(in string) (out PublicKey, err error) {
val, err := base58.Decode(in)
if err != nil {
return
}
if len(val) != 32 {
err = fmt.Errorf("invalid length, expected 32, got %d", len(val))
return
}
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
err = json.Unmarshal(data, &s)
if err != nil {
return
}
dat, err := base58.Decode(s)
if err != nil {
return err
}
if len(dat) != 32 {
return errors.New("invalid data length for public key")
}
target := PublicKey{}
copy(target[:], dat)
*p = target
return
}
func (p PublicKey) String() string {
return base58.Encode(p[:])
}
2020-07-31 09:17:36 -07:00
///
type Base58 []byte
func (t Base58) MarshalJSON() ([]byte, error) {
return json.Marshal(base58.Encode(t))
}
func (t *Base58) UnmarshalJSON(data []byte) (err error) {
var s string
err = json.Unmarshal(data, &s)
if err != nil {
return
}
*t, err = base58.Decode(s)
return
}
func (t Base58) String() string {
return base58.Encode(t)
}
///
type U64 uint64
func (i U64) MarshalJSON() (data []byte, err error) {
if i > 0xffffffff {
encodedInt, err := json.Marshal(uint64(i))
if err != nil {
return nil, err
}
data = append([]byte{'"'}, encodedInt...)
data = append(data, '"')
return data, nil
}
return json.Marshal(uint64(i))
}
func (i *U64) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return errors.New("empty value")
}
if data[0] == '"' {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
val, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return err
}
*i = U64(val)
return nil
}
var v uint64
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*i = U64(v)
return nil
}