Files
utxo/utxo_wire.go
zeekayandHanzo Dev 6efa1285f9 lint: clean all 21 staticcheck findings — CI green, drop last codec reference
- SA1019: math.Mul64/Add64 -> generic math.Mul/Add (ed25519fx, mldsafx,
  schnorrfx inputs; flow_checker; utxo_fetching); prefixdb.NewNested -> New (alias)
- unused: DELETE dead codecVersion const (last codec reference in utxo — full
  ZAP native now), isSortedAndUniqueOrdered (root + secp256k1fx), mldsafx
  Keychain.get, and all 6 redundant TransferOutput.isState() markers (the
  embedded verify.IsState already provides State membership)
- QF1008: drop embedded-field selectors (utxo_wire, ed25519fx, nftfx,
  propertyfx + tests); ST1005: lowercase Schnorr error string

Behavior-identical; 11/11 packages pass.

Co-authored-by: Hanzo Dev <dev@hanzo.ai>
2026-07-12 22:32:45 -07:00

70 lines
2.5 KiB
Go

// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package utxo
import (
"errors"
"github.com/luxfi/utxo/wire"
)
// ErrUTXOOutNotWireSerializable is returned by UTXO.WireBytes when the
// in-memory polymorphic Out field is not a known fxs primitive with a
// wire.NewXxx adapter. Each fx package's wire.go adapter file registers
// its concrete types to satisfy the wireSerializable interface — if a
// caller stuffs a third-party type into UTXO.Out, the wire layer cannot
// build a deterministic envelope and refuses.
var ErrUTXOOutNotWireSerializable = errors.New("utxo: UTXO.Out type does not implement wire-serializable interface; add Bytes() []byte to the fx primitive")
// wireSerializable is the minimal contract every fxs primitive's wire
// adapter must satisfy: a Bytes() returning the (TypeKind+ShapeKind+
// ZAP-message) envelope. All fx wire.go files satisfy this for their
// TransferOutput / MintOutput / AttestationOutput types.
type wireSerializable interface {
Bytes() []byte
}
// WireBytes returns the ZAP-native wire envelope for the UTXO. The
// envelope is = (TypeKindReserved, ShapeKindUTXO, ZAP message) where the
// ZAP message carries TxID, OutputIndex, AssetID, and the inner output's
// wire envelope (which itself carries its own TypeKind+ShapeKind+ZAP
// message).
//
// Returns ErrUTXOOutNotWireSerializable when the in-memory Out is not a
// known fxs primitive. The fx packages provide the adapter side via
// their `wire.go` Bytes() methods.
func (u *UTXO) WireBytes() ([]byte, error) {
if u == nil {
return nil, errNilUTXO
}
if u.Out == nil {
return nil, errEmptyUTXO
}
ws, ok := u.Out.(wireSerializable)
if !ok {
return nil, ErrUTXOOutNotWireSerializable
}
outputBytes := ws.Bytes()
return wire.NewUTXO(wire.UTXOInput{
TxID: u.TxID,
OutputIndex: u.OutputIndex,
AssetID: u.ID,
Output: outputBytes,
}), nil
}
// WrapUTXOBytes parses the outer UTXO wire envelope into the (TxID,
// OutputIndex, AssetID, Output-envelope-bytes) tuple. The caller is
// responsible for parsing the Output envelope through the appropriate
// fx package's WrapTransferOutput / WrapMintOutput / WrapAttestationOutput
// dispatched on the inner discriminator pair.
//
// This split is intentional: the root utxo package cannot import the fx
// packages (would be a cycle), so the inner envelope parse stays at the
// caller. Use wire.UTXO.OutputDiscriminator() to learn (TypeKind,
// ShapeKind) before dispatch.
func WrapUTXOBytes(b []byte) (wire.UTXO, error) {
return wire.WrapUTXO(b)
}