Skip to content

Commit

Permalink
feat: add name blocks to ic-message
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Aug 29, 2024
1 parent 505bd34 commit b04cc58
Show file tree
Hide file tree
Showing 17 changed files with 382 additions and 96 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ ic-cdk-timers = "0.8"
ic-stable-structures = "0.6"
icrc-ledger-types = "0.1"
ic_cose_types = "0.3"
ic-certification = "2.6"
getrandom = { version = "0.2", features = ["custom"] }
rand = { version = "0.8", features = ["getrandom"] }
1 change: 1 addition & 0 deletions src/ic_message/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ ic-cdk = { workspace = true }
ic-stable-structures = { workspace = true }
icrc-ledger-types = { workspace = true }
ic_cose_types = { workspace = true }
ic-certification = { workspace = true }
33 changes: 33 additions & 0 deletions src/ic_message/ic_message.did
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
type ArchivedBlocks = record {
args : vec GetBlocksRequest;
callback : func (vec GetBlocksRequest) -> (GetBlocksResult) query;
};
type BlockWithId = record { id : nat; block : ICRC3Value };
type CanisterKind = variant { Cose; Channel; Profile };
type CanisterStatusResponse = record {
status : CanisterStatusType;
Expand Down Expand Up @@ -27,6 +32,7 @@ type ChannelInfo = record {
created_at : nat64;
created_by : principal;
canister : principal;
image : text;
latest_message_at : nat32;
latest_message_by : principal;
my_setting : ChannelSetting;
Expand All @@ -46,6 +52,7 @@ type CreateChannelInput = record {
paid : nat64;
description : text;
created_by : principal;
image : text;
};
type DefiniteCanisterSettings = record {
freezing_threshold : nat;
Expand All @@ -56,6 +63,27 @@ type DefiniteCanisterSettings = record {
memory_allocation : nat;
compute_allocation : nat;
};
type GetArchivesArgs = record { from : opt principal };
type GetBlocksRequest = record { start : nat; length : nat };
type GetBlocksResult = record {
log_length : nat;
blocks : vec BlockWithId;
archived_blocks : vec ArchivedBlocks;
};
type ICRC3ArchiveInfo = record {
end : nat;
canister_id : principal;
start : nat;
};
type ICRC3DataCertificate = record { certificate : blob; hash_tree : blob };
type ICRC3Value = variant {
Int : int;
Map : vec record { text; ICRC3Value };
Nat : nat;
Blob : blob;
Text : text;
Array : vec ICRC3Value;
};
type InitArgs = record { managers : vec principal; name : text };
type LogVisibility = variant { controllers; public };
type Price = record {
Expand Down Expand Up @@ -91,6 +119,7 @@ type StateInfo = record {
incoming_total : nat;
channel_canisters : vec principal;
};
type SupportedBlockType = record { url : text; block_type : text };
type UpdatePriceInput = record {
name_l1 : opt nat64;
name_l2 : opt nat64;
Expand Down Expand Up @@ -121,6 +150,10 @@ service : (opt ChainArgs) -> {
get_canister_status : () -> (Result_4) query;
get_state : () -> (Result_5) query;
get_user : (opt principal) -> (Result_3) query;
icrc3_get_archives : (GetArchivesArgs) -> (vec ICRC3ArchiveInfo) query;
icrc3_get_blocks : (vec GetBlocksRequest) -> (GetBlocksResult) query;
icrc3_get_tip_certificate : () -> (opt ICRC3DataCertificate) query;
icrc3_supported_block_types : () -> (vec SupportedBlockType) query;
register_username : (text) -> (Result_3);
save_channel_kek : (ChannelKEKInput) -> (Result);
search_username : (text) -> (Result_6) query;
Expand Down
36 changes: 35 additions & 1 deletion src/ic_message/src/api_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ use candid::Principal;
use ic_cdk::api::management_canister::main::{
canister_status, CanisterIdRecord, CanisterStatusResponse,
};
use ic_cose_types::format_error;
use ic_cose_types::{format_error, to_cbor_bytes};
use ic_message_types::profile::UserInfo;
use icrc_ledger_types::icrc3::{
archive::{GetArchivesArgs, GetArchivesResult},
blocks::{GetBlocksRequest, GetBlocksResult, ICRC3DataCertificate, SupportedBlockType},
};
use serde_bytes::ByteBuf;
use std::collections::BTreeSet;

use crate::{is_authenticated, store, types};
Expand Down Expand Up @@ -36,6 +41,35 @@ async fn get_canister_status() -> Result<CanisterStatusResponse, String> {
Ok(res)
}

#[ic_cdk::query]
pub fn icrc3_supported_block_types() -> Vec<SupportedBlockType> {
vec![SupportedBlockType {
block_type: "ic-message".to_string(),
url: "https://github.com/ldclabs/ic-panda/tree/main/src/ic_message".to_string(),
}]
}

#[ic_cdk::query]
pub fn icrc3_get_tip_certificate() -> Option<ICRC3DataCertificate> {
let certificate = ByteBuf::from(ic_cdk::api::data_certificate()?);
let hash_tree = store::state::with(|s| s.hash_tree());
let buf = to_cbor_bytes(&hash_tree);
Some(ICRC3DataCertificate {
certificate,
hash_tree: ByteBuf::from(buf),
})
}

#[ic_cdk::query]
pub fn icrc3_get_archives(_args: GetArchivesArgs) -> GetArchivesResult {
vec![] // TODO: implement
}

#[ic_cdk::query]
pub fn icrc3_get_blocks(args: Vec<GetBlocksRequest>) -> GetBlocksResult {
store::user::get_blocks(args)
}

#[ic_cdk::query]
fn search_username(prefix: String) -> Result<Vec<String>, String> {
Ok(store::user::search_username(prefix))
Expand Down
5 changes: 3 additions & 2 deletions src/ic_message/src/api_update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ic_cose_types::validate_key;
use ic_cose_types::{validate_key, MILLISECONDS};
use ic_message_types::{
channel::{ChannelInfo, ChannelKEKInput, CreateChannelInput},
profile::UserInfo,
Expand All @@ -18,7 +18,8 @@ async fn register_username(username: String) -> Result<UserInfo, String> {
}

let caller = ic_cdk::caller();
store::user::register_username(caller, username).await
let now_ms = ic_cdk::api::time() / MILLISECONDS;
store::user::register_username(caller, username, now_ms).await
}

#[ic_cdk::update(guard = "is_authenticated")]
Expand Down
4 changes: 4 additions & 0 deletions src/ic_message/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use ic_message_types::{
channel::{ChannelInfo, CreateChannelInput},
profile::UserInfo,
};
use icrc_ledger_types::icrc3::{
archive::{GetArchivesArgs, GetArchivesResult},
blocks::{GetBlocksRequest, GetBlocksResult, ICRC3DataCertificate, SupportedBlockType},
};
use icrc_ledger_types::{
icrc1::{
account::Account,
Expand Down
Loading

0 comments on commit b04cc58

Please sign in to comment.