zanolib 0.2.0

Zano wallet library: address handling, transaction parsing/signing, deposit scanning and threshold (MPC) signing.
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! JSON-RPC and `.bin` transport for a Zano daemon.

use crate::error::{Error, Result};
use base64::Engine;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Mutex;

/// Base URL of the public modchain Zano gateway, used when a [`Client`]'s
/// endpoint is empty.
pub const MODCHAIN_ZANO: &str = "https://rpc.modchain.net/chain/zano";

/// Maximum response size accepted from the daemon.
const MAX_RESPONSE: usize = 64 << 20;

/// A client for a Zano daemon.
///
/// `endpoint` is the daemon's base URL (e.g. `http://127.0.0.1:11211`);
/// JSON-RPC lives at `<base>/json_rpc` and binary methods at
/// `<base>/<method>.bin`. As a special case an empty endpoint targets the
/// public modchain gateway ([`MODCHAIN_ZANO`]), whose layout differs
/// (`<base>/rpc` and `<base>/raw/<method>.bin`).
pub struct Client {
    endpoint: String,
    asset_cache: Mutex<HashMap<String, AssetDescriptor>>,
}

impl Client {
    /// Builds a client for the given daemon base URL. Pass `""` for the public
    /// modchain gateway.
    pub fn new(endpoint: &str) -> Client {
        Client {
            endpoint: endpoint.to_string(),
            asset_cache: Mutex::new(HashMap::new()),
        }
    }

    /// The configured endpoint (empty means the modchain gateway).
    pub fn endpoint(&self) -> &str {
        &self.endpoint
    }

    /// The JSON-RPC 2.0 endpoint URL.
    pub fn json_rpc_url(&self) -> String {
        if self.endpoint.is_empty() {
            return format!("{MODCHAIN_ZANO}/rpc");
        }
        format!("{}/json_rpc", self.endpoint.trim_end_matches('/'))
    }

    /// The binary endpoint URL for `method`.
    pub fn bin_url(&self, method: &str) -> String {
        if self.endpoint.is_empty() {
            return format!("{MODCHAIN_ZANO}/raw/{method}.bin");
        }
        format!("{}/{method}.bin", self.endpoint.trim_end_matches('/'))
    }

    fn post(&self, url: &str, content_type: &str, body: Vec<u8>) -> Result<Vec<u8>> {
        let resp = rsurl::Request::new("POST", url)
            .map_err(|e| crate::err!("zano rpc: bad url {url}: {e}"))?
            .header("Content-Type", content_type)
            .body(body)
            .send()
            .map_err(|e| crate::err!("zano rpc: {url}: {e}"))?;
        if resp.body.len() > MAX_RESPONSE {
            return Err(Error::msg("zano rpc: response too large"));
        }
        if resp.status != 200 {
            return Err(crate::err!(
                "zano rpc: http {}: {}",
                resp.status,
                String::from_utf8_lossy(&resp.body)
            ));
        }
        Ok(resp.body)
    }

    /// Performs one JSON-RPC call and deserializes the `result` field.
    pub fn call<P: Serialize, R: DeserializeOwned>(&self, method: &str, params: &P) -> Result<R> {
        let req = RpcRequest {
            jsonrpc: "2.0",
            id: 0,
            method,
            params,
        };
        let body = serde_json::to_vec(&req)?;
        let data = self.post(&self.json_rpc_url(), "application/json", body)?;

        let env: RpcResponse = serde_json::from_slice(&data)
            .map_err(|e| crate::err!("zano rpc {method}: decode envelope: {e}"))?;
        if let Some(err) = env.error {
            return Err(crate::err!(
                "zano rpc {method}: zano rpc error {}: {}",
                err.code,
                err.message
            ));
        }
        let result = env
            .result
            .ok_or_else(|| crate::err!("zano rpc {method}: no result in response"))?;
        serde_json::from_str(result.get())
            .map_err(|e| crate::err!("zano rpc {method}: decode result: {e}"))
    }

    /// Posts an epee-serialized request to a `.bin` endpoint.
    pub fn post_bin(&self, method: &str, body: Vec<u8>) -> Result<Vec<u8>> {
        self.post(&self.bin_url(method), "application/octet-stream", body)
    }

    /// The current blockchain height (number of blocks).
    pub fn get_block_count(&self) -> Result<u64> {
        #[derive(Deserialize)]
        struct Res {
            count: u64,
        }
        let res: Res = self.call("getblockcount", &serde_json::json!({}))?;
        Ok(res.count)
    }

    /// Details for `count` blocks starting at height `start`. When `ignore_tx`
    /// is true the daemon omits transaction details.
    pub fn get_blocks_details(
        &self,
        start: u64,
        count: u64,
        ignore_tx: bool,
    ) -> Result<Vec<BlockDetails>> {
        #[derive(Serialize)]
        struct Params {
            height_start: u64,
            count: u64,
            ignore_transactions: bool,
        }
        #[derive(Deserialize)]
        struct Res {
            #[serde(default)]
            blocks: Vec<BlockDetails>,
        }
        let res: Res = self.call(
            "get_blocks_details",
            &Params {
                height_start: start,
                count,
                ignore_transactions: ignore_tx,
            },
        )?;
        Ok(res.blocks)
    }

    /// Fetches a transaction by hash and decodes its raw blob.
    pub fn get_tx_details(&self, tx_hash: &str) -> Result<TxDetails> {
        #[derive(Serialize)]
        struct Params<'a> {
            tx_hash: &'a str,
        }
        #[derive(Deserialize)]
        struct Res {
            tx_info: TxDetailsRaw,
        }
        #[derive(Deserialize)]
        struct TxDetailsRaw {
            #[serde(default)]
            id: String,
            #[serde(default)]
            keeper_block: u64,
            #[serde(default)]
            blob: String,
            #[serde(default)]
            outs: Vec<TxOutInfo>,
        }

        let res: Res = self.call("get_tx_details", &Params { tx_hash })?;
        let blob = base64::engine::general_purpose::STANDARD
            .decode(res.tx_info.blob.as_bytes())
            .map_err(|e| crate::err!("get_tx_details {tx_hash}: decode blob: {e}"))?;
        Ok(TxDetails {
            id: res.tx_info.id,
            keeper_block: res.tx_info.keeper_block,
            blob,
            outs: res.tx_info.outs,
        })
    }

    /// Broadcasts a fully-serialized transaction. Returns the daemon status
    /// string (`"OK"` on success).
    pub fn send_raw_tx(&self, raw: &[u8]) -> Result<String> {
        #[derive(Serialize)]
        struct Params {
            tx_as_base64: String,
        }
        #[derive(Deserialize)]
        struct Res {
            #[serde(default)]
            status: String,
        }
        let res: Res = self.call(
            "sendrawtransaction",
            &Params {
                tx_as_base64: base64::engine::general_purpose::STANDARD.encode(raw),
            },
        )?;
        Ok(res.status)
    }

    /// Looks up a confidential asset's descriptor by hex id, caching the result.
    pub fn get_asset_info(&self, asset_id_hex: &str) -> Result<AssetDescriptor> {
        if let Some(d) = self
            .asset_cache
            .lock()
            .expect("asset cache mutex")
            .get(asset_id_hex)
        {
            return Ok(d.clone());
        }

        #[derive(Serialize)]
        struct Params<'a> {
            asset_id: &'a str,
        }
        #[derive(Deserialize)]
        struct Res {
            asset_descriptor: AssetDescriptor,
        }
        let res: Res = self.call(
            "get_asset_info",
            &Params {
                asset_id: asset_id_hex,
            },
        )?;
        self.asset_cache
            .lock()
            .expect("asset cache mutex")
            .insert(asset_id_hex.to_string(), res.asset_descriptor.clone());
        Ok(res.asset_descriptor)
    }
}

#[derive(Serialize)]
struct RpcRequest<'a, P> {
    jsonrpc: &'a str,
    id: u32,
    method: &'a str,
    params: &'a P,
}

#[derive(Deserialize)]
struct RpcResponse {
    #[serde(default)]
    result: Option<Box<serde_json::value::RawValue>>,
    #[serde(default)]
    error: Option<RpcError>,
}

#[derive(Deserialize)]
struct RpcError {
    #[serde(default)]
    code: i64,
    #[serde(default)]
    message: String,
}

/// A block's height and the transactions it contains.
#[derive(Clone, Debug, Deserialize)]
pub struct BlockDetails {
    /// Block height.
    #[serde(default)]
    pub height: u64,
    /// Transactions in this block.
    #[serde(default, rename = "transactions_details")]
    pub transactions: Vec<TxBrief>,
}

/// Identifies a transaction within a block.
#[derive(Clone, Debug, Deserialize)]
pub struct TxBrief {
    /// Transaction hash, hex.
    #[serde(default)]
    pub id: String,
}

/// The raw transaction blob plus the per-output global indices needed to spend
/// received outputs later.
#[derive(Clone, Debug)]
pub struct TxDetails {
    /// Transaction hash, hex.
    pub id: String,
    /// Height of the block holding this transaction.
    pub keeper_block: u64,
    /// The raw (binary) transaction.
    pub blob: Vec<u8>,
    /// The daemon's view of each output, in vout order.
    pub outs: Vec<TxOutInfo>,
}

/// The daemon's view of one transaction output.
#[derive(Clone, Copy, Debug, Deserialize)]
pub struct TxOutInfo {
    /// Chain-wide global output index.
    #[serde(default)]
    pub global_index: u64,
    /// Whether the output has been spent.
    #[serde(default)]
    pub is_spent: bool,
}

/// Describes a Zano confidential asset.
///
/// The native coin is not a registered asset and has no descriptor; see
/// [`ReceivedOutput::is_native`](crate::ReceivedOutput::is_native).
#[derive(Clone, Debug, Default, Deserialize)]
pub struct AssetDescriptor {
    /// Short ticker.
    #[serde(default)]
    pub ticker: String,
    /// Human-readable name.
    #[serde(default)]
    pub full_name: String,
    /// Number of decimals in the display representation.
    #[serde(default)]
    pub decimal_point: u8,
    /// Current supply, in atomic units.
    #[serde(default)]
    pub current_supply: u64,
    /// Maximum supply, in atomic units.
    #[serde(default)]
    pub total_max_supply: u64,
    /// Whether the supply is hidden.
    #[serde(default)]
    pub hidden_supply: bool,
    /// Owner key.
    #[serde(default)]
    pub owner: String,
    /// Owner's Ethereum public key, if any.
    #[serde(default)]
    pub owner_eth_pub_key: String,
    /// Free-form metadata.
    #[serde(default)]
    pub meta_info: String,
}

impl AssetDescriptor {
    /// Renders an atomic amount using this asset's `decimal_point`.
    pub fn format_amount(&self, atomic: u64) -> String {
        format_atomic(atomic, self.decimal_point)
    }
}

/// Renders an atomic amount with the given number of decimals
/// (e.g. `10000` with 4 decimals gives `"1.0000"`).
pub fn format_atomic(atomic: u64, decimals: u8) -> String {
    let s = atomic.to_string();
    if decimals == 0 {
        return s;
    }
    let d = decimals as usize;
    let s = if s.len() <= d {
        format!("{}{}", "0".repeat(d - s.len() + 1), s)
    } else {
        s
    };
    let split = s.len() - d;
    format!("{}.{}", &s[..split], &s[split..])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn urls_follow_the_endpoint_layout() {
        let direct = Client::new("http://127.0.0.1:11211");
        assert_eq!(direct.json_rpc_url(), "http://127.0.0.1:11211/json_rpc");
        assert_eq!(
            direct.bin_url("getrandom_outs3"),
            "http://127.0.0.1:11211/getrandom_outs3.bin"
        );

        // A trailing slash must not produce a double slash.
        let slash = Client::new("http://127.0.0.1:11211/");
        assert_eq!(slash.json_rpc_url(), "http://127.0.0.1:11211/json_rpc");
        assert_eq!(
            slash.bin_url("getrandom_outs3"),
            "http://127.0.0.1:11211/getrandom_outs3.bin"
        );

        // The empty endpoint targets the modchain gateway, which nests differently.
        let gw = Client::new("");
        assert_eq!(gw.json_rpc_url(), format!("{MODCHAIN_ZANO}/rpc"));
        assert_eq!(
            gw.bin_url("getrandom_outs3"),
            format!("{MODCHAIN_ZANO}/raw/getrandom_outs3.bin")
        );
    }

    #[test]
    fn format_atomic_pads_and_splits() {
        assert_eq!(format_atomic(10000, 4), "1.0000");
        assert_eq!(format_atomic(1, 4), "0.0001");
        assert_eq!(format_atomic(0, 4), "0.0000");
        assert_eq!(format_atomic(123456789, 8), "1.23456789");
        assert_eq!(format_atomic(42, 0), "42");
    }
}