Skip to content

Commit

Permalink
Count users on search keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Feb 23, 2024
1 parent 840b27f commit abac562
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
19 changes: 16 additions & 3 deletions dnas/trusted/zomes/coordinator/trusted/src/gpg_key_dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ pub struct SearchKeysRequest {
pub query: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, SerializedBytes)]
pub struct SearchKeysResponse {
pub key: Record,
pub key_collection_count: usize,
}

#[hdk_extern]
pub fn search_keys(request: SearchKeysRequest) -> ExternResult<Vec<Record>> {
pub fn search_keys(request: SearchKeysRequest) -> ExternResult<Vec<SearchKeysResponse>> {
let mut links = get_links(
GetLinksInputBuilder::try_new(
make_base_hash(&request.query)?,
Expand Down Expand Up @@ -87,9 +93,16 @@ pub fn search_keys(request: SearchKeysRequest) -> ExternResult<Vec<Record>> {
.into_iter()
.flat_map(|l| AnyDhtHash::try_from(l.target).ok())
{
match get(target, GetOptions::default())? {
match get(target.clone(), GetOptions::default())? {
Some(r) => {
out.push(r);
let link_count = count_links(LinkQuery::new(
target,
LinkTypes::GpgKeyDistToKeyCollection.try_into_filter()?,
))?;
out.push(SearchKeysResponse {
key: r,
key_collection_count: link_count,
});
}
_ => {
// Link target not found
Expand Down
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "tests",
"private": true,
"scripts": {
"test": "vitest run collection"
"test": "vitest run"
},
"dependencies": {
"@msgpack/msgpack": "^2.8.0",
Expand Down
53 changes: 48 additions & 5 deletions tests/src/trusted/trusted/gpg-key-dist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { runScenario, dhtSync, CallableCell } from '@holochain/tryorama';
import { NewEntryAction, ActionHash, Record, AppBundleSource, fakeDnaHash, fakeActionHash, fakeAgentPubKey, fakeEntryHash } from '@holochain/client';
import { decode } from '@msgpack/msgpack';

import { distributeGpgKey, sampleGpgKey } from './common.js';
import { createKeyCollection, distributeGpgKey, sampleGpgKey } from './common.js';

test('Distribute GPG Key', async () => {
await runScenario(async scenario => {
Expand Down Expand Up @@ -33,7 +33,7 @@ test('Get my keys', async () => {
// Bob gets the created GpgKey
const keys: Record[] = await alice.cells[0].callZome({
zome_name: "trusted",
fn_name: "get_my_keys",
fn_name: "get_my_gpg_key_dists",
payload: null,
});
assert.equal(1, keys.length);
Expand All @@ -57,17 +57,60 @@ test('Search for a key', async () => {
await dhtSync([alice, bob], alice.cells[0].cell_id[0]);

// Bob searches for Alice's GPG key
const keys: Record[] = await bob.cells[0].callZome({
const responses: any[] = await bob.cells[0].callZome({
zome_name: "trusted",
fn_name: "search_keys",
payload: {
// Assume Alice has told Bob the fingerprint
query: "0B1D4843CA2F198CAC2F5C6A449D7AE5D2532CEF"
},
});
assert.equal(1, keys.length);
const decoded = (decode((keys[0].entry as any).Present.entry) as any);
assert.equal(1, responses.length);
const decoded = (decode((responses[0].key.entry as any).Present.entry) as any);
assert.equal("Alice", decoded.name);
assert.equal(sampleGpgKey().trim(), decoded.public_key);
});
});

test('Search for a key which is in another agent collection', async () => {
await runScenario(async scenario => {
const testAppPath = process.cwd() + '/../workdir/hWOT.happ';
const appSource = { appBundleSource: { path: testAppPath } };

const [alice, bob] = await scenario.addPlayersWithApps([appSource, appSource]);

await scenario.shareAllAgents();

// Alice distributes a GPG key
const record: Record = await distributeGpgKey(alice.cells[0], sampleGpgKey());
assert.ok(record);

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

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

// Bob links the GPG key to their key collection
await bob.cells[0].callZome({
zome_name: "trusted",
fn_name: "link_gpg_key_to_key_collection",
payload: {
gpg_key_fingerprint: (decode((record.entry as any).Present.entry) as any).fingerprint,
key_collection_name: "a test",
},
});

// Alice searches for their own key
const responses: any[] = await bob.cells[0].callZome({
zome_name: "trusted",
fn_name: "search_keys",
payload: {
query: "0B1D4843CA2F198CAC2F5C6A449D7AE5D2532CEF"
},
});
assert.equal(1, responses.length);
const decoded = (decode((responses[0].key.entry as any).Present.entry) as any);
assert.equal("Alice", decoded.name);
assert.equal(1, responses[0].key_collection_count);
});
});

0 comments on commit abac562

Please sign in to comment.