|
1 | 1 | package txm |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "math/big" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | "github.com/aptos-labs/aptos-go-sdk" |
| 9 | + "github.com/aptos-labs/aptos-go-sdk/bcs" |
8 | 10 | "github.com/stretchr/testify/require" |
9 | 11 | ) |
10 | 12 |
|
| 13 | +func TestCreateBcsValue(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + t.Run("", func(t *testing.T) { |
| 16 | + address := &aptos.AccountAddress{} |
| 17 | + _ = address.ParseStringRelaxed("0x3b17dad1bdd88f337712cc2f6187bb741d56da467320373fd9198262cc93de76") |
| 18 | + stringAddress := address.StringLong() |
| 19 | + byteAddress, err := bcs.Serialize(address) |
| 20 | + require.NoError(t, err) |
| 21 | + typeTag, err := CreateTypeTag("address") |
| 22 | + require.NoError(t, err) |
| 23 | + |
| 24 | + // Test serializing a hex string value |
| 25 | + serialized, err := CreateBcsValue(typeTag, stringAddress) |
| 26 | + require.NoError(t, err) |
| 27 | + require.Equal(t, byteAddress, serialized) |
| 28 | + |
| 29 | + // Test serializing a base64 string |
| 30 | + // When marshalling using JSON, the bytearray will be serialized as a base64 string, |
| 31 | + // unmarshalling this string into an any will result in it being treated as a string, not a bytearray. |
| 32 | + // CreateBcsValue is supposed to account for this by first testing if the value can be decoded using base64 |
| 33 | + marshaled, err := json.Marshal(struct { |
| 34 | + Address []byte `json:"address"` |
| 35 | + }{Address: byteAddress}) |
| 36 | + require.NoError(t, err) |
| 37 | + result := make(map[string]interface{}) |
| 38 | + err = json.Unmarshal(marshaled, &result) |
| 39 | + require.NoError(t, err) |
| 40 | + serialized, err = CreateBcsValue(typeTag, result["address"].(string)) |
| 41 | + require.NoError(t, err) |
| 42 | + require.Equal(t, byteAddress, serialized) |
| 43 | + }) |
| 44 | +} |
| 45 | + |
11 | 46 | func TestGetBcsValues(t *testing.T) { |
12 | 47 | t.Parallel() |
13 | 48 | t.Run("uint32,uint64", func(t *testing.T) { |
|
0 commit comments