81 lines
1.2 KiB
Go
81 lines
1.2 KiB
Go
package solana
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strconv"
|
|
|
|
"github.com/mr-tron/base58"
|
|
)
|
|
|
|
///
|
|
|
|
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
|
|
}
|