diff --git a/pkg/core/transaction/output.go b/pkg/core/transaction/output.go index 1ee3d3252..11ad85b2e 100644 --- a/pkg/core/transaction/output.go +++ b/pkg/core/transaction/output.go @@ -24,6 +24,13 @@ type Output struct { Position int `json:"n"` } +type outputAux struct { + AssetID util.Uint256 `json:"asset"` + Amount util.Fixed8 `json:"value"` + ScriptHash string `json:"address"` + Position int `json:"n"` +} + // NewOutput returns a new transaction output. func NewOutput(assetID util.Uint256, amount util.Fixed8, scriptHash util.Uint160) *Output { return &Output{ @@ -56,3 +63,20 @@ func (out *Output) MarshalJSON() ([]byte, error) { "n": out.Position, }) } + +// UnmarshalJSON implements json.Unmarshaler interface. +func (out *Output) UnmarshalJSON(data []byte) error { + var outAux outputAux + err := json.Unmarshal(data, &outAux) + if err != nil { + return err + } + out.ScriptHash, err = address.StringToUint160(outAux.ScriptHash) + if err != nil { + return err + } + out.Amount = outAux.Amount + out.AssetID = outAux.AssetID + out.Position = outAux.Position + return nil +} diff --git a/pkg/core/transaction/transaction_test.go b/pkg/core/transaction/transaction_test.go index ec410e5aa..8af560d66 100644 --- a/pkg/core/transaction/transaction_test.go +++ b/pkg/core/transaction/transaction_test.go @@ -2,6 +2,7 @@ package transaction import ( "encoding/hex" + "encoding/json" "testing" "github.com/nspcc-dev/neo-go/pkg/encoding/address" @@ -175,3 +176,19 @@ func TestEncodingTXWithNoData(t *testing.T) { tx.EncodeBinary(buf.BinWriter) require.Error(t, buf.Err) } + +func TestMarshalUnmarshalJSON(t *testing.T) { + tx := NewContractTX() + tx.Outputs = []Output{{ + AssetID: util.Uint256{1, 2, 3, 4}, + Amount: 567, + ScriptHash: util.Uint160{7, 8, 9, 10}, + Position: 13, + }} + data, err := json.Marshal(tx) + require.NoError(t, err) + + txNew := new(Transaction) + require.NoError(t, json.Unmarshal(data, txNew)) + require.Equal(t, tx, txNew) +}