rpc.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 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: JSON/RPC routines for the 'mmgen-tool' utility
  20. """
  21. from .common import tool_cmd_base,options_annot_str
  22. from ..tw.view import TwView
  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 ..daemon import CoinDaemon
  31. d = CoinDaemon( cfg=self.cfg, proto=self.proto, test_suite=self.cfg.test_suite )
  32. if self.proto.base_proto == 'Monero':
  33. from ..proto.xmr.rpc import MoneroRPCClient
  34. r = MoneroRPCClient(
  35. cfg = self.cfg,
  36. proto = self.proto,
  37. daemon = d,
  38. host = 'localhost',
  39. port = d.rpc_port,
  40. user = None,
  41. passwd = None,
  42. ignore_daemon_version = True )
  43. else:
  44. from ..rpc import rpc_init
  45. r = await rpc_init(self.cfg, self.proto, ignore_daemon_version=True, ignore_wallet=True)
  46. return f'{d.coind_name} version {r.daemon_version} ({r.daemon_version_str})'
  47. async def getbalance(self,
  48. minconf: 'minimum number of confirmations' = 1,
  49. quiet: 'produce quieter output' = False,
  50. pager: 'send output to pager' = False ):
  51. "list confirmed/unconfirmed, spendable/unspendable balances in tracking wallet"
  52. from ..tw.bal import TwGetBalance
  53. return (await TwGetBalance(self.cfg,self.proto,minconf,quiet)).format(color=self.cfg.color)
  54. async def twops(self,
  55. obj,pager,reverse,detail,sort,age_fmt,interactive,
  56. **kwargs ):
  57. obj.reverse = reverse
  58. obj.age_fmt = age_fmt
  59. for k,v in kwargs.items():
  60. setattr(obj,k,v)
  61. await obj.get_data(sort_key=sort,reverse_sort=reverse)
  62. if interactive:
  63. await obj.view_filter_and_sort()
  64. ret = True
  65. else:
  66. ret = await obj.format('detail' if detail else 'squeezed')
  67. if hasattr(obj,'twctl'):
  68. del obj.twctl
  69. return ret
  70. async def twview(self,
  71. pager: 'send output to pager' = False,
  72. reverse: 'reverse order of unspent outputs' = False,
  73. wide: 'display data in wide tabular format' = False,
  74. minconf: 'minimum number of confirmations' = 1,
  75. sort: 'unspent output sort order ' + options_annot_str(TwView.sort_funcs) = 'age',
  76. age_fmt: 'format for the Age/Date column ' + options_annot_str(TwView.age_fmts) = 'confs',
  77. interactive: 'enable interactive operation' = False,
  78. show_mmid: 'show MMGen IDs along with coin addresses' = True ):
  79. "view tracking wallet unspent outputs"
  80. from ..tw.unspent import TwUnspentOutputs
  81. obj = await TwUnspentOutputs(self.cfg,self.proto,minconf=minconf)
  82. return await self.twops(
  83. obj,pager,reverse,wide,sort,age_fmt,interactive,
  84. show_mmid = show_mmid )
  85. async def txhist(self,
  86. pager: 'send output to pager' = False,
  87. reverse: 'reverse order of transactions' = False,
  88. detail: 'produce detailed, non-tabular output' = False,
  89. sinceblock: 'display transactions starting from this block' = 0,
  90. sort: 'transaction sort order ' + options_annot_str(TwTxHistory.sort_funcs) = 'age',
  91. age_fmt: 'format for the Age/Date column ' + options_annot_str(TwView.age_fmts) = 'confs',
  92. interactive: 'enable interactive operation' = False ):
  93. "view transaction history of tracking wallet"
  94. obj = await TwTxHistory(self.cfg,self.proto,sinceblock=sinceblock)
  95. return await self.twops(
  96. obj,pager,reverse,detail,sort,age_fmt,interactive )
  97. async def listaddress(self,
  98. mmgen_addr:str,
  99. wide: 'display data in wide tabular format' = False,
  100. minconf: 'minimum number of confirmations' = 1,
  101. showcoinaddr: 'display coin address in addition to MMGen ID' = True,
  102. age_fmt: 'format for the Age/Date column ' + options_annot_str(TwView.age_fmts) = 'confs' ):
  103. "list the specified MMGen address in the tracking wallet and its balance"
  104. return await self.listaddresses(
  105. mmgen_addrs = mmgen_addr,
  106. wide = wide,
  107. minconf = minconf,
  108. showcoinaddrs = showcoinaddr,
  109. age_fmt = age_fmt )
  110. async def listaddresses(self,
  111. pager: 'send output to pager' = False,
  112. reverse: 'reverse order of unspent outputs' = False,
  113. wide: 'display data in wide tabular format' = False,
  114. minconf: 'minimum number of confirmations' = 1,
  115. sort: 'address sort order ' + options_annot_str(['reverse','mmid','addr','amt']) = '',
  116. age_fmt: 'format for the Age/Date column ' + options_annot_str(TwView.age_fmts) = 'confs',
  117. interactive: 'enable interactive operation' = False,
  118. mmgen_addrs: 'hyphenated range or comma-separated list of addresses' = '',
  119. showcoinaddrs:'display coin addresses in addition to MMGen IDs' = True,
  120. showempty: 'show addresses with no balances' = True,
  121. showused: 'show used addresses (tristate: 0=no, 1=yes, 2=all)' = 1,
  122. all_labels: 'show all addresses with labels' = False ):
  123. "list MMGen addresses in the tracking wallet and their balances"
  124. assert showused in (0,1,2), "‘showused’ must have a value of 0, 1 or 2"
  125. from ..tw.addresses import TwAddresses
  126. obj = await TwAddresses(self.cfg,self.proto,minconf=minconf,mmgen_addrs=mmgen_addrs)
  127. return await self.twops(
  128. obj,pager,reverse,wide,sort,age_fmt,interactive,
  129. showcoinaddrs = showcoinaddrs,
  130. showempty = showempty,
  131. showused = showused,
  132. all_labels = all_labels )
  133. async def add_label(self,mmgen_or_coin_addr:str,label:str):
  134. "add descriptive label for address in tracking wallet"
  135. from ..tw.ctl import TwCtl
  136. return await (await TwCtl(self.cfg,self.proto,mode='w')).set_comment(mmgen_or_coin_addr,label)
  137. async def remove_label(self,mmgen_or_coin_addr:str):
  138. "remove descriptive label for address in tracking wallet"
  139. await self.add_label( mmgen_or_coin_addr, '' )
  140. return True
  141. async def remove_address(self,mmgen_or_coin_addr:str):
  142. "remove an address from tracking wallet"
  143. from ..tw.ctl import TwCtl
  144. # returns None on failure:
  145. ret = await (await TwCtl(self.cfg,self.proto,mode='w')).remove_address(mmgen_or_coin_addr)
  146. if ret:
  147. from ..util import msg
  148. msg(f'Address {ret!r} deleted from tracking wallet')
  149. return ret
  150. async def resolve_address(self,mmgen_or_coin_addr:str):
  151. "resolve an MMGen address in the tracking wallet to a coin address or vice-versa"
  152. from ..tw.ctl import TwCtl
  153. ret = await (await TwCtl(self.cfg,self.proto,mode='w')).resolve_address( mmgen_or_coin_addr )
  154. if ret:
  155. from ..addr import is_coin_addr
  156. return ret.twmmid if is_coin_addr(self.proto,mmgen_or_coin_addr) else ret.coinaddr
  157. else:
  158. return False
  159. async def rescan_address(self,mmgen_or_coin_addr:str):
  160. "rescan an address in the tracking wallet to update its balance"
  161. from ..tw.ctl import TwCtl
  162. return await (await TwCtl(self.cfg,self.proto,mode='w')).rescan_address( mmgen_or_coin_addr )
  163. async def rescan_blockchain(self,
  164. start_block: int = None,
  165. stop_block: int = None ):
  166. """
  167. rescan the blockchain to update historical transactions in the tracking wallet
  168. NOTE:
  169. The rescanning process typically takes several hours and may be interrupted
  170. using Ctrl-C. An interrupted rescan may be resumed using the ‘start_block’
  171. parameter.
  172. """
  173. from ..tw.ctl import TwCtl
  174. await (await TwCtl(self.cfg,self.proto,mode='w')).rescan_blockchain(start_block,stop_block)
  175. return True
  176. async def twexport(self,include_amts=True,pretty=False,prune=False,warn_used=False,force=False):
  177. """
  178. export a tracking wallet to JSON format
  179. NOTES:
  180. If ‘include_amts’ is true (the default), Ethereum balances will be restored
  181. from the dump upon import. For Bitcoin and forks, amount fields in the dump
  182. are ignored.
  183. If ‘pretty’ is true, JSON will be dumped in human-readable format to allow
  184. for editing of comment fields.
  185. If ‘prune’ is true, an interactive menu will be launched allowing the user
  186. to prune unwanted addresses before creating the JSON dump. Pruning has no
  187. effect on the existing tracking wallet.
  188. If ‘warn_used’ is true, the user will be prompted before pruning used
  189. addresses.
  190. If ‘force’ is true, any existing dump will be overwritten without prompting.
  191. """
  192. from ..tw.json import TwJSON
  193. await TwJSON.Export(
  194. self.cfg,
  195. self.proto,
  196. include_amts = include_amts,
  197. pretty = pretty,
  198. prune = prune,
  199. warn_used = warn_used,
  200. force_overwrite = force )
  201. return True
  202. async def twimport(self,filename:str,ignore_checksum=False,batch=False):
  203. """
  204. restore a tracking wallet from a JSON dump created by ‘twexport’
  205. NOTES:
  206. If comment fields in the JSON dump have been edited, ‘ignore_checksum’ must
  207. be set to true.
  208. The restored tracking wallet will have correct balances but no record of
  209. historical transactions. These may be restored by running ‘mmgen-tool
  210. rescan_blockchain’.
  211. """
  212. from ..tw.json import TwJSON
  213. await TwJSON.Import( self.cfg, self.proto, filename, ignore_checksum=ignore_checksum, batch=batch )
  214. return True