forked from TrueCloudLab/neoneo-go
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:
parent
33f99104e8
commit
634e9483d3
2 changed files with 19 additions and 1 deletions
|
@ -185,6 +185,10 @@ func TestMarshalUnmarshalJSON(t *testing.T) {
|
||||||
ScriptHash: util.Uint160{7, 8, 9, 10},
|
ScriptHash: util.Uint160{7, 8, 9, 10},
|
||||||
Position: 13,
|
Position: 13,
|
||||||
}}
|
}}
|
||||||
|
tx.Scripts = []Witness{{
|
||||||
|
InvocationScript: []byte{5, 3, 1},
|
||||||
|
VerificationScript: []byte{2, 4, 6},
|
||||||
|
}}
|
||||||
data, err := json.Marshal(tx)
|
data, err := json.Marshal(tx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ func (w *Witness) EncodeBinary(bw *io.BinWriter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json marshaller interface.
|
// MarshalJSON implements the json marshaller interface.
|
||||||
func (w *Witness) MarshalJSON() ([]byte, error) {
|
func (w Witness) MarshalJSON() ([]byte, error) {
|
||||||
data := map[string]string{
|
data := map[string]string{
|
||||||
"invocation": hex.EncodeToString(w.InvocationScript),
|
"invocation": hex.EncodeToString(w.InvocationScript),
|
||||||
"verification": hex.EncodeToString(w.VerificationScript),
|
"verification": hex.EncodeToString(w.VerificationScript),
|
||||||
|
@ -37,6 +37,20 @@ func (w *Witness) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(data)
|
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.
|
// ScriptHash returns the hash of the VerificationScript.
|
||||||
func (w Witness) ScriptHash() util.Uint160 {
|
func (w Witness) ScriptHash() util.Uint160 {
|
||||||
return hash.Hash160(w.VerificationScript)
|
return hash.Hash160(w.VerificationScript)
|
||||||
|
|
Loading…
Reference in a new issue