Skip to content

Commit f409e55

Browse files
authored
fix(crypto): basic_ibe example: use persistent actor and Motoko core (#234)
Adapts the basic_ibe example so that it works again with ICP Ninja, which recently upgraded to DFX 0.29.1, which ships with a new motoko compiler (0.16.1). The necessary changes for this were * Declaring the actor as `persistent` * Changing the `inboxes` map to the stable mo:core/Map As the examples was touched anyway, the PR switches from the outdated `mo:base` to the new `mo:core` and fixes all compiler issues. The Github workflow sets the `DFX_VERSION` environment variable, so that only this example uses the new DFX version 0.29.1. This also required to change the bind host from `localhost` to `127.0.0.1` as otherwise this new DFX version hits the following error: ``` Error: The replica returned an HTTP Error: Http Error: status 400 Bad Request, content type "text/plain; charset=utf-8", content: error: no_authority details: The request is missing the required authority information (e.g. 'Host' header). ``` The changes were tested in ICP Ninja with the following link: [![](https://icp.ninja/assets/open.svg)](http://icp.ninja/editor?g=https://github.com/dfinity/vetkeys/tree/franzstefan/CRP-2924-basic-ibe/examples/basic_ibe/motoko)
1 parent 7a046d2 commit f409e55

File tree

6 files changed

+32
-50
lines changed

6 files changed

+32
-50
lines changed

.github/workflows/examples-basic-ibe.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ on:
1212
concurrency:
1313
group: ${{ github.workflow }}-${{ github.ref }}
1414
cancel-in-progress: true
15+
env:
16+
DFX_VERSION: 0.29.1
1517
jobs:
1618
examples-basic-ibe-rust-darwin:
1719
runs-on: macos-15

examples/basic_ibe/frontend/src/main.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -226,33 +226,13 @@ async function displayMessages(inbox: Inbox) {
226226
if ("Err" in result) {
227227
alert("Error deleting message: " + result.Err);
228228
} else {
229-
// Remove the message element from the DOM
230-
const messageElement = target.closest(".message");
231-
if (messageElement) {
232-
messageElement.remove();
233-
234-
// If this was the last message, show the "no messages" message
235-
const messagesDiv =
236-
document.getElementById("messages")!;
237-
if (messagesDiv.children.length === 0) {
238-
const noMessagesDiv =
239-
document.createElement("div");
240-
noMessagesDiv.className = "no-messages";
241-
noMessagesDiv.textContent =
242-
"No messages in the inbox.";
243-
messagesDiv.appendChild(noMessagesDiv);
244-
}
245-
}
229+
// Re-load all messages to refresh message indices
230+
await showMessages();
246231
}
247232
} catch (error) {
248233
alert(
249234
"Error deleting message: " + (error as Error).message,
250235
);
251-
} finally {
252-
// Re-enable all delete buttons
253-
deleteButtons.forEach(
254-
(btn) => ((btn as HTMLButtonElement).disabled = false),
255-
);
256236
}
257237
})();
258238
});

examples/basic_ibe/motoko/backend/src/Main.mo

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import Principal "mo:base/Principal";
2-
import Time "mo:base/Time";
3-
import HashMap "mo:base/HashMap";
4-
import Text "mo:base/Text";
5-
import Blob "mo:base/Blob";
6-
import Array "mo:base/Array";
7-
import Buffer "mo:base/Buffer";
8-
import Nat64 "mo:base/Nat64";
9-
import Result "mo:base/Result";
10-
import Int "mo:base/Int";
11-
import Iter "mo:base/Iter";
12-
13-
actor class (keyNameString : Text) {
1+
import Principal "mo:core/Principal";
2+
import Time "mo:core/Time";
3+
import Map "mo:core/Map";
4+
import Text "mo:core/Text";
5+
import Blob "mo:core/Blob";
6+
import Array "mo:core/Array";
7+
import List "mo:core/List";
8+
import Nat64 "mo:core/Nat64";
9+
import Nat "mo:core/Nat";
10+
import Result "mo:core/Result";
11+
import Int "mo:core/Int";
12+
13+
persistent actor class (keyNameString : Text) {
1414
// Types
1515
type Message = {
1616
sender : Principal;
@@ -62,7 +62,7 @@ actor class (keyNameString : Text) {
6262
let DOMAIN_SEPARATOR : Text = "basic_ibe_example_dapp";
6363

6464
// State
65-
var inboxes = HashMap.HashMap<Principal, Inbox>(0, Principal.equal, Principal.hash);
65+
var inboxes = Map.empty<Principal, Inbox>();
6666

6767
// Management canister actor
6868
let vetKdSystemApi : VetKdSystemApi = actor ("aaaaa-aa");
@@ -76,18 +76,18 @@ actor class (keyNameString : Text) {
7676
};
7777

7878
let receiver = request.receiver;
79-
let current_inbox = switch (inboxes.get(receiver)) {
79+
let current_inbox = switch (Map.get(inboxes, Principal.compare, receiver)) {
8080
case (?inbox) { inbox };
8181
case null { { messages = [] } };
8282
};
8383

8484
if (current_inbox.messages.size() >= MAX_MESSAGES_PER_INBOX) {
8585
return #Err("Inbox for " # Principal.toText(receiver) # " is full");
8686
};
87-
88-
let new_messages = Array.append(current_inbox.messages, [message]);
87+
88+
let new_messages = Array.concat(current_inbox.messages, [message]);
8989
let new_inbox : Inbox = { messages = new_messages };
90-
inboxes.put(receiver, new_inbox);
90+
ignore Map.insert(inboxes, Principal.compare, receiver, new_inbox);
9191

9292
#Ok();
9393
};
@@ -132,15 +132,15 @@ actor class (keyNameString : Text) {
132132

133133
// Get the caller's messages
134134
public shared query ({ caller }) func get_my_messages() : async Inbox {
135-
switch (inboxes.get(caller)) {
135+
switch (Map.get(inboxes, Principal.compare, caller)) {
136136
case (?inbox) { inbox };
137137
case null { { messages = [] } };
138138
};
139139
};
140140

141141
// Remove a message by index
142142
public shared ({ caller }) func remove_my_message_by_index(message_index : Nat64) : async Result<(), Text> {
143-
let current_inbox = switch (inboxes.get(caller)) {
143+
let current_inbox = switch (Map.get(inboxes, Principal.compare, caller)) {
144144
case (?inbox) { inbox };
145145
case null { { messages = [] } };
146146
};
@@ -152,17 +152,17 @@ actor class (keyNameString : Text) {
152152

153153
// Create a new array without the specified index
154154
let messages = current_inbox.messages;
155-
let new_messages_buffer = Buffer.Buffer<Message>(0);
155+
let new_messages_list = List.empty<Message>();
156156

157-
for (i in Iter.range(0, messages.size() - 1)) {
157+
for (i in messages.keys()) {
158158
if (i != index) {
159-
new_messages_buffer.add(messages[i]);
159+
List.add(new_messages_list, messages[i]);
160160
};
161161
};
162162

163-
let new_messages = Buffer.toArray(new_messages_buffer);
163+
let new_messages = List.toArray(new_messages_list);
164164
let new_inbox : Inbox = { messages = new_messages };
165-
inboxes.put(caller, new_inbox);
165+
ignore Map.insert(inboxes, Principal.compare, caller, new_inbox);
166166

167167
#Ok();
168168
};

examples/basic_ibe/motoko/dfx.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
},
4343
"networks": {
4444
"local": {
45-
"bind": "localhost:8000",
45+
"bind": "127.0.0.1:8000",
4646
"type": "ephemeral"
4747
}
4848
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[dependencies]
2-
base = "0.14.9"
2+
core = "1.0.0"
33
ic-vetkeys = "0.3.0"

examples/basic_ibe/rust/dfx.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"networks": {
3838
"local": {
39-
"bind": "localhost:8000",
39+
"bind": "127.0.0.1:8000",
4040
"type": "ephemeral"
4141
}
4242
}

0 commit comments

Comments
 (0)