tbase_encode/base_decode: change to saner API - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 8cf3587aeb0b1fb767aee84792e15f75f90e8f23 DIR parent 01f94fcf5817be9ff9182bfcda957a8e0e31bf8e HTML Author: SomberNight <somber.night@protonmail.com> Date: Sun, 8 Dec 2019 06:19:51 +0100 base_encode/base_decode: change to saner API Diffstat: M electrum/bitcoin.py | 8 ++++---- M electrum/tests/test_bitcoin.py | 8 ++++---- M electrum/transaction.py | 2 +- M electrum/util.py | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) --- DIR diff --git a/electrum/bitcoin.py b/electrum/bitcoin.py t@@ -328,7 +328,7 @@ def hash160_to_b58_address(h160: bytes, addrtype: int) -> str: def b58_address_to_hash160(addr: str) -> Tuple[int, bytes]: addr = to_bytes(addr, 'ascii') - _bytes = base_decode(addr, 25, base=58) + _bytes = base_decode(addr, base=58, length=25) return _bytes[0], _bytes[1:21] t@@ -446,7 +446,7 @@ __b43chars = b'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$*+-./:' assert len(__b43chars) == 43 -def base_encode(v: bytes, base: int) -> str: +def base_encode(v: bytes, *, base: int) -> str: """ encode v, which is a string of bytes, to base58.""" assert_bytes(v) if base not in (58, 43): t@@ -479,7 +479,7 @@ def base_encode(v: bytes, base: int) -> str: return result.decode('ascii') -def base_decode(v: Union[bytes, str], length: Optional[int], base: int) -> Optional[bytes]: +def base_decode(v: Union[bytes, str], *, base: int, length: int = None) -> Optional[bytes]: """ decode v into a string of len bytes.""" # assert_bytes(v) v = to_bytes(v, 'ascii') t@@ -526,7 +526,7 @@ def EncodeBase58Check(vchIn: bytes) -> str: def DecodeBase58Check(psz: Union[bytes, str]) -> bytes: - vchRet = base_decode(psz, None, base=58) + vchRet = base_decode(psz, base=58) payload = vchRet[0:-4] csum_found = vchRet[-4:] csum_calculated = sha256d(payload)[0:4] DIR diff --git a/electrum/tests/test_bitcoin.py b/electrum/tests/test_bitcoin.py t@@ -764,20 +764,20 @@ class TestBaseEncode(ElectrumTestCase): def test_base43(self): tx_hex = "020000000001021cd0e96f9ca202e017ca3465e3c13373c0df3a4cdd91c1fd02ea42a1a65d2a410000000000fdffffff757da7cf8322e5063785e2d8ada74702d2648fa2add2d533ba83c52eb110df690200000000fdffffff02d07e010000000000160014b544c86eaf95e3bb3b6d2cabb12ab40fc59cad9ca086010000000000232102ce0d066fbfcf150a5a1bbc4f312cd2eb080e8d8a47e5f2ce1a63b23215e54fb5ac02483045022100a9856bf10a950810abceeabc9a86e6ba533e130686e3d7863971b9377e7c658a0220288a69ef2b958a7c2ecfa376841d4a13817ed24fa9a0e0a6b9cb48e6439794c701210324e291735f83ff8de47301b12034950b80fa4724926a34d67e413d8ff8817c53024830450221008f885978f7af746679200ed55fe2e86c1303620824721f95cc41eb7965a3dfcf02207872082ac4a3c433d41a203e6d685a459e70e551904904711626ac899238c20a0121023d4c9deae1aacf3f822dd97a28deaec7d4e4ff97be746d124a63d20e582f5b290a971600" tx_bytes = bfh(tx_hex) - tx_base43 = base_encode(tx_bytes, 43) + tx_base43 = base_encode(tx_bytes, base=43) self.assertEqual("3E2DH7.J3PKVZJ3RCOXQVS3Y./6-WE.75DDU0K58-0N1FRL565N8ZH-DG1Z.1IGWTE5HK8F7PWH5P8+V3XGZZ6GQBPHNDE+RD8CAQVV1/6PQEMJIZTGPMIJ93B8P$QX+Y2R:TGT9QW8S89U4N2.+FUT8VG+34USI/N/JJ3CE*KLSW:REE8T5Y*9:U6515JIUR$6TODLYHSDE3B5DAF:5TF7V*VAL3G40WBOM0DO2+CFKTTM$G-SO:8U0EW:M8V:4*R9ZDX$B1IRBP9PLMDK8H801PNTFB4$HL1+/U3F61P$4N:UAO88:N5D+J:HI4YR8IM:3A7K1YZ9VMRC/47$6GGW5JEL1N690TDQ4XW+TWHD:V.1.630QK*JN/.EITVU80YS3.8LWKO:2STLWZAVHUXFHQ..NZ0:.J/FTZM.KYDXIE1VBY7/:PHZMQ$.JZQ2.XT32440X/HM+UY/7QP4I+HTD9.DUSY-8R6HDR-B8/PF2NP7I2-MRW9VPW3U9.S0LQ.*221F8KVMD5ANJXZJ8WV4UFZ4R.$-NXVE+-FAL:WFERGU+WHJTHAP", tx_base43) self.assertEqual(tx_bytes, - base_decode(tx_base43, None, 43)) + base_decode(tx_base43, base=43)) def test_base58(self): data_hex = '0cd394bef396200774544c58a5be0189f3ceb6a41c8da023b099ce547dd4d8071ed6ed647259fba8c26382edbf5165dfd2404e7a8885d88437db16947a116e451a5d1325e3fd075f9d370120d2ab537af69f32e74fc0ba53aaaa637752964b3ac95cfea7' data_bytes = bfh(data_hex) - data_base58 = base_encode(data_bytes, 58) + data_base58 = base_encode(data_bytes, base=58) self.assertEqual("VuvZ2K5UEcXCVcogny7NH4Evd9UfeYipsTdWuU4jLDhyaESijKtrGWZTFzVZJPjaoC9jFBs3SFtarhDhQhAxkXosUD8PmUb5UXW1tafcoPiCp8jHy7Fe2CUPXAbYuMvAyrkocbe6", data_base58) self.assertEqual(data_bytes, - base_decode(data_base58, None, 58)) + base_decode(data_base58, base=58)) def test_base58check(self): data_hex = '0cd394bef396200774544c58a5be0189f3ceb6a41c8da023b099ce547dd4d8071ed6ed647259fba8c26382edbf5165dfd2404e7a8885d88437db16947a116e451a5d1325e3fd075f9d370120d2ab537af69f32e74fc0ba53aaaa637752964b3ac95cfea7' DIR diff --git a/electrum/transaction.py b/electrum/transaction.py t@@ -907,7 +907,7 @@ def convert_raw_tx_to_hex(raw: Union[str, bytes]) -> str: pass # try base43 try: - return base_decode(raw, length=None, base=43).hex() + return base_decode(raw, base=43).hex() except: pass # try base64 DIR diff --git a/electrum/util.py b/electrum/util.py t@@ -846,7 +846,7 @@ def parse_URI(uri: str, on_pr: Callable = None, *, loop=None) -> dict: raise InvalidBitcoinURI(f"failed to parse 'exp' field: {repr(e)}") from e if 'sig' in out: try: - out['sig'] = bh2u(bitcoin.base_decode(out['sig'], None, base=58)) + out['sig'] = bh2u(bitcoin.base_decode(out['sig'], base=58)) except Exception as e: raise InvalidBitcoinURI(f"failed to parse 'sig' field: {repr(e)}") from e