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

128 lines
4.9 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"
set -eo pipefail
if [[ $# -ne 1 ]]; then
echo "Usage: $0 \"<master_private_key>\""
echo "Example: $0 \"xprv9s21ZrQH143K3bjynHVk6hBTZLmV9wjqWScL3UyENBYK6RaFo75zu5jnWQtBi932zKbD7c2WARWLJNjBbE3Td2Cc44ym3dmp343qKKFXwxS\""
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"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
WALLET_NAME="restored_from_seed_${TIMESTAMP}"
# 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 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": ["restored_from_seed"]}' \
"http://$RPC_HOST:$RPC_PORT")
echo "Load response: $LOAD_RESPONSE"
if echo "$LOAD_RESPONSE" | grep -q '"error":null'; then
echo "✓ Existing wallet 'restored_from_seed' loaded successfully"
WALLET_NAME="restored_from_seed"
else
echo "Creating new wallet for seed restoration..."
# Create a new 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, true, "", 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=false (don't replace existing keys)
SEED_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": "sethdseed", "method": "sethdseed", "params": [false, "'$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
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 will automatically detect transactions as it syncs"
echo "- You may need to rescan the blockchain to find old transactions"
echo "- Run: ./rin/wallet/cmd/check_balance.sh to see if funds appear"
echo ""
echo "To rescan blockchain (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\""