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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* This file is part of DarkFi (https://dark.fi)
 *
 * Copyright (C) 2020-2023 Dyne.org foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/*
use log::{debug, error};
use serde_json::{json, Value};

use darkfi::{
    rpc::jsonrpc::{
        ErrorCode::{InternalError, InvalidParams, ParseError},
        JsonError, JsonResponse, JsonResult,
    },
    wallet::walletdb::QueryType,
};

use super::{error::RpcError, server_error, Darkfid};
*/
use darkfi::rpc::jsonrpc::JsonResult;
use tinyjson::JsonValue;

use super::Darkfid;

impl Darkfid {
    // RPCAPI:
    // Attempts to query for a single row in a given table.
    // The parameters given contain paired metadata so we know how to decode the SQL data.
    // An example of `params` is as such:
    // ```
    // params[0] -> "sql query"
    // params[1] -> column_type
    // params[2] -> "column_name"
    // ...
    // params[n-1] -> column_type
    // params[n] -> "column_name"
    // ```
    // This function will fetch the first row it finds, if any. The `column_type` field
    // is a type available in the `WalletDb` API as an enum called `QueryType`. If a row
    // is not found, the returned result will be a JSON-RPC error.
    // NOTE: This is obviously vulnerable to SQL injection. Open to interesting solutions.
    //
    // --> {"jsonrpc": "2.0", "method": "wallet.query_row_single", "params": [...], "id": 1}
    // <-- {"jsonrpc": "2.0", "result": ["va", "lu", "es", ...], "id": 1}
    pub async fn wallet_query_row_single(&self, _id: u16, _params: JsonValue) -> JsonResult {
        todo!();
        /* TODO: This will be abstracted away
        // We need at least 3 params for something we want to fetch, and we want them in pairs.
        // Also the first param should be a String
        if params.len() < 3 || params[1..].len() % 2 != 0 || !params[0].is_string() {
            return JsonError::new(InvalidParams, None, id).into()
        }

        // The remaining pairs should be typed properly too
        let mut types: Vec<QueryType> = vec![];
        let mut names: Vec<&str> = vec![];
        for pair in params[1..].chunks(2) {
            if !pair[0].is_u64() || !pair[1].is_string() {
                return JsonError::new(InvalidParams, None, id).into()
            }

            let typ = pair[0].as_u64().unwrap();
            if typ >= QueryType::Last as u64 {
                return JsonError::new(InvalidParams, None, id).into()
            }

            types.push((typ as u8).into());
            names.push(pair[1].as_str().unwrap());
        }

        // Get a wallet connection
        let mut conn = match self.wallet.conn.acquire().await {
            Ok(v) => v,
            Err(e) => {
                error!("[RPC] wallet.query_row_single: Failed to acquire wallet connection: {}", e);
                return JsonError::new(InternalError, None, id).into()
            }
        };

        // Execute the query and see if we find a row
        let row = match sqlx::query(params[0].as_str().unwrap()).fetch_one(&mut conn).await {
            Ok(v) => Some(v),
            Err(_) => None,
        };

        // Try to decode the row into what was requested
        let mut ret: Vec<Value> = vec![];

        for (typ, col) in types.iter().zip(names) {
            match typ {
                QueryType::Integer => {
                    let Some(ref row) = row else {
                        error!("[RPC] wallet.query_row_single: Got None for QueryType::Integer");
                        return server_error(RpcError::NoRowsFoundInWallet, id, None)
                    };

                    let value: i32 = match row.try_get(col) {
                        Ok(v) => v,
                        Err(e) => {
                            error!("[RPC] wallet.query_row_single: {}", e);
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    ret.push(json!(value));
                    continue
                }

                QueryType::Blob => {
                    let Some(ref row) = row else {
                        error!("[RPC] wallet.query_row_single: Got None for QueryType::Blob");
                        return server_error(RpcError::NoRowsFoundInWallet, id, None)
                    };

                    let value: Vec<u8> = match row.try_get(col) {
                        Ok(v) => v,
                        Err(e) => {
                            error!("[RPC] wallet.query_row_single: {}", e);
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    ret.push(json!(value));
                    continue
                }

                QueryType::OptionInteger => {
                    let Some(ref row) = row else {
                        ret.push(json!(None::<i32>));
                        continue
                    };

                    let value: i32 = match row.try_get(col) {
                        Ok(v) => v,
                        Err(e) => {
                            error!("[RPC] wallet.query_row_single: {}", e);
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    ret.push(json!(value));
                    continue
                }

                QueryType::OptionBlob => {
                    let Some(ref row) = row else {
                        ret.push(json!(None::<Vec<u8>>));
                        continue
                    };

                    let value: Vec<u8> = match row.try_get(col) {
                        Ok(v) => v,
                        Err(e) => {
                            error!("[RPC] wallet.query_row_single: {}", e);
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    ret.push(json!(value));
                    continue
                }

                QueryType::Text => {
                    let Some(ref row) = row else {
                        error!("[RPC] wallet.query_row_single: Got None for QueryType::Text");
                        return server_error(RpcError::NoRowsFoundInWallet, id, None)
                    };

                    let value: String = match row.try_get(col) {
                        Ok(v) => v,
                        Err(e) => {
                            error!("[RPC] wallet.query_row_single: {}", e);
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    ret.push(json!(value));
                    continue
                }

                _ => unreachable!(),
            }
        }

        JsonResponse::new(json!(ret), id).into()
        */
    }

    // RPCAPI:
    // Attempts to query for all available rows in a given table.
    // The parameters given contain paired metadata so we know how to decode the SQL data.
    // They're the same as above in `wallet.query_row_single`.
    // If there are any values found, they will be returned in a paired array. If not, an
    // empty array will be returned.
    //
    // --> {"jsonrpc": "2.0", "method": "wallet.query_row_multi", "params": [...], "id": 1}
    // <-- {"jsonrpc": "2.0", "result": [["va", "lu"], ["es", "es"], ...], "id": 1}
    pub async fn wallet_query_row_multi(&self, _id: u16, _params: JsonValue) -> JsonResult {
        todo!();
        /* TODO: This will be abstracted away
        // We need at least 3 params for something we want to fetch, and we want them in pairs.
        // Also the first param (the query) should be a String.
        if params.len() < 3 || params[1..].len() % 2 != 0 || !params[0].is_string() {
            return JsonError::new(InvalidParams, None, id).into()
        }

        // The remaining pairs should be typed properly too
        let mut types: Vec<QueryType> = vec![];
        let mut names: Vec<&str> = vec![];
        for pair in params[1..].chunks(2) {
            if !pair[0].is_u64() || !pair[1].is_string() {
                return JsonError::new(InvalidParams, None, id).into()
            }

            let typ = pair[0].as_u64().unwrap();
            if typ >= QueryType::Last as u64 {
                return JsonError::new(InvalidParams, None, id).into()
            }

            types.push((typ as u8).into());
            names.push(pair[1].as_str().unwrap());
        }

        // Get a wallet connection
        let mut conn = match self.wallet.conn.acquire().await {
            Ok(v) => v,
            Err(e) => {
                error!("[RPC] wallet.query_row_multi: Failed to acquire wallet connection: {}", e);
                return JsonError::new(InternalError, None, id).into()
            }
        };

        // Execute the query and see if we find any rows
        let rows = match sqlx::query(params[0].as_str().unwrap()).fetch_all(&mut conn).await {
            Ok(v) => v,
            Err(e) => {
                error!("[RPC] wallet.query_row_multi: Failed to execute SQL query: {}", e);
                return JsonError::new(InternalError, None, id).into()
            }
        };

        debug!("[RPC] wallet.query_row_multi: Found {} rows", rows.len());

        // Try to decode whatever we've found
        let mut ret: Vec<Vec<Value>> = vec![];

        for row in rows {
            let mut row_ret: Vec<Value> = vec![];
            for (typ, col) in types.iter().zip(names.clone()) {
                match typ {
                    QueryType::Integer => {
                        let value: i32 = match row.try_get(col) {
                            Ok(v) => v,
                            Err(e) => {
                                error!("[RPC] wallet.query_row_multi: {}", e);
                                return JsonError::new(ParseError, None, id).into()
                            }
                        };

                        row_ret.push(json!(value));
                    }

                    QueryType::Blob => {
                        let value: Vec<u8> = match row.try_get(col) {
                            Ok(v) => v,
                            Err(e) => {
                                error!("[RPC] wallet.query_row_multi: {}", e);
                                return JsonError::new(ParseError, None, id).into()
                            }
                        };

                        row_ret.push(json!(value));
                    }

                    QueryType::OptionInteger => {
                        let value: Option<i32> = match row.try_get(col) {
                            Ok(v) => Some(v),
                            Err(_) => None,
                        };

                        row_ret.push(json!(value));
                    }

                    QueryType::OptionBlob => {
                        let value: Option<Vec<u8>> = match row.try_get(col) {
                            Ok(v) => Some(v),
                            Err(_) => None,
                        };

                        row_ret.push(json!(value));
                    }

                    QueryType::Text => {
                        let value: String = match row.try_get(col) {
                            Ok(v) => v,
                            Err(e) => {
                                error!("[RPC] wallet.query_row_multi: {}", e);
                                return JsonError::new(ParseError, None, id).into()
                            }
                        };

                        row_ret.push(json!(value));
                    }

                    _ => unreachable!(),
                }
            }

            ret.push(row_ret);
        }

        JsonResponse::new(json!(ret), id).into()
        */
    }

    // RPCAPI:
    // Executes an arbitrary SQL query on the wallet, and returns `true` on success.
    // `params[1..]` can optionally be provided in pairs like in `wallet.query_row_single`.
    //
    // --> {"jsonrpc": "2.0", "method": "wallet.exec_sql", "params": ["CREATE TABLE ..."], "id": 1}
    // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
    pub async fn wallet_exec_sql(&self, _id: u16, _params: JsonValue) -> JsonResult {
        todo!();
        /* TODO: This will be abstracted away
        if params.is_empty() || !params[0].is_string() {
            return JsonError::new(InvalidParams, None, id).into()
        }

        if params.len() > 1 && params[1..].len() % 2 != 0 {
            return JsonError::new(InvalidParams, None, id).into()
        }

        let query = params[0].as_str().unwrap();
        debug!("Executing SQL query: {}", query);
        let mut query = sqlx::query(query);

        for pair in params[1..].chunks(2) {
            if !pair[0].is_u64() || pair[0].as_u64().unwrap() >= QueryType::Last as u64 {
                return JsonError::new(InvalidParams, None, id).into()
            }

            let typ = (pair[0].as_u64().unwrap() as u8).into();
            match typ {
                QueryType::Integer => {
                    let val: i32 = match serde_json::from_value(pair[1].clone()) {
                        Ok(v) => v,
                        Err(e) => {
                            error!("[RPC] wallet.exec_sql: Failed casting value to i32: {}", e);
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    query = query.bind(val);
                }

                QueryType::Blob => {
                    let val: Vec<u8> = match serde_json::from_value(pair[1].clone()) {
                        Ok(v) => v,
                        Err(e) => {
                            error!("[RPC] wallet.exec_sql: Failed casting value to Vec<u8>: {}", e);
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    query = query.bind(val);
                }

                QueryType::OptionInteger => {
                    let val: Option<i32> = match serde_json::from_value(pair[1].clone()) {
                        Ok(v) => v,
                        Err(e) => {
                            error!(
                                "[RPC] wallet.exec_sql: Failed casting value to Option<i32>: {}",
                                e
                            );
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    query = query.bind(val);
                }

                QueryType::OptionBlob => {
                    let val: Option<Vec<u8>> = match serde_json::from_value(pair[1].clone()) {
                        Ok(v) => v,
                        Err(e) => {
                            error!("[RPC] wallet.exec_sql: Failed casting value to Option<Vec<u8>>: {}", e);
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    query = query.bind(val);
                }

                QueryType::Text => {
                    let val: String = match serde_json::from_value(pair[1].clone()) {
                        Ok(v) => v,
                        Err(e) => {
                            error!("[RPC] wallet.exec_sql: Failed casting value to String: {}", e);
                            return JsonError::new(ParseError, None, id).into()
                        }
                    };

                    query = query.bind(val);
                }

                _ => return JsonError::new(InvalidParams, None, id).into(),
            }
        }

        // Get a wallet connection
        let mut conn = match self.wallet.conn.acquire().await {
            Ok(v) => v,
            Err(e) => {
                error!("[RPC] wallet.exec_sql: Failed to acquire wallet connection: {}", e);
                return JsonError::new(InternalError, None, id).into()
            }
        };

        if let Err(e) = query.execute(&mut conn).await {
            error!("[RPC] wallet.exec_sql: Failed to execute sql query: {}", e);
            return JsonError::new(InternalError, None, id).into()
        };

        JsonResponse::new(json!(true), id).into()
        */
    }
}