Files
mines/rin/wallet/cmd/restore_wallet_file.sh
2025-09-30 11:58:36 +03:00

180 lines
5.4 KiB
Bash

#!/bin/bash
# RinCoin Wallet File Restoration Script
# This is the RECOMMENDED restoration method for user-friendly wallets!
# Restores a wallet from a complete wallet directory backup
#
# This preserves:
# - All addresses (EXACT format - rin1q...)
# - HD wallet structure
# - Transaction history
# - All wallet metadata
set -euo pipefail
if [[ $# -lt 1 ]] || [[ $# -gt 2 ]]; then
echo "Usage: $0 <backup_file.tar.gz> [new_wallet_name]"
echo ""
echo "Examples:"
echo " $0 ~/rin_wallet_file_backup_main_20250929.tar.gz"
echo " $0 ~/rin_wallet_file_backup_main_20250929.tar.gz restored_main"
echo ""
echo "This restores a wallet from a complete wallet directory backup."
echo "The wallet will have IDENTICAL addresses to the original."
exit 1
fi
BACKUP_FILE="$1"
NEW_WALLET_NAME="${2:-}"
# Wallet paths
DATA_DIR="/mnt/data/docker_vol/rincoin/rincoin-node/data"
# RPC Configuration
RPC_USER="rinrpc"
RPC_PASSWORD="745ce784d5d537fc06105a1b935b7657903cfc71a5fb3b90"
RPC_HOST="localhost"
RPC_PORT="9556"
echo "RinCoin Wallet File Restoration"
echo "================================"
echo "Backup file: $BACKUP_FILE"
echo ""
# Check if backup file exists
if [[ ! -f "$BACKUP_FILE" ]]; then
echo "Error: Backup file not found: $BACKUP_FILE"
exit 1
fi
# Check if RIN node is running
if ! pgrep -f "rincoind" > /dev/null; then
echo "Error: RinCoin daemon is not running. Start it first."
exit 1
fi
# Extract wallet name from archive
echo "Examining backup file..."
ORIGINAL_WALLET_NAME=$(tar -tzf "$BACKUP_FILE" | head -1 | cut -d/ -f1)
if [[ -z "$ORIGINAL_WALLET_NAME" ]]; then
echo "Error: Could not determine wallet name from backup file"
exit 1
fi
echo "Original wallet name: $ORIGINAL_WALLET_NAME"
# Determine target wallet name
if [[ -z "$NEW_WALLET_NAME" ]]; then
NEW_WALLET_NAME="$ORIGINAL_WALLET_NAME"
echo "Target wallet name: $NEW_WALLET_NAME (same as original)"
else
echo "Target wallet name: $NEW_WALLET_NAME (renamed)"
fi
TARGET_WALLET_DIR="$DATA_DIR/$NEW_WALLET_NAME"
# Check if target already exists
if [[ -d "$TARGET_WALLET_DIR" ]]; then
echo ""
echo "Warning: Wallet '$NEW_WALLET_NAME' already exists at: $TARGET_WALLET_DIR"
read -p "Do you want to overwrite it? (yes/no): " CONFIRM
if [[ "$CONFIRM" != "yes" ]]; then
echo "Restoration cancelled."
exit 1
fi
# Try to unload if loaded
echo "Unloading existing wallet..."
curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "unloadwallet", "method": "unloadwallet", "params": ["'$NEW_WALLET_NAME'"]}' \
"http://$RPC_HOST:$RPC_PORT" > /dev/null 2>&1
# Remove existing
echo "Removing existing wallet directory..."
rm -rf "$TARGET_WALLET_DIR"
fi
# Extract backup
echo ""
echo "Extracting backup..."
cd "$DATA_DIR"
tar -xzf "$BACKUP_FILE"
# Rename if necessary
if [[ "$ORIGINAL_WALLET_NAME" != "$NEW_WALLET_NAME" ]]; then
mv "$ORIGINAL_WALLET_NAME" "$NEW_WALLET_NAME"
echo "✓ Wallet renamed from '$ORIGINAL_WALLET_NAME' to '$NEW_WALLET_NAME'"
fi
# Verify extraction
if [[ ! -d "$TARGET_WALLET_DIR" ]]; then
echo "❌ Error: Wallet directory was not created"
exit 1
fi
echo "✓ Wallet files extracted successfully"
# Load wallet
echo ""
echo "Loading wallet into RinCoin node..."
LOAD_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "loadwallet", "method": "loadwallet", "params": ["'$NEW_WALLET_NAME'"]}' \
"http://$RPC_HOST:$RPC_PORT")
if echo "$LOAD_RESPONSE" | grep -q '"error":null'; then
echo "✓ Wallet loaded successfully"
else
echo "Error loading wallet: $LOAD_RESPONSE"
echo ""
echo "The wallet files are restored, but RinCoin couldn't load them."
echo "You may need to restart the RinCoin daemon."
exit 1
fi
# Get wallet info
echo ""
echo "Verifying wallet..."
BALANCE_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "getbalance", "method": "getbalance", "params": []}' \
"http://$RPC_HOST:$RPC_PORT/wallet/$NEW_WALLET_NAME")
BALANCE=$(echo "$BALANCE_RESPONSE" | grep -o '"result":[0-9.]*' | cut -d: -f2)
echo "Current balance: $BALANCE RIN"
echo ""
# Show addresses for verification
echo "Restored addresses:"
ADDRESSES_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "listreceivedbyaddress", "method": "listreceivedbyaddress", "params": [0, true, true]}' \
"http://$RPC_HOST:$RPC_PORT/wallet/$NEW_WALLET_NAME")
echo "$ADDRESSES_RESPONSE" | grep -o '"address":"[^"]*"' | cut -d'"' -f4 | head -5
echo ""
echo "✅ Wallet restored successfully!"
echo ""
echo "📊 Restoration Summary:"
echo " Wallet name: $NEW_WALLET_NAME"
echo " Balance: $BALANCE RIN"
echo " Status: Ready to use"
echo ""
echo "💡 IMPORTANT:"
echo " - All addresses are IDENTICAL to the original wallet"
echo " - Transaction history is fully preserved"
echo " - No blockchain rescan needed"
echo " - This is the PERFECT restoration method!"
echo ""
echo "🔧 Commands:"
echo " Check balance:"
echo " curl -s -u \"$RPC_USER:$RPC_PASSWORD\" -d '{\"jsonrpc\": \"2.0\", \"id\": \"getbalance\", \"method\": \"getbalance\"}' \"http://$RPC_HOST:$RPC_PORT/wallet/$NEW_WALLET_NAME\""
echo ""
echo " List wallets:"
echo " ./list_wallets.sh"