-
Notifications
You must be signed in to change notification settings - Fork 5
/
consolidate.sh
executable file
·155 lines (142 loc) · 6.61 KB
/
consolidate.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
148
149
150
151
152
153
154
155
#!/bin/bash
#Copyright Alex English January 2020, adjusted for consolidation by Oink, December 2021.
#This script comes with no warranty whatsoever. Use at your own risk.
#This script looks for unspent transactions below the supplied limit (if none supplied smaller than 2500) and spends them back to the same address. If there are multiple UTXOs on an address, this consolidates them into one output. Privacy is preserved because this
#doesn't comingle any addresses. Furthermore, the option is given to allow for a random delay of 5 to 15 minutes between transaction submissions, so the transactions don't show up as a burst, but are metered over time, likely no more than one per block.
#The standard minimum amount of UTXOs being consolidated is 5, but can be altered using the -np command line option
#The maximum amount of UTXOs being consolidated on a single address is 400.
#Usage: ./consolidator.sh [-max || --maximum-size] [-np || --noprivacy]
#maxvalue is the upper limit of the UTXO sizes to consolidate. If not provided a standard value of 2500 is used.
#Unless -np||--no-privacy a delay is used between addresses to increase privacy. If false is passed, all actions will be performed without delay, finishing quickly, but also creating the possibility of correlating the addresses based on time.
if ! source "$( dirname "${BASH_SOURCE[0]}" )"/config; then
echo "Failed to source config file. Please make sure you have the whole VerusExtras repo or at least also have the config file."
exit 1
fi
#Dependencies: jq (command-line json parser/editor), bc (command-line calculator)
if ! command -v jq &>/dev/null ; then
echo "jq not found. please install using your package manager."
exit 1
fi
if ! command -v bc &>/dev/null ; then
echo "bc not found. please install using your package manager."
exit 1
fi
#set defaults
SENTTRANSACTION=""
USEDELAY=true
LIMIT=2500
MIN=5
DB=$(mktemp -d)
CONFS=$($VERUS getblockcount)
#process command line parameters
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-max|--maximum-size)
LIMIT=$2
shift # past argument
shift # past value
;;
-np|--no-privacy)
USEDELAY=""
shift # past argument
;;
-mu|--minimum-utxos)
MIN=$2
shift # past argument
shift # past value
;;
-h|--help)
printf "\nUsage:\n\nconsolidator.sh [options]\n\n"
printf "Options:\n\n"
printf "\t-max #\t--maximum-size # :\tThe maximum UTXO size to include in the consolidation. (default 2500)\n"
printf "\t-np\t--no-privacy :\t\tDo not delay between consolidating multiple addresses, finishing quickly, but also creating the possibility of correlating the addresses based on time.\n"
printf "\t-mu #\t--minimum-utxos # :\tThe minimum number of UTXOs to include in the consolidation. (default 5)\n"
printf "\nExamples:\n\n"
printf "consolidator.sh -max 1000\t\tConsolidate all UTXOs containing less than 1000 into a single UTXO, using the standard privacy enhancing delay\n"
printf "consolidator.sh -np\t\t\tConsolidate all UTXOs containing less than 2500 into a single UTXO, fast, with no regards to privacy accross addresses\n"
printf "consolidator.sh -np -max 25 -mu 10\t\tConsolidate all UTXOs (at least 10) containing less than 25 into a single UTXO, fast, with no regards to privacy accross addresses\n"
printf "\n"
exit 0
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
#listunspent, filter for limit amout
$VERUS listunspent 1 $CONFS | jq -cr --arg LIMIT "$LIMIT" ".[]|select(.amount<${LIMIT})|.address+\"\t\"+(.confirmations|tostring)+\"\t\"+.txid+\"\t\"+(.vout|tostring)+\"\t\"+(.amount|tostring)" | \
while read L; do
#for each, look up the block - if it is minted the utxo is staked
CONF=$(awk '{print $2}' <<< "$L")
#append txid and vout to file named for the address in $DB/
ADDR=$(awk '{print $1}' <<<"$L")
printf "$L\n" >> "$DB/$ADDR"
done
#format of lines in address files is address, confirmations, txid, vout
for F in `ls -1A $DB`; do
#random delay from 5 minutes to 15 minutes
ADDR="$F"
INPUTS='['
AMOUNT=0
UTXOS=$(sed -n "$=" "$DB/$F")
COUNTER=0
TXCOUNTER=0
if [ $UTXOS -ge $MIN ]; then
if [ "$USEDELAY" ] && [ "$SENTTRANSACTION" ]; then
DELAY=$((300+RANDOM%600))
date
echo "Using delay for privacy - sleeping $DELAY seconds"
sleep $DELAY
printf "\n"
fi
while read L; do
#build transaction inputs
TXID=$(awk '{print $3}' <<<"$L")
VOUT=$(awk '{print $4}' <<<"$L")
INPUTS="$INPUTS{\"txid\":\"$TXID\",\"vout\":$VOUT},"
INAMOUNT=$(awk '{print $5}' <<<"$L")
AMOUNT=$(bc<<<"$AMOUNT+$INAMOUNT")
((COUNTER++))
if [[ "$COUNTER" == '250' ]]; then
INPUTS="${INPUTS%,}]"
OUTAMOUNT=$(printf '%0.8f' $(bc<<<"$AMOUNT-$DEFAULT_FEE"))
OUTPUTS="{\"$ADDR\":$OUTAMOUNT}"
echo "TX $TXCOUNTER: Consolidating and moving $OUTAMOUNT on address $ADDR into a single UTXO"
echo "$((TXCOUNTER*250 + COUNTER)) of $UTXOS UTXOs are being processed."
#createrawtransaction
TXHEX="$($VERUS createrawtransaction "$INPUTS" "$OUTPUTS")"
#signrawtransaction
SIGNEDTXHEX="$($VERUS signrawtransaction "$TXHEX" | jq -r '.hex')"
# sendrawtransaction
SENTTXID="$($VERUS sendrawtransaction "$SIGNEDTXHEX")"
echo "TXID: $SENTTXID"
SENTTRANSACTION=true
INPUTS='['
AMOUNT=0
COUNTER=0
((TXCOUNTER++))
fi
done < "$DB/$F"
INPUTS="${INPUTS%,}]"
OUTAMOUNT=$(printf '%0.8f' $(bc<<<"$AMOUNT-$DEFAULT_FEE"))
OUTPUTS="{\"$ADDR\":$OUTAMOUNT}"
echo "TX $TXCOUNTER: Consolidating and moving $OUTAMOUNT on address $ADDR into a single UTXO"
echo "$((TXCOUNTER*250 + COUNTER)) of $UTXOS UTXOs are being processed."
#createrawtransaction
TXHEX="$($VERUS createrawtransaction "$INPUTS" "$OUTPUTS")"
#signrawtransaction
SIGNEDTXHEX="$($VERUS signrawtransaction "$TXHEX" | jq -r '.hex')"
#sendrawtransaction
SENTTXID="$($VERUS sendrawtransaction "$SIGNEDTXHEX")"
echo "TXID: $SENTTXID"
printf "\n"
SENTTRANSACTION=true
else
echo "$UTXOS UTXOs in $ADDR below $LIMIT, nothing to do..."
fi
done
rm -rf "$DB"
echo "Consolidation completed"