def test_valid_checksum(self): """Test checksum creation and validation.""" for test in VALID_CHECKSUM: hrp, _ = segwit_addr.bech32_decode(test) self.assertIsNotNone(hrp) pos = test.rfind('1') test = test[:pos+1] + chr(ord(test[pos + 1]) ^ 1) + test[pos+2:] hrp, _ = segwit_addr.bech32_decode(test) self.assertIsNone(hrp)
def test_valid_checksum(self): """Test checksum creation and validation.""" for spec in segwit_addr.Encoding: tests = VALID_BECH32 if spec == segwit_addr.Encoding.BECH32 else VALID_BECH32M for test in tests: hrp, _, dspec = segwit_addr.bech32_decode(test) self.assertTrue(hrp is not None and dspec == spec) pos = test.rfind('1') test = test[:pos + 1] + chr(ord(test[pos + 1]) ^ 1) + test[pos + 2:] hrp, _, dspec = segwit_addr.bech32_decode(test) self.assertIsNone(hrp)
def decodeAddress(addr, network="mainnet"): hrp = {"mainnet": "ckb", "testnet": "ckt"}[network] hrpgot, data = sa.bech32_decode(addr) if hrpgot != hrp or data == None: return False decoded = sa.convertbits(data, 5, 8, False) if decoded == None: return False payload = bytes(decoded) format_type = payload[0] if format_type == FORMAT_TYPE_SHORT: code_index = payload[1] pk = payload[2:].hex() return ("short", code_index, pk) elif format_type == FORMAT_TYPE_FULL_DATA or format_type == FORMAT_TYPE_FULL_TYPE: full_type = { FORMAT_TYPE_FULL_DATA: "Data", FORMAT_TYPE_FULL_TYPE: "Type" }[format_type] ptr = 1 code_hash = payload[ptr:ptr + 32].hex() ptr += 32 args = [] while ptr < len(payload): arg_len = int(payload[ptr]) ptr += 1 args.append(payload[ptr:ptr + arg_len].hex()) ptr += arg_len return ("full", full_type, code_hash, args)
def test_invalid_checksum(self): """Test validation of invalid checksums.""" for spec in segwit_addr.Encoding: tests = INVALID_BECH32 if spec == segwit_addr.Encoding.BECH32 else INVALID_BECH32M for test in tests: hrp, _, dspec = segwit_addr.bech32_decode(test) self.assertTrue(hrp is None or dspec != spec)
def encode_fallback(fallback, currency): """ Encode all supported fallback addresses. """ if currency in [ constants.BitcoinMainnet.SEGWIT_HRP, constants.BitcoinTestnet.SEGWIT_HRP ]: fbhrp, witness = bech32_decode(fallback, ignore_long_length=True) if fbhrp: if fbhrp != currency: raise ValueError("Not a bech32 address for this currency") wver = witness[0] if wver > 16: raise ValueError("Invalid witness version {}".format( witness[0])) wprog = u5_to_bitarray(witness[1:]) else: addrtype, addr = b58_address_to_hash160(fallback) if is_p2pkh(currency, addrtype): wver = 17 elif is_p2sh(currency, addrtype): wver = 18 else: raise ValueError( "Unknown address type for {}".format(currency)) wprog = addr return tagged('f', bitstring.pack("uint:5", wver) + wprog) else: raise NotImplementedError( "Support for currency {} not implemented".format(currency))
def bech32Decode(hrp, addr): hrpgot, data = segwit_addr.bech32_decode(addr) if hrpgot != hrp: return None decoded = segwit_addr.convertbits(data, 5, 8, False) if decoded is None or len(decoded) < 2 or len(decoded) > 40: return None return bytes(decoded)
def pk_decode(a): (hrp, pk5) = segwit_addr.bech32_decode(a) if hrp != 'pk': return None pk8 = segwit_addr.convertbits(pk5, 5, 8) if len(pk8) < 33: return None if len(pk8) > 33: for b in pk8[33:]: if b > 0: return None pk8 = pk8[0:33] return bytestohex(pk8)
def test_invalid_checksum(self): """Test validation of invalid checksums.""" for test in INVALID_CHECKSUM: hrp, _ = segwit_addr.bech32_decode(test) self.assertIsNone(hrp)
# it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import sys import segwit_addr if len(sys.argv) not in [2, 3]: print("Usage: convertBechAddress.py ADDRESS [TO-HRP]") sys.exit(-1) addr = sys.argv[1] if len(sys.argv) >= 3: hrp = sys.argv[2] else: hrp = "chi" oldHrp, data = segwit_addr.bech32_decode(addr) print("Old HRP: %s" % oldHrp) newAddr = segwit_addr.bech32_encode(hrp, data) print(newAddr)
# it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import sys import segwit_addr if len (sys.argv) not in [2, 3]: print ("Usage: convertBechAddress.py ADDRESS [TO-HRP]") sys.exit (-1); addr = sys.argv[1] if len (sys.argv) >= 3: hrp = sys.argv[2] else: hrp = "nc" oldHrp, data = segwit_addr.bech32_decode (addr) print ("Old HRP: %s" % oldHrp) newAddr = segwit_addr.bech32_encode (hrp, data) print (newAddr)