forked from petertodd/replace-by-fee-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
spend-brainwallets-to-fees.py
executable file
·157 lines (122 loc) · 5.54 KB
/
spend-brainwallets-to-fees.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
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
#!/usr/bin/python3
# Copyright (C) 2014 Peter Todd <[email protected]>
#
# This file is subject to the license terms in the LICENSE file found in the
# top-level directory of this distribution.
import argparse
import binascii
import bitcoin
import bitcoin.rpc
import hashlib
import logging
import time
from bitcoin.core import *
from bitcoin.core.script import *
from bitcoin.core.scripteval import *
from bitcoin.wallet import *
known_privkeys_by_scriptPubKey = {}
known_p2sh_redeemScripts = {CScript():CScript([1]),
CScript([1]):CScript()}
known_p2sh_scriptPubKeys = \
{redeemScript.to_p2sh_scriptPubKey():(redeemScript,scriptSig) for redeemScript, scriptSig in known_p2sh_redeemScripts.items()}
def create_spend_to_fees_tx(outpoint, privkey):
txin_scriptPubKey = CScript([OP_DUP, OP_HASH160, Hash160(privkey.pub), OP_EQUALVERIFY, OP_CHECKSIG])
txin = CMutableTxIn(outpoint, nSequence=0)
txout = CMutableTxOut(0, CScript([OP_RETURN]))
tx = CMutableTransaction([txin],[txout])
sigflags = SIGHASH_NONE | SIGHASH_ANYONECANPAY
sighash = SignatureHash(txin_scriptPubKey, tx, 0, sigflags)
sig = privkey.sign(sighash) + bytes([sigflags])
txin.scriptSig = CScript([sig, privkey.pub])
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
return tx
def create_p2sh_spend_to_fees_tx(outpoint, scriptSig, redeemScript):
return CTransaction([CTxIn(outpoint, scriptSig + redeemScript, nSequence=0)],
[CTxOut(0, CScript([OP_RETURN]))])
def scan_tx_for_spendable_outputs(tx, txid):
for (n, txout) in enumerate(tx.vout):
if txout.scriptPubKey in known_privkeys_by_scriptPubKey:
privkey = known_privkeys_by_scriptPubKey[txout.scriptPubKey]
outpoint = COutPoint(txid, n)
yield create_spend_to_fees_tx(outpoint, privkey)
elif txout.scriptPubKey in known_p2sh_scriptPubKeys:
outpoint = COutPoint(txid, n)
redeemScript, scriptSig = known_p2sh_scriptPubKeys[txout.scriptPubKey]
yield create_p2sh_spend_to_fees_tx(outpoint, scriptSig, redeemScript)
parser = argparse.ArgumentParser(description="Spend known secret-key outputs to fees. (e.g. brainwallets)")
parser.add_argument('-v', action='store_true',
dest='verbose',
help='Verbose')
parser.add_argument('-t', action='store_true',
dest='testnet',
help='Enable testnet')
parser.add_argument('-d', action='store', type=float,
dest='delay',
default=10,
help='Delay between mempool scans')
parser.add_argument('-f', action='store', type=str,
dest='privkey_file',
default='known-privkeys',
help='File of known privkeys and passphrases, one per line')
args = parser.parse_args()
logging.root.setLevel('INFO')
if args.verbose:
logging.root.setLevel('DEBUG')
if args.testnet:
bitcoin.SelectParams('testnet')
rpc = bitcoin.rpc.Proxy()
with open(args.privkey_file,'rb') as fd:
def add_privkey(known_privkey):
h = Hash160(known_privkey.pub)
scriptPubKey = CScript([OP_DUP, OP_HASH160, h, OP_EQUALVERIFY, OP_CHECKSIG])
known_privkeys_by_scriptPubKey[scriptPubKey] = known_privkey
logging.info('Known: %s %s' % (b2x(scriptPubKey), b2x(known_privkey.pub)))
n = 0
for l in fd.readlines():
n += 1
l = l.strip()
try:
privkey = CBitcoinSecret(l.decode('utf8'))
add_privkey(privkey)
except bitcoin.base58.Base58ChecksumError:
pass
except bitcoin.base58.InvalidBase58Error:
pass
secret = hashlib.sha256(l).digest()
add_privkey(CBitcoinSecret.from_secret_bytes(secret, False))
add_privkey(CBitcoinSecret.from_secret_bytes(secret, True))
logging.info('Added %d known privkeys/passphrases' % n)
known_txids = set()
while True:
mempool_txids = set(rpc.getrawmempool())
new_txids = mempool_txids.difference(known_txids)
known_txids.update(mempool_txids)
burn_txs = []
for new_txid in new_txids:
try:
new_tx = rpc.getrawtransaction(new_txid)
except IndexError:
continue
# The scriptSigs might not sign vout, in which case we can replace the
# whole thing with OP_RETURN.
if not (len(new_tx.vout) == 1
and new_tx.vout[0].nValue == 0
and new_tx.vout[0].scriptPubKey == CScript([OP_RETURN])):
to_fees_tx = CTransaction(new_tx.vin,
[CTxOut(0, CScript([OP_RETURN]))],
nLockTime=new_tx.nLockTime,
nVersion=new_tx.nVersion)
try:
to_fees_txid = rpc.sendrawtransaction(to_fees_tx, True)
logging.info('Replaced tx %s with all-to-fees %s' % (b2lx(new_txid), b2lx(to_fees_txid)))
except bitcoin.rpc.JSONRPCException as exp:
# Couldn't replace; try spending individual outputs instead.
burn_txs.extend(scan_tx_for_spendable_outputs(new_tx, new_txid))
for burn_tx in burn_txs:
try:
txid = rpc.sendrawtransaction(burn_tx, True)
logging.info('Sent burn tx %s' % b2lx(txid))
except bitcoin.rpc.JSONRPCException as err:
logging.info('Got error %s while sending %s' % (err, b2x(burn_tx.serialize())))
logging.debug('Sleeping %f seconds' % args.delay)
time.sleep(args.delay)