Skip to content

Commit 96b35bf

Browse files
committed
chore: improve ic_message
1 parent b04cc58 commit 96b35bf

31 files changed

+2625
-62
lines changed

Cargo.lock

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dfx.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
{
22
"canisters": {
3+
"ic_message": {
4+
"candid": "src/ic_message/ic_message.did",
5+
"declarations": {
6+
"node_compatibility": true
7+
},
8+
"package": "ic_message",
9+
"optimize": "cycles",
10+
"type": "rust"
11+
},
12+
"ic_message_channel": {
13+
"candid": "src/ic_message_channel/ic_message_channel.did",
14+
"declarations": {
15+
"node_compatibility": true
16+
},
17+
"package": "ic_message_channel",
18+
"optimize": "cycles",
19+
"type": "rust"
20+
},
21+
"ic_message_profile": {
22+
"candid": "src/ic_message_profile/ic_message_profile.did",
23+
"declarations": {
24+
"node_compatibility": true
25+
},
26+
"package": "ic_message_profile",
27+
"optimize": "cycles",
28+
"type": "rust"
29+
},
330
"ic_panda_luckypool": {
431
"candid": "src/ic_panda_luckypool/ic_panda_luckypool.did",
532
"declarations": {

src/ic_message/ic_message.did

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type ChannelInfo = record {
3333
created_by : principal;
3434
canister : principal;
3535
image : text;
36+
message_start : nat32;
3637
latest_message_at : nat32;
3738
latest_message_by : principal;
3839
my_setting : ChannelSetting;
@@ -113,8 +114,10 @@ type StateInfo = record {
113114
profile_canisters : vec principal;
114115
names_total : nat64;
115116
transfer_out_total : nat;
117+
next_block_height : nat64;
116118
users_total : nat64;
117119
price : Price;
120+
next_block_phash : blob;
118121
cose_canisters : vec principal;
119122
incoming_total : nat;
120123
channel_canisters : vec principal;
@@ -134,7 +137,6 @@ type UserInfo = record {
134137
username : opt text;
135138
cose_canister : opt principal;
136139
name : text;
137-
paid : nat64;
138140
image : text;
139141
profile_canister : principal;
140142
};

src/ic_message/src/api_admin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ fn admin_add_canister(kind: types::CanisterKind, id: Principal) -> Result<(), St
3232
s.cose_canisters.push(id);
3333
}
3434
types::CanisterKind::Profile => {
35-
s.cose_canisters.push(id);
35+
s.profile_canisters.push(id);
3636
}
3737
types::CanisterKind::Channel => {
38-
s.cose_canisters.push(id);
38+
s.channel_canisters.push(id);
3939
}
4040
}
4141
Ok(())

src/ic_message/src/api_query.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ fn get_state() -> Result<types::StateInfo, String> {
2626
users_total: store::user::users_total(),
2727
incoming_total: s.incoming_total,
2828
transfer_out_total: s.transfer_out_total,
29+
next_block_height: s.next_block_height,
30+
next_block_phash: s.next_block_phash,
2931
}))
3032
}
3133

src/ic_message/src/api_update.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use crate::{is_authenticated, store, types};
88

99
#[ic_cdk::update(guard = "is_authenticated")]
1010
async fn register_username(username: String) -> Result<UserInfo, String> {
11-
if username.len() > types::MAX_USER_NS_SIZE {
11+
if username.len() > types::MAX_USER_SIZE {
1212
Err("username is too long".to_string())?;
1313
}
14-
15-
validate_key(&username.to_ascii_lowercase())?;
16-
if username.starts_with("-") || username.starts_with("_") {
14+
if username.starts_with("_") {
1715
Err("invalid username".to_string())?;
1816
}
17+
validate_key(&username.to_ascii_lowercase())?;
1918

2019
let caller = ic_cdk::caller();
2120
let now_ms = ic_cdk::api::time() / MILLISECONDS;

src/ic_message/src/store.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,21 @@ pub struct User {
7474
pub image: String,
7575
#[serde(rename = "p")]
7676
pub profile_canister: Principal, // profile canister
77-
#[serde(rename = "cn")]
78-
pub cose_namespace: Option<(Principal, String)>, // namespace in shared COSE service
79-
#[serde(rename = "pa")]
80-
pub paid: u64,
77+
#[serde(rename = "c")]
78+
pub cose_canister: Option<Principal>, // COSE canister
79+
#[serde(rename = "u")]
80+
pub username: Option<String>,
8181
}
8282

8383
impl User {
8484
pub fn into_info(self, id: Principal) -> UserInfo {
85-
let (cose_canister, username) = match self.cose_namespace {
86-
Some((c, n)) => (Some(c), Some(n)),
87-
None => (None, None),
88-
};
89-
9085
UserInfo {
9186
id,
92-
name: self.name.clone(),
93-
image: self.image.clone(),
87+
name: self.name,
88+
image: self.image,
9489
profile_canister: self.profile_canister,
95-
username,
96-
cose_canister,
97-
paid: self.paid,
90+
cose_canister: self.cose_canister,
91+
username: self.username,
9892
}
9993
}
10094
}
@@ -227,16 +221,16 @@ pub mod user {
227221
name,
228222
image: "".to_string(),
229223
profile_canister,
230-
cose_namespace: None,
231-
paid: 0,
224+
cose_canister: None,
225+
username: None,
232226
};
233227
m.insert(caller, user.clone());
234228
user.into_info(caller)
235229
}
236230
}
237231
});
238232

239-
call(
233+
let _: Result<(), String> = call(
240234
info.profile_canister,
241235
"admin_upsert_profile",
242236
(caller, None::<(Principal, u64)>),
@@ -259,7 +253,7 @@ pub mod user {
259253
}
260254
})?;
261255

262-
call(
256+
let _: Result<(), String> = call(
263257
user_profile_canister,
264258
"admin_upsert_profile",
265259
(caller, None::<(Principal, u64)>),
@@ -325,8 +319,10 @@ pub mod user {
325319
let blk = NameBlock {
326320
height: s.next_block_height,
327321
phash: s.next_block_phash,
328-
name: ln.clone(),
322+
name: ln,
329323
user: caller,
324+
from: None,
325+
value: amount,
330326
timestamp: now_ms,
331327
};
332328
let blk = to_cbor_bytes(&blk);
@@ -344,12 +340,11 @@ pub mod user {
344340
cose_canister,
345341
"admin_create_namespace",
346342
(CreateNamespaceInput {
347-
name: ln.clone(),
343+
name: caller.to_text().replace("-", "_"),
348344
visibility: 0,
349345
desc: Some(format!(
350-
"registered by {}, $PANDA block: {}",
351-
caller.to_text(),
352-
blk
346+
"register_username: {}, $PANDA block: {}",
347+
username, blk
353348
)),
354349
max_payload_size: Some(1024),
355350
managers: BTreeSet::from([ic_cdk::id()]),
@@ -364,8 +359,8 @@ pub mod user {
364359
let mut m = r.borrow_mut();
365360
match m.get(&caller) {
366361
Some(mut user) => {
367-
user.paid = amount;
368-
user.cose_namespace = Some((cose_canister, username));
362+
user.cose_canister = Some(cose_canister);
363+
user.username = Some(username);
369364
m.insert(caller, user.clone());
370365
user.into_info(caller)
371366
}
@@ -374,16 +369,16 @@ pub mod user {
374369
name: username.clone(),
375370
image: "".to_string(),
376371
profile_canister,
377-
cose_namespace: Some((cose_canister, username)),
378-
paid: amount,
372+
cose_canister: Some(cose_canister),
373+
username: Some(username),
379374
};
380375
m.insert(caller, user.clone());
381376
user.into_info(caller)
382377
}
383378
}
384379
});
385380

386-
call(
381+
let _: Result<(), String> = call(
387382
info.profile_canister,
388383
"admin_upsert_profile",
389384
(caller, None::<(Principal, u64)>),
@@ -525,8 +520,8 @@ pub mod channel {
525520
name: input.name.clone(),
526521
image: "".to_string(),
527522
profile_canister,
528-
cose_namespace: None,
529-
paid: 0,
523+
cose_canister: None,
524+
username: None,
530525
};
531526
m.insert(caller, user.clone());
532527
(profile_canister, true)
@@ -535,7 +530,7 @@ pub mod channel {
535530
});
536531

537532
if is_new {
538-
call(
533+
let _: Result<(), String> = call(
539534
user_profile_canister,
540535
"admin_upsert_profile",
541536
(caller, None::<(Principal, u64)>),
@@ -565,7 +560,7 @@ pub mod channel {
565560
call(channel_canister, "admin_create_channel", (input,), 0).await?;
566561
let res = res?;
567562

568-
call(
563+
let _: Result<(), String> = call(
569564
user_profile_canister,
570565
"admin_upsert_profile",
571566
(caller, Some((res.canister, res.id))),
@@ -576,16 +571,16 @@ pub mod channel {
576571
}
577572

578573
pub async fn save_channel_kek(caller: Principal, input: ChannelKEKInput) -> Result<(), String> {
579-
let user_cose = USER_STORE
580-
.with(|r| r.borrow().get(&caller).map(|u| u.cose_namespace))
574+
let cose_canister = USER_STORE
575+
.with(|r| r.borrow().get(&caller).map(|u| u.cose_canister))
581576
.ok_or_else(|| "user not found".to_string())?;
582-
let user_cose = user_cose.ok_or_else(|| "user has no COSE namespace".to_string())?;
577+
let cose_canister = cose_canister.ok_or_else(|| "user has no COSE service".to_string())?;
583578
let res: Result<CreateSettingOutput, String> = call(
584-
user_cose.0,
579+
cose_canister,
585580
"setting_create",
586581
(
587582
SettingPath {
588-
ns: user_cose.1.to_ascii_lowercase(),
583+
ns: caller.to_text().replace("-", "_"),
589584
user_owned: false,
590585
subject: Some(caller),
591586
key: channel_kek_key(&input.canister, input.id),

src/ic_message/src/types.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use candid::{CandidType, Principal};
22
use serde::{Deserialize, Serialize};
3+
use serde_bytes::ByteArray;
34
use std::collections::BTreeSet;
45

56
pub const TOKEN_1: u64 = 100_000_000;
67
pub const TOKEN_FEE: u64 = 10_000; // 0.0001 token
78
pub const MIN_NAME_PRICE: u64 = TOKEN_1;
89
pub const MIN_CHANNEL_PRICE: u64 = TOKEN_1;
910
pub const MAX_USER_NAME_SIZE: usize = 32;
10-
pub const MAX_USER_NS_SIZE: usize = 16;
11+
pub const MAX_USER_SIZE: usize = 20;
1112

1213
#[derive(CandidType, Clone, Debug, Deserialize, Serialize)]
1314
pub struct StateInfo {
@@ -21,6 +22,8 @@ pub struct StateInfo {
2122
pub users_total: u64,
2223
pub incoming_total: u128,
2324
pub transfer_out_total: u128,
25+
pub next_block_height: u64,
26+
pub next_block_phash: ByteArray<32>,
2427
}
2528

2629
#[derive(CandidType, Clone, Debug, Default, Deserialize, Serialize)]

0 commit comments

Comments
 (0)