api.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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.api: Python interface to selected commands from the 'mmgen-tool' utility
  20. """
  21. from .common import tool_cmd_base
  22. from .util import tool_cmd as util_cmds
  23. from .coin import tool_cmd as coin_cmds
  24. from .mnemonic import tool_cmd as mnemonic_cmds
  25. class tool_api(
  26. util_cmds,
  27. coin_cmds,
  28. mnemonic_cmds,
  29. tool_cmd_base ):
  30. """
  31. API providing access to a subset of methods from the mmgen.tool module
  32. Example:
  33. from mmgen.tool.api import tool_api
  34. tool = tool_api()
  35. # Set the coin and network:
  36. tool.init_coin('btc','mainnet')
  37. # Print available address types:
  38. tool.print_addrtypes()
  39. # Set the address type:
  40. tool.addrtype = 'segwit'
  41. # Disable user entropy gathering (optional, reduces security):
  42. tool.usr_randchars = 0
  43. # Generate a random BTC segwit keypair:
  44. wif,addr = tool.randpair()
  45. # Set coin, network and address type:
  46. tool.init_coin('ltc','testnet')
  47. tool.addrtype = 'bech32'
  48. # Generate a random LTC testnet Bech32 keypair:
  49. wif,addr = tool.randpair()
  50. """
  51. need_proto = True
  52. need_addrtype = True
  53. def __init__(self):
  54. """
  55. Initializer - takes no arguments
  56. """
  57. import mmgen.opts as opts
  58. from ..opts import opt
  59. opts.UserOpts._reset_ok += ('usr_randchars',)
  60. if not opt._lock:
  61. opts.init()
  62. super().__init__()
  63. def init_coin(self,coinsym,network):
  64. """
  65. Initialize a coin/network pair
  66. Valid choices for coins: one of the symbols returned by the 'coins' attribute
  67. Valid choices for network: 'mainnet','testnet','regtest'
  68. """
  69. from ..protocol import init_proto,warn_trustlevel
  70. warn_trustlevel(coinsym)
  71. self.proto = init_proto(coinsym,network=network,need_amt=True)
  72. return self.proto
  73. @property
  74. def coins(self):
  75. """The available coins"""
  76. from ..protocol import CoinProtocol
  77. from ..altcoin import CoinInfo
  78. return sorted(set(
  79. [c.upper() for c in CoinProtocol.coins]
  80. + [c.symbol for c in CoinInfo.get_supported_coins(self.proto.network)]
  81. ))
  82. @property
  83. def coin(self):
  84. """The currently configured coin"""
  85. return self.proto.coin
  86. @property
  87. def network(self):
  88. """The currently configured network"""
  89. return self.proto.network
  90. @property
  91. def addrtypes(self):
  92. """
  93. The available address types for current coin/network pair. The
  94. first-listed is the default
  95. """
  96. from ..addr import MMGenAddrType
  97. return [MMGenAddrType(proto=self.proto,id_str=id_str).name for id_str in self.proto.mmtypes]
  98. def print_addrtypes(self):
  99. """
  100. Print the available address types for current coin/network pair along with
  101. a description. The first-listed is the default
  102. """
  103. from ..addr import MMGenAddrType
  104. for t in [MMGenAddrType(proto=self.proto,id_str=id_str) for id_str in self.proto.mmtypes]:
  105. print(f'{t.name:<12} - {t.desc}')
  106. @property
  107. def addrtype(self):
  108. """The currently configured address type (is assignable)"""
  109. return self.mmtype
  110. @addrtype.setter
  111. def addrtype(self,val):
  112. from ..addr import MMGenAddrType
  113. self.mmtype = MMGenAddrType(self.proto,val)
  114. @property
  115. def usr_randchars(self):
  116. """
  117. The number of keystrokes of entropy to be gathered from the user.
  118. Setting to zero disables user entropy gathering.
  119. """
  120. from ..opts import opt
  121. return opt.usr_randchars
  122. @usr_randchars.setter
  123. def usr_randchars(self,val):
  124. from ..opts import opt
  125. opt.usr_randchars = val