Skip to main content

casper_rust_wasm_sdk/js/
externs.rs

1#[cfg(feature = "js")]
2use js_sys::Promise;
3#[cfg(feature = "js")]
4use wasm_bindgen::prelude::*;
5
6/// Logs a message, prefixing it with "log wasm" and sends it to the console in JavaScript when running in a WebAssembly environment.
7/// When running outside WebAssembly, it prints the message to the standard output.
8#[wasm_bindgen]
9extern "C" {
10    #[wasm_bindgen(js_namespace = console, js_name = log)]
11    fn log_with_prefix(s: &str);
12}
13
14/// Logs a message, prefixing it with "log wasm" and sends it to the console in JavaScript when running in a WebAssembly environment.
15/// When running outside WebAssembly, it prints the message to the standard output.
16pub fn log(s: &str) {
17    let prefixed_s = format!("log wasm {s}");
18    #[cfg(all(feature = "js", target_arch = "wasm32"))]
19    log_with_prefix(&prefixed_s);
20    #[cfg(not(target_arch = "wasm32"))]
21    println!("{prefixed_s}");
22}
23
24/// Logs an error message, prefixing it with "error wasm" and sends it to the console in JavaScript when running in a WebAssembly environment.
25/// When running outside WebAssembly, it prints the error message to the standard output.
26#[wasm_bindgen]
27extern "C" {
28    #[wasm_bindgen(js_namespace = console, js_name = error)]
29    fn error_with_prefix(s: &str);
30}
31
32/// Logs an error message, prefixing it with "error wasm" and sends it to the console in JavaScript when running in a WebAssembly environment.
33/// When running outside WebAssembly, it prints the error message to the standard output.
34pub fn error(s: &str) {
35    let prefixed_s = format!("error wasm {s}");
36    #[cfg(all(feature = "js", target_arch = "wasm32"))]
37    error_with_prefix(&prefixed_s);
38    #[cfg(not(target_arch = "wasm32"))]
39    println!("{prefixed_s}");
40}
41
42#[wasm_bindgen]
43extern "C" {
44    pub(crate) type CasperWalletProvider;
45
46    #[wasm_bindgen(js_name = CasperWalletProvider)]
47    pub(crate) fn casper_wallet_provider() -> CasperWalletProvider;
48
49    #[wasm_bindgen(method, catch, js_class = "CasperWalletProvider")]
50    pub(crate) fn requestConnection(this: &CasperWalletProvider) -> Result<Promise, JsValue>;
51
52    #[wasm_bindgen(method, catch, js_class = "CasperWalletProvider")]
53    pub(crate) fn sign(
54        this: &CasperWalletProvider,
55        deploy: &str,
56        signing_public_key_hex: &str,
57    ) -> Result<Promise, JsValue>;
58
59    #[wasm_bindgen(method, catch, js_class = "CasperWalletProvider")]
60    pub(crate) fn signMessage(
61        this: &CasperWalletProvider,
62        message: &str,
63        signing_public_key_hex: &str,
64    ) -> Result<Promise, JsValue>;
65
66    #[wasm_bindgen(method, catch, js_class = "CasperWalletProvider")]
67    pub(crate) fn requestSwitchAccount(this: &CasperWalletProvider) -> Result<Promise, JsValue>;
68
69    #[wasm_bindgen(method, catch, js_class = "CasperWalletProvider")]
70    pub(crate) fn disconnectFromSite(this: &CasperWalletProvider) -> Result<Promise, JsValue>;
71
72    #[wasm_bindgen(method, catch, js_class = "CasperWalletProvider")]
73    pub(crate) fn isConnected(this: &CasperWalletProvider) -> Result<Promise, JsValue>;
74
75    #[wasm_bindgen(method, catch, js_class = "CasperWalletProvider")]
76    pub(crate) fn getActivePublicKey(this: &CasperWalletProvider) -> Result<Promise, JsValue>;
77
78    #[wasm_bindgen(method, catch, js_class = "CasperWalletProvider")]
79    pub(crate) fn getVersion(this: &CasperWalletProvider) -> Result<Promise, JsValue>;
80}