github.com/MetalBlockchain/metalgo@v1.11.9/vms/example/xsvm/tx/payload.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package tx
     5  
     6  import "github.com/MetalBlockchain/metalgo/ids"
     7  
     8  type Payload struct {
     9  	// Sender + Nonce provides replay protection
    10  	Sender   ids.ShortID `serialize:"true" json:"sender"`
    11  	Nonce    uint64      `serialize:"true" json:"nonce"`
    12  	IsReturn bool        `serialize:"true" json:"isReturn"`
    13  	Amount   uint64      `serialize:"true" json:"amount"`
    14  	To       ids.ShortID `serialize:"true" json:"to"`
    15  
    16  	bytes []byte
    17  }
    18  
    19  func (p *Payload) Bytes() []byte {
    20  	return p.bytes
    21  }
    22  
    23  func NewPayload(
    24  	sender ids.ShortID,
    25  	nonce uint64,
    26  	isReturn bool,
    27  	amount uint64,
    28  	to ids.ShortID,
    29  ) (*Payload, error) {
    30  	p := &Payload{
    31  		Sender:   sender,
    32  		Nonce:    nonce,
    33  		IsReturn: isReturn,
    34  		Amount:   amount,
    35  		To:       to,
    36  	}
    37  	bytes, err := Codec.Marshal(CodecVersion, p)
    38  	p.bytes = bytes
    39  	return p, err
    40  }
    41  
    42  func ParsePayload(bytes []byte) (*Payload, error) {
    43  	p := &Payload{
    44  		bytes: bytes,
    45  	}
    46  	_, err := Codec.Unmarshal(bytes, p)
    47  	return p, err
    48  }