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