94 lines
2.8 KiB
PHP
94 lines
2.8 KiB
PHP
<?php
|
||
// migrate_addresses.php
|
||
|
||
// --- Setup & Dependencies ---
|
||
error_reporting(E_ALL);
|
||
ini_set('display_errors', 1);
|
||
|
||
require_once __DIR__ . '/src/salvium_tipbot_db.php';
|
||
require_once __DIR__ . '/src/salvium_tipbot_wallet.php';
|
||
|
||
use Salvium\SalviumTipBotDB;
|
||
use Salvium\SalviumWallet;
|
||
|
||
// --- Load Configuration ---
|
||
$configFile = __DIR__ . '/config.php';
|
||
if (!file_exists($configFile)) {
|
||
die("❌ ERROR: config.php not found.\n");
|
||
}
|
||
$config = require $configFile;
|
||
|
||
// --- Confirmation Prompt ---
|
||
echo "*****************************************************************\n";
|
||
echo "WARNING: This script will generate and assign a new 'SC1'\n";
|
||
echo " address for EVERY user in your database.\n";
|
||
echo " This action is irreversible.\n";
|
||
echo "*****************************************************************\n";
|
||
echo "Are you sure you want to continue? (yes/no): ";
|
||
|
||
$handle = fopen("php://stdin", "r");
|
||
$line = trim(fgets($handle));
|
||
fclose($handle);
|
||
|
||
if (strtolower($line) !== 'yes') {
|
||
die("🛑 Migration cancelled by user.\n");
|
||
}
|
||
|
||
echo "\n✅ Confirmation received. Starting migration...\n";
|
||
|
||
// --- Initialize Classes ---
|
||
try {
|
||
$db = new SalviumTipBotDB($config);
|
||
$wallet = new SalviumWallet(
|
||
$config['SALVIUM_RPC_HOST'],
|
||
$config['SALVIUM_RPC_PORT'],
|
||
$config['SALVIUM_RPC_USERNAME'],
|
||
$config['SALVIUM_RPC_PASSWORD']
|
||
);
|
||
} catch (Exception $e) {
|
||
die("❌ ERROR: Failed to initialize classes: " . $e->getMessage() . "\n");
|
||
}
|
||
|
||
// --- Fetch All Users ---
|
||
$users = $db->getAllUsers();
|
||
if (empty($users)) {
|
||
die("ℹ️ No users found in the database. Nothing to do.\n");
|
||
}
|
||
$totalUsers = count($users);
|
||
echo "✅ Found {$totalUsers} users to update.\n\n";
|
||
|
||
// --- Migration Loop ---
|
||
$successCount = 0;
|
||
foreach ($users as $index => $user) {
|
||
$userId = $user['id'];
|
||
$oldAddress = $user['salvium_subaddress'];
|
||
|
||
echo "Updating user #{$userId} (" . ($index + 1) . "/{$totalUsers})... ";
|
||
|
||
// 1. Generate new address
|
||
$newAddress = $wallet->getNewSubaddress();
|
||
|
||
if (!$newAddress || !str_starts_with($newAddress, 'SC1')) {
|
||
echo "❌ FAILED: Could not generate a valid new address from the wallet RPC.\n";
|
||
continue; // Skip to the next user
|
||
}
|
||
|
||
// 2. Update the database
|
||
if ($db->updateUserSubaddress($userId, $newAddress)) {
|
||
echo "✅ SUCCESS: New address assigned.\n";
|
||
// Optional: uncomment the line below for detailed logging
|
||
// echo " - Old: {$oldAddress}\n - New: {$newAddress}\n";
|
||
$successCount++;
|
||
} else {
|
||
echo "❌ FAILED: Database update failed for user #{$userId}.\n";
|
||
}
|
||
}
|
||
|
||
// --- Final Report ---
|
||
echo "\n============================================\n";
|
||
echo "🎉 Migration Complete!\n";
|
||
echo "Successfully updated: {$successCount} / {$totalUsers} users.\n";
|
||
echo "============================================\n";
|
||
|
||
?>
|