-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathresetwallet
More file actions
executable file
·166 lines (144 loc) · 5.83 KB
/
resetwallet
File metadata and controls
executable file
·166 lines (144 loc) · 5.83 KB
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
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
#
# Reset KMD way
#
# Usage: ./resetwallet <COINNAME> <resettype (optional) (1=resettooldest|2=zimport)>
# e.g. ./resetwallet KMD 2 will use the zimport reset method
#
# resettooldest will not send any transactions and only delete wallet and rescan from the oldest tx in the wallet
#
# @author webworker01
#
scriptpath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source ${scriptpath}/main
# @see functions
setupCoinVars $1 "resetwallet"
if [[ $processid == "" ]]; then
log "resetwallet" "pid not found, cannot continue" "red"
exit 1
fi
resettooldest=0
zimport=0
if [[ $2 == "1" ]]; then
resettooldest=1
elif [[ $2 == "2" ]]; then
zimport=1
fi
sapling=$(isSapling $coin)
privkey=$($komodocli $asset dumpprivkey $nn_address)
if (( resettooldest < 1 )); then
#unlock all tx
lockedtx=$($komodocli $asset listlockunspent | jq -c .)
$komodocli $asset lockunspent true "${lockedtx}"
#send complete balance to self
unspent=$($komodocli $asset listunspent | jq '[.[] | select(.spendable==true)]')
if [[ $coin == "KMD" ]]; then
interest=$(jq -r '[.[].interest] | add' <<< $unspent)
if [[ "$interest" != "null" ]]; then
interest=$( printf "%.8f" $interest )
else
interest=0
fi
setnlocktime=1
else
interest=0
setnlocktime=0
fi
consolidatethese=$(jq -rc '[.[] | {"txid":.txid, "vout":.vout}]' <<< $unspent)
consolidatetheselength=$(jq -r '. | length' <<< $unspent)
consolidatethesetxfee=$(calcFee ${consolidatetheselength} 1 "p2pkh" "p2pkh" $feepkb)
consolidateamount=$(jq -r '[.[].amount] | add' <<< $unspent)
consolidateamountfixed=$( printf "%.8f" $(bc -l <<< "(${consolidateamount}+${interest}-${consolidatethesetxfee})") )
rawtxresult=$($komodocli $asset createrawtransaction "${consolidatethese}" '''{ "'$nn_address'": '$consolidateamountfixed' }''')
txid=$(sendRaw ${rawtxresult} ${coin} ${setnlocktime})
log "resetwallet" "${coin} - balance sent $consolidateamountfixed - interest: $interest - txid : $txid"
i=0
confirmations=0
while [ "$confirmations" -eq "0" ]; do
sleep 10
confirmations=$($komodocli $asset gettransaction $txid | jq .confirmations)
i=$((i+1))
${komodocli} ${asset} sendrawtransaction $($komodocli $asset getrawtransaction ${txid}) > /dev/null
log "resetwallet" "${coin} - sendbalance - waiting for confirmation $confirmations ($i) - ${txid}" "red" "echo"
done
blockhash=$($komodocli $asset gettransaction $txid | jq -r .blockhash)
scanfrom=$($komodocli $asset getblock $blockhash | jq .height)
#scan a bit further back to account for immature mining rewards
if [[ $coin == "KMD" ]]; then
scanfrom=$(( scanfrom - 100 ))
fi
#send feeder tx
if [[ $coin == "KMD" ]]; then
txid2=$($komodocli sendtoaddress $nn_address 5)
log "resetwallet" "${coin} - send feeder UTXO of 5: $txid2"
i=0
confirmations=0
while [ "$confirmations" -eq "0" ]; do
sleep 10
confirmations=$($komodocli gettransaction $txid2 | jq .confirmations)
i=$((i+1))
${komodocli} ${asset} sendrawtransaction $($komodocli $asset getrawtransaction ${txid2}) > /dev/null
log "resetwallet" "${coin} - send feeder - waiting for confirmation $confirmations ($i) - ${txid2}" "red" "echo"
done
fi
sleep 10
#split utxos
if [[ $coin == "KMD" ]]; then
splitter=$($scriptpath/splitfunds $coin $kmdutxoqty)
else
splitter=$($scriptpath/splitfunds $coin $acutxoqty)
fi
if [[ $splitter == "Error! Failed signing tx :((" ]] || [[ $splitter == "Error! No UTXOs to split :((" ]]; then
log "resetwallet" "${coin} - splitter: $splitter"
exit 1
fi
log "resetwallet" "${coin} - splitter txid: $splitter"
i=0
confirmations=0
while [ -z $confirmations ] || [ "$confirmations" == "null" ] || [ "$confirmations" -eq "0" ]; do
sleep 10
confirmations=$(${komodocli} ${asset} getrawtransaction $splitter 1 | jq .confirmations)
i=$((i+1))
${komodocli} ${asset} sendrawtransaction $($komodocli $asset getrawtransaction ${splitter}) > /dev/null
log "resetwallet" "${coin} - send split - waiting for confirmation $confirmations ($i) - ${splitter}" "red" "echo"
done
else
#reset to oldest utxo
blockcount=$($komodocli $asset getblockcount)
oldestutxo=$($komodocli $asset listunspent | jq 'sort_by(.confirmations)[-1] | .rawconfirmations')
scanfrom=$(( blockcount - oldestutxo ))
fi
if (( zimport > 0 )); then
tempaddress=$(${komodocli} ${asset} z_getnewaddress)
tempkey=$(${komodocli} ${asset} z_exportkey $tempaddress)
log "resetwallet" "${coin} Temp Address: ${tempaddress} Key: ${tempkey}"
fi
log "resetwallet" "${coin} - killing daemon - scanfrom: ${scanfrom}"
${scriptpath}/stop ${coin}
i=0
while ps -p $processid > /dev/null; do
i=$((i+1))
log "resetwallet" "${coin} - Waiting for daemon [${processid}] stop ($i)" "red" "echo"
sleep 1
done
sleep 15
mv $datadir/wallet.dat $datadir/wallet.dat.old
#restart the daemon
log "resetwallet" "${coin} - Restart daemon"
${scriptpath}/start ${coin}
i=0
while ! $komodocli $asset getinfo >/dev/null 2>&1; do
i=$((i+1))
log "resetwallet" "${coin} - Waiting for daemon start ($i)" "red" "echo"
sleep 1
done
#Import the keys in the new wallet
if (( zimport < 1 )); then
importedaddress=$($komodocli $asset importprivkey $privkey "" true $scanfrom)
else
importedaddress=$($komodocli $asset importprivkey $privkey "" false)
results=$(${komodocli} ${asset} z_importkey "${tempkey}" yes $scanfrom)
fi
log "resetwallet" "${coin} - Imported address -> $importedaddress at height $scanfrom"
newbalance=$($komodocli $asset getbalance)
log "resetwallet" "${coin} - Done! Balance $newbalance"