Skip to content

Commit 062d150

Browse files
authored
Merge pull request #1890 from input-output-hk/dlachaume/1878/mithril-client-future-proof-options
Add support for custom HTTP Headers in Mithril client WASM library
2 parents 8fdf0bb + ada426f commit 062d150

File tree

13 files changed

+211
-26
lines changed

13 files changed

+211
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ As a minor extension, we have adopted a slightly different versioning convention
1010
## Mithril Distribution [XXXX] - UNRELEASED
1111

1212
- Support for Mithril nodes footprint support in Prometheus monitoring in infrastructure
13+
- Add support for custom HTTP headers in Mithril client WASM library
1314

1415
- **UNSTABLE** Cardano stake distribution certification:
1516

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/website/root/manual/developer-docs/nodes/mithril-client-library-wasm.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ console.log(
146146
);
147147
```
148148

149+
:::tip Adding Custom HTTP Headers
150+
151+
You can customize the HTTP headers sent by the Mithril client. This is particularly useful in scenarios where the Mithril client is used with a proxy, as it allows you to include headers like **Authorization** or custom headers for specific use cases. Below is an example of how to set custom headers:
152+
153+
```js
154+
let http_headers_map = new Map();
155+
http_headers_map.set("Authorization", "Bearer YourBearerToken");
156+
http_headers_map.set("X-Custom-Header", "YourCustomHeaderValue");
157+
158+
let client_options = {
159+
http_headers: http_headers_map,
160+
};
161+
162+
let client = new MithrilClient(
163+
aggregator_endpoint,
164+
genesis_verification_key,
165+
client_options,
166+
);
167+
```
168+
169+
:::
170+
149171
If the aggregator signs **CardanoTransactions**, you can add the code below to the previous example:
150172

151173
:::tip

mithril-client-wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-client-wasm"
3-
version = "0.3.9"
3+
version = "0.3.10"
44
description = "Mithril client WASM"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-client-wasm/src/client_wasm.rs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use wasm_bindgen::prelude::*;
66
use mithril_client::{
77
common::Epoch,
88
feedback::{FeedbackReceiver, MithrilEvent},
9-
CardanoTransactionsProofs, Client, ClientBuilder, MessageBuilder, MithrilCertificate,
9+
CardanoTransactionsProofs, Client, ClientBuilder, ClientOptions, MessageBuilder,
10+
MithrilCertificate,
1011
};
1112

1213
use crate::WasmResult;
@@ -71,10 +72,24 @@ pub struct MithrilUnstableClient {
7172
impl MithrilClient {
7273
/// Constructor for wasm client
7374
#[wasm_bindgen(constructor)]
74-
pub fn new(aggregator_endpoint: &str, genesis_verification_key: &str) -> MithrilClient {
75+
pub fn new(
76+
aggregator_endpoint: &str,
77+
genesis_verification_key: &str,
78+
options: JsValue,
79+
) -> MithrilClient {
7580
let feedback_receiver = Arc::new(JSBroadcastChannelFeedbackReceiver::new("mithril-client"));
81+
82+
let client_options = if options.is_undefined() {
83+
ClientOptions::default()
84+
} else {
85+
serde_wasm_bindgen::from_value(options)
86+
.map_err(|err| format!("Failed to parse options: {err:?}"))
87+
.unwrap()
88+
};
89+
7690
let client = ClientBuilder::aggregator(aggregator_endpoint, genesis_verification_key)
7791
.add_feedback_receiver(feedback_receiver)
92+
.with_options(client_options)
7893
.build()
7994
.map_err(|err| format!("{err:?}"))
8095
.unwrap();
@@ -361,8 +376,7 @@ impl MithrilUnstableClient {
361376

362377
#[cfg(test)]
363378
mod tests {
364-
use super::*;
365-
use crate::test_data;
379+
use std::collections::HashMap;
366380
use wasm_bindgen_test::*;
367381

368382
use mithril_client::{
@@ -371,6 +385,10 @@ mod tests {
371385
MithrilStakeDistributionListItem, Snapshot, SnapshotListItem,
372386
};
373387

388+
use crate::test_data;
389+
390+
use super::*;
391+
374392
const GENESIS_VERIFICATION_KEY: &str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d";
375393
const FAKE_AGGREGATOR_IP: &str = "127.0.0.1";
376394
const FAKE_AGGREGATOR_PORT: &str = "8000";
@@ -382,10 +400,55 @@ mod tests {
382400
FAKE_AGGREGATOR_IP, FAKE_AGGREGATOR_PORT
383401
),
384402
GENESIS_VERIFICATION_KEY,
403+
JsValue::undefined(),
385404
)
386405
}
387406

388407
wasm_bindgen_test_configure!(run_in_browser);
408+
409+
#[wasm_bindgen_test]
410+
fn build_mithril_client_with_custom_http_header() {
411+
let mut http_headers = HashMap::new();
412+
http_headers.insert("Authorization".to_string(), "Bearer token".to_string());
413+
let options = ClientOptions::new(Some(http_headers));
414+
let options_js_value = serde_wasm_bindgen::to_value(&options).unwrap();
415+
416+
MithrilClient::new(
417+
&format!(
418+
"http://{}:{}/aggregator",
419+
FAKE_AGGREGATOR_IP, FAKE_AGGREGATOR_PORT
420+
),
421+
GENESIS_VERIFICATION_KEY,
422+
options_js_value,
423+
);
424+
}
425+
426+
#[wasm_bindgen_test]
427+
fn build_mithril_client_with_undefined_options() {
428+
MithrilClient::new(
429+
&format!(
430+
"http://{}:{}/aggregator",
431+
FAKE_AGGREGATOR_IP, FAKE_AGGREGATOR_PORT
432+
),
433+
GENESIS_VERIFICATION_KEY,
434+
JsValue::undefined(),
435+
);
436+
}
437+
438+
#[wasm_bindgen_test]
439+
#[should_panic(expected = "Failed to parse options")]
440+
fn build_mithril_client_with_unparsable_options() {
441+
let invalid_js_value = JsValue::from_str("invalid");
442+
MithrilClient::new(
443+
&format!(
444+
"http://{}:{}/aggregator",
445+
FAKE_AGGREGATOR_IP, FAKE_AGGREGATOR_PORT
446+
),
447+
GENESIS_VERIFICATION_KEY,
448+
invalid_js_value,
449+
);
450+
}
451+
389452
#[wasm_bindgen_test]
390453
async fn list_snapshots_should_return_value_convertible_in_rust_type() {
391454
let snapshots_list_js_value = get_mithril_client()

mithril-client-wasm/www-test/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-client-wasm/www/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,21 @@ function format_tx_list(transactions_hashes) {
6464
return "<ul>" + transactions_hashes.map((tx) => "<li>" + tx + "</li>").join("") + "</ul>";
6565
}
6666

67+
function client_options_with_custom_headers() {
68+
// The following header is set as an example.
69+
// It's used to demonstrate how to add headers.
70+
let http_headers_map = new Map();
71+
http_headers_map.set("Content-Type", "application/json");
72+
73+
return {
74+
http_headers: http_headers_map,
75+
};
76+
}
77+
6778
await initMithrilClient();
6879

69-
let client = new MithrilClient(aggregator_endpoint, genesis_verification_key);
80+
let client_options = client_options_with_custom_headers();
81+
let client = new MithrilClient(aggregator_endpoint, genesis_verification_key, client_options);
7082

7183
displayStepInDOM(1, "Getting stake distributions list...");
7284
let mithril_stake_distributions_list = await client.list_mithril_stake_distributions();

mithril-client-wasm/www/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-client-wasm/www/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "www",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"private": true,
55
"scripts": {
66
"build": "webpack --config webpack.config.js",

mithril-client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-client"
3-
version = "0.8.13"
3+
version = "0.8.14"
44
description = "Mithril client library"
55
authors = { workspace = true }
66
edition = { workspace = true }

0 commit comments

Comments
 (0)