|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/fystack/mpcium/pkg/config" |
| 9 | + "github.com/fystack/mpcium/pkg/infra" |
| 10 | + "github.com/fystack/mpcium/pkg/keyinfo" |
| 11 | + "github.com/fystack/mpcium/pkg/logger" |
| 12 | + "github.com/spf13/viper" |
| 13 | +) |
| 14 | + |
| 15 | +type KeyReport struct { |
| 16 | + Key string `json:"key"` |
| 17 | + WalletID string `json:"wallet_id"` |
| 18 | + KeyType string `json:"key_type"` |
| 19 | + ParticipantCount int `json:"participant_count"` |
| 20 | + ParticipantPeerIDs []string `json:"participant_peer_ids"` |
| 21 | + Threshold int `json:"threshold"` |
| 22 | + Version int `json:"version"` |
| 23 | +} |
| 24 | + |
| 25 | +// extractWalletIDAndKeyType extracts wallet ID and key type from the consul key |
| 26 | +// Format: threshold_keyinfo/eddsa:0f2d9b28-7066-4571-855c-980983928fe8:0 |
| 27 | +// or: threshold_keyinfo/ecdsa:wallet-id:index |
| 28 | +func extractWalletIDAndKeyType(consulKey string) (walletID, keyType string) { |
| 29 | + // Remove the prefix |
| 30 | + withoutPrefix := strings.TrimPrefix(consulKey, "threshold_keyinfo/") |
| 31 | + |
| 32 | + // Split by colon to get key type and wallet info |
| 33 | + parts := strings.SplitN(withoutPrefix, ":", 2) |
| 34 | + if len(parts) >= 2 { |
| 35 | + keyType = parts[0] |
| 36 | + walletID = parts[1] // This includes the wallet ID and any suffix like ":0" |
| 37 | + } else { |
| 38 | + // Fallback: if no colon, treat the whole thing as wallet ID |
| 39 | + walletID = withoutPrefix |
| 40 | + keyType = "unknown" |
| 41 | + } |
| 42 | + |
| 43 | + return walletID, keyType |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + config.InitViperConfig() |
| 48 | + environment := viper.GetString("environment") |
| 49 | + logger.Init(environment, true) |
| 50 | + |
| 51 | + consulClient := infra.GetConsulClient(environment) |
| 52 | + // Get KV client |
| 53 | + kv := consulClient.KV() |
| 54 | + |
| 55 | + // List all keys under threshold_keyinfo/ |
| 56 | + pairs, _, err := kv.List("threshold_keyinfo/", nil) |
| 57 | + if err != nil { |
| 58 | + logger.Fatal("Failed to list keys", err) |
| 59 | + } |
| 60 | + |
| 61 | + var keysWithLowParticipants []KeyReport |
| 62 | + |
| 63 | + // Check each key |
| 64 | + for _, pair := range pairs { |
| 65 | + var info keyinfo.KeyInfo |
| 66 | + if err := json.Unmarshal(pair.Value, &info); err != nil { |
| 67 | + logger.Warn("Failed to unmarshal key", |
| 68 | + "key", pair.Key, |
| 69 | + "error", err, |
| 70 | + ) |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + // Check if participants are less than 3 |
| 75 | + if len(info.ParticipantPeerIDs) < 3 { |
| 76 | + walletID, keyType := extractWalletIDAndKeyType(pair.Key) |
| 77 | + |
| 78 | + report := KeyReport{ |
| 79 | + Key: pair.Key, |
| 80 | + WalletID: walletID, |
| 81 | + KeyType: keyType, |
| 82 | + ParticipantCount: len(info.ParticipantPeerIDs), |
| 83 | + ParticipantPeerIDs: info.ParticipantPeerIDs, |
| 84 | + Threshold: info.Threshold, |
| 85 | + Version: info.Version, |
| 86 | + } |
| 87 | + keysWithLowParticipants = append(keysWithLowParticipants, report) |
| 88 | + logger.Info("Found key with low participants", |
| 89 | + "key", pair.Key, |
| 90 | + "wallet_id", walletID, |
| 91 | + "key_type", keyType, |
| 92 | + "count", len(info.ParticipantPeerIDs), |
| 93 | + "participants", info.ParticipantPeerIDs, |
| 94 | + ) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Create report |
| 99 | + report := struct { |
| 100 | + TotalKeysFound int `json:"total_keys_found"` |
| 101 | + Keys []KeyReport `json:"keys"` |
| 102 | + }{ |
| 103 | + TotalKeysFound: len(keysWithLowParticipants), |
| 104 | + Keys: keysWithLowParticipants, |
| 105 | + } |
| 106 | + |
| 107 | + // Save to JSON file |
| 108 | + outputFile := "low_participant_keys.json" |
| 109 | + reportJSON, err := json.MarshalIndent(report, "", " ") |
| 110 | + if err != nil { |
| 111 | + logger.Fatal("Failed to marshal report", err) |
| 112 | + } |
| 113 | + |
| 114 | + if err := os.WriteFile(outputFile, reportJSON, 0644); err != nil { |
| 115 | + logger.Fatal("Failed to write report", err) |
| 116 | + } |
| 117 | + |
| 118 | + logger.Info("Report generated", |
| 119 | + "total_keys", len(keysWithLowParticipants), |
| 120 | + "output_file", outputFile, |
| 121 | + ) |
| 122 | +} |
0 commit comments