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

98 lines
2.7 KiB
Go

// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package wire
import (
"bytes"
"testing"
"github.com/luxfi/crypto/hash"
"github.com/luxfi/crypto/secp256k1"
)
func TestSignSecp256k1_RoundTrip(t *testing.T) {
// Generate a couple of test keys.
k1, err := secp256k1.NewPrivateKey()
if err != nil {
t.Fatalf("NewPrivateKey: %v", err)
}
k2, err := secp256k1.NewPrivateKey()
if err != nil {
t.Fatalf("NewPrivateKey: %v", err)
}
// Pretend we have an unsigned tx blob (in production this would be a
// zap_native-encoded UnsignedTx with a TxKind discriminator at offset 0).
unsigned := bytes.Repeat([]byte("UNSIGNED-TX-BYTES "), 16)
// Two inputs: input 0 needs (k1, k2) co-signers; input 1 needs k2 alone.
signers := [][]*secp256k1.PrivateKey{
{k1, k2},
{k2},
}
signedTxBytes, err := SignSecp256k1(unsigned, signers)
if err != nil {
t.Fatalf("SignSecp256k1: %v", err)
}
got, err := WrapSignedTx(signedTxBytes)
if err != nil {
t.Fatalf("WrapSignedTx: %v", err)
}
if !bytes.Equal(got.UnsignedBytes(), unsigned) {
t.Fatalf("UnsignedBytes mismatch")
}
if got.CredentialCount() != 2 {
t.Fatalf("CredentialCount: got %d, want 2", got.CredentialCount())
}
all, err := got.AllCredentials()
if err != nil {
t.Fatalf("AllCredentials: %v", err)
}
if len(all) != 2 {
t.Fatalf("AllCredentials len: got %d, want 2", len(all))
}
// Validate the signatures recover the right pubkey.
txHash := hash.ComputeHash256(unsigned)
// Input 0: 2 sigs (k1, k2).
cred0 := all[0]
if cred0.SignatureCount(secp256k1.SignatureLen) != 2 {
t.Fatalf("cred0 sig count: got %d, want 2", cred0.SignatureCount(secp256k1.SignatureLen))
}
sig0_0 := cred0.SignatureAt(0, secp256k1.SignatureLen)
pk0_0, err := secp256k1.RecoverPublicKeyFromHash(txHash, sig0_0)
if err != nil {
t.Fatalf("recover sig0_0: %v", err)
}
if !bytes.Equal(pk0_0.Bytes(), k1.PublicKey().Bytes()) {
t.Errorf("sig0_0 pubkey mismatch")
}
sig0_1 := cred0.SignatureAt(1, secp256k1.SignatureLen)
pk0_1, err := secp256k1.RecoverPublicKeyFromHash(txHash, sig0_1)
if err != nil {
t.Fatalf("recover sig0_1: %v", err)
}
if !bytes.Equal(pk0_1.Bytes(), k2.PublicKey().Bytes()) {
t.Errorf("sig0_1 pubkey mismatch")
}
// Input 1: 1 sig (k2).
cred1 := all[1]
if cred1.SignatureCount(secp256k1.SignatureLen) != 1 {
t.Fatalf("cred1 sig count: got %d, want 1", cred1.SignatureCount(secp256k1.SignatureLen))
}
sig1_0 := cred1.SignatureAt(0, secp256k1.SignatureLen)
pk1_0, err := secp256k1.RecoverPublicKeyFromHash(txHash, sig1_0)
if err != nil {
t.Fatalf("recover sig1_0: %v", err)
}
if !bytes.Equal(pk1_0.Bytes(), k2.PublicKey().Bytes()) {
t.Errorf("sig1_0 pubkey mismatch")
}
}