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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use anyhow::{Context, Result};
use bech32::{decode, encode, FromBase32, ToBase32, Variant};
#[cfg(target_arch = "wasm32")]
use reqwest::{self, header::AUTHORIZATION};
use serde::Serialize;
#[macro_export]
macro_rules! info {
($($arg:expr),+) => {
let output = vec![$(String::from($arg.to_owned()),)+].join(" ");
#[cfg(target_arch = "wasm32")]
gloo_console::info!(format!("{}", output));
#[cfg(not(target_arch = "wasm32"))]
log::info!("{}", output);
};
}
#[macro_export]
macro_rules! debug {
($($arg:expr),+) => {
let output = vec![$(String::from($arg.to_owned()),)+].join(" ");
#[cfg(target_arch = "wasm32")]
gloo_console::debug!(format!("{}", output));
#[cfg(not(target_arch = "wasm32"))]
log::debug!("{}", output);
};
}
#[macro_export]
macro_rules! error {
($($arg:expr),+) => {
let output = vec![$(String::from($arg.to_owned()),)+].join(" ");
#[cfg(target_arch = "wasm32")]
gloo_console::error!(format!("{}", output));
#[cfg(not(target_arch = "wasm32"))]
log::error!("{}", output);
};
}
#[macro_export]
macro_rules! warn {
($($arg:expr),+) => {
let output = vec![$(String::from($arg.to_owned()),)+].join(" ");
#[cfg(target_arch = "wasm32")]
gloo_console::warn!(format!("{}", output));
#[cfg(not(target_arch = "wasm32"))]
log::warn!("{}", output);
};
}
#[macro_export]
macro_rules! trace {
($($arg:expr),+) => {
let output = vec![$(String::from($arg.to_owned()),)+].join(" ");
#[cfg(target_arch = "wasm32")]
gloo_console::trace!(format!("{}", output));
#[cfg(not(target_arch = "wasm32"))]
log::trace!("{}", output);
};
}
#[cfg(target_arch = "wasm32")]
pub async fn post_json<T: Serialize>(url: &str, body: &T) -> Result<(String, u16)> {
let client = reqwest::Client::new();
let response = client
.post(url)
.body(serde_json::to_string(body)?)
.header("Content-Type", "application/json; charset=UTF-8")
.send()
.await
.context(format!("Error sending JSON POST request to {url}"))?;
let status_code = response.status().as_u16();
let response_text = response.text().await.context(format!(
"Error in parsing server response for POST JSON request to {url}"
))?;
Ok((response_text, status_code))
}
#[cfg(target_arch = "wasm32")]
pub async fn get(url: &str, token: Option<&str>) -> Result<String> {
let client = reqwest::Client::new();
let mut response = client.get(url);
if let Some(t) = token {
response = response.header(AUTHORIZATION, t);
}
let response = response
.send()
.await
.context(format!("Error sending GET request to {url}"))?;
let response_text = response.text().await.context(format!(
"Error in parsing server response for GET request to {url}"
))?;
Ok(response_text)
}
#[cfg(target_arch = "wasm32")]
pub async fn post_json_auth<T: Serialize>(
url: &str,
body: &Option<T>,
token: Option<&str>,
) -> Result<String> {
let client = reqwest::Client::new();
let mut response = client.post(url);
if let Some(b) = body {
response = response.json(&b);
}
if let Some(t) = token {
response = response.header(AUTHORIZATION, t);
}
let response = response
.send()
.await
.context(format!("Error sending JSON POST request to {url}"))?;
let response_text = response.text().await.context(format!(
"Error in parsing server response for POST JSON request to {url}"
))?;
Ok(response_text)
}
#[cfg(not(target_arch = "wasm32"))]
pub async fn post_json<T: Serialize>(url: &str, body: &T) -> Result<(String, u16)> {
let client = reqwest::Client::new();
let response = client
.post(url)
.body(serde_json::to_string(body)?)
.header("Content-Type", "application/json; charset=UTF-8")
.send()
.await
.context(format!("Error sending JSON POST request to {url}"))?;
let status_code = response.status().as_u16();
let response_text = response.text().await.context(format!(
"Error in parsing server response for POST JSON request to {url}"
))?;
Ok((response_text, status_code))
}
#[cfg(not(target_arch = "wasm32"))]
pub async fn post_json_auth<T: Serialize>(
url: &str,
body: &Option<T>,
token: Option<&str>,
) -> Result<String> {
let client = reqwest::Client::new();
let mut response = client.post(url);
if let Some(b) = body {
response = response.json(&b);
}
if let Some(t) = token {
response = response.header(reqwest::header::AUTHORIZATION, t);
}
let response = response
.send()
.await
.context(format!("Error sending JSON POST request to {url}"))?;
let response_text = response.text().await.context(format!(
"Error in parsing server response for POST JSON request to {url}"
))?;
Ok(response_text)
}
#[cfg(not(target_arch = "wasm32"))]
pub async fn get(url: &str, token: Option<&str>) -> Result<String> {
let client = reqwest::Client::new();
let mut response = client.get(url);
if let Some(t) = token {
response = response.header(reqwest::header::AUTHORIZATION, t);
}
let response = response
.send()
.await
.context(format!("Error sending GET request to {url}"))?;
let response_text = response.text().await.context(format!(
"Error in parsing server response for GET request to {url}"
))?;
Ok(response_text)
}
pub fn bech32_encode(hrp: &str, bytes: &[u8]) -> Result<String> {
Ok(encode(hrp, bytes.to_base32(), Variant::Bech32)?)
}
pub fn bech32m_encode(hrp: &str, bytes: &[u8]) -> Result<String> {
Ok(encode(hrp, bytes.to_base32(), Variant::Bech32m)?)
}
pub const RAW_DATA_ENCODING_DEFLATE: u8 = 1u8;
#[cfg(not(target_arch = "wasm32"))]
pub fn bech32m_zip_encode(hrp: &str, bytes: &[u8]) -> Result<String> {
use deflate::{write::DeflateEncoder, Compression};
use std::io::Write;
let writer = vec![RAW_DATA_ENCODING_DEFLATE];
let mut encoder = DeflateEncoder::new(writer, Compression::Best);
encoder.write_all(bytes)?;
let bytes = encoder.finish()?;
Ok(encode(hrp, bytes.to_base32(), Variant::Bech32m)?)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn bech32m_zip_decode(bech32_str: &str) -> Result<Vec<u8>> {
use anyhow::anyhow;
let (_, data, _) = bech32_decode(bech32_str)?;
match *data[..].first().unwrap() {
RAW_DATA_ENCODING_DEFLATE => {
let decoded = inflate::inflate_bytes(&data[1..]).map_err(|e| anyhow!(e))?;
Ok(decoded)
}
_ => Err(anyhow!("Unknown version")),
}
}
pub fn bech32_decode(bech32_str: &str) -> Result<(String, Vec<u8>, Variant)> {
let (hrp, words, variant) = decode(bech32_str)?;
Ok((hrp, Vec::<u8>::from_base32(&words)?, variant))
}