erpc-sdk 0.9.0

Async multi-network Rust SDK for ERPC
Documentation
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
use std::{fmt, str::FromStr, time::Duration};

use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use url::Url;

use crate::{ErpcError, Result};

/// Default JSON-RPC and price endpoint.
pub const DEFAULT_ENDPOINT: &str = "https://edge.erpc.global";
/// Default Avalanche JSON-RPC endpoint.
pub const DEFAULT_AVALANCHE_ENDPOINT: &str = "https://ava-rpc.erpc.global";
/// Default account endpoint.
pub const DEFAULT_ACCOUNT_ENDPOINT: &str = "https://solana-rpc.erpc.global";
/// Default user and Cloud endpoint.
pub const DEFAULT_USER_ENDPOINT: &str = "https://user-api.erpc.global";
/// Default request and connection-establishment timeout.
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// Configuration for one caller-owned JSON-RPC endpoint.
///
/// The HTTP URL is the complete request target. Its path and query are
/// preserved exactly and no eRPC route or API-key query parameter is added.
/// A WebSocket URL, when supplied, is independent from the HTTP URL.
#[derive(Clone)]
pub struct RpcEndpointConfig {
    /// Complete HTTP(S) JSON-RPC request target.
    pub http_url: String,
    /// Optional complete WS(S) subscription target.
    pub websocket_url: Option<String>,
    /// Headers scoped to this endpoint's HTTP JSON-RPC requests only.
    pub headers: Vec<(String, String)>,
}

impl fmt::Debug for RpcEndpointConfig {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("RpcEndpointConfig")
            .field("http_url", &debug_endpoint(&self.http_url))
            .field(
                "websocket_url",
                &self.websocket_url.as_deref().map(debug_endpoint),
            )
            .field(
                "header_names",
                &self
                    .headers
                    .iter()
                    .map(|(name, _)| name)
                    .collect::<Vec<_>>(),
            )
            .finish()
    }
}

impl RpcEndpointConfig {
    /// Starts an endpoint configuration with a complete HTTP(S) URL.
    #[must_use]
    pub fn new(http_url: impl Into<String>) -> Self {
        Self {
            http_url: http_url.into(),
            websocket_url: None,
            headers: Vec::new(),
        }
    }

    /// Replaces the complete HTTP(S) request target.
    #[must_use]
    pub fn with_http_url(mut self, http_url: impl Into<String>) -> Self {
        self.http_url = http_url.into();
        self
    }

    /// Sets the independent complete WS(S) subscription target.
    #[must_use]
    pub fn with_websocket_url(mut self, websocket_url: impl Into<String>) -> Self {
        self.websocket_url = Some(websocket_url.into());
        self
    }

    /// Sets the independent complete WS(S) subscription target.
    #[must_use]
    pub fn with_websocket(self, websocket_url: impl Into<String>) -> Self {
        self.with_websocket_url(websocket_url)
    }

    /// Adds an HTTP header scoped to this endpoint.
    #[must_use]
    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }

    /// Replaces the endpoint-scoped headers.
    #[must_use]
    pub fn with_headers<I, N, V>(mut self, headers: I) -> Self
    where
        I: IntoIterator<Item = (N, V)>,
        N: Into<String>,
        V: Into<String>,
    {
        self.headers = headers
            .into_iter()
            .map(|(name, value)| (name.into(), value.into()))
            .collect();
        self
    }
}

impl From<&str> for RpcEndpointConfig {
    fn from(http_url: &str) -> Self {
        Self::new(http_url)
    }
}

impl From<String> for RpcEndpointConfig {
    fn from(http_url: String) -> Self {
        Self::new(http_url)
    }
}

impl Default for RpcEndpointConfig {
    fn default() -> Self {
        Self::new("")
    }
}

/// Configuration used to create an [`crate::ErpcClient`].
#[derive(Clone)]
pub struct ErpcClientConfig {
    /// ERPC API key. It is held in memory and omitted from debug output.
    api_key: String,
    /// Optional JSON-RPC and price endpoint override.
    pub endpoint: String,
    /// Optional Avalanche endpoint override.
    pub avalanche_endpoint: String,
    /// Optional account endpoint override.
    pub account_endpoint: String,
    /// Optional user endpoint override.
    pub user_endpoint: String,
    /// Extra request headers.
    pub headers: Vec<(String, String)>,
    /// Request timeout.
    pub timeout: Duration,
    /// Optional caller-owned Solana JSON-RPC endpoint.
    pub solana_rpc: Option<RpcEndpointConfig>,
    /// Optional caller-owned Ethereum JSON-RPC endpoint.
    pub ethereum_rpc: Option<RpcEndpointConfig>,
    /// Optional caller-owned Avalanche C-Chain JSON-RPC endpoint.
    pub avalanche_c_rpc: Option<RpcEndpointConfig>,
}

impl fmt::Debug for ErpcClientConfig {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ErpcClientConfig")
            .field("api_key", &"[REDACTED]")
            .field("endpoint", &debug_endpoint(&self.endpoint))
            .field(
                "avalanche_endpoint",
                &debug_endpoint(&self.avalanche_endpoint),
            )
            .field("account_endpoint", &debug_endpoint(&self.account_endpoint))
            .field("user_endpoint", &debug_endpoint(&self.user_endpoint))
            .field("solana_rpc", &self.solana_rpc)
            .field("ethereum_rpc", &self.ethereum_rpc)
            .field("avalanche_c_rpc", &self.avalanche_c_rpc)
            .field(
                "header_names",
                &self
                    .headers
                    .iter()
                    .map(|(name, _)| name)
                    .collect::<Vec<_>>(),
            )
            .field("timeout", &self.timeout)
            .finish()
    }
}

impl ErpcClientConfig {
    /// Starts a configuration with production endpoints and a 30-second timeout.
    #[must_use]
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            endpoint: DEFAULT_ENDPOINT.to_owned(),
            avalanche_endpoint: DEFAULT_AVALANCHE_ENDPOINT.to_owned(),
            account_endpoint: DEFAULT_ACCOUNT_ENDPOINT.to_owned(),
            user_endpoint: DEFAULT_USER_ENDPOINT.to_owned(),
            headers: Vec::new(),
            timeout: DEFAULT_TIMEOUT,
            solana_rpc: None,
            ethereum_rpc: None,
            avalanche_c_rpc: None,
        }
    }

    /// Starts a keyless configuration for one or more direct RPC endpoints.
    ///
    /// Construction succeeds before an override is attached so callers can
    /// build a configuration fluently. [`crate::ErpcClient::new`] rejects it
    /// when no valid direct endpoint is configured.
    #[must_use]
    pub fn for_rpc() -> Self {
        Self::new(String::new())
    }

    /// Returns the API key without placing it in debug output.
    #[must_use]
    pub fn api_key(&self) -> &str {
        &self.api_key
    }

    /// Overrides the JSON-RPC and price endpoint.
    #[must_use]
    pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.endpoint = endpoint.into();
        self
    }

    /// Overrides the Avalanche endpoint.
    #[must_use]
    pub fn with_avalanche_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.avalanche_endpoint = endpoint.into();
        self
    }

    /// Overrides the account endpoint.
    #[must_use]
    pub fn with_account_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.account_endpoint = endpoint.into();
        self
    }

    /// Overrides the user endpoint.
    #[must_use]
    pub fn with_user_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.user_endpoint = endpoint.into();
        self
    }

    /// Adds an HTTP header after construction-time validation.
    #[must_use]
    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }

    /// Overrides the request timeout.
    #[must_use]
    pub const fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Selects a caller-owned Solana JSON-RPC endpoint.
    #[must_use]
    pub fn with_solana_rpc(mut self, endpoint: impl Into<RpcEndpointConfig>) -> Self {
        self.solana_rpc = Some(endpoint.into());
        self
    }

    /// Selects a caller-owned Ethereum JSON-RPC endpoint.
    #[must_use]
    pub fn with_ethereum_rpc(mut self, endpoint: impl Into<RpcEndpointConfig>) -> Self {
        self.ethereum_rpc = Some(endpoint.into());
        self
    }

    /// Selects a caller-owned Avalanche C-Chain JSON-RPC endpoint.
    #[must_use]
    pub fn with_avalanche_c_rpc(mut self, endpoint: impl Into<RpcEndpointConfig>) -> Self {
        self.avalanche_c_rpc = Some(endpoint.into());
        self
    }

    /// Selects a caller-owned Avalanche C-Chain JSON-RPC endpoint.
    #[must_use]
    pub fn with_avalanche_rpc(self, endpoint: impl Into<RpcEndpointConfig>) -> Self {
        self.with_avalanche_c_rpc(endpoint)
    }
}

pub(crate) struct ResolvedRpcEndpoint {
    pub http_url: Url,
    pub websocket_url: Option<Url>,
    pub headers: HeaderMap,
    pub redaction_secrets: Vec<String>,
}

pub(crate) struct ResolvedErpcClientConfig {
    pub api_key: Option<String>,
    pub endpoint: Url,
    pub avalanche_endpoint: Url,
    pub account_endpoint: Url,
    pub user_endpoint: Url,
    pub headers: HeaderMap,
    pub timeout: Duration,
    pub solana_rpc: Option<ResolvedRpcEndpoint>,
    pub ethereum_rpc: Option<ResolvedRpcEndpoint>,
    pub avalanche_c_rpc: Option<ResolvedRpcEndpoint>,
}

impl TryFrom<ErpcClientConfig> for ResolvedErpcClientConfig {
    type Error = ErpcError;

    fn try_from(config: ErpcClientConfig) -> Result<Self> {
        let api_key = config.api_key.trim().to_owned();
        if api_key.is_empty()
            && config.solana_rpc.is_none()
            && config.ethereum_rpc.is_none()
            && config.avalanche_c_rpc.is_none()
        {
            return Err(ErpcError::Config("api_key must not be empty".to_owned()));
        }
        if config.timeout.is_zero() {
            return Err(ErpcError::Config("timeout must be positive".to_owned()));
        }
        let headers = parse_headers(&config.headers)?;
        let solana_rpc = config
            .solana_rpc
            .as_ref()
            .map(resolve_rpc_endpoint)
            .transpose()?;
        let ethereum_rpc = config
            .ethereum_rpc
            .as_ref()
            .map(resolve_rpc_endpoint)
            .transpose()?;
        let avalanche_rpc = config
            .avalanche_c_rpc
            .as_ref()
            .map(resolve_rpc_endpoint)
            .transpose()?;
        Ok(Self {
            api_key: (!api_key.is_empty()).then_some(api_key),
            endpoint: endpoint(&config.endpoint, false)?,
            avalanche_endpoint: endpoint(&config.avalanche_endpoint, false)?,
            account_endpoint: endpoint(&config.account_endpoint, false)?,
            user_endpoint: endpoint(&config.user_endpoint, false)?,
            headers,
            timeout: config.timeout,
            solana_rpc,
            ethereum_rpc,
            avalanche_c_rpc: avalanche_rpc,
        })
    }
}

impl ResolvedErpcClientConfig {
    pub fn http_client() -> Result<reqwest::Client> {
        reqwest::Client::builder()
            .build()
            .map_err(|_| ErpcError::Config("unable to create HTTP client".to_owned()))
    }

    pub fn direct_http_client() -> Result<reqwest::Client> {
        reqwest::Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .build()
            .map_err(|_| ErpcError::Config("unable to create direct HTTP client".to_owned()))
    }
}

pub(crate) fn endpoint(value: &str, local_http_only: bool) -> Result<Url> {
    let mut url = Url::parse(value)
        .map_err(|_| ErpcError::Config("endpoint must be an absolute HTTP(S) URL".to_owned()))?;
    let local = matches!(url.host_str(), Some("127.0.0.1" | "::1" | "localhost"));
    let allowed =
        url.scheme() == "https" || (url.scheme() == "http" && (!local_http_only || local));
    if !allowed {
        let message = if local_http_only {
            "endpoint must use HTTPS except on localhost"
        } else {
            "endpoint must use HTTP or HTTPS"
        };
        return Err(ErpcError::Config(message.to_owned()));
    }
    url.set_query(None);
    url.set_fragment(None);
    let normalized = url.path().trim_end_matches('/').to_owned();
    url.set_path(if normalized.is_empty() {
        "/"
    } else {
        &normalized
    });
    Ok(url)
}

fn resolve_rpc_endpoint(config: &RpcEndpointConfig) -> Result<ResolvedRpcEndpoint> {
    let http_url = direct_http_endpoint(&config.http_url)?;
    let websocket_url = config
        .websocket_url
        .as_deref()
        .map(direct_websocket_endpoint)
        .transpose()?;
    let headers = parse_headers(&config.headers)?;
    let mut redaction_secrets = redaction_secrets_for_raw_headers(&config.headers);
    collect_query_values(&http_url, &mut redaction_secrets);
    if let Some(websocket_url) = &websocket_url {
        collect_query_values(websocket_url, &mut redaction_secrets);
    }
    Ok(ResolvedRpcEndpoint {
        http_url,
        websocket_url,
        headers,
        redaction_secrets,
    })
}

fn parse_headers(values: &[(String, String)]) -> Result<HeaderMap> {
    let mut headers = HeaderMap::new();
    for (name, value) in values {
        let name = HeaderName::from_str(name)
            .map_err(|_| ErpcError::Config("header name is invalid".to_owned()))?;
        let value = HeaderValue::from_str(value)
            .map_err(|_| ErpcError::Config("header value is invalid".to_owned()))?;
        headers.insert(name, value);
    }
    Ok(headers)
}

fn direct_http_endpoint(value: &str) -> Result<Url> {
    direct_endpoint(value, false)
        .map_err(|_| ErpcError::Config("rpc http_url must be an absolute HTTP(S) URL".to_owned()))
}

fn direct_websocket_endpoint(value: &str) -> Result<Url> {
    direct_endpoint(value, true).map_err(|_| {
        ErpcError::Config("rpc websocket_url must be an absolute WS(S) URL".to_owned())
    })
}

fn direct_endpoint(value: &str, websocket: bool) -> Result<Url> {
    let value = value.trim();
    let Some(scheme_end) = value.find("://") else {
        return Err(ErpcError::Config(
            "direct endpoint is missing an authority".to_owned(),
        ));
    };
    let authority = &value[scheme_end + 3..];
    let authority_end = authority.find(['/', '?', '#']).unwrap_or(authority.len());
    let authority = &authority[..authority_end];
    if authority.is_empty() || authority.contains('@') || value.contains('#') {
        return Err(ErpcError::Config(
            "direct endpoint authority is invalid".to_owned(),
        ));
    }
    let url = Url::parse(value)
        .map_err(|_| ErpcError::Config("direct endpoint is invalid".to_owned()))?;
    let allowed = if websocket {
        matches!(url.scheme(), "ws" | "wss")
    } else {
        matches!(url.scheme(), "http" | "https")
    };
    if !allowed || url.host_str().is_none() {
        return Err(ErpcError::Config(
            "direct endpoint scheme or authority is invalid".to_owned(),
        ));
    }
    Ok(url)
}

fn collect_query_values(url: &Url, secrets: &mut Vec<String>) {
    if let Some(query) = url.query() {
        for component in query.split('&') {
            let raw_value = component
                .split_once('=')
                .map_or(component, |(_, value)| value);
            if !raw_value.is_empty() && !secrets.contains(&raw_value.to_owned()) {
                secrets.push(raw_value.to_owned());
            }
        }
    }
    for (_, value) in url.query_pairs() {
        let value = value.into_owned();
        if !value.is_empty() && !secrets.contains(&value) {
            secrets.push(value);
        }
    }
}

pub(crate) fn redaction_secrets_for_headers(headers: &HeaderMap) -> Vec<String> {
    let mut secrets = Vec::new();
    for (name, value) in headers {
        let Ok(value) = value.to_str() else { continue };
        add_header_redaction_secrets(&mut secrets, name.as_str(), value);
    }
    secrets
}

fn redaction_secrets_for_raw_headers(headers: &[(String, String)]) -> Vec<String> {
    let mut secrets = Vec::new();
    for (name, value) in headers {
        add_header_redaction_secrets(&mut secrets, name, value);
    }
    secrets
}

fn add_header_redaction_secrets(secrets: &mut Vec<String>, name: &str, value: &str) {
    let value = value.trim();
    if value.is_empty() {
        return;
    }
    for candidate in [
        value.to_owned(),
        format!("{name}: {value}"),
        format!("{name}:{value}"),
        format!("{name}={value}"),
    ] {
        if !secrets.contains(&candidate) {
            secrets.push(candidate);
        }
    }
    let mut parts = value.splitn(2, char::is_whitespace);
    let scheme = parts.next().unwrap_or_default();
    if !matches!(scheme.to_ascii_lowercase().as_str(), "bearer" | "basic") {
        return;
    }
    let Some(component) = parts
        .next()
        .map(str::trim)
        .filter(|value| !value.is_empty())
    else {
        return;
    };
    if !secrets.iter().any(|secret| secret == component) {
        secrets.push(component.to_owned());
    }
    if !scheme.eq_ignore_ascii_case("basic") {
        return;
    }
    let Some(decoded) = decode_base64(component).and_then(|bytes| String::from_utf8(bytes).ok())
    else {
        return;
    };
    if !secrets.iter().any(|secret| secret == &decoded) {
        secrets.push(decoded.clone());
    }
    if let Some((username, password)) = decoded.split_once(':') {
        for value in [username, password] {
            if !value.is_empty() && !secrets.iter().any(|secret| secret == value) {
                secrets.push(value.to_owned());
            }
        }
    }
}

fn decode_base64(value: &str) -> Option<Vec<u8>> {
    if value.is_empty() {
        return None;
    }
    let mut output = Vec::new();
    let mut accumulator = 0_u32;
    let mut bits = 0_u8;
    let mut padding = false;
    let mut padding_count = 0_usize;
    let mut data_count = 0_usize;
    for byte in value.bytes() {
        if byte == b'=' {
            padding = true;
            padding_count += 1;
            if padding_count > 2 {
                return None;
            }
            continue;
        }
        if padding {
            return None;
        }
        data_count += 1;
        let digit = match byte {
            b'A'..=b'Z' => byte - b'A',
            b'a'..=b'z' => byte - b'a' + 26,
            b'0'..=b'9' => byte - b'0' + 52,
            b'+' => 62,
            b'/' => 63,
            _ => return None,
        };
        accumulator = (accumulator << 6) | u32::from(digit);
        bits += 6;
        while bits >= 8 {
            bits -= 8;
            let byte = (accumulator >> bits) & 0xff;
            output.push(u8::try_from(byte).ok()?);
            if bits == 0 {
                accumulator = 0;
            } else {
                accumulator &= (1_u32 << bits) - 1;
            }
        }
    }
    if data_count == 0 || data_count % 4 == 1 {
        return None;
    }
    if padding_count > 0
        && ((data_count + padding_count) % 4 != 0 || 4 - (data_count % 4) != padding_count)
    {
        return None;
    }
    if bits > 0 && accumulator & ((1_u32 << bits) - 1) != 0 {
        return None;
    }
    Some(output)
}

fn debug_endpoint(value: &str) -> String {
    let Ok(mut url) = Url::parse(value) else {
        return "[REDACTED]".to_owned();
    };
    url.set_query(None);
    url.set_fragment(None);
    let _ = url.set_username("");
    let _ = url.set_password(None);
    url.to_string()
}

pub(crate) fn endpoint_with_path(endpoint: &Url, path: &str) -> Url {
    let mut url = endpoint.clone();
    let base = endpoint.path().trim_end_matches('/');
    let path = path.trim_start_matches('/');
    url.set_path(&format!("{base}/{path}"));
    url
}

pub(crate) fn websocket_url(endpoint: &Url, api_key: &str, path: &str) -> Result<Url> {
    let mut url = endpoint_with_path(endpoint, path);
    let scheme = if url.scheme() == "https" { "wss" } else { "ws" };
    url.set_scheme(scheme)
        .map_err(|()| ErpcError::Config("unable to create WebSocket URL".to_owned()))?;
    url.query_pairs_mut().append_pair("api-key", api_key);
    Ok(url)
}