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.
152 lines
3.5 KiB
Go
152 lines
3.5 KiB
Go
// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package slhdsafx
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestWire_OutputOwners_RoundTrip(t *testing.T) {
|
|
pk1 := make([]byte, SLH192fPubKeyLen)
|
|
pk2 := make([]byte, SLH192fPubKeyLen)
|
|
for i := range pk1 {
|
|
pk1[i] = byte(i)
|
|
}
|
|
for i := range pk2 {
|
|
pk2[i] = byte(0xFF - i)
|
|
}
|
|
in := &OutputOwners{
|
|
Level: SecLevelSLH192f,
|
|
Locktime: 9999,
|
|
Threshold: 1,
|
|
Addrs: [][]byte{pk1, pk2},
|
|
}
|
|
got, err := WrapOutputOwners(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapOutputOwners: %v", err)
|
|
}
|
|
if got.Level != SecLevelSLH192f {
|
|
t.Errorf("Level: got %v, want %v", got.Level, SecLevelSLH192f)
|
|
}
|
|
if got.Threshold != 1 {
|
|
t.Errorf("Threshold: got %d, want 1", got.Threshold)
|
|
}
|
|
if !bytes.Equal(got.Addrs[0], pk1) {
|
|
t.Errorf("Addrs[0] mismatch")
|
|
}
|
|
}
|
|
|
|
func TestWire_TransferOutput_RoundTrip(t *testing.T) {
|
|
pk := make([]byte, SLH192fPubKeyLen)
|
|
for i := range pk {
|
|
pk[i] = byte(i)
|
|
}
|
|
in := &TransferOutput{
|
|
Amt: 1_000_000,
|
|
OutputOwners: OutputOwners{
|
|
Level: SecLevelSLH192f,
|
|
Locktime: 42,
|
|
Threshold: 1,
|
|
Addrs: [][]byte{pk},
|
|
},
|
|
}
|
|
got, err := WrapTransferOutput(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapTransferOutput: %v", err)
|
|
}
|
|
if got.Amt != in.Amt {
|
|
t.Errorf("Amt: got %d, want %d", got.Amt, in.Amt)
|
|
}
|
|
if !got.OutputOwners.Equals(&in.OutputOwners) {
|
|
t.Errorf("OutputOwners mismatch")
|
|
}
|
|
}
|
|
|
|
func TestWire_TransferInput_RoundTrip(t *testing.T) {
|
|
in := &TransferInput{
|
|
Amt: 500,
|
|
Input: Input{SigIndices: []uint32{0, 1}},
|
|
}
|
|
got, err := WrapTransferInput(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapTransferInput: %v", err)
|
|
}
|
|
if got.Amt != in.Amt {
|
|
t.Errorf("Amt: got %d, want %d", got.Amt, in.Amt)
|
|
}
|
|
if len(got.SigIndices) != 2 {
|
|
t.Fatalf("SigIndices.Len: got %d, want 2", len(got.SigIndices))
|
|
}
|
|
}
|
|
|
|
func TestWire_MintOutput_RoundTrip(t *testing.T) {
|
|
pk := make([]byte, SLH192fPubKeyLen)
|
|
for i := range pk {
|
|
pk[i] = byte(i + 7)
|
|
}
|
|
in := &MintOutput{
|
|
OutputOwners: OutputOwners{
|
|
Level: SecLevelSLH192f,
|
|
Threshold: 1,
|
|
Addrs: [][]byte{pk},
|
|
},
|
|
}
|
|
got, err := WrapMintOutput(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapMintOutput: %v", err)
|
|
}
|
|
if !got.OutputOwners.Equals(&in.OutputOwners) {
|
|
t.Errorf("OutputOwners mismatch")
|
|
}
|
|
}
|
|
|
|
func TestWire_MintOperation_RoundTrip(t *testing.T) {
|
|
pk := make([]byte, SLH192fPubKeyLen)
|
|
for i := range pk {
|
|
pk[i] = byte(i)
|
|
}
|
|
owners := OutputOwners{
|
|
Level: SecLevelSLH192f,
|
|
Threshold: 1,
|
|
Addrs: [][]byte{pk},
|
|
}
|
|
in := &MintOperation{
|
|
MintInput: Input{SigIndices: []uint32{0}},
|
|
MintOutput: MintOutput{OutputOwners: owners},
|
|
TransferOutput: TransferOutput{Amt: 100, OutputOwners: owners},
|
|
}
|
|
got, err := WrapMintOperation(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapMintOperation: %v", err)
|
|
}
|
|
if got.TransferOutput.Amt != 100 {
|
|
t.Errorf("TransferOutput.Amt: got %d, want 100", got.TransferOutput.Amt)
|
|
}
|
|
}
|
|
|
|
func TestWire_Credential_RoundTrip_192f(t *testing.T) {
|
|
sig := make([]byte, SLH192fSigLen)
|
|
for i := range sig {
|
|
sig[i] = byte(i)
|
|
}
|
|
in := &Credential{
|
|
Level: SecLevelSLH192f,
|
|
Sigs: [][]byte{sig},
|
|
}
|
|
got, err := WrapCredential(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapCredential: %v", err)
|
|
}
|
|
if got.Level != SecLevelSLH192f {
|
|
t.Errorf("Level: got %v, want %v", got.Level, SecLevelSLH192f)
|
|
}
|
|
if len(got.Sigs) != 1 {
|
|
t.Fatalf("Sigs.Len: got %d, want 1", len(got.Sigs))
|
|
}
|
|
if !bytes.Equal(got.Sigs[0], sig) {
|
|
t.Errorf("Sigs[0] mismatch")
|
|
}
|
|
}
|