forked from TrueCloudLab/neoneo-go
transaction: unmarshal Output properly
Address is marshaled in base58 and needs to be decoded accordingly.
This commit is contained in:
parent
1dd7c8d337
commit
33f99104e8
2 changed files with 41 additions and 0 deletions
|
@ -24,6 +24,13 @@ type Output struct {
|
||||||
Position int `json:"n"`
|
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.
|
// NewOutput returns a new transaction output.
|
||||||
func NewOutput(assetID util.Uint256, amount util.Fixed8, scriptHash util.Uint160) *Output {
|
func NewOutput(assetID util.Uint256, amount util.Fixed8, scriptHash util.Uint160) *Output {
|
||||||
return &Output{
|
return &Output{
|
||||||
|
@ -56,3 +63,20 @@ func (out *Output) MarshalJSON() ([]byte, error) {
|
||||||
"n": out.Position,
|
"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
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package transaction
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
||||||
|
@ -175,3 +176,19 @@ func TestEncodingTXWithNoData(t *testing.T) {
|
||||||
tx.EncodeBinary(buf.BinWriter)
|
tx.EncodeBinary(buf.BinWriter)
|
||||||
require.Error(t, buf.Err)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue