From 5f9f481d086c2d56a3f7653119884262b3381170 Mon Sep 17 00:00:00 2001 From: ThetaSinner Date: Wed, 27 Mar 2024 00:22:05 +0000 Subject: [PATCH] Extract zome call functions --- tests/src/checked/signing_keys/common.ts | 86 +++++++++++++++++-- .../signing_keys/key-collection.test.ts | 71 +++------------ .../signing_keys/reference-count.test.ts | 63 +++----------- .../signing_keys/validation-key-dist.test.ts | 71 +++------------ 4 files changed, 116 insertions(+), 175 deletions(-) diff --git a/tests/src/checked/signing_keys/common.ts b/tests/src/checked/signing_keys/common.ts index 15ef57e..520e4d7 100644 --- a/tests/src/checked/signing_keys/common.ts +++ b/tests/src/checked/signing_keys/common.ts @@ -1,5 +1,5 @@ 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(); @@ -7,7 +7,7 @@ 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 { @@ -15,15 +15,16 @@ export interface VerificationKeyDistMarkCompromised { 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 { @@ -118,3 +119,76 @@ export async function createKeyCollection( payload: { name }, }); } + +export const getMyKeyCollections = async ( + cell: CallableCell, +): Promise => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 => { + return cell.callZome({ + zome_name: "signing_keys", + fn_name: "mark_verification_key_dist", + payload: { + verification_key_dist_address, + mark, + }, + }) +} + + diff --git a/tests/src/checked/signing_keys/key-collection.test.ts b/tests/src/checked/signing_keys/key-collection.test.ts index 0015e42..c91ed57 100644 --- a/tests/src/checked/signing_keys/key-collection.test.ts +++ b/tests/src/checked/signing_keys/key-collection.test.ts @@ -11,6 +11,9 @@ import { testAppPath, sampleMiniSignProof, sampleMiniSignProofSignature, + getMyKeyCollections, + linkVerificationKeyToKeyCollection, + unlinkVerificationKeyToKeyCollection, } from "./common.js"; test("Create key collection", async () => { @@ -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); }); @@ -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); @@ -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); @@ -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]); diff --git a/tests/src/checked/signing_keys/reference-count.test.ts b/tests/src/checked/signing_keys/reference-count.test.ts index 24c1bc7..a44c336 100644 --- a/tests/src/checked/signing_keys/reference-count.test.ts +++ b/tests/src/checked/signing_keys/reference-count.test.ts @@ -4,7 +4,6 @@ import { runScenario, dhtSync } from "@holochain/tryorama"; import { Record } from "@holochain/client"; import { - VerificationKeyResponse, KeyCollectionWithKeys, createKeyCollection, distributeVerificationKey, @@ -12,6 +11,10 @@ import { 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 () => { @@ -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); }); @@ -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); }); @@ -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); diff --git a/tests/src/checked/signing_keys/validation-key-dist.test.ts b/tests/src/checked/signing_keys/validation-key-dist.test.ts index 9e69980..b7d5610 100644 --- a/tests/src/checked/signing_keys/validation-key-dist.test.ts +++ b/tests/src/checked/signing_keys/validation-key-dist.test.ts @@ -12,7 +12,7 @@ import { sampleMiniSignProofSignature, sampleMiniSignProofSignature2, sampleMiniSignProof2, - sampleMiniSignKey2, + sampleMiniSignKey2, getMyVerificationKeyDistributions, searchKeys, searchKeysLocal, markVerificationKeyRotated, } from "./common.js"; test("Distribute a key", async () => { @@ -71,11 +71,8 @@ test("Get my keys", async () => { assert.ok(record); // Alice gets the created key in her list of keys - const keys: VerificationKeyResponse[] = await alice.cells[0].callZome({ - zome_name: "signing_keys", - fn_name: "get_my_verification_key_distributions", - payload: null, - }); + const keys = await getMyVerificationKeyDistributions(alice.cells[0]); + assert.equal(keys.length, 1); assert.deepEqual( keys[0].verification_key_dist.verification_key, @@ -108,13 +105,8 @@ test("Search for a key", async () => { await dhtSync([alice, bob], alice.cells[0].cell_id[0]); // Bob searches for Alice's key - const responses: VerificationKeyResponse[] = await bob.cells[0].callZome({ - zome_name: "signing_keys", - fn_name: "search_keys", - payload: { - agent_pub_key: alice.agentPubKey, - }, - }); + const responses = await searchKeys(bob.cells[0], alice.agentPubKey); + assert.equal(responses.length, 1); assert.equal(responses[0].verification_key_dist.name, "test"); assert.equal( @@ -124,14 +116,8 @@ test("Search for a key", async () => { // Note: This is more of a PoC than anything at this point, but it's a start // Bob searches for Alice's key while offline - const offline_responses: VerificationKeyResponse[] = - await bob.cells[0].callZome({ - zome_name: "signing_keys", - fn_name: "search_keys_local", - payload: { - agent_pub_key: alice.agentPubKey, - }, - }); + const offline_responses = await searchKeysLocal(bob.cells[0], alice.agentPubKey); + assert.equal(offline_responses.length, 1); assert.equal(offline_responses[0].verification_key_dist.name, "test"); assert.equal( @@ -171,32 +157,14 @@ test("Mark a key as compromised", async () => { // Alice marks her own key as compromised const compromisedSince = new Date().getUTCMilliseconds() * 1000; - await alice.cells[0].callZome({ - zome_name: "signing_keys", - fn_name: "mark_verification_key_dist", - payload: { - verification_key_dist_address: vf_key_dist_address, - mark: { - Compromised: { - note: "I think someone is using my private key!", - since: compromisedSince, - }, - }, - }, - }); + await markVerificationKeyRotated(alice.cells[0], vf_key_dist_address, { Compromised: { note: "I think someone is using my private key!", since: compromisedSince } }) // TODO should not need to DHT sync here. What I actually want is an 'Alice synced' to be serving up the links // that Bob will need in the next step. For now, the test is flaky without this sync... await dhtSync([alice, bob], alice.cells[0].cell_id[0]); // No DHT sync, but Bob does an online search for Alice's keys which *should* do a get_links to the network and see Alice's mark. - const responses: VerificationKeyResponse[] = await bob.cells[0].callZome({ - zome_name: "signing_keys", - fn_name: "search_keys", - payload: { - agent_pub_key: alice.agentPubKey, - }, - }); + const responses = await searchKeys(bob.cells[0], alice.agentPubKey); assert.equal(responses.length, 1); assert.equal(responses[0].verification_key_dist.marks.length, 1); @@ -251,31 +219,14 @@ test("Mark a key as rotated", async () => { const new_vf_key_dist_address = new_record.signed_action.hashed.hash; // Alice marks her own key as compromised - await alice.cells[0].callZome({ - zome_name: "signing_keys", - fn_name: "mark_verification_key_dist", - payload: { - verification_key_dist_address: vf_key_dist_address, - mark: { - Rotated: { - new_verification_key_dist_address: new_vf_key_dist_address, - }, - }, - }, - }); + await markVerificationKeyRotated(alice.cells[0], vf_key_dist_address, { Rotated: { new_verification_key_dist_address: new_vf_key_dist_address } }) // TODO should not need to DHT sync here. What I actually want is an 'Alice synced' to be serving up the links // that Bob will need in the next step. For now, the test is flaky without this sync... await dhtSync([alice, bob], alice.cells[0].cell_id[0]); // No DHT sync, but Bob does an online search for Alice's keys which *should* do a get_links to the network and see Alice's mark. - const responses: VerificationKeyResponse[] = await bob.cells[0].callZome({ - zome_name: "signing_keys", - fn_name: "search_keys", - payload: { - agent_pub_key: alice.agentPubKey, - }, - }); + const responses = await searchKeys(bob.cells[0], alice.agentPubKey); // Sort in place by created_at. The ordering is not guaranteed from the search! responses.sort((a, b) => a.created_at - b.created_at);