def _show_cashaddr_warning(self, address_text): ''' cash addresses are not in the future for BSV. Anyone who uses one should be warned that they are being phased out, in order to encourage them to pre-emptively move on. ''' address_text = self._parse_address_text(address_text) # We only care if it is decoded, as this will be a cash address. try: cashaddr.decode(address_text) except: return last_check_time = PayToEdit.last_cashaddr_warning ignore_watermark_time = time.time() - 24 * 60 * 60 if last_check_time is None or last_check_time < ignore_watermark_time: PayToEdit.last_cashaddr_warning = time.time() message = ("<p>" + _( "One or more of the addresses you have provided has been recognized " + "as a 'cash address'. For now, this is acceptable but is recommended that you get " + "in the habit of requesting that anyone who provides you with payment addresses " + "do so in the form of normal Bitcoin SV addresses." ) + "</p>" + "<p>" + _( "Within the very near future, various services and applications in the Bitcoin " + "SV ecosystem will stop accepting 'cash addresses'. It is in your best interest " + "to make sure you transition over to normal Bitcoin SV addresses as soon as " + "possible, in order to ensure that you can both be paid, and also get paid." ) + "</p>") util.MessageBox.show_warning(message, title=_("Cash address warning"))
def test_prefix(self): with self.assertRaises(ValueError): cashaddr.decode(":ppm2qsznhks23z7629mms6s4cwef74vcwvn0h82") with self.assertRaises(ValueError): cashaddr.decode("ppm2qsznhks23z7629mms6s4cwef74vcwvn0h82") with self.assertRaises(ValueError): cashaddr.decode("bitcoin cash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h82") with self.assertRaises(ValueError): cashaddr.decode("bitcoin cash:ab") # b is invalid with self.assertRaises(ValueError): cashaddr.decode("bitcoincash:ppm2qsznbks23z7629mms6s4cwef74vcwvn0h82")
def test_valid_pubkeys(self): """Test whether valid P2SH addresses decode to the correct output.""" for (address, hashbytes) in zip(VALID_PUBKEY_ADDRESSES, VALID_HASHES): rprefix, kind, addr_hash = cashaddr.decode(address) self.assertEqual(rprefix, BSV_PREFIX) self.assertEqual(kind, cashaddr.PUBKEY_TYPE) self.assertEqual(addr_hash, hashbytes)
def test_valid_scripthash(self): """Test whether valid P2PK addresses decode to the correct output.""" for (address, hashbytes) in zip(VALID_SCRIPT_ADDRESSES, VALID_HASHES): rprefix, kind, addr_hash = cashaddr.decode(address) self.assertEqual(rprefix, BSV_PREFIX) self.assertEqual(kind, cashaddr.SCRIPT_TYPE) self.assertEqual(addr_hash, hashbytes)
def test_bad_decode_size(self): """Test that addresses with invalid sizes fail to decode.""" for bits_size in self.valid_sizes: size = bits_size // 8 # Convert to a valid number of bytes for a hash hashbytes = bytes(random.randint(0, 255) for i in range(size)) payload = cashaddr._pack_addr_data(cashaddr.PUBKEY_TYPE, hashbytes) # Add some more 5-bit data after size has been encoded payload += bytes(random.randint(0, 15) for i in range(3)) # Add checksum payload += cashaddr._create_checksum(BSV_PREFIX, payload) addr = BSV_PREFIX + ':' + ''.join(cashaddr._CHARSET[d] for d in payload) # Check decode fails. This can trigger the length mismatch, # excess padding, or non-zero padding errors with self.assertRaises(ValueError): cashaddr.decode(addr)
def test_valid_pubkeys(self): """Test whether valid P2SH addresses decode to the correct output.""" for (address, hashbytes) in zip(VALID_PUBKEY_ADDRESSES, VALID_HASHES): rprefix, kind, addr_hash = cashaddr.decode(address) assert rprefix == BSV_PREFIX assert kind == cashaddr.PUBKEY_TYPE assert addr_hash == hashbytes
def test_valid_scripthash(self): """Test whether valid P2PK addresses decode to the correct output.""" for (address, hashbytes) in zip(VALID_SCRIPT_ADDRESSES, VALID_HASHES): rprefix, kind, addr_hash = cashaddr.decode(address) assert rprefix == BSV_PREFIX assert kind == cashaddr.SCRIPT_TYPE assert addr_hash == hashbytes
def test_bad_decode_checksum(self): """Test whether addresses with invalid checksums fail to decode.""" for bits_size in self.valid_sizes: size = bits_size // 8 # Convert to a valid number of bytes for a hash hashbytes = bytes(random.randint(0, 255) for i in range(size)) addr = cashaddr.encode_full(BSV_PREFIX, cashaddr.PUBKEY_TYPE, hashbytes) addrlist = list(addr) # Inject an error values = list(cashaddr._CHARSET) while True: pos = random.randint(0, len(addr) - 1) choice = random.choice(values) if choice != addrlist[pos] and addrlist[pos] in values: addrlist[pos] = choice break mangled_addr = ''.join(addrlist) with self.assertRaises(ValueError) as e: cashaddr.decode(mangled_addr) self.assertTrue('invalid checksum' in e.exception.args[0])
def test_address_case(self): prefix, _kind, _hash160 = cashaddr.decode("bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq") assert prefix == "bitcoincash" prefix, _kind, _hash160 = cashaddr.decode("BITCOINCASH:PPM2QSZNHKS23Z7629MMS6S4CWEF74VCWVN0H829PQ") assert prefix == "BITCOINCASH" with self.assertRaises(ValueError): cashaddr.decode("bitcoincash:PPM2QSZNHKS23Z7629MMS6S4CWEF74VCWVN0H829PQ") with self.assertRaises(ValueError): cashaddr.decode("bitcoincash:ppm2qsznhks23z7629mmS6s4cwef74vcwvn0h829pq")
def test_encode_decode(self): """Test whether valid addresses encode and decode properly, for all valid hash sizes. """ for prefix in (BSV_PREFIX, BSV_TESTNET_PREFIX): for bits_size in self.valid_sizes: size = bits_size // 8 # Convert to a valid number of bytes for a hash hashbytes = bytes(random.randint(0, 255) for i in range(size)) addr = cashaddr.encode_full(prefix, cashaddr.PUBKEY_TYPE, hashbytes) rprefix, kind, addr_hash = cashaddr.decode(addr) self.assertEqual(rprefix, prefix) self.assertEqual(kind, cashaddr.PUBKEY_TYPE) self.assertEqual(addr_hash, hashbytes)
def test_decode_bad_inputs(self): with self.assertRaises(TypeError): cashaddr.decode(b'foobar')
def test_decode_bad_inputs(self): with pytest.raises(TypeError): cashaddr.decode(b'foobar')
def test_bad_decode_checksum(self, mangled_addr): """Test whether addresses with invalid checksums fail to decode.""" with pytest.raises(ValueError) as e: cashaddr.decode('bitcoincash:' + mangled_addr) assert 'invalid checksum' in str(e.value)