This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
forked from dfinity/ic-websocket-poc
/
canister_methods.rs
110 lines (93 loc) · 2.87 KB
/
canister_methods.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use candid::CandidType;
use candid::Decode;
use ic_agent::{
agent::http_transport::ReqwestHttpReplicaV2Transport, export::Principal,
identity::BasicIdentity, Agent,
};
use serde::{Deserialize, Serialize};
use crate::GatewayMessage;
#[derive(CandidType, Clone, Deserialize, Serialize, Eq, PartialEq)]
#[candid_path("ic_cdk::export::candid")]
pub struct WebsocketMessage {
pub client_key: Vec<u8>,
pub sequence_num: u64,
pub timestamp: u64,
#[serde(with = "serde_bytes")]
pub message: Vec<u8>,
}
#[derive(CandidType, Clone, Deserialize, Serialize, Eq, PartialEq, Debug)]
pub struct EncodedMessage {
pub client_key: Vec<u8>,
pub key: String,
#[serde(with = "serde_bytes")]
pub val: Vec<u8>,
}
#[derive(CandidType, Clone, Deserialize, Serialize, Eq, PartialEq, Debug)]
pub struct CertMessages {
pub messages: Vec<EncodedMessage>,
#[serde(with = "serde_bytes")]
pub cert: Vec<u8>,
#[serde(with = "serde_bytes")]
pub tree: Vec<u8>,
}
pub async fn get_new_agent(url: &str, identity: BasicIdentity, fetch_key: bool) -> Agent {
let transport = ReqwestHttpReplicaV2Transport::create(url.to_string()).unwrap();
let agent = Agent::builder()
.with_transport(transport)
.with_identity(identity)
.build()
.unwrap();
if fetch_key {
agent.fetch_root_key().await.unwrap();
}
agent
}
pub async fn ws_open(
agent: &Agent,
canister_id: &Principal,
content: Vec<u8>,
sig: Vec<u8>,
) -> bool {
let args = candid::encode_args((content, sig)).unwrap();
let res = agent
.update(canister_id, "ws_open")
.with_arg(args)
.call_and_wait()
.await
.unwrap();
Decode!(&res, bool).map_err(|e| e.to_string()).unwrap()
}
pub async fn ws_close(agent: &Agent, canister_id: &Principal, can_client_key: Vec<u8>) {
let args = candid::encode_args((can_client_key,)).unwrap();
let res = agent
.update(canister_id, "ws_close")
.with_arg(args)
.call_and_wait()
.await
.unwrap();
Decode!(&res, ()).map_err(|e| e.to_string()).unwrap()
}
pub async fn ws_message(agent: &Agent, canister_id: &Principal, mes: GatewayMessage) -> bool {
let args = candid::encode_args((mes,)).unwrap();
let res = agent
.update(canister_id, "ws_message")
.with_arg(args)
.call_and_wait()
.await
.unwrap();
Decode!(&res, bool).map_err(|e| e.to_string()).unwrap()
}
pub async fn ws_get_messages(agent: &Agent, canister_id: &Principal, nonce: u64) -> CertMessages {
let args = candid::encode_args((nonce,))
.map_err(|e| e.to_string())
.unwrap();
let res = agent
.query(canister_id, "ws_get_messages")
.with_arg(&args)
.call()
.await
.unwrap();
Decode!(&res, CertMessages)
.map_err(|e| e.to_string())
.unwrap()
}