addrgen.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. addrgen.py: Address and view key generation classes for the MMGen suite
  20. """
  21. from .proto.common import hash160,b58chk_encode
  22. from .addr import CoinAddr,MMGenAddrType,MoneroViewKey,ZcashViewKey
  23. # decorator for to_addr() and to_viewkey()
  24. def check_data(orig_func):
  25. def f(self,data):
  26. assert data.pubkey_type == self.pubkey_type, 'addrgen.py:check_data() pubkey_type mismatch'
  27. assert data.compressed == self.compressed,(
  28. f'addrgen.py:check_data() expected compressed={self.compressed} but got compressed={data.compressed}'
  29. )
  30. return orig_func(self,data)
  31. return f
  32. class addr_generator:
  33. """
  34. provide a generator for each supported address format
  35. """
  36. class base:
  37. def __init__(self,proto,addr_type):
  38. self.proto = proto
  39. self.pubkey_type = addr_type.pubkey_type
  40. self.compressed = addr_type.compressed
  41. desc = f'AddrGenerator {type(self).__name__!r}'
  42. def to_segwit_redeem_script(self,data):
  43. raise NotImplementedError('Segwit redeem script not supported by this address type')
  44. class p2pkh(base):
  45. @check_data
  46. def to_addr(self,data):
  47. return CoinAddr(
  48. self.proto,
  49. self.proto.pubhash2addr( hash160(data.pubkey), p2sh=False ))
  50. class legacy(p2pkh): pass
  51. class compressed(p2pkh): pass
  52. class segwit(base):
  53. @check_data
  54. def to_addr(self,data):
  55. return CoinAddr(
  56. self.proto,
  57. self.proto.pubhash2segwitaddr( hash160(data.pubkey)) )
  58. def to_segwit_redeem_script(self,data): # NB: returns hex
  59. return self.proto.pubhash2redeem_script( hash160(data.pubkey) ).hex()
  60. class bech32(base):
  61. @check_data
  62. def to_addr(self,data):
  63. return CoinAddr(
  64. self.proto,
  65. self.proto.pubhash2bech32addr( hash160(data.pubkey) ))
  66. class keccak(base):
  67. def __init__(self,proto,addr_type):
  68. super().__init__(proto,addr_type)
  69. from .util import get_keccak
  70. self.keccak_256 = get_keccak()
  71. class ethereum(keccak):
  72. @check_data
  73. def to_addr(self,data):
  74. return CoinAddr(
  75. self.proto,
  76. self.keccak_256(data.pubkey[1:]).hexdigest()[24:] )
  77. class monero(keccak):
  78. def b58enc(self,addr_bytes):
  79. from .baseconv import baseconv
  80. enc = baseconv('b58').frombytes
  81. l = len(addr_bytes)
  82. a = ''.join([enc( addr_bytes[i*8:i*8+8], pad=11, tostr=True ) for i in range(l//8)])
  83. b = enc( addr_bytes[l-l%8:], pad=7, tostr=True )
  84. return a + b
  85. @check_data
  86. def to_addr(self,data):
  87. step1 = self.proto.addr_fmt_to_ver_bytes('monero') + data.pubkey
  88. return CoinAddr(
  89. proto = self.proto,
  90. addr = self.b58enc( step1 + self.keccak_256(step1).digest()[:4]) )
  91. @check_data
  92. def to_viewkey(self,data):
  93. return MoneroViewKey( data.viewkey_bytes.hex() )
  94. class zcash_z(base):
  95. @check_data
  96. def to_addr(self,data):
  97. ret = b58chk_encode(
  98. self.proto.addr_fmt_to_ver_bytes('zcash_z')
  99. + data.pubkey )
  100. return CoinAddr( self.proto, ret )
  101. @check_data
  102. def to_viewkey(self,data):
  103. ret = b58chk_encode(
  104. self.proto.addr_fmt_to_ver_bytes('viewkey')
  105. + data.viewkey_bytes )
  106. return ZcashViewKey( self.proto, ret )