github.com/mavryk-network/mvgo@v1.19.9/codec/failing_noop.go (about)

     1  // Copyright (c) 2020-2022 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package codec
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/binary"
     9  	"strconv"
    10  
    11  	"github.com/mavryk-network/mvgo/mavryk"
    12  )
    13  
    14  // FailingNoop represents "failing_noop" operations. Used for signing arbitrary messages
    15  // and guaranteed to be not included on-chain. This prevents an attack vector where a
    16  // message is crafted which looks like a regular transaction.
    17  type FailingNoop struct {
    18  	Simple
    19  	Arbitrary string `json:"arbitrary"`
    20  }
    21  
    22  func (o FailingNoop) Kind() mavryk.OpType {
    23  	return mavryk.OpTypeFailingNoop
    24  }
    25  
    26  func (o FailingNoop) MarshalJSON() ([]byte, error) {
    27  	buf := bytes.NewBuffer(nil)
    28  	buf.WriteByte('{')
    29  	buf.WriteString(`"kind":`)
    30  	buf.WriteString(strconv.Quote(o.Kind().String()))
    31  	buf.WriteString(`,"arbitrary":`)
    32  	buf.WriteString(strconv.Quote(o.Arbitrary))
    33  	buf.WriteByte('}')
    34  	return buf.Bytes(), nil
    35  }
    36  
    37  func (o FailingNoop) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
    38  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
    39  	// convert utf8 string to bytes
    40  	val := []byte(o.Arbitrary)
    41  	binary.Write(buf, enc, uint32(len(val)))
    42  	buf.Write(val)
    43  	return nil
    44  }
    45  
    46  func (o *FailingNoop) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
    47  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
    48  		return
    49  	}
    50  	l, err := readUint32(buf.Next(4))
    51  	if err != nil {
    52  		return err
    53  	}
    54  	val := make([]byte, l)
    55  	copy(val, buf.Next(int(l)))
    56  	o.Arbitrary = string(val)
    57  	return nil
    58  }
    59  
    60  func (o FailingNoop) MarshalBinary() ([]byte, error) {
    61  	buf := bytes.NewBuffer(nil)
    62  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
    63  	return buf.Bytes(), err
    64  }
    65  
    66  func (o *FailingNoop) UnmarshalBinary(data []byte) error {
    67  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
    68  }