tx-old2new.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python
  2. import sys,os
  3. repo_root = os.path.split(os.path.abspath(os.path.dirname(sys.argv[0])))[0]
  4. sys.path = [repo_root] + sys.path
  5. from mmgen.common import *
  6. from mmgen.tool import *
  7. from mmgen.tx import *
  8. from mmgen.bitcoin import *
  9. from mmgen.seed import *
  10. from mmgen.term import do_pager
  11. help_data = {
  12. 'desc': "Convert MMGen transaction file from old format to new format",
  13. 'usage': "<tx file>",
  14. 'options': """
  15. -h, --help Print this help message
  16. """
  17. }
  18. import mmgen.opts
  19. cmd_args = opts.init(help_data)
  20. if len(cmd_args) != 1: opts.usage()
  21. def parse_tx_file(infile):
  22. err_str,err_fmt = '','Invalid %s in transaction file'
  23. tx_data = get_lines_from_file(infile)
  24. if len(tx_data) == 5:
  25. metadata,tx_hex,inputs_data,outputs_data,comment = tx_data
  26. elif len(tx_data) == 4:
  27. metadata,tx_hex,inputs_data,outputs_data = tx_data
  28. comment = ''
  29. else:
  30. err_str = 'number of lines'
  31. if not err_str:
  32. if len(metadata.split()) != 3:
  33. err_str = 'metadata'
  34. else:
  35. try: unhexlify(tx_hex)
  36. except: err_str = 'hex data'
  37. else:
  38. try: inputs_data = eval(inputs_data)
  39. except: err_str = 'inputs data'
  40. else:
  41. try: outputs_data = eval(outputs_data)
  42. except: err_str = 'btc-to-mmgen address map data'
  43. else:
  44. if comment:
  45. from mmgen.bitcoin import b58decode
  46. comment = b58decode(comment)
  47. if comment == False:
  48. err_str = 'encoded comment (not base58)'
  49. else:
  50. if is_valid_tx_comment(comment):
  51. comment = comment.decode('utf8')
  52. else:
  53. err_str = 'comment'
  54. if err_str:
  55. msg(err_fmt % err_str)
  56. sys.exit(2)
  57. else:
  58. return metadata.split(),tx_hex,inputs_data,outputs_data,comment
  59. def find_block_by_time(c,timestamp):
  60. secs = decode_timestamp(timestamp)
  61. block_num = c.getblockcount()
  62. # print 'secs:',secs, 'last block:',last_block
  63. top,bot = block_num,0
  64. m = 'Searching for block'
  65. msg_r(m)
  66. for i in range(40):
  67. msg_r('.')
  68. bhash = c.getblockhash(block_num)
  69. block = c.getblock(bhash)
  70. # print 'block_num:',block_num, 'mediantime:',block['mediantime'], 'target:',secs
  71. cur_secs = block['mediantime']
  72. if cur_secs > secs:
  73. top = block_num
  74. else:
  75. bot = block_num
  76. block_num = (top + bot) / 2
  77. if top - bot < 2:
  78. msg('\nFound: %s ' % block_num)
  79. break
  80. return block_num
  81. tx = MMGenTX()
  82. [tx.txid,send_amt,tx.timestamp],tx.hex,inputs,b2m_map,tx.label = parse_tx_file(cmd_args[0])
  83. tx.send_amt = Decimal(send_amt)
  84. c = bitcoin_connection()
  85. # attrs = 'txid','vout','amt','comment','mmid','addr','wif'
  86. #pp_msg(inputs)
  87. for i in inputs:
  88. if not 'mmid' in i and 'account' in i:
  89. from mmgen.tw import parse_tw_acct_label
  90. a,b = parse_tw_acct_label(i['account'])
  91. if a:
  92. i['mmid'] = a.decode('utf8')
  93. if b: i['comment'] = b.decode('utf8')
  94. #pp_msg(inputs)
  95. tx.inputs = tx.decode_io_oldfmt(inputs)
  96. if tx.check_signed(c):
  97. msg('Transaction is signed')
  98. dec_tx = c.decoderawtransaction(tx.hex)
  99. tx.outputs = [MMGenTxOutput(addr=i['scriptPubKey']['addresses'][0],amt=i['value'])
  100. for i in dec_tx['vout']]
  101. for e in tx.outputs:
  102. if e.addr in b2m_map:
  103. f = b2m_map[e.addr]
  104. e.mmid = f[0]
  105. if f[1]: e.label = f[1].decode('utf8')
  106. else:
  107. for f in tx.inputs:
  108. if e.addr == f.addr and f.mmid:
  109. e.mmid = f.mmid
  110. if f.label: e.label = f.label.decode('utf8')
  111. #for i in tx.inputs: print i
  112. #for i in tx.outputs: print i
  113. #die(1,'')
  114. tx.blockcount = find_block_by_time(c,tx.timestamp)
  115. tx.write_to_file(ask_write=False)