lsdk 0.0.2

Rust SDK for hardware wallets speaking the BOLOS APDU protocol: HID/WebHID transport, device info, genuine check, app management, firmware updates and app signers
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
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
//! JavaScript API (wasm-bindgen) used by the web UI.
//!
//! ```js
//! import init, { HwDevice } from "lsdk";
//! await init();
//! const dev = await HwDevice.request(); // device picker (user gesture)
//! console.log(await dev.info());
//! ```

use std::cell::RefCell;

use js_sys::Function;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

use crate::apps::{btc, eth, sol};
use crate::hsm::HsmEvent;
use crate::manager::Manager;
use crate::manager::ops::FirmwareUpdate;
use crate::path::DerivationPath;
use crate::transport::webhid::{self, WebHidTransport};
use crate::{Device, Error, Result};

type JsResult<T> = core::result::Result<T, JsValue>;

fn js_err(e: Error) -> JsValue {
    let err = js_sys::Error::new(&e.to_string());
    let code = match &e {
        Error::Status(sw) => Some(format!("0x{:04x}", sw.0)),
        _ => None,
    };
    let kind = match &e {
        Error::UserRefused => "UserRefused",
        Error::Locked => "Locked",
        Error::Disconnected => "Disconnected",
        Error::DashboardExpected => "DashboardExpected",
        Error::NoDevice => "NoDevice",
        Error::NotFound(_) => "NotFound",
        Error::Status(_) => "Status",
        _ => "Error",
    };
    let _ = js_sys::Reflect::set(&err, &"kind".into(), &kind.into());
    if let Some(code) = code {
        let _ = js_sys::Reflect::set(&err, &"statusCode".into(), &code.into());
    }
    err.into()
}

fn to_js<T: Serialize + ?Sized>(v: &T) -> JsResult<JsValue> {
    v.serialize(&serde_wasm_bindgen::Serializer::json_compatible())
        .map_err(|e| js_sys::Error::new(&e.to_string()).into())
}

fn from_js<T: for<'de> Deserialize<'de>>(v: JsValue) -> JsResult<T> {
    serde_wasm_bindgen::from_value(v)
        .map_err(|e| js_sys::Error::new(&format!("invalid argument: {e}")).into())
}

fn path(s: &str) -> JsResult<DerivationPath> {
    s.parse().map_err(js_err)
}

fn endorsement_slot(n: u8) -> JsResult<crate::os::EndorsementSlot> {
    match n {
        1 => Ok(crate::os::EndorsementSlot::Slot1),
        2 => Ok(crate::os::EndorsementSlot::Slot2),
        _ => Err(js_err(Error::InvalidInput("slot must be 1 or 2".into()))),
    }
}

fn scp_emitter(cb: &Option<Function>) -> impl FnMut(crate::scp::ScpEvent) + '_ {
    move |ev| {
        if let (Some(cb), Ok(v)) = (cb, to_js(&ev)) {
            let _ = cb.call1(&JsValue::NULL, &v);
        }
    }
}

fn emitter(cb: Option<Function>) -> impl FnMut(HsmEvent) {
    move |ev: HsmEvent| {
        if let Some(cb) = &cb
            && let Ok(v) = to_js(&ev)
        {
            let _ = cb.call1(&JsValue::NULL, &v);
        }
    }
}

/// Whether the browser supports WebHID.
#[wasm_bindgen(js_name = isSupported)]
pub fn is_supported() -> bool {
    js_sys::Reflect::get(&js_sys::global(), &"navigator".into())
        .and_then(|n| js_sys::Reflect::get(&n, &"hid".into()))
        .map(|h| !h.is_undefined())
        .unwrap_or(false)
}

/// Derive single-sig addresses locally from an account xpub.
#[wasm_bindgen(js_name = btcDeriveAddresses)]
pub fn btc_derive_addresses(
    xpub: &str,
    kind: &str,
    testnet: bool,
    change: u32,
    start: u32,
    count: u32,
) -> JsResult<JsValue> {
    let kind = btc::AddressType::parse(kind).map_err(js_err)?;
    let x = btc::Xpub::parse(xpub).map_err(js_err)?;
    let mut out = Vec::new();
    for i in start..start + count {
        let (_, addr, _) =
            btc::psbt::derive_script(&x, kind, testnet, change, i).map_err(js_err)?;
        out.push(addr);
    }
    to_js(&out)
}

/// Generate a CA / root key. Returns `{ privateKey, publicKey }` (hex).
#[wasm_bindgen(js_name = generateRootKey)]
pub fn generate_root_key() -> JsResult<JsValue> {
    let k = crate::scp::RootKey::generate().map_err(js_err)?;
    to_js(
        &serde_json::json!({ "privateKey": hex::encode(k.to_bytes()), "publicKey": hex::encode(k.public_key()) }),
    )
}

/// Public key (hex) of a private key (hex).
#[wasm_bindgen(js_name = rootPublicKey)]
pub fn root_public_key(private_key: &str) -> JsResult<String> {
    Ok(hex::encode(
        crate::scp::RootKey::from_hex(private_key)
            .map_err(js_err)?
            .public_key(),
    ))
}

/// Metadata of an app ELF (name, version, flags, API level, target id...).
#[wasm_bindgen(js_name = appImageInfo)]
pub fn app_image_info(elf: &[u8]) -> JsResult<JsValue> {
    let img = crate::loader::AppImage::from_elf(elf).map_err(js_err)?;
    let mut v = serde_json::to_value(&img).map_err(|e| js_err(Error::App(e.to_string())))?;
    v["imageSize"] = img.image_size().into();
    to_js(&v)
}

fn root_or_random(key: Option<String>) -> Result<crate::scp::RootKey> {
    match key.filter(|k| !k.trim().is_empty()) {
        Some(k) => crate::scp::RootKey::from_hex(&k),
        None => crate::scp::RootKey::generate(),
    }
}

/// Estimated virtual size of a single-sig transaction.
#[wasm_bindgen(js_name = btcEstimateVsize)]
pub fn btc_estimate_vsize(kind: &str, inputs: usize, outputs: usize) -> JsResult<f64> {
    Ok(btc::psbt::estimate_vsize(
        btc::AddressType::parse(kind).map_err(js_err)?,
        inputs,
        outputs,
    ) as f64)
}

/// Parse a decimal amount into base units (string).
#[wasm_bindgen(js_name = parseUnits)]
pub fn parse_units(amount: &str, decimals: u32) -> JsResult<String> {
    eth::parse_units(amount, decimals)
        .map(|v| v.to_string())
        .map_err(js_err)
}

/// Firmware / app catalog lookups that don't need a device.
#[wasm_bindgen(js_name = catalogFor)]
pub async fn catalog_for(
    target_id: u32,
    version: String,
    provider: Option<u32>,
) -> JsResult<JsValue> {
    let m = Manager::default();
    to_js(
        &m.api
            .get_apps_by_target(target_id, &version, provider.unwrap_or(1))
            .await
            .map_err(js_err)?,
    )
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct InfoOut {
    model: Option<&'static str>,
    product_name: String,
    product_id: u16,
    app: Option<crate::os::AppAndVersion>,
    version: Option<crate::os::OsVersion>,
    /// The device is locked (PIN / screensaver): unlock it to continue.
    locked: bool,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct EthTxIn {
    chain_id: u64,
    nonce: u64,
    to: String,
    #[serde(default)]
    value: Option<String>,
    gas: u64,
    #[serde(default)]
    max_fee: Option<String>,
    #[serde(default)]
    priority_fee: Option<String>,
    #[serde(default)]
    gas_price: Option<String>,
    #[serde(default)]
    data: Option<String>,
    #[serde(default)]
    token: Option<String>,
    #[serde(default)]
    decimals: Option<u32>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct EthTxOut {
    unsigned: String,
    raw: String,
    hash: String,
    from: String,
    signature: eth::EthSignature,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct BtcSendIn {
    kind: String,
    #[serde(default)]
    account: u32,
    #[serde(default)]
    testnet: bool,
    utxos: Vec<btc::Utxo>,
    payments: Vec<btc::Payment>,
    #[serde(default)]
    change_index: Option<u32>,
    #[serde(default)]
    change_amount: Option<u64>,
    #[serde(default = "yes")]
    rbf: bool,
}

fn yes() -> bool {
    true
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct BtcSendOut {
    unsigned_psbt: String,
    signed_psbt: String,
    tx: String,
    txid: String,
    signatures: Vec<btc::PartialSignature>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct BtcAccountOut {
    key: String,
    xpub: String,
    fingerprint: String,
    path: String,
    descriptor: String,
    change_descriptor: String,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SolTxOut {
    from: String,
    base64: String,
    base58: String,
    signature: String,
}

/// A device connected over WebHID.
#[wasm_bindgen]
pub struct HwDevice {
    device: Device<WebHidTransport>,
    manager: RefCell<Manager>,
}

impl HwDevice {
    fn manager(&self) -> Manager {
        self.manager.borrow().clone()
    }
}

#[wasm_bindgen]
impl HwDevice {
    /// Open the browser device picker and connect. Must run in a user gesture.
    pub async fn request() -> JsResult<HwDevice> {
        let t = WebHidTransport::request().await.map_err(js_err)?;
        Ok(Self::from_transport(t))
    }

    /// Reconnect to an already authorized device (no picker), e.g. after the
    /// device re-enumerated (app opened / closed, firmware reboot).
    pub async fn reconnect() -> JsResult<HwDevice> {
        let t = WebHidTransport::open_granted().await.map_err(js_err)?;
        Ok(Self::from_transport(t))
    }

    /// Number of authorized devices currently plugged.
    #[wasm_bindgen(js_name = grantedCount)]
    pub async fn granted_count() -> JsResult<u32> {
        Ok(webhid::granted_devices().await.map_err(js_err)?.len() as u32)
    }

    fn from_transport(t: WebHidTransport) -> Self {
        Self {
            device: Device::new(t),
            manager: RefCell::new(Manager::default()),
        }
    }

    /// Override the manager API / HSM endpoints.
    #[wasm_bindgen(js_name = setEndpoints)]
    pub fn set_endpoints(&self, api: String, hsm: String) {
        *self.manager.borrow_mut() = Manager::new(api, hsm);
    }

    #[wasm_bindgen(getter, js_name = productName)]
    pub fn product_name(&self) -> String {
        self.device.transport().device().product_name()
    }

    #[wasm_bindgen(getter, js_name = productId)]
    pub fn product_id(&self) -> u16 {
        self.device.transport().product_id()
    }

    #[wasm_bindgen(getter)]
    pub fn model(&self) -> Option<String> {
        self.device
            .transport()
            .model()
            .map(|m| m.name().to_string())
    }

    /// The underlying browser `HIDDevice` (to match `navigator.hid` events).
    #[wasm_bindgen(getter, js_name = hidDevice)]
    pub fn hid_device(&self) -> JsValue {
        self.device.transport().device().clone().into()
    }

    #[wasm_bindgen(getter)]
    pub fn disconnected(&self) -> bool {
        self.device.transport().is_disconnected()
    }

    pub async fn close(&self) -> JsResult<()> {
        self.device.transport().close().await.map_err(js_err)
    }

    /// App + firmware information.
    pub async fn info(&self) -> JsResult<JsValue> {
        let (app, locked) = match self.device.get_app_and_version().await {
            Ok(a) => (Some(a), false),
            Err(Error::Locked) => (None, true),
            Err(_) => (None, false),
        };
        let on_dashboard = !locked && app.as_ref().map(|a| a.is_dashboard()).unwrap_or(true);
        // No device name here: reading it asks the user for consent on the
        // device. Use `getName()` explicitly.
        let version = if on_dashboard {
            self.device.get_version().await.ok()
        } else {
            None
        };
        let t = self.device.transport();
        to_js(&InfoOut {
            model: version
                .as_ref()
                .and_then(|v| v.model())
                .or(t.model())
                .map(|m| m.name()),
            product_name: t.device().product_name(),
            product_id: t.product_id(),
            app,
            version,
            locked,
        })
    }

    #[wasm_bindgen(js_name = appAndVersion)]
    pub async fn app_and_version(&self) -> JsResult<JsValue> {
        to_js(&self.device.get_app_and_version().await.map_err(js_err)?)
    }

    #[wasm_bindgen(js_name = openApp)]
    pub async fn open_app(&self, name: String) -> JsResult<()> {
        self.device.open_app(&name).await.map_err(js_err)
    }

    #[wasm_bindgen(js_name = quitApp)]
    pub async fn quit_app(&self) -> JsResult<()> {
        self.device.quit_app().await.map_err(js_err)
    }

    /// Installed apps with catalog metadata (consent required on device).
    #[wasm_bindgen(js_name = listApps)]
    pub async fn list_apps(&self) -> JsResult<JsValue> {
        to_js(
            &self
                .manager()
                .installed_apps(&self.device)
                .await
                .map_err(js_err)?,
        )
    }

    /// Device name. Asks the user for consent on the device.
    #[wasm_bindgen(js_name = getName)]
    pub async fn get_name(&self) -> JsResult<String> {
        self.device.get_device_name().await.map_err(js_err)
    }

    #[wasm_bindgen(js_name = setName)]
    pub async fn set_name(&self, name: String) -> JsResult<()> {
        self.device.set_device_name(&name).await.map_err(js_err)
    }

    /// Custom screensaver (`null` = default). Recovery mode only.
    #[wasm_bindgen(js_name = getScreenSaver)]
    pub async fn get_screen_saver(&self) -> JsResult<JsValue> {
        to_js(&self.device.get_screen_saver().await.map_err(js_err)?)
    }

    /// Set a custom screensaver. `animation`: classic | circle-rl | circle-lr;
    /// `speed`: fast | normal | slow. Recovery mode only, confirmed on device.
    #[wasm_bindgen(js_name = setScreenSaver)]
    pub async fn set_screen_saver(
        &self,
        text: String,
        animation: String,
        speed: String,
    ) -> JsResult<()> {
        let a = crate::os::ScreenSaverAnimation::parse(&animation).map_err(js_err)?;
        let sp = crate::os::ScreenSaverSpeed::parse(&speed).map_err(js_err)?;
        self.device
            .set_screen_saver(&text, a, sp)
            .await
            .map_err(js_err)
    }

    /// Restore the default screensaver. Recovery mode only, confirmed on device.
    #[wasm_bindgen(js_name = resetScreenSaver)]
    pub async fn reset_screen_saver(&self) -> JsResult<()> {
        self.device.reset_screen_saver().await.map_err(js_err)
    }

    pub async fn battery(&self) -> JsResult<JsValue> {
        to_js(&self.device.get_battery_status().await.map_err(js_err)?)
    }

    /// Raw APDU exchange (hex in, hex out including the status word).
    pub async fn apdu(&self, hex_apdu: String) -> JsResult<String> {
        let raw = hex::decode(hex_apdu.replace([' ', '\n'], ""))
            .map_err(|e| js_err(Error::InvalidInput(e.to_string())))?;
        Ok(hex::encode(
            self.device.exchange_raw(&raw).await.map_err(js_err)?,
        ))
    }

    #[wasm_bindgen(js_name = genuineCheck)]
    pub async fn genuine_check(&self, on_event: Option<Function>) -> JsResult<bool> {
        self.manager()
            .genuine_check(&self.device, &mut emitter(on_event))
            .await
            .map_err(js_err)
    }

    /// Offline genuine check: verify the device attestation locally (no
    /// network). The device asks to allow an insecure manager connection.
    /// `extraIssuerKeys`: optional array of hex issuer public keys to trust.
    #[wasm_bindgen(js_name = offlineGenuineCheck)]
    pub async fn offline_genuine_check(
        &self,
        on_event: Option<Function>,
        extra_issuer_keys: Option<Vec<String>>,
    ) -> JsResult<JsValue> {
        let extra = extra_issuer_keys
            .unwrap_or_default()
            .iter()
            .enumerate()
            .map(|(i, k)| {
                hex::decode(k.trim().trim_start_matches("0x"))
                    .map(|b| (format!("custom issuer key #{}", i + 1), b))
                    .map_err(|e| js_err(Error::InvalidInput(format!("issuer key: {e}"))))
            })
            .collect::<JsResult<Vec<_>>>()?;
        let mut cb = |ev: crate::attestation::AttestationEvent| {
            if let (Some(cb), Ok(v)) = (&on_event, to_js(&ev)) {
                let _ = cb.call1(&JsValue::NULL, &v);
            }
        };
        let att = crate::attestation::offline_genuine_check(&self.device, &extra, &mut cb)
            .await
            .map_err(js_err)?;
        to_js(&att)
    }

    /// App catalog for this device.
    pub async fn catalog(&self) -> JsResult<JsValue> {
        let os = self.device.get_version().await.map_err(js_err)?;
        to_js(&self.manager().catalog(&os).await.map_err(js_err)?)
    }

    /// Install an app by name (and its parent if missing). Events: `{app, ...}`.
    /// `installed`: names of the installed apps if already known (avoids a
    /// second listing approval on the device).
    #[wasm_bindgen(js_name = installApp)]
    pub async fn install_app(
        &self,
        name: String,
        on_event: Option<Function>,
        installed: Option<Vec<String>>,
    ) -> JsResult<()> {
        let mut cb = |app: &str, ev: HsmEvent| {
            if let (Some(cb), Ok(v)) = (&on_event, to_js(&ev)) {
                let _ = js_sys::Reflect::set(&v, &"app".into(), &app.into());
                let _ = cb.call1(&JsValue::NULL, &v);
            }
        };
        self.manager()
            .install_app_with_deps(&self.device, &name, installed.as_deref(), &mut cb)
            .await
            .map_err(js_err)
    }

    #[wasm_bindgen(js_name = uninstallApp)]
    pub async fn uninstall_app(&self, name: String, on_event: Option<Function>) -> JsResult<()> {
        self.manager()
            .uninstall_app_by_name(&self.device, &name, &mut emitter(on_event))
            .await
            .map_err(js_err)
    }

    /// Firmware update available? Returns the update descriptor or `null`.
    #[wasm_bindgen(js_name = checkFirmware)]
    pub async fn check_firmware(&self) -> JsResult<JsValue> {
        let os = self.device.get_version().await.map_err(js_err)?;
        let u = self
            .manager()
            .check_firmware_update(&os)
            .await
            .map_err(js_err)?;
        to_js(&u)
    }

    /// Install the OS updater (step 1). The device reboots afterwards.
    #[wasm_bindgen(js_name = installOsu)]
    pub async fn install_osu(&self, update: JsValue, on_event: Option<Function>) -> JsResult<()> {
        let u: FirmwareUpdate = from_js(update)?;
        self.manager()
            .install_osu(&self.device, &u, &mut emitter(on_event))
            .await
            .map_err(js_err)
    }

    /// Flash MCU / bootloader (step 2, bootloader mode only). Returns "mcu" or "bootloader".
    #[wasm_bindgen(js_name = flashMcu)]
    pub async fn flash_mcu(
        &self,
        update: JsValue,
        on_event: Option<Function>,
    ) -> JsResult<JsValue> {
        let u: FirmwareUpdate = from_js(update)?;
        to_js(
            &self
                .manager()
                .flash_mcu(&self.device, &u, &mut emitter(on_event))
                .await
                .map_err(js_err)?,
        )
    }

    /// Bootloader mode recovery: flash the MCU required by the installed
    /// firmware. Returns "mcu", "bootloader" or `null` if not in bootloader.
    #[wasm_bindgen(js_name = repairMcu)]
    pub async fn repair_mcu(&self, on_event: Option<Function>) -> JsResult<JsValue> {
        to_js(
            &self
                .manager()
                .repair_mcu(&self.device, &mut emitter(on_event))
                .await
                .map_err(js_err)?,
        )
    }

    // ---- Device settings ----

    /// Restart the dashboard.
    pub async fn reset(&self) -> JsResult<()> {
        self.device.reset().await.map_err(js_err)
    }

    /// Bluetooth MAC address (BLE models).
    #[wasm_bindgen(js_name = getMac)]
    pub async fn get_mac(&self) -> JsResult<String> {
        self.device.get_device_mac().await.map_err(js_err)
    }

    /// Battery charge limit (percent).
    #[wasm_bindgen(js_name = setChargeLimit)]
    pub async fn set_charge_limit(&self, percent: u8) -> JsResult<()> {
        self.device
            .set_battery_charge_limit(percent)
            .await
            .map_err(js_err)
    }

    // ---- Language packs ----

    /// Installed language packs `[{id, size}]`.
    #[wasm_bindgen(js_name = listLanguagePacks)]
    pub async fn list_language_packs(&self) -> JsResult<JsValue> {
        to_js(&self.device.list_language_packs().await.map_err(js_err)?)
    }

    /// Language packs available for this firmware.
    #[wasm_bindgen(js_name = availableLanguagePacks)]
    pub async fn available_language_packs(&self) -> JsResult<JsValue> {
        let os = self.device.get_version().await.map_err(js_err)?;
        to_js(&self.manager().language_packs(&os).await.map_err(js_err)?)
    }

    /// Install a language pack by name (`french`...). Progress: `(sent, total)`.
    #[wasm_bindgen(js_name = installLanguagePack)]
    pub async fn install_language_pack(
        &self,
        language: String,
        on_progress: Option<Function>,
    ) -> JsResult<()> {
        let os = self.device.get_version().await.map_err(js_err)?;
        let m = self.manager();
        let pack = m
            .language_packs(&os)
            .await
            .map_err(js_err)?
            .into_iter()
            .find(|p| p.language.eq_ignore_ascii_case(&language))
            .ok_or_else(|| {
                js_err(Error::NotFound(format!(
                    "no {language} pack for this firmware"
                )))
            })?;
        let mut cb = |i: usize, n: usize| {
            if let Some(f) = &on_progress {
                let _ = f.call2(&JsValue::NULL, &(i as u32).into(), &(n as u32).into());
            }
        };
        m.install_language_pack(&self.device, &pack, &mut cb)
            .await
            .map_err(js_err)
    }

    /// Install a language pack from a signed APDU script (file contents).
    #[wasm_bindgen(js_name = installLanguageScript)]
    pub async fn install_language_script(
        &self,
        script: String,
        on_progress: Option<Function>,
    ) -> JsResult<()> {
        let mut cb = |i: usize, n: usize| {
            if let Some(f) = &on_progress {
                let _ = f.call2(&JsValue::NULL, &(i as u32).into(), &(n as u32).into());
            }
        };
        self.device
            .install_language_script(&script, &mut cb)
            .await
            .map_err(js_err)
    }

    /// Replay an APDU script (one hex APDU per line).
    #[wasm_bindgen(js_name = runApduScript)]
    pub async fn run_apdu_script(
        &self,
        script: String,
        on_progress: Option<Function>,
    ) -> JsResult<()> {
        let mut cb = |i: usize, n: usize| {
            if let Some(f) = &on_progress {
                let _ = f.call2(&JsValue::NULL, &(i as u32).into(), &(n as u32).into());
            }
        };
        self.device
            .run_apdu_script(&script, &mut cb)
            .await
            .map_err(js_err)
    }

    /// Remove every language pack (back to English).
    #[wasm_bindgen(js_name = removeLanguagePacks)]
    pub async fn remove_language_packs(&self) -> JsResult<()> {
        self.manager()
            .remove_language_packs(&self.device)
            .await
            .map_err(js_err)
    }

    // ---- Endorsement ----

    /// Endorsement key and certificate of slot 1 or 2.
    #[wasm_bindgen(js_name = endorsementInfo)]
    pub async fn endorsement_info(&self, slot: u8) -> JsResult<JsValue> {
        to_js(
            &self
                .device
                .endorsement_info(endorsement_slot(slot)?)
                .await
                .map_err(js_err)?,
        )
    }

    /// Create the slot key and certify it with `ownerKey` (hex private key).
    /// Returns `{ publicKey, certificate }`.
    #[wasm_bindgen(js_name = endorseSetup)]
    pub async fn endorse_setup(&self, slot: u8, owner_key: String) -> JsResult<JsValue> {
        let owner = crate::scp::RootKey::from_hex(&owner_key).map_err(js_err)?;
        let (public, _) = self
            .device
            .endorse_start(endorsement_slot(slot)?)
            .await
            .map_err(js_err)?;
        let cert = crate::scp::endorsement_certificate(&owner, &public).map_err(js_err)?;
        self.device.endorse_commit(&cert).await.map_err(js_err)?;
        to_js(
            &serde_json::json!({ "publicKey": hex::encode(&public), "certificate": hex::encode(&cert) }),
        )
    }

    // ---- Secure channel (custom CA, sideloading) ----

    /// Install a custom CA (recovery mode). `recover` uses the Recover channel.
    #[wasm_bindgen(js_name = caInstall)]
    pub async fn ca_install(
        &self,
        name: String,
        public_key: String,
        recover: bool,
        on_event: Option<Function>,
    ) -> JsResult<()> {
        let pk = hex::decode(public_key.trim().trim_start_matches("0x"))
            .map_err(|e| js_err(Error::InvalidInput(e.to_string())))?;
        let root = crate::scp::RootKey::generate().map_err(js_err)?;
        let mode = if recover {
            crate::scp::ScpMode::Recover
        } else {
            crate::scp::ScpMode::Default
        };
        let mut chan = crate::scp::SecureChannel::open_mode(
            &self.device,
            &root,
            mode,
            &mut scp_emitter(&on_event),
        )
        .await
        .map_err(js_err)?;
        if recover {
            chan.recover_set_ca(&name, &pk).await
        } else {
            chan.setup_custom_ca(&name, &pk).await
        }
        .map_err(js_err)
    }

    /// Remove the custom CA (recovery mode). The Recover channel needs the
    /// CA name and public key.
    #[wasm_bindgen(js_name = caRemove)]
    pub async fn ca_remove(
        &self,
        recover: bool,
        name: Option<String>,
        public_key: Option<String>,
        on_event: Option<Function>,
    ) -> JsResult<()> {
        let root = crate::scp::RootKey::generate().map_err(js_err)?;
        if recover {
            let pk = hex::decode(
                public_key
                    .unwrap_or_default()
                    .trim()
                    .trim_start_matches("0x"),
            )
            .map_err(|e| js_err(Error::InvalidInput(e.to_string())))?;
            let mut chan = crate::scp::SecureChannel::open_mode(
                &self.device,
                &root,
                crate::scp::ScpMode::Recover,
                &mut scp_emitter(&on_event),
            )
            .await
            .map_err(js_err)?;
            chan.recover_delete_ca(&name.unwrap_or_default(), &pk)
                .await
                .map_err(js_err)
        } else {
            let mut chan =
                crate::scp::SecureChannel::open(&self.device, &root, &mut scp_emitter(&on_event))
                    .await
                    .map_err(js_err)?;
            chan.reset_custom_ca().await.map_err(js_err)
        }
    }

    /// Run a secure command set with `rootKey` (hex; empty = random key, the
    /// device then asks to allow an unknown manager). `op`: `memoryInfo`,
    /// `listApps`, `hashFirmware`, `deleteAllApps`, `resetEndorsement`,
    /// `deleteApp` (arg: name), `deleteAppByHash` (arg: hex hash).
    pub async fn secure(
        &self,
        op: String,
        root_key: Option<String>,
        arg: Option<String>,
        on_event: Option<Function>,
    ) -> JsResult<JsValue> {
        let root = root_or_random(root_key).map_err(js_err)?;
        let mut chan =
            crate::scp::SecureChannel::open(&self.device, &root, &mut scp_emitter(&on_event))
                .await
                .map_err(js_err)?;
        let arg = arg.unwrap_or_default();
        let r: Result<JsValue> = match op.as_str() {
            "memoryInfo" => chan
                .get_memory_info()
                .await
                .and_then(|v| to_js(&v).map_err(|_| Error::App("js".into()))),
            "listApps" => chan
                .list_apps()
                .await
                .and_then(|v| to_js(&v).map_err(|_| Error::App("js".into()))),
            "hashFirmware" => {
                let mut it = arg.split(',').map(|v| {
                    u32::from_str_radix(v.trim().trim_start_matches("0x"), 16).unwrap_or(0)
                });
                let (a, l) = (it.next().unwrap_or(0), it.next().unwrap_or(0));
                chan.hash_firmware(a, l)
                    .await
                    .map(|h| hex::encode(h).into())
            }
            "deleteAllApps" => chan.delete_all_apps().await.map(|_| JsValue::UNDEFINED),
            "resetEndorsement" => chan.reset_endorsement().await.map(|_| JsValue::UNDEFINED),
            "deleteApp" => chan.delete_app(&arg).await.map(|_| JsValue::UNDEFINED),
            "deleteAppByHash" => {
                let h: [u8; 32] = hex::decode(arg.trim())
                    .ok()
                    .and_then(|v| v.try_into().ok())
                    .ok_or_else(|| {
                        js_err(Error::InvalidInput("hash must be 32 bytes hex".into()))
                    })?;
                chan.delete_app_by_hash(&h)
                    .await
                    .map(|_| JsValue::UNDEFINED)
            }
            other => Err(Error::InvalidInput(format!(
                "unknown secure operation {other:?}"
            ))),
        };
        r.map_err(js_err)
    }

    /// Sideload an app ELF with the custom CA private key (hex).
    /// Events: handshake events, then `{type: "loading", sent, total}`.
    #[wasm_bindgen(js_name = sideload)]
    pub async fn sideload(
        &self,
        elf: Vec<u8>,
        ca_key: String,
        replace: bool,
        on_event: Option<Function>,
        sign: Option<bool>,
    ) -> JsResult<()> {
        let img = crate::loader::AppImage::from_elf(&elf).map_err(js_err)?;
        let root = crate::scp::RootKey::from_hex(&ca_key).map_err(js_err)?;
        let mut chan =
            crate::scp::SecureChannel::open(&self.device, &root, &mut scp_emitter(&on_event))
                .await
                .map_err(js_err)?;
        let mut progress = |sent: usize, total: usize| {
            if let Some(f) = &on_event {
                let v = serde_json::json!({ "type": "loading", "sent": sent, "total": total });
                if let Ok(v) = to_js(&v) {
                    let _ = f.call1(&JsValue::NULL, &v);
                }
            }
        };
        let signer = if sign.unwrap_or(false) {
            Some(&root)
        } else {
            None
        };
        let opts = crate::loader::InstallOptions { replace, signer };
        crate::loader::install_app_with(&mut chan, &img, &opts, &mut progress)
            .await
            .map_err(js_err)
    }

    // ---- Factory (devices not yet personalized) ----

    /// `FACTORY_TEST` raw (hex data), returns hex.
    #[wasm_bindgen(js_name = factoryTest)]
    pub async fn factory_test(&self, p1: u8, p2: u8, data: String) -> JsResult<String> {
        let d = hex::decode(data.trim()).map_err(|e| js_err(Error::InvalidInput(e.to_string())))?;
        Ok(hex::encode(
            self.device.factory_test(p1, p2, &d).await.map_err(js_err)?,
        ))
    }

    /// `GET_DEVICE_PUBLIC_KEY` (personalization), hex.
    #[wasm_bindgen(js_name = factoryPublicKey)]
    pub async fn factory_public_key(&self) -> JsResult<String> {
        Ok(hex::encode(
            self.device
                .factory_get_device_public_key()
                .await
                .map_err(js_err)?,
        ))
    }

    /// `GET_CXPORT`, hex.
    #[wasm_bindgen(js_name = factoryCxport)]
    pub async fn factory_cxport(&self) -> JsResult<String> {
        Ok(hex::encode(
            self.device.factory_get_cxport().await.map_err(js_err)?,
        ))
    }

    // ---- Ethereum ----

    #[wasm_bindgen(js_name = ethConfig)]
    pub async fn eth_config(&self) -> JsResult<JsValue> {
        to_js(
            &eth::EthApp::new(&self.device)
                .get_configuration()
                .await
                .map_err(js_err)?,
        )
    }

    #[wasm_bindgen(js_name = ethAddress)]
    pub async fn eth_address(&self, path_str: String, display: bool) -> JsResult<JsValue> {
        let a = eth::EthApp::new(&self.device)
            .get_address(&path(&path_str)?, display, false)
            .await
            .map_err(js_err)?;
        to_js(&a)
    }

    /// Build (with outscript) and sign a transaction.
    #[wasm_bindgen(js_name = ethSignTx)]
    pub async fn eth_sign_tx(&self, path_str: String, tx: JsValue) -> JsResult<JsValue> {
        let p = path(&path_str)?;
        let input: EthTxIn = from_js(tx)?;
        let app = eth::EthApp::new(&self.device);
        let from = app
            .get_address(&p, false, false)
            .await
            .map_err(js_err)?
            .address;
        let mut tx = build_eth_tx(&input).map_err(js_err)?;
        let unsigned = tx
            .sign_bytes()
            .map_err(|e| js_err(Error::InvalidInput(format!("{e:?}"))))?;
        let signature = app.sign_tx(&p, &mut tx).await.map_err(js_err)?;
        let raw = tx
            .to_bytes()
            .map_err(|e| js_err(Error::App(format!("{e:?}"))))?;
        let hash = tx
            .hash()
            .map_err(|e| js_err(Error::App(format!("{e:?}"))))?;
        to_js(&EthTxOut {
            unsigned: format!("0x{}", hex::encode(unsigned)),
            raw: format!("0x{}", hex::encode(raw)),
            hash: format!("0x{}", hex::encode(hash)),
            from,
            signature,
        })
    }

    #[wasm_bindgen(js_name = ethSignMessage)]
    pub async fn eth_sign_message(&self, path_str: String, message: String) -> JsResult<String> {
        let s = eth::EthApp::new(&self.device)
            .sign_personal_message(&path(&path_str)?, message.as_bytes())
            .await
            .map_err(js_err)?;
        Ok(format!("0x{}", hex::encode(s.to_rsv())))
    }

    #[wasm_bindgen(js_name = ethSignTypedHashes)]
    pub async fn eth_sign_typed_hashes(
        &self,
        path_str: String,
        domain: String,
        message: String,
    ) -> JsResult<String> {
        let h = |s: &str| -> JsResult<[u8; 32]> {
            hex::decode(s.trim_start_matches("0x"))
                .ok()
                .and_then(|v| v.try_into().ok())
                .ok_or_else(|| js_err(Error::InvalidInput("expected a 32-byte hex hash".into())))
        };
        let s = eth::EthApp::new(&self.device)
            .sign_eip712_hashed(&path(&path_str)?, &h(&domain)?, &h(&message)?)
            .await
            .map_err(js_err)?;
        Ok(format!("0x{}", hex::encode(s.to_rsv())))
    }

    // ---- Bitcoin ----

    /// Account key information for a default single-sig account.
    #[wasm_bindgen(js_name = btcAccount)]
    pub async fn btc_account(
        &self,
        kind: String,
        account: u32,
        testnet: bool,
    ) -> JsResult<JsValue> {
        let kind = btc::AddressType::parse(&kind).map_err(js_err)?;
        let key = btc::BtcApp::new(&self.device)
            .get_account_key(kind, testnet, account)
            .await
            .map_err(js_err)?;
        let policy = btc::WalletPolicy::default_single_sig(kind, key.clone());
        to_js(&BtcAccountOut {
            key: key.to_string(),
            xpub: key.xpub.clone(),
            fingerprint: hex::encode(key.fingerprint.unwrap_or_default()),
            path: key.path.to_string(),
            descriptor: policy.descriptor(false),
            change_descriptor: policy.descriptor(true),
        })
    }

    #[wasm_bindgen(js_name = btcXpub)]
    pub async fn btc_xpub(&self, path_str: String, display: bool) -> JsResult<String> {
        btc::BtcApp::new(&self.device)
            .get_extended_pubkey(&path(&path_str)?, display)
            .await
            .map_err(js_err)
    }

    /// Address from the device, checked against local derivation.
    #[wasm_bindgen(js_name = btcAddress)]
    pub async fn btc_address(
        &self,
        kind: String,
        account: u32,
        testnet: bool,
        change: bool,
        index: u32,
        display: bool,
    ) -> JsResult<String> {
        let kind = btc::AddressType::parse(&kind).map_err(js_err)?;
        let app = btc::BtcApp::new(&self.device);
        let key = app
            .get_account_key(kind, testnet, account)
            .await
            .map_err(js_err)?;
        app.get_wallet_address_checked(kind, testnet, &key, change, index, display)
            .await
            .map_err(js_err)
    }

    /// Sign a message, returns the base64 signature.
    #[wasm_bindgen(js_name = btcSignMessage)]
    pub async fn btc_sign_message(&self, path_str: String, message: String) -> JsResult<String> {
        let sig = btc::BtcApp::new(&self.device)
            .sign_message(&path(&path_str)?, message.as_bytes())
            .await
            .map_err(js_err)?;
        Ok(outscript::base64::encode(&sig))
    }

    /// Sign a PSBT (base64) with a default account. Returns `{signedPsbt, tx?}`.
    #[wasm_bindgen(js_name = btcSignPsbt)]
    pub async fn btc_sign_psbt(
        &self,
        kind: String,
        account: u32,
        testnet: bool,
        psbt_b64: String,
    ) -> JsResult<JsValue> {
        let kind = btc::AddressType::parse(&kind).map_err(js_err)?;
        let bytes = outscript::psbt::Psbt::decode_base64(psbt_b64.trim())
            .map_err(|e| js_err(Error::InvalidInput(format!("{e:?}"))))?;
        let app = btc::BtcApp::new(&self.device);
        let policy = app
            .default_policy(kind, testnet, account)
            .await
            .map_err(js_err)?;
        let signed = app
            .sign_psbt_into(&bytes, &policy, None)
            .await
            .map_err(js_err)?;
        let tx = btc::psbt::finalize_and_extract(&signed)
            .ok()
            .map(hex::encode);
        let out = serde_json::json!({ "signedPsbt": outscript::base64::encode(&signed), "tx": tx });
        to_js(&out)
    }

    /// Build a PSBT with outscript from UTXOs, sign it and finalize it.
    #[wasm_bindgen(js_name = btcSend)]
    pub async fn btc_send(&self, request: JsValue) -> JsResult<JsValue> {
        let req: BtcSendIn = from_js(request)?;
        let kind = btc::AddressType::parse(&req.kind).map_err(js_err)?;
        let app = btc::BtcApp::new(&self.device);
        let key = app
            .get_account_key(kind, req.testnet, req.account)
            .await
            .map_err(js_err)?;
        let xpub = btc::Xpub::parse(&key.xpub).map_err(js_err)?;
        let change = match (req.change_index, req.change_amount) {
            (Some(i), Some(a)) if a > 0 => Some((i, a)),
            _ => None,
        };
        let spend = btc::psbt::SpendRequest {
            kind,
            testnet: req.testnet,
            master_fingerprint: key.fingerprint.unwrap_or_default(),
            account_path: key.path.components(),
            account_xpub: &xpub,
            utxos: &req.utxos,
            payments: &req.payments,
            change,
            rbf: req.rbf,
        };
        let unsigned = btc::psbt::build_psbt(&spend).map_err(js_err)?;
        let policy = btc::WalletPolicy::default_single_sig(kind, key);
        let signatures = app
            .sign_psbt(&unsigned, &policy, None)
            .await
            .map_err(js_err)?;
        let signed = btc::psbt::insert_signatures(&unsigned, &signatures).map_err(js_err)?;
        let tx = btc::psbt::finalize_and_extract(&signed).map_err(js_err)?;
        let txid = outscript::btctx::BtcTx::from_bytes(&tx)
            .map(|t| hex::encode(t.hash()))
            .unwrap_or_default();
        to_js(&BtcSendOut {
            unsigned_psbt: outscript::base64::encode(&unsigned),
            signed_psbt: outscript::base64::encode(&signed),
            tx: hex::encode(tx),
            txid,
            signatures,
        })
    }

    // ---- Solana ----

    #[wasm_bindgen(js_name = solConfig)]
    pub async fn sol_config(&self) -> JsResult<JsValue> {
        to_js(
            &sol::SolApp::new(&self.device)
                .get_configuration()
                .await
                .map_err(js_err)?,
        )
    }

    #[wasm_bindgen(js_name = solAddress)]
    pub async fn sol_address(&self, path_str: String, display: bool) -> JsResult<String> {
        sol::SolApp::new(&self.device)
            .get_address(&path(&path_str)?, display)
            .await
            .map_err(js_err)
    }

    /// Build (with outscript) and sign a SOL transfer. `amount` in SOL.
    #[wasm_bindgen(js_name = solTransfer)]
    pub async fn sol_transfer(
        &self,
        path_str: String,
        to: String,
        amount: String,
        blockhash: String,
    ) -> JsResult<JsValue> {
        let p = path(&path_str)?;
        let app = sol::SolApp::new(&self.device);
        let from = app.get_address(&p, false).await.map_err(js_err)?;
        let lamports: u64 = eth::parse_units(&amount, 9)
            .map_err(js_err)?
            .try_into()
            .map_err(|_| js_err(Error::InvalidInput("amount too large".into())))?;
        let mut tx = sol::build_transfer(&from, &to, lamports, &blockhash).map_err(js_err)?;
        let sig = app.sign_tx(&p, &mut tx).await.map_err(js_err)?;
        let raw = tx
            .to_bytes()
            .map_err(|e| js_err(Error::App(format!("{e:?}"))))?;
        to_js(&SolTxOut {
            from,
            base64: outscript::base64::encode(&raw),
            base58: outscript::base58::encode(&raw),
            signature: outscript::base58::encode(&sig),
        })
    }
}

fn build_eth_tx(i: &EthTxIn) -> Result<eth::EvmTx> {
    let mut tx = eth::EvmTx::default();
    tx.chain_id = i.chain_id;
    tx.nonce = i.nonce;
    tx.gas = i.gas;
    let value = i
        .value
        .as_deref()
        .filter(|v| !v.trim().is_empty())
        .unwrap_or("0");
    match i.token.as_deref().filter(|t| !t.is_empty()) {
        Some(token) => {
            tx.to = token.to_string();
            tx.data = eth::erc20_transfer_data(
                &i.to,
                &eth::parse_units(value, i.decimals.unwrap_or(18))?,
            )?;
        }
        None => {
            tx.to = i.to.clone();
            tx.value = eth::parse_units(value, 18)?;
            if let Some(d) = i.data.as_deref().filter(|d| !d.is_empty() && *d != "0x") {
                tx.data = hex::decode(d.trim_start_matches("0x"))
                    .map_err(|e| Error::InvalidInput(e.to_string()))?;
            }
        }
    }
    let gwei = |s: &str| eth::parse_units(s, 9);
    match i.gas_price.as_deref().filter(|g| !g.is_empty()) {
        Some(gp) => {
            tx.tx_type = eth::EvmTxType::Legacy;
            tx.gas_fee_cap = gwei(gp)?;
        }
        None => {
            tx.tx_type = eth::EvmTxType::Eip1559;
            tx.gas_tip_cap = gwei(i.priority_fee.as_deref().unwrap_or("1"))?;
            tx.gas_fee_cap = gwei(i.max_fee.as_deref().ok_or_else(|| {
                Error::InvalidInput("maxFee (gwei) or gasPrice is required".into())
            })?)?;
        }
    }
    Ok(tx)
}