main_txfind.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free Software
  8. # Foundation, either version 3 of the License, or (at your option) any later
  9. # version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU General Public License along with
  17. # this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmnode-txfind: Find a transaction in the blockchain or mempool
  20. """
  21. import sys
  22. from mmgen.cfg import Config
  23. from mmgen.util import msg,Msg,die,is_hex_str,async_run
  24. opts_data = {
  25. 'text': {
  26. 'desc': 'Find a transaction in the blockchain or mempool',
  27. 'usage': '[opts] <transaction ID>',
  28. 'options': """
  29. -h, --help Print this help message
  30. --, --longhelp Print help message for long options (common options)
  31. -q, --quiet Be quieter
  32. -v, --verbose Be more verbose
  33. """,
  34. 'notes': """
  35. If transaction is in blockchain, the block number and number of confirmations
  36. are displayed.
  37. Requires --txindex for correct operation.
  38. """
  39. }
  40. }
  41. msg_data = {
  42. 'normal': {
  43. 'none': 'Transaction not found in blockchain or mempool',
  44. 'block': 'Transaction is in block {b} ({c} confirmations)',
  45. 'mem': 'Transaction is in mempool',
  46. },
  47. 'quiet': {
  48. 'none': 'None',
  49. 'block': '{b} {c}',
  50. 'mem': 'mempool',
  51. }
  52. }
  53. async def main(txid):
  54. if len(txid) != 64 or not is_hex_str(txid):
  55. die(2,f'{txid}: invalid transaction ID')
  56. if cfg.verbose:
  57. msg(f'TxID: {txid}')
  58. from mmgen.rpc import rpc_init
  59. c = await rpc_init(cfg,ignore_wallet=True)
  60. exitval = 0
  61. try:
  62. tip1 = await c.call('getblockcount')
  63. ret = await c.call('getrawtransaction',txid,True)
  64. tip2 = await c.call('getblockcount')
  65. except:
  66. Msg('\r' + msgs['none'])
  67. exitval = 1
  68. else:
  69. assert tip1 == tip2, 'Blockchain is updating. Try again later'
  70. if 'confirmations' in ret:
  71. confs = ret['confirmations']
  72. Msg('\r' + msgs['block'].format(b = tip1 - confs + 1, c = confs))
  73. else:
  74. Msg('\r' + msgs['mem'])
  75. return exitval
  76. cfg = Config(opts_data=opts_data)
  77. msgs = msg_data['quiet' if cfg.quiet else 'normal']
  78. if len(cfg._args) != 1:
  79. die(1,'One transaction ID must be specified')
  80. sys.exit(async_run(main(cfg._args[0])))