Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor authenticated OP helper to allow returning proper results #2155

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/internet_identity/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn verify_tentative_device(
user_verification_code,
)
})
.unwrap_or_else(|err| err)
}

#[update]
Expand All @@ -111,6 +112,7 @@ fn add(anchor_number: AnchorNumber, device_data: DeviceData) {
authenticated_anchor_operation(anchor_number, |anchor| {
Ok(((), anchor_management::add(anchor, device_data)))
})
.unwrap_or_else(|err| err)
}

#[update]
Expand All @@ -122,6 +124,7 @@ fn update(anchor_number: AnchorNumber, device_key: DeviceKey, device_data: Devic
anchor_management::update(anchor, device_key, device_data),
))
})
.unwrap_or_else(|err| err)
}

#[update]
Expand All @@ -133,6 +136,7 @@ fn replace(anchor_number: AnchorNumber, device_key: DeviceKey, device_data: Devi
anchor_management::replace(anchor_number, anchor, device_key, device_data),
))
})
.unwrap_or_else(|err| err)
}

#[update]
Expand All @@ -144,6 +148,7 @@ fn remove(anchor_number: AnchorNumber, device_key: DeviceKey) {
anchor_management::remove(anchor_number, anchor, device_key),
))
})
.unwrap_or_else(|err| err)
}

/// Returns all devices of the anchor (authentication and recovery) but no information about device registrations.
Expand Down Expand Up @@ -457,16 +462,13 @@ fn authenticate_and_record_activity(anchor_number: AnchorNumber) -> Option<IIDom
/// the necessary bookkeeping for anchor operations.
///
/// * anchor_number: indicates the anchor to be provided op should be called on
/// * op: Function that modifies an anchor and returns a value `R` wrapped in a [Result] indicating
/// * op: Function that modifies an anchor and returns a [Result] indicating
/// success or failure which determines whether additional bookkeeping (on success) is required.
/// On success, the function must also return an [Operation] which is used for archiving purposes.
/// The type `R` is usually bound to an interface type specified in the candid file. This type
/// is either unit or a variant unifying success and error cases (which is why the [Result] has
/// `R` in both success and error positions).
fn authenticated_anchor_operation<R>(
fn authenticated_anchor_operation<R, E>(
anchor_number: AnchorNumber,
op: impl FnOnce(&mut Anchor) -> Result<(R, Operation), R>,
) -> R {
op: impl FnOnce(&mut Anchor) -> Result<(R, Operation), E>,
) -> Result<R, E> {
let Ok((mut anchor, device_key)) = check_authentication(anchor_number) else {
trap(&format!("{} could not be authenticated.", caller()));
};
Expand All @@ -482,9 +484,9 @@ fn authenticated_anchor_operation<R>(
match result {
Ok((ret, operation)) => {
post_operation_bookkeeping(anchor_number, operation);
ret
Ok(ret)
}
Err(err) => err,
Err(err) => Err(err),
}
}

Expand Down Expand Up @@ -602,8 +604,7 @@ mod v2_api {
(),
anchor_management::identity_metadata_replace(anchor, metadata),
))
});
Ok(())
})
}
}

Expand Down