nef: support JSON serialization

This commit is contained in:
Evgeniy Stratonikov 2021-01-13 15:26:35 +03:00
parent ca86b78536
commit 11191c0a08
2 changed files with 34 additions and 6 deletions

View file

@ -36,16 +36,16 @@ const (
// File represents compiled contract file structure according to the NEF3 standard. // File represents compiled contract file structure according to the NEF3 standard.
type File struct { type File struct {
Header Header Header
Script []byte Script []byte `json:"script"`
Checksum uint32 Checksum uint32 `json:"checksum"`
} }
// Header represents File header. // Header represents File header.
type Header struct { type Header struct {
Magic uint32 Magic uint32 `json:"magic"`
Compiler string Compiler string `json:"compiler"`
Version string Version string `json:"version"`
} }
// NewFile returns new NEF3 file with script specified. // NewFile returns new NEF3 file with script specified.

View file

@ -1,6 +1,9 @@
package nef package nef
import ( import (
"encoding/base64"
"encoding/json"
"strconv"
"testing" "testing"
"github.com/nspcc-dev/neo-go/internal/testserdes" "github.com/nspcc-dev/neo-go/internal/testserdes"
@ -74,3 +77,28 @@ func TestBytesFromBytes(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expected, actual) 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)
}