Skip to main content

bsv/wallet/substrates/
wallet_client.rs

1//! WalletClient: validates args before delegating to wire transport.
2//!
3//! Wraps a WalletWireTransceiver and adds argument validation to each
4//! method call before sending over the wire. This prevents invalid
5//! requests from being transmitted.
6//!
7//! Translated from Go SDK wallet/substrates/wallet_client.go.
8
9use crate::wallet::error::WalletError;
10use crate::wallet::interfaces::*;
11use crate::wallet::substrates::{WalletWire, WalletWireTransceiver};
12use crate::wallet::validation;
13
14/// Client that validates all arguments before delegating to wire transport.
15pub struct WalletClient<W: WalletWire> {
16    transceiver: WalletWireTransceiver<W>,
17}
18
19impl<W: WalletWire> WalletClient<W> {
20    /// Create a new WalletClient wrapping the given wire substrate.
21    pub fn new(substrate: W) -> Self {
22        Self {
23            transceiver: WalletWireTransceiver::new(substrate),
24        }
25    }
26}
27
28/// Macro to reduce boilerplate: validate args then delegate to transceiver.
29macro_rules! impl_validated_method {
30    // Methods with args
31    ($method:ident, $args_type:ty, $result_type:ty, $validator:path) => {
32        async fn $method(
33            &self,
34            args: $args_type,
35            originator: Option<&str>,
36        ) -> Result<$result_type, WalletError> {
37            $validator(&args)?;
38            self.transceiver.$method(args, originator).await
39        }
40    };
41    // Methods without args (no validation needed, just delegate)
42    (no_args $method:ident, $result_type:ty) => {
43        async fn $method(&self, originator: Option<&str>) -> Result<$result_type, WalletError> {
44            self.transceiver.$method(originator).await
45        }
46    };
47}
48
49#[allow(async_fn_in_trait)]
50impl<W: WalletWire> WalletInterface for WalletClient<W> {
51    impl_validated_method!(
52        create_action,
53        CreateActionArgs,
54        CreateActionResult,
55        validation::validate_create_action_args
56    );
57
58    impl_validated_method!(
59        sign_action,
60        SignActionArgs,
61        SignActionResult,
62        validation::validate_sign_action_args
63    );
64
65    impl_validated_method!(
66        abort_action,
67        AbortActionArgs,
68        AbortActionResult,
69        validation::validate_abort_action_args
70    );
71
72    impl_validated_method!(
73        list_actions,
74        ListActionsArgs,
75        ListActionsResult,
76        validation::validate_list_actions_args
77    );
78
79    impl_validated_method!(
80        internalize_action,
81        InternalizeActionArgs,
82        InternalizeActionResult,
83        validation::validate_internalize_action_args
84    );
85
86    impl_validated_method!(
87        list_outputs,
88        ListOutputsArgs,
89        ListOutputsResult,
90        validation::validate_list_outputs_args
91    );
92
93    impl_validated_method!(
94        relinquish_output,
95        RelinquishOutputArgs,
96        RelinquishOutputResult,
97        validation::validate_relinquish_output_args
98    );
99
100    impl_validated_method!(
101        get_public_key,
102        GetPublicKeyArgs,
103        GetPublicKeyResult,
104        validation::validate_get_public_key_args
105    );
106
107    impl_validated_method!(
108        reveal_counterparty_key_linkage,
109        RevealCounterpartyKeyLinkageArgs,
110        RevealCounterpartyKeyLinkageResult,
111        validation::validate_reveal_counterparty_key_linkage_args
112    );
113
114    impl_validated_method!(
115        reveal_specific_key_linkage,
116        RevealSpecificKeyLinkageArgs,
117        RevealSpecificKeyLinkageResult,
118        validation::validate_reveal_specific_key_linkage_args
119    );
120
121    impl_validated_method!(
122        encrypt,
123        EncryptArgs,
124        EncryptResult,
125        validation::validate_encrypt_args
126    );
127
128    impl_validated_method!(
129        decrypt,
130        DecryptArgs,
131        DecryptResult,
132        validation::validate_decrypt_args
133    );
134
135    impl_validated_method!(
136        create_hmac,
137        CreateHmacArgs,
138        CreateHmacResult,
139        validation::validate_create_hmac_args
140    );
141
142    impl_validated_method!(
143        verify_hmac,
144        VerifyHmacArgs,
145        VerifyHmacResult,
146        validation::validate_verify_hmac_args
147    );
148
149    impl_validated_method!(
150        create_signature,
151        CreateSignatureArgs,
152        CreateSignatureResult,
153        validation::validate_create_signature_args
154    );
155
156    impl_validated_method!(
157        verify_signature,
158        VerifySignatureArgs,
159        VerifySignatureResult,
160        validation::validate_verify_signature_args
161    );
162
163    impl_validated_method!(
164        acquire_certificate,
165        AcquireCertificateArgs,
166        Certificate,
167        validation::validate_acquire_certificate_args
168    );
169
170    impl_validated_method!(
171        list_certificates,
172        ListCertificatesArgs,
173        ListCertificatesResult,
174        validation::validate_list_certificates_args
175    );
176
177    impl_validated_method!(
178        prove_certificate,
179        ProveCertificateArgs,
180        ProveCertificateResult,
181        validation::validate_prove_certificate_args
182    );
183
184    impl_validated_method!(
185        relinquish_certificate,
186        RelinquishCertificateArgs,
187        RelinquishCertificateResult,
188        validation::validate_relinquish_certificate_args
189    );
190
191    impl_validated_method!(
192        discover_by_identity_key,
193        DiscoverByIdentityKeyArgs,
194        DiscoverCertificatesResult,
195        validation::validate_discover_by_identity_key_args
196    );
197
198    impl_validated_method!(
199        discover_by_attributes,
200        DiscoverByAttributesArgs,
201        DiscoverCertificatesResult,
202        validation::validate_discover_by_attributes_args
203    );
204
205    impl_validated_method!(no_args is_authenticated, AuthenticatedResult);
206    impl_validated_method!(no_args wait_for_authentication, AuthenticatedResult);
207    impl_validated_method!(no_args get_height, GetHeightResult);
208
209    impl_validated_method!(
210        get_header_for_height,
211        GetHeaderArgs,
212        GetHeaderResult,
213        validation::validate_get_header_args
214    );
215
216    impl_validated_method!(no_args get_network, GetNetworkResult);
217    impl_validated_method!(no_args get_version, GetVersionResult);
218}