Skip to content

Commit 606b484

Browse files
authored
feat: Implement lookup_caller_identity_by_recovery_phrase (#3497)
first
1 parent a03f9a8 commit 606b484

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

src/internet_identity/src/anchor_management.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use crate::archive::{archive_operation, device_diff};
22
use crate::openid::{OpenIdCredential, OpenIdCredentialKey};
33
use crate::storage::anchor::{Anchor, AnchorError, Device};
44
use crate::{state, stats::activity_stats};
5+
use candid::Principal;
56
use ic_cdk::api::time;
67
use ic_cdk::{caller, trap};
78
use internet_identity_interface::archive::types::{DeviceDataWithoutAlias, Operation};
89
use internet_identity_interface::internet_identity::types::openid::OpenIdCredentialData;
910
use internet_identity_interface::internet_identity::types::{
1011
AnchorNumber, AuthorizationKey, CredentialId, DeviceData, DeviceKey, DeviceKeyWithAnchor,
11-
DeviceWithUsage, IdentityAnchorInfo, IdentityPropertiesReplace, MetadataEntry,
12+
DeviceWithUsage, IdentityAnchorInfo, IdentityNumber, IdentityPropertiesReplace, MetadataEntry,
1213
};
1314
use state::storage_borrow;
1415
use std::collections::HashMap;
@@ -237,6 +238,11 @@ pub fn lookup_device_key_with_credential_id(
237238
})
238239
}
239240

241+
/// Lookup `IdentityNumber` for the given caller `Principal` used as recovery phrase.
242+
pub fn lookup_caller_identity_by_recovery_phrase(caller: Principal) -> Option<IdentityNumber> {
243+
storage_borrow(|storage| storage.lookup_anchor_with_recovery_phrase_principal(caller))
244+
}
245+
240246
/// Set `name` of the given anchor.
241247
/// Return an error if the `name` to be updated is too long.
242248
pub fn set_name(anchor: &mut Anchor, name: Option<String>) -> Result<Operation, AnchorError> {

src/internet_identity/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,8 @@ fn get_anchor_credentials(anchor_number: AnchorNumber) -> AnchorCredentials {
321321

322322
#[query]
323323
fn lookup_caller_identity_by_recovery_phrase() -> Option<IdentityNumber> {
324-
// TODO: Implement this function after the `lookup_anchor_with_recovery_phrase_principal_memory`
325-
// TODO: index is initialized with existing user data.
326-
None
324+
let caller = caller();
325+
anchor_management::lookup_caller_identity_by_recovery_phrase(caller)
327326
}
328327

329328
#[query]

src/internet_identity/src/storage.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,15 @@ impl<M: Memory + Clone> Storage<M> {
702702
anchor_numbers.first().copied()
703703
}
704704

705-
pub(crate) fn sync_anchor_with_recovery_phrase_principal_index(
705+
pub fn lookup_anchor_with_recovery_phrase_principal(
706+
&self,
707+
key: Principal,
708+
) -> Option<AnchorNumber> {
709+
self.lookup_anchor_with_recovery_phrase_principal_memory
710+
.get(&key)
711+
}
712+
713+
fn sync_anchor_with_recovery_phrase_principal_index(
706714
&mut self,
707715
anchor_number: AnchorNumber,
708716
previous_devices: &[Device],

src/internet_identity/tests/integration/upgrade.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,37 @@ fn test_sync_anchor_indices_migration() {
418418

419419
assert_eq!(count_recovery_phrases, NUM_ANCHORS as u64);
420420
}
421+
422+
// smoke test
423+
for (label, sender, expected_anchor_number) in [
424+
(
425+
"Anonymous user should not get any anchor number",
426+
Principal::anonymous(),
427+
None,
428+
),
429+
(
430+
"User with pub-key-0 should get anchor number 10000",
431+
Principal::self_authenticating("pub-key-0"),
432+
Some(10_000),
433+
),
434+
] {
435+
let payload = candid::encode_one(()).unwrap();
436+
437+
let data = env
438+
.update_call(
439+
canister_id,
440+
sender,
441+
"lookup_caller_identity_by_recovery_phrase",
442+
payload,
443+
)
444+
.unwrap();
445+
446+
let observed_anchor_number: Option<u64> = candid::decode_one(&data).unwrap();
447+
448+
assert_eq!(
449+
observed_anchor_number, expected_anchor_number,
450+
"failed for {}",
451+
label
452+
);
453+
}
421454
}

0 commit comments

Comments
 (0)