Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions bindings/bind/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/block-vision/sui-go-sdk/transaction"

bindutils "github.com/smartcontractkit/chainlink-sui/bindings/utils"
"github.com/smartcontractkit/chainlink-sui/codec"
"github.com/smartcontractkit/chainlink-sui/relayer/client"
)

Expand All @@ -32,10 +33,10 @@ type transactionObjectChangesClient interface {
GetTransactionChangedObjects(ctx context.Context, digest string) ([]*suirpcv2.ChangedObject, error)
}

type Object struct {
Id string
InitialSharedVersion *uint64
}
// Object is an alias for codec.Object for backward compatibility.
//
// Deprecated: use codec.Object directly.
type Object = codec.Object

type EmptyMoveStructWitness struct{}

Expand Down
36 changes: 7 additions & 29 deletions bindings/bind/json_decoder.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
package bind

import (
"encoding/json"
"errors"
"fmt"
"math/big"
"reflect"
"strings"

"github.com/mitchellh/mapstructure"

"github.com/smartcontractkit/chainlink-sui/codec"
)

// DecodeJSONReturn decodes gRPC/JSON Move return values into the provided target.
//
// Deprecated: use codec.DecodeJSONReturn directly.
func DecodeJSONReturn(data any, target any) error {
if target == nil {
return errors.New("target cannot be nil")
}

if raw, ok := data.(json.RawMessage); ok {
var intermediate any
if err := json.Unmarshal(raw, &intermediate); err != nil {
return fmt.Errorf("json unmarshal failed: %w", err)
}

return DecodeJSONReturn(intermediate, target)
}

if reflect.TypeOf(data) == reflect.TypeOf(target).Elem() {
reflect.ValueOf(target).Elem().Set(reflect.ValueOf(data))
return nil
}

targetType := reflect.TypeOf(target).Elem()

bigPtrT := reflect.TypeOf((*big.Int)(nil))
bigValT := bigPtrT.Elem()
if targetType == bigValT || targetType == bigPtrT {
return decodeBigInt(data, target)
}

return decodeWithMapstructure(data, target)
return codec.DecodeJSONReturn(data, target)
}

// decodeBigInt decodes a string into a big.Int target.
func decodeBigInt(data any, target any) error {
str, ok := data.(string)
if !ok {
Expand All @@ -67,6 +44,7 @@ func decodeBigInt(data any, target any) error {
return nil
}

// decodeWithMapstructure decodes using mapstructure with custom hooks.
func decodeWithMapstructure(data any, target any) error {
config := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
Expand Down
58 changes: 9 additions & 49 deletions bindings/bind/utils.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,25 @@
package bind

import (
"encoding/hex"
"fmt"
"strings"
"unicode"

"github.com/block-vision/sui-go-sdk/models"
"github.com/block-vision/sui-go-sdk/utils"

"github.com/smartcontractkit/chainlink-sui/codec"
)

// IsSuiAddress returns true if addr is a valid Sui address/ObjectID.
// It is an improvement over the sui-go-sdk's IsValidSuiAddress function.
//
// Deprecated: use codec.IsSuiAddress directly.
func IsSuiAddress(addr string) bool {
if !(strings.HasPrefix(addr, "0x") || strings.HasPrefix(addr, "0X")) {
return false
}

h := addr[2:]

// 1..64 hex chars
if len(h) == 0 || len(h) > 64 {
return false
}

// hex only
for _, r := range h {
if !isHexRune(r) {
return false
}
}

// hex.DecodeString requires even length; allow odd by left-padding one '0'
if len(h)%2 == 1 {
h = "0" + h
}

b, err := hex.DecodeString(h)

if err != nil {
return false
}

// must be ≤32 bytes (32 bytes == 64 hex chars)
return len(b) <= 32
return codec.IsSuiAddress(addr)
}

Comment thread
FelixFan1992 marked this conversation as resolved.
// ToSuiAddress normalizes and validates a Sui address
// ToSuiAddress normalizes and validates a Sui address.
//
// Deprecated: use codec.ToSuiAddress directly.
func ToSuiAddress(address string) (string, error) {
normalized := utils.NormalizeSuiAddress(address)
if !IsSuiAddress(string(normalized)) {
return "", fmt.Errorf("invalid sui address: %s", address)
}

return string(normalized), nil
return codec.ToSuiAddress(address)
}

func GetFailedTxError(tx *models.SuiTransactionBlockResponse) error {
Expand All @@ -63,9 +29,3 @@ func GetFailedTxError(tx *models.SuiTransactionBlockResponse) error {

return fmt.Errorf("transaction failed with error: %s", tx.Effects.Status.Error)
}

func isHexRune(r rune) bool {
return unicode.IsDigit(r) ||
('a' <= r && r <= 'f') ||
('A' <= r && r <= 'F')
}
Loading
Loading