github.com/MetalBlockchain/metalgo@v1.11.9/vms/nftfx/transfer_output.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package nftfx 5 6 import ( 7 "encoding/json" 8 "errors" 9 10 "github.com/MetalBlockchain/metalgo/utils/units" 11 "github.com/MetalBlockchain/metalgo/vms/components/verify" 12 "github.com/MetalBlockchain/metalgo/vms/secp256k1fx" 13 "github.com/MetalBlockchain/metalgo/vms/types" 14 ) 15 16 const ( 17 // MaxPayloadSize is the maximum size that can be placed into a payload 18 MaxPayloadSize = units.KiB 19 ) 20 21 var ( 22 errNilTransferOutput = errors.New("nil transfer output") 23 errPayloadTooLarge = errors.New("payload too large") 24 _ verify.State = (*TransferOutput)(nil) 25 ) 26 27 type TransferOutput struct { 28 verify.IsState `json:"-"` 29 30 GroupID uint32 `serialize:"true" json:"groupID"` 31 Payload types.JSONByteSlice `serialize:"true" json:"payload"` 32 secp256k1fx.OutputOwners `serialize:"true"` 33 } 34 35 // MarshalJSON marshals Amt and the embedded OutputOwners struct 36 // into a JSON readable format 37 // If OutputOwners cannot be serialized then this will return error 38 func (out *TransferOutput) MarshalJSON() ([]byte, error) { 39 result, err := out.OutputOwners.Fields() 40 if err != nil { 41 return nil, err 42 } 43 44 result["groupID"] = out.GroupID 45 result["payload"] = out.Payload 46 return json.Marshal(result) 47 } 48 49 func (out *TransferOutput) Verify() error { 50 switch { 51 case out == nil: 52 return errNilTransferOutput 53 case len(out.Payload) > MaxPayloadSize: 54 return errPayloadTooLarge 55 default: 56 return out.OutputOwners.Verify() 57 } 58 }