def _validate_blockchain_account_schemas( data: Dict[str, Any], address_getter: Callable, ) -> None: """Validates schema input for the PUT/PATCH/DELETE on blockchain account data""" # Make sure no duplicates addresses are given given_addresses = set() # Make sure ethereum addresses are checksummed if data['blockchain'] == SupportedBlockchain.ETHEREUM: for account_data in data['accounts']: address = address_getter(account_data) if not is_checksum_address(address): raise ValidationError( f'Given value {address} is not a checksummed ethereum address', ) if address in given_addresses: raise ValidationError( f'Address {address} appears multiple times in the request data', ) given_addresses.add(address) # Make sure bitcoin addresses are valid elif data['blockchain'] == SupportedBlockchain.BITCOIN: for account_data in data['accounts']: address = address_getter(account_data) if not is_valid_btc_address(address): raise ValidationError( f'Given value {address} is not a valid bitcoin address', ) if address in given_addresses: raise ValidationError( f'Address {address} appears multiple times in the request data', ) given_addresses.add(address)
def test_is_valid_btc_address(): assert is_valid_btc_address(UNIT_BTC_ADDRESS1) assert is_valid_btc_address(UNIT_BTC_ADDRESS2) assert is_valid_btc_address(UNIT_BTC_ADDRESS3) assert is_valid_btc_address('18ddjB7HWTVxzvTbLp1nWvaBxU3U2oTZF2') assert not is_valid_btc_address('foo') assert not is_valid_btc_address('18ddjB7HWTaxzvTbLp1nWvaixU3U2oTZ1')
def _validate_blockchain_account_schemas( data: Dict[str, Any], address_getter: Callable, ) -> None: """Validates schema input for the PUT/PATCH/DELETE on blockchain account data""" # Make sure no duplicates addresses are given given_addresses = set() # Make sure ethereum addresses are checksummed if data['blockchain'] == SupportedBlockchain.ETHEREUM: for account_data in data['accounts']: address_string = address_getter(account_data) if not address_string.endswith('.eth'): # Make sure that given value is an ethereum address try: address = to_checksum_address(address_string) except (ValueError, TypeError): raise ValidationError( f'Given value {address_string} is not an ethereum address', field_name='address', ) else: # else it's ENS name and will be checked in the transformation step and not here address = address_string if address in given_addresses: raise ValidationError( f'Address {address} appears multiple times in the request data', field_name='address', ) given_addresses.add(address) # Make sure bitcoin addresses are valid elif data['blockchain'] == SupportedBlockchain.BITCOIN: for account_data in data['accounts']: address = address_getter(account_data) if not is_valid_btc_address(address): raise ValidationError( f'Given value {address} is not a valid bitcoin address', field_name='address', ) if address in given_addresses: raise ValidationError( f'Address {address} appears multiple times in the request data', field_name='address', ) given_addresses.add(address)
def test_is_valid_btc_address(): """Test cases for Bech32 addresses taken from here: https://en.bitcoin.it/wiki/BIP_0173#Test_vectors Note that we do not support test net addresses (starting with 'tb1'). """ assert is_valid_btc_address(UNIT_BTC_ADDRESS1) assert is_valid_btc_address(UNIT_BTC_ADDRESS2) assert is_valid_btc_address(UNIT_BTC_ADDRESS3) assert is_valid_btc_address('18ddjB7HWTVxzvTbLp1nWvaBxU3U2oTZF2') assert is_valid_btc_address('bc1qhkje0xfvhmgk6mvanxwy09n45df03tj3h3jtnf') assert is_valid_btc_address('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4') assert is_valid_btc_address( 'bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3') assert is_valid_btc_address('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4') assert is_valid_btc_address( 'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx' ) assert is_valid_btc_address('BC1SW50QA3JX3S') assert is_valid_btc_address('bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj') assert not is_valid_btc_address('foo') assert not is_valid_btc_address('18ddjB7HWTaxzvTbLp1nWvaixU3U2oTZ1') assert not is_valid_btc_address( 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3') assert not is_valid_btc_address('bc1qr') assert not is_valid_btc_address( 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7') assert not is_valid_btc_address( 'tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy') assert not is_valid_btc_address( 'tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty') assert not is_valid_btc_address( 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5') assert not is_valid_btc_address( 'BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2') assert not is_valid_btc_address('bc1rw5uspcuh') assert not is_valid_btc_address( 'bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90' ) assert not is_valid_btc_address('BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P') assert not is_valid_btc_address( 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7') assert not is_valid_btc_address('bc1zw508d6qejxtdg4y5r3zarvaryvqyzf3du') assert not is_valid_btc_address( 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv') assert not is_valid_btc_address('bc1gmk9yu')