btc.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen
  9. # https://gitlab.com/mmgen/mmgen
  10. """
  11. Bitcoin protocol
  12. """
  13. from ..protocol import CoinProtocol,parsed_wif,parsed_addr,_finfo,_nw
  14. from .common import *
  15. class mainnet(CoinProtocol.Secp256k1): # chainparams.cpp
  16. """
  17. All Bitcoin code and chain forks inherit from this class
  18. """
  19. mod_clsname = 'Bitcoin'
  20. network_names = _nw('mainnet','testnet','regtest')
  21. addr_ver_bytes = { '00': 'p2pkh', '05': 'p2sh' }
  22. addr_len = 20
  23. wif_ver_num = { 'std': '80' }
  24. mmtypes = ('L','C','S','B')
  25. dfl_mmtype = 'L'
  26. coin_amt = 'BTCAmt'
  27. max_tx_fee = '0.003'
  28. sighash_type = 'ALL'
  29. block0 = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
  30. forks = [
  31. _finfo(478559,'00000000000000000019f112ec0a9982926f1258cdcc558dd7c3b7e5dc7fa148','BCH',False),
  32. ]
  33. caps = ('rbf','segwit')
  34. mmcaps = ('key','addr','rpc','tx')
  35. base_coin = 'BTC'
  36. base_proto = 'Bitcoin'
  37. # From BIP173: witness version 'n' is stored as 'OP_n'. OP_0 is encoded as 0x00,
  38. # but OP_1 through OP_16 are encoded as 0x51 though 0x60 (81 to 96 in decimal).
  39. witness_vernum_hex = '00'
  40. witness_vernum = int(witness_vernum_hex,16)
  41. bech32_hrp = 'bc'
  42. sign_mode = 'daemon'
  43. avg_bdi = int(9.7 * 60) # average block discovery interval (historical)
  44. halving_interval = 210000
  45. max_halvings = 64
  46. start_subsidy = 50
  47. ignore_daemon_version = False
  48. max_int = 0xffffffff
  49. def bytes2wif(self,privbytes,pubkey_type,compressed): # input is preprocessed hex
  50. assert len(privbytes) == self.privkey_len, f'{len(privbytes)} bytes: incorrect private key length!'
  51. assert pubkey_type in self.wif_ver_num, f'{pubkey_type!r}: invalid pubkey_type'
  52. return b58chk_encode(
  53. bytes.fromhex(self.wif_ver_num[pubkey_type])
  54. + privbytes
  55. + (b'',b'\x01')[bool(compressed)])
  56. def parse_wif(self,wif):
  57. key = b58chk_decode(wif)
  58. for k,v in self.wif_ver_num.items():
  59. v = bytes.fromhex(v)
  60. if key[:len(v)] == v:
  61. pubkey_type = k
  62. key = key[len(v):]
  63. break
  64. else:
  65. raise ValueError('Invalid WIF version number')
  66. if len(key) == self.privkey_len + 1:
  67. assert key[-1] == 0x01, f'{key[-1]!r}: invalid compressed key suffix byte'
  68. compressed = True
  69. elif len(key) == self.privkey_len:
  70. compressed = False
  71. else:
  72. raise ValueError(f'{len(key)}: invalid key length')
  73. return parsed_wif(
  74. sec = key[:self.privkey_len],
  75. pubkey_type = pubkey_type,
  76. compressed = compressed )
  77. def parse_addr(self,addr):
  78. if 'B' in self.mmtypes and addr[:len(self.bech32_hrp)] == self.bech32_hrp:
  79. import mmgen.bech32 as bech32
  80. ret = bech32.decode(self.bech32_hrp,addr)
  81. if ret[0] != self.witness_vernum:
  82. from ..util import msg
  83. msg(f'{ret[0]}: Invalid witness version number')
  84. return False
  85. return parsed_addr( bytes(ret[1]), 'bech32' ) if ret[1] else False
  86. return self.parse_addr_bytes(b58chk_decode(addr))
  87. def pubhash2addr(self,pubkey_hash,p2sh):
  88. assert len(pubkey_hash) == 20, f'{len(pubkey_hash)}: invalid length for pubkey hash'
  89. return b58chk_encode(
  90. self.addr_fmt_to_ver_bytes(('p2pkh','p2sh')[p2sh],return_hex=False) + pubkey_hash
  91. )
  92. # Segwit:
  93. def pubkey2redeem_script(self,pubkey):
  94. # https://bitcoincore.org/en/segwit_wallet_dev/
  95. # The P2SH redeemScript is always 22 bytes. It starts with a OP_0, followed
  96. # by a canonical push of the keyhash (i.e. 0x0014{20-byte keyhash})
  97. return bytes.fromhex(self.witness_vernum_hex + '14') + hash160(pubkey)
  98. def pubkey2segwitaddr(self,pubkey):
  99. return self.pubhash2addr(
  100. hash160( self.pubkey2redeem_script(pubkey)), p2sh=True )
  101. def pubhash2bech32addr(self,pubhash):
  102. d = list(pubhash)
  103. import mmgen.bech32 as bech32
  104. return bech32.bech32_encode(self.bech32_hrp,[self.witness_vernum]+bech32.convertbits(d,8,5))
  105. class testnet(mainnet):
  106. addr_ver_bytes = { '6f': 'p2pkh', 'c4': 'p2sh' }
  107. wif_ver_num = { 'std': 'ef' }
  108. bech32_hrp = 'tb'
  109. class regtest(testnet):
  110. bech32_hrp = 'bcrt'
  111. halving_interval = 150