neorpc: adjust the way SignerWithWitness is marshalled

Marshal account with `0x` prefix and add a test.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-07-12 08:39:15 +03:00
parent 9f69522ff5
commit 165525b7e9
2 changed files with 41 additions and 1 deletions

View file

@ -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,

40
pkg/neorpc/types_test.go Normal file
View file

@ -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))
}