コード例 #1
0
def test_proof_not_hashed():
    target_root = '7102ecb1c3412a29997744c8e18d848c4f668d5563325ada204a453dc694308f'

    tree = MerkleTree()

    for user, perm in permission_json.items():
        for schema_id, schema_perm in perm.items():
            tree.add_leaf(json.dumps(schema_perm))

    tree.grow_tree()

    requested_leaf = json.dumps(permission_json['user2']['0'])

    proof = tree.get_proof(requested_leaf, is_hashed=False)

    is_good = tree.verify_leaf(requested_leaf, target_root,
                               proof, is_hashed=False)

    assert is_good

    requested_leaf = json.dumps(permission_json['user3']['0'])
    proof_json = tree.get_proof(requested_leaf, is_hashed=False, as_json=True)

    is_good = tree.verify_leaf(requested_leaf,
                               target_root,
                               json.loads(proof_json), is_hashed=False)

    assert is_good
コード例 #2
0
def systest_merkle_proof_permissions():
    ipfs = get_ipfs_client()
    users, consumers, providers = compile_actors()

    for provider in providers:
        log.debug(f'run systest_merkle_proof_'
                  f'permissions for Provider {provider}')

        provider_uapp = UnificationUapp(get_eos_rpc_client(), provider)

        permission_db = PermissionBatchDatabase(pb_default_db())
        permissions = UnifPermissions(ipfs, provider_uapp, permission_db)

        for consumer in consumers:
            if consumer != provider:
                log.debug(f'Provider {provider}: load '
                          f'permissions for Consumer {consumer}')
                permissions.load_consumer_perms(consumer)

                permissions_obj = permissions.get_all_perms()

                tree = MerkleTree()

                for user, perm in permissions_obj['permissions'].items():
                    for schema_id, schema_perm in perm.items():
                        tree.add_leaf(json.dumps(schema_perm))

                tree.grow_tree()

                log.debug(f"Generated merkle root: {tree.get_root_str()}")
                log.debug(f"Recorded merkle root: "
                          f"{permissions_obj['merkle_root']}")

                for user, perm in permissions_obj['permissions'].items():
                    for schema_id, schema_perm in perm.items():
                        requested_leaf = json.dumps(schema_perm)
                        proof_chain = tree.get_proof(requested_leaf,
                                                     is_hashed=False)
                        log.debug(f'Permission leaf for {user}:'
                                  f' {requested_leaf}')
                        log.debug(f'Proof chain for {user} - '
                                  f'Schema {schema_id} '
                                  f'permission leaf: '
                                  f'{json.dumps(proof_chain)}')

                        # simulate only having access to leaf,
                        # root and proof chain for leaf
                        verify_tree = MerkleTree()

                        is_good = verify_tree.verify_leaf(
                            requested_leaf,
                            permissions_obj['merkle_root'],
                            proof_chain,
                            is_hashed=False)

                        log.debug(f'Leaf is valid: {is_good}')

                        assert is_good
コード例 #3
0
    def __generate_merkle(self, perms):
        tree = MerkleTree()
        for user, perm in perms.items():
            for schema_id, schema_perm in perm.items():
                tree.add_leaf(json.dumps(schema_perm))

        tree.grow_tree()

        return tree.get_root_str()
コード例 #4
0
    def get_proof(self, user, schema_id='0'):
        user_permissions = self.__consumer_perms[user][schema_id]
        tree = MerkleTree()
        for user, perm in self.__consumer_perms.items():
            for schema_id, schema_perm in perm.items():
                tree.add_leaf(json.dumps(schema_perm))

        tree.grow_tree()

        proof = tree.get_proof(json.dumps(user_permissions), is_hashed=False)

        return proof
コード例 #5
0
def test_get_root_not_grown_exception():
    tree = MerkleTree()

    for user, perm in permission_json.items():
        tree.add_leaf(json.dumps(perm))

    with pytest.raises(MerkleException) as e:
        tree.get_root()
    assert 'No tree stored. Grow from seed, or generate new tree' in str(e.value)

    with pytest.raises(MerkleException) as e:
        tree.get_root_str()
    assert 'No tree stored. Grow from seed, or generate new tree' in str(e.value)
コード例 #6
0
def test_three_leaves():
    tree = MerkleTree()
    for user, perm in permission_json_three.items():
        tree.add_leaf(json.dumps(perm))

    tree.grow_tree()

    for user, perm in permission_json_three.items():
        requested_leaf = json.dumps(permission_json_three[user])
        proof = tree.get_proof(requested_leaf, is_hashed=False)
        is_good = tree.verify_leaf(requested_leaf, tree.get_root_str(),
                                   proof, is_hashed=False)

        assert is_good
コード例 #7
0
def test_load_from_seed_json():
    tree = MerkleTree()

    for user, perm in permission_json.items():
        for schema_id, schema_perm in perm.items():
            tree.add_leaf(json.dumps(schema_perm))

    tree.grow_tree()

    seed_json = tree.get_seed(as_json=True)
    original_root = tree.get_root_str()

    new_tree = MerkleTree(seed_json)

    assert original_root == new_tree.get_root_str()
コード例 #8
0
def test_proof_all():
    tree = MerkleTree()

    for user, perm in permission_json.items():
        for schema_id, schema_perm in perm.items():
            tree.add_leaf(json.dumps(schema_perm))

    tree.grow_tree()

    for user, perm in permission_json.items():
        for schema_id, schema_perm in perm.items():
            requested_leaf = json.dumps(permission_json[user][schema_id])
            proof = tree.get_proof(requested_leaf, is_hashed=False)
            is_good = tree.verify_leaf(requested_leaf, tree.get_root_str(),
                                   proof, is_hashed=False)

        assert is_good
コード例 #9
0
def test_merkle_root():

    target_root = '7102ecb1c3412a29997744c8e18d848c4f668d5563325ada204a453dc694308f'

    tree = MerkleTree()

    for user, perm in permission_json.items():
        for schema_id, schema_perm in perm.items():
            tree.add_leaf(json.dumps(schema_perm))

    tree.grow_tree()

    m_root = tree.get_root_str()

    print(m_root)

    assert m_root == target_root
コード例 #10
0
def test_proof_from_new_seeded_tree():
    tree = MerkleTree()

    for user, perm in permission_json.items():
        for schema_id, schema_perm in perm.items():
            tree.add_leaf(json.dumps(schema_perm))

    tree.grow_tree()
    original_root = tree.get_root_str()

    seed_json = tree.get_seed(as_json=True)

    seeded_tree = MerkleTree(seed_json)

    proof_from_new = seeded_tree.get_proof(json.dumps(permission_json['user2']['0']), is_hashed=False)

    is_good = seeded_tree.verify_leaf(json.dumps(permission_json['user2']['0']),
                                      original_root,
                                      proof_from_new, is_hashed=False)

    assert is_good
コード例 #11
0
def test_proof_new_tree():
    target_root = '7102ecb1c3412a29997744c8e18d848c4f668d5563325ada204a453dc694308f'

    tree = MerkleTree()

    for user, perm in permission_json.items():
        for schema_id, schema_perm in perm.items():
            tree.add_leaf(json.dumps(schema_perm))

    tree.grow_tree()

    requested_leaf = json.dumps(permission_json['user2']['0'])

    proof_chain = tree.get_proof(requested_leaf, is_hashed=False)

    # simulate only having access to leaf, root and proof chain for leaf
    verify_tree = MerkleTree()

    is_good = verify_tree.verify_leaf(requested_leaf, target_root,
                                      proof_chain, is_hashed=False)

    assert is_good