Files
mpc/pkg/kms/kms_client.go
zeekay 19b49809fd Implement threshold MPC signing with CGGMP21, FROST, and LSS protocols
This commit adds complete multi-party computation (MPC) threshold signing
for multiple cryptographic protocols:

CGGMP21 (ECDSA/secp256k1):
- Key generation for Bitcoin/Ethereum/EVM chains
- Threshold signing with t-of-n security
- Recovery byte calculation for Ethereum compatibility

FROST/Taproot (EdDSA/BIP-340):
- EdDSA key generation for Solana/Sui/Polkadot
- BIP-340 compatible Schnorr signatures (64 bytes)
- CBOR serialization for TaprootConfig crypto types

LSS (Linear Secret Sharing):
- Dynamic resharing support (change participants)
- ECDSA signing with blinding protocol
- Protocol adapter for unified interface

Key fixes:
- Use MarshalBinary/UnmarshalBinary for protocol messages
- CBOR serialization for FROST TaprootConfig (not JSON)
- Correct type assertion for taproot.Signature ([]byte)
- Sorted party IDs for deterministic ordering

E2E tests pass for both ECDSA and EdDSA signing.
2026-01-24 23:41:43 -08:00

303 lines
8.4 KiB
Go

package kms
import (
"context"
"encoding/json"
"fmt"
"github.com/hanzoai/mpc/pkg/logger"
)
// KMSClient wraps the Lux KMS SDK for MPC key management
type KMSClient struct {
projectID string
environment string
secretPath string
siteURL string
// Using map to store secrets locally for now
secrets map[string][]byte
}
// KMSConfig holds configuration for Lux KMS integration
type KMSConfig struct {
ClientID string
ClientSecret string
ProjectID string
Environment string
SecretPath string
SiteURL string
}
// SecretMetadata represents metadata about a secret
type SecretMetadata struct {
Name string `json:"name"`
Path string `json:"path"`
Version int `json:"version"`
Environment string `json:"environment"`
Type string `json:"type"`
}
// NewKMSClient creates a new Lux KMS client for secure key operations
func NewKMSClient(config KMSConfig) (*KMSClient, error) {
// Validate config
if config.ProjectID == "" {
return nil, fmt.Errorf("project ID is required")
}
// Set defaults
if config.Environment == "" {
config.Environment = "prod"
}
if config.SecretPath == "" {
config.SecretPath = "/mpc"
}
if config.SiteURL == "" {
config.SiteURL = "http://localhost:8080" // Default local instance
}
logger.Info("Initializing KMS client (stub implementation)",
"projectID", config.ProjectID,
"environment", config.Environment,
"secretPath", config.SecretPath,
)
return &KMSClient{
projectID: config.ProjectID,
environment: config.Environment,
secretPath: config.SecretPath,
siteURL: config.SiteURL,
secrets: make(map[string][]byte),
}, nil
}
// StoreKeyShare stores an MPC key share in Lux KMS
func (c *KMSClient) StoreKeyShare(ctx context.Context, walletID string, keyShare []byte) error {
secretName := fmt.Sprintf("%s/wallets/%s/keyshare", c.secretPath, walletID)
// Store in local map for stub implementation
c.secrets[secretName] = keyShare
logger.Info("Stored key share (stub)",
"walletID", walletID,
"size", len(keyShare),
)
return nil
}
// RetrieveKeyShare retrieves an MPC key share from Lux KMS
func (c *KMSClient) RetrieveKeyShare(ctx context.Context, walletID string) ([]byte, error) {
secretName := fmt.Sprintf("%s/wallets/%s/keyshare", c.secretPath, walletID)
// Retrieve from local map for stub implementation
keyShare, ok := c.secrets[secretName]
if !ok {
return nil, fmt.Errorf("key share not found for wallet %s", walletID)
}
logger.Info("Retrieved key share (stub)",
"walletID", walletID,
"size", len(keyShare),
)
return keyShare, nil
}
// RotateKeyShare rotates the key share for a wallet
func (c *KMSClient) RotateKeyShare(ctx context.Context, walletID string, newKeyShare []byte) error {
// For stub implementation, just update the existing key
return c.StoreKeyShare(ctx, walletID, newKeyShare)
}
// StorePresignature stores a presignature in Lux KMS
func (c *KMSClient) StorePresignature(ctx context.Context, walletID, sigID string, presigData []byte) error {
secretName := fmt.Sprintf("%s/wallets/%s/presigs/%s", c.secretPath, walletID, sigID)
// Store in local map for stub implementation
c.secrets[secretName] = presigData
logger.Info("Stored presignature (stub)",
"walletID", walletID,
"sigID", sigID,
"size", len(presigData),
)
return nil
}
// RetrievePresignature retrieves a presignature from Lux KMS
func (c *KMSClient) RetrievePresignature(ctx context.Context, walletID, sigID string) ([]byte, error) {
secretName := fmt.Sprintf("%s/wallets/%s/presigs/%s", c.secretPath, walletID, sigID)
// Retrieve from local map for stub implementation
presigData, ok := c.secrets[secretName]
if !ok {
return nil, fmt.Errorf("presignature not found for wallet %s, sig %s", walletID, sigID)
}
logger.Info("Retrieved presignature (stub)",
"walletID", walletID,
"sigID", sigID,
"size", len(presigData),
)
return presigData, nil
}
// DeletePresignature removes a used presignature
func (c *KMSClient) DeletePresignature(ctx context.Context, walletID, sigID string) error {
secretName := fmt.Sprintf("%s/wallets/%s/presigs/%s", c.secretPath, walletID, sigID)
// Delete from local map for stub implementation
delete(c.secrets, secretName)
logger.Info("Deleted presignature (stub)",
"walletID", walletID,
"sigID", sigID,
)
return nil
}
// ListSecrets lists all secrets in a given path
func (c *KMSClient) ListSecrets(ctx context.Context, path string) ([]SecretMetadata, error) {
fullPath := fmt.Sprintf("%s%s", c.secretPath, path)
var secrets []SecretMetadata
for key := range c.secrets {
if len(key) >= len(fullPath) && key[:len(fullPath)] == fullPath {
secrets = append(secrets, SecretMetadata{
Name: key,
Path: fullPath,
Version: 1,
Environment: c.environment,
Type: "keyshare",
})
}
}
return secrets, nil
}
// Healthcheck verifies KMS connectivity
func (c *KMSClient) Healthcheck(ctx context.Context) error {
// Stub implementation always returns healthy
logger.Debug("KMS health check passed (stub)")
return nil
}
// Close closes the KMS client connection
func (c *KMSClient) Close() error {
// Clear local secrets map
c.secrets = nil
logger.Info("KMS client closed (stub)")
return nil
}
// StoreMPCKeyShare stores an MPC key share with specific node and wallet IDs
func (c *KMSClient) StoreMPCKeyShare(nodeID, walletID, keyType string, keyData []byte) error {
secretName := fmt.Sprintf("%s/nodes/%s/wallets/%s/%s", c.secretPath, nodeID, walletID, keyType)
c.secrets[secretName] = keyData
logger.Info("Stored MPC key share",
"nodeID", nodeID,
"walletID", walletID,
"keyType", keyType,
"size", len(keyData),
)
return nil
}
// RetrieveMPCKeyShare retrieves an MPC key share with specific node and wallet IDs
func (c *KMSClient) RetrieveMPCKeyShare(nodeID, walletID, keyType string) ([]byte, error) {
secretName := fmt.Sprintf("%s/nodes/%s/wallets/%s/%s", c.secretPath, nodeID, walletID, keyType)
keyData, ok := c.secrets[secretName]
if !ok {
return nil, fmt.Errorf("key share not found for node %s, wallet %s, type %s", nodeID, walletID, keyType)
}
logger.Info("Retrieved MPC key share",
"nodeID", nodeID,
"walletID", walletID,
"keyType", keyType,
"size", len(keyData),
)
return keyData, nil
}
// ListKeys lists all keys (without the actual key data)
func (c *KMSClient) ListKeys() ([]string, error) {
var keys []string
for key := range c.secrets {
keys = append(keys, key)
}
logger.Info("Listed keys", "count", len(keys))
return keys, nil
}
// DeleteKey removes a key from storage
func (c *KMSClient) DeleteKey(key string) error {
if _, ok := c.secrets[key]; !ok {
return fmt.Errorf("key not found: %s", key)
}
delete(c.secrets, key)
logger.Info("Deleted key", "key", key)
return nil
}
// BatchStore stores multiple secrets in a single operation
func (c *KMSClient) BatchStore(ctx context.Context, secrets map[string][]byte) error {
for name, data := range secrets {
fullName := fmt.Sprintf("%s/%s", c.secretPath, name)
c.secrets[fullName] = data
}
logger.Info("Batch stored secrets (stub)", "count", len(secrets))
return nil
}
// BatchRetrieve retrieves multiple secrets in a single operation
func (c *KMSClient) BatchRetrieve(ctx context.Context, names []string) (map[string][]byte, error) {
results := make(map[string][]byte)
for _, name := range names {
fullName := fmt.Sprintf("%s/%s", c.secretPath, name)
if data, ok := c.secrets[fullName]; ok {
results[name] = data
}
}
logger.Info("Batch retrieved secrets (stub)", "requested", len(names), "found", len(results))
return results, nil
}
// StoreConfig stores MPC node configuration
func (c *KMSClient) StoreConfig(ctx context.Context, nodeID string, config interface{}) error {
configName := fmt.Sprintf("%s/nodes/%s/config", c.secretPath, nodeID)
data, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("failed to marshal config: %w", err)
}
c.secrets[configName] = data
logger.Info("Stored node config (stub)", "nodeID", nodeID)
return nil
}
// RetrieveConfig retrieves MPC node configuration
func (c *KMSClient) RetrieveConfig(ctx context.Context, nodeID string, config interface{}) error {
configName := fmt.Sprintf("%s/nodes/%s/config", c.secretPath, nodeID)
data, ok := c.secrets[configName]
if !ok {
return fmt.Errorf("config not found for node %s", nodeID)
}
if err := json.Unmarshal(data, config); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
logger.Info("Retrieved node config (stub)", "nodeID", nodeID)
return nil
}