-
Notifications
You must be signed in to change notification settings - Fork 6
feat(rust): custom_value_endpoints form for export_encrypted_maps_canister! #424
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bb57cf0
feat(rust): custom_value_endpoints form for export_encrypted_maps_can…
marc0olo ad95a19
docs: surface the canister generator in the crate + reference READMEs
marc0olo 4119115
docs+fix(rust): address Copilot review on the em canister macro
marc0olo 5dd315f
docs(rust): address Martin's review comments on the em canister macro
marc0olo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
backend/rs/canisters/ic_vetkeys_encrypted_maps_custom_canister/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| [package] | ||
| name = "ic-vetkeys-encrypted-maps-custom-canister" | ||
| authors = ["DFINITY Stiftung"] | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| license = "Apache-2.0" | ||
| description = "Reference canister for the export_encrypted_maps_canister! custom_value_endpoints form" | ||
| repository = "https://github.com/dfinity/vetkeys" | ||
| rust-version = "1.85.0" | ||
|
|
||
| [lib] | ||
| path = "src/lib.rs" | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| candid = "0.10.2" | ||
| ic-cdk = { workspace = true } | ||
| ic-cdk-management-canister = { workspace = true } | ||
| ic-dummy-getrandom-for-wasm = "0.1.0" | ||
| ic-stable-structures = { workspace = true } | ||
| ic-vetkeys = { workspace = true } | ||
| serde = "1.0.217" |
18 changes: 18 additions & 0 deletions
18
backend/rs/canisters/ic_vetkeys_encrypted_maps_custom_canister/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # ic-vetkeys-encrypted-maps-custom-canister | ||
|
|
||
| A reference canister for the `custom_value_endpoints` form of the | ||
| [`export_encrypted_maps_canister!`](https://docs.rs/ic-vetkeys/latest/ic_vetkeys/macro.export_encrypted_maps_canister.html) | ||
| macro. | ||
|
|
||
| Unlike [`ic-vetkeys-encrypted-maps-canister`](../ic_vetkeys_encrypted_maps_canister), | ||
| which exposes the full EncryptedMaps interface, this canister generates only the | ||
| control-plane endpoints (vetKD keys, access control, map-name enumeration) plus | ||
| the state, lifecycle hooks, and the `with_encrypted_maps`/`with_encrypted_maps_mut` | ||
| accessors, and provides its own value read/write endpoints. | ||
|
|
||
| This is the pattern for dapps that keep state linked to each encrypted value | ||
| (e.g. a metadata row per entry) and therefore must own the value endpoints to | ||
| keep the two stores consistent — see `password_manager_with_metadata` in | ||
| [dfinity/examples](https://github.com/dfinity/examples). The endpoints here are | ||
| kept minimal (a plain insert/get against the accessor) to exercise the macro | ||
| form; they are not a full metadata implementation. |
77 changes: 77 additions & 0 deletions
77
backend/rs/canisters/ic_vetkeys_encrypted_maps_custom_canister/src/lib.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Reference canister for the `custom_value_endpoints` form of | ||
| // `export_encrypted_maps_canister!`. It generates the control-plane endpoints | ||
| // plus the state, lifecycle hooks, and the `with_encrypted_maps`/`_mut` | ||
| // accessors, but NOT the value read/write endpoints — the canister exposes its | ||
| // own. This is the pattern an app uses when it keeps state linked to each value | ||
| // (e.g. a metadata row per entry) and must own the value endpoints to keep that | ||
| // state consistent. | ||
| // | ||
| // This minimal example just re-implements a plain insert/get against the | ||
| // accessor to exercise the surface; a full linked-metadata example lives in | ||
| // `password_manager_with_metadata` in dfinity/examples. | ||
| use candid::Principal; | ||
| use ic_stable_structures::memory_manager::{MemoryId, MemoryManager, VirtualMemory}; | ||
| use ic_stable_structures::storable::Blob; | ||
| use ic_stable_structures::DefaultMemoryImpl; | ||
| use ic_vetkeys::types::{ByteBuf, EncryptedMapValue}; | ||
| use std::cell::RefCell; | ||
|
|
||
| type Memory = VirtualMemory<DefaultMemoryImpl>; | ||
|
|
||
| thread_local! { | ||
| static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> = | ||
| RefCell::new(MemoryManager::init(DefaultMemoryImpl::default())); | ||
| } | ||
|
|
||
| fn memory(id: u8) -> Memory { | ||
| MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(id))) | ||
| } | ||
|
|
||
| // Control plane + state + lifecycle + accessors; no value endpoints. | ||
| ic_vetkeys::export_encrypted_maps_canister!( | ||
| "encrypted_maps_custom_dapp", | ||
| [memory(0), memory(1), memory(2), memory(3)], | ||
| custom_value_endpoints, | ||
| ); | ||
|
|
||
| fn to_blob(bytes: ByteBuf) -> Result<Blob<32>, String> { | ||
| Blob::try_from(bytes.as_ref()).map_err(|_| "too large input".to_string()) | ||
| } | ||
|
|
||
| // A canister-owned value write, wrapping the library's own via the accessor. | ||
| // This is where linked side-state (metadata, counters, …) would be maintained | ||
| // in the same call — kept minimal here. | ||
| #[ic_cdk::update] | ||
| fn insert_encrypted_value_custom( | ||
| map_owner: Principal, | ||
| map_name: ByteBuf, | ||
| map_key: ByteBuf, | ||
| value: EncryptedMapValue, | ||
| ) -> Result<Option<EncryptedMapValue>, String> { | ||
| with_encrypted_maps_mut(|encrypted_maps| { | ||
| encrypted_maps.insert_encrypted_value( | ||
| ic_cdk::api::msg_caller(), | ||
| (map_owner, to_blob(map_name)?), | ||
| to_blob(map_key)?, | ||
| value, | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| // A canister-owned value read via the shared-reference accessor. | ||
| #[ic_cdk::query] | ||
| fn get_encrypted_value_custom( | ||
| map_owner: Principal, | ||
| map_name: ByteBuf, | ||
| map_key: ByteBuf, | ||
| ) -> Result<Option<EncryptedMapValue>, String> { | ||
| with_encrypted_maps(|encrypted_maps| { | ||
| encrypted_maps.get_encrypted_value( | ||
| ic_cdk::api::msg_caller(), | ||
| (map_owner, to_blob(map_name)?), | ||
| to_blob(map_key)?, | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| ic_cdk::export_candid!(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.