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
// Rust Monero Light Wallet Server RPC Client
// Written in 2021-2022 by
//   Sebastian Kung <seb.kung@gmail.com>
//   Monero Rust Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//

//! Monero daemon and wallet RPC.

// Coding conventions
#![forbid(unsafe_code)]

#[macro_use]
mod util;
mod models;

pub use self::{models::*, util::*};

use jsonrpc_core::types::*;

use serde::Deserialize;
use serde_json::Value;
use std::{
    fmt::Debug,
    iter::{empty, once},
    sync::Arc,
};

enum RpcParams {
    Map(Box<dyn Iterator<Item = (String, Value)> + Send + 'static>),
}

impl RpcParams {
    fn map<M>(v: M) -> Self
    where
        M: Iterator<Item = (&'static str, Value)> + Send + 'static,
    {
        RpcParams::Map(Box::new(v.map(|(k, v)| (k.to_string(), v))))
    }
}

impl From<RpcParams> for Params {
    fn from(value: RpcParams) -> Self {
        match value {
            RpcParams::Map(v) => Params::Map(v.collect()),
        }
    }
}

#[derive(Clone, Debug)]
struct RemoteCaller {
    http_client: reqwest::Client,
    addr: String,
}

impl RemoteCaller {
    async fn daemon_rpc_call<T>(&self, method: &'static str, params: RpcParams) -> anyhow::Result<T>
    where
        T: for<'de> Deserialize<'de> + Send + 'static + Debug,
    {
        let client = self.http_client.clone();
        let uri = format!("{}/{}", &self.addr, method);
        let json_params: jsonrpc_core::types::params::Params = params.into();
        let rsp = client.post(uri).json(&json_params).send().await?;
        if rsp.status() != 200 {
            rsp.error_for_status()?;
            panic!("should never reach here");
        }
        let rsp = rsp.json::<T>().await?;
        Ok(rsp)
    }
}

#[derive(Clone, Debug)]
struct CallerWrapper(Arc<RemoteCaller>);

impl CallerWrapper {
    async fn request<T>(&self, method: &'static str, params: RpcParams) -> anyhow::Result<T>
    where
        T: for<'de> Deserialize<'de> + Send + 'static + Debug,
    {
        let c = self.0.daemon_rpc_call(method, params).await?;
        Ok(serde_json::from_value(c)?)
    }
}

/// Base RPC client. It is useless on its own, please see the attached methods instead.
#[derive(Clone, Debug)]
pub struct LwsRpcClient {
    inner: CallerWrapper,
}

impl LwsRpcClient {
    pub fn new(addr: String) -> Self {
        Self {
            inner: CallerWrapper(Arc::new(RemoteCaller {
                http_client: reqwest::ClientBuilder::new().build().unwrap(),
                addr,
            })),
        }
    }

    pub async fn get_address_info(
        &self,
        address: monero::Address,
        view_key: monero::PrivateKey,
    ) -> anyhow::Result<AddressInfo> {
        let params = empty()
            .chain(once(("address", address.to_string().into())))
            .chain(once(("view_key", view_key.to_string().into())));
        self.inner
            .request("get_address_info", RpcParams::map(params))
            .await
    }

    pub async fn get_address_txs(
        &self,
        address: monero::Address,
        view_key: monero::PrivateKey,
    ) -> anyhow::Result<AddressTxs> {
        let params = empty()
            .chain(once(("address", address.to_string().into())))
            .chain(once(("view_key", view_key.to_string().into())));
        self.inner
            .request("get_address_txs", RpcParams::map(params))
            .await
    }

    pub async fn get_random_outs(
        &self,
        count: u32,
        amounts: Vec<monero::Amount>,
    ) -> anyhow::Result<AmountOuts> {
        let params = empty().chain(once(("count", count.into()))).chain(once((
            "amounts",
            amounts
                .into_iter()
                .map(|s| s.as_pico().to_string())
                .collect::<Vec<_>>()
                .into(),
        )));

        self.inner
            .request("get_random_outs", RpcParams::map(params))
            .await
    }

    pub async fn get_unspent_outs(
        &self,
        address: monero::Address,
        view_key: monero::PrivateKey,
        amount: monero::Amount,
        mixin: u32,
        use_dust: bool,
        dust_threshold: monero::Amount,
    ) -> anyhow::Result<UnspentOuts> {
        let params = empty()
            .chain(once(("address", address.to_string().into())))
            .chain(once(("view_key", view_key.to_string().into())))
            .chain(once(("amount", amount.as_pico().to_string().into())))
            .chain(once(("mixin", mixin.into())))
            .chain(once(("use_dust", use_dust.into())))
            .chain(once((
                "dust_threshold",
                dust_threshold.as_pico().to_string().into(),
            )));
        self.inner
            .request("get_unspent_outs", RpcParams::map(params))
            .await
    }

    pub async fn import_request(
        &self,
        address: monero::Address,
        view_key: monero::PrivateKey,
        from_height: Option<u64>,
    ) -> anyhow::Result<ImportResponse> {
        let params = empty()
            .chain(once(("address", address.to_string().into())))
            .chain(once(("view_key", view_key.to_string().into())))
            .chain(from_height.map(|v| ("from_height", v.into())));

        self.inner
            .request("import_wallet_request", RpcParams::map(params))
            .await
    }

    pub async fn login(
        &self,
        address: monero::Address,
        view_key: monero::PrivateKey,
        create_account: bool,
        generated_locally: bool,
    ) -> anyhow::Result<LoginResponse> {
        let params = empty()
            .chain(once(("address", address.to_string().into())))
            .chain(once(("view_key", view_key.to_string().into())))
            .chain(once(("create_account", create_account.into())))
            .chain(once(("generated_locally", generated_locally.into())));
        self.inner.request("login", RpcParams::map(params)).await
    }
}