main_txfind.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 'quiet': {
  47. 'none': 'None',
  48. 'block': '{b} {c}',
  49. 'mem': 'mempool'}}
  50. async def main(txid):
  51. if len(txid) != 64 or not is_hex_str(txid):
  52. die(2, f'{txid}: invalid transaction ID')
  53. if cfg.verbose:
  54. msg(f'TxID: {txid}')
  55. from mmgen.rpc import rpc_init
  56. c = await rpc_init(cfg, ignore_wallet=True)
  57. exitval = 0
  58. try:
  59. tip1 = await c.call('getblockcount')
  60. ret = await c.call('getrawtransaction', txid, True)
  61. tip2 = await c.call('getblockcount')
  62. except:
  63. Msg('\r' + msgs['none'])
  64. exitval = 1
  65. else:
  66. assert tip1 == tip2, 'Blockchain is updating. Try again later'
  67. if 'confirmations' in ret:
  68. confs = ret['confirmations']
  69. Msg('\r' + msgs['block'].format(b = tip1 - confs + 1, c = confs))
  70. else:
  71. Msg('\r' + msgs['mem'])
  72. return exitval
  73. cfg = Config(opts_data=opts_data)
  74. msgs = msg_data['quiet' if cfg.quiet else 'normal']
  75. if len(cfg._args) != 1:
  76. die(1, 'One transaction ID must be specified')
  77. sys.exit(async_run(cfg, main, args=[cfg._args[0]]))