Files
mines/rin/wallet/cmd/restore_from_seed.sh
2025-09-29 23:54:32 +03:00

146 lines
5.8 KiB
Bash

#!/bin/bash
# RinCoin Wallet Restoration from Master Seed Script
# Restores a wallet from just the master private key (xprv...)
# Usage: ./restore_from_seed.sh "xprv9s21ZrQH143K3bjynHVk6hBTZLmV9wjqWScL3UyENBYK6RaFo75zu5jnWQtBi932zKbD7c2WARWLJNjBbE3Td2Cc44ym3dmp343qKKFXwxS" [wallet_name]
set -eo pipefail
if [[ $# -lt 1 ]] || [[ $# -gt 2 ]]; then
echo "Usage: $0 \"<master_private_key>\" [wallet_name]"
echo "Example: $0 \"xprv9s21ZrQH143K3bjynHVk6hBTZLmV9wjqWScL3UyENBYK6RaFo75zu5jnWQtBi932zKbD7c2WARWLJNjBbE3Td2Cc44ym3dmp343qKKFXwxS\""
echo "Example: $0 \"xprv9s21ZrQH143K3bjynHVk6hBTZLmV9wjqWScL3UyENBYK6RaFo75zu5jnWQtBi932zKbD7c2WARWLJNjBbE3Td2Cc44ym3dmp343qKKFXwxS\" my_restored_wallet"
echo ""
echo "To get the master key from your backup file:"
echo "grep 'extended private masterkey' ~/rin_wallet_backups/rin_wallet_backup_*.txt"
exit 1
fi
MASTER_KEY="$1"
if [[ $# -eq 2 ]]; then
WALLET_NAME="$2"
else
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
WALLET_NAME="restored_from_seed_${TIMESTAMP}"
fi
# RPC Configuration
RPC_USER="rinrpc"
RPC_PASSWORD="745ce784d5d537fc06105a1b935b7657903cfc71a5fb3b90"
RPC_HOST="localhost"
RPC_PORT="9556"
# Validate master key format
if [[ ! "$MASTER_KEY" =~ ^xprv[a-zA-Z0-9]+ ]]; then
echo "Error: Invalid master key format. Must start with 'xprv'"
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
echo "Checking if wallet '$WALLET_NAME' already exists..."
# First try to load existing wallet
LOAD_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "loadwallet", "method": "loadwallet", "params": ["'$WALLET_NAME'"]}' \
"http://$RPC_HOST:$RPC_PORT")
echo "Load response: $LOAD_RESPONSE"
if echo "$LOAD_RESPONSE" | grep -q '"error":null'; then
echo "✓ Existing wallet '$WALLET_NAME' loaded successfully"
else
echo "Creating new wallet for seed restoration..."
# Create a new HD wallet (same format as main wallet)
CREATE_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "createwallet", "method": "createwallet", "params": ["'$WALLET_NAME'", false, false, "", false, false, true]}' \
"http://$RPC_HOST:$RPC_PORT")
if echo "$CREATE_RESPONSE" | grep -q '"error":null'; then
echo "✓ New wallet '$WALLET_NAME' created successfully"
else
echo "Error creating wallet: $CREATE_RESPONSE"
exit 1
fi
fi
echo "Setting HD seed from master private key..."
# First, try to import the master key directly
IMPORT_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "importprivkey", "method": "importprivkey", "params": ["'$MASTER_KEY'", "", false]}' \
"http://$RPC_HOST:$RPC_PORT/wallet/$WALLET_NAME")
if echo "$IMPORT_RESPONSE" | grep -q '"error":null'; then
echo "✓ Master key imported successfully"
else
echo "Import response: $IMPORT_RESPONSE"
# Alternative: Try sethdseed with newkeypool=true (flush old keys and set new seed)
SEED_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "sethdseed", "method": "sethdseed", "params": [true, "'$MASTER_KEY'"]}' \
"http://$RPC_HOST:$RPC_PORT/wallet/$WALLET_NAME")
if echo "$SEED_RESPONSE" | grep -q '"error":null'; then
echo "✓ HD seed set successfully"
else
echo "Error setting HD seed: $SEED_RESPONSE"
echo "Note: The master key format might not be compatible with this method"
echo "Try using the full wallet dump restoration instead"
fi
fi
# Rescan the blockchain to find existing transactions
echo "Rescanning blockchain to find existing transactions..."
RESCAN_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "rescanblockchain", "method": "rescanblockchain", "params": []}' \
"http://$RPC_HOST:$RPC_PORT/wallet/$WALLET_NAME")
if echo "$RESCAN_RESPONSE" | grep -q '"error":null'; then
echo "✓ Blockchain rescan completed"
else
echo "Warning: Blockchain rescan failed or is still in progress: $RESCAN_RESPONSE"
fi
echo "Generating addresses from seed..."
# Generate some addresses to populate the wallet
for i in {1..10}; do
ADDRESS_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "getnewaddress", "method": "getnewaddress", "params": []}' \
"http://$RPC_HOST:$RPC_PORT/wallet/$WALLET_NAME")
if echo "$ADDRESS_RESPONSE" | grep -q '"error":null'; then
echo " Generated address $i"
else
echo "Warning: Failed to generate address $i"
fi
done
echo ""
echo "✅ Wallet restored from master key successfully!"
echo "Wallet name: $WALLET_NAME"
echo ""
echo "Important notes:"
echo "- The wallet has been restored and blockchain rescan has been initiated"
echo "- It may take several minutes for the rescan to complete and funds to appear"
echo "- Monitor the node logs for rescan progress"
echo "- Run: ./rin/wallet/cmd/check_balance.sh to check current balance"
echo ""
echo "To manually trigger rescan again (if needed):"
echo "curl -s -u \"$RPC_USER:$RPC_PASSWORD\" -H \"Content-Type: application/json\" -d '{\"jsonrpc\": \"2.0\", \"id\": \"rescanblockchain\", \"method\": \"rescanblockchain\", \"params\": []}' \"http://$RPC_HOST:$RPC_PORT/wallet/$WALLET_NAME\""
echo ""
echo "Check balance with:"
echo "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/$WALLET_NAME\""