#!/usr/bin/env bash # btc_wallet.sh - receiving wallet on the local Bitcoin Core node (snap build). # Usage: ./btc_wallet.sh setup | address | balance | watch | withdraw set -euo pipefail CLI="${BTC_CLI:-bitcoin-core.cli}" W="${BTC_WALLET:-btc402}" LOG="${BTC_WALLET_LOG:-$HOME/btc402/payments.log}" w() { "$CLI" -rpcwallet="$W" "$@"; } case "${1:-}" in setup) if "$CLI" listwallets | grep -q "\"$W\""; then echo "Wallet $W already loaded." elif "$CLI" loadwallet "$W" >/dev/null 2>&1; then echo "Loaded existing wallet $W." else "$CLI" createwallet "$W" >/dev/null; echo "Created new descriptor wallet $W." echo "Back it up now: $CLI -rpcwallet=$W backupwallet ~/btc402/$W-backup.dat"; fi ;; address) BECH=$(w getnewaddress "x402" bech32); LEG=$(w getnewaddress "x402" legacy) echo "bech32: $BECH"; echo "legacy: $LEG" echo "Config line: BTC_PAY_TO=$BECH" ;; balance) echo "Confirmed: $(w getbalance) BTC" w getbalances ;; watch) mkdir -p "$(dirname "$LOG")"; touch "$LOG" echo "Watching wallet $W for incoming payments (Ctrl+C to stop). Log: $LOG" while true; do w listtransactions "*" 50 | python3 -c ' import json,sys,os log=sys.argv[1]; seen=set(l.split()[1] for l in open(log) if l.strip()) for t in json.load(sys.stdin): if t["category"]=="receive" and t["txid"] not in seen: line=f"{t.get(\"time\")} {t[\"txid\"]} {t[\"address\"]} {t[\"amount\"]} BTC conf={t[\"confirmations\"]}" open(log,"a").write(line+"\n"); print("NEW:",line) ' "$LOG" sleep 60 done ;; withdraw) [ $# -eq 3 ] || { echo "Usage: $0 withdraw
"; exit 1; } echo "About to send $3 BTC to $2 from wallet $W." read -r -p "Type YES to confirm: " ok; [ "$ok" = "YES" ] || { echo "Cancelled."; exit 1; } w sendtoaddress "$2" "$3" ;; *) echo "Usage: $0 setup | address | balance | watch | withdraw "; exit 1;; esac