Files
zeekay 45df2649d9 feat: ZAP-native wire schemas — activation 2025-12-25T16:20:00-08: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.
2026-06-03 09:34:23 -07:00

146 lines
3.7 KiB
Go

// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package wire
import (
"bytes"
"testing"
)
func TestPQOutputOwners_RoundTrip(t *testing.T) {
// Two ML-DSA-65 pubkeys (1952 bytes each), simulated.
const stride = 1952
pk1 := make([]byte, stride)
pk2 := make([]byte, stride)
for i := range pk1 {
pk1[i] = byte(i)
}
for i := range pk2 {
pk2[i] = byte(0xFF - i)
}
in := PQOutputOwnersInput{
TypeKind: TypeKindMLDSA,
SecurityLevel: 1, // ML-DSA-65
Locktime: 9999,
Threshold: 1,
PubKeyStride: stride,
PubKeys: [][]byte{pk1, pk2},
}
envelope := NewPQOutputOwners(in)
got, err := WrapPQOutputOwners(envelope, stride)
if err != nil {
t.Fatalf("WrapPQOutputOwners: %v", err)
}
if got.TypeKind() != TypeKindMLDSA {
t.Errorf("TypeKind: got %x, want %x", got.TypeKind(), TypeKindMLDSA)
}
if got.SecurityLevel() != 1 {
t.Errorf("SecurityLevel: got %d, want 1", got.SecurityLevel())
}
if got.Locktime() != 9999 {
t.Errorf("Locktime: got %d, want 9999", got.Locktime())
}
if got.Threshold() != 1 {
t.Errorf("Threshold: got %d, want 1", got.Threshold())
}
pks := got.PubKeys()
if pks.Len() != 2 {
t.Fatalf("PubKeys.Len: got %d, want 2", pks.Len())
}
if !bytes.Equal(pks.At(0), pk1) {
t.Errorf("PubKeys[0] mismatch")
}
if !bytes.Equal(pks.At(1), pk2) {
t.Errorf("PubKeys[1] mismatch")
}
// pk1 < pk2 lexicographically (bytes 0x00..ff < 0xFF..00 starting at byte 0).
if err := got.SyntacticVerify(); err != nil {
t.Errorf("SyntacticVerify: %v", err)
}
}
func TestPQTransferOutput_RoundTrip(t *testing.T) {
// Single SLH-DSA-SHA2-192f pubkey (48 bytes), simulated.
const stride = 48
pk := make([]byte, stride)
for i := range pk {
pk[i] = byte(i + 10)
}
in := PQTransferOutputInput{
TypeKind: TypeKindSLHDSA,
SecurityLevel: 1,
Amount: 7_777_777,
Locktime: 42,
Threshold: 1,
PubKeyStride: stride,
PubKeys: [][]byte{pk},
}
envelope := NewPQTransferOutput(in)
got, err := WrapPQTransferOutput(envelope, stride)
if err != nil {
t.Fatalf("WrapPQTransferOutput: %v", err)
}
if got.Amount() != 7_777_777 {
t.Errorf("Amount: got %d, want 7_777_777", got.Amount())
}
if got.Threshold() != 1 {
t.Errorf("Threshold: got %d, want 1", got.Threshold())
}
pks := got.PubKeys()
if pks.Len() != 1 {
t.Fatalf("PubKeys.Len: got %d, want 1", pks.Len())
}
if !bytes.Equal(pks.At(0), pk) {
t.Errorf("PubKeys[0] mismatch")
}
if err := got.SyntacticVerify(); err != nil {
t.Errorf("SyntacticVerify: %v", err)
}
}
func TestPQMintOutput_RoundTrip(t *testing.T) {
const stride = 1952
pk := make([]byte, stride)
for i := range pk {
pk[i] = byte(i * 3)
}
in := PQMintOutputInput{
TypeKind: TypeKindMLDSA,
SecurityLevel: 1,
Locktime: 0,
Threshold: 1,
PubKeyStride: stride,
PubKeys: [][]byte{pk},
}
envelope := NewPQMintOutput(in)
got, err := WrapPQMintOutput(envelope, stride)
if err != nil {
t.Fatalf("WrapPQMintOutput: %v", err)
}
if got.SecurityLevel() != 1 {
t.Errorf("SecurityLevel: got %d, want 1", got.SecurityLevel())
}
if got.Threshold() != 1 {
t.Errorf("Threshold: got %d, want 1", got.Threshold())
}
if !bytes.Equal(got.PubKeys().At(0), pk) {
t.Errorf("PubKeys[0] mismatch")
}
if err := got.SyntacticVerify(); err != nil {
t.Errorf("SyntacticVerify: %v", err)
}
}
func TestPQOutputOwners_RejectsWrongShape(t *testing.T) {
// Build a classical OutputOwners and feed it to the PQ Wrap. Should fail
// the ShapeKind check.
classical := NewOutputOwners(OutputOwnersInput{
Locktime: 0,
Threshold: 1,
})
if _, err := WrapPQOutputOwners(classical, 1952); err != ErrWrongShapeKind {
t.Errorf("WrapPQOutputOwners(classical): got err=%v, want ErrWrongShapeKind", err)
}
}