def check_if_chain_matches_chain_uri(web3: Web3, blockchain_uri: URI) -> bool: chain_id, resource_type, resource_hash = parse_BIP122_uri(blockchain_uri) genesis_block = web3.eth.getBlock("earliest") if encode_hex(genesis_block["hash"]) != chain_id: return False if resource_type == BLOCK: resource = web3.eth.getBlock(resource_hash) else: raise ValueError(f"Unsupported resource type: {resource_type}") if encode_hex(resource["hash"]) == resource_hash: return True else: return False
def get_matching_w3(chain_uri: URI, config: Config) -> Tuple[Web3, str]: genesis_hash = parse_BIP122_uri(chain_uri)[0] chain_data = SUPPORTED_GENESIS_HASHES[genesis_hash] web3 = setup_w3(chain_data[1], config.private_key) return web3, chain_data[0]
def test_parse_BIP122_uri(value, expected_resource_type): chain_id, resource_type, resource_identifier = parse_BIP122_uri(value) assert chain_id == HASH_A assert resource_type == expected_resource_type assert resource_identifier == HASH_B