diff --git a/rin/wallet/cmd/find_address.sh b/rin/wallet/cmd/find_address.sh new file mode 100644 index 0000000..0beae16 --- /dev/null +++ b/rin/wallet/cmd/find_address.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +# Script to find if an address belongs to any loaded wallet +# Usage: ./find_address.sh
+ +if [[ $# -ne 1 ]]; then + echo "Usage: $0
" + echo "Example: $0 rin1qvj0yyt9phvled9kxflju3p687a4s7kareglpk5" + exit 1 +fi + +ADDRESS="$1" + +# RPC Configuration +RPC_USER="rinrpc" +RPC_PASSWORD="745ce784d5d537fc06105a1b935b7657903cfc71a5fb3b90" +RPC_HOST="localhost" +RPC_PORT="9556" + +echo "Searching for address: $ADDRESS" +echo "" + +# First, get list of all loaded wallets +WALLETS_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \ + -H "Content-Type: application/json" \ + -d '{"jsonrpc": "2.0", "id": "listwallets", "method": "listwallets", "params": []}' \ + "http://$RPC_HOST:$RPC_PORT") + +if echo "$WALLETS_RESPONSE" | grep -q '"error":null'; then + echo "Loaded wallets found. Checking each wallet..." + + # Extract wallet names (this is a simple approach, may need refinement) + WALLET_NAMES=$(echo "$WALLETS_RESPONSE" | grep -o '"[^"]*"' | grep -v '"result"' | grep -v '"error"' | grep -v '"id"' | grep -v '"jsonrpc"' | sed 's/"//g') + + if [ -z "$WALLET_NAMES" ]; then + echo "No wallets are currently loaded." + echo "Load a wallet first with: ./rin/wallet/cmd/load_main_wallet.sh" + exit 1 + fi + + FOUND=false + + for wallet in $WALLET_NAMES; do + echo "Checking wallet: $wallet" + + # Check if address belongs to this wallet + VALIDATE_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \ + -H "Content-Type: application/json" \ + -d '{"jsonrpc": "2.0", "id": "validateaddress", "method": "validateaddress", "params": ["'$ADDRESS'"]}' \ + "http://$RPC_HOST:$RPC_PORT/wallet/$wallet") + + if echo "$VALIDATE_RESPONSE" | grep -q '"ismine":true'; then + echo "✓ FOUND! Address belongs to wallet: $wallet" + FOUND=true + + # Get more details + echo "Address details:" + echo "$VALIDATE_RESPONSE" | grep -E '"isvalid"|"ismine"|"iswatchonly"|"isscript"|"pubkey"|"hdkeypath"' + echo "" + fi + done + + if [ "$FOUND" = false ]; then + echo "❌ Address not found in any loaded wallet." + echo "" + echo "The address might be:" + echo "1. In an unloaded wallet" + echo "2. Not belonging to this node" + echo "3. From a different wallet backup" + fi + +else + echo "Error getting wallet list: $WALLETS_RESPONSE" +fi diff --git a/rin/wallet/cmd/list_wallets.sh b/rin/wallet/cmd/list_wallets.sh index 5a2797a..2a06330 100644 --- a/rin/wallet/cmd/list_wallets.sh +++ b/rin/wallet/cmd/list_wallets.sh @@ -24,21 +24,34 @@ RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \ -d "$RPC_REQUEST" \ "http://$RPC_HOST:$RPC_PORT") -# Check for errors -ERROR=$(echo "$RESPONSE" | jq -r '.error' 2>/dev/null) -if [ "$ERROR" != "null" ] && [ -n "$ERROR" ]; then - echo "Error: $(echo "$ERROR" | jq -r '.message')" - exit 1 -fi +# Show raw response for debugging +echo "Raw response: $RESPONSE" +echo "" -# Get wallet list -WALLETS=$(echo "$RESPONSE" | jq -r '.result[]' 2>/dev/null) -if [ -n "$WALLETS" ]; then - echo "Loaded wallets:" - echo "$WALLETS" | while read -r wallet; do - echo " - $wallet" - done +# Check for errors (without jq) +if echo "$RESPONSE" | grep -q '"error":null'; then + echo "No errors in response" + + # Extract wallet names from the result array + # Look for pattern: "result":["wallet1","wallet2"] + WALLET_SECTION=$(echo "$RESPONSE" | grep -o '"result":\[[^]]*\]') + + if [ -n "$WALLET_SECTION" ]; then + # Extract wallet names between quotes + WALLETS=$(echo "$WALLET_SECTION" | grep -o '"[^"]*"' | grep -v '"result"' | sed 's/"//g') + + if [ -n "$WALLETS" ]; then + echo "Loaded wallets:" + echo "$WALLETS" | while read -r wallet; do + echo " - $wallet" + done + else + echo "No wallets are currently loaded." + fi + else + echo "No wallet result found in response." + fi else - echo "No wallets are currently loaded." + echo "Error in response: $RESPONSE" fi diff --git a/rin/wallet/cmd/load_main_wallet.sh b/rin/wallet/cmd/load_main_wallet.sh new file mode 100644 index 0000000..e7590d2 --- /dev/null +++ b/rin/wallet/cmd/load_main_wallet.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Script to load the main wallet +# Usage: ./load_main_wallet.sh + +RPC_USER="rinrpc" +RPC_PASSWORD="745ce784d5d537fc06105a1b935b7657903cfc71a5fb3b90" +RPC_HOST="localhost" +RPC_PORT="9556" + +echo "Loading main wallet..." + +LOAD_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \ + -H "Content-Type: application/json" \ + -d '{"jsonrpc": "2.0", "id": "loadwallet", "method": "loadwallet", "params": ["main"]}' \ + "http://$RPC_HOST:$RPC_PORT") + +echo "Response: $LOAD_RESPONSE" + +if echo "$LOAD_RESPONSE" | grep -q '"error":null'; then + echo "✓ Main wallet loaded successfully" +elif echo "$LOAD_RESPONSE" | grep -q "already loaded"; then + echo "✓ Main wallet is already loaded" +else + echo "Failed to load main wallet" +fi + diff --git a/rin/wallet/cmd/restore_from_seed.sh b/rin/wallet/cmd/restore_from_seed.sh new file mode 100644 index 0000000..9a89f49 --- /dev/null +++ b/rin/wallet/cmd/restore_from_seed.sh @@ -0,0 +1,127 @@ +#!/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 \"\"" + 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\"" diff --git a/rin/wallet/cmd/restore_wallet.sh b/rin/wallet/cmd/restore_wallet.sh index 26cc982..815de78 100644 --- a/rin/wallet/cmd/restore_wallet.sh +++ b/rin/wallet/cmd/restore_wallet.sh @@ -2,20 +2,25 @@ # RinCoin Wallet Restoration Script # Restores a wallet from a dump file on a new RinCoin node. -# Prerequisites: New RinCoin node running in Docker, backup file available. +# Prerequisites: RinCoin node running, backup file available. -set -euo pipefail +set -eo pipefail -if [[ $# -ne 1 ]]; then - echo "Usage: $0 " +if [[ $# -lt 1 ]] || [[ $# -gt 2 ]]; then + echo "Usage: $0 [wallet_name]" echo "Example: $0 ~/rin_wallet_backups/rin_wallet_backup_20230923.txt" + echo "Example: $0 ~/rin_wallet_backups/rin_wallet_backup_20230923.txt my_restored_wallet" exit 1 fi BACKUP_FILE="$1" -CONTAINER="rincoin-node" -WALLET_NAME="main" # Will be renamed to avoid conflicts -NEW_WALLET_NAME="restored_main" +NEW_WALLET_NAME="${2:-restored_main}" # Use provided name or default + +# RPC Configuration +RPC_USER="rinrpc" +RPC_PASSWORD="745ce784d5d537fc06105a1b935b7657903cfc71a5fb3b90" +RPC_HOST="localhost" +RPC_PORT="9556" # Verify backup file exists if [[ ! -f "$BACKUP_FILE" ]]; then @@ -23,41 +28,63 @@ if [[ ! -f "$BACKUP_FILE" ]]; then exit 1 fi -# Check if container is running -if ! sudo docker ps --format '{{.Names}}' | grep -q "^${CONTAINER}$"; then - echo "Error: ${CONTAINER} container is not running. Start it with 'sudo docker start ${CONTAINER}'." +# 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 "Stopping RinCoin node for safe restoration..." -sudo docker stop "$CONTAINER" +# Copy backup file to daemon's data directory +TIMESTAMP=$(date +%Y%m%d_%H%M%S) +DAEMON_BACKUP_FILE="/data/restore_backup_${TIMESTAMP}.txt" +SYSTEM_BACKUP_FILE="/mnt/data/docker_vol/rincoin/rincoin-node/data/restore_backup_${TIMESTAMP}.txt" -echo "Copying backup file into container..." -sudo docker cp "$BACKUP_FILE" "$CONTAINER:/tmp/wallet_backup.txt" +echo "Copying backup file to daemon directory..." +cp "$BACKUP_FILE" "$SYSTEM_BACKUP_FILE" -echo "Starting RinCoin node..." -sudo docker start "$CONTAINER" - -# Wait for RPC to be ready -echo "Waiting for node to fully start..." -sleep 10 +echo "Backup file copied to: $DAEMON_BACKUP_FILE" echo "Creating new wallet and importing keys..." + # Create a new wallet to avoid conflicts -sudo docker exec "$CONTAINER" rincoin-cli -datadir=/data -conf=/data/rincoin.conf createwallet "$NEW_WALLET_NAME" +CREATE_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \ + -H "Content-Type: application/json" \ + -d '{"jsonrpc": "2.0", "id": "createwallet", "method": "createwallet", "params": ["'$NEW_WALLET_NAME'", false, false, "", false, false, true]}' \ + "http://$RPC_HOST:$RPC_PORT") + +if echo "$CREATE_RESPONSE" | grep -q '"error":null'; then + echo "✓ New wallet '$NEW_WALLET_NAME' created successfully" +else + echo "Error creating wallet: $CREATE_RESPONSE" + exit 1 +fi # Import the dump file -sudo docker exec "$CONTAINER" rincoin-cli -datadir=/data -conf=/data/rincoin.conf -rpcwallet="$NEW_WALLET_NAME" importwallet /tmp/wallet_backup.txt +echo "Importing wallet from backup file..." +IMPORT_RESPONSE=$(curl -s -u "$RPC_USER:$RPC_PASSWORD" \ + -H "Content-Type: application/json" \ + -d '{"jsonrpc": "2.0", "id": "importwallet", "method": "importwallet", "params": ["'$DAEMON_BACKUP_FILE'"]}' \ + "http://$RPC_HOST:$RPC_PORT/wallet/$NEW_WALLET_NAME") + +if echo "$IMPORT_RESPONSE" | grep -q '"error":null'; then + echo "✓ Wallet imported successfully" +else + echo "Error importing wallet: $IMPORT_RESPONSE" + exit 1 +fi # Clean up -sudo docker exec "$CONTAINER" rm /tmp/wallet_backup.txt +echo "Cleaning up temporary files..." +rm -f "$SYSTEM_BACKUP_FILE" echo "✅ Wallet restored successfully!" echo "New wallet name: $NEW_WALLET_NAME" echo "" echo "Verify with:" -echo "sudo docker exec $CONTAINER rincoin-cli -datadir=/data -conf=/data/rincoin.conf -rpcwallet=$NEW_WALLET_NAME getbalance" +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/$NEW_WALLET_NAME\"" echo "" -echo "To use this wallet, update scripts to use -rpcwallet=$NEW_WALLET_NAME" -echo "Or rename it back: unloadwallet $NEW_WALLET_NAME && loadwallet $WALLET_NAME" +echo "Or check balance with:" +echo "./rin/wallet/cmd/check_balance.sh" +echo "" +echo "To use this wallet as default, update scripts to use wallet name: $NEW_WALLET_NAME" diff --git a/rin/wallet/cmd/simple_restore.sh b/rin/wallet/cmd/simple_restore.sh new file mode 100644 index 0000000..5b059b7 --- /dev/null +++ b/rin/wallet/cmd/simple_restore.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Simple seed restoration +MASTER_KEY="$1" +TIMESTAMP=$(date +%Y%m%d_%H%M%S) +WALLET_NAME="seed_restore_${TIMESTAMP}" + +echo "Creating wallet: $WALLET_NAME" +echo "Master key: $MASTER_KEY" + +curl -s -u "rinrpc:745ce784d5d537fc06105a1b935b7657903cfc71a5fb3b90" \ + -H "Content-Type: application/json" \ + -d "{\"jsonrpc\": \"2.0\", \"id\": \"createwallet\", \"method\": \"createwallet\", \"params\": [\"$WALLET_NAME\", false, true, \"\", false, false, true]}" \ + "http://localhost:9556" + +echo "" +echo "Setting seed..." + +curl -s -u "rinrpc:745ce784d5d537fc06105a1b935b7657903cfc71a5fb3b90" \ + -H "Content-Type: application/json" \ + -d "{\"jsonrpc\": \"2.0\", \"id\": \"sethdseed\", \"method\": \"sethdseed\", \"params\": [true, \"$MASTER_KEY\"]}" \ + "http://localhost:9556/wallet/$WALLET_NAME" + +echo "" +echo "Done! Wallet name: $WALLET_NAME" diff --git a/test_list.sh b/test_list.sh new file mode 100644 index 0000000..1e0e67e --- /dev/null +++ b/test_list.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +echo "Testing wallet list..." + +RESPONSE=$(curl -s -u "rinrpc:745ce784d5d537fc06105a1b935b7657903cfc71a5fb3b90" \ + -H "Content-Type: application/json" \ + -d '{"jsonrpc": "2.0", "id": "listwallets", "method": "listwallets", "params": []}' \ + "http://localhost:9556") + +echo "Raw response:" +echo "$RESPONSE" +echo "" + +echo "Checking if jq is available:" +which jq || echo "jq not found" + +echo "" +echo "Trying to parse without jq:" +echo "$RESPONSE" | grep -o '"[^"]*"' | grep -v '"result"' | grep -v '"error"' | grep -v '"id"' | grep -v '"jsonrpc"'