kaspa_wallet_core/wasm/api/
message.rs

1#![allow(non_snake_case)]
2
3use super::extensions::*;
4use crate::account::descriptor::IAccountDescriptor;
5use crate::api::message::*;
6use crate::imports::*;
7use crate::tx::{Fees, PaymentDestination, PaymentOutputs};
8use crate::wasm::tx::fees::IFees;
9use crate::wasm::tx::GeneratorSummary;
10use js_sys::Array;
11use serde_wasm_bindgen::from_value;
12use workflow_wasm::serde::to_value;
13
14use kaspa_wallet_macros::declare_typescript_wasm_interface as declare;
15
16macro_rules! try_from {
17    ($name:ident : $from_type:ty, $to_type:ty, $body:block) => {
18        impl TryFrom<$from_type> for $to_type {
19            type Error = Error;
20            fn try_from($name: $from_type) -> Result<Self> {
21                $body
22            }
23        }
24    };
25}
26
27#[wasm_bindgen(typescript_custom_section)]
28const TS_CATEGORY_WALLET: &'static str = r#"
29/**
30 * @categoryDescription Wallet API
31 * Wallet API for interfacing with Rusty Kaspa Wallet implementation.
32 */
33"#;
34
35// ---
36
37// declare! {
38//     IPingRequest,
39//     r#"
40//     /**
41//      *
42//      */
43//     export interface IPingRequest {
44//         message?: string;
45//     }
46//     "#,
47// }
48
49// try_from! ( args: IPingRequest, PingRequest, {
50//     let message = args.try_get_string("message")?;
51//     Ok(PingRequest { message })
52// });
53
54// declare! {
55//     IPingResponse,
56//     r#"
57//     /**
58//      *
59//      */
60//     export interface IPingResponse {
61//         message?: string;
62//     }
63//     "#,
64// }
65
66// try_from! ( args: PingResponse, IPingResponse, {
67//     let response = IPingResponse::default();
68//     if let Some(message) = args.message {
69//         response.set("message", &JsValue::from_str(&message))?;
70//     }
71//     Ok(response)
72// });
73
74// ---
75
76declare! {
77    IBatchRequest,
78    r#"
79    /**
80     * Suspend storage operations until invocation of flush().
81     * 
82     * @category Wallet API
83     */
84    export interface IBatchRequest { }
85    "#,
86}
87
88try_from! ( _args: IBatchRequest, BatchRequest, {
89    Ok(BatchRequest { })
90});
91
92declare! {
93    IBatchResponse,
94    r#"
95    /**
96     * 
97     * 
98     * @category Wallet API
99     */
100    export interface IBatchResponse { }
101    "#,
102}
103
104try_from! ( _args: BatchResponse, IBatchResponse, {
105    Ok(IBatchResponse::default())
106});
107
108// ---
109
110declare! {
111    IFlushRequest,
112    r#"
113    /**
114     * 
115     *  
116     * @category Wallet API
117     */
118    export interface IFlushRequest {
119        walletSecret : string;
120    }
121    "#,
122}
123
124try_from! ( args: IFlushRequest, FlushRequest, {
125    let wallet_secret = args.get_secret("walletSecret")?;
126    Ok(FlushRequest { wallet_secret })
127});
128
129declare! {
130    IFlushResponse,
131    r#"
132    /**
133     * 
134     *  
135     * @category Wallet API
136     */
137    export interface IFlushResponse { }
138    "#,
139}
140
141try_from! ( _args: FlushResponse, IFlushResponse, {
142    Ok(IFlushResponse::default())
143});
144
145// ---
146
147declare! {
148    IConnectRequest,
149    r#"
150    /**
151     * 
152     *  
153     * @category Wallet API
154     */
155    export interface IConnectRequest {
156        // destination wRPC node URL (if omitted, the resolver is used)
157        url? : string;
158        // network identifier
159        networkId : NetworkId | string;
160        // retry on error
161        retryOnError? : boolean;
162        // block async connect (method will not return until the connection is established)
163        block? : boolean;
164        // require node to be synced (fail otherwise)
165        requireSync? : boolean;
166    }
167    "#,
168}
169
170try_from! ( args: IConnectRequest, ConnectRequest, {
171    let url = args.try_get_string("url")?;
172    let network_id = args.get_network_id("networkId")?;
173    let retry_on_error = args.try_get_bool("retryOnError")?.unwrap_or(true);
174    let block_async_connect = args.try_get_bool("block")?.unwrap_or(false);
175    let require_sync = args.try_get_bool("requireSync")?.unwrap_or(true);
176    Ok(ConnectRequest { url, network_id, retry_on_error, block_async_connect, require_sync })
177});
178
179declare! {
180    IConnectResponse,
181    r#"
182    /**
183     * 
184     *  
185     * @category Wallet API
186     */
187    export interface IConnectResponse { }
188    "#,
189}
190
191try_from! ( _args: ConnectResponse, IConnectResponse, {
192    Ok(IConnectResponse::default())
193});
194
195// ---
196
197declare! {
198    IDisconnectRequest,
199    r#"
200    /**
201     * 
202     *  
203     * @category Wallet API
204     */
205    export interface IDisconnectRequest { }
206    "#,
207}
208
209try_from! ( _args: IDisconnectRequest, DisconnectRequest, {
210    Ok(DisconnectRequest { })
211});
212
213declare! {
214    IDisconnectResponse,
215    r#"
216    /**
217     * 
218     *  
219     * @category Wallet API
220     */
221    export interface IDisconnectResponse { }
222    "#,
223}
224
225try_from! ( _args: DisconnectResponse, IDisconnectResponse, {
226    Ok(IDisconnectResponse::default())
227});
228
229// ---
230
231declare! {
232    IGetStatusRequest,
233    r#"
234    /**
235     * 
236     *  
237     * @category Wallet API
238     */
239    export interface IGetStatusRequest {
240        /**
241         * Optional context creation name.
242         * @see {@link IRetainContextRequest}
243         */
244        name? : string;
245    }
246    "#,
247}
248
249try_from! ( args: IGetStatusRequest, GetStatusRequest, {
250    let name = args.try_get_string("name")?;
251    Ok(GetStatusRequest { name })
252});
253
254declare! {
255    IGetStatusResponse,
256    r#"
257    /**
258     * 
259     *  
260     * @category Wallet API
261     */
262    export interface IGetStatusResponse {
263        isConnected : boolean;
264        isSynced : boolean;
265        isOpen : boolean;
266        url? : string;
267        networkId? : NetworkId;
268        context? : HexString;
269    }
270    "#,
271}
272
273try_from! ( args: GetStatusResponse, IGetStatusResponse, {
274    let GetStatusResponse { is_connected, is_synced, is_open, url, network_id, .. } = args;
275    let response = IGetStatusResponse::default();
276    response.set("isConnected", &is_connected.into())?;
277    response.set("isSynced", &is_synced.into())?;
278    response.set("isOpen", &is_open.into())?;
279    if let Some(url) = url {
280        response.set("url", &url.into())?;
281    }
282    if let Some(network_id) = network_id {
283        response.set("networkId", &network_id.into())?;
284    }
285    Ok(response)
286});
287
288// ---
289
290declare! {
291    IRetainContextRequest,
292    r#"
293    /**
294     * 
295     *  
296     * @category Wallet API
297     */
298    export interface IRetainContextRequest {
299        /**
300         * Optional context creation name.
301         */
302        name : string;
303        /**
304         * Optional context data to retain.
305         */
306        data? : string;
307    }
308    "#,
309}
310
311try_from! ( args: IRetainContextRequest, RetainContextRequest, {
312    let name = args.get_string("name")?;
313    let data = args.try_get_string("data")?;
314    let data = data.map(|data|Vec::<u8>::from_hex(data.as_str())).transpose()?;
315    Ok(RetainContextRequest { name, data })
316});
317
318declare! {
319    IRetainContextResponse,
320    r#"
321    /**
322     * 
323     *  
324     * @category Wallet API
325     */
326    export interface IRetainContextResponse {
327    }
328    "#,
329}
330
331try_from! ( _args: RetainContextResponse, IRetainContextResponse, {
332    Ok(IRetainContextResponse::default())
333});
334
335// ---
336
337declare! {
338    IWalletEnumerateRequest,
339    r#"
340    /**
341     * 
342     *  
343     * @category Wallet API
344     */
345    export interface IWalletEnumerateRequest { }
346    "#,
347}
348
349try_from! ( _args: IWalletEnumerateRequest, WalletEnumerateRequest, {
350    Ok(WalletEnumerateRequest { })
351});
352
353declare! {
354    IWalletEnumerateResponse,
355    r#"
356    /**
357     * 
358     *  
359     * @category Wallet API
360     */
361    export interface IWalletEnumerateResponse {
362        walletDescriptors: WalletDescriptor[];
363    }
364    "#,
365}
366
367try_from! ( args: WalletEnumerateResponse, IWalletEnumerateResponse, {
368    let response = IWalletEnumerateResponse::default();
369    let wallet_descriptors = Array::from_iter(args.wallet_descriptors.into_iter().map(JsValue::from));
370    response.set("walletDescriptors", &JsValue::from(&wallet_descriptors))?;
371    Ok(response)
372});
373
374// ---
375
376declare! {
377    IWalletCreateRequest,
378    r#"
379    /**
380     * 
381     * If filename is not supplied, the filename will be derived from the wallet title.
382     * If both wallet title and filename are not supplied, the wallet will be create
383     * with the default filename `kaspa`.
384     * 
385     * @category Wallet API
386     */
387    export interface IWalletCreateRequest {
388        /** Wallet encryption secret */
389        walletSecret: string;
390        /** Optional wallet title */
391        title?: string;
392        /** Optional wallet filename */
393        filename?: string;
394        /** Optional user hint */
395        userHint?: string;
396        /** 
397         * Overwrite wallet data if the wallet with the same filename already exists.
398         * (Use with caution!)
399         */
400        overwriteWalletStorage?: boolean;
401    }
402    "#,
403}
404
405// TODO
406try_from! ( args: IWalletCreateRequest, WalletCreateRequest, {
407
408    let wallet_secret = args.get_secret("walletSecret")?;
409    let title = args.try_get_string("title")?;
410    let filename = args.try_get_string("filename")?;
411    let user_hint = args.try_get_string("userHint")?.map(Hint::from);
412    let encryption_kind = EncryptionKind::default();
413    let overwrite_wallet_storage = args.try_get_bool("overwriteWalletStorage")?.unwrap_or(false);
414
415    let wallet_args = WalletCreateArgs {
416        title,
417        filename,
418        user_hint,
419        encryption_kind,
420        overwrite_wallet_storage,
421    };
422
423    Ok(WalletCreateRequest { wallet_secret, wallet_args })
424});
425
426declare! {
427    IWalletCreateResponse,
428    r#"
429    /**
430     * 
431     *  
432     * @category Wallet API
433     */
434    export interface IWalletCreateResponse {
435        walletDescriptor: IWalletDescriptor;
436        storageDescriptor: IStorageDescriptor;
437    }
438    "#,
439}
440
441try_from! ( args: WalletCreateResponse, IWalletCreateResponse, {
442    Ok(to_value(&args)?.into())
443});
444
445// ---
446
447// ---
448// NOTE: `legacy_accounts` are disabled in JS API
449declare! {
450    IWalletOpenRequest,
451    r#"
452    /**
453     * 
454     * @category Wallet API
455     */
456    export interface IWalletOpenRequest {
457        walletSecret: string;
458        filename?: string;
459        accountDescriptors: boolean;
460    }
461    "#,
462}
463
464try_from! ( args: IWalletOpenRequest, WalletOpenRequest, {
465    let wallet_secret = args.get_secret("walletSecret")?;
466    let filename = args.try_get_string("filename")?;
467    let account_descriptors = args.get_value("accountDescriptors")?.as_bool().unwrap_or(false);
468
469    Ok(WalletOpenRequest { wallet_secret, filename, account_descriptors, legacy_accounts: None })
470});
471
472declare! {
473    IWalletOpenResponse,
474    r#"
475    /**
476     * 
477     * 
478     * @category Wallet API
479     */
480    export interface IWalletOpenResponse {
481        accountDescriptors: IAccountDescriptor[];
482    }
483    "#  
484}
485
486try_from!(args: WalletOpenResponse, IWalletOpenResponse, {
487    let response = IWalletOpenResponse::default();
488    if let Some(account_descriptors) = args.account_descriptors {
489        let account_descriptors = account_descriptors.into_iter().map(IAccountDescriptor::try_from).collect::<Result<Vec<IAccountDescriptor>>>()?;
490        response.set("accountDescriptors", &Array::from_iter(account_descriptors.into_iter()))?;
491    }
492    Ok(response)
493});
494
495// ---
496
497declare! {
498    IWalletCloseRequest,
499    r#"
500    /**
501     * 
502     *  
503     * @category Wallet API
504     */
505    export interface IWalletCloseRequest { }
506    "#,
507}
508
509try_from! ( _args: IWalletCloseRequest, WalletCloseRequest, {
510    Ok(WalletCloseRequest { })
511});
512
513declare! {
514    IWalletCloseResponse,
515    r#"
516    /**
517     * 
518     *  
519     * @category Wallet API
520     */
521    export interface IWalletCloseResponse { }
522    "#,
523}
524
525try_from! ( _args: WalletCloseResponse, IWalletCloseResponse, {
526    Ok(IWalletCloseResponse::default())
527});
528
529// ---
530
531declare! {
532    IWalletReloadRequest,
533    r#"
534    /**
535     * 
536     *  
537     * @category Wallet API
538     */
539    export interface IWalletReloadRequest {
540        /**
541         * Reactivate accounts that are active before the reload.
542         */
543        reactivate: boolean;
544    }
545    "#,
546}
547
548try_from! ( args: IWalletReloadRequest, WalletReloadRequest, {
549    let reactivate = args.get_bool("reactivate")?;
550    Ok(WalletReloadRequest { reactivate })
551});
552
553declare! {
554    IWalletReloadResponse,
555    r#"
556    /**
557     * 
558     *  
559     * @category Wallet API
560     */
561    export interface IWalletReloadResponse { }
562    "#,
563}
564
565try_from! ( _args: WalletReloadResponse, IWalletReloadResponse, {
566    Ok(IWalletReloadResponse::default())
567});
568
569// ---
570
571declare! {
572    IWalletChangeSecretRequest,
573    r#"
574    /**
575     * 
576     *  
577     * @category Wallet API
578     */
579    export interface IWalletChangeSecretRequest {
580        oldWalletSecret: string;
581        newWalletSecret: string;
582    }
583    "#,
584}
585
586try_from! ( args: IWalletChangeSecretRequest, WalletChangeSecretRequest, {
587    let old_wallet_secret = args.get_secret("oldWalletSecret")?;
588    let new_wallet_secret = args.get_secret("newWalletSecret")?;
589    Ok(WalletChangeSecretRequest { old_wallet_secret, new_wallet_secret })
590});
591
592declare! {
593    IWalletChangeSecretResponse,
594    r#"
595    /**
596     * 
597     *  
598     * @category Wallet API
599     */
600    export interface IWalletChangeSecretResponse { }
601    "#,
602}
603
604try_from! ( _args: WalletChangeSecretResponse, IWalletChangeSecretResponse, {
605    Ok(IWalletChangeSecretResponse::default())
606});
607
608// ---
609
610declare! {
611    IWalletExportRequest,
612    r#"
613    /**
614     * 
615     *  
616     * @category Wallet API
617     */
618    export interface IWalletExportRequest {
619        walletSecret: string;
620        includeTransactions: boolean;
621    }
622    "#,
623}
624
625try_from! ( args: IWalletExportRequest, WalletExportRequest, {
626    let wallet_secret = args.get_secret("walletSecret")?;
627    let include_transactions = args.get_bool("includeTransactions")?;
628    Ok(WalletExportRequest { wallet_secret, include_transactions })
629});
630
631declare! {
632    IWalletExportResponse,
633    r#"
634    /**
635     * 
636     *  
637     * @category Wallet API
638     */
639    export interface IWalletExportResponse {
640        walletData: HexString;
641    }
642    "#,
643}
644
645// TODO
646try_from! ( args: WalletExportResponse, IWalletExportResponse, {
647    let response = IWalletExportResponse::default();
648    response.set("walletData", &JsValue::from_str(&args.wallet_data.to_hex()))?;
649    Ok(response)
650});
651
652// ---
653
654declare! {
655    IWalletImportRequest,
656    r#"
657    /**
658     * 
659     *  
660     * @category Wallet API
661     */
662    export interface IWalletImportRequest {
663        walletSecret: string;
664        walletData: HexString | Uint8Array;
665    }
666    "#,
667}
668
669try_from! ( args: IWalletImportRequest, WalletImportRequest, {
670    let wallet_secret = args.get_secret("walletSecret")?;
671    let wallet_data = args.get_vec_u8("walletData").map_err(|err|Error::custom(format!("walletData: {err}")))?;
672    Ok(WalletImportRequest { wallet_secret, wallet_data })
673});
674
675declare! {
676    IWalletImportResponse,
677    r#"
678    /**
679     * 
680     *  
681     * @category Wallet API
682     */
683    export interface IWalletImportResponse { }
684    "#,
685}
686
687try_from! ( _args: WalletImportResponse, IWalletImportResponse, {
688    Ok(IWalletImportResponse::default())
689});
690
691// ---
692
693declare! {
694    IPrvKeyDataEnumerateRequest,
695    r#"
696    /**
697     * 
698     * 
699     * @category Wallet API
700     */
701    export interface IPrvKeyDataEnumerateRequest { }
702    "#,
703}
704
705try_from! ( _args: IPrvKeyDataEnumerateRequest, PrvKeyDataEnumerateRequest, {
706    Ok(PrvKeyDataEnumerateRequest { })
707});
708
709declare! {
710    IPrvKeyDataEnumerateResponse,
711    r#"
712    /**
713     * 
714     * Response returning a list of private key ids, their optional names and properties.
715     * 
716     * @see {@link IPrvKeyDataInfo}
717     * @category Wallet API
718     */
719    export interface IPrvKeyDataEnumerateResponse {
720        prvKeyDataList: IPrvKeyDataInfo[],
721    }
722    "#,
723}
724
725try_from! ( args: PrvKeyDataEnumerateResponse, IPrvKeyDataEnumerateResponse, {
726    Ok(to_value(&args)?.into())
727});
728
729// ---
730
731declare! {
732    IPrvKeyDataCreateRequest,
733    r#"
734    /**
735     * 
736     *  
737     * @category Wallet API
738     */
739    export interface IPrvKeyDataCreateRequest {
740        /** Wallet encryption secret */
741        walletSecret: string;
742        /** Optional name of the private key */
743        name? : string;
744        /** 
745         * Optional key secret (BIP39 passphrase).
746         * 
747         * If supplied, all operations requiring access 
748         * to the key will require the `paymentSecret` 
749         * to be provided.
750         */
751        paymentSecret? : string;
752        /** BIP39 mnemonic phrase (12 or 24 words)*/
753        mnemonic : string;
754    }
755    "#,
756}
757
758try_from! ( args: IPrvKeyDataCreateRequest, PrvKeyDataCreateRequest, {
759    let wallet_secret = args.get_secret("walletSecret")?;
760    let name = args.try_get_string("name")?;
761    let payment_secret = args.try_get_secret("paymentSecret")?;
762    let mnemonic = args.get_secret("mnemonic")?;
763
764    let prv_key_data_args = PrvKeyDataCreateArgs {
765        name,
766        payment_secret,
767        mnemonic,
768    };
769
770    Ok(PrvKeyDataCreateRequest { wallet_secret, prv_key_data_args })
771});
772
773// TODO
774declare! {
775    IPrvKeyDataCreateResponse,
776    r#"
777    /**
778     * 
779     *  
780     * @category Wallet API
781     */
782    export interface IPrvKeyDataCreateResponse {
783        prvKeyDataId: HexString;
784    }
785    "#,
786}
787
788try_from!(args: PrvKeyDataCreateResponse, IPrvKeyDataCreateResponse, {
789    Ok(to_value(&args)?.into())
790});
791
792// ---
793
794declare! {
795    IPrvKeyDataRemoveRequest,
796    r#"
797    /**
798     * 
799     *  
800     * @category Wallet API
801     */
802    export interface IPrvKeyDataRemoveRequest {
803        walletSecret: string;
804        prvKeyDataId: HexString;
805    }
806    "#,
807}
808
809try_from! ( args: IPrvKeyDataRemoveRequest, PrvKeyDataRemoveRequest, {
810    let wallet_secret = args.get_secret("walletSecret")?;
811    let prv_key_data_id = args.get_prv_key_data_id("prvKeyDataId")?;
812    Ok(PrvKeyDataRemoveRequest { wallet_secret, prv_key_data_id })
813});
814
815declare! {
816    IPrvKeyDataRemoveResponse,
817    r#"
818    /**
819     * 
820     *  
821     * @category Wallet API
822     */
823    export interface IPrvKeyDataRemoveResponse { }
824    "#,
825}
826
827// TODO
828try_from! ( _args: PrvKeyDataRemoveResponse, IPrvKeyDataRemoveResponse, {
829    Ok(IPrvKeyDataRemoveResponse::default())
830});
831
832// ---
833
834declare! {
835    IPrvKeyDataGetRequest,
836    r#"
837    /**
838     * 
839     *  
840     * @category Wallet API
841     */
842    export interface IPrvKeyDataGetRequest {
843        walletSecret: string;
844        prvKeyDataId: HexString;
845    }
846    "#,
847}
848
849try_from! ( args: IPrvKeyDataGetRequest, PrvKeyDataGetRequest, {
850    let wallet_secret = args.get_secret("walletSecret")?;
851    let prv_key_data_id = args.get_prv_key_data_id("prvKeyDataId")?;
852    Ok(PrvKeyDataGetRequest { wallet_secret, prv_key_data_id })
853});
854
855declare! {
856    IPrvKeyDataGetResponse,
857    r#"
858    /**
859     * 
860     *  
861     * @category Wallet API
862     */
863    export interface IPrvKeyDataGetResponse {
864        // prvKeyData: PrvKeyData,
865    }
866    "#,
867}
868
869// TODO
870try_from! ( _args: PrvKeyDataGetResponse, IPrvKeyDataGetResponse, {
871    todo!();
872    // let response = IPrvKeyDataGetResponse::default();
873    // Ok(response)
874});
875
876// ---
877
878declare! {
879    IAccountsEnumerateRequest,
880    r#"
881    /**
882     * 
883     * 
884     * @category Wallet API
885     */
886    export interface IAccountsEnumerateRequest { }
887    "#,
888}
889
890try_from!(_args: IAccountsEnumerateRequest, AccountsEnumerateRequest, {
891    Ok(AccountsEnumerateRequest { })
892});
893
894declare! {
895    IAccountsEnumerateResponse,
896    r#"
897    /**
898     * 
899     *  
900     * @category Wallet API
901     */
902    export interface IAccountsEnumerateResponse {
903        accountDescriptors: IAccountDescriptor[];
904    }
905    "#,
906}
907
908// TODO
909try_from! ( args: AccountsEnumerateResponse, IAccountsEnumerateResponse, {
910    let response = IAccountsEnumerateResponse::default();
911    let account_descriptors = args.account_descriptors.into_iter().map(IAccountDescriptor::try_from).collect::<Result<Vec<IAccountDescriptor>>>()?;
912    response.set("accountDescriptors", &Array::from_iter(account_descriptors.into_iter()))?;
913    Ok(response)
914});
915
916// ---
917
918declare! {
919    IAccountsRenameRequest,
920    r#"
921    /**
922     * 
923     *  
924     * @category Wallet API
925     */
926    export interface IAccountsRenameRequest {
927        accountId: string;
928        name?: string;
929        walletSecret: string;
930    }
931    "#,
932}
933
934try_from! ( args: IAccountsRenameRequest, AccountsRenameRequest, {
935    let account_id = args.get_account_id("accountId")?;
936    let name = args.try_get_string("name")?;
937    let wallet_secret = args.get_secret("walletSecret")?;
938    Ok(AccountsRenameRequest { account_id, name, wallet_secret })
939});
940
941declare! {
942    IAccountsRenameResponse,
943    r#"
944    /**
945     * 
946     *  
947     * @category Wallet API
948     */
949    export interface IAccountsRenameResponse { }
950    "#,
951}
952
953try_from! ( _args: AccountsRenameResponse, IAccountsRenameResponse, {
954    Ok(IAccountsRenameResponse::default())
955});
956
957// ---
958
959// TODO
960declare! {
961    IAccountsDiscoveryRequest,
962    r#"
963    /**
964     * 
965     *  
966     * @category Wallet API
967     */
968    export interface IAccountsDiscoveryRequest {
969        discoveryKind: AccountsDiscoveryKind,
970        accountScanExtent: number,
971        addressScanExtent: number,
972        bip39_passphrase?: string,
973        bip39_mnemonic: string,
974    }
975    "#,
976}
977
978// TODO
979try_from! (args: IAccountsDiscoveryRequest, AccountsDiscoveryRequest, {
980
981    let discovery_kind = args.get_value("discoveryKind")?;
982    let discovery_kind = if let Some(discovery_kind) = discovery_kind.as_string() {
983        discovery_kind.parse()?
984    } else {
985        AccountsDiscoveryKind::try_enum_from(&discovery_kind)?
986    };
987    let account_scan_extent = args.get_u32("accountScanExtent")?;
988    let address_scan_extent = args.get_u32("addressScanExtent")?;
989    let bip39_passphrase = args.try_get_secret("bip39_passphrase")?;
990    let bip39_mnemonic = args.get_secret("bip39_mnemonic")?;
991
992    Ok(AccountsDiscoveryRequest {
993        discovery_kind,
994        account_scan_extent,
995        address_scan_extent,
996        bip39_passphrase,
997        bip39_mnemonic,
998    })
999});
1000
1001declare! {
1002    IAccountsDiscoveryResponse,
1003    r#"
1004    /**
1005     * 
1006     *  
1007     * @category Wallet API
1008     */
1009    export interface IAccountsDiscoveryResponse {
1010        lastAccountIndexFound : number;
1011    }
1012    "#,
1013}
1014
1015try_from! ( args: AccountsDiscoveryResponse, IAccountsDiscoveryResponse, {
1016    Ok(to_value(&args)?.into())
1017});
1018
1019// ---
1020
1021declare! {
1022    IAccountsCreateRequest,
1023    r#"
1024    /**
1025     * 
1026     *  
1027     * @category Wallet API
1028     */
1029    export type IAccountsCreateRequest = {
1030        walletSecret: string;
1031        type: "bip32";
1032        accountName:string;
1033        accountIndex?:number;
1034        prvKeyDataId:string;
1035        paymentSecret?:string;
1036    };
1037    //   |{
1038    //     walletSecret: string;
1039    //     type: "multisig";
1040    //     accountName:string;
1041    //     accountIndex?:number;
1042    //     prvKeyDataId:string;
1043    //     pubkeys:HexString[];
1044    //     paymentSecret?:string;
1045    //   }
1046
1047    //   |{
1048    //     walletSecret: string;
1049    //     type: "bip32-readonly";
1050    //     accountName:string;
1051    //     accountIndex?:number;
1052    //     pubkey:HexString;
1053    //     paymentSecret?:string;
1054    //  }
1055    "#,
1056}
1057
1058try_from! (args: IAccountsCreateRequest, AccountsCreateRequest, {
1059    let wallet_secret = args.get_secret("walletSecret")?;
1060
1061    let kind = AccountKind::try_from(args.try_get_value("type")?.ok_or(Error::custom("type is required"))?)?;
1062
1063    if kind != crate::account::BIP32_ACCOUNT_KIND {
1064        return Err(Error::custom("only BIP32 accounts are currently supported"));
1065    }
1066
1067    let prv_key_data_args = PrvKeyDataArgs {
1068        prv_key_data_id: args.try_get_prv_key_data_id("prvKeyDataId")?.ok_or(Error::custom("prvKeyDataId is required"))?,
1069        payment_secret: args.try_get_secret("paymentSecret")?,
1070    };
1071
1072    let account_args = AccountCreateArgsBip32 {
1073        account_name: args.try_get_string("accountName")?,
1074        account_index: args.get_u64("accountIndex").ok(),
1075    };
1076
1077    let account_create_args = AccountCreateArgs::Bip32 { prv_key_data_args, account_args };
1078
1079    Ok(AccountsCreateRequest { wallet_secret, account_create_args })
1080});
1081
1082declare! {
1083    IAccountsCreateResponse,
1084    r#"
1085    /**
1086     * 
1087     *  
1088     * @category Wallet API
1089     */
1090    export interface IAccountsCreateResponse {
1091        accountDescriptor : IAccountDescriptor;
1092    }
1093    "#,
1094}
1095
1096try_from!(args: AccountsCreateResponse, IAccountsCreateResponse, {
1097    let response = IAccountsCreateResponse::default();
1098    response.set("accountDescriptor", &IAccountDescriptor::try_from(args.account_descriptor)?.into())?;
1099    Ok(response)
1100});
1101
1102// ---
1103
1104declare! {
1105    IAccountsEnsureDefaultRequest,
1106    r#"
1107    /**
1108     * 
1109     *  
1110     * @category Wallet API
1111     */
1112    export interface IAccountsEnsureDefaultRequest {
1113        walletSecret: string;
1114        paymentSecret?: string;
1115        type : AccountKind | string;
1116        mnemonic? : string;
1117    }
1118    "#,
1119}
1120
1121try_from! (args: IAccountsEnsureDefaultRequest, AccountsEnsureDefaultRequest, {
1122    let wallet_secret = args.get_secret("walletSecret")?;
1123    let payment_secret = args.try_get_secret("paymentSecret")?;
1124    let account_kind = AccountKind::try_from(args.get_value("type")?)?;
1125    let mnemonic_phrase = args.try_get_secret("mnemonic")?;
1126
1127    Ok(AccountsEnsureDefaultRequest { wallet_secret, payment_secret, account_kind, mnemonic_phrase })
1128});
1129
1130declare! {
1131    IAccountsEnsureDefaultResponse,
1132    r#"
1133    /**
1134     * 
1135     *  
1136     * @category Wallet API
1137     */
1138    export interface IAccountsEnsureDefaultResponse {
1139        accountDescriptor : IAccountDescriptor;
1140    }
1141    "#,
1142}
1143
1144try_from!(args: AccountsEnsureDefaultResponse, IAccountsEnsureDefaultResponse, {
1145    let response = IAccountsEnsureDefaultResponse::default();
1146    response.set("accountDescriptor", &IAccountDescriptor::try_from(args.account_descriptor)?.into())?;
1147    Ok(response)
1148});
1149
1150// ---
1151
1152declare! {
1153    IAccountsImportRequest,
1154    r#"
1155    /**
1156     * 
1157     *  
1158     * @category Wallet API
1159     */
1160    export interface IAccountsImportRequest {
1161        walletSecret: string;
1162        // TODO
1163    }
1164    "#,
1165}
1166
1167try_from! ( _args: IAccountsImportRequest, AccountsImportRequest, {
1168    unimplemented!();
1169    // Ok(AccountsImportRequest { })
1170});
1171
1172declare! {
1173    IAccountsImportResponse,
1174    r#"
1175    /**
1176     * 
1177     *  
1178     * @category Wallet API
1179     */
1180    export interface IAccountsImportResponse {
1181        // TODO
1182    }
1183    "#,
1184}
1185
1186try_from! ( _args: AccountsImportResponse, IAccountsImportResponse, {
1187    unimplemented!();
1188    // let response = IAccountsImportResponse::default();
1189    // Ok(response)
1190});
1191
1192// ---
1193
1194declare! {
1195    IAccountsActivateRequest,
1196    "IAccountsActivateRequest",
1197    r#"
1198    /**
1199     * 
1200     *  
1201     * @category Wallet API
1202     */
1203    export interface IAccountsActivateRequest {
1204        accountIds?: HexString[],
1205    }
1206    "#,
1207}
1208
1209try_from! (args: IAccountsActivateRequest, AccountsActivateRequest, {
1210    Ok(from_value::<AccountsActivateRequest>(args.into())?)
1211});
1212
1213declare! {
1214    IAccountsActivateResponse,
1215    r#"
1216    /**
1217     * 
1218     *  
1219     * @category Wallet API
1220     */
1221    export interface IAccountsActivateResponse { }
1222    "#,
1223}
1224
1225try_from! ( _args: AccountsActivateResponse, IAccountsActivateResponse, {
1226    Ok(IAccountsActivateResponse::default())
1227});
1228
1229// ---
1230
1231declare! {
1232    IAccountsDeactivateRequest,
1233    "IAccountsDeactivateRequest",
1234    r#"
1235    /**
1236     * 
1237     *  
1238     * @category Wallet API
1239     */
1240    export interface IAccountsDeactivateRequest {
1241        accountIds?: string[];
1242    }
1243    "#,
1244}
1245
1246try_from! ( args: IAccountsDeactivateRequest, AccountsDeactivateRequest, {
1247    Ok(from_value::<AccountsDeactivateRequest>(args.into())?)
1248});
1249
1250declare! {
1251    IAccountsDeactivateResponse,
1252    r#"
1253    /**
1254     * 
1255     *  
1256     * @category Wallet API
1257     */
1258    export interface IAccountsDeactivateResponse { }
1259    "#,
1260}
1261
1262try_from! ( _args: AccountsDeactivateResponse, IAccountsDeactivateResponse, {
1263    Ok(IAccountsDeactivateResponse::default())
1264});
1265
1266// ---
1267
1268declare! {
1269    IAccountsGetRequest,
1270    r#"
1271    /**
1272     * 
1273     *  
1274     * @category Wallet API
1275     */
1276    export interface IAccountsGetRequest {
1277        accountId: string;
1278    }
1279    "#,
1280}
1281
1282try_from! ( args: IAccountsGetRequest, AccountsGetRequest, {
1283    Ok(from_value::<AccountsGetRequest>(args.into())?)
1284});
1285
1286declare! {
1287    IAccountsGetResponse,
1288    r#"
1289    /**
1290     * 
1291     *  
1292     * @category Wallet API
1293     */
1294    export interface IAccountsGetResponse {
1295        accountDescriptor: IAccountDescriptor;
1296    }
1297    "#,
1298}
1299
1300try_from! ( args: AccountsGetResponse, IAccountsGetResponse, {
1301    Ok(to_value(&args)?.into())
1302});
1303
1304// ---
1305
1306declare! {
1307    IAccountsCreateNewAddressRequest,
1308    r#"
1309    /**
1310     * 
1311     *  
1312     * @category Wallet API
1313     */
1314    export interface IAccountsCreateNewAddressRequest {
1315        accountId: string;
1316        addressKind?: NewAddressKind | string,
1317    }
1318    "#,
1319}
1320
1321try_from!(args: IAccountsCreateNewAddressRequest, AccountsCreateNewAddressRequest, {
1322    let account_id = args.get_account_id("accountId")?;
1323    let value = args.get_value("addressKind")?;
1324    let kind: NewAddressKind = if let Some(string) = value.as_string() {
1325        string.parse()?
1326    } else if let Ok(kind) = NewAddressKind::try_enum_from(&value) {
1327        kind
1328    } else {
1329        NewAddressKind::Receive
1330    };
1331    Ok(AccountsCreateNewAddressRequest { account_id, kind })
1332});
1333
1334declare! {
1335    IAccountsCreateNewAddressResponse,
1336    r#"
1337    /**
1338     * 
1339     *  
1340     * @category Wallet API
1341     */
1342    export interface IAccountsCreateNewAddressResponse {
1343        address: Address;
1344    }
1345    "#,
1346}
1347
1348try_from! ( args: AccountsCreateNewAddressResponse, IAccountsCreateNewAddressResponse, {
1349    Ok(to_value(&args)?.into())
1350});
1351
1352// ---
1353
1354declare! {
1355    IAccountsSendRequest,
1356    r#"
1357    /**
1358     * 
1359     *  
1360     * @category Wallet API
1361     */
1362    export interface IAccountsSendRequest {
1363        /**
1364         * Hex identifier of the account.
1365         */
1366        accountId : HexString;
1367        /**
1368         * Wallet encryption secret.
1369         */
1370        walletSecret : string;
1371        /**
1372         * Optional key encryption secret or BIP39 passphrase.
1373         */
1374        paymentSecret? : string;
1375        /**
1376         * Priority fee.
1377         */
1378        priorityFeeSompi? : IFees | bigint;
1379        /**
1380         * 
1381         */
1382        payload? : Uint8Array | HexString;
1383        /**
1384         * If not supplied, the destination will be the change address resulting in a UTXO compound transaction.
1385         */
1386        destination? : IPaymentOutput[];
1387    }
1388    "#,
1389}
1390
1391try_from! ( args: IAccountsSendRequest, AccountsSendRequest, {
1392    let account_id = args.get_account_id("accountId")?;
1393    let wallet_secret = args.get_secret("walletSecret")?;
1394    let payment_secret = args.try_get_secret("paymentSecret")?;
1395    let priority_fee_sompi = args.get::<IFees>("priorityFeeSompi")?.try_into()?;
1396    let payload = args.try_get_value("payload")?.map(|v| v.try_as_vec_u8()).transpose()?;
1397
1398    let outputs = args.get_value("destination")?;
1399    let destination: PaymentDestination =
1400        if outputs.is_undefined() { PaymentDestination::Change } else { PaymentOutputs::try_owned_from(outputs)?.into() };
1401
1402    Ok(AccountsSendRequest { account_id, wallet_secret, payment_secret, priority_fee_sompi, destination, payload })
1403});
1404
1405declare! {
1406    IAccountsSendResponse,
1407    r#"
1408    /**
1409     * 
1410     *  
1411     * @category Wallet API
1412     */
1413    export interface IAccountsSendResponse {
1414        /**
1415         * Summary produced by the transaction generator.
1416         */
1417        generatorSummary : GeneratorSummary;
1418        /**
1419         * Hex identifiers of successfully submitted transactions.
1420         */
1421        transactionIds : HexString[];
1422    }
1423    "#,
1424}
1425
1426try_from!(args: AccountsSendResponse, IAccountsSendResponse, {
1427
1428    let response = IAccountsSendResponse::default();
1429    response.set("generatorSummary", &GeneratorSummary::from(args.generator_summary).into())?;
1430    response.set("transactionIds", &to_value(&args.transaction_ids)?)?;
1431    Ok(response)
1432});
1433
1434// ---
1435
1436declare! {
1437    IAccountsTransferRequest,
1438    r#"
1439    /**
1440     * 
1441     *  
1442     * @category Wallet API
1443     */
1444    export interface IAccountsTransferRequest {
1445        sourceAccountId : HexString;
1446        destinationAccountId : HexString;
1447        walletSecret : string;
1448        paymentSecret? : string;
1449        priorityFeeSompi? : IFees | bigint;
1450        transferAmountSompi : bigint;
1451    }
1452    "#,
1453}
1454
1455try_from! ( args: IAccountsTransferRequest, AccountsTransferRequest, {
1456    let source_account_id = args.get_account_id("sourceAccountId")?;
1457    let destination_account_id = args.get_account_id("destinationAccountId")?;
1458    let wallet_secret = args.get_secret("walletSecret")?;
1459    let payment_secret = args.try_get_secret("paymentSecret")?;
1460    let priority_fee_sompi = args.try_get::<IFees>("priorityFeeSompi")?.map(Fees::try_from).transpose()?;
1461    let transfer_amount_sompi = args.get_u64("transferAmountSompi")?;
1462
1463    Ok(AccountsTransferRequest {
1464        source_account_id,
1465        destination_account_id,
1466        wallet_secret,
1467        payment_secret,
1468        priority_fee_sompi,
1469        transfer_amount_sompi,
1470    })
1471});
1472
1473declare! {
1474    IAccountsTransferResponse,
1475    r#"
1476    /**
1477     * 
1478     *  
1479     * @category Wallet API
1480     */
1481    export interface IAccountsTransferResponse {
1482        generatorSummary : GeneratorSummary;
1483        transactionIds : HexString[];
1484    }
1485    "#,
1486}
1487
1488try_from! ( args: AccountsTransferResponse, IAccountsTransferResponse, {
1489    let response = IAccountsTransferResponse::default();
1490    response.set("generatorSummary", &GeneratorSummary::from(args.generator_summary).into())?;
1491    response.set("transactionIds", &to_value(&args.transaction_ids)?)?;
1492    Ok(response)
1493});
1494
1495// ---
1496
1497declare! {
1498    IAccountsEstimateRequest,
1499    r#"
1500    /**
1501     * 
1502     *  
1503     * @category Wallet API
1504     */
1505    export interface IAccountsEstimateRequest {
1506        accountId : HexString;
1507        destination : IPaymentOutput[];
1508        priorityFeeSompi : IFees | bigint;
1509        payload? : Uint8Array | string;
1510    }
1511    "#,
1512}
1513
1514try_from! ( args: IAccountsEstimateRequest, AccountsEstimateRequest, {
1515    let account_id = args.get_account_id("accountId")?;
1516    let priority_fee_sompi = args.get::<IFees>("priorityFeeSompi")?.try_into()?;
1517    let payload = args.try_get_value("payload")?.map(|v| v.try_as_vec_u8()).transpose()?;
1518
1519    let outputs = args.get_value("destination")?;
1520    let destination: PaymentDestination =
1521        if outputs.is_undefined() { PaymentDestination::Change } else { PaymentOutputs::try_owned_from(outputs)?.into() };
1522
1523    Ok(AccountsEstimateRequest { account_id, priority_fee_sompi, destination, payload })
1524});
1525
1526declare! {
1527    IAccountsEstimateResponse,
1528    r#"
1529    /**
1530     * 
1531     *  
1532     * @category Wallet API
1533     */
1534    export interface IAccountsEstimateResponse {
1535        generatorSummary : GeneratorSummary;
1536    }
1537    "#,
1538}
1539
1540try_from! ( args: AccountsEstimateResponse, IAccountsEstimateResponse, {
1541    let response = IAccountsEstimateResponse::default();
1542    response.set("generatorSummary", &GeneratorSummary::from(args.generator_summary).into())?;
1543    Ok(response)
1544});
1545
1546// ---
1547
1548declare! {
1549    ITransactionsDataGetRequest,
1550    r#"
1551    /**
1552     * 
1553     *  
1554     * @category Wallet API
1555     */
1556    export interface ITransactionsDataGetRequest {
1557        accountId : HexString;
1558        networkId : NetworkId | string;
1559        filter? : TransactionKind[];
1560        start : bigint;
1561        end : bigint;
1562    }
1563    "#,
1564}
1565
1566try_from! ( args: ITransactionsDataGetRequest, TransactionsDataGetRequest, {
1567    let account_id = args.get_account_id("accountId")?;
1568    let network_id = args.get_network_id("networkId")?;
1569    let filter = args.get_vec("filter").ok().map(|filter| {
1570        filter.into_iter().map(TransactionKind::try_from).collect::<Result<Vec<TransactionKind>>>()
1571    }).transpose()?;
1572    let start = args.get_u64("start")?;
1573    let end = args.get_u64("end")?;
1574
1575    let request = TransactionsDataGetRequest {
1576        account_id,
1577        network_id,
1578        filter,
1579        start,
1580        end,
1581    };
1582    Ok(request)
1583});
1584
1585declare! {
1586    ITransactionsDataGetResponse,
1587    r#"
1588    /**
1589     * 
1590     * 
1591     * @category Wallet API
1592     */
1593    export interface ITransactionsDataGetResponse {
1594        accountId : HexString;
1595        transactions : ITransactionRecord[];
1596        start : bigint;
1597        total : bigint;
1598    }
1599    "#,
1600}
1601
1602try_from! ( args: TransactionsDataGetResponse, ITransactionsDataGetResponse, {
1603    Ok(to_value(&args)?.into())
1604});
1605
1606// ---
1607
1608declare! {
1609    ITransactionsReplaceNoteRequest,
1610    r#"
1611    /**
1612     * 
1613     *  
1614     * @category Wallet API
1615     */
1616    export interface ITransactionsReplaceNoteRequest {
1617        /**
1618         * The id of account the transaction belongs to.
1619         */
1620        accountId: HexString,
1621        /**
1622         * The network id of the transaction.
1623         */
1624        networkId: NetworkId | string,
1625        /**
1626         * The id of the transaction.
1627         */
1628        transactionId: HexString,
1629        /**
1630         * Optional note string to replace the existing note.
1631         * If not supplied, the note will be removed.
1632         */
1633        note?: string,
1634    }
1635    "#,
1636}
1637
1638try_from! ( args: ITransactionsReplaceNoteRequest, TransactionsReplaceNoteRequest, {
1639
1640    let account_id = args.get_account_id("accountId")?;
1641    let network_id = args.get_network_id("networkId")?;
1642    let transaction_id = args.get_transaction_id("transactionId")?;
1643    let note = args.try_get_string("note")?;
1644
1645    Ok(TransactionsReplaceNoteRequest {
1646        account_id,
1647        network_id,
1648        transaction_id,
1649        note,
1650    })
1651});
1652
1653declare! {
1654    ITransactionsReplaceNoteResponse,
1655    r#"
1656    /**
1657     * 
1658     *  
1659     * @category Wallet API
1660     */
1661    export interface ITransactionsReplaceNoteResponse { }
1662    "#,
1663}
1664
1665try_from! ( _args: TransactionsReplaceNoteResponse, ITransactionsReplaceNoteResponse, {
1666    Ok(ITransactionsReplaceNoteResponse::default())
1667});
1668
1669// ---
1670
1671// TODO
1672declare! {
1673    ITransactionsReplaceMetadataRequest,
1674    r#"
1675    /**
1676     * Metadata is a wallet-specific string that can be used to store arbitrary data.
1677     * It should contain a serialized JSON string with `key` containing the custom
1678     * data stored by the wallet.  When interacting with metadata, the wallet should
1679     * always deserialize the JSON string and then serialize it again after making
1680     * changes, preserving any foreign keys that it might encounter.
1681     *  
1682     * To preserve foreign metadata, the pattern of access should be:
1683     * `Get -> Modify -> Replace`
1684     * 
1685     * @category Wallet API
1686     */
1687    export interface ITransactionsReplaceMetadataRequest {
1688        /**
1689         * The id of account the transaction belongs to.
1690         */
1691        accountId: HexString,
1692        /**
1693         * The network id of the transaction.
1694         */
1695        networkId: NetworkId | string,
1696        /**
1697         * The id of the transaction.
1698         */
1699        transactionId: HexString,
1700        /**
1701         * Optional metadata string to replace the existing metadata.
1702         * If not supplied, the metadata will be removed.
1703         */
1704        metadata?: string,    
1705    }
1706    "#,
1707}
1708
1709try_from! ( args: ITransactionsReplaceMetadataRequest, TransactionsReplaceMetadataRequest, {
1710    let account_id = args.get_account_id("accountId")?;
1711    let network_id = args.get_network_id("networkId")?;
1712    let transaction_id = args.get_transaction_id("transactionId")?;
1713    let metadata = args.try_get_string("metadata")?;
1714
1715    Ok(TransactionsReplaceMetadataRequest {
1716        account_id,
1717        network_id,
1718        transaction_id,
1719        metadata,
1720    })
1721});
1722
1723declare! {
1724    ITransactionsReplaceMetadataResponse,
1725    r#"
1726    /**
1727     * 
1728     *  
1729     * @category Wallet API
1730     */
1731    export interface ITransactionsReplaceMetadataResponse { }
1732    "#,
1733}
1734
1735try_from! ( _args: TransactionsReplaceMetadataResponse, ITransactionsReplaceMetadataResponse, {
1736    Ok(ITransactionsReplaceMetadataResponse::default())
1737});
1738
1739// ---
1740
1741declare! {
1742    IAddressBookEnumerateRequest,
1743    r#"
1744    /**
1745     * 
1746     *  
1747     * @category Wallet API
1748     */
1749    export interface IAddressBookEnumerateRequest { }
1750    "#,
1751}
1752
1753try_from! ( _args: IAddressBookEnumerateRequest, AddressBookEnumerateRequest, {
1754    Ok(AddressBookEnumerateRequest { })
1755});
1756
1757declare! {
1758    IAddressBookEnumerateResponse,
1759    r#"
1760    /**
1761     * 
1762     *  
1763     * @category Wallet API
1764     */
1765    export interface IAddressBookEnumerateResponse {
1766        // TODO
1767    }
1768    "#,
1769}
1770
1771try_from! ( _args: AddressBookEnumerateResponse, IAddressBookEnumerateResponse, {
1772    Err(Error::NotImplemented)
1773});
1774
1775// ---