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.
68 lines
2.3 KiB
Go
68 lines
2.3 KiB
Go
// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package wire
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/luxfi/crypto/hash"
|
|
"github.com/luxfi/crypto/secp256k1"
|
|
)
|
|
|
|
// SignSecp256k1 is the ZAP-native replacement for the legacy
|
|
// `txs.Tx.Sign(c codec.Manager, ...)` method. It takes the unsigned tx
|
|
// bytes (already ZAP-encoded by the txs.zap_native build path) plus a
|
|
// list of signer groups (one group per input, like the legacy API), and
|
|
// returns a fully-formed SignedTx wire envelope.
|
|
//
|
|
// The signing target is hash(unsignedBytes) — same as the legacy Sign.
|
|
// Each signer group produces one Credential whose Signatures field is
|
|
// the concatenation of all per-key secp256k1 signatures.
|
|
//
|
|
// This is the canonical secp256k1fx signing entry point. For multi-fx
|
|
// signing (mixing secp256k1 + ML-DSA + Ed25519 credentials), build the
|
|
// per-input credentials individually with NewCredential and pass them
|
|
// directly to NewSignedTx.
|
|
//
|
|
// Returns:
|
|
// - signedTxBytes: the canonical wire envelope (SignedTx{unsigned, creds})
|
|
// - err: a typed error from secp256k1.PrivateKey.SignHash on failure
|
|
func SignSecp256k1(unsignedBytes []byte, signers [][]*secp256k1.PrivateKey) ([]byte, error) {
|
|
txHash := hash.ComputeHash256(unsignedBytes)
|
|
|
|
credentials := make([][]byte, 0, len(signers))
|
|
for groupIdx, keys := range signers {
|
|
// Each input may have multiple co-signers; concatenate their
|
|
// secp256k1.SignatureLen-byte signatures into the credential.
|
|
sigsConcat := make([]byte, 0, len(keys)*secp256k1.SignatureLen)
|
|
for keyIdx, key := range keys {
|
|
sig, err := key.SignHash(txHash)
|
|
if err != nil {
|
|
return nil, fmt.Errorf(
|
|
"wire.SignSecp256k1: group %d key %d: %w",
|
|
groupIdx, keyIdx, err,
|
|
)
|
|
}
|
|
if len(sig) != secp256k1.SignatureLen {
|
|
return nil, fmt.Errorf(
|
|
"wire.SignSecp256k1: group %d key %d: sig length %d != %d",
|
|
groupIdx, keyIdx, len(sig), secp256k1.SignatureLen,
|
|
)
|
|
}
|
|
sigsConcat = append(sigsConcat, sig...)
|
|
}
|
|
cred := NewCredential(CredentialInput{
|
|
TypeKind: TypeKindSecp256k1,
|
|
SecurityLevel: 0,
|
|
Signatures: sigsConcat,
|
|
})
|
|
credentials = append(credentials, cred)
|
|
}
|
|
|
|
return NewSignedTx(SignedTxInput{
|
|
UnsignedBytes: unsignedBytes,
|
|
Credentials: credentials,
|
|
}), nil
|
|
}
|