2020-06-24 13:47:57 +00:00
|
|
|
package nef
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2023-11-02 11:01:36 +00:00
|
|
|
"fmt"
|
2020-06-24 13:47:57 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2023-11-02 11:01:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-06-24 13:47:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NEO Executable Format 3 (NEF3)
|
|
|
|
// Standard: https://github.com/neo-project/proposals/pull/121/files
|
|
|
|
// Implementation: https://github.com/neo-project/neo/blob/v3.0.0-preview2/src/neo/SmartContract/NefFile.cs#L8
|
|
|
|
// +------------+-----------+------------------------------------------------------------+
|
|
|
|
// | Field | Length | Comment |
|
|
|
|
// +------------+-----------+------------------------------------------------------------+
|
|
|
|
// | Magic | 4 bytes | Magic header |
|
2021-01-19 08:08:34 +00:00
|
|
|
// | Compiler | 64 bytes | Compiler used and it's version |
|
2021-09-23 21:19:37 +00:00
|
|
|
// | Source | Var bytes | Source file URL. |
|
2020-06-24 13:47:57 +00:00
|
|
|
// +------------+-----------+------------------------------------------------------------+
|
2021-09-23 21:19:37 +00:00
|
|
|
// | Reserved | 1 byte | Reserved for extensions. Must be 0. |
|
2021-01-18 14:05:11 +00:00
|
|
|
// | Tokens | Var array | List of method tokens |
|
2021-01-19 07:55:34 +00:00
|
|
|
// | Reserved | 2-bytes | Reserved for extensions. Must be 0. |
|
2020-06-24 13:47:57 +00:00
|
|
|
// | Script | Var bytes | Var bytes for the payload |
|
|
|
|
// +------------+-----------+------------------------------------------------------------+
|
2020-11-27 16:33:36 +00:00
|
|
|
// | Checksum | 4 bytes | First four bytes of double SHA256 hash of the header |
|
|
|
|
// +------------+-----------+------------------------------------------------------------+
|
2020-06-24 13:47:57 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
// Magic is a magic File header constant.
|
|
|
|
Magic uint32 = 0x3346454E
|
2021-09-23 21:19:37 +00:00
|
|
|
// MaxSourceURLLength is the maximum allowed source URL length.
|
|
|
|
MaxSourceURLLength = 256
|
2021-01-19 08:08:34 +00:00
|
|
|
// compilerFieldSize is the length of `Compiler` File header field in bytes.
|
|
|
|
compilerFieldSize = 64
|
2020-06-24 13:47:57 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// File represents a compiled contract file structure according to the NEF3 standard.
|
2020-06-24 13:47:57 +00:00
|
|
|
type File struct {
|
2021-01-13 12:26:35 +00:00
|
|
|
Header
|
2021-09-23 21:19:37 +00:00
|
|
|
Source string `json:"source"`
|
2021-01-18 14:05:11 +00:00
|
|
|
Tokens []MethodToken `json:"tokens"`
|
|
|
|
Script []byte `json:"script"`
|
|
|
|
Checksum uint32 `json:"checksum"`
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Header represents a File header.
|
2020-06-24 13:47:57 +00:00
|
|
|
type Header struct {
|
2021-01-13 12:26:35 +00:00
|
|
|
Magic uint32 `json:"magic"`
|
|
|
|
Compiler string `json:"compiler"`
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewFile returns a new NEF3 file with the script specified.
|
2020-11-27 18:42:54 +00:00
|
|
|
func NewFile(script []byte) (*File, error) {
|
|
|
|
file := &File{
|
2020-06-24 13:47:57 +00:00
|
|
|
Header: Header{
|
2020-11-26 18:31:24 +00:00
|
|
|
Magic: Magic,
|
2021-01-19 08:08:34 +00:00
|
|
|
Compiler: "neo-go-" + config.Version,
|
2020-06-24 13:47:57 +00:00
|
|
|
},
|
2021-01-18 14:05:11 +00:00
|
|
|
Tokens: []MethodToken{},
|
2020-06-24 13:47:57 +00:00
|
|
|
Script: script,
|
|
|
|
}
|
2021-01-19 08:08:34 +00:00
|
|
|
if len(file.Compiler) > compilerFieldSize {
|
|
|
|
return nil, errors.New("too long compiler field")
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
2020-11-27 16:33:36 +00:00
|
|
|
file.Checksum = file.CalculateChecksum()
|
2020-06-24 13:47:57 +00:00
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeBinary implements the io.Serializable interface.
|
2020-06-24 13:47:57 +00:00
|
|
|
func (h *Header) EncodeBinary(w *io.BinWriter) {
|
|
|
|
w.WriteU32LE(h.Magic)
|
|
|
|
if len(h.Compiler) > compilerFieldSize {
|
|
|
|
w.Err = errors.New("invalid compiler name length")
|
|
|
|
return
|
|
|
|
}
|
2020-11-27 18:42:54 +00:00
|
|
|
var b = make([]byte, compilerFieldSize)
|
|
|
|
copy(b, []byte(h.Compiler))
|
|
|
|
w.WriteBytes(b)
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinary implements the io.Serializable interface.
|
2020-06-24 13:47:57 +00:00
|
|
|
func (h *Header) DecodeBinary(r *io.BinReader) {
|
|
|
|
h.Magic = r.ReadU32LE()
|
|
|
|
if h.Magic != Magic {
|
|
|
|
r.Err = errors.New("invalid Magic")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf := make([]byte, compilerFieldSize)
|
|
|
|
r.ReadBytes(buf)
|
|
|
|
buf = bytes.TrimRightFunc(buf, func(r rune) bool {
|
|
|
|
return r == 0
|
|
|
|
})
|
|
|
|
h.Compiler = string(buf)
|
|
|
|
}
|
|
|
|
|
2020-08-03 11:25:27 +00:00
|
|
|
// CalculateChecksum returns first 4 bytes of double-SHA256(Header) converted to uint32.
|
2023-11-02 11:48:51 +00:00
|
|
|
// CalculateChecksum doesn't perform the resulting serialized NEF size check, and return
|
|
|
|
// the checksum irrespectively to the size limit constraint. It's a caller's duty to check
|
|
|
|
// the resulting NEF size.
|
2020-11-27 16:33:36 +00:00
|
|
|
func (n *File) CalculateChecksum() uint32 {
|
2023-11-02 11:48:51 +00:00
|
|
|
bb, err := n.BytesLong()
|
2020-11-27 16:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
2020-11-27 16:33:36 +00:00
|
|
|
return binary.LittleEndian.Uint32(hash.Checksum(bb[:len(bb)-4]))
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeBinary implements the io.Serializable interface.
|
2020-06-24 13:47:57 +00:00
|
|
|
func (n *File) EncodeBinary(w *io.BinWriter) {
|
|
|
|
n.Header.EncodeBinary(w)
|
2021-09-23 21:19:37 +00:00
|
|
|
if len(n.Source) > MaxSourceURLLength {
|
|
|
|
w.Err = errors.New("source url too long")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteString(n.Source)
|
|
|
|
w.WriteB(0)
|
2021-01-18 14:05:11 +00:00
|
|
|
w.WriteArray(n.Tokens)
|
2021-01-19 07:55:34 +00:00
|
|
|
w.WriteU16LE(0)
|
2020-06-24 13:47:57 +00:00
|
|
|
w.WriteVarBytes(n.Script)
|
2020-11-27 16:33:36 +00:00
|
|
|
w.WriteU32LE(n.Checksum)
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 07:55:34 +00:00
|
|
|
var errInvalidReserved = errors.New("reserved bytes must be 0")
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinary implements the io.Serializable interface.
|
2020-06-24 13:47:57 +00:00
|
|
|
func (n *File) DecodeBinary(r *io.BinReader) {
|
|
|
|
n.Header.DecodeBinary(r)
|
2021-09-23 21:19:37 +00:00
|
|
|
n.Source = r.ReadString(MaxSourceURLLength)
|
|
|
|
reservedB := r.ReadB()
|
|
|
|
if r.Err == nil && reservedB != 0 {
|
2021-01-19 07:55:34 +00:00
|
|
|
r.Err = errInvalidReserved
|
|
|
|
return
|
|
|
|
}
|
2021-01-18 14:05:11 +00:00
|
|
|
r.ReadArray(&n.Tokens)
|
2021-09-23 21:19:37 +00:00
|
|
|
reserved := r.ReadU16LE()
|
2021-01-19 07:55:34 +00:00
|
|
|
if r.Err == nil && reserved != 0 {
|
|
|
|
r.Err = errInvalidReserved
|
|
|
|
return
|
|
|
|
}
|
2023-11-02 11:01:36 +00:00
|
|
|
n.Script = r.ReadVarBytes(stackitem.MaxSize)
|
2021-01-18 14:05:11 +00:00
|
|
|
if r.Err == nil && len(n.Script) == 0 {
|
2020-11-18 09:57:44 +00:00
|
|
|
r.Err = errors.New("empty script")
|
|
|
|
return
|
|
|
|
}
|
2020-11-27 16:33:36 +00:00
|
|
|
n.Checksum = r.ReadU32LE()
|
|
|
|
checksum := n.CalculateChecksum()
|
2021-01-18 14:05:11 +00:00
|
|
|
if r.Err == nil && checksum != n.Checksum {
|
2020-11-27 16:33:36 +00:00
|
|
|
r.Err = errors.New("checksum verification failure")
|
|
|
|
return
|
|
|
|
}
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 11:48:51 +00:00
|
|
|
// Bytes returns a byte array with a serialized NEF File. It performs the
|
|
|
|
// resulting NEF file size check and returns an error if serialized slice length
|
|
|
|
// exceeds [stackitem.MaxSize].
|
2020-06-24 13:47:57 +00:00
|
|
|
func (n File) Bytes() ([]byte, error) {
|
2023-11-02 11:48:51 +00:00
|
|
|
return n.bytes(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BytesLong returns a byte array with a serialized NEF File. It performs no
|
|
|
|
// resulting slice check.
|
|
|
|
func (n File) BytesLong() ([]byte, error) {
|
|
|
|
return n.bytes(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bytes returns the serialized NEF File representation and performs the resulting
|
|
|
|
// byte array size check if needed.
|
|
|
|
func (n File) bytes(checkSize bool) ([]byte, error) {
|
2020-06-24 13:47:57 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
n.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return nil, buf.Err
|
|
|
|
}
|
2023-11-02 11:48:51 +00:00
|
|
|
res := buf.Bytes()
|
|
|
|
if checkSize && len(res) > stackitem.MaxSize {
|
|
|
|
return nil, fmt.Errorf("serialized NEF size exceeds VM stackitem limits: %d bytes is allowed at max, got %d", stackitem.MaxSize, len(res))
|
|
|
|
}
|
|
|
|
return res, nil
|
2020-06-24 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// FileFromBytes returns a NEF File deserialized from the given bytes.
|
2020-06-24 13:47:57 +00:00
|
|
|
func FileFromBytes(source []byte) (File, error) {
|
|
|
|
result := File{}
|
2023-11-02 11:01:36 +00:00
|
|
|
if len(source) > stackitem.MaxSize {
|
|
|
|
return result, fmt.Errorf("invalid NEF file size: expected %d at max, got %d", stackitem.MaxSize, len(source))
|
|
|
|
}
|
2020-06-24 13:47:57 +00:00
|
|
|
r := io.NewBinReaderFromBuf(source)
|
|
|
|
result.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return result, r.Err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|