tx.py 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  5. # Copyright (C)2013-2018 The MMGen Project <mmgen@tuta.io>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. tx.py: Transaction routines for the MMGen suite
  21. """
  22. import sys,os
  23. from stat import *
  24. from binascii import unhexlify
  25. from mmgen.common import *
  26. from mmgen.obj import *
  27. pnm = g.proj_name
  28. wmsg = {
  29. 'addr_in_addrfile_only': """
  30. Warning: output address {mmgenaddr} is not in the tracking wallet, which means
  31. its balance will not be tracked. You're strongly advised to import the address
  32. into your tracking wallet before broadcasting this transaction.
  33. """.strip(),
  34. 'addr_not_found': """
  35. No data for {pnm} address {mmgenaddr} could be found in either the tracking
  36. wallet or the supplied address file. Please import this address into your
  37. tracking wallet, or supply an address file for it on the command line.
  38. """.strip(),
  39. 'addr_not_found_no_addrfile': """
  40. No data for {pnm} address {mmgenaddr} could be found in the tracking wallet.
  41. Please import this address into your tracking wallet or supply an address file
  42. for it on the command line.
  43. """.strip(),
  44. 'non_mmgen_inputs': """
  45. NOTE: This transaction includes non-{pnm} inputs, which makes the signing
  46. process more complicated. When signing the transaction, keys for non-{pnm}
  47. inputs must be supplied to '{pnl}-txsign' in a file with the '--keys-from-file'
  48. option.
  49. Selected non-{pnm} inputs: {{}}
  50. """.strip().format(pnm=pnm,pnl=pnm.lower()),
  51. 'not_enough_coin': """
  52. Selected outputs insufficient to fund this transaction ({{}} {} needed)
  53. """.strip().format(g.coin),
  54. 'no_change_output': """
  55. ERROR: No change address specified. If you wish to create a transaction with
  56. only one output, specify a single output address with no {} amount
  57. """.strip().format(g.coin),
  58. }
  59. def strfmt_locktime(num,terse=False):
  60. # Locktime itself is an unsigned 4-byte integer which can be parsed two ways:
  61. #
  62. # If less than 500 million, locktime is parsed as a block height. The transaction can be
  63. # added to any block which has this height or higher.
  64. # MMGen note: s/this height or higher/a higher block height/
  65. #
  66. # If greater than or equal to 500 million, locktime is parsed using the Unix epoch time
  67. # format (the number of seconds elapsed since 1970-01-01T00:00 UTC). The transaction can be
  68. # added to any block whose block time is greater than the locktime.
  69. if num >= 5 * 10**6:
  70. return ' '.join(time.strftime('%c',time.gmtime(num)).split()[1:])
  71. elif num > 0:
  72. return '{}{}'.format(('block height ','')[terse],num)
  73. elif num == None:
  74. return '(None)'
  75. else:
  76. die(2,"'{}': invalid locktime value!".format(num))
  77. def select_unspent(unspent,prompt):
  78. while True:
  79. reply = my_raw_input(prompt).strip()
  80. if reply:
  81. selected = AddrIdxList(fmt_str=','.join(reply.split()),on_fail='return')
  82. if selected:
  83. if selected[-1] <= len(unspent):
  84. return selected
  85. msg('Unspent output number must be <= {}'.format(len(unspent)))
  86. def mmaddr2coinaddr(mmaddr,ad_w,ad_f):
  87. # assume mmaddr has already been checked
  88. coin_addr = ad_w.mmaddr2coinaddr(mmaddr)
  89. if not coin_addr:
  90. if ad_f:
  91. coin_addr = ad_f.mmaddr2coinaddr(mmaddr)
  92. if coin_addr:
  93. msg(wmsg['addr_in_addrfile_only'].format(mmgenaddr=mmaddr))
  94. if not keypress_confirm('Continue anyway?'):
  95. sys.exit(1)
  96. else:
  97. die(2,wmsg['addr_not_found'].format(pnm=pnm,mmgenaddr=mmaddr))
  98. else:
  99. die(2,wmsg['addr_not_found_no_addrfile'].format(pnm=pnm,mmgenaddr=mmaddr))
  100. return CoinAddr(coin_addr)
  101. def segwit_is_active(exit_on_error=False):
  102. d = g.rpch.getblockchaininfo()
  103. if d['chain'] == 'regtest':
  104. return True
  105. if 'segwit' in d['bip9_softforks'] and d['bip9_softforks']['segwit']['status'] == 'active':
  106. return True
  107. if g.skip_segwit_active_check:
  108. return True
  109. if exit_on_error:
  110. die(2,'Segwit not active on this chain. Exiting')
  111. else:
  112. return False
  113. def bytes2int(hex_bytes):
  114. r = hexlify(unhexlify(hex_bytes)[::-1])
  115. if r[0] in '89abcdef':
  116. die(3,"{}: Negative values not permitted in transaction!".format(hex_bytes))
  117. return int(r,16)
  118. def bytes2coin_amt(hex_bytes):
  119. return g.proto.coin_amt(bytes2int(hex_bytes) * g.proto.coin_amt.min_coin_unit)
  120. def scriptPubKey2addr(s):
  121. if len(s) == 50 and s[:6] == '76a914' and s[-4:] == '88ac':
  122. return g.proto.pubhash2addr(s[6:-4],p2sh=False)
  123. elif len(s) == 46 and s[:4] == 'a914' and s[-2:] == '87':
  124. return g.proto.pubhash2addr(s[4:-2],p2sh=True)
  125. elif len(s) == 44 and s[:4] == g.proto.witness_vernum_hex + '14':
  126. return g.proto.pubhash2bech32addr(s[4:])
  127. else:
  128. raise NotImplementedError,'Unknown scriptPubKey ({})'.format(s)
  129. from collections import OrderedDict
  130. class DeserializedTX(OrderedDict,MMGenObject): # need to add MMGen types
  131. def __init__(self,txhex):
  132. tx = list(unhexlify(txhex))
  133. tx_copy = tx[:]
  134. d = { 'raw_tx':'' }
  135. def hshift(l,n,reverse=False,skip=False):
  136. ret = l[:n]
  137. if not skip: d['raw_tx'] += ''.join(ret)
  138. del l[:n]
  139. return hexlify(''.join(ret[::-1] if reverse else ret))
  140. # https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers
  141. # For example, the number 515 is encoded as 0xfd0302.
  142. def readVInt(l,skip=False,sub_null=False):
  143. s = int(hexlify(l[0]),16)
  144. bytes_len = 1 if s < 0xfd else 2 if s == 0xfd else 4 if s == 0xfe else 8
  145. if bytes_len != 1: del l[0]
  146. ret = int(hexlify(''.join(l[:bytes_len][::-1])),16)
  147. if sub_null: d['raw_tx'] += '\0'
  148. elif not skip: d['raw_tx'] += ''.join(l[:bytes_len])
  149. del l[:bytes_len]
  150. return ret
  151. d['version'] = bytes2int(hshift(tx,4))
  152. has_witness = tx[0] == '\x00'
  153. if has_witness:
  154. u = hshift(tx,2,skip=True)[2:]
  155. if u != '01':
  156. die(2,"'{}': Illegal value for flag in transaction!".format(u))
  157. del tx_copy[-len(tx)-2:-len(tx)]
  158. d['num_txins'] = readVInt(tx)
  159. d['txins'] = MMGenList([OrderedDict((
  160. ('txid', hshift(tx,32,reverse=True)),
  161. ('vout', bytes2int(hshift(tx,4))),
  162. ('scriptSig', hshift(tx,readVInt(tx,sub_null=True),skip=True)),
  163. ('nSeq', hshift(tx,4,reverse=True))
  164. )) for i in range(d['num_txins'])])
  165. d['num_txouts'] = readVInt(tx)
  166. d['txouts'] = MMGenList([OrderedDict((
  167. ('amount', bytes2coin_amt(hshift(tx,8))),
  168. ('scriptPubKey', hshift(tx,readVInt(tx)))
  169. )) for i in range(d['num_txouts'])])
  170. for o in d['txouts']:
  171. o['address'] = scriptPubKey2addr(o['scriptPubKey'])
  172. d['witness_size'] = 0
  173. if has_witness:
  174. # https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
  175. # A non-witness program (defined hereinafter) txin MUST be associated with an empty
  176. # witness field, represented by a 0x00.
  177. del tx_copy[-len(tx):-4]
  178. wd,tx = tx[:-4],tx[-4:]
  179. d['witness_size'] = len(wd) + 2 # add marker and flag
  180. for i in range(len(d['txins'])):
  181. if hexlify(wd[0]) == '00':
  182. hshift(wd,1,skip=True)
  183. continue
  184. d['txins'][i]['witness'] = [
  185. hshift(wd,readVInt(wd,skip=True),skip=True) for item in range(readVInt(wd,skip=True))
  186. ]
  187. if wd:
  188. die(3,'More witness data than inputs with witnesses!')
  189. d['lock_time'] = bytes2int(hshift(tx,4))
  190. d['txid'] = hexlify(sha256(sha256(''.join(tx_copy)).digest()).digest()[::-1])
  191. d['unsigned_hex'] = hexlify(d['raw_tx'])
  192. del d['raw_tx']
  193. keys = 'txid','version','lock_time','witness_size','num_txins','txins','num_txouts','txouts','unsigned_hex'
  194. return OrderedDict.__init__(self, ((k,d[k]) for k in keys))
  195. txio_attrs = {
  196. 'vout': MMGenListItemAttr('vout',int,typeconv=False),
  197. 'amt': MMGenImmutableAttr('amt',g.proto.coin_amt,typeconv=False), # require amt to be of proper type
  198. 'label': MMGenListItemAttr('label','TwComment',reassign_ok=True),
  199. 'mmid': MMGenListItemAttr('mmid','MMGenID'),
  200. 'addr': MMGenImmutableAttr('addr','CoinAddr'),
  201. 'confs': MMGenListItemAttr('confs',int,typeconv=True), # long confs exist in the wild, so convert
  202. 'txid': MMGenListItemAttr('txid','CoinTxID'),
  203. 'have_wif': MMGenListItemAttr('have_wif',bool,typeconv=False,delete_ok=True)
  204. }
  205. class MMGenTX(MMGenObject):
  206. ext = 'rawtx'
  207. raw_ext = 'rawtx'
  208. sig_ext = 'sigtx'
  209. txid_ext = 'txid'
  210. desc = 'transaction'
  211. class MMGenTxInput(MMGenListItem):
  212. for k in txio_attrs: locals()[k] = txio_attrs[k] # in lieu of inheritance
  213. scriptPubKey = MMGenListItemAttr('scriptPubKey','HexStr')
  214. sequence = MMGenListItemAttr('sequence',(int,long)[g.platform=='win'],typeconv=False)
  215. class MMGenTxOutput(MMGenListItem):
  216. for k in txio_attrs: locals()[k] = txio_attrs[k]
  217. is_chg = MMGenListItemAttr('is_chg',bool,typeconv=False)
  218. class MMGenTxInputList(list,MMGenObject):
  219. desc = 'transaction inputs'
  220. member_type = 'MMGenTxInput'
  221. def convert_coin(self,verbose=False):
  222. from mmgen.protocol import CoinProtocol
  223. io = getattr(MMGenTX,self.member_type)
  224. if verbose:
  225. msg('{}:'.format(self.desc.capitalize()))
  226. for i in self:
  227. d = i.__dict__
  228. d['amt'] = g.proto.coin_amt(d['amt'])
  229. i = io(**d)
  230. if verbose:
  231. pmsg(i.__dict__)
  232. def check_coin_mismatch(self):
  233. for i in self:
  234. if type(i.amt) != g.proto.coin_amt:
  235. die(2,'Coin mismatch in transaction: amount {} not of type {}!'.format(i.amt,g.proto.coin_amt))
  236. class MMGenTxOutputList(MMGenTxInputList):
  237. desc = 'transaction outputs'
  238. member_type = 'MMGenTxOutput'
  239. def __init__(self,filename=None,md_only=False,caller=None,silent_open=False):
  240. self.inputs = self.MMGenTxInputList()
  241. self.outputs = self.MMGenTxOutputList()
  242. self.send_amt = g.proto.coin_amt('0') # total amt minus change
  243. self.hex = '' # raw serialized hex transaction
  244. self.label = MMGenTXLabel('')
  245. self.txid = ''
  246. self.coin_txid = ''
  247. self.timestamp = ''
  248. self.chksum = ''
  249. self.fmt_data = ''
  250. self.fn = ''
  251. self.blockcount = 0
  252. self.chain = None
  253. self.coin = None
  254. self.caller = caller
  255. self.locktime = None
  256. if filename:
  257. self.parse_tx_file(filename,md_only=md_only,silent_open=silent_open)
  258. if md_only: return
  259. self.check_sigs() # marks the tx as signed
  260. # repeat with sign and send, because coin daemon could be restarted
  261. self.die_if_incorrect_chain()
  262. def die_if_incorrect_chain(self):
  263. if self.chain and g.chain and self.chain != g.chain:
  264. die(2,'Transaction is for {}, but current chain is {}!'.format(self.chain,g.chain))
  265. def add_output(self,coinaddr,amt,is_chg=None):
  266. self.outputs.append(MMGenTX.MMGenTxOutput(addr=coinaddr,amt=amt,is_chg=is_chg))
  267. def get_chg_output_idx(self):
  268. for i in range(len(self.outputs)):
  269. if self.outputs[i].is_chg == True:
  270. return i
  271. return None
  272. def update_output_amt(self,idx,amt):
  273. o = self.outputs[idx].__dict__
  274. o['amt'] = amt
  275. self.outputs[idx] = MMGenTX.MMGenTxOutput(**o)
  276. def del_output(self,idx):
  277. self.outputs.pop(idx)
  278. def sum_outputs(self,exclude=None):
  279. olist = self.outputs if exclude == None else \
  280. self.outputs[:exclude] + self.outputs[exclude+1:]
  281. return g.proto.coin_amt(sum(e.amt for e in olist))
  282. def add_mmaddrs_to_outputs(self,ad_w,ad_f):
  283. a = [e.addr for e in self.outputs]
  284. d = ad_w.make_reverse_dict(a)
  285. if ad_f:
  286. d.update(ad_f.make_reverse_dict(a))
  287. for e in self.outputs:
  288. if e.addr and e.addr in d:
  289. e.mmid,f = d[e.addr]
  290. if f: e.label = f
  291. def check_dup_addrs(self,io_str):
  292. assert io_str in ('inputs','outputs')
  293. io = getattr(self,io_str)
  294. for k in ('mmid','addr'):
  295. old_attr = None
  296. for attr in sorted(getattr(e,k) for e in io):
  297. if attr != None and attr == old_attr:
  298. die(2,'{}: duplicate address in transaction {}'.format(attr,io_str))
  299. old_attr = attr
  300. def update_txid(self):
  301. self.txid = MMGenTxID(make_chksum_6(unhexlify(self.hex)).upper())
  302. def create_raw(self):
  303. i = [{'txid':e.txid,'vout':e.vout} for e in self.inputs]
  304. if self.inputs[0].sequence:
  305. i[0]['sequence'] = self.inputs[0].sequence
  306. o = dict([(e.addr,e.amt) for e in self.outputs])
  307. self.hex = g.rpch.createrawtransaction(i,o)
  308. self.update_txid()
  309. # returns true if comment added or changed
  310. def add_comment(self,infile=None):
  311. if infile:
  312. self.label = MMGenTXLabel(get_data_from_file(infile,'transaction comment'))
  313. else: # get comment from user, or edit existing comment
  314. m = ('Add a comment to transaction?','Edit transaction comment?')[bool(self.label)]
  315. if keypress_confirm(m,default_yes=False):
  316. while True:
  317. s = MMGenTXLabel(my_raw_input('Comment: ',insert_txt=self.label))
  318. if s:
  319. lbl_save = self.label
  320. self.label = s
  321. return (True,False)[lbl_save == self.label]
  322. else:
  323. msg('Invalid comment')
  324. return False
  325. def edit_comment(self):
  326. return self.add_comment(self)
  327. def has_segwit_inputs(self):
  328. return any(i.mmid and i.mmid.mmtype in ('S','B') for i in self.inputs)
  329. def compare_size_and_estimated_size(self):
  330. est_vsize = self.estimate_size()
  331. d = g.rpch.decoderawtransaction(self.hex)
  332. vsize = d['vsize'] if 'vsize' in d else d['size']
  333. vmsg('\nSize: {}, Vsize: {} (true) {} (estimated)'.format(d['size'],vsize,est_vsize))
  334. m1 = '\nERROR: Estimated transaction vsize is {:1.2f} times the true vsize\n'
  335. m2 = 'Your transaction fee estimates will be inaccurate\n'
  336. m3 = 'Please re-create and re-sign the transaction using the option --vsize-adj={:1.2f}'
  337. # allow for 5% error
  338. ratio = float(est_vsize) / vsize
  339. if not (0.95 < ratio < 1.05):
  340. die(2,(m1+m2+m3).format(ratio,1/ratio))
  341. # https://bitcoin.stackexchange.com/questions/1195/how-to-calculate-transaction-size-before-sending
  342. # 180: uncompressed, 148: compressed
  343. def estimate_size_old(self):
  344. if not self.inputs or not self.outputs: return None
  345. return len(self.inputs)*180 + len(self.outputs)*34 + 10
  346. # https://bitcoincore.org/en/segwit_wallet_dev/
  347. # vsize: 3 times of the size with original serialization, plus the size with new
  348. # serialization, divide the result by 4 and round up to the next integer.
  349. # TODO: results differ slightly from actual transaction size
  350. def estimate_size(self):
  351. if not self.inputs or not self.outputs: return None
  352. sig_size = 72 # sig in DER format
  353. pubkey_size_uncompressed = 65
  354. pubkey_size_compressed = 33
  355. def get_inputs_size():
  356. # txid vout [scriptSig size (vInt)] scriptSig (<sig> <pubkey>) nSeq
  357. isize_common = 32 + 4 + 1 + 4 # txid vout [scriptSig size] nSeq = 41
  358. input_size = {
  359. 'L': isize_common + sig_size + pubkey_size_uncompressed, # = 180
  360. 'C': isize_common + sig_size + pubkey_size_compressed, # = 148
  361. 'S': isize_common + 23, # = 64
  362. 'B': isize_common + 0 # = 41
  363. }
  364. ret = sum(input_size[i.mmid.mmtype] for i in self.inputs if i.mmid)
  365. # We have no way of knowing whether a non-MMGen addr is compressed or uncompressed until
  366. # we see the key, so assume compressed for fee-estimation purposes. If fee estimate is
  367. # off by more than 5%, sign() aborts and user is instructed to use --vsize-adj option
  368. return ret + sum(input_size['C'] for i in self.inputs if not i.mmid)
  369. def get_outputs_size():
  370. # output bytes = amt: 8, byte_count: 1+, pk_script
  371. # pk_script bytes: p2pkh: 25, p2sh: 23, bech32: 22
  372. return sum({'p2pkh':34,'p2sh':32,'bech32':31}[o.addr.addr_fmt] for o in self.outputs)
  373. # https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
  374. # The witness is a serialization of all witness data of the transaction. Each txin is
  375. # associated with a witness field. A witness field starts with a var_int to indicate the
  376. # number of stack items for the txin. It is followed by stack items, with each item starts
  377. # with a var_int to indicate the length. Witness data is NOT script.
  378. # A non-witness program txin MUST be associated with an empty witness field, represented
  379. # by a 0x00. If all txins are not witness program, a transaction's wtxid is equal to its txid.
  380. def get_witness_size():
  381. if not self.has_segwit_inputs(): return 0
  382. wf_size = 1 + 1 + sig_size + 1 + pubkey_size_compressed # vInt vInt sig vInt pubkey = 108
  383. return sum((1,wf_size)[bool(i.mmid) and i.mmid.mmtype in ('S','B')] for i in self.inputs)
  384. isize = get_inputs_size()
  385. osize = get_outputs_size()
  386. wsize = get_witness_size()
  387. # TODO: compute real varInt sizes instead of assuming 1 byte
  388. # old serialization: [nVersion] [vInt][txins][vInt][txouts] [nLockTime]
  389. old_size = 4 + 1 + isize + 1 + osize + 4
  390. # marker = 0x00, flag = 0x01
  391. # new serialization: [nVersion][marker][flag][vInt][txins][vInt][txouts][witness][nLockTime]
  392. new_size = 4 + 1 + 1 + 1 + isize + 1 + osize + wsize + 4 \
  393. if wsize else old_size
  394. ret = (old_size * 3 + new_size) / 4
  395. dmsg('\nData from estimate_size():')
  396. dmsg(' inputs size: {}, outputs size: {}, witness size: {}'.format(isize,osize,wsize))
  397. dmsg(' size: {}, vsize: {}, old_size: {}'.format(new_size,ret,old_size))
  398. return int(ret * float(opt.vsize_adj)) if hasattr(opt,'vsize_adj') and opt.vsize_adj else ret
  399. def get_fee(self):
  400. return self.sum_inputs() - self.sum_outputs()
  401. def btc2spb(self,coin_fee):
  402. return int(coin_fee/g.proto.coin_amt.min_coin_unit/self.estimate_size())
  403. def get_relay_fee(self):
  404. kb_fee = g.proto.coin_amt(g.rpch.getnetworkinfo()['relayfee'])
  405. ret = kb_fee * self.estimate_size() / 1024
  406. vmsg('Relay fee: {} {c}/kB, for transaction: {} {c}'.format(kb_fee,ret,c=g.coin))
  407. return ret
  408. def convert_fee_spec(self,tx_fee,tx_size,on_fail='throw'):
  409. if g.proto.coin_amt(tx_fee,on_fail='silent'):
  410. return g.proto.coin_amt(tx_fee)
  411. elif len(tx_fee) >= 2 and tx_fee[-1] == 's' and is_int(tx_fee[:-1]) and int(tx_fee[:-1]) >= 1:
  412. if tx_size:
  413. return g.proto.coin_amt(int(tx_fee[:-1]) * tx_size * g.proto.coin_amt.min_coin_unit)
  414. else:
  415. return None
  416. else:
  417. if on_fail == 'return':
  418. return False
  419. elif on_fail == 'throw':
  420. assert False, "'{}': invalid tx-fee argument".format(tx_fee)
  421. def get_usr_fee(self,tx_fee,desc='Missing description'):
  422. coin_fee = self.convert_fee_spec(tx_fee,self.estimate_size(),on_fail='return')
  423. if coin_fee == None:
  424. # we shouldn't be calling this if tx size is unknown
  425. m = "'{}': cannot convert satoshis-per-byte to {} because transaction size is unknown"
  426. assert False, m.format(tx_fee,g.coin)
  427. elif coin_fee == False:
  428. m = "'{}': invalid TX fee (not a {} amount or satoshis-per-byte specification)"
  429. msg(m.format(tx_fee,g.coin))
  430. return False
  431. elif coin_fee > g.proto.max_tx_fee:
  432. m = '{} {c}: {} fee too large (maximum fee: {} {c})'
  433. msg(m.format(coin_fee,desc,g.proto.max_tx_fee,c=g.coin))
  434. return False
  435. elif coin_fee < self.get_relay_fee():
  436. m = '{} {c}: {} fee too small (below relay fee of {} {c})'
  437. msg(m.format(str(coin_fee),desc,str(self.get_relay_fee()),c=g.coin))
  438. return False
  439. else:
  440. return coin_fee
  441. def get_usr_fee_interactive(self,tx_fee=None,desc='Starting'):
  442. coin_fee = None
  443. while True:
  444. if tx_fee:
  445. coin_fee = self.get_usr_fee(tx_fee,desc)
  446. if coin_fee:
  447. m = ('',' (after {}x adjustment)'.format(opt.tx_fee_adj))[opt.tx_fee_adj != 1]
  448. p = '{} TX fee{}: {} {} ({} satoshis per byte)'.format(desc,m,
  449. coin_fee.hl(),g.coin,pink(str(self.btc2spb(coin_fee))))
  450. if opt.yes or keypress_confirm(p+'. OK?',default_yes=True):
  451. if opt.yes: msg(p)
  452. return coin_fee
  453. tx_fee = my_raw_input('Enter transaction fee: ')
  454. desc = 'User-selected'
  455. def delete_attrs(self,desc,attr):
  456. for e in getattr(self,desc):
  457. if hasattr(e,attr): delattr(e,attr)
  458. def decode_io_oldfmt(self,data):
  459. tr = {'amount':'amt', 'address':'addr', 'confirmations':'confs','comment':'label'}
  460. tr_rev = dict(map(reversed,tr.items()))
  461. copy_keys = [tr_rev[k] if k in tr_rev else k for k in self.MMGenTxInput.__dict__]
  462. ret = MMGenList(self.MMGenTxInput(**dict([(tr[k] if k in tr else k,d[k])
  463. for k in copy_keys if k in d and d[k] != ''])) for d in data)
  464. for i in ret: i.sequence = int('0xffffffff',16)
  465. return ret
  466. # inputs methods
  467. def copy_inputs_from_tw(self,tw_unspent_data):
  468. txi,self.inputs = self.MMGenTxInput,self.MMGenTxInputList()
  469. for d in tw_unspent_data:
  470. t = txi(**dict([(attr,getattr(d,attr)) for attr in d.__dict__ if attr in txi.__dict__]))
  471. if d.twmmid.type == 'mmgen': t.mmid = d.twmmid # twmmid -> mmid
  472. self.inputs.append(t)
  473. def get_input_sids(self):
  474. return set(e.mmid.sid for e in self.inputs if e.mmid)
  475. def get_output_sids(self):
  476. return set(e.mmid.sid for e in self.outputs if e.mmid)
  477. def sum_inputs(self):
  478. return sum(e.amt for e in self.inputs)
  479. def add_timestamp(self):
  480. self.timestamp = make_timestamp()
  481. def get_hex_locktime(self):
  482. return int(hexlify(unhexlify(self.hex[-8:])[::-1]),16)
  483. def set_hex_locktime(self,val):
  484. assert type(val) == int,'locktime value not an integer'
  485. self.hex = self.hex[:-8] + hexlify(unhexlify('{:08x}'.format(val))[::-1])
  486. def add_blockcount(self):
  487. self.blockcount = int(g.rpch.getblockcount())
  488. def format(self):
  489. self.inputs.check_coin_mismatch()
  490. self.outputs.check_coin_mismatch()
  491. def amt_to_str(d):
  492. return dict([(k,str(d[k]) if k == 'amt' else d[k]) for k in d])
  493. lines = [
  494. '{}{} {} {} {} {}{}'.format(
  495. (g.coin+' ','')[g.coin=='BTC'],
  496. self.chain.upper() if self.chain else 'Unknown',
  497. self.txid,
  498. self.send_amt,
  499. self.timestamp,
  500. self.blockcount,
  501. ('',' LT={}'.format(self.locktime))[bool(self.locktime)]
  502. ),
  503. self.hex,
  504. repr([amt_to_str(e.__dict__) for e in self.inputs]),
  505. repr([amt_to_str(e.__dict__) for e in self.outputs])
  506. ]
  507. if self.label:
  508. lines.append(baseconv.b58encode(self.label.encode('utf8')))
  509. if self.coin_txid:
  510. if not self.label: lines.append('-') # keep old tx files backwards compatible
  511. lines.append(self.coin_txid)
  512. self.chksum = make_chksum_6(' '.join(lines))
  513. self.fmt_data = '\n'.join([self.chksum] + lines)+'\n'
  514. assert len(self.fmt_data) <= g.max_tx_file_size,(
  515. 'Transaction file size exceeds limit ({} bytes)'.format(g.max_tx_file_size))
  516. def get_non_mmaddrs(self,desc):
  517. return list(set(i.addr for i in getattr(self,desc) if not i.mmid))
  518. # return true or false; don't exit
  519. def sign(self,tx_num_str,keys):
  520. if self.marked_signed():
  521. die(1,'Transaction is already signed!')
  522. self.die_if_incorrect_chain()
  523. if (self.has_segwit_inputs() or self.has_segwit_outputs()) and not g.proto.cap('segwit'):
  524. die(2,yellow("TX has Segwit inputs or outputs, but {} doesn't support Segwit!".format(g.coin)))
  525. qmsg('Passing {} key{} to {}'.format(len(keys),suf(keys,'s'),g.proto.daemon_name))
  526. if self.has_segwit_inputs():
  527. from mmgen.addr import KeyGenerator,AddrGenerator
  528. kg = KeyGenerator('std')
  529. ag = AddrGenerator('segwit')
  530. keydict = MMGenDict([(d.addr,d.sec) for d in keys])
  531. sig_data = []
  532. for d in self.inputs:
  533. e = dict([(k,getattr(d,k)) for k in ('txid','vout','scriptPubKey','amt')])
  534. e['amount'] = e['amt']
  535. del e['amt']
  536. if d.mmid and d.mmid.mmtype == 'S':
  537. e['redeemScript'] = ag.to_segwit_redeem_script(kg.to_pubhex(keydict[d.addr]))
  538. sig_data.append(e)
  539. msg_r('Signing transaction{}...'.format(tx_num_str))
  540. wifs = [d.sec.wif for d in keys]
  541. ret = g.rpch.signrawtransaction(self.hex,sig_data,wifs,g.proto.sighash_type,on_fail='return')
  542. from mmgen.rpc import rpc_error,rpc_errmsg
  543. if rpc_error(ret):
  544. errmsg = rpc_errmsg(ret)
  545. if 'Invalid sighash param' in errmsg:
  546. m = 'This is not the BCH chain.'
  547. m += "\nRe-run the script without the --coin=bch option."
  548. else:
  549. m = errmsg
  550. msg(yellow(m))
  551. return False
  552. else:
  553. if ret['complete']:
  554. # Msg(pretty_hexdump(unhexlify(self.hex),cols=16)) # DEBUG
  555. # pmsg(make_chksum_6(unhexlify(self.hex)).upper())
  556. self.hex = ret['hex']
  557. self.compare_size_and_estimated_size()
  558. dt = DeserializedTX(self.hex)
  559. self.check_hex_tx_matches_mmgen_tx(dt)
  560. self.coin_txid = CoinTxID(dt['txid'],on_fail='return')
  561. self.check_sigs(dt)
  562. assert self.coin_txid == g.rpch.decoderawtransaction(self.hex)['txid'],(
  563. 'txid mismatch (after signing)')
  564. msg('OK')
  565. return True
  566. else:
  567. msg('failed\n{} returned the following errors:'.format(g.proto.daemon_name.capitalize()))
  568. msg(repr(ret['errors']))
  569. return False
  570. def mark_raw(self):
  571. self.desc = 'transaction'
  572. self.ext = self.raw_ext
  573. def mark_signed(self): # called ONLY by check_sigs()
  574. self.desc = 'signed transaction'
  575. self.ext = self.sig_ext
  576. def marked_signed(self,color=False):
  577. ret = self.desc == 'signed transaction'
  578. return (red,green)[ret](str(ret)) if color else ret
  579. # check that a malicious, compromised or malfunctioning coin daemon hasn't altered hex tx data:
  580. # does not check witness or signature data
  581. def check_hex_tx_matches_mmgen_tx(self,deserial_tx):
  582. m = 'Fatal error: a malicious or malfunctioning coin daemon or other program may have altered your data!'
  583. lt = deserial_tx['lock_time']
  584. if lt != int(self.locktime or 0):
  585. m2 = '\nTransaction hex locktime ({}) does not match MMGen transaction locktime ({})\n{}'
  586. rdie(3,m2.format(lt,self.locktime,m))
  587. def check_equal(desc,hexio,mmio):
  588. if mmio != hexio:
  589. msg('\nMMGen {}:\n{}'.format(desc,pformat(mmio)))
  590. msg('Hex {}:\n{}'.format(desc,pformat(hexio)))
  591. m2 = '{} in hex transaction data from coin daemon do not match those in MMGen transaction!\n'
  592. rdie(3,(m2+m).format(desc.capitalize()))
  593. seq_hex = map(lambda i: int(i['nSeq'],16),deserial_tx['txins'])
  594. seq_mmgen = map(lambda i: i.sequence or g.max_int,self.inputs)
  595. check_equal('sequence numbers',seq_hex,seq_mmgen)
  596. d_hex = sorted((i['txid'],i['vout']) for i in deserial_tx['txins'])
  597. d_mmgen = sorted((i.txid,i.vout) for i in self.inputs)
  598. check_equal('inputs',d_hex,d_mmgen)
  599. d_hex = sorted((o['address'],g.proto.coin_amt(o['amount'])) for o in deserial_tx['txouts'])
  600. d_mmgen = sorted((o.addr,o.amt) for o in self.outputs)
  601. check_equal('outputs',d_hex,d_mmgen)
  602. uh = deserial_tx['unsigned_hex']
  603. if str(self.txid) != make_chksum_6(unhexlify(uh)).upper():
  604. rdie(3,'MMGen TxID ({}) does not match hex transaction data!\n{}'.format(self.txid,m))
  605. # check signature and witness data
  606. def check_sigs(self,deserial_tx=None): # return False if no sigs, die on error
  607. txins = (deserial_tx or DeserializedTX(self.hex))['txins']
  608. has_ss = any(ti['scriptSig'] for ti in txins)
  609. has_witness = any('witness' in ti and ti['witness'] for ti in txins)
  610. if not (has_ss or has_witness):
  611. return False
  612. fs = "Hex TX has {} scriptSig but input is of type '{}'!"
  613. for n in range(len(txins)):
  614. ti,mmti = txins[n],self.inputs[n]
  615. if ti['scriptSig'] == '' or ( len(ti['scriptSig']) == 46 and # native P2WPKH or P2SH-P2WPKH
  616. ti['scriptSig'][:6] == '16' + g.proto.witness_vernum_hex + '14' ):
  617. assert 'witness' in ti, 'missing witness'
  618. assert type(ti['witness']) == list and len(ti['witness']) == 2, 'malformed witness'
  619. assert len(ti['witness'][1]) == 66, 'incorrect witness pubkey length'
  620. assert mmti.mmid, fs.format('witness-type','non-MMGen')
  621. assert mmti.mmid.mmtype == ('S','B')[ti['scriptSig']==''],fs.format('witness-type',mmti.mmid.mmtype)
  622. else: # non-witness
  623. if mmti.mmid:
  624. assert mmti.mmid.mmtype not in ('S','B'), fs.format('signature in',mmti.mmid.mmtype)
  625. assert not 'witness' in ti, 'non-witness input has witness'
  626. # sig_size 72 (DER format), pubkey_size 'compressed':33, 'uncompressed':65
  627. assert (200 < len(ti['scriptSig']) < 300), 'malformed scriptSig' # VERY rough check
  628. self.mark_signed()
  629. return True
  630. def has_segwit_outputs(self):
  631. return any(o.mmid and o.mmid.mmtype in ('S','B') for o in self.outputs)
  632. def is_in_mempool(self):
  633. return 'size' in g.rpch.getmempoolentry(self.coin_txid,on_fail='silent')
  634. def is_in_wallet(self):
  635. ret = g.rpch.gettransaction(self.coin_txid,on_fail='silent')
  636. if 'confirmations' in ret and ret['confirmations'] > 0:
  637. return ret['confirmations']
  638. else:
  639. return False
  640. def is_replaced(self):
  641. if self.is_in_mempool(): return False
  642. ret = g.rpch.gettransaction(self.coin_txid,on_fail='silent')
  643. if not 'bip125-replaceable' in ret or not 'confirmations' in ret or ret['confirmations'] > 0:
  644. return False
  645. return -ret['confirmations'] + 1,ret # 1: replacement in mempool, 2: replacement confirmed
  646. def is_in_utxos(self):
  647. return 'txid' in g.rpch.getrawtransaction(self.coin_txid,True,on_fail='silent')
  648. def get_status(self,status=False):
  649. if self.is_in_mempool():
  650. if status:
  651. d = g.rpch.gettransaction(self.coin_txid,on_fail='silent')
  652. brs = 'bip125-replaceable'
  653. r = '{}replaceable'.format(('NOT ','')[brs in d and d[brs]=='yes'])
  654. t = d['timereceived']
  655. m = 'Sent {} ({} h/m/s ago)'
  656. b = m.format(time.strftime('%c',time.gmtime(t)),secs_to_dhms(int(time.time()-t)))
  657. if opt.quiet:
  658. msg('Transaction is in mempool')
  659. else:
  660. msg('TX status: in mempool, {}\n{}'.format(r,b))
  661. else:
  662. msg('Warning: transaction is in mempool!')
  663. elif self.is_in_wallet():
  664. confs = self.is_in_wallet()
  665. die(0,'Transaction has {} confirmation{}'.format(confs,suf(confs,'s')))
  666. elif self.is_in_utxos():
  667. die(2,red('ERROR: transaction is in the blockchain (but not in the tracking wallet)!'))
  668. else:
  669. ret = self.is_replaced() # ret[0]==1: replacement in mempool, ret[0]==2: replacement confirmed
  670. if ret and ret[0]:
  671. m1 = 'Transaction has been replaced'
  672. m2 = ('',', and the replacement TX is confirmed')[ret[0]==2]
  673. msg('{}{}!'.format(m1,m2))
  674. if not opt.quiet:
  675. msg('Replacing transactions:')
  676. rt = ret[1]['walletconflicts']
  677. for t,s in [(tx,'size' in g.rpch.getmempoolentry(tx,on_fail='silent')) for tx in rt]:
  678. msg(' {}{}'.format(t,('',' in mempool')[s]))
  679. die(0,'')
  680. def send(self,prompt_user=True,exit_on_fail=False):
  681. if not self.marked_signed():
  682. die(1,'Transaction is not signed!')
  683. self.die_if_incorrect_chain()
  684. self.check_hex_tx_matches_mmgen_tx(DeserializedTX(self.hex))
  685. bogus_send = os.getenv('MMGEN_BOGUS_SEND')
  686. if self.has_segwit_outputs() and not segwit_is_active() and not bogus_send:
  687. m = 'Transaction has MMGen Segwit outputs, but this blockchain does not support Segwit'
  688. die(2,m+' at the current height')
  689. if self.get_fee() > g.proto.max_tx_fee:
  690. die(2,'Transaction fee ({}) greater than {} max_tx_fee ({} {})!'.format(
  691. self.get_fee(),g.proto.name.capitalize(),g.proto.max_tx_fee,g.coin.upper()))
  692. self.get_status()
  693. if prompt_user:
  694. m1 = ("Once this transaction is sent, there's no taking it back!",'')[bool(opt.quiet)]
  695. m2 = 'broadcast this transaction to the {} network'.format(g.chain.upper())
  696. m3 = ('YES, I REALLY WANT TO DO THIS','YES')[bool(opt.quiet or opt.yes)]
  697. confirm_or_exit(m1,m2,m3)
  698. msg('Sending transaction')
  699. ret = None if bogus_send else g.rpch.sendrawtransaction(self.hex,on_fail='return')
  700. from mmgen.rpc import rpc_error,rpc_errmsg
  701. if rpc_error(ret):
  702. errmsg = rpc_errmsg(ret)
  703. if 'Signature must use SIGHASH_FORKID' in errmsg:
  704. m = 'The Aug. 1 2017 UAHF has activated on this chain.'
  705. m += "\nRe-run the script with the --coin=bch option."
  706. elif 'Illegal use of SIGHASH_FORKID' in errmsg:
  707. m = 'The Aug. 1 2017 UAHF is not yet active on this chain.'
  708. m += "\nRe-run the script without the --coin=bch option."
  709. elif '64: non-final' in errmsg:
  710. m2 = "Transaction with locktime '{}' can't be included in this block!"
  711. m = m2.format(strfmt_locktime(self.get_hex_locktime()))
  712. else:
  713. m = errmsg
  714. msg(yellow(m))
  715. msg(red('Send of MMGen transaction {} failed'.format(self.txid)))
  716. if exit_on_fail: sys.exit(1)
  717. return False
  718. else:
  719. if bogus_send:
  720. m = 'BOGUS transaction NOT sent: {}'
  721. else:
  722. assert ret == self.coin_txid, 'txid mismatch (after sending)'
  723. m = 'Transaction sent: {}'
  724. self.desc = 'sent transaction'
  725. msg(m.format(self.coin_txid.hl()))
  726. self.add_timestamp()
  727. self.add_blockcount()
  728. return True
  729. def write_txid_to_file(self,ask_write=False,ask_write_default_yes=True):
  730. fn = '{}[{}].{}'.format(self.txid,self.send_amt,self.txid_ext)
  731. write_data_to_file(fn,self.coin_txid+'\n','transaction ID',
  732. ask_write=ask_write,
  733. ask_write_default_yes=ask_write_default_yes)
  734. def create_fn(self):
  735. tl = self.get_hex_locktime()
  736. self.fn = u'{}{}[{!s}{}{}]{x}.{}'.format(
  737. self.txid,
  738. ('-'+g.coin,'')[g.coin=='BTC'],
  739. self.send_amt,
  740. ('',',{}'.format(self.btc2spb(self.get_fee())))[self.is_rbf()],
  741. ('',',tl={}'.format(tl))[bool(tl)],
  742. self.ext,
  743. x=u'-α' if g.debug_utf8 else '')
  744. def write_to_file( self,
  745. add_desc='',
  746. ask_write=True,
  747. ask_write_default_yes=False,
  748. ask_tty=True,
  749. ask_overwrite=True):
  750. if ask_write == False: ask_write_default_yes = True
  751. if not self.fmt_data: self.format()
  752. if not self.fn: self.create_fn()
  753. write_data_to_file(self.fn,self.fmt_data,self.desc+add_desc,
  754. ask_overwrite=ask_overwrite,
  755. ask_write=ask_write,
  756. ask_tty=ask_tty,
  757. ask_write_default_yes=ask_write_default_yes)
  758. def view_with_prompt(self,prompt=''):
  759. prompt += ' (y)es, (N)o, pager (v)iew, (t)erse view'
  760. reply = prompt_and_get_char(prompt,'YyNnVvTt',enter_ok=True)
  761. if reply and reply in 'YyVvTt':
  762. self.view(pager=reply in 'Vv',terse=reply in 'Tt')
  763. def view(self,pager=False,pause=True,terse=False):
  764. o = self.format_view(terse=terse)
  765. if pager: do_pager(o)
  766. else:
  767. msg_r(o)
  768. from mmgen.term import get_char
  769. if pause:
  770. get_char('Press any key to continue: ')
  771. msg('')
  772. # def is_rbf_from_rpc(self):
  773. # dec_tx = g.rpch.decoderawtransaction(self.hex)
  774. # return None < dec_tx['vin'][0]['sequence'] <= g.max_int - 2
  775. def is_rbf(self):
  776. return self.inputs[0].sequence == g.max_int - 2
  777. def format_view(self,terse=False):
  778. try:
  779. rpc_init()
  780. blockcount = g.rpch.getblockcount()
  781. except:
  782. blockcount = None
  783. def get_max_mmwid(io):
  784. if io == self.inputs:
  785. sel_f = lambda o: len(o.mmid) + 2 # len('()')
  786. else:
  787. sel_f = lambda o: len(o.mmid) + (2,8)[bool(o.is_chg)] # + len(' (chg)')
  788. return max(max([sel_f(o) for o in io if o.mmid] or [0]),len(nonmm_str))
  789. nonmm_str = '(non-{pnm} address)'.format(pnm=g.proj_name)
  790. max_mmwid = max(get_max_mmwid(self.inputs),get_max_mmwid(self.outputs))
  791. def format_io(io):
  792. ip = io is self.inputs
  793. io_out = ''
  794. addr_w = max(len(e.addr) for e in io)
  795. confs_per_day = 60*60*24 / g.proto.secs_per_block
  796. for n,e in enumerate(sorted(io,key=lambda o: o.mmid.sort_key if o.mmid else o.addr)):
  797. if ip and blockcount != None:
  798. confs = e.confs + blockcount - self.blockcount
  799. days = int(confs / confs_per_day)
  800. if e.mmid:
  801. app=('',' (chg)')[bool(not ip and e.is_chg and terse)]
  802. mmid_fmt = e.mmid.fmt(width=max_mmwid,encl='()',color=True,app=app,appcolor='green')
  803. else:
  804. mmid_fmt = MMGenID.fmtc(nonmm_str,width=max_mmwid)
  805. if terse:
  806. io_out += '{:3} {} {} {} {}\n'.format(n+1,
  807. e.addr.fmt(color=True,width=addr_w),
  808. mmid_fmt,e.amt.hl(),g.coin)
  809. else:
  810. icommon = [
  811. ((n+1,'')[ip],'address:',e.addr.fmt(color=True,width=addr_w) + ' '+mmid_fmt),
  812. ('','comment:',e.label.hl() if e.label else ''),
  813. ('','amount:','{} {}'.format(e.amt.hl(),g.coin))]
  814. items = [(n+1, 'tx,vout:','{},{}'.format(e.txid,e.vout))] + icommon + [
  815. ('','confirmations:','{} (around {} days)'.format(confs,days) if blockcount!=None else '')
  816. ] if ip else icommon + [
  817. ('','change:',green('True') if e.is_chg else '')]
  818. io_out += '\n'.join([(u'{:>3} {:<8} {}'.format(*d)) for d in items if d[2]]) + '\n\n'
  819. return io_out
  820. hdr_fs = (
  821. 'TRANSACTION DATA\n\nID={} ({} {}) UTC={} RBF={} Sig={} Locktime={}\n',
  822. 'TX {} ({} {}) UTC={} RBF={} Sig={} Locktime={}\n'
  823. )[bool(terse)]
  824. out = hdr_fs.format(self.txid.hl(),
  825. self.send_amt.hl(),
  826. g.coin,
  827. self.timestamp,
  828. (red('False'),
  829. green('True'))[self.is_rbf()],
  830. self.marked_signed(color=True),
  831. (green('None'),orange(strfmt_locktime(self.locktime,terse=True)))[bool(self.locktime)])
  832. if self.chain in ('testnet','regtest'):
  833. out += green('Chain: {}\n'.format(self.chain.upper()))
  834. if self.coin_txid:
  835. out += '{} TxID: {}\n'.format(g.coin,self.coin_txid.hl())
  836. enl = ('\n','')[bool(terse)]
  837. out += enl
  838. if self.label:
  839. out += u'Comment: {}\n{}'.format(self.label.hl(),enl)
  840. out += 'Inputs:\n' + enl + format_io(self.inputs)
  841. out += 'Outputs:\n' + enl + format_io(self.outputs)
  842. fs = (
  843. 'Total input: {} {c}\nTotal output: {} {c}\nTX fee: {} {c} ({} satoshis per byte)\n',
  844. 'In {} {c} - Out {} {c} - Fee {} {c} ({} satoshis/byte)\n'
  845. )[bool(terse)]
  846. t_in,t_out = self.sum_inputs(),self.sum_outputs()
  847. fee = t_in-t_out
  848. out += fs.format(t_in.hl(),t_out.hl(),fee.hl(),pink(str(self.btc2spb(fee))),c=g.coin)
  849. if opt.verbose:
  850. ts = len(self.hex)/2 if self.hex else 'unknown'
  851. out += 'Transaction size: Vsize {} (estimated), Total {}'.format(self.estimate_size(),ts)
  852. if self.marked_signed():
  853. ws = DeserializedTX(self.hex)['witness_size']
  854. out += ', Base {}, Witness {}'.format(ts-ws,ws)
  855. out += '\n'
  856. return out # TX label might contain non-ascii chars
  857. def parse_tx_file(self,infile,md_only=False,silent_open=False):
  858. def eval_io_data(raw_data,desc):
  859. from ast import literal_eval
  860. try:
  861. d = literal_eval(raw_data)
  862. except:
  863. if desc == 'inputs' and not silent_open:
  864. ymsg('Warning: transaction data appears to be in old format')
  865. import re
  866. d = literal_eval(re.sub(r"[A-Za-z]+?\(('.+?')\)",r'\1',raw_data))
  867. assert type(d) == list,'{} data not a list!'.format(desc)
  868. assert len(d),'no {}!'.format(desc)
  869. for e in d: e['amt'] = g.proto.coin_amt(e['amt'])
  870. io,io_list = (
  871. (MMGenTX.MMGenTxOutput,MMGenTX.MMGenTxOutputList),
  872. (MMGenTX.MMGenTxInput,MMGenTX.MMGenTxInputList)
  873. )[desc=='inputs']
  874. return io_list([io(**e) for e in d])
  875. tx_data = get_data_from_file(infile,self.desc+' data',silent=silent_open)
  876. try:
  877. desc = 'data'
  878. assert len(tx_data) <= g.max_tx_file_size,(
  879. 'Transaction file size exceeds limit ({} bytes)'.format(g.max_tx_file_size))
  880. tx_data = tx_data.decode('ascii').splitlines()
  881. assert len(tx_data) >= 5,'number of lines less than 5'
  882. assert len(tx_data[0]) == 6,'invalid length of first line'
  883. self.chksum = HexStr(tx_data.pop(0),on_fail='raise')
  884. assert self.chksum == make_chksum_6(' '.join(tx_data)),'file data does not match checksum'
  885. if len(tx_data) == 6:
  886. assert len(tx_data[-1]) == 64,'invalid coin TxID length'
  887. desc = '{} TxID'.format(g.proto.name.capitalize())
  888. self.coin_txid = CoinTxID(tx_data.pop(-1),on_fail='raise')
  889. if len(tx_data) == 5:
  890. # rough check: allow for 4-byte utf8 characters + base58 (4 * 11 / 8 = 6 (rounded up))
  891. assert len(tx_data[-1]) < MMGenTXLabel.max_len*6,'invalid comment length'
  892. c = tx_data.pop(-1)
  893. if c != '-':
  894. desc = 'encoded comment (not base58)'
  895. comment = baseconv.b58decode(c).decode('utf8')
  896. assert comment != False,'invalid comment'
  897. desc = 'comment'
  898. self.label = MMGenTXLabel(comment,on_fail='raise')
  899. desc = 'number of lines' # four required lines
  900. metadata,self.hex,inputs_data,outputs_data = tx_data
  901. assert len(metadata) < 60,'invalid metadata length' # rough check
  902. metadata = metadata.split()
  903. if metadata[-1].find('LT=') == 0:
  904. desc = 'locktime'
  905. self.locktime = int(metadata.pop()[3:])
  906. self.coin = metadata.pop(0) if len(metadata) == 6 else 'BTC'
  907. if len(metadata) == 5:
  908. t = metadata.pop(0)
  909. self.chain = (t.lower(),None)[t=='Unknown']
  910. desc = 'metadata (4 items minimum required)'
  911. self.txid,send_amt,self.timestamp,blockcount = metadata
  912. desc = 'metadata'
  913. self.txid = MMGenTxID(self.txid,on_fail='raise')
  914. self.send_amt = g.proto.coin_amt(send_amt,on_fail='raise')
  915. desc = 'block count in metadata'
  916. self.blockcount = int(blockcount)
  917. desc = 'transaction hex data'
  918. self.hex = HexStr(self.hex,on_fail='raise')
  919. if md_only: return # the following ops will all fail if g.coin doesn't match self.coin
  920. desc = 'coin type in metadata'
  921. assert self.coin == g.coin,'invalid coin type'
  922. desc = 'inputs data'
  923. self.inputs = eval_io_data(inputs_data,'inputs')
  924. desc = 'outputs data'
  925. self.outputs = eval_io_data(outputs_data,'outputs')
  926. except Exception as e:
  927. die(2,'Invalid {} in transaction file: {}'.format(desc,e[0]))
  928. if not self.chain and not self.inputs[0].addr.is_for_chain('testnet'):
  929. self.chain = 'mainnet'
  930. def get_fee_from_user(self,have_estimate_fail=[]):
  931. if opt.tx_fee:
  932. desc = 'User-selected'
  933. start_fee = opt.tx_fee
  934. else:
  935. desc = 'Network-estimated'
  936. try:
  937. ret = g.rpch.estimatesmartfee(opt.tx_confs,on_fail='raise')
  938. except:
  939. fetype = 'estimatefee'
  940. fee_per_kb = g.rpch.estimatefee(opt.tx_confs)
  941. else:
  942. fetype = 'estimatesmartfee'
  943. fee_per_kb = ret['feerate'] if 'feerate' in ret else -2
  944. if fee_per_kb < 0:
  945. if not have_estimate_fail:
  946. msg('Network fee estimation for {} confirmations failed ({})'.format(opt.tx_confs,fetype))
  947. have_estimate_fail.append(True)
  948. start_fee = None
  949. else:
  950. start_fee = g.proto.coin_amt(fee_per_kb) * opt.tx_fee_adj * self.estimate_size() / 1024
  951. if opt.verbose:
  952. msg('{} fee for {} confirmations: {} {}/kB'.format(
  953. fetype.upper(),opt.tx_confs,fee_per_kb,g.coin))
  954. msg('TX size (estimated): {}'.format(self.estimate_size()))
  955. return self.get_usr_fee_interactive(start_fee,desc=desc)
  956. def get_outputs_from_cmdline(self,cmd_args):
  957. from mmgen.addr import AddrList,AddrData
  958. addrfiles = [a for a in cmd_args if get_extension(a) == AddrList.ext]
  959. cmd_args = set(cmd_args) - set(addrfiles)
  960. ad_f = AddrData()
  961. for a in addrfiles:
  962. check_infile(a)
  963. ad_f.add(AddrList(a))
  964. ad_w = AddrData(source='tw')
  965. for a in cmd_args:
  966. if ',' in a:
  967. a1,a2 = a.split(',',1)
  968. if is_mmgen_id(a1) or is_coin_addr(a1):
  969. coin_addr = mmaddr2coinaddr(a1,ad_w,ad_f) if is_mmgen_id(a1) else CoinAddr(a1)
  970. self.add_output(coin_addr,g.proto.coin_amt(a2))
  971. else:
  972. die(2,"{}: invalid subargument in command-line argument '{}'".format(a1,a))
  973. elif is_mmgen_id(a) or is_coin_addr(a):
  974. if self.get_chg_output_idx() != None:
  975. die(2,'ERROR: More than one change address listed on command line')
  976. coin_addr = mmaddr2coinaddr(a,ad_w,ad_f) if is_mmgen_id(a) else CoinAddr(a)
  977. self.add_output(coin_addr,g.proto.coin_amt('0'),is_chg=True)
  978. else:
  979. die(2,'{}: invalid command-line argument'.format(a))
  980. if not self.outputs:
  981. die(2,'At least one output must be specified on the command line')
  982. if self.get_chg_output_idx() == None:
  983. die(2,('ERROR: No change output specified',wmsg['no_change_output'])[len(self.outputs) == 1])
  984. self.add_mmaddrs_to_outputs(ad_w,ad_f)
  985. self.check_dup_addrs('outputs')
  986. if not segwit_is_active() and self.has_segwit_outputs():
  987. fs = '{} Segwit address requested on the command line, but Segwit is not active on this chain'
  988. rdie(2,fs.format(g.proj_name))
  989. def get_inputs_from_user(self,tw):
  990. while True:
  991. m = 'Enter a range or space-separated list of outputs to spend: '
  992. sel_nums = select_unspent(tw.unspent,m)
  993. msg('Selected output{}: {}'.format(suf(sel_nums,'s'),' '.join(map(str,sel_nums))))
  994. sel_unspent = tw.MMGenTwOutputList([tw.unspent[i-1] for i in sel_nums])
  995. t_inputs = sum(s.amt for s in sel_unspent)
  996. if t_inputs < self.send_amt:
  997. msg(wmsg['not_enough_coin'].format(self.send_amt-t_inputs))
  998. continue
  999. non_mmaddrs = [i for i in sel_unspent if i.twmmid.type == 'non-mmgen']
  1000. if non_mmaddrs and self.caller != 'txdo':
  1001. msg(wmsg['non_mmgen_inputs'].format(', '.join(set(sorted([a.addr.hl() for a in non_mmaddrs])))))
  1002. if not keypress_confirm('Accept?'):
  1003. continue
  1004. self.copy_inputs_from_tw(sel_unspent) # makes self.inputs
  1005. change_amt = self.sum_inputs() - self.send_amt - self.get_fee_from_user()
  1006. if change_amt >= 0:
  1007. p = 'Transaction produces {} {} in change'.format(change_amt.hl(),g.coin)
  1008. if opt.yes or keypress_confirm(p+'. OK?',default_yes=True):
  1009. if opt.yes: msg(p)
  1010. return change_amt
  1011. else:
  1012. msg(wmsg['not_enough_coin'].format(abs(change_amt)))
  1013. def create(self,cmd_args,locktime,do_info=False):
  1014. assert type(locktime) == int
  1015. if opt.comment_file: self.add_comment(opt.comment_file)
  1016. if not do_info: self.get_outputs_from_cmdline(cmd_args)
  1017. do_license_msg()
  1018. from mmgen.tw import MMGenTrackingWallet
  1019. tw = MMGenTrackingWallet(minconf=opt.minconf)
  1020. tw.view_and_sort(self)
  1021. tw.display_total()
  1022. if do_info: sys.exit(0)
  1023. self.send_amt = self.sum_outputs()
  1024. msg('Total amount to spend: {}'.format(
  1025. ('Unknown','{} {}'.format(self.send_amt.hl(),g.coin))[bool(self.send_amt)]
  1026. ))
  1027. change_amt = self.get_inputs_from_user(tw)
  1028. # only after we have inputs
  1029. if locktime: self.inputs[0].sequence = g.max_int - 1
  1030. if opt.rbf: self.inputs[0].sequence = g.max_int - 2
  1031. chg_idx = self.get_chg_output_idx()
  1032. if change_amt == 0:
  1033. msg('Warning: Change address will be deleted as transaction produces no change')
  1034. self.del_output(chg_idx)
  1035. else:
  1036. self.update_output_amt(chg_idx,g.proto.coin_amt(change_amt))
  1037. if not self.send_amt:
  1038. self.send_amt = change_amt
  1039. if not opt.yes:
  1040. self.add_comment() # edits an existing comment
  1041. self.create_raw() # creates self.hex, self.txid
  1042. if locktime:
  1043. msg('Setting nlocktime to {}!'.format(strfmt_locktime(locktime)))
  1044. self.set_hex_locktime(locktime)
  1045. self.update_txid()
  1046. self.locktime = locktime
  1047. self.add_timestamp()
  1048. self.add_blockcount()
  1049. self.chain = g.chain
  1050. assert self.sum_inputs() - self.sum_outputs() <= g.proto.max_tx_fee
  1051. qmsg('Transaction successfully created')
  1052. if not opt.yes:
  1053. self.view_with_prompt('View decoded transaction?')
  1054. class MMGenBumpTX(MMGenTX):
  1055. min_fee = None
  1056. bump_output_idx = None
  1057. def __init__(self,filename,send=False):
  1058. super(type(self),self).__init__(filename)
  1059. if not self.is_rbf():
  1060. die(1,"Transaction '{}' is not replaceable (RBF)".format(self.txid))
  1061. # If sending, require tx to have been signed
  1062. if send:
  1063. if not self.marked_signed():
  1064. die(1,"File '{}' is not a signed {} transaction file".format(filename,g.proj_name))
  1065. if not self.coin_txid:
  1066. die(1,"Transaction '{}' was not broadcast to the network".format(self.txid,g.proj_name))
  1067. self.coin_txid = ''
  1068. self.mark_raw()
  1069. def choose_output(self):
  1070. chg_idx = self.get_chg_output_idx()
  1071. init_reply = opt.output_to_reduce
  1072. while True:
  1073. if init_reply == None:
  1074. m = 'Choose an output to deduct the fee from (Hit ENTER for the change output): '
  1075. reply = my_raw_input(m) or 'c'
  1076. else:
  1077. reply,init_reply = init_reply,None
  1078. if chg_idx == None and not is_int(reply):
  1079. msg("Output must be an integer")
  1080. elif chg_idx != None and not is_int(reply) and reply != 'c':
  1081. msg("Output must be an integer, or 'c' for the change output")
  1082. else:
  1083. idx = chg_idx if reply == 'c' else (int(reply) - 1)
  1084. if idx < 0 or idx >= len(self.outputs):
  1085. msg('Output must be in the range 1-{}'.format(len(self.outputs)))
  1086. else:
  1087. o_amt = self.outputs[idx].amt
  1088. cs = ('',' (change output)')[chg_idx == idx]
  1089. p = 'Fee will be deducted from output {}{} ({} {})'.format(idx+1,cs,o_amt,g.coin)
  1090. if o_amt < self.min_fee:
  1091. msg('Minimum fee ({} {c}) is greater than output amount ({} {c})'.format(
  1092. self.min_fee,o_amt,c=g.coin))
  1093. elif opt.yes or keypress_confirm(p+'. OK?',default_yes=True):
  1094. if opt.yes: msg(p)
  1095. self.bump_output_idx = idx
  1096. return idx
  1097. def set_min_fee(self):
  1098. self.min_fee = self.sum_inputs() - self.sum_outputs() + self.get_relay_fee()
  1099. def get_usr_fee(self,tx_fee,desc):
  1100. ret = super(type(self),self).get_usr_fee(tx_fee,desc)
  1101. if ret < self.min_fee:
  1102. msg('{} {c}: {} fee too small. Minimum fee: {} {c} ({} satoshis per byte)'.format(
  1103. ret,desc,self.min_fee,self.btc2spb(self.min_fee),c=g.coin))
  1104. return False
  1105. output_amt = self.outputs[self.bump_output_idx].amt
  1106. if ret >= output_amt:
  1107. msg('{} {c}: {} fee too large. Maximum fee: <{} {c}'.format(ret,desc,output_amt,c=g.coin))
  1108. return False
  1109. return ret
  1110. class MMGenSplitTX(MMGenTX):
  1111. def get_outputs_from_cmdline(self,mmid): # TODO: check that addr is empty
  1112. from mmgen.addr import AddrData
  1113. ad_w = AddrData(source='tw')
  1114. if is_mmgen_id(mmid):
  1115. coin_addr = mmaddr2coinaddr(mmid,ad_w,None) if is_mmgen_id(mmid) else CoinAddr(mmid)
  1116. self.add_output(coin_addr,g.proto.coin_amt('0'),is_chg=True)
  1117. else:
  1118. die(2,'{}: invalid command-line argument'.format(mmid))
  1119. self.add_mmaddrs_to_outputs(ad_w,None)
  1120. if not segwit_is_active() and self.has_segwit_outputs():
  1121. fs = '{} Segwit address requested on the command line, but Segwit is not active on this chain'
  1122. rdie(2,fs.format(g.proj_name))
  1123. def get_split_fee_from_user(self):
  1124. if opt.rpc_host2:
  1125. g.rpc_host = opt.rpc_host2
  1126. if opt.tx_fees:
  1127. opt.tx_fee = opt.tx_fees.split(',')[1]
  1128. try:
  1129. rpc_init(reinit=True)
  1130. except:
  1131. ymsg('Connect to {} daemon failed. Network fee estimation unavailable'.format(g.coin))
  1132. return self.get_usr_fee_interactive(opt.tx_fee,'User-selected')
  1133. return super(type(self),self).get_fee_from_user()
  1134. def create_split(self,mmid):
  1135. self.outputs = self.MMGenTxOutputList()
  1136. self.get_outputs_from_cmdline(mmid)
  1137. while True:
  1138. change_amt = self.sum_inputs() - self.get_split_fee_from_user()
  1139. if change_amt >= 0:
  1140. p = 'Transaction produces {} {} in change'.format(change_amt.hl(),g.coin)
  1141. if opt.yes or keypress_confirm(p+'. OK?',default_yes=True):
  1142. if opt.yes: msg(p)
  1143. break
  1144. else:
  1145. msg(wmsg['not_enough_coin'].format(abs(change_amt)))
  1146. self.update_output_amt(0,change_amt)
  1147. self.send_amt = change_amt
  1148. if not opt.yes:
  1149. self.add_comment() # edits an existing comment
  1150. self.create_raw() # creates self.hex, self.txid
  1151. self.add_timestamp()
  1152. self.add_blockcount() # TODO
  1153. self.chain = g.chain
  1154. assert self.sum_inputs() - self.sum_outputs() <= g.proto.max_tx_fee
  1155. qmsg('Transaction successfully created')
  1156. if not opt.yes:
  1157. self.view_with_prompt('View decoded transaction?')