From 165525b7e9f5864ae8784fcc60ce0147de145f58 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 12 Jul 2023 08:39:15 +0300 Subject: [PATCH] neorpc: adjust the way SignerWithWitness is marshalled Marshal account with `0x` prefix and add a test. Signed-off-by: Anna Shaleva --- pkg/neorpc/types.go | 2 +- pkg/neorpc/types_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 pkg/neorpc/types_test.go diff --git a/pkg/neorpc/types.go b/pkg/neorpc/types.go index bbe7b690a..8a2a24dd2 100644 --- a/pkg/neorpc/types.go +++ b/pkg/neorpc/types.go @@ -97,7 +97,7 @@ func (s *SignerWithWitness) MarshalJSON() ([]byte, error) { return nil, fmt.Errorf("failed to marshal scopes: %w", err) } signer := &signerWithWitnessAux{ - Account: s.Account.StringLE(), + Account: `0x` + s.Account.StringLE(), Scopes: sc, AllowedContracts: s.AllowedContracts, AllowedGroups: s.AllowedGroups, diff --git a/pkg/neorpc/types_test.go b/pkg/neorpc/types_test.go new file mode 100644 index 000000000..f0c99ea23 --- /dev/null +++ b/pkg/neorpc/types_test.go @@ -0,0 +1,40 @@ +package neorpc + +import ( + "encoding/json" + "testing" + + "github.com/nspcc-dev/neo-go/internal/testserdes" + "github.com/nspcc-dev/neo-go/pkg/core/transaction" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/stretchr/testify/require" +) + +func TestSignerWithWitnessMarshalUnmarshalJSON(t *testing.T) { + s := &SignerWithWitness{ + Signer: transaction.Signer{ + Account: util.Uint160{1, 2, 3}, + Scopes: transaction.CalledByEntry | transaction.CustomContracts, + AllowedContracts: []util.Uint160{{1, 2, 3, 4}}, + }, + Witness: transaction.Witness{ + InvocationScript: []byte{1, 2, 3}, + VerificationScript: []byte{4, 5, 6}, + }, + } + testserdes.MarshalUnmarshalJSON(t, s, &SignerWithWitness{}) + + // Check marshalling separately to ensure Scopes are marshalled OK. + expected := `{"account":"0xcadb3dc2faa3ef14a13b619c9a43124755aa2569","scopes":"CalledByEntry, CustomContracts"}` + acc, err := util.Uint160DecodeStringLE("cadb3dc2faa3ef14a13b619c9a43124755aa2569") + require.NoError(t, err) + s = &SignerWithWitness{ + Signer: transaction.Signer{ + Account: acc, + Scopes: transaction.CalledByEntry | transaction.CustomContracts, + }, + } + actual, err := json.Marshal(s) + require.NoError(t, err) + require.Equal(t, expected, string(actual)) +}