Skip to content

Commit

Permalink
added all_agents anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
matthme committed Jun 20, 2024
1 parent a4623b2 commit 176b135
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
14 changes: 14 additions & 0 deletions dnas/tools/zomes/coordinator/library/src/all_agents.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use hdk::prelude::*;
use library_integrity::*;
#[hdk_extern]
pub fn get_all_agents(_: ()) -> ExternResult<Vec<AgentPubKey>> {
let path = Path::from("all_agents");
let links = get_links(
GetLinksInputBuilder::try_new(path.path_entry_hash()?, LinkTypes::AllAgents)?.build(),
)?;
Ok(links
.into_iter()
.map(|l| l.target.into_agent_pub_key())
.filter_map(|l| l)
.collect())
}
8 changes: 8 additions & 0 deletions dnas/tools/zomes/coordinator/library/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod all_agents;
pub mod all_curators;
pub mod all_developer_collectives;
pub mod contributor_permission;
Expand All @@ -10,6 +11,13 @@ use hdk::prelude::*;
use library_integrity::*;
#[hdk_extern]
pub fn init(_: ()) -> ExternResult<InitCallbackResult> {
let path = Path::from("all_agents");
create_link(
path.path_entry_hash()?,
agent_info()?.agent_initial_pubkey,
LinkTypes::AllAgents,
(),
)?;
Ok(InitCallbackResult::Pass)
}
#[derive(Serialize, Deserialize, Debug)]
Expand Down
51 changes: 51 additions & 0 deletions dnas/tools/zomes/integrity/library/src/all_agents.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use hdi::prelude::*;

pub fn validate_create_link_all_agents(
action: CreateLink,
base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
// Check that base address is pointing away from the all_agents anchor
let base_address_entry_hash = EntryHash::try_from(base_address).map_err(|_| {
wasm_error!(WasmErrorInner::Guest(
"Base address is not an entry hash".into()
))
})?;
let path = Path::from("all_agents");
if path.path_entry_hash()? != base_address_entry_hash {
return Ok(ValidateCallbackResult::Invalid(
"AllAgents link is not pointing away from the all_agents anchor.".into(),
));
}

// Check the entry type for the given action hash
let agent_pub_key =
target_address
.into_agent_pub_key()
.ok_or(wasm_error!(WasmErrorInner::Guest(
"Link target is not an agent public key".to_string()
)))?;

if action.author != agent_pub_key {
return Ok(ValidateCallbackResult::Invalid(
"Links from the all_agents anchor can only point to the agent public key of the agent that creates the link.".into(),
));
}

Ok(ValidateCallbackResult::Valid)
}

/// Rules
/// 1. Cannot be deleted
pub fn validate_delete_link_all_agents(
_action: DeleteLink,
_original_action: CreateLink,
_base: AnyLinkableHash,
_target: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
Ok(ValidateCallbackResult::Invalid(String::from(
"Links from the all_agents anchor cannot be deleted.",
)))
}
23 changes: 23 additions & 0 deletions dnas/tools/zomes/integrity/library/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub mod developer_collective;
pub use developer_collective::*;
pub mod curator;
pub use curator::*;
pub mod all_agents;
pub use all_agents::*;
use hdi::prelude::*;
#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
Expand All @@ -37,6 +39,7 @@ pub enum LinkTypes {
DeveloperCollectiveToCurators,
CuratorToTools,
ToolToCurators,
AllAgents,
AllCurators,
AllDeveloperCollectives,
}
Expand Down Expand Up @@ -322,6 +325,9 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
LinkTypes::ToolToCurators => {
validate_create_link_tool_to_curators(action, base_address, target_address, tag)
}
LinkTypes::AllAgents => {
validate_create_link_all_agents(action, base_address, target_address, tag)
}
LinkTypes::AllCurators => {
validate_create_link_all_curators(action, base_address, target_address, tag)
}
Expand Down Expand Up @@ -431,6 +437,13 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
target_address,
tag,
),
LinkTypes::AllAgents => validate_delete_link_all_agents(
action,
original_action,
base_address,
target_address,
tag,
),
LinkTypes::AllCurators => validate_delete_link_all_curators(
action,
original_action,
Expand Down Expand Up @@ -762,6 +775,9 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
LinkTypes::ToolToCurators => {
validate_create_link_tool_to_curators(action, base_address, target_address, tag)
}
LinkTypes::AllAgents => {
validate_create_link_all_agents(action, base_address, target_address, tag)
}
LinkTypes::AllCurators => {
validate_create_link_all_curators(action, base_address, target_address, tag)
}
Expand Down Expand Up @@ -887,6 +903,13 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
create_link.target_address,
create_link.tag,
),
LinkTypes::AllAgents => validate_delete_link_all_agents(
action,
create_link.clone(),
base_address,
create_link.target_address,
create_link.tag,
),
LinkTypes::AllCurators => validate_delete_link_all_curators(
action,
create_link.clone(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tools-library-dev",
"version": "0.0.4",
"version": "0.0.6",
"private": true,
"workspaces": [
"ui",
Expand Down

0 comments on commit 176b135

Please sign in to comment.