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

     1  // Copyright (c) 2023 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package codec
     5  
     6  import (
     7  	"bytes"
     8  	"strconv"
     9  
    10  	"github.com/mavryk-network/mvgo/mavryk"
    11  )
    12  
    13  // UpdateConsensusKey represents "update_consensus_key" operation
    14  type UpdateConsensusKey struct {
    15  	Manager
    16  	Amount    mavryk.Z   `json:"amount"`
    17  	PublicKey mavryk.Key `json:"pk"`
    18  }
    19  
    20  func (o UpdateConsensusKey) Kind() mavryk.OpType {
    21  	return mavryk.OpTypeUpdateConsensusKey
    22  }
    23  
    24  func (o UpdateConsensusKey) MarshalJSON() ([]byte, error) {
    25  	buf := bytes.NewBuffer(nil)
    26  	buf.WriteByte('{')
    27  	buf.WriteString(`"kind":`)
    28  	buf.WriteString(strconv.Quote(o.Kind().String()))
    29  	buf.WriteByte(',')
    30  	o.Manager.EncodeJSON(buf)
    31  	buf.WriteString(`,"pk":`)
    32  	buf.WriteString(strconv.Quote(o.PublicKey.String()))
    33  	buf.WriteByte('}')
    34  	return buf.Bytes(), nil
    35  }
    36  
    37  func (o UpdateConsensusKey) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
    38  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
    39  	o.Manager.EncodeBuffer(buf, p)
    40  	buf.Write(o.PublicKey.Bytes())
    41  	return nil
    42  }
    43  
    44  func (o *UpdateConsensusKey) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
    45  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
    46  		return
    47  	}
    48  	if err = o.Manager.DecodeBuffer(buf, p); err != nil {
    49  		return err
    50  	}
    51  	if err = o.PublicKey.DecodeBuffer(buf); err != nil {
    52  		return
    53  	}
    54  	return
    55  }
    56  
    57  func (o UpdateConsensusKey) MarshalBinary() ([]byte, error) {
    58  	buf := bytes.NewBuffer(nil)
    59  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
    60  	return buf.Bytes(), err
    61  }
    62  
    63  func (o *UpdateConsensusKey) UnmarshalBinary(data []byte) error {
    64  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
    65  }