transaction: unmarshal Witness properly

Both verification and invocation scripts need to
be unmarshaled from hex.
Also fix failing RPC tests: block contains non-pointer
`transaction.Witness` field and (*Witness).MarshalJSON method
is not called.
This commit is contained in:
Evgenii Stratonikov 2020-03-03 11:29:07 +03:00
parent 33f99104e8
commit 634e9483d3
2 changed files with 19 additions and 1 deletions

View file

@ -185,6 +185,10 @@ func TestMarshalUnmarshalJSON(t *testing.T) {
ScriptHash: util.Uint160{7, 8, 9, 10},
Position: 13,
}}
tx.Scripts = []Witness{{
InvocationScript: []byte{5, 3, 1},
VerificationScript: []byte{2, 4, 6},
}}
data, err := json.Marshal(tx)
require.NoError(t, err)

View file

@ -28,7 +28,7 @@ func (w *Witness) EncodeBinary(bw *io.BinWriter) {
}
// MarshalJSON implements the json marshaller interface.
func (w *Witness) MarshalJSON() ([]byte, error) {
func (w Witness) MarshalJSON() ([]byte, error) {
data := map[string]string{
"invocation": hex.EncodeToString(w.InvocationScript),
"verification": hex.EncodeToString(w.VerificationScript),
@ -37,6 +37,20 @@ func (w *Witness) MarshalJSON() ([]byte, error) {
return json.Marshal(data)
}
// UnmarshalJSON implements json.Unmarshaler interface.
func (w *Witness) UnmarshalJSON(data []byte) error {
m := map[string]string{}
err := json.Unmarshal(data, &m)
if err != nil {
return err
}
if w.InvocationScript, err = hex.DecodeString(m["invocation"]); err != nil {
return err
}
w.VerificationScript, err = hex.DecodeString(m["verification"])
return err
}
// ScriptHash returns the hash of the VerificationScript.
func (w Witness) ScriptHash() util.Uint160 {
return hash.Hash160(w.VerificationScript)