main_txfind.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. from mmgen.cfg import Config
  22. from mmgen.util import msg,Msg,die
  23. opts_data = {
  24. 'text': {
  25. 'desc': 'Find a transaction in the blockchain or mempool',
  26. 'usage': '[opts] <transaction ID>',
  27. 'options': """
  28. -h, --help Print this help message
  29. --, --longhelp Print help message for long options (common options)
  30. -q, --quiet Be quieter
  31. -v, --verbose Be more verbose
  32. """,
  33. 'notes': """
  34. If transaction is in blockchain, the block number and number of confirmations
  35. are displayed.
  36. Requires --txindex for correct operation.
  37. """
  38. }
  39. }
  40. msg_data = {
  41. 'normal': {
  42. 'none': 'Transaction not found in blockchain or mempool',
  43. 'block': 'Transaction is in block {b} ({c} confirmations)',
  44. 'mem': 'Transaction is in mempool',
  45. },
  46. 'quiet': {
  47. 'none': 'None',
  48. 'block': '{b} {c}',
  49. 'mem': 'mempool',
  50. }
  51. }
  52. async def main(txid):
  53. if len(txid) != 64 or not is_hex_str(txid):
  54. die(2,f'{txid}: invalid transaction ID')
  55. if cfg.verbose:
  56. msg(f'TxID: {txid}')
  57. from mmgen.rpc import rpc_init
  58. c = await rpc_init(cfg,ignore_wallet=True)
  59. exitval = 0
  60. try:
  61. tip1 = await c.call('getblockcount')
  62. ret = await c.call('getrawtransaction',txid,True)
  63. tip2 = await c.call('getblockcount')
  64. except:
  65. Msg('\r' + msgs['none'])
  66. exitval = 1
  67. else:
  68. assert tip1 == tip2, 'Blockchain is updating. Try again later'
  69. if 'confirmations' in ret:
  70. confs = ret['confirmations']
  71. Msg('\r' + msgs['block'].format(b = tip1 - confs + 1, c = confs))
  72. else:
  73. Msg('\r' + msgs['mem'])
  74. return exitval
  75. cfg = Config(opts_data=opts_data)
  76. msgs = msg_data['quiet' if cfg.quiet else 'normal']
  77. if len(cfg._args) != 1:
  78. die(1,'One transaction ID must be specified')
  79. sys.exit(async_run(main(cfg._args[0])))