main_txfind.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 opt.verbose:
  55. msg(f'TxID: {txid}')
  56. from mmgen.protocol import init_proto_from_opts
  57. proto = init_proto_from_opts()
  58. from mmgen.rpc import rpc_init
  59. c = await rpc_init(proto)
  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. cmd_args = opts.init(opts_data)
  77. msgs = msg_data['quiet' if opt.quiet else 'normal']
  78. if len(cmd_args) != 1:
  79. die(1,'One transaction ID must be specified')
  80. sys.exit(async_run(main(cmd_args[0])))