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.
150 lines
4.0 KiB
Go
150 lines
4.0 KiB
Go
// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package secp256k1fx
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/luxfi/crypto/secp256k1"
|
|
"github.com/luxfi/ids"
|
|
)
|
|
|
|
func TestWire_OutputOwners_RoundTrip(t *testing.T) {
|
|
in := &OutputOwners{
|
|
Locktime: 1234567,
|
|
Threshold: 2,
|
|
Addrs: []ids.ShortID{
|
|
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
|
|
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
|
|
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
|
|
},
|
|
}
|
|
got, err := WrapOutputOwners(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapOutputOwners: %v", err)
|
|
}
|
|
if !in.Equals(got) {
|
|
t.Errorf("OutputOwners mismatch: got %+v, want %+v", got, in)
|
|
}
|
|
}
|
|
|
|
func TestWire_TransferOutput_RoundTrip(t *testing.T) {
|
|
in := &TransferOutput{
|
|
Amt: 7_777_777,
|
|
OutputOwners: OutputOwners{
|
|
Locktime: 42,
|
|
Threshold: 1,
|
|
Addrs: []ids.ShortID{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}},
|
|
},
|
|
}
|
|
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 !in.OutputOwners.Equals(&got.OutputOwners) {
|
|
t.Errorf("OutputOwners mismatch: got %+v, want %+v", got.OutputOwners, in.OutputOwners)
|
|
}
|
|
}
|
|
|
|
func TestWire_TransferInput_RoundTrip(t *testing.T) {
|
|
in := &TransferInput{
|
|
Amt: 500,
|
|
Input: Input{SigIndices: []uint32{0, 2, 5, 7}},
|
|
}
|
|
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 !equalUint32Slice(got.SigIndices, in.SigIndices) {
|
|
t.Errorf("SigIndices: got %v, want %v", got.SigIndices, in.SigIndices)
|
|
}
|
|
}
|
|
|
|
func TestWire_MintOutput_RoundTrip(t *testing.T) {
|
|
in := &MintOutput{
|
|
OutputOwners: OutputOwners{
|
|
Locktime: 0,
|
|
Threshold: 1,
|
|
Addrs: []ids.ShortID{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}},
|
|
},
|
|
}
|
|
got, err := WrapMintOutput(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapMintOutput: %v", err)
|
|
}
|
|
if !in.OutputOwners.Equals(&got.OutputOwners) {
|
|
t.Errorf("OutputOwners mismatch: got %+v, want %+v", got.OutputOwners, in.OutputOwners)
|
|
}
|
|
}
|
|
|
|
func TestWire_MintOperation_RoundTrip(t *testing.T) {
|
|
addr := ids.ShortID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
|
|
in := &MintOperation{
|
|
MintInput: Input{SigIndices: []uint32{0}},
|
|
MintOutput: MintOutput{
|
|
OutputOwners: OutputOwners{Locktime: 0, Threshold: 1, Addrs: []ids.ShortID{addr}},
|
|
},
|
|
TransferOutput: TransferOutput{
|
|
Amt: 100,
|
|
OutputOwners: OutputOwners{Locktime: 0, Threshold: 1, Addrs: []ids.ShortID{addr}},
|
|
},
|
|
}
|
|
got, err := WrapMintOperation(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapMintOperation: %v", err)
|
|
}
|
|
if !equalUint32Slice(got.MintInput.SigIndices, in.MintInput.SigIndices) {
|
|
t.Errorf("MintInput.SigIndices: got %v, want %v", got.MintInput.SigIndices, in.MintInput.SigIndices)
|
|
}
|
|
if !in.MintOutput.OutputOwners.Equals(&got.MintOutput.OutputOwners) {
|
|
t.Errorf("MintOutput.OutputOwners mismatch")
|
|
}
|
|
if got.TransferOutput.Amt != in.TransferOutput.Amt {
|
|
t.Errorf("TransferOutput.Amt: got %d, want %d", got.TransferOutput.Amt, in.TransferOutput.Amt)
|
|
}
|
|
}
|
|
|
|
func TestWire_Credential_RoundTrip(t *testing.T) {
|
|
var s1, s2 [secp256k1.SignatureLen]byte
|
|
for i := range s1 {
|
|
s1[i] = byte(i)
|
|
}
|
|
for i := range s2 {
|
|
s2[i] = byte(0xFF - i)
|
|
}
|
|
in := &Credential{Sigs: [][secp256k1.SignatureLen]byte{s1, s2}}
|
|
got, err := WrapCredential(in.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("WrapCredential: %v", err)
|
|
}
|
|
if len(got.Sigs) != 2 {
|
|
t.Fatalf("Sigs len: got %d, want 2", len(got.Sigs))
|
|
}
|
|
if !bytes.Equal(got.Sigs[0][:], s1[:]) {
|
|
t.Errorf("Sigs[0] mismatch")
|
|
}
|
|
if !bytes.Equal(got.Sigs[1][:], s2[:]) {
|
|
t.Errorf("Sigs[1] mismatch")
|
|
}
|
|
}
|
|
|
|
func equalUint32Slice(a, b []uint32) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|