-
Notifications
You must be signed in to change notification settings - Fork 22
/
merge-dust-txs.py
executable file
·74 lines (54 loc) · 2 KB
/
merge-dust-txs.py
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
#!/usr/bin/python
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
from __future__ import absolute_import, division, print_function, unicode_literals
# Script to merge the dust transactions.
import argparse
import random
import sys
import bitcoin
import bitcoin.rpc
from bitcoin.core import COIN, x, b2x, b2lx, str_money_value, CTxIn, CTxOut, CTransaction
from bitcoin.core.script import CScript, OP_RETURN
parser = argparse.ArgumentParser(description='Merge dust txouts')
parser.add_argument('--testnet', action='store_true',
help='Use testnet rather than mainnet')
args = parser.parse_args()
if args.testnet:
bitcoin.SelectParams('testnet')
proxy = bitcoin.rpc.Proxy()
txins = []
prevouts = set()
sum_value_in = 0
line = -1
for l in sys.stdin.readlines():
line += 1
l = l.strip()
try:
tx = CTransaction.deserialize(x(l))
except Exception:
continue
for txin in tx.vin:
try:
txout_info = proxy.gettxout(txin.prevout)
except IndexError:
print('Already spent! line %d, txid %s %d' % \
(line, b2lx(txin.prevout.hash), txin.prevout.n),
file=sys.stderr)
continue
print('line %d: %s %d: %s' % \
(line, b2lx(txin.prevout.hash), txin.prevout.n,
str_money_value(txout_info['txout'].nValue)),
file=sys.stderr)
sum_value_in += txout_info['txout'].nValue
if txin.prevout not in prevouts:
prevouts.add(txin.prevout)
txins.append(txin)
else:
print('Dup! line %d, txid %s %d' % \
(line, b2lx(txin.prevout.hash), txin.prevout.n),
file=sys.stderr)
random.shuffle(txins)
tx = CTransaction(txins, [CTxOut(0, CScript([OP_RETURN]))])
print(b2x(tx.serialize()))
print('Total: %s Size: %d' % (str_money_value(sum_value_in), len(tx.serialize())), file=sys.stderr)