Skip to content

Commit

Permalink
Extract zome call functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Mar 27, 2024
1 parent 999c5a4 commit 5f9f481
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 175 deletions.
86 changes: 80 additions & 6 deletions tests/src/checked/signing_keys/common.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { CallableCell } from "@holochain/tryorama";
import { Record } from "@holochain/client";
import {ActionHash, AgentPubKey, Record} from "@holochain/client";
import { decode } from "@msgpack/msgpack";

const utf8Encode = new TextEncoder();

export const testAppPath = process.cwd() + "/../workdir/checked.happ";

export interface VerificationKeyDistMarkRotated {
new_verification_key_dist_address: number[];
new_verification_key_dist_address: Uint8Array;
}

export interface VerificationKeyDistMarkCompromised {
note: string;
since: number;
}

export type VfKeyDistMark =
| { Rotated: VerificationKeyDistMarkRotated }
| { Compromised: VerificationKeyDistMarkCompromised };

export interface VerificationKeyDist {
verification_key: string;
key_type: { MiniSignEd25519: null };
name: string;
expires_at: number;
marks: (
| { Rotated: VerificationKeyDistMarkRotated }
| { Compromised: VerificationKeyDistMarkCompromised }
)[];
marks: VfKeyDistMark[];
}

export interface VerificationKeyResponse {
Expand Down Expand Up @@ -118,3 +119,76 @@ export async function createKeyCollection(
payload: { name },
});
}

export const getMyKeyCollections = async (
cell: CallableCell,
): Promise<KeyCollectionWithKeys[]> => {
return cell.callZome({
zome_name: "signing_keys",
fn_name: "get_my_key_collections",
payload: null,
});
}

export const linkVerificationKeyToKeyCollection = async (cell: CallableCell, verification_key_dist_address: ActionHash, key_collection_name: string): Promise<ActionHash> => {
return cell.callZome({
zome_name: "signing_keys",
fn_name: "link_verification_key_to_key_collection",
payload: {
verification_key_dist_address,
key_collection_name,
},
})
}

export const unlinkVerificationKeyToKeyCollection = async (cell: CallableCell, verification_key_dist_address: ActionHash, key_collection_name: string): Promise<ActionHash> => {
return cell.callZome({
zome_name: "signing_keys",
fn_name: "unlink_verification_key_from_key_collection",
payload: {
verification_key_dist_address,
key_collection_name,
},
})
}

export const getMyVerificationKeyDistributions = async (cell: CallableCell): Promise<VerificationKeyResponse[]> => {
return cell.callZome({
zome_name: "signing_keys",
fn_name: "get_my_verification_key_distributions",
payload: null,
});
}

export const searchKeys = async (cell: CallableCell, agent_pub_key: AgentPubKey): Promise<VerificationKeyResponse[]> => {
return cell.callZome({
zome_name: "signing_keys",
fn_name: "search_keys",
payload: {
agent_pub_key,
},
});
}

export const searchKeysLocal = async (cell: CallableCell, agent_pub_key: AgentPubKey): Promise<VerificationKeyResponse[]> => {
return cell.callZome({
zome_name: "signing_keys",
fn_name: "search_keys_local",
payload: {
agent_pub_key,
},
});
}

export const markVerificationKeyRotated = async (cell: CallableCell, verification_key_dist_address: ActionHash, mark: VfKeyDistMark): Promise<ActionHash> => {
return cell.callZome({
zome_name: "signing_keys",
fn_name: "mark_verification_key_dist",
payload: {
verification_key_dist_address,
mark,
},
})
}


71 changes: 12 additions & 59 deletions tests/src/checked/signing_keys/key-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import {
testAppPath,
sampleMiniSignProof,
sampleMiniSignProofSignature,
getMyKeyCollections,
linkVerificationKeyToKeyCollection,
unlinkVerificationKeyToKeyCollection,
} from "./common.js";

test("Create key collection", async () => {
Expand Down Expand Up @@ -65,12 +68,7 @@ test("Get my key collections", async () => {
assert.ok(record);
}

const key_collections: KeyCollectionWithKeys[] =
await alice.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "get_my_key_collections",
payload: null,
});
const key_collections = await getMyKeyCollections(alice.cells[0]);

assert.equal(key_collections.length, 2);
});
Expand Down Expand Up @@ -105,21 +103,9 @@ test("Link verification key distribution to collection", async () => {
assert.ok(key_collection_record);

// Bob links Alice's verification key to the key collection
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "link_verification_key_to_key_collection",
payload: {
verification_key_dist_address: vf_key_dist_address,
key_collection_name: "a test",
},
});

const key_collections: KeyCollectionWithKeys[] =
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "get_my_key_collections",
payload: null,
});
await linkVerificationKeyToKeyCollection(bob.cells[0], vf_key_dist_address, "a test");

const key_collections= await getMyKeyCollections(bob.cells[0]);

assert.equal(key_collections.length, 1);
assert.equal(key_collections[0].verification_keys.length, 1);
Expand Down Expand Up @@ -155,32 +141,13 @@ test("Unlink verification key from collection", async () => {
assert.ok(key_collection_record);

// Bob links Alice's verification key to the key collection
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "link_verification_key_to_key_collection",
payload: {
verification_key_dist_address: vf_key_dist_address,
key_collection_name: "a test",
},
});
await linkVerificationKeyToKeyCollection(bob.cells[0], vf_key_dist_address, "a test");

// Bob unlinks Alice's verification key from the key collection
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "unlink_verification_key_from_key_collection",
payload: {
verification_key_dist_address: vf_key_dist_address,
key_collection_name: "a test",
},
});
await unlinkVerificationKeyToKeyCollection(bob.cells[0], vf_key_dist_address, "a test");

// Now getting key collections should return a single, empty key collection
const key_collections: KeyCollectionWithKeys[] =
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "get_my_key_collections",
payload: null,
});
const key_collections = await getMyKeyCollections(bob.cells[0]);

assert.equal(key_collections.length, 1);
assert.equal(key_collections[0].verification_keys.length, 0);
Expand Down Expand Up @@ -223,27 +190,13 @@ test("Remote validation", async () => {
verification_key_record.signed_action.hashed.hash;

// Bob links Alice's verification key to the key collection
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "link_verification_key_to_key_collection",
payload: {
verification_key_dist_address: vf_key_dist_address,
key_collection_name: "a test 1",
},
});
await linkVerificationKeyToKeyCollection(bob.cells[0], vf_key_dist_address, "a test 1");

// The DHT shouldn't sync if the remote validation fails
await dhtSync([alice, bob], alice.cells[0].cell_id[0]);

// Bob unlinks Alice's verification key from the key collection
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "unlink_verification_key_from_key_collection",
payload: {
verification_key_dist_address: vf_key_dist_address,
key_collection_name: "a test 1",
},
});
await unlinkVerificationKeyToKeyCollection(bob.cells[0], vf_key_dist_address, "a test 1");

// The DHT shouldn't sync if the remote validation fails
await dhtSync([alice, bob], alice.cells[0].cell_id[0]);
Expand Down
63 changes: 13 additions & 50 deletions tests/src/checked/signing_keys/reference-count.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import { runScenario, dhtSync } from "@holochain/tryorama";
import { Record } from "@holochain/client";

import {
VerificationKeyResponse,
KeyCollectionWithKeys,
createKeyCollection,
distributeVerificationKey,
sampleMiniSignKey,
testAppPath,
sampleMiniSignProof,
sampleMiniSignProofSignature,
linkVerificationKeyToKeyCollection,
getMyVerificationKeyDistributions,
searchKeys,
getMyKeyCollections,
} from "./common.js";

test("Get my keys for a key which is in another agent's collection", async () => {
Expand Down Expand Up @@ -42,23 +45,12 @@ test("Get my keys for a key which is in another agent's collection", async () =>
await createKeyCollection(bob.cells[0], "a test");

// Bob links Alice's verification key to their key collection
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "link_verification_key_to_key_collection",
payload: {
verification_key_dist_address: vf_key_dist_address,
key_collection_name: "a test",
},
});
await linkVerificationKeyToKeyCollection(bob.cells[0], vf_key_dist_address, "a test");

await dhtSync([alice, bob], alice.cells[0].cell_id[0]);

// Alice searches for their own key
const responses: VerificationKeyResponse[] = await alice.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "get_my_verification_key_distributions",
payload: null,
});
const responses = await getMyVerificationKeyDistributions(alice.cells[0]);
assert.equal(responses.length, 1);
assert.equal(responses[0].reference_count, 1);
});
Expand Down Expand Up @@ -92,25 +84,13 @@ test("Search for a key which is in another agent's collection", async () => {
await createKeyCollection(bob.cells[0], "a test");

// Bob links Alice's verification key to their key collection
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "link_verification_key_to_key_collection",
payload: {
verification_key_dist_address: vf_key_dist_address,
key_collection_name: "a test",
},
});
await linkVerificationKeyToKeyCollection(bob.cells[0], vf_key_dist_address, "a test");

await dhtSync([alice, bob], alice.cells[0].cell_id[0]);

// Alice searches for their own key
const responses: VerificationKeyResponse[] = await alice.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "search_keys",
payload: {
agent_pub_key: alice.agentPubKey,
},
});
const responses = await searchKeys(alice.cells[0], alice.agentPubKey);

assert.equal(responses.length, 1);
assert.equal(responses[0].reference_count, 1);
});
Expand Down Expand Up @@ -146,36 +126,19 @@ test("Get my key collections for a key which is in another agent's collection",
await createKeyCollection(bob.cells[0], "bob test");

// Bob links Alice's verification key to their key collection
await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "link_verification_key_to_key_collection",
payload: {
verification_key_dist_address: vf_key_dist_address,
key_collection_name: "bob test",
},
});
await linkVerificationKeyToKeyCollection(bob.cells[0], vf_key_dist_address, "bob test");

// Carol creates a collection
await createKeyCollection(carol.cells[0], "carol test");

// Carol links Alice's verification key to their key collection
await carol.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "link_verification_key_to_key_collection",
payload: {
verification_key_dist_address: vf_key_dist_address,
key_collection_name: "carol test",
},
});
await linkVerificationKeyToKeyCollection(carol.cells[0], vf_key_dist_address, "carol test");

await dhtSync([alice, bob, carol], alice.cells[0].cell_id[0]);

// Bob checks their key collections
const responses: KeyCollectionWithKeys[] = await bob.cells[0].callZome({
zome_name: "signing_keys",
fn_name: "get_my_key_collections",
payload: null,
});
const responses = await getMyKeyCollections(bob.cells[0]);

assert.equal(responses.length, 1);
assert.equal(responses[0].name, "bob test");
assert.equal(responses[0].verification_keys.length, 1);
Expand Down
Loading

0 comments on commit 5f9f481

Please sign in to comment.