diff --git a/pkg/smartcontract/nef/nef.go b/pkg/smartcontract/nef/nef.go index 9d1088af8..74476d0ab 100644 --- a/pkg/smartcontract/nef/nef.go +++ b/pkg/smartcontract/nef/nef.go @@ -36,16 +36,16 @@ const ( // File represents compiled contract file structure according to the NEF3 standard. type File struct { - Header Header - Script []byte - Checksum uint32 + Header + Script []byte `json:"script"` + Checksum uint32 `json:"checksum"` } // Header represents File header. type Header struct { - Magic uint32 - Compiler string - Version string + Magic uint32 `json:"magic"` + Compiler string `json:"compiler"` + Version string `json:"version"` } // NewFile returns new NEF3 file with script specified. diff --git a/pkg/smartcontract/nef/nef_test.go b/pkg/smartcontract/nef/nef_test.go index f5236fd64..c0054e461 100644 --- a/pkg/smartcontract/nef/nef_test.go +++ b/pkg/smartcontract/nef/nef_test.go @@ -1,6 +1,9 @@ package nef import ( + "encoding/base64" + "encoding/json" + "strconv" "testing" "github.com/nspcc-dev/neo-go/internal/testserdes" @@ -74,3 +77,28 @@ func TestBytesFromBytes(t *testing.T) { require.NoError(t, err) require.Equal(t, expected, actual) } + +func TestMarshalUnmarshalJSON(t *testing.T) { + expected := &File{ + Header: Header{ + Magic: Magic, + Compiler: "test.compiler", + Version: "test.ver", + }, + Script: []byte{1, 2, 3, 4}, + } + expected.Checksum = expected.CalculateChecksum() + + data, err := json.Marshal(expected) + require.NoError(t, err) + require.JSONEq(t, `{ + "magic":`+strconv.FormatUint(uint64(Magic), 10)+`, + "compiler": "test.compiler", + "version": "test.ver", + "script": "`+base64.StdEncoding.EncodeToString(expected.Script)+`", + "checksum":`+strconv.FormatUint(uint64(expected.Checksum), 10)+`}`, string(data)) + + actual := new(File) + require.NoError(t, json.Unmarshal(data, actual)) + require.Equal(t, expected, actual) +}