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

177 lines
5.3 KiB
Go

// Copyright (c) 2024-2025 Hanzo AI Inc.
// SPDX-License-Identifier: BSD-3-Clause
// Ethereum/EVM Threshold Signing Example
// Demonstrates threshold ECDSA signing for Ethereum and EVM-compatible chains
package main
import (
"encoding/hex"
"fmt"
"log"
"math/big"
"github.com/luxfi/mpc/pkg/threshold"
"golang.org/x/crypto/sha3"
)
func main() {
fmt.Println("=== Ethereum/EVM Threshold Signing Example ===")
fmt.Println()
// Create unified threshold API
api := threshold.NewUnifiedThresholdAPI()
defer api.Close()
// Define MPC nodes with 2-of-3 threshold
partyIDs := []string{"signer1", "signer2", "signer3"}
thresholdValue := 1 // 2-of-3 threshold
fmt.Println("1. Ethereum Transaction Signing")
fmt.Println(" Protocol: CGGMP21 (ECDSA on secp256k1)")
fmt.Println(" Compatible: Ethereum, Polygon, BSC, Arbitrum, Optimism, Avalanche C-Chain")
fmt.Println()
// Initialize key generation
party, err := api.KeyGen(threshold.SchemeECDSA, "signer1", partyIDs, thresholdValue)
if err != nil {
log.Fatalf("Failed to start keygen: %v", err)
}
// Example EIP-155 transaction (Ethereum mainnet)
tx := &EthTransaction{
Nonce: uint64(42),
GasPrice: big.NewInt(50_000_000_000), // 50 Gwei
GasLimit: uint64(21000),
To: "0x742d35Cc6634C0532925a3b844Bc9e7595f1D123",
Value: big.NewInt(1_000_000_000_000_000_000), // 1 ETH
Data: []byte{},
ChainID: big.NewInt(1), // Mainnet
}
// Compute transaction hash (Keccak256)
txHash := tx.Hash()
fmt.Printf(" ✓ Key generation initiated\n")
fmt.Printf(" ✓ Transaction details:\n")
fmt.Printf(" - Nonce: %d\n", tx.Nonce)
fmt.Printf(" - Gas Price: %s Gwei\n", new(big.Int).Div(tx.GasPrice, big.NewInt(1_000_000_000)))
fmt.Printf(" - Gas Limit: %d\n", tx.GasLimit)
fmt.Printf(" - To: %s\n", tx.To)
fmt.Printf(" - Value: %s ETH\n", formatEther(tx.Value))
fmt.Printf(" - Chain ID: %d (Mainnet)\n", tx.ChainID)
fmt.Printf(" ✓ TX Hash: 0x%s\n", hex.EncodeToString(txHash[:16])+"...")
fmt.Printf(" ✓ Threshold: %d-of-%d signers required\n", thresholdValue+1, len(partyIDs))
fmt.Printf(" ✓ Party status: Done=%v\n", party.Done())
fmt.Println()
// Example 2: ERC-20 Token Transfer
fmt.Println("2. ERC-20 Token Transfer")
fmt.Println()
// ERC-20 transfer function signature: transfer(address,uint256)
transferData := encodeERC20Transfer(
"0x1234567890123456789012345678901234567890",
big.NewInt(1000_000_000_000_000_000), // 1 token (18 decimals)
)
erc20Tx := &EthTransaction{
Nonce: uint64(43),
GasPrice: big.NewInt(50_000_000_000),
GasLimit: uint64(60000),
To: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // Example: USDC
Value: big.NewInt(0),
Data: transferData,
ChainID: big.NewInt(1),
}
erc20Hash := erc20Tx.Hash()
fmt.Printf(" ✓ ERC-20 Transfer encoded\n")
fmt.Printf(" ✓ Contract: %s\n", erc20Tx.To)
fmt.Printf(" ✓ TX Hash: 0x%s\n", hex.EncodeToString(erc20Hash[:16])+"...")
fmt.Println()
// Supported EVM chains
fmt.Println("=== Supported EVM Chains ===")
chains := []struct {
name string
chainID int64
}{
{"Ethereum Mainnet", 1},
{"Polygon", 137},
{"BNB Smart Chain", 56},
{"Arbitrum One", 42161},
{"Optimism", 10},
{"Avalanche C-Chain", 43114},
{"Base", 8453},
{"Lux Network", 9000},
}
for _, chain := range chains {
fmt.Printf(" ✓ %s (Chain ID: %d)\n", chain.name, chain.chainID)
}
fmt.Println()
fmt.Println("=== EVM MPC Benefits ===")
fmt.Println(" • Institutional-grade custody for ETH and tokens")
fmt.Println(" • Compatible with all ERC-20, ERC-721, ERC-1155 tokens")
fmt.Println(" • DeFi protocol interaction support")
fmt.Println(" • Multi-chain deployment with single key setup")
fmt.Println(" • EIP-712 typed data signing support")
fmt.Println()
fmt.Println("✅ Ethereum/EVM threshold signing ready!")
}
// EthTransaction represents a simplified Ethereum transaction
type EthTransaction struct {
Nonce uint64
GasPrice *big.Int
GasLimit uint64
To string
Value *big.Int
Data []byte
ChainID *big.Int
}
// Hash computes the Keccak256 hash of the transaction for signing
func (tx *EthTransaction) Hash() [32]byte {
// Simplified: in production use RLP encoding
h := sha3.NewLegacyKeccak256()
h.Write(big.NewInt(int64(tx.Nonce)).Bytes())
h.Write(tx.GasPrice.Bytes())
h.Write(big.NewInt(int64(tx.GasLimit)).Bytes())
h.Write([]byte(tx.To))
h.Write(tx.Value.Bytes())
h.Write(tx.Data)
h.Write(tx.ChainID.Bytes())
var result [32]byte
copy(result[:], h.Sum(nil))
return result
}
// encodeERC20Transfer encodes an ERC-20 transfer call
func encodeERC20Transfer(to string, amount *big.Int) []byte {
// Function selector: keccak256("transfer(address,uint256)")[:4]
selector := []byte{0xa9, 0x05, 0x9c, 0xbb}
// Pad address to 32 bytes
addrBytes := make([]byte, 32)
copy(addrBytes[12:], []byte(to)[2:22]) // Remove 0x prefix
// Pad amount to 32 bytes
amountBytes := make([]byte, 32)
amount.FillBytes(amountBytes)
result := make([]byte, 0, 68)
result = append(result, selector...)
result = append(result, addrBytes...)
result = append(result, amountBytes...)
return result
}
// formatEther converts wei to ETH string
func formatEther(wei *big.Int) string {
eth := new(big.Float).SetInt(wei)
divisor := new(big.Float).SetInt(big.NewInt(1_000_000_000_000_000_000))
eth.Quo(eth, divisor)
return eth.Text('f', 4)
}