twbal.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. twbal: Tracking wallet getbalance class for the MMGen suite
  20. """
  21. from .color import red,green
  22. from .util import base_proto_subclass
  23. from .base_obj import AsyncInit
  24. from .objmethods import MMGenObject
  25. from .rpc import rpc_init
  26. class TwGetBalance(MMGenObject,metaclass=AsyncInit):
  27. def __new__(cls,proto,*args,**kwargs):
  28. return MMGenObject.__new__(base_proto_subclass(cls,proto,'twbal'))
  29. async def __init__(self,proto,minconf,quiet):
  30. self.minconf = minconf
  31. self.quiet = quiet
  32. self.data = {k:[proto.coin_amt('0')] * 4 for k in ('TOTAL','Non-MMGen','Non-wallet')}
  33. self.rpc = await rpc_init(proto)
  34. self.proto = proto
  35. await self.create_data()
  36. def format(self):
  37. def gen_output():
  38. if self.proto.chain_name != 'mainnet':
  39. yield 'Chain: ' + green(self.proto.chain_name.upper())
  40. if self.quiet:
  41. yield str(self.data['TOTAL'][2] if self.data else 0)
  42. else:
  43. yield self.fs.format(
  44. w = 'Wallet',
  45. u = ' Unconfirmed',
  46. p = f' <{self.minconf} confirms',
  47. c = f' >={self.minconf} confirms' )
  48. for key in sorted(self.data):
  49. if not any(self.data[key]):
  50. continue
  51. yield self.fs.format(**dict(zip(
  52. ('w','u','p','c'),
  53. [key+':'] + [a.fmt(color=True,suf=' '+self.proto.dcoin) for a in self.data[key]]
  54. )))
  55. for key,vals in list(self.data.items()):
  56. if key == 'TOTAL':
  57. continue
  58. if vals[3]:
  59. yield red(f'Warning: this wallet contains PRIVATE KEYS for {key} outputs!')
  60. return '\n'.join(gen_output()).rstrip()