rpc.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. tool/rpc.py: JSON/RPC routines for the 'mmgen-tool' utility
  20. """
  21. from .common import tool_cmd_base,options_annot_str
  22. from ..tw.common import TwCommon
  23. from ..tw.txhistory import TwTxHistory
  24. class tool_cmd(tool_cmd_base):
  25. "tracking-wallet commands using the JSON-RPC interface"
  26. need_proto = True
  27. need_amt = True
  28. async def daemon_version(self):
  29. "print coin daemon version"
  30. from ..rpc import rpc_init
  31. r = await rpc_init( self.proto, ignore_daemon_version=True )
  32. return f'{r.daemon.coind_name} version {r.daemon_version} ({r.daemon_version_str})'
  33. async def getbalance(self,
  34. minconf: 'minimum number of confirmations' = 1,
  35. quiet: 'produce quieter output' = False,
  36. pager: 'send output to pager' = False ):
  37. "list confirmed/unconfirmed, spendable/unspendable balances in tracking wallet"
  38. from ..tw.bal import TwGetBalance
  39. return (await TwGetBalance(self.proto,minconf,quiet)).format()
  40. async def listaddress(self,
  41. mmgen_addr:str,
  42. minconf: 'minimum number of confirmations' = 1,
  43. showbtcaddr: 'display coin address in addition to MMGen ID' = True,
  44. age_fmt: 'format for the Age/Date column ' + options_annot_str(TwCommon.age_fmts) = 'confs' ):
  45. "list the specified MMGen address in the tracking wallet and its balance"
  46. return await self.listaddresses(
  47. mmgen_addrs = mmgen_addr,
  48. minconf = minconf,
  49. showbtcaddrs = showbtcaddr,
  50. age_fmt = age_fmt )
  51. async def listaddresses(self,
  52. mmgen_addrs: 'hyphenated range or comma-separated list of addresses' = '',
  53. minconf: 'minimum number of confirmations' = 1,
  54. pager: 'send output to pager' = False,
  55. showbtcaddrs: 'display coin addresses in addition to MMGen IDs' = True,
  56. showempty: 'show addresses with no balances' = True,
  57. all_labels: 'show all addresses with labels' = False,
  58. age_fmt: 'format for the Age/Date column ' + options_annot_str(TwCommon.age_fmts) = 'confs',
  59. sort: 'address sort order ' + options_annot_str(['reverse','age']) = '' ):
  60. "list MMGen addresses in the tracking wallet and their balances"
  61. show_age = bool(age_fmt)
  62. if sort:
  63. sort = set(sort.split(','))
  64. sort_params = {'reverse','age'}
  65. if not sort.issubset( sort_params ):
  66. from ..util import die
  67. die(1,"The sort option takes the following parameters: '{}'".format( "','".join(sort_params) ))
  68. usr_addr_list = []
  69. if mmgen_addrs:
  70. a = mmgen_addrs.rsplit(':',1)
  71. if len(a) != 2:
  72. from ..util import die
  73. die(1,
  74. f'{mmgen_addrs}: invalid address list argument ' +
  75. '(must be in form <seed ID>:[<type>:]<idx list>)' )
  76. from ..addr import MMGenID
  77. from ..addrlist import AddrIdxList
  78. usr_addr_list = [MMGenID(self.proto,f'{a[0]}:{i}') for i in AddrIdxList(a[1])]
  79. from ..tw.addrs import TwAddrList
  80. al = await TwAddrList( self.proto, usr_addr_list, minconf, showempty, showbtcaddrs, all_labels )
  81. if not al:
  82. from ..util import die
  83. die(0,('No tracked addresses with balances!','No tracked addresses!')[showempty])
  84. return await al.format( showbtcaddrs, sort, show_age, age_fmt or 'confs' )
  85. async def twops(self,
  86. obj,pager,reverse,detail,sort,age_fmt,interactive,show_mmid):
  87. obj.interactive = interactive
  88. obj.reverse = reverse
  89. obj.age_fmt = age_fmt
  90. obj.show_mmid = show_mmid
  91. await obj.get_data(sort_key=sort,reverse_sort=reverse)
  92. if interactive:
  93. await obj.view_and_sort()
  94. return True
  95. elif detail:
  96. return await obj.format_detail( color=True )
  97. else:
  98. return await obj.format_squeezed()
  99. async def twview(self,
  100. pager: 'send output to pager' = False,
  101. reverse: 'reverse order of unspent outputs' = False,
  102. wide: 'display data in wide tabular format' = False,
  103. minconf: 'minimum number of confirmations' = 1,
  104. sort: 'unspent output sort order ' + options_annot_str(TwCommon.sort_funcs) = 'age',
  105. age_fmt: 'format for the Age/Date column ' + options_annot_str(TwCommon.age_fmts) = 'confs',
  106. interactive: 'enable interactive operation' = False,
  107. show_mmid: 'show MMGen IDs along with coin addresses' = True ):
  108. "view tracking wallet unspent outputs"
  109. from ..tw.unspent import TwUnspentOutputs
  110. obj = await TwUnspentOutputs(self.proto,minconf=minconf)
  111. ret = await self.twops(
  112. obj,pager,reverse,wide,sort,age_fmt,interactive,show_mmid)
  113. del obj.wallet
  114. return ret
  115. async def txhist(self,
  116. pager: 'send output to pager' = False,
  117. reverse: 'reverse order of transactions' = False,
  118. detail: 'produce detailed, non-tabular output' = False,
  119. sinceblock: 'display transactions starting from this block' = 0,
  120. sort: 'transaction sort order ' + options_annot_str(TwTxHistory.sort_funcs) = 'age',
  121. age_fmt: 'format for the Age/Date column ' + options_annot_str(TwCommon.age_fmts) = 'confs',
  122. interactive: 'enable interactive operation' = False ):
  123. "view transaction history of tracking wallet"
  124. obj = await TwTxHistory(self.proto,sinceblock=sinceblock)
  125. return await self.twops(
  126. obj,pager,reverse,detail,sort,age_fmt,interactive,show_mmid=None)
  127. async def add_label(self,mmgen_or_coin_addr:str,label:str):
  128. "add descriptive label for address in tracking wallet"
  129. from ..tw.ctl import TrackingWallet
  130. await (await TrackingWallet(self.proto,mode='w')).add_comment( mmgen_or_coin_addr, label, on_fail='raise' )
  131. return True
  132. async def remove_label(self,mmgen_or_coin_addr:str):
  133. "remove descriptive label for address in tracking wallet"
  134. await self.add_label( mmgen_or_coin_addr, '' )
  135. return True
  136. async def remove_address(self,mmgen_or_coin_addr:str):
  137. "remove an address from tracking wallet"
  138. from ..tw.ctl import TrackingWallet
  139. # returns None on failure:
  140. ret = await (await TrackingWallet(self.proto,mode='w')).remove_address(mmgen_or_coin_addr)
  141. if ret:
  142. from ..util import msg
  143. msg(f'Address {ret!r} deleted from tracking wallet')
  144. return ret
  145. async def resolve_address(self,mmgen_or_coin_addr:str):
  146. "resolve an MMGen address in the tracking wallet to a coin address or vice-versa"
  147. from ..tw.ctl import TrackingWallet
  148. ret = await (await TrackingWallet(self.proto,mode='w')).resolve_address( mmgen_or_coin_addr )
  149. if ret:
  150. from ..util import Msg
  151. from ..addr import is_coin_addr
  152. return ret.mmaddr if is_coin_addr(self.proto,mmgen_or_coin_addr) else ret.coinaddr
  153. else:
  154. return False
  155. async def rescan_address(self,mmgen_or_coin_addr:str):
  156. "rescan an address in the tracking wallet to update its balance"
  157. from ..tw.ctl import TrackingWallet
  158. return await (await TrackingWallet(self.proto,mode='w')).rescan_address( mmgen_or_coin_addr )
  159. async def rescan_blockchain(self,
  160. start_block: int = None,
  161. stop_block: int = None ):
  162. """
  163. rescan the blockchain to update historical transactions in the tracking wallet
  164. NOTE:
  165. The rescanning process typically takes several hours and may be interrupted
  166. using Ctrl-C. An interrupted rescan may be resumed using the ‘start_block’
  167. parameter.
  168. """
  169. from ..tw.ctl import TrackingWallet
  170. ret = await (await TrackingWallet(self.proto,mode='w')).rescan_blockchain(start_block,stop_block)
  171. return True
  172. async def twexport(self,include_amts=True,pretty=False):
  173. """
  174. export a tracking wallet to JSON format
  175. NOTES:
  176. If ‘include_amts’ is true (the default), Ethereum balances will be restored
  177. from the dump upon import. For Bitcoin and forks, amount fields in the dump
  178. are ignored.
  179. If ‘pretty’ is true, JSON will be dumped in human-readable format to allow
  180. for editing of comment fields.
  181. """
  182. from ..tw.json import TwJSON
  183. await TwJSON.Export( self.proto, include_amts=include_amts, pretty=pretty )
  184. return True
  185. async def twimport(self,filename:str,ignore_checksum=False,batch=False):
  186. """
  187. restore a tracking wallet from a JSON dump created by ‘twexport’
  188. NOTES:
  189. If comment fields in the JSON dump have been edited, ‘ignore_checksum’ must
  190. be set to true.
  191. The restored tracking wallet will have correct balances but no record of
  192. historical transactions. These may be restored by running ‘mmgen-tool
  193. rescan_blockchain’.
  194. """
  195. from ..tw.json import TwJSON
  196. await TwJSON.Import( self.proto, filename, ignore_checksum=ignore_checksum, batch=batch )
  197. return True