Implemented ShortVec.

Pack and unpack a Token `Account` struct too.
This commit is contained in:
Alexandre Bourget 2020-08-14 17:03:31 -04:00
parent 1b77746a25
commit 95ab89e873
4 changed files with 69 additions and 16 deletions

View File

@ -67,6 +67,11 @@ func (p Signature) String() string {
type PublicKey [32]byte
func MustPublicKeyFromBase58(in string) PublicKey {
out, _ := PublicKeyFromBase58(in)
return out
}
func PublicKeyFromBase58(in string) (out PublicKey, err error) {
val, err := base58.Decode(in)
if err != nil {
@ -196,7 +201,16 @@ func (w *ByteWrapper) ReadByte() (byte, error) {
/// ShortVec
type ShortVec uint16
func (v ShortVec) Pack(p []byte, opt *struc.Options) (int, error) {
func (v ShortVec) Pack(buf []byte, opt *struc.Options) (int, error) {
x := uint64(v)
i := 0
for x >= 0x80 {
buf[i] = byte(x) | 0x80
x >>= 7
i++
}
buf[i] = byte(x)
return i + 1, nil
// JAVASCRIPT
// let rem_len = len;
// for (;;) {
@ -229,12 +243,17 @@ func (v ShortVec) Pack(p []byte, opt *struc.Options) (int, error) {
// }
// }
// seq.end()
return 0, nil
}
func (v *ShortVec) Unpack(r io.Reader, length int, opt *struc.Options) error {
var l, s int
for {
res, err := readShortVec(&ByteWrapper{r})
if err != nil {
return err
}
*v = ShortVec(res)
return nil
// var l, s int
// for {
// JAVASCRIPT
// let elem = bytes.shift();
@ -243,7 +262,7 @@ func (v *ShortVec) Unpack(r io.Reader, length int, opt *struc.Options) error {
// if ((elem & 0x80) === 0) {
// break;
// }
}
// }
// RUST
// let mut len: usize = 0;

View File

@ -1,6 +1,7 @@
package solana
import (
"bytes"
"fmt"
"testing"
@ -14,6 +15,12 @@ func TestShortVec(t *testing.T) {
expect []byte
}{
{input: 0x0, expect: []byte{0x0}},
{input: 0x7f, expect: []byte{0x7f}},
{input: 0x80, expect: []byte{0x80, 0x01}},
{input: 0xff, expect: []byte{0xff, 0x01}},
{input: 0x100, expect: []byte{0x80, 0x02}},
{input: 0x7fff, expect: []byte{0xff, 0xff, 0x01}},
{input: 0xffff, expect: []byte{0xff, 0xff, 0x03}},
}
for idx, test := range tests {
@ -25,6 +32,11 @@ func TestShortVec(t *testing.T) {
size, err := el.Pack(b, nil)
require.NoError(t, err)
assert.Equal(t, test.expect, b[:size])
buf := bytes.NewBuffer(b)
target := ShortVec(0)
require.NoError(t, (&target).Unpack(buf, 0, nil))
assert.Equal(t, test.input, uint16(target))
})
}
}

View File

@ -2,9 +2,12 @@ package token
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"testing"
"github.com/dfuse-io/solana-go"
"github.com/lunixbochs/struc"
"github.com/mr-tron/base58"
"github.com/stretchr/testify/assert"
@ -15,11 +18,24 @@ func TestAccount(t *testing.T) {
b58data := "SqtzmJArwV2556pK7AdHbHNPVP2L2WaR6zfcFeot94TzGRUyUMEWew558UxnYEGrmm9b9VZY7MS6TCHT5wqtzaA5Vy8ghoFyGmbRNC58CttRf5GzH9wfjCkncyrmKjfevyjrJ2W9XKLgYGth46ctFWzJJXCeHsYwDx1d"
data, _ := base58.Decode(b58data)
//fmt.Println("HEX:", hex.EncodeToString(data))
// ba71eb12868584549b86f75620e7bb3ac5ef49df3fef0d48ad08e48dfa0fc786 // mint
// d7a1d0a56e355f17cedd5733e36a0cc9e2caf7a435e3256e4c9bff755f682b5a // owner
// 5ece000000000000 // amount
// 00000000 // is delegate set
// 0000000000000000000000000000000000000000000000000000000000000000 // delegate
// 01000000 // is initialized, is native + padding
// 0000000000000000 // delegate amount
var out Account
err := struc.Unpack(bytes.NewReader(data), &out)
require.NoError(t, err)
expect := Account{}
expect := Account{
Mint: solana.MustPublicKeyFromBase58("DYoajiN32pjK8zMAa67ScNn2E7EmXrZ6doABRqfSZ63F"),
Owner: solana.MustPublicKeyFromBase58("FWjmNcjufwC3QFdcHrAK1yAQkCwJSUAxvVFFgvQ1nAJM"),
Amount: solana.U64(52830),
IsInitialized: true,
}
expectJSON, err := json.MarshalIndent(expect, "", " ")
require.NoError(t, err)
@ -27,6 +43,12 @@ func TestAccount(t *testing.T) {
require.NoError(t, err)
assert.JSONEq(t, string(expectJSON), string(outJSON))
buf := &bytes.Buffer{}
assert.NoError(t, struc.Pack(buf, out))
fmt.Println("MAMA", hex.EncodeToString(buf.Bytes()))
assert.Equal(t, b58data, base58.Encode(buf.Bytes()))
}
// func TestMint(t *testing.T) {

View File

@ -26,9 +26,9 @@ type MessageHeader struct {
type CompiledInstruction struct {
ProgramIDIndex uint8 `json:"programIdIndex"`
AccountsCount uint16 `json:"-",struc:"sizeof=Accounts,little"`
AccountsCount uint16 `json:"-" struc:"sizeof=Accounts,little"`
Accounts []uint8 `json:"accounts"`
DataLength uint16 `json:"-",struc:"sizeof=Data,little"`
DataLength uint16 `json:"-" struc:"sizeof=Data,little"`
Data Base58 `json:"data"`
}