mirror of
https://github.com/luxfi/utxo.git
synced 2026-10-30 15:13:57 +00:00
Adds github.com/luxfi/utxo/wire package + per-fx wire.go adapters so
that luxfi/node's vms/{platformvm,xvm}/txs/codec.go can be deleted.
wire/ package mirrors luxfi/node/vms/wire — every fxs primitive carries
a (TypeKind, ShapeKind) discriminator pair on its wire envelope, then
a ZAP message describing its fields. Decomposes the legacy
codec.Manager slot map (which braided "fx family" and "primitive shape"
into a single dense uint32 slot id) into two orthogonal 1-byte tags.
PQ-specific schemas (pq_output_owners, pq_transfer_output,
pq_mint_output) handle the mldsafx/slhdsafx case where addresses are
full PQ pubkeys (1312-2592 bytes) rather than 20-byte hashed ShortIDs.
The wire layer is stride-agnostic — the per-fx wire.go resolves
SecurityLevel → pubkey stride before parsing.
Per-fx wire.go adapters provide (value T).Bytes() []byte (Go type →
wire envelope) and Wrap*(b []byte) (*T, error) (wire envelope → Go
type). The Initialize(vm)/codec.Manager.RegisterType chain stays in
place pending downstream coordination; this commit gets the wire/
package + adapters in so consumer agents (platformvm/warp,
platformvm/state, xvm) can pin a SHA and start their migrations.
UTXO.WireBytes() builds the outer envelope by calling the polymorphic
Out's Bytes() (any fxs primitive implementing wireSerializable).
WrapUTXOBytes(b) parses the outer envelope; the inner output is
dispatched by the caller via OutputDiscriminator() → fx-package
WrapTransferOutput / WrapMintOutput / WrapAttestationOutput.
Tests: 35 round-trip tests across 9 packages. go vet clean. go test
-race clean. gofmt clean.
Activation: 2025-12-25T16:20:00-08:00 (unix 1766708400). No
backwards compat — pre-activation legacy codec blobs are a
protocol-violation reject after activation.
70 lines
2.6 KiB
Go
70 lines
2.6 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.Asset.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)
|
|
}
|