neoneo-go/pkg/core/transaction/witness.go
Roman Khimov ec7e17ffa6 pkg: make use of the new crypto/hash package
Simplifies a lot of code and removes some duplication. Unfortunately I had to
move test_util random functions in same commit to avoid cycle
dependencies. One of these random functions was also used in core/transaction
testing, to simplify things I've just dropped it there and used a static
string (which is nice to have for a test anyway).

There is still sha256 left in wallet (but it needs to pass Hash structure into
the signing function).
2019-08-26 13:32:19 +03:00

63 lines
1.7 KiB
Go

package transaction
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"io"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/util"
)
// Witness contains 2 scripts.
type Witness struct {
InvocationScript []byte
VerificationScript []byte
}
// DecodeBinary implements the payload interface.
func (w *Witness) DecodeBinary(r io.Reader) error {
lenb := util.ReadVarUint(r)
w.InvocationScript = make([]byte, lenb)
if err := binary.Read(r, binary.LittleEndian, w.InvocationScript); err != nil {
return err
}
lenb = util.ReadVarUint(r)
w.VerificationScript = make([]byte, lenb)
return binary.Read(r, binary.LittleEndian, w.VerificationScript)
}
// EncodeBinary implements the payload interface.
func (w *Witness) EncodeBinary(writer io.Writer) error {
if err := util.WriteVarUint(writer, uint64(len(w.InvocationScript))); err != nil {
return err
}
if err := binary.Write(writer, binary.LittleEndian, w.InvocationScript); err != nil {
return err
}
if err := util.WriteVarUint(writer, uint64(len(w.VerificationScript))); err != nil {
return err
}
return binary.Write(writer, binary.LittleEndian, w.VerificationScript)
}
// MarshalJSON implements the json marshaller interface.
func (w *Witness) MarshalJSON() ([]byte, error) {
data := map[string]string{
"invocation": hex.EncodeToString(w.InvocationScript),
"verification": hex.EncodeToString(w.VerificationScript),
}
return json.Marshal(data)
}
// Size returns the size in bytes of the Witness.
func (w *Witness) Size() int {
return util.GetVarSize(w.InvocationScript) + util.GetVarSize(w.VerificationScript)
}
// ScriptHash returns the hash of the VerificationScript.
func (w Witness) ScriptHash() util.Uint160 {
return hash.Hash160(w.VerificationScript)
}