protocol.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #!/usr/bin/env python
  2. #
  3. # MMGen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2018 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. protocol.py: Coin protocol functions, classes and methods
  20. """
  21. import sys,os,hashlib
  22. from binascii import unhexlify
  23. from mmgen.util import msg,pmsg,Msg,pdie
  24. from mmgen.obj import MMGenObject,BTCAmt,LTCAmt,BCHAmt,B2XAmt
  25. from mmgen.globalvars import g
  26. import mmgen.bech32 as bech32
  27. def hash160(hexnum): # take hex, return hex - OP_HASH160
  28. return hashlib.new('ripemd160',hashlib.sha256(unhexlify(hexnum)).digest()).hexdigest()
  29. def hash256(hexnum): # take hex, return hex - OP_HASH256
  30. return hashlib.sha256(hashlib.sha256(unhexlify(hexnum)).digest()).hexdigest()
  31. # From en.bitcoin.it:
  32. # The Base58 encoding used is home made, and has some differences.
  33. # Especially, leading zeroes are kept as single zeroes when conversion happens.
  34. # Test: 5JbQQTs3cnoYN9vDYaGY6nhQ1DggVsY4FJNBUfEfpSQqrEp3srk
  35. # The 'zero address':
  36. # 1111111111111111111114oLvT2 (pubkeyhash = '\0'*20)
  37. _b58a='123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  38. def _numtob58(num):
  39. def b58enc(n):
  40. while n:
  41. yield _b58a[n % 58]
  42. n /= 58
  43. return ''.join(b58enc(num))[::-1]
  44. def _b58tonum(b58str):
  45. return sum(_b58a.index(ch) * 58**n for n,ch in enumerate(b58str[::-1]))
  46. def _b58chk_encode(hexstr):
  47. return _numtob58(int(hexstr+hash256(hexstr)[:8],16))
  48. def _b58chk_decode(s):
  49. hexstr = '{:x}'.format(_b58tonum(s))
  50. if hexstr[-8:] == hash256(hexstr[:-8])[:8]:
  51. return hexstr[:-8]
  52. raise ValueError,'_b58chk_decode(): checksum incorrect'
  53. # chainparams.cpp
  54. class BitcoinProtocol(MMGenObject):
  55. name = 'bitcoin'
  56. daemon_name = 'bitcoind'
  57. addr_ver_num = { 'p2pkh': ('00','1'), 'p2sh': ('05','3') }
  58. wif_ver_num = { 'std': '80' }
  59. mmtypes = ('L','C','S','B')
  60. dfl_mmtype = 'L'
  61. data_subdir = ''
  62. rpc_port = 8332
  63. secs_per_block = 600
  64. coin_amt = BTCAmt
  65. max_tx_fee = BTCAmt('0.003')
  66. daemon_data_dir = os.path.join(os.getenv('APPDATA'),'Bitcoin') if g.platform == 'win' \
  67. else os.path.join(g.home_dir,'.bitcoin')
  68. daemon_data_subdir = ''
  69. sighash_type = 'ALL'
  70. block0 = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
  71. forks = [ # height, hash, name, replayable
  72. (478559,'00000000000000000019f112ec0a9982926f1258cdcc558dd7c3b7e5dc7fa148','bch',False),
  73. (None,'','b2x',True)
  74. ]
  75. caps = ('rbf','segwit')
  76. mmcaps = ('key','addr','rpc','tx')
  77. base_coin = 'BTC'
  78. # From BIP173: witness version 'n' is stored as 'OP_n'. OP_0 is encoded as 0x00,
  79. # but OP_1 through OP_16 are encoded as 0x51 though 0x60 (81 to 96 in decimal).
  80. witness_vernum_hex = '00'
  81. witness_vernum = int(witness_vernum_hex,16)
  82. bech32_hrp = 'bc'
  83. @staticmethod
  84. def get_protocol_by_chain(chain):
  85. return CoinProtocol(g.coin,{'mainnet':False,'testnet':True,'regtest':True}[chain])
  86. @staticmethod
  87. def get_rpc_coin_amt_type():
  88. return (float,str)[g.daemon_version>=120000]
  89. @classmethod
  90. def cap(cls,s): return s in cls.caps
  91. @classmethod
  92. def preprocess_key(cls,hexpriv,pubkey_type): return hexpriv
  93. @classmethod
  94. def hex2wif(cls,hexpriv,pubkey_type,compressed):
  95. return _b58chk_encode(cls.wif_ver_num[pubkey_type] + hexpriv + ('','01')[bool(compressed)])
  96. @classmethod
  97. def wif2hex(cls,wif):
  98. key = _b58chk_decode(wif)
  99. pubkey_type = None
  100. for k,v in cls.wif_ver_num.items():
  101. if key[:len(v)] == v:
  102. pubkey_type = k
  103. key = key[len(v):]
  104. assert pubkey_type,'invalid WIF version number'
  105. if len(key) == 66:
  106. assert key[-2:] == '01','invalid compressed key suffix'
  107. compressed = True
  108. else:
  109. assert len(key) == 64,'invalid key length'
  110. compressed = False
  111. return { 'hex':key[:64], 'pubkey_type':pubkey_type, 'compressed':compressed }
  112. @classmethod
  113. def verify_addr(cls,addr,hex_width,return_dict=False):
  114. if 'B' in cls.mmtypes and addr[:len(cls.bech32_hrp)] == cls.bech32_hrp:
  115. ret = bech32.decode(cls.bech32_hrp,addr)
  116. if ret[0] != cls.witness_vernum:
  117. msg('{}: Invalid witness version number'.format(ret[0]))
  118. elif ret[1]:
  119. return {
  120. 'hex': ''.join(map(chr,ret[1])).encode('hex'),
  121. 'format': 'bech32'
  122. } if return_dict else True
  123. return False
  124. for addr_fmt in cls.addr_ver_num:
  125. ver_num,pfx = cls.addr_ver_num[addr_fmt]
  126. if type(pfx) == tuple:
  127. if addr[0] not in pfx: continue
  128. elif addr[:len(pfx)] != pfx: continue
  129. num = _b58tonum(addr)
  130. if num == False:
  131. if g.debug: Msg('Address cannot be converted to base 58')
  132. break
  133. addr_hex = '{:0{}x}'.format(num,len(ver_num)+hex_width+8)
  134. # pmsg(hex_width,len(addr_hex),addr_hex[:len(ver_num)],ver_num)
  135. if addr_hex[:len(ver_num)] != ver_num: continue
  136. if hash256(addr_hex[:-8])[:8] == addr_hex[-8:]:
  137. return {
  138. 'hex': addr_hex[len(ver_num):-8],
  139. 'format': {'p2pkh':'p2pkh','p2sh':'p2sh','p2sh2':'p2sh',
  140. 'zcash_z':'zcash_z','viewkey':'viewkey'}[addr_fmt]
  141. } if return_dict else True
  142. else:
  143. if g.debug: Msg('Invalid checksum in address')
  144. break
  145. return False
  146. @classmethod
  147. def pubhash2addr(cls,pubkey_hash,p2sh):
  148. assert len(pubkey_hash) == 40,'{}: invalid length for pubkey hash'.format(len(pubkey_hash))
  149. s = cls.addr_ver_num[('p2pkh','p2sh')[p2sh]][0] + pubkey_hash
  150. lzeroes = (len(s) - len(s.lstrip('0'))) / 2 # non-zero only for ver num '00' (BTC p2pkh)
  151. return ('1' * lzeroes) + _b58chk_encode(s)
  152. # Segwit:
  153. @classmethod
  154. def pubhex2redeem_script(cls,pubhex):
  155. # https://bitcoincore.org/en/segwit_wallet_dev/
  156. # The P2SH redeemScript is always 22 bytes. It starts with a OP_0, followed
  157. # by a canonical push of the keyhash (i.e. 0x0014{20-byte keyhash})
  158. return cls.witness_vernum_hex + '14' + hash160(pubhex)
  159. @classmethod
  160. def pubhex2segwitaddr(cls,pubhex):
  161. return cls.pubhash2addr(hash160(cls.pubhex2redeem_script(pubhex)),p2sh=True)
  162. @classmethod
  163. def pubhash2bech32addr(cls,pubhash):
  164. d = map(ord,pubhash.decode('hex'))
  165. return bech32.bech32_encode(cls.bech32_hrp,[cls.witness_vernum]+bech32.convertbits(d,8,5))
  166. class BitcoinTestnetProtocol(BitcoinProtocol):
  167. addr_ver_num = { 'p2pkh': ('6f',('m','n')), 'p2sh': ('c4','2') }
  168. wif_ver_num = { 'std': 'ef' }
  169. data_subdir = 'testnet'
  170. daemon_data_subdir = 'testnet3'
  171. rpc_port = 18332
  172. bech32_hrp = 'tb'
  173. bech32_hrp_rt = 'bcrt'
  174. class BitcoinCashProtocol(BitcoinProtocol):
  175. # TODO: assumes MSWin user installs in custom dir 'Bitcoin_ABC'
  176. daemon_name = 'bitcoind-abc'
  177. daemon_data_dir = os.path.join(os.getenv('APPDATA'),'Bitcoin_ABC') if g.platform == 'win' \
  178. else os.path.join(g.home_dir,'.bitcoin-abc')
  179. rpc_port = 8442
  180. mmtypes = ('L','C')
  181. sighash_type = 'ALL|FORKID'
  182. forks = [
  183. (478559,'000000000000000000651ef99cb9fcbe0dadde1d424bd9f15ff20136191a5eec','btc',False)
  184. ]
  185. caps = ()
  186. coin_amt = BCHAmt
  187. max_tx_fee = BCHAmt('0.1')
  188. @classmethod
  189. def pubhex2redeem_script(cls,pubhex): raise NotImplementedError
  190. @classmethod
  191. def pubhex2segwitaddr(cls,pubhex): raise NotImplementedError
  192. class BitcoinCashTestnetProtocol(BitcoinCashProtocol):
  193. rpc_port = 18442
  194. addr_ver_num = { 'p2pkh': ('6f',('m','n')), 'p2sh': ('c4','2') }
  195. wif_ver_num = { 'std': 'ef' }
  196. data_subdir = 'testnet'
  197. daemon_data_subdir = 'testnet3'
  198. class B2XProtocol(BitcoinProtocol):
  199. daemon_name = 'bitcoind-2x'
  200. daemon_data_dir = os.path.join(os.getenv('APPDATA'),'Bitcoin_2X') if g.platform == 'win' \
  201. else os.path.join(g.home_dir,'.bitcoin-2x')
  202. rpc_port = 8338
  203. coin_amt = B2XAmt
  204. max_tx_fee = B2XAmt('0.1')
  205. forks = [
  206. (None,'','btc',True) # activation: 494784
  207. ]
  208. class B2XTestnetProtocol(B2XProtocol):
  209. addr_ver_num = { 'p2pkh': ('6f',('m','n')), 'p2sh': ('c4','2') }
  210. wif_ver_num = { 'std': 'ef' }
  211. data_subdir = 'testnet'
  212. daemon_data_subdir = 'testnet5'
  213. rpc_port = 18338
  214. class LitecoinProtocol(BitcoinProtocol):
  215. block0 = '12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2'
  216. name = 'litecoin'
  217. daemon_name = 'litecoind'
  218. daemon_data_dir = os.path.join(os.getenv('APPDATA'),'Litecoin') if g.platform == 'win' \
  219. else os.path.join(g.home_dir,'.litecoin')
  220. addr_ver_num = { 'p2pkh': ('30','L'), 'p2sh': ('32','M'), 'p2sh2': ('05','3') } # 'p2sh' is new fmt
  221. wif_ver_num = { 'std': 'b0' }
  222. mmtypes = ('L','C','S','B')
  223. secs_per_block = 150
  224. rpc_port = 9332
  225. coin_amt = LTCAmt
  226. max_tx_fee = LTCAmt('0.3')
  227. base_coin = 'LTC'
  228. forks = []
  229. bech32_hrp = 'ltc'
  230. class LitecoinTestnetProtocol(LitecoinProtocol):
  231. # addr ver nums same as Bitcoin testnet, except for 'p2sh'
  232. addr_ver_num = { 'p2pkh': ('6f',('m','n')), 'p2sh': ('3a','Q'), 'p2sh2': ('c4','2') }
  233. wif_ver_num = { 'std': 'ef' } # same as Bitcoin testnet
  234. data_subdir = 'testnet'
  235. daemon_data_subdir = 'testnet4'
  236. rpc_port = 19332
  237. bech32_hrp = 'tltc'
  238. bech32_hrp_rt = 'rltc'
  239. class BitcoinProtocolAddrgen(BitcoinProtocol): mmcaps = ('key','addr')
  240. class BitcoinTestnetProtocolAddrgen(BitcoinTestnetProtocol): mmcaps = ('key','addr')
  241. class DummyWIF(object):
  242. @classmethod
  243. def hex2wif(cls,hexpriv,pubkey_type,compressed):
  244. n = cls.name.capitalize()
  245. assert pubkey_type == cls.pubkey_type,'{}: invalid pubkey_type for {}!'.format(pubkey_type,n)
  246. assert compressed == False,'{} does not support compressed pubkeys!'.format(n)
  247. return str(hexpriv)
  248. @classmethod
  249. def wif2hex(cls,wif):
  250. return { 'hex':str(wif), 'pubkey_type':cls.pubkey_type, 'compressed':False }
  251. class EthereumProtocol(DummyWIF,BitcoinProtocolAddrgen):
  252. addr_width = 40
  253. mmtypes = ('E',)
  254. dfl_mmtype = 'E'
  255. name = 'ethereum'
  256. base_coin = 'ETH'
  257. pubkey_type = 'std' # required by DummyWIF
  258. @classmethod
  259. def verify_addr(cls,addr,hex_width,return_dict=False):
  260. from mmgen.util import is_hex_str_lc
  261. if is_hex_str_lc(addr) and len(addr) == cls.addr_width:
  262. return { 'hex': addr, 'format': 'ethereum' } if return_dict else True
  263. if g.debug: Msg("Invalid address '{}'".format(addr))
  264. return False
  265. @classmethod
  266. def pubhash2addr(cls,pubkey_hash,p2sh):
  267. assert len(pubkey_hash) == 40,'{}: invalid length for pubkey hash'.format(len(pubkey_hash))
  268. assert not p2sh,'Ethereum has no P2SH address format'
  269. return pubkey_hash
  270. class EthereumTestnetProtocol(EthereumProtocol): pass
  271. class EthereumClassicProtocol(EthereumProtocol):
  272. name = 'ethereum_classic'
  273. class EthereumClassicTestnetProtocol(EthereumClassicProtocol): pass
  274. class ZcashProtocol(BitcoinProtocolAddrgen):
  275. name = 'zcash'
  276. base_coin = 'ZEC'
  277. addr_ver_num = {
  278. 'p2pkh': ('1cb8','t1'),
  279. 'p2sh': ('1cbd','t3'),
  280. 'zcash_z': ('169a','zc'),
  281. 'viewkey': ('a8abd3','ZiVK') }
  282. wif_ver_num = { 'std': '80', 'zcash_z': 'ab36' }
  283. mmtypes = ('L','C','Z')
  284. dfl_mmtype = 'L'
  285. @classmethod
  286. def preprocess_key(cls,hexpriv,pubkey_type): # zero the first four bits
  287. if pubkey_type == 'zcash_z':
  288. return '{:02x}'.format(int(hexpriv[:2],16) & 0x0f) + hexpriv[2:]
  289. else:
  290. return hexpriv
  291. @classmethod
  292. def pubhash2addr(cls,pubkey_hash,p2sh):
  293. hl = len(pubkey_hash)
  294. if hl == 40:
  295. return super(cls,cls).pubhash2addr(pubkey_hash,p2sh)
  296. elif hl == 128:
  297. raise NotImplementedError,'Zcash z-addresses have no pubkey hash'
  298. else:
  299. raise ValueError,'{}: incorrect pubkey_hash length'.format(hl)
  300. class ZcashTestnetProtocol(ZcashProtocol):
  301. wif_ver_num = { 'std': 'ef', 'zcash_z': 'ac08' }
  302. addr_ver_num = {
  303. 'p2pkh': ('1d25','tm'),
  304. 'p2sh': ('1cba','t2'),
  305. 'zcash_z': ('16b6','zt'),
  306. 'viewkey': ('a8ac0c','ZiVt') }
  307. # https://github.com/monero-project/monero/blob/master/src/cryptonote_config.h
  308. class MoneroProtocol(DummyWIF,BitcoinProtocolAddrgen):
  309. name = 'monero'
  310. base_coin = 'XMR'
  311. addr_ver_num = { 'monero': ('12','4'), 'monero_sub': ('2a','8') } # 18,42
  312. wif_ver_num = {}
  313. mmtypes = ('M',)
  314. dfl_mmtype = 'M'
  315. addr_width = 95
  316. pubkey_type = 'monero' # required by DummyWIF
  317. @classmethod
  318. def preprocess_key(cls,hexpriv,pubkey_type): # reduce key
  319. try:
  320. from ed25519ll.djbec import l
  321. except:
  322. from mmgen.ed25519 import l
  323. n = int(hexpriv.decode('hex')[::-1].encode('hex'),16) % l
  324. return '{:064x}'.format(n).decode('hex')[::-1].encode('hex')
  325. @classmethod
  326. def verify_addr(cls,addr,hex_width,return_dict=False):
  327. def b58dec(addr_str):
  328. from mmgen.util import baseconv
  329. dec,l = baseconv.tohex,len(addr_str)
  330. a = ''.join([dec(addr_str[i*11:i*11+11],'b58',pad=16) for i in range(l/11)])
  331. b = dec(addr_str[-(l%11):],'b58',pad=10)
  332. return a + b
  333. from mmgen.util import is_b58_str
  334. assert is_b58_str(addr),'Not valid base-58 string'
  335. assert len(addr) == cls.addr_width,'Incorrect width'
  336. ret = b58dec(addr)
  337. import sha3
  338. chk = sha3.keccak_256(ret.decode('hex')[:-4]).hexdigest()[:8]
  339. assert chk == ret[-8:],'Incorrect checksum'
  340. return { 'hex': ret, 'format': 'monero' } if return_dict else True
  341. class MoneroTestnetProtocol(MoneroProtocol):
  342. addr_ver_num = { 'monero': ('35','4'), 'monero_sub': ('3f','8') } # 53,63
  343. class CoinProtocol(MMGenObject):
  344. coins = {
  345. # mainnet testnet trustlevel (None == skip)
  346. 'btc': (BitcoinProtocol,BitcoinTestnetProtocol,None),
  347. 'bch': (BitcoinCashProtocol,BitcoinCashTestnetProtocol,None),
  348. 'ltc': (LitecoinProtocol,LitecoinTestnetProtocol,None),
  349. 'eth': (EthereumProtocol,EthereumTestnetProtocol,2),
  350. 'etc': (EthereumClassicProtocol,EthereumClassicTestnetProtocol,2),
  351. 'zec': (ZcashProtocol,ZcashTestnetProtocol,2),
  352. 'xmr': (MoneroProtocol,MoneroTestnetProtocol,None)
  353. }
  354. def __new__(cls,coin,testnet):
  355. coin = coin.lower()
  356. assert type(testnet) == bool
  357. m = "'{}': not a valid coin. Valid choices are {}"
  358. assert coin in cls.coins,m.format(coin,','.join(cls.get_valid_coins()))
  359. return cls.coins[coin][testnet]
  360. @classmethod
  361. def get_valid_coins(cls,upcase=False):
  362. from mmgen.altcoin import CoinInfo as ci
  363. ret = sorted(set(
  364. [e[1] for e in ci.coin_constants['mainnet'] if e[6] != -1]
  365. + cls.coins.keys()))
  366. return [getattr(e,('lower','upper')[upcase])() for e in ret]
  367. @classmethod
  368. def get_base_coin_from_name(cls,name):
  369. for proto,foo in cls.coins.values():
  370. if name == proto.__name__[:-8].lower():
  371. return proto.base_coin
  372. return False
  373. def init_genonly_altcoins(usr_coin,trust_level=None):
  374. from mmgen.altcoin import CoinInfo as ci
  375. if trust_level is None:
  376. if not usr_coin: return None # BTC
  377. if usr_coin.lower() in CoinProtocol.coins:
  378. return CoinProtocol.coins[usr_coin.lower()][2]
  379. usr_coin = usr_coin.upper()
  380. mn_coins = [e[1] for e in ci.coin_constants['mainnet'] if e[6] != -1]
  381. if usr_coin not in mn_coins: return None
  382. trust_level = ci.coin_constants['mainnet'][mn_coins.index(usr_coin)][6]
  383. data = {}
  384. for k in ('mainnet','testnet'):
  385. data[k] = [e for e in ci.coin_constants[k] if e[6] >= trust_level]
  386. exec(make_init_genonly_altcoins_str(data))
  387. return trust_level
  388. def make_init_genonly_altcoins_str(data):
  389. def make_proto(e,testnet=False):
  390. tn_str = 'Testnet' if testnet else ''
  391. proto,coin = '{}{}Protocol'.format(e[0],tn_str),e[1]
  392. if proto[0] in '0123456789': proto = 'X_'+proto
  393. if proto in globals(): return ''
  394. if coin.lower() in CoinProtocol.coins: return ''
  395. def num2hexstr(n):
  396. return '{:0{}x}'.format(n,2 if n < 256 else 4)
  397. o = ['class {}(Bitcoin{}ProtocolAddrgen):'.format(proto,tn_str)]
  398. o += ["base_coin = '{}'".format(coin)]
  399. o += ["name = '{}'".format(e[0].lower())]
  400. o += ["nameCaps = '{}'".format(e[0])]
  401. a = "addr_ver_num = {{ 'p2pkh': ({!r},{!r})".format(num2hexstr(e[3][0]),e[3][1])
  402. b = ", 'p2sh': ({!r},{!r})".format(num2hexstr(e[4][0]),e[4][1]) if e[4] else ''
  403. o += [a+b+' }']
  404. o += ["wif_ver_num = {{ 'std': {!r} }}".format(num2hexstr(e[2]))]
  405. o += ["mmtypes = ('L','C'{})".format(",'S'" if e[5] else '')]
  406. o += ["dfl_mmtype = '{}'".format('L')]
  407. return '\n\t'.join(o) + '\n'
  408. out = ''
  409. for e in data['mainnet']:
  410. out += make_proto(e)
  411. for e in data['testnet']:
  412. out += make_proto(e,testnet=True)
  413. tn_coins = [e[1] for e in data['testnet']]
  414. fs = "CoinProtocol.coins['{}'] = ({}Protocol,{})\n"
  415. for e in data['mainnet']:
  416. proto,coin = e[0],e[1]
  417. if proto[0] in '0123456789': proto = 'X_'+proto
  418. if proto+'Protocol' in globals(): continue
  419. if coin.lower() in CoinProtocol.coins: continue
  420. out += fs.format(coin.lower(),proto,('None',proto+'TestnetProtocol')[coin in tn_coins])
  421. # print out
  422. return out
  423. def init_coin(coin):
  424. coin = coin.upper()
  425. g.coin = coin
  426. g.proto = CoinProtocol(coin,g.testnet)