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

     1  // Copyright (c) 2024 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  // DalPublishCommitment represents "Dal_publish_commitment" operation
    15  type DalPublishCommitment struct {
    16  	Manager
    17  	Level      int32           `json:"level"`
    18  	Index      byte            `json:"index"`
    19  	Commitment mavryk.HexBytes `json:"commitment"`
    20  	Proof      mavryk.HexBytes `json:"commitment_proof"`
    21  }
    22  
    23  func (o DalPublishCommitment) Kind() mavryk.OpType {
    24  	return mavryk.OpTypeDalPublishCommitment
    25  }
    26  
    27  func (o DalPublishCommitment) MarshalJSON() ([]byte, error) {
    28  	buf := bytes.NewBuffer(nil)
    29  	buf.WriteByte('{')
    30  	buf.WriteString(`"kind":`)
    31  	buf.WriteString(strconv.Quote(o.Kind().String()))
    32  	buf.WriteByte(',')
    33  	o.Manager.EncodeJSON(buf)
    34  	buf.WriteString(`,"slot_header":{`)
    35  	buf.WriteString(`"level":`)
    36  	buf.WriteString(strconv.Itoa(int(o.Level)))
    37  	buf.WriteString(`,"index":`)
    38  	buf.WriteString(strconv.Itoa(int(o.Index)))
    39  	buf.WriteString(`,"commitment":`)
    40  	buf.WriteString(strconv.Quote(o.Commitment.String()))
    41  	buf.WriteString(`,"commitment_proof":`)
    42  	buf.WriteString(strconv.Quote(o.Proof.String()))
    43  	buf.WriteString("}}")
    44  	return buf.Bytes(), nil
    45  }
    46  
    47  func (o DalPublishCommitment) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
    48  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
    49  	o.Manager.EncodeBuffer(buf, p)
    50  	binary.Write(buf, enc, o.Level)
    51  	binary.Write(buf, enc, o.Index)
    52  	buf.Write(o.Commitment.Bytes())
    53  	buf.Write(o.Proof.Bytes())
    54  	return nil
    55  }
    56  
    57  func (o *DalPublishCommitment) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
    58  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
    59  		return
    60  	}
    61  	if err = o.Manager.DecodeBuffer(buf, p); err != nil {
    62  		return
    63  	}
    64  	if o.Level, err = readInt32(buf.Next(4)); err != nil {
    65  		return
    66  	}
    67  	if o.Index, err = readByte(buf.Next(1)); err != nil {
    68  		return
    69  	}
    70  	if err = o.Commitment.ReadBytes(buf, 48); err != nil {
    71  		return
    72  	}
    73  	if err = o.Proof.ReadBytes(buf, 48); err != nil {
    74  		return
    75  	}
    76  	return
    77  }
    78  
    79  func (o DalPublishCommitment) MarshalBinary() ([]byte, error) {
    80  	buf := bytes.NewBuffer(nil)
    81  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
    82  	return buf.Bytes(), err
    83  }
    84  
    85  func (o *DalPublishCommitment) UnmarshalBinary(data []byte) error {
    86  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
    87  }