Files
zeekay 1fa6eb5fd5 refactor(mpc): thin wrapper over luxfi/mpc (canonical) — v2.0.0
Per CTO directive: hanzo/mpc must be identical to luxfi/mpc with only
defaults differing. Eliminates fork drift and consolidates the canonical
implementation in github.com/luxfi/mpc.

What changed:
  - cmd/hanzo-mpc/main.go: now mirrors luxfi/mpc/cmd/mpcd verbatim, with
    Hanzo overrides applied via setEnvDefault before viper init:
      MPC_DATA_DIR    = /data/hanzo-mpc      (lux: /data/mpc)
      MPC_DB_PATH     = /data/hanzo-mpc/db
      MPC_BACKUP_DIR  = /data/hanzo-mpc/backups
      BRAND_NAME      = Hanzo
    Version bumped to 2.0.0; banner shows "Hanzo MPC v2.0.0 — thin wrapper
    over luxfi/mpc (canonical)".
  - cmd/hanzo-mpc-cli/main.go: removed `recover` subcommand (the kvstore
    BackupExecutor API moved into luxfi/mpc/pkg/backup; downstream tooling
    should call that directly). Renamed binary to "hanzo-mpc-cli" and
    bumped to v2.0.0.
  - examples/, scripts/: import paths swapped from
    github.com/hanzoai/mpc/pkg/* → github.com/luxfi/mpc/pkg/*. Broken
    migration scripts (BadgerKVStore API removed upstream) deleted.
  - go.mod: drops 40+ direct deps that lived in the fork. Adds direct
    dep on github.com/luxfi/mpc with replace directive to ../../lux/mpc
    for local dev. Mirrors luxfi/mpc's `replace github.com/agl/ed25519
    => github.com/luxfi/edwards25519` for transitive correctness.
  - Dockerfile: data dir bumped to /data/hanzo-mpc; ports updated to the
    canonical luxfi/mpc layout (9651=P2P, 9800=internal API,
    8081=dashboard).
  - pkg/: DELETED entirely (api/, client/, config/, constant/, encoding/,
    encryption/, event/, eventconsumer/, hsm/, identity/, infra/,
    keyinfo/, kms/, kvstore/, logger/, messaging/, mpc/, policy/,
    protocol/, storage/, threshold/, types/, utils/). All consumers
    (zero external Go consumers found) point at luxfi/mpc/pkg/* now.

Net: -26961 / +1663 LOC.

Wire/protocol compatibility:
  Hanzo MPC speaks the same NATS messaging, ZapDB key-share format,
  consensus transport, and HSM signer protocols as luxfi/mpc — they are
  literally the same code. A Hanzo MPC node can join a Lux MPC cluster
  and participate in keygen/sign rounds without modification.
2026-04-18 21:26:32 -07:00

99 lines
3.3 KiB
Go

// Copyright (c) 2024-2025 Hanzo AI Inc.
// SPDX-License-Identifier: BSD-3-Clause
// Bitcoin Threshold Signing Example
// Demonstrates both Legacy/SegWit (ECDSA) and Taproot (Schnorr) signing
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"github.com/luxfi/mpc/pkg/threshold"
)
func main() {
fmt.Println("=== Bitcoin Threshold Signing Examples ===")
fmt.Println()
// Create unified threshold API
api := threshold.NewUnifiedThresholdAPI()
defer api.Close()
// Define 3 MPC nodes with 2-of-3 threshold
partyIDs := []string{"node1", "node2", "node3"}
thresholdValue := 1 // t=1 means 2-of-3 (t+1 signers required)
// Example 1: Bitcoin Legacy/SegWit (ECDSA/secp256k1)
fmt.Println("1. Bitcoin Legacy/SegWit Transaction Signing")
fmt.Println(" Protocol: CGGMP21 (ECDSA on secp256k1)")
fmt.Println(" Use case: P2PKH, P2SH, P2WPKH, P2WSH addresses")
fmt.Println()
legacyParty, err := api.KeyGen(threshold.SchemeECDSA, "node1", partyIDs, thresholdValue)
if err != nil {
log.Fatalf("Failed to start Legacy keygen: %v", err)
}
// Simulate a Bitcoin transaction hash to sign
// In production: serialized tx -> double SHA256
txData := []byte("Bitcoin Legacy Transaction Data")
legacyTxHash := doubleSHA256(txData)
fmt.Printf(" ✓ Key generation initiated\n")
fmt.Printf(" ✓ Transaction hash: %s\n", hex.EncodeToString(legacyTxHash[:16])+"...")
fmt.Printf(" ✓ Threshold: %d-of-%d signers required\n", thresholdValue+1, len(partyIDs))
fmt.Printf(" ✓ Party status: Done=%v\n", legacyParty.Done())
fmt.Println()
// Example 2: Bitcoin Taproot (Schnorr/BIP-340)
fmt.Println("2. Bitcoin Taproot Transaction Signing")
fmt.Println(" Protocol: FROST (Schnorr on secp256k1)")
fmt.Println(" Use case: P2TR addresses, script path spending")
fmt.Println()
taprootParty, err := api.KeyGen(threshold.SchemeTaproot, "node2", partyIDs, thresholdValue)
if err != nil {
log.Fatalf("Failed to start Taproot keygen: %v", err)
}
// Taproot uses tagged hashes (BIP-340)
taprootTxHash := taggedHash("TapSighash", txData)
fmt.Printf(" ✓ Key generation initiated\n")
fmt.Printf(" ✓ Tagged hash: %s\n", hex.EncodeToString(taprootTxHash[:16])+"...")
fmt.Printf(" ✓ Threshold: %d-of-%d signers required\n", thresholdValue+1, len(partyIDs))
fmt.Printf(" ✓ Party status: Done=%v\n", taprootParty.Done())
fmt.Println()
// Summary
fmt.Println("=== Bitcoin MPC Benefits ===")
fmt.Println(" • No single point of failure for private keys")
fmt.Println(" • Distributed custody across multiple parties")
fmt.Println(" • Standard Bitcoin transactions (indistinguishable on-chain)")
fmt.Println(" • Taproot: smaller signatures, better privacy")
fmt.Println(" • Legacy: broad wallet compatibility")
fmt.Println()
fmt.Println("✅ Bitcoin threshold signing ready!")
}
// doubleSHA256 computes SHA256(SHA256(data)) for Bitcoin tx signing
func doubleSHA256(data []byte) [32]byte {
first := sha256.Sum256(data)
return sha256.Sum256(first[:])
}
// taggedHash computes SHA256(SHA256(tag) || SHA256(tag) || msg) per BIP-340
func taggedHash(tag string, msg []byte) [32]byte {
tagHash := sha256.Sum256([]byte(tag))
h := sha256.New()
h.Write(tagHash[:])
h.Write(tagHash[:])
h.Write(msg)
var result [32]byte
copy(result[:], h.Sum(nil))
return result
}