Skip to content

Commit

Permalink
Lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Mar 3, 2024
1 parent b283e5f commit fff2d4d
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 109 deletions.
27 changes: 12 additions & 15 deletions dnas/trusted/zomes/coordinator/trusted/src/key_collection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
convert_to_app_entry_type,
verification_key_dist::VfKeyResponse,
};
use crate::{convert_to_app_entry_type, verification_key_dist::VfKeyResponse};
use hdk::prelude::*;
use nanoid::nanoid;
use trusted_integrity::prelude::*;
Expand Down Expand Up @@ -95,9 +92,9 @@ pub struct LinkVfKeyDistToKeyCollectionRequest {
pub fn link_verification_key_to_key_collection(
request: LinkVfKeyDistToKeyCollectionRequest,
) -> ExternResult<ActionHash> {
let (kc_action, _) = find_key_collection(&request.key_collection_name)?.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"No key collection found with the given name"
))))?;
let (kc_action, _) = find_key_collection(&request.key_collection_name)?.ok_or(wasm_error!(
WasmErrorInner::Guest(String::from("No key collection found with the given name"))
))?;

let tag_id = nanoid!();

Expand Down Expand Up @@ -128,9 +125,9 @@ pub struct UnlinkVfKeyFromKeyCollectionRequest {
pub fn unlink_verification_key_from_key_collection(
request: UnlinkVfKeyFromKeyCollectionRequest,
) -> ExternResult<()> {
let (kc_action, _) = find_key_collection(&request.key_collection_name)?.ok_or(wasm_error!(WasmErrorInner::Guest(
String::from("No key collection found with the given name")
)))?;
let (kc_action, _) = find_key_collection(&request.key_collection_name)?.ok_or(wasm_error!(
WasmErrorInner::Guest(String::from("No key collection found with the given name"))
))?;

let agent_info = agent_info()?;

Expand Down Expand Up @@ -250,11 +247,11 @@ fn find_key_collection(name: &str) -> ExternResult<Option<(ActionHash, KeyCollec
.filter_map(|r| -> Option<(ActionHash, KeyCollection)> {
let action_hash = r.action_hashed().hash.clone();

convert_to_app_entry_type(r).ok().map(|kc| {
(action_hash, kc)
})
convert_to_app_entry_type(r)
.ok()
.map(|kc| (action_hash, kc))
})
.find(|(_, kc)| &kc.name == name))
.find(|(_, kc)| kc.name == name))
}

/// Counts the number of references from a [VerificationKeyDist] to [KeyCollection]s.
Expand All @@ -267,7 +264,7 @@ pub fn get_key_collections_reference_count(
) -> ExternResult<usize> {
let links = get_links(
GetLinksInputBuilder::try_new(entry_hash, LinkTypes::VfKeyDistToKeyCollection)?
.get_options(get_options.strategy.clone())
.get_options(get_options.strategy)
.build(),
)?;
Ok(links
Expand Down
41 changes: 23 additions & 18 deletions dnas/trusted/zomes/coordinator/trusted/src/verification_key_dist.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::{convert_to_app_entry_type, key_collection::get_key_collections_reference_count};
use chrono::{DateTime, Utc};
use crate::{
convert_to_app_entry_type,
key_collection::get_key_collections_reference_count,
};
use hdk::prelude::*;
use trusted_integrity::prelude::*;

Expand Down Expand Up @@ -94,13 +91,18 @@ pub fn get_my_verification_key_distributions(_: ()) -> ExternResult<Vec<VfKeyRes
let mut out = Vec::with_capacity(vf_key_dist_entries.len());
for r in vf_key_dist_entries.into_iter() {
let created_at = r.action().timestamp();
let key_dist_address = r.action().entry_hash().ok_or_else(|| {
wasm_error!(WasmErrorInner::Guest(
"Missing entry hash for VerificationKeyDist".to_string()
))
})?.clone();
let key_dist_address = r
.action()
.entry_hash()
.ok_or_else(|| {
wasm_error!(WasmErrorInner::Guest(
"Missing entry hash for VerificationKeyDist".to_string()
))
})?
.clone();
let vf_key_dist: VerificationKeyDist = convert_to_app_entry_type(r)?;
let reference_count = get_key_collections_reference_count(key_dist_address.clone(), &GetOptions::content())?;
let reference_count =
get_key_collections_reference_count(key_dist_address.clone(), &GetOptions::content())?;
out.push(VfKeyResponse {
verification_key_dist: vf_key_dist.into(),
key_dist_address,
Expand Down Expand Up @@ -135,7 +137,7 @@ fn search_keys_with_get_options(
Some(agent_pub_key) => {
let links = get_links(
GetLinksInputBuilder::try_new(agent_pub_key, LinkTypes::AgentToVfKeyDist)?
.get_options(get_options.strategy.clone())
.get_options(get_options.strategy)
.build(),
)?;

Expand All @@ -144,7 +146,8 @@ fn search_keys_with_get_options(
.into_iter()
.flat_map(|l| EntryHash::try_from(l.target).ok())
{
let reference_count = get_key_collections_reference_count(key_dist_address.clone(), &get_options)?;
let reference_count =
get_key_collections_reference_count(key_dist_address.clone(), &get_options)?;

match get(key_dist_address.clone(), GetOptions::latest())? {
Some(r) => {
Expand All @@ -166,9 +169,9 @@ fn search_keys_with_get_options(
Ok(out)
}
None => {
return Err(wasm_error!(WasmErrorInner::Guest(
Err(wasm_error!(WasmErrorInner::Guest(
"No fields on the request to perform a search on".to_string()
)));
)))
}
}
}
Expand Down Expand Up @@ -212,10 +215,12 @@ fn verify_key_not_distributed_by_me(
key_type: &VerificationKeyType,
) -> ExternResult<()> {
// Check that we haven't already distributed this key, that would never be valid and will be checked by our peers.
let has_key = get_my_verification_key_distributions(())?.iter().any(|response| {
response.verification_key_dist.verification_key == vf_key
&& &response.verification_key_dist.key_type == key_type
});
let has_key = get_my_verification_key_distributions(())?
.iter()
.any(|response| {
response.verification_key_dist.verification_key == vf_key
&& &response.verification_key_dist.key_type == key_type
});
if has_key {
return Err(wasm_error!(WasmErrorInner::Guest(
"You have already distributed this key".to_string()
Expand Down
7 changes: 3 additions & 4 deletions dnas/trusted/zomes/integrity/trusted/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use hdi::prelude::*;

///! Conversions that are fiddly to code by hand again and again that should probably be affordances
///! of the SDK rather than being here.
pub fn try_extract_entry_to_app_type<T: TryFrom<SerializedBytes>>(entry: EntryHashed) -> ExternResult<T> {
pub fn try_extract_entry_to_app_type<T: TryFrom<SerializedBytes>>(
entry: EntryHashed,
) -> ExternResult<T> {
match entry.as_app_entry() {
Some(app_entry) => {
match <SerializedBytes as TryInto<T>>::try_into(app_entry.clone().into_sb()) {
Expand Down
13 changes: 7 additions & 6 deletions dnas/trusted/zomes/integrity/trusted/src/key_collection.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use hdi::prelude::*;
use crate::convert::try_extract_entry_to_app_type;
use hdi::prelude::*;

use crate::LinkTypes;
use crate::prelude::VerificationKeyDist;
use crate::LinkTypes;
use crate::UnitEntryTypes;

pub const KEY_COLLECTION_LIMIT: usize = 10;
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn validate_key_collection_to_vf_key_dist_link(

let maybe_vf_key_dist_entry = must_get_entry(vf_key_dist_address)?;

if let Err(_) = try_extract_entry_to_app_type::<VerificationKeyDist>(maybe_vf_key_dist_entry) {
if try_extract_entry_to_app_type::<VerificationKeyDist>(maybe_vf_key_dist_entry).is_err() {
return Ok(ValidateCallbackResult::Invalid(format!(
"The target for {:?} must be a {}",
link_type,
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn validate_vf_key_dist_to_key_collection_link(

let maybe_vf_key_dist_entry = must_get_entry(vf_key_dist_address)?;

if let Err(_) = try_extract_entry_to_app_type::<VerificationKeyDist>(maybe_vf_key_dist_entry) {
if try_extract_entry_to_app_type::<VerificationKeyDist>(maybe_vf_key_dist_entry).is_err() {
return Ok(ValidateCallbackResult::Invalid(format!(
"The base for {:?} must be a {}",
link_type,
Expand All @@ -165,12 +165,13 @@ pub fn validate_vf_key_dist_to_key_collection_link(
if !matches!(key_collection_action.action(), Action::Create(Create {
entry_type: EntryType::App(def),
..
}) if def == &entry_def) {
}) if def == &entry_def)
{
return Ok(ValidateCallbackResult::Invalid(format!(
"The target for {:?} must be a {}",
link_type,
std::any::type_name::<KeyCollection>()
)))
)));
}

//
Expand Down
16 changes: 10 additions & 6 deletions dnas/trusted/zomes/integrity/trusted/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// Conversions that are fiddly to code by hand again and again that should probably be affordances
/// of the SDK rather than being here.
pub(crate) mod convert;
pub(crate) mod key_collection;
pub(crate) mod key_util;
Expand Down Expand Up @@ -149,12 +151,14 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
tag,
..
} => match link_type {
LinkTypes::AgentToVfKeyDist => verification_key_dist::validate_create_agent_to_vf_key_dist_link(
action,
base_address,
target_address,
link_type,
),
LinkTypes::AgentToVfKeyDist => {
verification_key_dist::validate_create_agent_to_vf_key_dist_link(
action,
base_address,
target_address,
link_type,
)
}
LinkTypes::VfKeyDistToAgent => {
verification_key_dist::validate_create_vf_key_dist_to_agent_link(
action,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Distribute public keys to be used for verification.
use crate::convert::try_extract_entry_to_app_type;
use crate::key_util::{check_mini_sign_proof, try_read_mini_sign_vf_key};
use crate::{LinkTypes, UnitEntryTypes};
use chrono::{DateTime, Utc};
use hdi::prelude::*;
use crate::key_util::{check_mini_sign_proof, try_read_mini_sign_vf_key};

pub const VERIFICATION_KEY_NAME_MIN_LENGTH: usize = 3;

Expand Down Expand Up @@ -81,7 +81,10 @@ pub fn validate_create_vf_key_dist(
let activity = {
// Must check from the previous action otherwise this create action will show up and appear as an
// existing distribution of the key we're checking isn't already present.
must_get_agent_activity(create_action.author().clone(), ChainFilter::new(create_action.prev_action().clone()))?
must_get_agent_activity(
create_action.author().clone(),
ChainFilter::new(create_action.prev_action().clone()),
)?
};

let entry_def: AppEntryDef = UnitEntryTypes::VerificationKeyDist.try_into()?;
Expand Down Expand Up @@ -180,7 +183,7 @@ pub fn validate_create_agent_to_vf_key_dist_link(
}
};
let entry = must_get_entry(entry_hash)?;
if let Err(_) = try_extract_entry_to_app_type::<VerificationKeyDist>(entry) {
if try_extract_entry_to_app_type::<VerificationKeyDist>(entry).is_err() {
return Ok(ValidateCallbackResult::Invalid(format!(
"The target for {:?} must be a {}",
link_type,
Expand Down Expand Up @@ -219,7 +222,7 @@ pub fn validate_create_vf_key_dist_to_agent_link(
}
};
let entry = must_get_entry(entry_hash)?;
if let Err(_) = try_extract_entry_to_app_type::<VerificationKeyDist>(entry) {
if try_extract_entry_to_app_type::<VerificationKeyDist>(entry).is_err() {
return Ok(ValidateCallbackResult::Invalid(format!(
"The base for {:?} must be a {}",
link_type,
Expand Down
18 changes: 10 additions & 8 deletions tests/src/trusted/trusted/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export const testAppPath = process.cwd() + "/../workdir/hWOT.happ";

export interface VerificationKeyDist {
verification_key: string;
key_type: { 'MiniSignEd25519': null };
key_type: { MiniSignEd25519: null };
name: string;
expires_at: number;
}

export interface VerificationKeyResponse {
verification_key_dist: VerificationKeyDist;
key_dist_address: number[],
key_dist_address: number[];
reference_count: number;
created_at: number;
}
Expand All @@ -39,16 +39,18 @@ RWS1f3hCs0vfXeaPCLyiQt9NDQ+MzReDNLz+kaw+hK9NV8nb9G7opa7q

export const sampleMiniSignProof = () => {
// The formatting of this must be EXACT, since this content is being signed
return 'some test data\n';
}
return "some test data\n";
};

export function sampleMiniSignProofSignature() {
// Similar with the signature, this must be EXACT. Removing whitespace is permitted but extra whitespace is not.
return Array.from(utf8Encode.encode(`untrusted comment: signature from minisign secret key
return Array.from(
utf8Encode.encode(`untrusted comment: signature from minisign secret key
RUS1f3hCs0vfXb4ExmkOtLWNkqaPkEyzEIRrcmHWyoJuSMUR3U7jx08hri3cr8EYyBNVnH1LOSdjY3Hfk2BQU15jMD25ub5sBAU=
trusted comment: timestamp:1709423483\tfile:test.txt\thashed
Gjpn4nbsrDPysp3Nl63GZO5YWaB0aiJljBlUOQWIYE6tgUL7inOyiYcx5EWb2yOKvwbIjRk3u0ShhgqBIwM7Dg==
`));
`),
);
}

export const distributeVerificationKey = async (
Expand All @@ -63,12 +65,12 @@ export const distributeVerificationKey = async (
payload: {
name: "test",
verification_key: verificationKey,
key_type: {"MiniSignEd25519": null},
key_type: { MiniSignEd25519: null },
proof,
proof_signature: proofSignature,
},
});
}
};

export async function createKeyCollection(
cell: CallableCell,
Expand Down
Loading

0 comments on commit fff2d4d

Please sign in to comment.