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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
pub mod charges;
pub mod errors;
pub mod gamertag;
pub mod internal_transfer;
pub mod keysend;
pub mod ln_address;
pub mod login_with_zbd;
pub mod payments;
pub mod utilities;
pub mod wallet;
pub mod withdrawal_request;

use charges::*;
use errors::*;
use gamertag::*;
use internal_transfer::*;
use keysend::*;
use ln_address::*;
use login_with_zbd::*;
use payments::*;
use rand::Rng;
use reqwest::{RequestBuilder, Response};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use utilities::*;
use validator::Validate;
use wallet::*;
use withdrawal_request::*;

pub type Result<T, E = errors::ZebedeeError> = std::result::Result<T, E>;

#[derive(Clone, Debug)]
pub struct ZebedeeClient {
    domain: String,
    reqw_cli: reqwest::Client,
    apikey: String,
    oauth: ZebedeeOauth,
}

impl ZebedeeClient {
    pub fn new() -> Self {
        ZebedeeClient::default()
    }

    /// Zebedee REST API url
    pub fn domain(mut self, domain: String) -> Self {
        self.domain = domain;
        self
    }

    /// Project API key
    pub fn apikey(mut self, apikey: String) -> Self {
        self.apikey = apikey;
        self
    }

    pub fn reqw_cli(mut self, reqw_cli: reqwest::Client) -> Self {
        self.reqw_cli = reqw_cli;
        self
    }
    pub fn oauth(
        mut self,
        client_id: String,
        secret: String,
        redirect_uri: String,
        state: String,
        scope: String,
    ) -> Self {
        let oauth = ZebedeeOauth::new(client_id, secret, redirect_uri, state, scope);
        self.oauth = oauth;
        self
    }

    pub fn build(self) -> Self {
        ZebedeeClient {
            domain: self.domain,
            reqw_cli: self.reqw_cli,
            apikey: self.apikey,
            oauth: self.oauth,
        }
    }

    async fn parse_response<T>(&self, resp: Response) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let is_success = resp.status().is_success();
        // parse the resp body
        let body = resp.json::<Value>().await?;

        // based on success or error choose the appropriate data structure to deserialize
        match is_success {
            true => {
                let body = serde_json::from_value::<T>(body)?;
                Ok(body)
            }
            false => {
                let err_body: ApiError = serde_json::from_value(body)?;
                Err(err_body.into())
            }
        }
    }

    fn add_headers(&self, request_builder: RequestBuilder) -> RequestBuilder {
        request_builder
            .header("Content-Type", "application/json")
            .header("apikey", &self.apikey)
    }

    /// Retrieves the total balance of a given Project Wallet.
    pub async fn get_wallet_details(&self) -> Result<WalletInfoResponse> {
        let url = format!("{}/v0/wallet", &self.domain);
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Make payment directly to a Lightning Network node Public Key, without the need for a Payment Request / Charge.
    pub async fn keysend(&self, keysend_payload: &Keysend) -> Result<KeysendResponse> {
        let url = format!("{}/v0/keysend-payment", &self.domain);

        let resp = self
            .add_headers(self.reqw_cli.post(&url))
            .json(keysend_payload)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    /// Creates a new Charge / Payment Request in the Bitcoin Lightning Network, payable by any Lightning Network wallet.
    /// These payment requests are single-use, fixed-amount QR codes. If you're looking for multi-use and multi-amount
    /// payment requests you want Static Charges.
    pub async fn create_charge(&self, charge: &Charge) -> Result<FetchOneChargeResponse> {
        let url = format!("{}/v0/charges", &self.domain);

        let resp = self
            .add_headers(self.reqw_cli.post(&url))
            .json(&charge)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    pub async fn get_charges(&self) -> Result<FetchChargesResponse> {
        let url = format!("{}/v0/charges", &self.domain);
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Retrieves all information relating a specific Charge / Payment Request.
    pub async fn get_charge<T>(&self, charge_id: T) -> Result<FetchOneChargeResponse>
    where
        T: AsRef<str>,
    {
        let url = format!("{}/v0/charges/{}", &self.domain, charge_id.as_ref());
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Send Bitcoin payments directly to a user's ZBD Gamertag
    pub async fn pay_gamertag(&self, payment: &GamertagPayment) -> Result<GamertagPayResponse> {
        payment
            .validate()
            .map_err(|e| ErrorMsg::BadGamerTagFormat(e.to_string()))?;

        let url = format!("{}/v0/gamertag/send-payment", &self.domain);

        let resp = self
            .add_headers(self.reqw_cli.post(&url))
            .json(payment)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    /// Create a bolt 11 invoice so you can pay a specified gamertag
    pub async fn fetch_charge_from_gamertag(
        &self,
        payment: &GamertagPayment,
    ) -> Result<GamertagChargeResponse> {
        payment
            .validate()
            .map_err(|e| ErrorMsg::BadPayloadData(e.to_string()))?;

        let url = format!("{}/v0/gamertag/charges", &self.domain);

        let resp = self
            .add_headers(self.reqw_cli.post(&url))
            .json(payment)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    /// Get data on payments sent to ZBD Gamertags.
    /// The data payload returned will inform you of the status of that transaction as well as any associated fees.
    pub async fn get_gamertag_tx<T>(&self, transaction_id: T) -> Result<GamertagTxResponse>
    where
        T: AsRef<str>,
    {
        let url = format!(
            "{}/v0/gamertag/transaction/{}",
            &self.domain,
            transaction_id.as_ref()
        );

        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Get a given User's ID when provided with a ZBD Gamertag.
    pub async fn get_userid_by_gamertag<T>(&self, gamertag: T) -> Result<IdFromGamertagResponse>
    where
        T: AsRef<str>,
    {
        let url = format!("{}/v0/user-id/gamertag/{}", &self.domain, gamertag.as_ref());
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Get a given user's ZBD Gamertag from user id
    pub async fn get_gamertag_by_userid<T>(&self, user_id: T) -> Result<GamertagUserIdResponse>
    where
        T: AsRef<str>,
    {
        let url = format!("{}/v0/gamertag/user-id/{}", &self.domain, user_id.as_ref());
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Initiates a transfer of funds between two Project Wallets you own.
    pub async fn internal_transfer(
        &self,
        internal_transfer_payload: &InternalTransfer,
    ) -> Result<InternalTransferResponse> {
        let url = format!("{}/v0/internal-transfer", &self.domain);
        let resp = self
            .add_headers(self.reqw_cli.post(&url))
            .json(internal_transfer_payload)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    /// Send Bitcoin payments directly to a Lightning Address.
    pub async fn pay_ln_address(&self, payment: &LnPayment) -> Result<PayLnAddressResponse> {
        let url = format!("{}/v0/ln-address/send-payment", &self.domain);
        let resp = self
            .add_headers(self.reqw_cli.post(&url))
            .json(payment)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    /// Create a Charge / Payment Request QR code for a Lightning Address
    pub async fn fetch_charge_ln_address(
        &self,
        payment: &LnFetchCharge,
    ) -> Result<FetchLnChargeResponse> {
        let url = format!("{}/v0/ln-address/fetch-charge", &self.domain);

        let resp = self
            .add_headers(self.reqw_cli.post(&url))
            .json(payment)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    /// Validate whether a user's entered Lightning Address is indeed a real Lightning Address
    pub async fn validate_ln_address(
        &self,
        lightning_address: &LnAddress,
    ) -> Result<ValidateLnAddrResponse> {
        lightning_address.validate().map_err(|e| {
            ErrorMsg::BadLnAddress(lightning_address.address.clone(), e.to_string())
        })?;

        let url = format!(
            "{}/v0/ln-address/validate/{}",
            &self.domain, &lightning_address.address
        );

        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;

        self.parse_response(resp).await
    }

    /// Pays a Charge / Payment Request in the Bitcoin Lightning Network
    pub async fn pay_invoice(&self, payment: &Payment) -> Result<PaymentInvoiceResponse> {
        let url = format!("{}/v0/payments", &self.domain);

        let resp = self
            .add_headers(self.reqw_cli.post(&url))
            .json(&payment)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    pub async fn get_payments(&self) -> Result<FetchPaymentsResponse> {
        let url = format!("{}/v0/payments", &self.domain);
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        println!("{:#?}", resp);
        self.parse_response(resp).await
    }

    /// Retrieves all the information related to a specific Payment
    pub async fn get_payment<T>(&self, payment_id: T) -> Result<FetchOnePaymentsResponse>
    where
        T: AsRef<str>,
    {
        let url = format!("{}/v0/payments/{}", &self.domain, payment_id.as_ref());
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Check if provided ip address will be [supported](https://zebedee.io/countries) by Zebedee REST API
    pub async fn get_is_supported_region_by_ip<T>(&self, ip: T) -> Result<SupportedIpResponse>
    where
        T: AsRef<str>,
    {
        let url = format!("{}/v0/is-supported-region/{}", &self.domain, ip.as_ref());
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Check if callback response is from legit Zebedee ip address
    pub async fn get_prod_ips(&self) -> Result<ProdIpsResponse> {
        let url = format!("{}/v0/prod-ips", &self.domain);
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Get the latest price for Bitcoin in US Dollars.
    /// The exchange rate feed is refreshed every 5 seconds and is based upon a combination of industry-leading
    /// partner exchange providers's price feeds.
    pub async fn get_btc_usd(&self) -> Result<BtcToUsdResponse> {
        let url = format!("{}/v0/btcusd", &self.domain);
        let resp = self.reqw_cli.get(&url).send().await?;
        self.parse_response(resp).await
    }

    /// Withdrawal Requests can be thought of as exact opposites to Charges.
    /// Charges in the ZEBEDEE API are QR codes that represent Payment Requests in the Bitcoin Lightning Network.
    /// These QR codes expect that a payer will scan and perform a payment against it.
    /// ***
    /// `Charges`: Lightning QR codes that YOU SPEND
    /// ***
    /// `Withdrawal Requests`: Lightning QR codes that YOU RECEIVE
    pub async fn create_withdrawal_request(
        &self,
        withdrawal_request: &WithdrawalReqest,
    ) -> Result<CreateWithdrawalResponse> {
        let url = format!("{}/v0/withdrawal-requests", &self.domain);

        let resp = self
            .add_headers(self.reqw_cli.post(&url))
            .json(&withdrawal_request)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    pub async fn get_withdrawal_requests(&self) -> Result<FetchWithdrawalsResponse> {
        let url = format!("{}/v0/withdrawal-requests", &self.domain);
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    /// Retrieves details about a specific Withdrawal Request.
    pub async fn get_withdrawal_request<T>(
        &self,
        withdrawal_id: T,
    ) -> Result<FetchOneWithdrawalResponse>
    where
        T: AsRef<str>,
    {
        let url = format!(
            "{}/v0/withdrawal-requests/{}",
            &self.domain,
            withdrawal_id.as_ref()
        );
        let resp = self.add_headers(self.reqw_cli.get(&url)).send().await?;
        self.parse_response(resp).await
    }

    pub async fn create_auth_url<T>(&self, challenge: T) -> Result<String>
    where
        T: AsRef<str>,
    {
        let url = format!("{}/v1/oauth2/authorize", &self.domain);

        let auth_url = self
            .reqw_cli
            .get(url)
            .header("Content-Type", "application/json")
            .query(&[("client_id", &self.oauth.client_id)])
            .query(&[("response_type", "code")])
            .query(&[("redirect_uri", &self.oauth.redirect_uri)])
            .query(&[("code_challenge_method", "S256")])
            .query(&[("code_challenge", challenge.as_ref())])
            .query(&[("scope", &self.oauth.scope)])
            .query(&[("state", &self.oauth.state)])
            .build()
            .unwrap()
            .url()
            .to_string();

        AuthURL::new(&auth_url).validate()?;

        Ok(auth_url)
    }

    pub async fn fetch_token<A, B>(&self, code: A, verifier: B) -> Result<FetchAccessTokenRes>
    where
        A: AsRef<str>,
        B: AsRef<str>,
    {
        let payload = FetchTokenBody::new(self, code.as_ref(), verifier.as_ref());
        payload.validate()?;

        let url = format!("{}/v1/oauth2/token", &self.domain);

        let resp = self
            .reqw_cli
            .post(&url)
            .header("Content-Type", "application/json")
            .json(&payload)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    /// In order to fetch a new accessToken for a given ZBD User, make sure to use the refreshToken using the token endpoint.
    pub async fn refresh_token<T>(&self, refresh_token: T) -> Result<FetchPostRes>
    where
        T: AsRef<str>,
    {
        let payload = FetchRefresh::new(self, refresh_token.as_ref());
        payload.validate()?;

        let url = format!("{}/v1/oauth2/token", &self.domain);
        let resp = self
            .reqw_cli
            .post(&url)
            .header("Content-Type", "application/json")
            .json(&payload)
            .send()
            .await?;

        self.parse_response(resp).await
    }

    /// You can use this API endpoint to fetch information about a given ZBD User, granted you can pass the provided accessToken.

    pub async fn fetch_user_data<T>(&self, token: T) -> Result<StdResp<ZBDUserData>>
    where
        T: AsRef<str>,
    {
        //let mut token_header_string: String = "Bearer ".to_owned();
        //token_header_string.push_str(&bearer_token);

        let url = format!("{}/v1/oauth2/user", &self.domain);

        let resp = self
            .add_headers(self.reqw_cli.get(&url))
            .header("usertoken", token.as_ref())
            .send()
            .await?;

        self.parse_response(resp).await
    }

    /// You can use this API endpoint to fetch information about a given ZBD User's Wallet, granted you can pass the provided accessToken.
    pub async fn fetch_user_wallet_data<T>(&self, token: T) -> Result<StdResp<ZBDUserWalletData>>
    where
        T: AsRef<str>,
    {
        //let mut token_header_string: String = "Bearer ".to_owned();
        //token_header_string.push_str(&bearer_token);

        let url = format!("{}/v1/oauth2/wallet", &self.domain);

        let resp = self
            .add_headers(self.reqw_cli.get(&url))
            .header("usertoken", token.as_ref())
            .send()
            .await?;

        self.parse_response(resp).await
    }
}

#[derive(Default, Clone, Validate, Deserialize, Debug)]
pub struct ZebedeeOauth {
    #[validate(length(equal = 36))]
    client_id: String,
    #[validate(length(equal = 36))]
    secret: String,
    #[validate(url)]
    redirect_uri: String,
    #[validate(length(equal = 36))]
    state: String,
    scope: String,
}

impl ZebedeeOauth {
    fn new(
        client_id: String,
        secret: String,
        redirect_uri: String,
        state: String,
        scope: String,
    ) -> Self {
        ZebedeeOauth {
            client_id,
            secret,
            redirect_uri,
            state,
            scope,
        }
    }
}

impl Default for ZebedeeClient {
    fn default() -> Self {
        ZebedeeClient {
            domain: String::from("https://api.zebedee.io"),
            reqw_cli: reqwest::Client::new(),
            apikey: String::from("errornotset"),
            oauth: Default::default(),
        }
    }
}

#[derive(Clone, Debug, Validate, Deserialize)]
pub struct PKCE {
    #[validate(length(equal = 43))]
    pub verifier: String,
    #[validate(length(equal = 43))]
    pub challenge: String,
}

impl PKCE {
    pub fn new(input: [u8; 32]) -> Self {
        let verifier = base64_url::encode(&input);

        let mut hasher_2 = Sha256::new();
        hasher_2.update(verifier.clone());
        let hash_result_2 = hasher_2.finalize();
        let challenge = base64_url::encode(&hash_result_2);

        let p = PKCE {
            verifier,
            challenge,
        };
        p.validate().unwrap();
        p
    }

    pub fn new_rand() -> Self {
        let random_bytes = rand::thread_rng().gen::<[u8; 32]>();
        Self::new(random_bytes)
    }
}

impl From<&str> for PKCE {
    fn from(value: &str) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(value);
        let hash_result: [u8; 32] = hasher
            .finalize()
            .as_slice()
            .try_into()
            .expect("hashing went wrong from string");
        Self::new(hash_result)
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct StdResp<T> {
    pub success: bool,
    pub data: T,
    pub message: Option<String>,
}