-
Notifications
You must be signed in to change notification settings - Fork 29
/
walletreset.sh
executable file
·147 lines (133 loc) · 5.18 KB
/
walletreset.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
#
# You MUST have jq installed for this to work https://stedolan.github.io/jq/download/
#
# use like: ./Consolidate.sh SUPERNET RJ8q5vbzEiSRNeAu39xYfawuTa9djYEsQK
#
# Thanks Genie for the merge script :)
# https://raw.githubusercontent.com/TheComputerGenie/Misc_Stuff/master/Wallet%20stuff/Consolidate.sh
cd "${BASH_SOURCE%/*}" || exit
date=$(date +%Y-%m-%d:%H:%M:%S)
RESET="\033[0m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
if [[ -z "$1" ]]; then
echo "Specify a chain, fool!"
exit
fi
chain=$1
cli=$(./listclis.sh ${chain})
Addy=$(./printkey.py Radd)
echo "[${chain}] \"fixing\" wallet - ${date}"
# arrays for sent transactions.
declare -a unconfirmed=() confirmed=() tmpunconf=()
# Send the entire balance to ourself with genies script, it will merge any number of utxo given enough time :D
enabled="y"
maxInc="800" MinCheck="1" RawOut="[" OutAmount="0"
maxconf=$(${cli} getblockcount) maxconf=$((maxconf + 1))
txids=() vouts=() amounts=()
SECONDS=0
echo "Finding UTXOS in $maxconf blocks to consolidate ..."
unspents=$(${cli} listunspent $MinCheck $maxconf)
inputUTXOs=$(jq -cr '[map(select(.spendable == true)) | .[] | {txid, vout, amount}]' <<<"${unspents}")
UTXOcount=$(jq -r '.|length' <<<"${inputUTXOs}")
duration=$SECONDS
if (( $UTXOcount == 0 )); then
echo "zero UTXOs found... try again later... "
exit 1
fi
echo "Found $UTXOcount UTXOs.... $(($duration % 60)) seconds"
function makeRaw() {
for ((tc = 0; tc <= $1 - 1; tc++)); do
RawOut2="{\"txid\":\"${txids[tc]}\",\"vout\":${vouts[tc]}},"
RawOut="$RawOut$RawOut2"
OutAmount=$(echo "scale=8; ($OutAmount + ${amounts[tc]})" | bc)
done
OutAmount=$(echo "scale=8; $OutAmount - 0.00001" | bc) OutAmount=${OutAmount/#./0.}
RawOut="${RawOut::-1}" RawOut=$RawOut"] {\"$Addy\":$OutAmount}"
}
function addnlocktime() {
nlocktime=$(printf "%08x" $(date +%s) | dd conv=swab 2>/dev/null | rev)
chophex=$(echo $toSign | sed 's/.\{38\}$//')
nExpiryHeight=$(echo $toSign | grep -o '.\{30\}$')
newhex=$chophex$nlocktime$nExpiryHeight
}
LoopsCount=$(echo "scale=0; ($UTXOcount / $maxInc)" | bc)
echo "This will take $LoopsCount transaction(s) to complete...."
SECONDS=0
for txid in $(jq -r '.[].txid' <<<"${inputUTXOs}"); do txids+=("$txid"); done
duration=$SECONDS
echo "Captured txids... $(($duration % 60)) seconds"
SECONDS=0
for vout in $(jq -r '.[].vout' <<<"${inputUTXOs}"); do vouts+=("$vout"); done
duration=$SECONDS
echo "Captured vouts... $(($duration % 60)) seconds"
SECONDS=0
for amount in $(jq -r '.[].amount' <<<"${inputUTXOs}"); do
if [[ "$amount" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
amounts+=("$amount")
else
amounts+=("$(printf "%.8f" $amount)")
fi
done
duration=$SECONDS
echo "Captured amounts... $(($duration % 60)) seconds"
echo "Packed and ready to begin...."
for ((tlc = 0; tlc <= $LoopsCount; tlc++)); do
echo "${#vouts[@]} UTXOs left to consolitate..."
SECONDS=0
if [[ ${#vouts[@]} -ge $maxInc ]]; then
makeRaw $maxInc
else
makeRaw ${#vouts[@]}
fi
duration=$SECONDS
echo "Created raw consolidated tx $(($duration % 60)) seconds"
#echo $RawOut
SECONDS=0
toSign=$(${cli} createrawtransaction $RawOut)
addnlocktime
Signed=$(${cli} signrawtransaction $newhex | jq -r '.hex')
lasttx=$(echo -e "$Signed" | ${cli} -stdin sendrawtransaction)
echo "Consolidated $(jq '. | length' <<<"${RawOut}") UTXOs:"
duration=$SECONDS
echo "Sent signed raw consolidated tx: $lasttx for $OutAmount $ac_name $(($duration % 60)) seconds"
txids=("${txids[@]:$maxInc}")
vouts=("${vouts[@]:$maxInc}")
amounts=("${amounts[@]:$maxInc}")
RawOut="[" OutAmount="0"
unconfirmed+=("$lasttx")
sleep 10
done
# sort confirmed and unconfirmed and wait until all tx are confirmed before clearing wallet.dat of tx history.
while (( ${#unconfirmed[@]} > 0 )); do
tmpunconf="${unconfirmed[@]}"
unconfirmed=()
for txid in ${tmpunconf[@]}; do
rawtx=$(${cli} getrawtransaction "${txid}" 1 2> /dev/null )
confs=$(jq -r .confirmations <<<"${rawtx}")
rawconfs=$(jq -r .rawconfirmations <<<"${rawtx}")
if [[ "${confs}" == "null" ]]; then
unconfirmed+=("${txid}")
# 1/3 of the time rebroadcast unconfirmed txns
if (( RANDOM % 100 < 33 )); then
txhex=$(jq -r .hex <<<"${rawtx}")
echo "[${chain} Rebroadcasting: $(echo -e "$txhex" | ${cli} -stdin sendrawtransaction)"
fi
# change else below to wait for notarized confirm, using dpowconfs or 10 confs.
# elif (( confs > 1 )) || (( rawconfs > 10 )); then
else
confirmed+=("${txid}")
echo "[${chain}] txid: ${txid} confs: ${confs} rawconfs: ${rawconfs}"
fi
done
# wait between 30-60s before rechecking remaining unconfirmed txns
sleep $(( RANDOM % 30 + 30 ))
echo "[${chain}] Confirmed txns: ${#confirmed[@]} Unconfirmed txns: ${#unconfirmed[@]}"
done
echo "[${chain}] All our txns are confirmed, running wallet cleaner RPC to wipe transaction history..."
${cli} cleanwallettransactions
echo "[${chain}] Start breaking it all over again..."
./utxosplitter.sh ${chain}
echo "[${chain}] wallet fixed!"