forked from proofchains/python-proofchains
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sus-tool.py
executable file
·279 lines (211 loc) · 9.3 KB
/
sus-tool.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
#
# Copyright (C) 2015 Peter Todd <pete@petertodd.org>
#
# This file is part of python-proofchains.
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-proofchains, including this file, may be copied, modified,
# propagated, or distributed except according to the terms contained in the
# LICENSE file.
"""Single-use seal tool"""
import argparse
import collections
import logging
import bitcoin.rpc
from bitcoin.core import x, lx, b2x, b2lx, CTransaction
from proofchains.core.bitcoin import TxProof, TxInProof, TxOutProof
from proofchains.core.uniquebits.singleuseseal import BitcoinSingleUseSeal, BitcoinSealWitness
from proofchains.uniquebits.singleuseseal import make_close_seal_tx_template
class ParseCOutPointArg(argparse.Action):
@staticmethod
def str_to_COutPoint(str_outpoint, parser):
try:
if str_outpoint.count(':') != 1:
raise ValueError
str_txid, str_n = str_outpoint.split(':')
txid = bitcoin.core.lx(str_txid)
n = int(str_n)
return bitcoin.core.COutPoint(txid, n)
except ValueError as exp:
parser.exit('Bad outpoint %r: %s' % (str_outpoint, exp))
def __call__(self, parser, args, values, option_string=None):
values = self.str_to_COutPoint(values, parser)
setattr(args, self.dest, values)
class ParseDigestArg(argparse.Action):
@staticmethod
def str_to_digest(str_digest, parser):
try:
digest = bytes.fromhex(str_digest)
if len(digest) != 32:
raise ValueError('Length != 32')
return digest
except ValueError as exp:
parser.exit('Bad digest %r: %s' % (str_digest, exp))
def __call__(self, parser, args, values, option_string=None):
values = self.str_to_digest(values, parser)
setattr(args, self.dest, values)
parser = argparse.ArgumentParser(
description="Single-Use-Seal Tool",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-t', action='store_true',
dest='testnet',
help='Enable testnet')
parser.add_argument('-n', action='store_true',
dest='dryrun',
help="Dry-run; don't actually send the transactions")
parser.add_argument("-q","--quiet",action="count",default=0,
help="Be more quiet.")
parser.add_argument("-v","--verbose",action="count",default=0,
help="Be more verbose. Both -v and -q may be used multiple times.")
subparsers = parser.add_subparsers()
mkseal_parser = subparsers.add_parser('mkseal',
help='Make a single-use-seal from an outpoint')
mkseal_parser.add_argument('outpoint', metavar='TXID:N',
action=ParseCOutPointArg,
help='Outpoint')
mkseal_parser.add_argument('seal_fd', type=argparse.FileType('xb'), metavar='FILE',
help='Seal file')
def cmd_mkseal(args):
btc_seal = BitcoinSingleUseSeal(outpoint=args.outpoint)
args.seal_fd.write(btc_seal.serialize())
mkseal_parser.set_defaults(cmd_func=cmd_mkseal)
mkclosetx_parser = subparsers.add_parser('mkclosetx',
help='Make a transaction that would close one or more seals over a digest')
mkclosetx_parser.add_argument('digest', metavar='DIGEST',
action=ParseDigestArg,
help='Hash digest (hex)')
mkclosetx_parser.add_argument('seal_fds', type=argparse.FileType('rb'), metavar='SEAL-FILE',
nargs='+',
help='Seal file(s)')
mkclosetx_meth_group = mkclosetx_parser.add_mutually_exclusive_group()
mkclosetx_meth_group.add_argument('--op-return',
action='store_const', dest='meth', const='op_return',
default='op_return',
help='Close seal with a OP_RETURN output (default)')
mkclosetx_meth_group.add_argument('--p2sh',
action='store_const', dest='meth', const='p2sh',
help='Close seal with a P2SH output')
mkclosetx_meth_group.add_argument('--p2pkh',
action='store_const', dest='meth', const='p2pkh',
help='Close seal with a P2PKH output')
def cmd_mkclosetx(args):
seals = collections.OrderedDict()
for seal_fd in args.seal_fds:
seal = BitcoinSingleUseSeal.deserialize(seal_fd.read())
if seal in seals:
args.parser.error("Duplicate seal: '%s' duplicates '%s'" % \
(seal_fd.name, seals[seal].name))
seals[seal] = seal_fd
logging.debug('Closing seal %s on outpoint %s:%d' % \
(b2x(seal.hash), b2lx(seal.outpoint.hash), seal.outpoint.n))
seals = seals.keys()
close_tx = make_close_seal_tx_template(args.digest, *seals, meth=args.meth)
print(b2x(close_tx.serialize()))
mkclosetx_parser.set_defaults(cmd_func=cmd_mkclosetx)
mkwitness_parser = subparsers.add_parser('mkwitness',
help='Witness the fact that one or more seals were closed')
mkwitness_parser.add_argument('seal_fds', type=argparse.FileType('rb'), metavar='SEAL-FILE',
nargs='+',
help='Seal file(s)')
mkwitness_tx_group = mkwitness_parser.add_mutually_exclusive_group(required=True)
mkwitness_tx_group.add_argument('--txid',
action='store', type=lx,
help='Specify closing tx by txid')
mkwitness_tx_group.add_argument('--tx',
action='store', type=x,
help='Provide closing tx as hex bytes')
def cmd_mkwitness(args):
tx = None
if args.tx is not None:
serialized_tx = args.tx
tx = CTransaction.deserialize(serialized_tx)
else:
tx = args.proxy.getrawtransaction(args.txid)
txproof = TxProof(tx=tx)
for seal_fd in args.seal_fds:
seal = BitcoinSingleUseSeal.deserialize(seal_fd.read())
txinproof = None
txoutproof = None
for i, txin in enumerate(txproof.tx.vin):
if txin.prevout == seal.outpoint:
txinproof = TxInProof(i=i, txproof=txproof)
txoutproof = TxOutProof(i=0, txproof=txproof)
break
else:
args.parser.error("Seal '%s' not closed by this transaction" % seal_fd.name)
witness = BitcoinSealWitness(seal=seal, txinproof=txinproof, txoutproof=txoutproof)
witness_filename = seal_fd.name + '.witness'
logging.info("Creating witness file '%s'" % witness_filename)
with open(seal_fd.name + '.witness', 'xb') as witness_fd:
witness_fd.write(witness.serialize())
mkwitness_parser.set_defaults(cmd_func=cmd_mkwitness)
verifywitness_parser = subparsers.add_parser('verifywitness',
help='Verify a witness to a single-use-seal being closed')
verifywitness_parser.add_argument('witness_fd', metavar='WITNESS-FILE',
type=argparse.FileType('rb'),
help='Witness file')
verifywitness_parser.add_argument('-d', metavar='DIGEST',
dest='digest',
action=ParseDigestArg,
help='Hash digest')
verifywitness_parser.add_argument('--seal-file', metavar='SEAL-FILE',
type=argparse.FileType('rb'),
help='Seal file')
verifywitness_parser.add_argument('--seal-id', metavar='SEAL-ID',
action=ParseDigestArg,
help='Seal hash (hex)')
verifywitness_parser.add_argument('-l','--local',
action='store_true',
help="Only verify local self-consistency; don't attempt to determine if the witness tx is in the chain")
def cmd_verifywitness(args):
witness = BitcoinSealWitness.deserialize(args.witness_fd.read())
# FIXME: implement --local
witness.verify()
if args.digest is not None:
witness.verify_hash(args.digest)
# FIXME: seal and seal hash
verifywitness_parser.set_defaults(cmd_func=cmd_verifywitness)
sealinfo_parser = subparsers.add_parser('sealinfo',
help='Show information about a seal')
sealinfo_parser.add_argument('seal_fd', metavar='SEAL-FILE',
type=argparse.FileType('rb'),
help='Seal file')
def cmd_sealinfo(args):
seal = BitcoinSingleUseSeal.deserialize(args.seal_fd.read())
print('Hash:\t\t%s' % b2x(seal.hash))
print('OutPoint:\t%s:%d' % (b2lx(seal.outpoint.hash), seal.outpoint.n))
sealinfo_parser.set_defaults(cmd_func=cmd_sealinfo)
witnessinfo_parser = subparsers.add_parser('witnessinfo',
help='Show information about a witness')
witnessinfo_parser.add_argument('witness_fd', metavar='WITNESS-FILE',
type=argparse.FileType('rb'),
help='Witness file')
def cmd_witnessinfo(args):
witness = BitcoinSealWitness.deserialize(args.witness_fd.read())
print('Hash:\t\t%s' % b2x(witness.hash))
print('Txid:\t\t%s' % b2lx(witness.txinproof.txproof.txhash))
print('Seal Hash:\t%s' % b2x(witness.seal.hash))
print('Seal OutPoint:\t%s:%d' % (b2x(witness.seal.outpoint.hash), witness.seal.outpoint.n))
witnessinfo_parser.set_defaults(cmd_func=cmd_witnessinfo)
args = parser.parse_args()
args.parser = parser
# Setup logging
args.verbosity = args.verbose - args.quiet
if args.verbosity == 1:
logging.root.setLevel(logging.INFO)
elif args.verbosity >= 2:
logging.root.setLevel(logging.DEBUG)
elif args.verbosity == 0:
logging.root.setLevel(logging.WARNING)
elif args.verbosity <= -1:
logging.root.setLevel(logging.ERROR)
if args.testnet:
bitcoin.SelectParams('testnet')
args.proxy = bitcoin.rpc.Proxy()
if hasattr(args, 'cmd_func'):
args.cmd_func(args)
else:
parser.error('No command specified')