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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#[macro_use]
#[cfg(not(target_arch = "wasm32"))]
extern crate amplify;

use std::str::FromStr;

use anyhow::anyhow;
use anyhow::Result;
use bdk::{wallet::AddressIndex, FeeRate, LocalUtxo};
#[cfg(not(target_arch = "wasm32"))]
use bitcoin::consensus::serialize as serialize_psbt; // Desktop
use bitcoin::{util::address::Address, OutPoint, Transaction}; // Shared
use bitcoin_hashes::{sha256, Hash};
use serde::{Deserialize, Serialize};
use serde_encrypt::{
    serialize::impls::BincodeSerializer, shared_key::SharedKey, traits::SerdeEncryptSharedKey,
    AsSharedKey, EncryptedMessage,
};
use tokio::try_join;

pub mod data;
pub mod operations;
pub mod util;
#[cfg(target_arch = "wasm32")]
pub mod web;

// Desktop
#[cfg(not(target_arch = "wasm32"))]
pub use crate::{
    data::structs::{AcceptResponse, AssetResponse, FinalizeTransfer, TransfersResponse},
    operations::rgb::{
        self, blind_utxo, get_asset_by_genesis, get_assets, issue_asset, transfer_asset,
        validate_transfer,
    },
};
// Web
#[cfg(target_arch = "wasm32")]
pub use crate::{
    data::structs::{
        AcceptLambdaResponse, AcceptRequest, AssetRequest, BlindRequest, BlindResponse,
        IssueRequest,
    },
    util::post_json,
};
// Shared
pub use crate::{
    data::{
        constants::{get_endpoint, get_network, switch_host, switch_network},
        structs::{
            BlindingUtxo, EncryptedWalletData, FullUtxo, FundVaultDetails, SatsInvoice, ThinAsset,
            TransferResult, TransfersRequest, TransfersSerializeResponse, WalletData,
            WalletTransaction,
        },
    },
    operations::{
        bitcoin::{
            create_payjoin, create_transaction, dust_tx, get_wallet, new_mnemonic, save_mnemonic,
            sign_psbt, synchronize_wallet,
        },
        lightning,
    },
};

impl SerdeEncryptSharedKey for EncryptedWalletData {
    type S = BincodeSerializer<Self>; // you can specify serializer implementation (or implement it by yourself).
}

pub fn get_encrypted_wallet(
    password: &str,
    encrypted_descriptors: &str,
) -> Result<EncryptedWalletData> {
    // read hash digest and consume hasher
    let hash = sha256::Hash::hash(password.as_bytes());
    let shared_key: [u8; 32] = hash.into_inner();
    let encrypted_descriptors: Vec<u8> = hex::decode(encrypted_descriptors)?;
    let encrypted_message = EncryptedMessage::deserialize(encrypted_descriptors)?;
    Ok(EncryptedWalletData::decrypt_owned(
        &encrypted_message,
        &SharedKey::from_array(shared_key),
    )?)
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MnemonicSeedData {
    pub mnemonic: String,
    pub serialized_encrypted_message: String,
}

// TODO: should probably be called "new_mnemonic_seed"
pub fn get_mnemonic_seed(
    encryption_password: &str,
    seed_password: &str,
) -> Result<MnemonicSeedData> {
    let hash = sha256::Hash::hash(encryption_password.as_bytes());
    let shared_key: [u8; 32] = hash.into_inner();

    let encrypted_wallet_data = new_mnemonic(seed_password)?;
    let encrypted_message = encrypted_wallet_data.encrypt(&SharedKey::from_array(shared_key))?;
    let serialized_encrypted_message = hex::encode(encrypted_message.serialize());
    let mnemonic_seed_data = MnemonicSeedData {
        mnemonic: encrypted_wallet_data.mnemonic,
        serialized_encrypted_message,
    };

    Ok(mnemonic_seed_data)
}

pub fn save_mnemonic_seed(
    mnemonic_phrase: &str,
    encryption_password: &str,
    seed_password: &str,
) -> Result<MnemonicSeedData> {
    let hash = sha256::Hash::hash(encryption_password.as_bytes());
    let shared_key: [u8; 32] = hash.into_inner();

    let vault_data = save_mnemonic(mnemonic_phrase, seed_password)?;
    let encrypted_message = vault_data.encrypt(&SharedKey::from_array(shared_key))?;
    let serialized_encrypted_message = hex::encode(encrypted_message.serialize());
    let mnemonic_seed_data = MnemonicSeedData {
        mnemonic: vault_data.mnemonic,
        serialized_encrypted_message,
    };

    Ok(mnemonic_seed_data)
}

pub async fn get_wallet_data(
    descriptor: &str,
    change_descriptor: Option<String>,
) -> Result<WalletData> {
    info!("get_wallet_data");
    info!("descriptor:", &descriptor);
    info!("change_descriptor:", format!("{:?}", &change_descriptor));

    let wallet = get_wallet(descriptor, change_descriptor)?;
    synchronize_wallet(&wallet).await?;
    let address = wallet.get_address(AddressIndex::LastUnused)?.to_string();
    info!(format!("address: {address}"));
    let balance = wallet.get_balance()?;
    info!(format!("balance: {balance:?}"));
    let utxos = wallet.list_unspent().unwrap_or_default();
    let utxos: Vec<String> = utxos.into_iter().map(|x| x.outpoint.to_string()).collect();
    trace!(format!("unspent: {utxos:#?}"));

    let mut transactions = wallet.list_transactions(false).unwrap_or_default();
    trace!(format!("transactions: {transactions:#?}"));

    transactions.sort_by(|a, b| {
        b.confirmation_time
            .as_ref()
            .map(|t| t.height)
            .cmp(&a.confirmation_time.as_ref().map(|t| t.height))
    });

    let transactions: Vec<WalletTransaction> = transactions
        .into_iter()
        .map(|tx| WalletTransaction {
            txid: tx.txid,
            received: tx.received,
            sent: tx.sent,
            fee: tx.fee,
            confirmed: tx.confirmation_time.is_some(),
            confirmation_time: tx.confirmation_time,
        })
        .collect();

    Ok(WalletData {
        address,
        balance,
        transactions,
        utxos,
    })
}

pub async fn get_new_address(
    descriptor: &str,
    change_descriptor: Option<String>,
) -> Result<String> {
    info!("get_new_address");
    info!(format!("descriptor: {descriptor}"));
    info!(format!("change_descriptor: {change_descriptor:?}"));

    let wallet = get_wallet(descriptor, change_descriptor)?;
    synchronize_wallet(&wallet).await?;
    let address = wallet.get_address(AddressIndex::New)?.to_string();
    info!(format!("address: {address}"));
    Ok(address)
}

#[cfg(not(target_arch = "wasm32"))]
pub fn list_assets(contract: &str) -> Result<Vec<AssetResponse>> {
    info!("list_assets");
    let assets = get_assets(contract)?;
    info!(format!("get assets: {assets:#?}"));
    Ok(assets)
}

// TODO: web list_assets

#[derive(Serialize, Deserialize)]
pub struct CreateAssetResult {
    pub genesis: String,   // in bech32m encoding
    pub id: String,        // contract ID
    pub asset_id: String,  // asset ID
    pub schema_id: String, // schema ID (i.e., RGB20)
}

#[cfg(not(target_arch = "wasm32"))]
pub fn create_asset(
    ticker: &str,
    name: &str,
    precision: u8,
    supply: u64,
    utxo: &str,
) -> Result<CreateAssetResult> {
    let utxo = OutPoint::from_str(utxo)?;
    let contract = issue_asset(ticker, name, precision, supply, utxo)?;
    let genesis = contract.to_string();
    let id = contract.id().to_string();
    let asset_id = contract.contract_id().to_string();
    let schema_id = contract.schema_id().to_string();

    Ok(CreateAssetResult {
        genesis,
        id,
        asset_id,
        schema_id,
    })
}

#[cfg(target_arch = "wasm32")]
pub async fn create_asset(
    ticker: &str,
    name: &str,
    precision: u8,
    supply: u64,
    utxo: &str,
) -> Result<CreateAssetResult> {
    let endpoint = &get_endpoint("issue").await;
    let body = IssueRequest {
        ticker: ticker.to_owned(),
        name: name.to_owned(),
        description: "TODO".to_owned(),
        precision,
        supply,
        utxo: utxo.to_owned(),
    };
    let (issue_res, status) = post_json(endpoint, &body).await?;
    if status != 200 {
        return Err(anyhow!("Error calling {endpoint}"));
    }
    let CreateAssetResult {
        genesis,
        id,
        asset_id,
        schema_id,
    } = serde_json::from_str(&issue_res)?;

    Ok(CreateAssetResult {
        genesis,
        id,
        asset_id,
        schema_id,
    })
}

pub async fn get_utxos(
    descriptor: &str,
    change_descriptor: Option<String>,
) -> Result<Vec<LocalUtxo>> {
    let rgb_wallet = get_wallet(descriptor, change_descriptor)?;
    synchronize_wallet(&rgb_wallet).await?;
    let utxos = rgb_wallet.list_unspent()?;

    Ok(utxos)
}

pub fn parse_outpoints(outpoints: Vec<String>) -> Result<Vec<OutPoint>> {
    outpoints
        .into_iter()
        .map(|outpoint| OutPoint::from_str(&outpoint).map_err(|e| anyhow!(e)))
        .collect()
}

pub fn utxos_to_outpoints(utxos: Vec<LocalUtxo>) -> Vec<String> {
    utxos
        .into_iter()
        .map(|utxo| utxo.outpoint.to_string())
        .collect()
}

#[cfg(not(target_arch = "wasm32"))]
pub fn import_asset(asset: &str, utxos: Vec<String>) -> Result<ThinAsset> {
    let utxos = parse_outpoints(utxos)?;

    match asset.as_bytes() {
        #[allow(unreachable_code)]
        [b'r', b'g', b'b', b'1', ..] => Ok(todo!(
            "asset persistence for asset_import not yet implemented"
        )),
        [b'r', b'g', b'b', b'c', b'1', ..] => {
            info!("Getting asset by contract genesis:", asset);
            get_asset_by_genesis(asset, &utxos)
        }
        _ => Err(anyhow!("Asset did not match expected format")),
    }
}

#[cfg(target_arch = "wasm32")]
pub async fn import_asset(asset: &str, utxo: &str) -> Result<ThinAsset> {
    info!("Getting asset:", asset);

    let endpoint = &get_endpoint("import").await;
    let body = AssetRequest {
        asset: asset.to_owned(),
        utxos: vec![utxo.to_owned()],
    };
    let (asset_res, status) = post_json(endpoint, &body).await?;
    if status != 200 {
        return Err(anyhow!("Error calling {endpoint}"));
    }
    let ThinAsset {
        id,
        ticker,
        name,
        description,
        allocations,
        balance,
        genesis,
    } = serde_json::from_str(&asset_res)?;

    Ok(ThinAsset {
        id,
        ticker,
        name,
        description,
        allocations,
        balance,
        genesis,
    })
}

#[derive(Serialize, Deserialize)]
struct TransactionData {
    blinding: String,
    utxo: OutPoint,
}

#[cfg(not(target_arch = "wasm32"))]
pub fn get_blinded_utxo(utxo_string: &Option<String>) -> Result<BlindingUtxo> {
    if let Some(utxo_string) = utxo_string {
        let utxo = OutPoint::from_str(utxo_string)?;

        let blind = blind_utxo(utxo)?;

        let blinding_utxo = BlindingUtxo {
            conceal: blind.conceal,
            blinding: blind.blinding,
            utxo,
        };

        Ok(blinding_utxo)
    } else {
        Err(anyhow!("No utxo string passed"))
    }
}

#[cfg(target_arch = "wasm32")]
pub async fn get_blinded_utxo(utxo_string: &str) -> Result<BlindingUtxo> {
    let utxo = OutPoint::from_str(utxo_string)?;

    let endpoint = &get_endpoint("blind").await;
    let body = BlindRequest {
        utxo: utxo.to_string(),
    };
    let (blind_res, status) = post_json(endpoint, &body).await?;
    if status != 200 {
        return Err(anyhow!("Error calling {endpoint}"));
    }
    let BlindResponse { conceal, blinding } = serde_json::from_str(&blind_res)?;
    let blinding_utxo = BlindingUtxo {
        conceal,
        blinding,
        utxo,
    };

    Ok(blinding_utxo)
}

pub async fn send_sats(
    descriptor: &str,
    change_descriptor: &str,
    destination: &str, // bip21 uri or address
    amount: u64,
    fee_rate: Option<f32>,
) -> Result<Transaction> {
    use payjoin::UriExt;

    let wallet = get_wallet(descriptor, Some(change_descriptor.to_owned()))?;
    synchronize_wallet(&wallet).await?;

    let fee_rate = fee_rate.map(FeeRate::from_sat_per_vb);

    let transaction = match payjoin::Uri::try_from(destination) {
        Ok(uri) => {
            let address = uri.address.clone();
            if let Ok(pj_uri) = uri.check_pj_supported() {
                create_payjoin(
                    vec![SatsInvoice { address, amount }],
                    &wallet,
                    fee_rate,
                    pj_uri,
                )
                .await?
            } else {
                create_transaction(vec![SatsInvoice { address, amount }], &wallet, fee_rate).await?
            }
        }
        _ => {
            let address = Address::from_str(destination)?;
            create_transaction(vec![SatsInvoice { address, amount }], &wallet, fee_rate).await?
        }
    };

    Ok(transaction)
}

pub async fn fund_vault(
    btc_descriptor_xprv: &str,
    btc_change_descriptor_xprv: &str,
    assets_address: &str,
    uda_address: &str,
    asset_amount: u64,
    uda_amount: u64,
    fee_rate: Option<f32>,
) -> Result<FundVaultDetails> {
    let assets_address = Address::from_str(assets_address)?;
    let uda_address = Address::from_str(uda_address)?;

    let wallet = get_wallet(
        btc_descriptor_xprv,
        Some(btc_change_descriptor_xprv.to_owned()),
    )?;
    synchronize_wallet(&wallet).await?;

    let asset_invoice = SatsInvoice {
        address: assets_address,
        amount: asset_amount,
    };
    let uda_invoice = SatsInvoice {
        address: uda_address,
        amount: uda_amount,
    };

    let fee_rate = fee_rate.map(FeeRate::from_sat_per_vb);

    let asset_tx_details = create_transaction(
        vec![asset_invoice.clone(), asset_invoice],
        &wallet,
        fee_rate,
    )
    .await?;

    let uda_tx_details =
        create_transaction(vec![uda_invoice.clone(), uda_invoice], &wallet, fee_rate).await?;

    let asset_txid = asset_tx_details.txid();
    let asset_outputs: Vec<String> = asset_tx_details
        .output
        .iter()
        .enumerate()
        .map(|(i, _)| format!("{asset_txid}:{i}"))
        .collect();

    let uda_txid = uda_tx_details.txid();
    let uda_outputs: Vec<String> = uda_tx_details
        .output
        .iter()
        .enumerate()
        .map(|(i, _)| format!("{uda_txid}:{i}"))
        .collect();

    Ok(FundVaultDetails {
        assets_output: Some(asset_outputs[0].to_owned()),
        assets_change_output: Some(asset_outputs[1].to_owned()),
        udas_output: Some(uda_outputs[0].to_owned()),
        udas_change_output: Some(uda_outputs[1].to_owned()),
    })
}

fn utxo_string(utxo: &LocalUtxo) -> String {
    utxo.outpoint.to_string()
}

pub async fn get_assets_vault(
    rgb_assets_descriptor_xpub: &str,
    rgb_udas_descriptor_xpub: &str,
) -> Result<FundVaultDetails> {
    let assets_wallet = get_wallet(rgb_assets_descriptor_xpub, None)?;
    let udas_wallet = get_wallet(rgb_udas_descriptor_xpub, None)?;

    try_join!(
        synchronize_wallet(&assets_wallet),
        synchronize_wallet(&udas_wallet)
    )?;

    let assets_utxos = assets_wallet.list_unspent()?;
    let uda_utxos = udas_wallet.list_unspent()?;

    debug!(format!("Asset UTXOs: {assets_utxos:#?}"));
    debug!(format!("UDA UTXOs: {uda_utxos:#?}"));

    let mut assets_utxos: Vec<String> = assets_utxos.iter().map(utxo_string).collect();
    assets_utxos.sort();

    let mut uda_utxos: Vec<String> = uda_utxos.iter().map(utxo_string).collect();
    uda_utxos.sort();

    let assets_change_output = assets_utxos.pop();
    let assets_output = assets_utxos.pop();
    let udas_change_output = uda_utxos.pop();
    let udas_output = uda_utxos.pop();

    Ok(FundVaultDetails {
        assets_output,
        assets_change_output,
        udas_output,
        udas_change_output,
    })
}

#[cfg(not(target_arch = "wasm32"))]
pub async fn transfer_assets(transfers: TransfersRequest) -> Result<TransfersSerializeResponse> {
    use rgb_std::{Contract, StateTransfer};
    use strict_encoding::strict_serialize;

    use crate::data::structs::{BlindedOrNotOutpoint, ChangeTansfer, DeclareRequest};

    let resp = transfer_asset(transfers).await?;

    let psbt = serialize_psbt(&resp.psbt);
    let psbt = base64::encode(&psbt);
    let disclosure = serde_json::to_string(&resp.disclosure)?;

    let mut transfers = vec![];
    let mut change_transfers = vec![];

    // Retrieve consignment information + transition state
    let state_transfers: Vec<StateTransfer> =
        resp.transfers.clone().into_iter().map(|(f, _)| f).collect();

    for (index, asset_transfer_info) in resp.transaction_info.iter().enumerate() {
        let consig = Contract::from_str(&asset_transfer_info.consignment.clone())?;
        let state_transfer = state_transfers
            .clone()
            .into_iter()
            .find(|st| st.contract_id() == consig.contract_id());

        let state_serialize = strict_serialize(&state_transfer.unwrap())
            .expect("Consignment information must be valid");
        let state_serialize = util::bech32m_zip_encode("rgbc", &state_serialize)
            .expect("Strict encoded information must be a valid consignment");

        transfers.push(FinalizeTransfer {
            consignment: state_serialize,
            asset: asset_transfer_info.asset_contract.clone(),
            beneficiaries: asset_transfer_info
                .beneficiaries
                .iter()
                .map(|info| {
                    let parts: Vec<&str> = info.split('@').collect();
                    let balance = parts[0].parse::<u64>().unwrap();
                    let outpoint = parts[1].to_string();
                    BlindedOrNotOutpoint { outpoint, balance }
                })
                .collect(),
            previous_utxo: resp.origin[index].outpoint.to_string(),
        });
        change_transfers.push(ChangeTansfer {
            asset: asset_transfer_info.asset_contract.clone(),
            change: BlindedOrNotOutpoint {
                outpoint: asset_transfer_info.change_utxo.clone(),
                balance: asset_transfer_info.change,
            },
            previous_utxo: resp.origin[index].outpoint.to_string(),
        });
    }

    Ok(TransfersSerializeResponse {
        psbt,
        declare: DeclareRequest {
            change_transfers,
            transfers,
            disclosure,
        },
    })
}

#[cfg(target_arch = "wasm32")]
pub async fn transfer_assets(transfers: TransfersRequest) -> Result<TransfersSerializeResponse> {
    let endpoint = &get_endpoint("send").await;
    let body = transfers;

    let (transfer_res, status) = post_json(endpoint, &body).await?;
    if status != 200 {
        return Err(anyhow!("Error calling {endpoint}"));
    }

    let transfer_res: TransfersSerializeResponse = serde_json::from_str(&transfer_res)?;

    let _declare = async {
        let declare_endpoint = &get_endpoint("declare").await;
        let (_, status) = post_json(declare_endpoint, &transfer_res.declare).await?;
        if status != 200 {
            return Err(anyhow!("Error calling {declare_endpoint}"));
        }
        Ok(status)
    }
    .await?;

    Ok(transfer_res)
}

#[cfg(not(target_arch = "wasm32"))]
pub async fn accept_transfer(
    consignment: &str,
    blinding_factor: &str,
    outpoint: &str,
) -> Result<AcceptResponse> {
    let (id, info, valid) = rgb::accept_transfer(consignment, blinding_factor, outpoint).await?;
    if valid {
        info!("Transaction accepted");
        Ok(AcceptResponse { id, info, valid })
    } else {
        Err(anyhow!("Incorrect seals. id: {} stratus: {}", id, info))
    }
}

#[cfg(target_arch = "wasm32")]
pub async fn sign_psbt_web(rgb_descriptor_xprv: &str, psbt: &str) -> Result<Transaction> {
    use bitcoin::psbt::PartiallySignedTransaction;

    let wallet = get_wallet(rgb_descriptor_xprv, None)?;
    synchronize_wallet(&wallet).await?;

    let psbt = PartiallySignedTransaction::from_str(psbt)?;
    let transaction = sign_psbt(&wallet, psbt).await?;

    Ok(transaction)
}

#[cfg(target_arch = "wasm32")]
pub async fn accept_transfer(
    consignment: &str,
    blinding_factor: &str,
    outpoint: &str,
    blinded: &str,
) -> Result<AcceptLambdaResponse> {
    let endpoint = &get_endpoint("accept").await;
    let body = AcceptRequest {
        consignment: consignment.to_owned(),
        blinding_factor: blinding_factor.to_owned(),
        outpoint: outpoint.to_owned(),
        blinded: blinded.to_owned(),
    };
    let (transfer_res, status) = post_json(endpoint, &body).await?;
    if status != 200 {
        return Err(anyhow!("Error calling {endpoint}"));
    }
    Ok(serde_json::from_str(&transfer_res)?)
}