From 2c253c0750576a5788c10dabe9972859fe5ed6a2 Mon Sep 17 00:00:00 2001 From: MMGen Date: Wed, 31 Oct 2018 13:28:54 +0000 Subject: [PATCH] py3port: use bytes literals where applicable ('foo' -> b'foo') --- mmgen/addr.py | 4 ++-- mmgen/protocol.py | 58 +++++++++++++++++++++++------------------------ mmgen/tool.py | 4 ++-- mmgen/tx.py | 9 ++++---- mmgen/util.py | 10 ++++---- test/objtest.py | 2 +- test/test.py | 6 ++--- 7 files changed, 47 insertions(+), 46 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index 2f25ff69..151ff131 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -244,9 +244,9 @@ class KeyGeneratorPython(KeyGenerator): pubkey = hexlify(pko.get_verifying_key().to_string()) if compressed: # discard Y coord, replace with appropriate version byte # even y: <0, odd y: >0 -- https://bitcointalk.org/index.php?topic=129652.0 - return ('03','02')[pubkey[-1] in '02468ace'] + pubkey[:64] + return (b'03',b'02')[pubkey[-1] in b'02468ace'] + pubkey[:64] else: - return '04' + pubkey + return b'04' + pubkey def to_pubhex(self,privhex): assert type(privhex) == PrivKey diff --git a/mmgen/protocol.py b/mmgen/protocol.py index 1bcd8ab7..dfc88da4 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -66,8 +66,8 @@ class BitcoinProtocol(MMGenObject): name = 'bitcoin' daemon_name = 'bitcoind' daemon_family = 'bitcoind' - addr_ver_num = { 'p2pkh': ('00','1'), 'p2sh': ('05','3') } - wif_ver_num = { 'std': '80' } + addr_ver_num = { 'p2pkh': (b'00','1'), 'p2sh': (b'05','3') } + wif_ver_num = { 'std': b'80' } mmtypes = ('L','C','S','B') dfl_mmtype = 'L' data_subdir = '' @@ -89,7 +89,7 @@ class BitcoinProtocol(MMGenObject): base_coin = 'BTC' # From BIP173: witness version 'n' is stored as 'OP_n'. OP_0 is encoded as 0x00, # but OP_1 through OP_16 are encoded as 0x51 though 0x60 (81 to 96 in decimal). - witness_vernum_hex = '00' + witness_vernum_hex = b'00' witness_vernum = int(witness_vernum_hex,16) bech32_hrp = 'bc' sign_mode = 'daemon' @@ -123,7 +123,7 @@ class BitcoinProtocol(MMGenObject): @classmethod def hex2wif(cls,hexpriv,pubkey_type,compressed): - return _b58chk_encode(cls.wif_ver_num[pubkey_type] + hexpriv + ('','01')[bool(compressed)]) + return _b58chk_encode(cls.wif_ver_num[pubkey_type] + hexpriv + (b'',b'01')[bool(compressed)]) @classmethod def wif2hex(cls,wif): @@ -135,7 +135,7 @@ class BitcoinProtocol(MMGenObject): key = key[len(v):] assert pubkey_type,'invalid WIF version number' if len(key) == 66: - assert key[-2:] == '01','invalid compressed key suffix' + assert key[-2:] == b'01','invalid compressed key suffix' compressed = True else: assert len(key) == 64,'invalid key length' @@ -186,7 +186,7 @@ class BitcoinProtocol(MMGenObject): def pubhash2addr(cls,pubkey_hash,p2sh): assert len(pubkey_hash) == 40,'{}: invalid length for pubkey hash'.format(len(pubkey_hash)) s = cls.addr_ver_num[('p2pkh','p2sh')[p2sh]][0] + pubkey_hash - lzeroes = (len(s) - len(s.lstrip('0'))) / 2 # non-zero only for ver num '00' (BTC p2pkh) + lzeroes = (len(s) - len(s.lstrip(b'0'))) // 2 # non-zero only for ver num '00' (BTC p2pkh) return ('1' * lzeroes) + _b58chk_encode(s) # Segwit: @@ -195,7 +195,7 @@ class BitcoinProtocol(MMGenObject): # https://bitcoincore.org/en/segwit_wallet_dev/ # The P2SH redeemScript is always 22 bytes. It starts with a OP_0, followed # by a canonical push of the keyhash (i.e. 0x0014{20-byte keyhash}) - return cls.witness_vernum_hex + '14' + hash160(pubhex) + return cls.witness_vernum_hex + b'14' + hash160(pubhex) @classmethod def pubhex2segwitaddr(cls,pubhex): @@ -207,8 +207,8 @@ class BitcoinProtocol(MMGenObject): return bech32.bech32_encode(cls.bech32_hrp,[cls.witness_vernum]+bech32.convertbits(d,8,5)) class BitcoinTestnetProtocol(BitcoinProtocol): - addr_ver_num = { 'p2pkh': ('6f',('m','n')), 'p2sh': ('c4','2') } - wif_ver_num = { 'std': 'ef' } + addr_ver_num = { 'p2pkh': (b'6f',('m','n')), 'p2sh': (b'c4','2') } + wif_ver_num = { 'std': b'ef' } data_subdir = 'testnet' daemon_data_subdir = 'testnet3' rpc_port = 18332 @@ -237,8 +237,8 @@ class BitcoinCashProtocol(BitcoinProtocol): class BitcoinCashTestnetProtocol(BitcoinCashProtocol): rpc_port = 18442 - addr_ver_num = { 'p2pkh': ('6f',('m','n')), 'p2sh': ('c4','2') } - wif_ver_num = { 'std': 'ef' } + addr_ver_num = { 'p2pkh': (b'6f',('m','n')), 'p2sh': (b'c4','2') } + wif_ver_num = { 'std': b'ef' } data_subdir = 'testnet' daemon_data_subdir = 'testnet3' @@ -254,8 +254,8 @@ class B2XProtocol(BitcoinProtocol): ] class B2XTestnetProtocol(B2XProtocol): - addr_ver_num = { 'p2pkh': ('6f',('m','n')), 'p2sh': ('c4','2') } - wif_ver_num = { 'std': 'ef' } + addr_ver_num = { 'p2pkh': (b'6f',('m','n')), 'p2sh': (b'c4','2') } + wif_ver_num = { 'std': b'ef' } data_subdir = 'testnet' daemon_data_subdir = 'testnet5' rpc_port = 18338 @@ -266,8 +266,8 @@ class LitecoinProtocol(BitcoinProtocol): daemon_name = 'litecoind' daemon_data_dir = os.path.join(os.getenv('APPDATA'),'Litecoin') if g.platform == 'win' \ else os.path.join(g.home_dir,'.litecoin') - addr_ver_num = { 'p2pkh': ('30','L'), 'p2sh': ('32','M'), 'p2sh2': ('05','3') } # 'p2sh' is new fmt - wif_ver_num = { 'std': 'b0' } + addr_ver_num = { 'p2pkh': (b'30','L'), 'p2sh': (b'32','M'), 'p2sh2': (b'05','3') } # 'p2sh' is new fmt + wif_ver_num = { 'std': b'b0' } mmtypes = ('L','C','S','B') secs_per_block = 150 rpc_port = 9332 @@ -279,8 +279,8 @@ class LitecoinProtocol(BitcoinProtocol): class LitecoinTestnetProtocol(LitecoinProtocol): # addr ver nums same as Bitcoin testnet, except for 'p2sh' - addr_ver_num = { 'p2pkh': ('6f',('m','n')), 'p2sh': ('3a','Q'), 'p2sh2': ('c4','2') } - wif_ver_num = { 'std': 'ef' } # same as Bitcoin testnet + addr_ver_num = { 'p2pkh': (b'6f',('m','n')), 'p2sh': (b'3a','Q'), 'p2sh2': (b'c4','2') } + wif_ver_num = { 'std': b'ef' } # same as Bitcoin testnet data_subdir = 'testnet' daemon_data_subdir = 'testnet4' rpc_port = 19332 @@ -356,11 +356,11 @@ class ZcashProtocol(BitcoinProtocolAddrgen): name = 'zcash' base_coin = 'ZEC' addr_ver_num = { - 'p2pkh': ('1cb8','t1'), - 'p2sh': ('1cbd','t3'), - 'zcash_z': ('169a','zc'), - 'viewkey': ('a8abd3','ZiVK') } - wif_ver_num = { 'std': '80', 'zcash_z': 'ab36' } + 'p2pkh': (b'1cb8','t1'), + 'p2sh': (b'1cbd','t3'), + 'zcash_z': (b'169a','zc'), + 'viewkey': (b'a8abd3','ZiVK') } + wif_ver_num = { 'std': b'80', 'zcash_z': b'ab36' } mmtypes = ('L','C','Z') dfl_mmtype = 'L' @@ -382,18 +382,18 @@ class ZcashProtocol(BitcoinProtocolAddrgen): raise ValueError('{}: incorrect pubkey_hash length'.format(hl)) class ZcashTestnetProtocol(ZcashProtocol): - wif_ver_num = { 'std': 'ef', 'zcash_z': 'ac08' } + wif_ver_num = { 'std': b'ef', 'zcash_z': b'ac08' } addr_ver_num = { - 'p2pkh': ('1d25','tm'), - 'p2sh': ('1cba','t2'), - 'zcash_z': ('16b6','zt'), - 'viewkey': ('a8ac0c','ZiVt') } + 'p2pkh': (b'1d25','tm'), + 'p2sh': (b'1cba','t2'), + 'zcash_z': (b'16b6','zt'), + 'viewkey': (b'a8ac0c','ZiVt') } # https://github.com/monero-project/monero/blob/master/src/cryptonote_config.h class MoneroProtocol(DummyWIF,BitcoinProtocolAddrgen): name = 'monero' base_coin = 'XMR' - addr_ver_num = { 'monero': ('12','4'), 'monero_sub': ('2a','8') } # 18,42 + addr_ver_num = { 'monero': (b'12','4'), 'monero_sub': (b'2a','8') } # 18,42 wif_ver_num = {} mmtypes = ('M',) dfl_mmtype = 'M' @@ -431,7 +431,7 @@ class MoneroProtocol(DummyWIF,BitcoinProtocolAddrgen): return { 'hex': ret, 'format': 'monero' } if return_dict else True class MoneroTestnetProtocol(MoneroProtocol): - addr_ver_num = { 'monero': ('35','4'), 'monero_sub': ('3f','8') } # 53,63 + addr_ver_num = { 'monero': (b'35','4'), 'monero_sub': (b'3f','8') } # 53,63 class CoinProtocol(MMGenObject): coins = { diff --git a/mmgen/tool.py b/mmgen/tool.py index 6d50080e..331e8fb9 100755 --- a/mmgen/tool.py +++ b/mmgen/tool.py @@ -411,7 +411,7 @@ def Decrypt(infile,outfile='',hash_preset=''): def Find_incog_data(filename,iv_id,keep_searching=False): ivsize,bsize,mod = g.aesctr_iv_len,4096,4096*8 - n,carry = 0,' '*ivsize + n,carry = 0,b' '*ivsize flgs = os.O_RDONLY|os.O_BINARY if g.platform == 'win' else os.O_RDONLY f = os.open(filename,flgs) for ch in iv_id: @@ -508,7 +508,7 @@ def monero_wallet_ops(infile,op,blockheight=None,addrs=None): def test_rpc(): p = run_cmd(['monero-wallet-cli','--version']) - if not 'Monero' in p.stdout.read(): + if not b'Monero' in p.stdout.read(): die(1,"Unable to run 'monero-wallet-cli'!") p = run_cmd(['monerod','status']) import re diff --git a/mmgen/tx.py b/mmgen/tx.py index 66953dfb..c918d35b 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -277,7 +277,7 @@ Selected non-{pnm} inputs: {{}}""".strip().format(pnm=g.proj_name,pnl=g.proj_nam self.outputs = self.MMGenTxOutputList() self.send_amt = g.proto.coin_amt('0') # total amt minus change self.fee = g.proto.coin_amt('0') - self.hex = '' # raw serialized hex transaction + self.hex = b'' # raw serialized hex transaction self.label = MMGenTXLabel('') self.txid = '' self.coin_txid = '' @@ -812,13 +812,14 @@ Selected non-{pnm} inputs: {{}}""".strip().format(pnm=g.proj_name,pnl=g.proj_nam fs = "Hex TX has {} scriptSig but input is of type '{}'!" for n in range(len(txins)): ti,mmti = txins[n],self.inputs[n] - if ti['scriptSig'] == '' or ( len(ti['scriptSig']) == 46 and # native P2WPKH or P2SH-P2WPKH - ti['scriptSig'][:6] == '16' + g.proto.witness_vernum_hex + '14' ): + if ti['scriptSig'] == b'' or ( len(ti['scriptSig']) == 46 and # native P2WPKH or P2SH-P2WPKH + ti['scriptSig'][:6] == b'16' + g.proto.witness_vernum_hex + b'14' ): assert 'witness' in ti, 'missing witness' assert type(ti['witness']) == list and len(ti['witness']) == 2, 'malformed witness' assert len(ti['witness'][1]) == 66, 'incorrect witness pubkey length' assert mmti.mmid, fs.format('witness-type','non-MMGen') - assert mmti.mmid.mmtype == ('S','B')[ti['scriptSig']==''],fs.format('witness-type',mmti.mmid.mmtype) + assert mmti.mmid.mmtype == ('S','B')[ti['scriptSig']==b''],( + fs.format('witness-type',mmti.mmid.mmtype)) else: # non-witness if mmti.mmid: assert mmti.mmid.mmtype not in ('S','B'), fs.format('signature in',mmti.mmid.mmtype) diff --git a/mmgen/util.py b/mmgen/util.py index 6bfd4982..d46d9832 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -743,8 +743,8 @@ def keypress_confirm(prompt,default_yes=False,verbose=False,no_nl=False): if not reply: if default_yes: msg_r(nl); return True else: msg_r(nl); return False - elif reply in 'yY': msg_r(nl); return True - elif reply in 'nN': msg_r(nl); return False + elif r in b'yY': msg_r(nl); return True + elif r in b'nN': msg_r(nl); return False else: if verbose: msg('\nInvalid reply') else: msg_r('\r') @@ -754,7 +754,7 @@ def prompt_and_get_char(prompt,chars,enter_ok=False,verbose=False): from mmgen.term import get_char while True: - reply = get_char('{}: '.format(prompt)).strip('\n\r') + reply = get_char('{}: '.format(prompt)).strip(b'\n\r') if reply in chars or (enter_ok and not reply): msg('') @@ -799,9 +799,9 @@ def do_license_msg(immed=False): while True: reply = get_char(prompt, immed_chars=('','wc')[bool(immed)]) - if reply == 'w': + if reply == b'w': do_pager(gpl.conditions) - elif reply == 'c': + elif reply == b'c': msg(''); break else: msg_r('\r') diff --git a/test/objtest.py b/test/objtest.py index eb19e5f0..95e02764 100755 --- a/test/objtest.py +++ b/test/objtest.py @@ -168,7 +168,7 @@ tests = OrderedDict([ 'good': ('DEADBE','F00BAA') }), ('CoinTxID',{ - 'bad': (1,[],'\0','\1','я','g','gg','FF','f00','F00F0012',hexlify(r16),hexlify(r32)+'ee'), + 'bad': (1,[],'\0','\1','я','g','gg','FF','f00','F00F0012',hexlify(r16),hexlify(r32)+b'ee'), 'good': (hexlify(r32),) }), ('WifKey', { diff --git a/test/test.py b/test/test.py index 11017eeb..1ee2db48 100755 --- a/test/test.py +++ b/test/test.py @@ -203,7 +203,7 @@ if opt.bech32 and 'B' not in g.proto.mmtypes: die(1,'--bech32 option incompatible with {}'.format(g.proto.__name__)) def randbool(): - return hexlify(os.urandom(1))[1] in '12345678' + return hexlify(os.urandom(1))[1] in b'12345678' def get_segwit_bool(): return randbool() if opt.segwit_random else True if opt.segwit or opt.bech32 else False @@ -3144,7 +3144,7 @@ class MMGenTestSuite(object): return self.regtest_alice_add_label_badaddr(name,rt_pw,'Invalid coin address for this chain: '+rt_pw) def regtest_alice_add_label_badaddr2(self,name): - addr = g.proto.pubhash2addr('00'*20,False) # mainnet zero address + addr = g.proto.pubhash2addr(b'00'*20,False) # mainnet zero address return self.regtest_alice_add_label_badaddr(name,addr,'Invalid coin address for this chain: '+addr) def regtest_alice_add_label_badaddr3(self,name): @@ -3153,7 +3153,7 @@ class MMGenTestSuite(object): "MMGen address '{}' not found in tracking wallet".format(addr)) def regtest_alice_add_label_badaddr4(self,name): - addr = CoinProtocol(g.coin,True).pubhash2addr('00'*20,False) # testnet zero address + addr = CoinProtocol(g.coin,True).pubhash2addr(b'00'*20,False) # testnet zero address return self.regtest_alice_add_label_badaddr(name,addr, "Address '{}' not found in tracking wallet".format(addr))