Skip to content

Commit

Permalink
All working
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Oct 16, 2024
1 parent 7428d47 commit 0f90564
Show file tree
Hide file tree
Showing 11 changed files with 478 additions and 196 deletions.
28 changes: 14 additions & 14 deletions crates/coordinator/src/link_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ fn linking_agents_path() -> Path {
Path::from("linking_agents")
}

fn secret_from_passnumber(passnumber: Vec<u8>) -> CapSecret {
fn secret_from_passcode(passcode: Vec<u8>) -> CapSecret {
let mut secret: CapSecretBytes = [0; CAP_SECRET_BYTES];

for i in 0..passnumber.len() {
secret[i] = passnumber[i];
for i in 0..passcode.len() {
secret[i] = passcode[i];
}

for i in 0..(CAP_SECRET_BYTES - passnumber.len()) {
for i in passcode.len()..(CAP_SECRET_BYTES - passcode.len()) {
secret[i] = 0;
}

CapSecret::from(secret)
}

#[hdk_extern]
pub fn prepare_link_agent(passnumber: Vec<u8>) -> ExternResult<()> {
pub fn prepare_link_agent(passcode: Vec<u8>) -> ExternResult<()> {
let mut functions = BTreeSet::new();
functions.insert((
zome_info()?.name,
FunctionName("receive_request_link_agent".into()),
));
let access = CapAccess::Transferable {
secret: secret_from_passnumber(passnumber),
secret: secret_from_passcode(passcode),
};
let cap_grant_entry: CapGrantEntry = CapGrantEntry::new(
String::from("link-agents"), // A string by which to later query for saved grants.
Expand Down Expand Up @@ -148,18 +148,18 @@ fn delete_link_relaxed(address: ActionHash) -> ExternResult<()> {
#[derive(Serialize, Deserialize, Debug)]
pub struct RequestLinkAgentInput {
recipient: AgentPubKey,
recipient_passnumber: Vec<u8>,
requestor_passnumber: Vec<u8>,
recipient_passcode: Vec<u8>,
requestor_passcode: Vec<u8>,
}

#[hdk_extern]
pub fn request_link_agent_message(input: RequestLinkAgentInput) -> ExternResult<()> {
pub fn request_link_agent(input: RequestLinkAgentInput) -> ExternResult<()> {
let response = call_remote(
input.recipient,
zome_info()?.name,
"receive_request_link_agent".into(),
Some(secret_from_passnumber(input.recipient_passnumber)),
input.requestor_passnumber,
Some(secret_from_passcode(input.recipient_passcode)),
input.requestor_passcode,
)?;

match response {
Expand All @@ -171,13 +171,13 @@ pub fn request_link_agent_message(input: RequestLinkAgentInput) -> ExternResult<
#[derive(Serialize, Deserialize, SerializedBytes, Debug, Clone)]
pub struct RequestLinkAgentSignal {
from: AgentPubKey,
requestor_passnumber: Vec<u8>,
requestor_passcode: Vec<u8>,
}

const TTL_LIVE_AGENTS_CAP_GRANTS: i64 = 1000 * 1000 * 60; // 1 minute

#[hdk_extern]
pub fn receive_request_link_agent(requestor_passnumber: Vec<u8>) -> ExternResult<()> {
pub fn receive_request_link_agent(requestor_passcode: Vec<u8>) -> ExternResult<()> {
let link_agents_cap_grants = query_link_agents_cap_grants()?;
let now = sys_time()?;

Expand All @@ -196,7 +196,7 @@ pub fn receive_request_link_agent(requestor_passnumber: Vec<u8>) -> ExternResult

let request_link_agent_signal = RequestLinkAgentSignal {
from: call_info.provenance,
requestor_passnumber,
requestor_passcode,
};

emit_signal(request_link_agent_signal)?;
Expand Down
50 changes: 49 additions & 1 deletion crates/integrity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,55 @@ pub fn validate(op: Op) -> ExternResult<ValidateCallbackResult> {
},
_ => Ok(ValidateCallbackResult::Valid),
},
FlatOp::RegisterDelete(delete_entry) => validate_delete_profile(delete_entry.action),
FlatOp::RegisterDelete(delete_entry) => {
let action = delete_entry.action;
let original_action_hash = action.deletes_address.clone();
let original_record = must_get_valid_record(original_action_hash)?;
let original_action = original_record.action().clone();
let original_action = match original_action {
Action::Create(create) => EntryCreationAction::Create(create),
Action::Update(update) => EntryCreationAction::Update(update),
_ => {
return Ok(ValidateCallbackResult::Invalid(
"Original action for a delete must be a Create or Update action"
.to_string(),
));
}
};
let app_entry_type = match original_action.entry_type() {
EntryType::App(app_entry_type) => app_entry_type,
_ => {
return Ok(ValidateCallbackResult::Valid);
}
};
let entry = match original_record.entry().as_option() {
Some(entry) => entry,
None => {
return Ok(ValidateCallbackResult::Invalid(
"Original record for a delete must contain an entry".to_string(),
));
}
};
let original_app_entry = match EntryTypes::deserialize_from_type(
app_entry_type.zome_index,
app_entry_type.entry_index,
entry,
)? {
Some(app_entry) => app_entry,
None => {
return Ok(ValidateCallbackResult::Invalid(
"Original app entry must be one of the defined entry types for this zome"
.to_string(),
));
}
};
match original_app_entry {
EntryTypes::Profile(_original_profile) => validate_delete_profile(action),
EntryTypes::ProfileClaim(_original_profile_claim) => {
validate_delete_profile_claim(action)
}
}
}
FlatOp::RegisterCreateLink {
link_type,
base_address,
Expand Down
19 changes: 18 additions & 1 deletion ui/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@

render() {
if (!this.loaded) return html`<span>Loading...</span>`;
if (this.linkAgent)
return html`
<profiles-context .store=${this.store}>
<link-agent-recipient
style="flex: 1"
@agent-linked=${() => {
this.linkAgent = false;
this.requestUpdate();
}}
>
</link-agent-recipient>
</profiles-context>
`;
return html`
<profiles-context .store=${this.store}>
<profile-prompt
Expand Down Expand Up @@ -119,8 +132,12 @@
.agentPubKey=${this.store.client.client.myPubKey}
>
</agent-mention>
<sl-button @click=${() => {
this.linkAgent = true;
this.requestUpdate();
}}>Link Device
</sl-button>
<link-agent-recipient> </link-agent-recipient>
</profile-prompt>
</profiles-context>
`;
Expand Down
6 changes: 4 additions & 2 deletions ui/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export interface ProfilesConfig {
avatarMode: 'identicon' | 'avatar-required' | 'avatar-optional';
additionalFields: FieldConfig[];
minNicknameLength: number;
passnumberLength: number;
linkDevicePasscodeLength: number;
}

export const defaultConfig: ProfilesConfig = {
avatarMode: 'avatar-optional',
additionalFields: [],
minNicknameLength: 3,
passnumberLength: 6,
linkDevicePasscodeLength: 4,
};

export const TTL_CAP_GRANT = 60 * 1000; // 1 minute
Loading

0 comments on commit 0f90564

Please sign in to comment.