Fix pure bcs encode
This commit is contained in:
@@ -8,4 +8,5 @@ var (
|
|||||||
ErrGasDataNotFullySet = errors.New("gas data not fully set")
|
ErrGasDataNotFullySet = errors.New("gas data not fully set")
|
||||||
ErrObjectIdNotSet = errors.New("object id not set")
|
ErrObjectIdNotSet = errors.New("object id not set")
|
||||||
ErrObjectTypeNotSupported = errors.New("object type not supported")
|
ErrObjectTypeNotSupported = errors.New("object type not supported")
|
||||||
|
ErrInvalidSuiAddress = errors.New("invalid Sui address")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package transaction
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/block-vision/sui-go-sdk/models"
|
"github.com/block-vision/sui-go-sdk/models"
|
||||||
@@ -202,7 +203,7 @@ func (tx *Transaction) Object(inputObject InputObject) (Argument, error) {
|
|||||||
return findObj, nil
|
return findObj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
input := tx.Data.AddInput(callArg)
|
input := tx.Data.AddInput(callArg, "Object")
|
||||||
arg := Input{
|
arg := Input{
|
||||||
Input: input.(Input).Input,
|
Input: input.(Input).Input,
|
||||||
Type: input.(Input).Type,
|
Type: input.(Input).Type,
|
||||||
@@ -212,19 +213,33 @@ func (tx *Transaction) Object(inputObject InputObject) (Argument, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tx *Transaction) Pure(inputPure InputPure) (Argument, error) {
|
func (tx *Transaction) Pure(inputPure InputPure) (Argument, error) {
|
||||||
value := inputPure.Value
|
val := inputPure.Value
|
||||||
|
|
||||||
|
if s, ok := val.(string); ok && utils.IsValidSuiAddress(s) {
|
||||||
|
normalized := utils.NormalizeSuiAddress(s)
|
||||||
|
vBytes, err := hex.DecodeString(normalized[2:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(vBytes) != 32 {
|
||||||
|
return nil, ErrInvalidSuiAddress
|
||||||
|
}
|
||||||
|
|
||||||
|
var fixedBytes [32]byte
|
||||||
|
copy(fixedBytes[:], vBytes)
|
||||||
|
val = fixedBytes
|
||||||
|
}
|
||||||
|
|
||||||
bcsEncodedMsg := bytes.Buffer{}
|
bcsEncodedMsg := bytes.Buffer{}
|
||||||
bcsEncoder := mystenbcs.NewEncoder(&bcsEncodedMsg)
|
bcsEncoder := mystenbcs.NewEncoder(&bcsEncodedMsg)
|
||||||
err := bcsEncoder.Encode(value)
|
err := bcsEncoder.Encode(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
bcsBase64 := mystenbcs.ToBase64(bcsEncodedMsg.Bytes())
|
bcsBase64 := mystenbcs.ToBase64(bcsEncodedMsg.Bytes())
|
||||||
|
|
||||||
input := tx.Data.AddInput(Pure{
|
input := tx.Data.AddInput(Pure{bcsBase64}, "Pure")
|
||||||
bcsBase64,
|
|
||||||
})
|
|
||||||
arg := Input{
|
arg := Input{
|
||||||
Input: input.(Input).Input,
|
Input: input.(Input).Input,
|
||||||
Type: input.(Input).Type,
|
Type: input.(Input).Type,
|
||||||
@@ -283,6 +298,7 @@ func (tx *Transaction) build(onlyTransactionKind bool) (string, error) {
|
|||||||
if tx.Data.Sender == nil {
|
if tx.Data.Sender == nil {
|
||||||
return "", ErrSenderNotSet
|
return "", ErrSenderNotSet
|
||||||
}
|
}
|
||||||
|
// TODO: Support get latest gas data online
|
||||||
if !tx.Data.GasData.IsFullySet() {
|
if !tx.Data.GasData.IsFullySet() {
|
||||||
return "", ErrGasDataNotFullySet
|
return "", ErrGasDataNotFullySet
|
||||||
}
|
}
|
||||||
@@ -301,16 +317,12 @@ func (tx *Transaction) build(onlyTransactionKind bool) (string, error) {
|
|||||||
|
|
||||||
func createTransactionResult(index uint16, length *uint16) Argument {
|
func createTransactionResult(index uint16, length *uint16) Argument {
|
||||||
if length == nil {
|
if length == nil {
|
||||||
max := uint16(math.MaxUint16)
|
m := uint16(math.MaxUint16)
|
||||||
length = &max
|
length = &m
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Support multiple results
|
// TODO: Support NestedResult
|
||||||
|
return Result{
|
||||||
return NestedResult{
|
Value: index,
|
||||||
Value: NestedResultValue{
|
|
||||||
Index: index,
|
|
||||||
ResultIndex: 0,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package transaction
|
package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/block-vision/sui-go-sdk/models"
|
"github.com/block-vision/sui-go-sdk/models"
|
||||||
"github.com/block-vision/sui-go-sdk/models/sui_types"
|
"github.com/block-vision/sui-go-sdk/models/sui_types"
|
||||||
)
|
)
|
||||||
@@ -22,13 +20,13 @@ func (td *TransactionDataV1) AddCommand(command Command) (index uint16) {
|
|||||||
return index
|
return index
|
||||||
}
|
}
|
||||||
|
|
||||||
func (td *TransactionDataV1) AddInput(input CallArg) Argument {
|
func (td *TransactionDataV1) AddInput(input CallArg, inputType string) Argument {
|
||||||
index := len(td.TransactionKind.ProgrammableTransaction.Inputs)
|
index := len(td.TransactionKind.ProgrammableTransaction.Inputs)
|
||||||
td.TransactionKind.ProgrammableTransaction.Inputs = append(td.TransactionKind.ProgrammableTransaction.Inputs, input)
|
td.TransactionKind.ProgrammableTransaction.Inputs = append(td.TransactionKind.ProgrammableTransaction.Inputs, input)
|
||||||
|
|
||||||
return Input{
|
return Input{
|
||||||
Input: uint16(index),
|
Input: uint16(index),
|
||||||
Type: strings.ToLower(input.callArgKind()),
|
Type: inputType,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +56,7 @@ func (td *TransactionDataV1) GetInputObject(objectId string) Argument {
|
|||||||
if inputId == objectId {
|
if inputId == objectId {
|
||||||
return Input{
|
return Input{
|
||||||
Input: uint16(i),
|
Input: uint16(i),
|
||||||
Type: strings.ToLower(input.callArgKind()),
|
Type: input.callArgKind(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -323,7 +321,7 @@ func (i Input) argumentKind() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type InputPure struct {
|
type InputPure struct {
|
||||||
Value string
|
Value any
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i InputPure) argumentKind() string {
|
func (i InputPure) argumentKind() string {
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ func NormalizeSuiAddress(input string) string {
|
|||||||
return "0x" + addr
|
return "0x" + addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsValidSuiAddress(addr string) bool {
|
||||||
|
addr = NormalizeSuiAddress(addr)
|
||||||
|
return len(addr) == 66 && strings.HasPrefix(addr, "0x")
|
||||||
|
}
|
||||||
|
|
||||||
func PrettyPrint(v interface{}) {
|
func PrettyPrint(v interface{}) {
|
||||||
b, err := json.Marshal(v)
|
b, err := json.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user