From d1c9686fe01809681bbf5b157de4e79a5ceed6fe Mon Sep 17 00:00:00 2001 From: Minh Huy Tran Date: Tue, 18 Jun 2024 10:57:47 +0200 Subject: [PATCH] feat(transaction): add marshal method for script group Signed-off-by: Minh Huy Tran --- transaction/script_group.go | 24 ++++++++++++++++++++++++ transaction/signer_test/signer_test.go | 7 ++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/transaction/script_group.go b/transaction/script_group.go index d6048bbb..f463c0b2 100644 --- a/transaction/script_group.go +++ b/transaction/script_group.go @@ -2,6 +2,7 @@ package transaction import ( "encoding/json" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/nervosnetwork/ckb-sdk-go/v2/types" ) @@ -43,3 +44,26 @@ func (r *ScriptGroup) UnmarshalJSON(input []byte) error { } return nil } + +func (r *ScriptGroup) MarshalJSON() ([]byte, error) { + toHexutilArray := func(in []uint32) []hexutil.Uint { + out := make([]hexutil.Uint, len(in)) + for i, data := range in { + out[i] = hexutil.Uint(data) + } + return out + } + + jsonObj := struct { + Script *types.Script `json:"script"` + GroupType types.ScriptType `json:"group_type"` + InputIndices []hexutil.Uint `json:"input_indices"` + OutputIndices []hexutil.Uint `json:"output_indices"` + }{ + Script: r.Script, + GroupType: r.GroupType, + InputIndices: toHexutilArray(r.InputIndices), + OutputIndices: toHexutilArray(r.OutputIndices), + } + return json.Marshal(jsonObj) +} diff --git a/transaction/signer_test/signer_test.go b/transaction/signer_test/signer_test.go index 091453a1..f7404684 100644 --- a/transaction/signer_test/signer_test.go +++ b/transaction/signer_test/signer_test.go @@ -4,6 +4,10 @@ import ( "encoding/json" "errors" "fmt" + "os" + "runtime/debug" + "testing" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/nervosnetwork/ckb-sdk-go/v2/crypto/secp256k1" @@ -13,9 +17,6 @@ import ( "github.com/nervosnetwork/ckb-sdk-go/v2/transaction/signer/omnilock" "github.com/nervosnetwork/ckb-sdk-go/v2/types" "github.com/stretchr/testify/assert" - "os" - "runtime/debug" - "testing" ) func TestIsSingleSigMatched(t *testing.T) {