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