From 634e9483d31c949a67e3d279ee4a2495acb8c55c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 3 Mar 2020 11:29:07 +0300 Subject: [PATCH] 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. --- pkg/core/transaction/transaction_test.go | 4 ++++ pkg/core/transaction/witness.go | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/core/transaction/transaction_test.go b/pkg/core/transaction/transaction_test.go index 8af560d66..b98d07ab0 100644 --- a/pkg/core/transaction/transaction_test.go +++ b/pkg/core/transaction/transaction_test.go @@ -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) diff --git a/pkg/core/transaction/witness.go b/pkg/core/transaction/witness.go index 501663cd8..2c90fa8a7 100644 --- a/pkg/core/transaction/witness.go +++ b/pkg/core/transaction/witness.go @@ -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)