Skip to content

Commit

Permalink
Merge pull request #43 from holonauts/validation-2
Browse files Browse the repository at this point in the history
validate authors, wallet, evaluators and amount range
  • Loading branch information
dauphin3 authored Mar 1, 2024
2 parents f296ff4 + 70ed738 commit 362cedf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
16 changes: 10 additions & 6 deletions dnas/grant_pools/zomes/integrity/grants/src/agent_to_evm_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ use alloy_primitives::Address;
use hdi::prelude::*;

pub fn validate_create_link_agent_to_evm_wallet(
_action: CreateLink,
action: CreateLink,
base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
let author_pubkey = base_address
.into_agent_pub_key()
.ok_or(wasm_error!("Base address must be an AgentPubKey"))?;
if action.author != author_pubkey {
return Ok(ValidateCallbackResult::Invalid(
"Cannot add wallet for another agent besides yourself".to_string(),
));
}
let evm_address = String::from_utf8(target_address.into_inner()).map_err(|e| {
wasm_error!(WasmErrorInner::Guest(format!(
"Error converting target address to string: {:?}",
e
)))
})?;
if !AgentPubKey::try_from(base_address).is_ok() {
return Ok(ValidateCallbackResult::Invalid(
"Can only link wallet from an AgentPubKey".to_string(),
));
}

if !Address::parse_checksummed(&evm_address, None).is_ok() {
return Ok(ValidateCallbackResult::Invalid(
"Can only link to valid evm address".to_string(),
Expand Down
11 changes: 11 additions & 0 deletions dnas/grant_pools/zomes/integrity/grants/src/grant_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ pub fn validate_create_grant_pool(
"Evaluators cannot be empty".to_string(),
));
}
if grant_pool.amount_range.min > grant_pool.amount_range.min {
return Ok(ValidateCallbackResult::Invalid(
"Amount range max must be greater than min".to_string(),
));
}
if grant_pool.evaluators.len() == 0 {
return Ok(ValidateCallbackResult::Invalid(
"Must have at least one evaluator".to_string(),
));
}

let record = must_get_valid_record(grant_pool.time_period.clone())?;
let _time_period: crate::TimePeriod = record
.entry()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ pub fn validate_create_link_grant_pool_to_application(
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
let action_hash = base_address
.into_action_hash()
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"No action hash associated with link"
))))?;
let record = must_get_valid_record(action_hash)?;
let base_action_hash =
base_address
.into_action_hash()
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"No action hash associated with link"
))))?;
let record = must_get_valid_record(base_action_hash.clone())?;
let _grant_pool: crate::GrantPool = record
.entry()
.to_app_option()
Expand All @@ -25,13 +26,18 @@ pub fn validate_create_link_grant_pool_to_application(
"No action hash associated with link"
))))?;
let record = must_get_valid_record(action_hash)?;
let _application: crate::Application = record
let application: crate::Application = record
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"Linked action must reference an entry"
))))?;
if application.grant_pool != base_action_hash {
return Ok(ValidateCallbackResult::Invalid(
"Base address must be the Grant Pool action hash".to_string(),
));
}
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_link_grant_pool_to_applications(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use alloy_primitives::U256;
use hdi::prelude::*;
pub fn validate_create_link_sponsor_to_grant_pool(
_action: CreateLink,
_base_address: AnyLinkableHash,
action: CreateLink,
base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
let author_pubkey = base_address
.into_agent_pub_key()
.ok_or(wasm_error!("Base address must be an AgentPubKey"))?;
if action.author != author_pubkey {
return Ok(ValidateCallbackResult::Invalid(
"Author not valid, can only add yourself as a sponsor".to_string(),
));
}
if U256::from_le_slice(&tag.into_inner()) == U256::from(0) {
return Ok(ValidateCallbackResult::Invalid(
"amount cannot be zero".to_string(),
Expand Down

0 comments on commit 362cedf

Please sign in to comment.