emerald-hwkey 0.7.0

Emerald HWKey - Harware Cryptocurrency Key Access
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
use std::str::from_utf8;
use std::sync::{Arc, Mutex};
use crate::{
    errors::HWKeyError,
    ledger::{
        apdu::ApduBuilder,
        app::{AsChainCode, AsExtendedKey, AsPubkey, LedgerApp, PubkeyAddressApp},
        comm::{sendrecv, LedgerTransport},
        commons::as_compact
    },
};
use std::convert::TryFrom;
use hdpath::{AccountHDPath, HDPath, Purpose};
use bitcoin::{
    secp256k1::PublicKey,
    bip32::ChainCode
};
use crate::ledger::connect::direct::CHUNK_SIZE;

/// ECDSA crypto signature length in bytes
pub const ECDSA_SIGNATURE_BYTES: usize = 65;

const COMMAND_GET_ADDRESS: u8 = 0x02;
const COMMAND_SIGN_TRANSACTION: u8 = 0x04;
const COMMAND_SIGN_MESSAGE: u8 = 0x08;
const COMMAND_APP_CONFIG: u8 = 0x06;
const COMMAND_SIGN_EIP712: u8 = 0x0C;

/// The signature as it's used in Ethereum
/// R-S-V as 65 bytes
/// i.e., 32 bytes for R, 32 bytes for S, and 1 byte for V
///
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct SignatureBytes([u8; ECDSA_SIGNATURE_BYTES]);

impl SignatureBytes {

    /// Ledger answers with V-R-S, but Ethereum uses R-S-V, so here we move that byte to the end (and move others to the left)
    pub fn from_ledger_bytes(bytes: &[u8]) -> Result<Self, HWKeyError> {
        if bytes.len() != ECDSA_SIGNATURE_BYTES {
            return Err(HWKeyError::CryptoError(format!(
                "Invalid signature length. Expected: {}, received: {}",
                ECDSA_SIGNATURE_BYTES, bytes.len()
            )));
        }
        let mut val: [u8; ECDSA_SIGNATURE_BYTES] = [0; ECDSA_SIGNATURE_BYTES];
        val[0..64].copy_from_slice(&bytes[1..65]);
        val[64] = bytes[0];
        Ok(SignatureBytes(val))
    }

    pub fn to_vec(&self) -> Vec<u8> {
        self.0.to_vec()
    }

    /// Convert to the original V-R-S format as it was received from Ledger
    pub fn to_ledger_bytes(&self) -> [u8; ECDSA_SIGNATURE_BYTES] {
        let mut vrs = [0; ECDSA_SIGNATURE_BYTES];
        vrs[1..65].copy_from_slice(&self.0[0..64]);
        vrs[0] = self.0[64];
        vrs
    }
}

impl std::fmt::Debug for SignatureBytes {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "0x{}", hex::encode(self.0))
    }
}

impl AsRef<[u8]> for SignatureBytes {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl AsRef<[u8; ECDSA_SIGNATURE_BYTES]> for SignatureBytes {
    fn as_ref(&self) -> &[u8; ECDSA_SIGNATURE_BYTES] {
        &self.0
    }
}

pub struct EthereumApp {
    ledger: Arc<Mutex<dyn LedgerTransport>>
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct AddressResponse {
    pub pubkey: PublicKey,
    pub address: String,
    pub chaincode: ChainCode
}

impl AsPubkey for AddressResponse {
    fn as_pubkey(&self) -> &PublicKey {
        &self.pubkey
    }
}

impl AsChainCode for AddressResponse {
    fn as_chaincode(&self) -> &ChainCode {
        &self.chaincode
    }
}

impl AsExtendedKey for AddressResponse {}

impl TryFrom<Vec<u8>> for AddressResponse {
    type Error = HWKeyError;

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        if value.is_empty() {
            return Err(HWKeyError::EncodingError("Empty data".to_string()))
        }
        let pubkey_len = value[0] as usize;
        if 1 + pubkey_len > value.len() {
            return Err(HWKeyError::EncodingError(
                format!("Pubkey cutoff. {:?} > {:?} for {:?}", 1 + pubkey_len, value.len(), pubkey_len)
            ))
        }
        let pubkey = &value[1..pubkey_len+1];
        let pubkey = PublicKey::from_slice(pubkey)
            .map_err(|_| HWKeyError::CryptoError("Invalid public key".to_string()))?;
        let pubkey_comp = as_compact(&pubkey)?;

        let address_len = value[pubkey_len + 1] as usize;
        let address_start = 1 + pubkey_len + 1;
        let address_end = address_start + address_len;
        if address_end > value.len() {
            return Err(HWKeyError::EncodingError(
                format!("Address cutoff. {:?} > {:?} as {:?}..{:?} for {:?}", address_end, value.len(), address_start, address_end, address_len)
            ))
        }
        let address = &value[address_start..address_end];
        let address = from_utf8(address)
            .map(|a| a.to_string())
            .map(|a| if a.starts_with("0x") { a } else { format!("0x{}", a)} )
            .map_err(|e| HWKeyError::EncodingError(format!("Can't parse address: {}", e)))?;

        let chaincode_len = 32_usize;
        let chaincode_start = address_end;
        let chaincode_end = chaincode_start + chaincode_len;
        if chaincode_end > value.len() {
            return Err(HWKeyError::EncodingError(
                format!("Chaincode cutoff. {:?} > {:?}", chaincode_end, value.len())
            ))
        }
        let chaincode = value[chaincode_start..chaincode_end].to_vec();
        let chaincode = ChainCode::try_from(chaincode.as_slice()).unwrap();

        Ok(AddressResponse {
            pubkey: pubkey_comp, address, chaincode
        })
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct AppVersion {
    ///  arbitrary data signature enabled by user
    pub data_sign_enabled: bool,
    /// ERC 20 Token information needs to be provided externally
    pub external_erc20: bool,
    pub version_major: u8,
    pub version_minor: u8,
    pub version_patch: u8,
}

impl TryFrom<Vec<u8>> for AppVersion {
    type Error = ();

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        if value.len() < 4 {
            return Err(())
        }
        let flags = value[0];
        Ok(AppVersion {
            data_sign_enabled: flags & 0x01 > 0,
            external_erc20: flags & 0x02 > 0,
            version_major: value[1],
            version_minor: value[2],
            version_patch: value[3]
        })
    }
}

impl EthereumApp {

    /// Split data into chunks for sending to Ledger device
    /// 
    /// # Arguments
    /// * `data` - The data to be chunked
    /// * `hd_path` - HD path that will be included in the initial chunk
    /// 
    /// # Returns
    /// A tuple of (initial_chunk, continuation_data) where:
    /// * initial_chunk - First chunk including HD path data
    /// * continuation_data - Remaining data to be sent in subsequent chunks
    fn chunk_data<'a>(&self, data: &'a [u8], hd_path: &dyn HDPath) -> (&'a [u8], &'a [u8]) {
        match data.len() {
            0..=CHUNK_SIZE => (data, &[]),
            _ => data.split_at(CHUNK_SIZE - hd_path.to_bytes().len()),
        }
    }

    /// Send chunked data to the Ledger device using the standard chunking protocol
    ///
    /// # Protocol Details:
    ///
    /// ```no_run,ignore
    ///   P1: 00 : first message data block
    ///       80 : subsequent message data block
    ///   P2: 00 : always
    /// ```
    /// 
    /// # Arguments
    /// * `command` - The APDU command to use
    /// * `hd_path` - HD path for the initial chunk
    /// * `data` - The data to be sent in chunks
    /// 
    /// # Returns
    /// The response from the device after sending all chunks
    fn send_chunked_data(&self, command: u8, hd_path: &dyn HDPath, data: &[u8]) -> Result<Vec<u8>, HWKeyError> {
        let (init, cont) = self.chunk_data(data, hd_path);

        let init_apdu = ApduBuilder::new(command)
            .with_p1(0x00)
            .with_data(hd_path.to_bytes().as_slice())
            .with_data(init)
            .build();

        let handle = self.ledger.lock().unwrap();
        let mut res = sendrecv(&*handle, &init_apdu)?;

        for chunk in cont.chunks(CHUNK_SIZE) {
            let apdu_cont = ApduBuilder::new(command)
                .with_p1(0x80)
                .with_data(chunk)
                .build();
            res = sendrecv(&*handle, &apdu_cont)?;
        }

        Ok(res)
    }

    /// Get address
    ///
    /// # Arguments:
    /// hd_path - HD path, prefixed with count of derivation indexes
    ///
    pub fn get_address(&self, hd_path: &dyn HDPath, confirm: bool) -> Result<AddressResponse, HWKeyError> {
        let apdu = ApduBuilder::new(COMMAND_GET_ADDRESS)
            // 00 : return address
            // 01 : display address and confirm before returning
            .with_p1(if confirm {0x01} else {0x00})
            // 01 : return the chain code
            .with_p2(0x01)
            .with_data(hd_path.to_bytes().as_slice())
            .build();

        let ledger = self.ledger.lock().unwrap();


        // let mut handle = self.ledger.lock().unwrap().deref();
        sendrecv(&*ledger, &apdu)
            .and_then(AddressResponse::try_from)
    }

    /// Sign transaction
    ///
    /// # Arguments:
    /// tx - RLP encoded transaction
    /// hd_path - HD path, prefixed with count of derivation indexes
    ///
    /// # Protocol Details:
    /// According to the Ledger Ethereum specification (reference/ledger-ethereum.adoc), 
    /// transaction data is sent as:
    /// - First block: HD path + RLP transaction chunk (no length prefix)
    /// - Other blocks: RLP transaction chunk only
    ///   The RLP transaction data is sent directly without any length prefix.
    ///
    pub fn sign_transaction(
        &self,
        tx: &[u8],
        hd_path: &dyn HDPath,
    ) -> Result<SignatureBytes, HWKeyError> {

        // Send RLP transaction data directly without length prefix
        // as per SIGN ETH TRANSACTION specification
        let res = self.send_chunked_data(COMMAND_SIGN_TRANSACTION, hd_path, tx)?;
        let signature = SignatureBytes::from_ledger_bytes(res.as_slice())?;
        debug!("Received signature: {:?}", signature);
        Ok(signature)
    }

    /// Sign a message as per ERC-191.
    /// The Ledger asks the user to validate the SHA-256 hash of the message being signed.
    /// This command has been supported since firmware version 1.0.8
    ///
    /// # Arguments:
    /// message - a string to sign
    /// hd_path - HD path, prefixed with count of derivation indexes
    ///
    /// # Protocol Details:
    /// According to the Ledger Ethereum specification (reference/ledger-ethereum.adoc),
    /// message data is sent as:
    /// - First block: HD path + Message length (4 bytes) + Message chunk
    /// - Other blocks: Message chunk only
    ///   The message data requires a 4-byte length prefix (big-endian) before the message content.
    ///
    /// # See 
    /// - https://github.com/LedgerHQ/app-ethereum/blob/d408c161dc43ce4640165464bd8a4f45d662a6f1/doc/ethapp.adoc#sign-eth-transaction
    /// - https://eips.ethereum.org/EIPS/eip-191
    pub fn sign_message_erc191(
        &self,
        message: String,
        hd_path: &dyn HDPath,
    ) -> Result<SignatureBytes, HWKeyError> {

        let message = message.as_bytes();
        // Prepare message data with 4-byte length prefix as per SIGN ETH PERSONAL MESSAGE specification
        let mut message_data = Vec::<u8>::with_capacity(message.len() + 4);
        message_data.extend_from_slice((message.len() as u32).to_be_bytes().as_slice());
        message_data.extend_from_slice(message);
        let message_data = message_data.as_slice();

        let res = self.send_chunked_data(COMMAND_SIGN_MESSAGE, hd_path, message_data)?;
        let signature = SignatureBytes::from_ledger_bytes(res.as_slice())?;
        debug!("Received signature: {:?}", signature);
        Ok(signature)
    }

    /// Sign a message as per EIP-712 (v0 implementation - simple signing with hashes only).
    /// This implementation requires the domain hash and message hash to be pre-computed
    /// and provided to the device for signing.
    /// 
    /// The Ledger displays the hashes to the user for validation before signing.
    /// This command has been supported since app version 1.5.0
    ///
    /// # Arguments
    /// * `domain_hash` - The 32-byte domain hash (keccak256 of the domain separator)
    /// * `message_hash` - The 32-byte message hash (keccak256 of the message)
    /// * `hd_path` - HD path for the signing key
    ///
    /// # Protocol Details
    /// According to the Ledger Ethereum specification (reference/ledger-ethereum.adoc),
    /// for the v0 implementation, the data sent is:
    /// - HD path
    /// - Domain hash (32 bytes)
    /// - Message hash (32 bytes)
    /// 
    /// # See
    /// - https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md
    /// - reference/ledger-ethereum.adoc section "SIGN ETH EIP 712"
    pub fn sign_message_eip712(
        &self,
        domain_hash: &[u8; 32],
        message_hash: &[u8; 32],
        hd_path: &dyn HDPath,
    ) -> Result<SignatureBytes, HWKeyError> {
        // Prepare the data payload: domain_hash + message_hash
        let mut data = Vec::<u8>::with_capacity(64);
        data.extend_from_slice(domain_hash);
        data.extend_from_slice(message_hash);

        let apdu = ApduBuilder::new(COMMAND_SIGN_EIP712)
            .with_p1(0x00)
            // 00 for v0 implementation (simple signing with hashes only)
            .with_p2(0x00)
            .with_data(hd_path.to_bytes().as_slice())
            .with_data(&data)
            .build();

        let handle = self.ledger.lock().unwrap();

        let res = sendrecv(&*handle, &apdu)?;
        let signature = SignatureBytes::from_ledger_bytes(res.as_slice())?;
        debug!("Received EIP-712 signature: {:?}", signature);
        Ok(signature)
    }

    pub fn get_version(&self) -> Result<AppVersion, HWKeyError> {
        let apdu = ApduBuilder::new(COMMAND_APP_CONFIG)
            .build();
        let handle = self.ledger.lock().unwrap();
        let resp = sendrecv(&*handle, &apdu)?;
        AppVersion::try_from(resp).map_err(|_| HWKeyError::EncodingError("Invalid version config".to_string()))
    }

    fn is_path_available(&self, hd_path: &dyn HDPath) -> bool {
        self.get_address(hd_path, false).is_ok_and(|_| true)
    }
}

impl PubkeyAddressApp for EthereumApp {
    fn get_extkey_at(&self, hd_path: &dyn HDPath) -> Result<Box<dyn AsExtendedKey>, HWKeyError> {
        let address = self.get_address(hd_path, false)?;
        Ok(Box::new(address))
    }
}

#[derive(Copy, Debug, Clone, Eq, PartialEq)]
pub enum EthereumApps {
    Ethereum,
    EthereumClassic
}

impl LedgerApp for EthereumApp {
    type Networks = EthereumApps;

    fn new(manager: Arc<Mutex<dyn LedgerTransport>>) -> Self{
        EthereumApp {
            ledger: manager
        }
    }

    fn is_open(&self) -> Option<Self::Networks> {
        self.get_version().ok().and_then(|_| {
            // ETC app gives address for both m/44'/60' and m/44'/61'
            // but ETH app gives only address for m/44'/60'

            let has_60 = self.is_path_available(
                &AccountHDPath::try_new(Purpose::Pubkey, 60, 0).expect("no-eth-acc")
            );
            let has_61 = self.is_path_available(
                &AccountHDPath::try_new(Purpose::Pubkey, 61, 0).expect("no-etc-acc")
            );

            if has_60 && has_61 {
                Some(EthereumApps::EthereumClassic)
            } else if has_60 && !has_61 {
                Some(EthereumApps::Ethereum)
            } else {
                None
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::ledger::app::ethereum::{AddressResponse, EthereumApp, SignatureBytes, ECDSA_SIGNATURE_BYTES};
    use crate::ledger::app::LedgerApp;
    use crate::ledger::connect::direct::CHUNK_SIZE;
    use crate::ledger::connect::mock::MockTransport;
    use std::convert::TryFrom;
    use std::sync::{Arc, Mutex};
    use hdpath::{StandardHDPath, HDPath};

    #[test]
    fn chunk_data_small_data() {
        let transport = Arc::new(Mutex::new(MockTransport::new()));
        let app = EthereumApp::new(transport);
        let hd_path = StandardHDPath::try_from("m/44'/60'/0'/0/0").unwrap();
        
        let small_data = vec![1, 2, 3, 4, 5];
        let (init, cont) = app.chunk_data(&small_data, &hd_path);
        
        assert_eq!(init, &small_data);
        assert_eq!(cont.len(), 0);
    }

    #[test]
    fn chunk_data_large_data() {
        let transport = Arc::new(Mutex::new(MockTransport::new()));
        let app = EthereumApp::new(transport);
        let hd_path = StandardHDPath::try_from("m/44'/60'/0'/0/0").unwrap();
        
        // Create data larger than CHUNK_SIZE
        let large_data = vec![0u8; CHUNK_SIZE + 100];
        let (init, cont) = app.chunk_data(&large_data, &hd_path);
        
        let expected_init_size = CHUNK_SIZE - hd_path.to_bytes().len();
        assert_eq!(init.len(), expected_init_size);
        assert_eq!(cont.len(), large_data.len() - expected_init_size);
        
        // Verify the data is correctly split
        let mut reconstructed = Vec::new();
        reconstructed.extend_from_slice(init);
        reconstructed.extend_from_slice(cont);
        assert_eq!(reconstructed, large_data);
    }

    #[test]
    fn chunk_data_exact_chunk_size() {
        let transport = Arc::new(Mutex::new(MockTransport::new()));
        let app = EthereumApp::new(transport);
        let hd_path = StandardHDPath::try_from("m/44'/60'/0'/0/0").unwrap();
        
        // Create data exactly CHUNK_SIZE
        let exact_data = vec![0u8; CHUNK_SIZE];
        let (init, cont) = app.chunk_data(&exact_data, &hd_path);
        
        assert_eq!(init, &exact_data);
        assert_eq!(cont.len(), 0);
    }

    #[test]
    fn send_chunked_data_builds_correct_apdu() {
        let transport = Arc::new(Mutex::new(MockTransport::new()));
        let app = EthereumApp::new(transport);
        let hd_path = StandardHDPath::try_from("m/44'/60'/0'/0/0").unwrap();
        
        let small_data = vec![1, 2, 3, 4, 5];
        let (init, cont) = app.chunk_data(&small_data, &hd_path);
        
        // Verify that chunking works as expected for small data
        assert_eq!(init, &small_data);
        assert_eq!(cont.len(), 0);
        
        // Test with large data
        let large_data = vec![0u8; CHUNK_SIZE + 100];
        let (init, cont) = app.chunk_data(&large_data, &hd_path);
        
        // Verify chunking works for large data
        let expected_init_size = CHUNK_SIZE - hd_path.to_bytes().len();
        assert_eq!(init.len(), expected_init_size);
        assert_eq!(cont.len(), large_data.len() - expected_init_size);
        
        // Verify data integrity
        let mut reconstructed = Vec::new();
        reconstructed.extend_from_slice(init);
        reconstructed.extend_from_slice(cont);
        assert_eq!(reconstructed, large_data);
    }

    #[test]
    fn chunk_data_respects_hd_path_length() {
        let transport = Arc::new(Mutex::new(MockTransport::new()));
        let app = EthereumApp::new(transport);
        
        // Test with different HD path lengths using CustomHDPath and StandardHDPath
        use hdpath::CustomHDPath;
        
        // CustomHDPath with fewer derivation levels than StandardHDPath
        let short_path = CustomHDPath::try_from("m/44'/60'/0'/0").unwrap();      // 4 levels
        let long_path = StandardHDPath::try_from("m/44'/60'/0'/0/0").unwrap();   // 5 levels
        
        let test_data = vec![0u8; CHUNK_SIZE + 50];
        
        let (init_short, cont_short) = app.chunk_data(&test_data, &short_path);
        let (init_long, cont_long) = app.chunk_data(&test_data, &long_path);
        
        // Verify the expected chunk sizes
        let short_path_len = short_path.to_bytes().len();
        let long_path_len = long_path.to_bytes().len();
        
        // Short path should have fewer bytes than long path
        assert!(short_path_len < long_path_len, "Short path should have fewer bytes than long path");
        
        let expected_init_short = CHUNK_SIZE - short_path_len;
        let expected_init_long = CHUNK_SIZE - long_path_len;
        
        assert_eq!(init_short.len(), expected_init_short);
        assert_eq!(init_long.len(), expected_init_long);
        
        // With shorter HD path, more data should fit in initial chunk
        assert!(init_short.len() > init_long.len(), "Shorter path should allow more data in initial chunk");
        assert!(cont_short.len() < cont_long.len(), "Shorter path should have less continuation data");
        
        // Both should reconstruct to the same original data
        let mut reconstructed_short = Vec::new();
        reconstructed_short.extend_from_slice(init_short);
        reconstructed_short.extend_from_slice(cont_short);
        
        let mut reconstructed_long = Vec::new();
        reconstructed_long.extend_from_slice(init_long);
        reconstructed_long.extend_from_slice(cont_long);
        
        assert_eq!(reconstructed_short, test_data);
        assert_eq!(reconstructed_long, test_data);
    }

    #[test]
    fn decode_std_address() {
        let resp = hex::decode("4104b28217096d8ad3dd25461404c3941a5196ac8f089f1be5bcb62df2ce08a71ba1ca4b879ee38217cced7ef1c9dc5c15cb804ab159503514f73559d1a1192ba1fc28354164343233663565623437333534313563393366306365353266366532633133424436413530309000000000000035140000000000000000000000000000000000000000000000000000000000000000").unwrap();
        let parsed = AddressResponse::try_from(resp);
        assert!(parsed.is_ok(), "{:?}", parsed);
        let parsed = parsed.unwrap();
        assert_eq!("0x5Ad423f5eb4735415c93f0ce52f6e2c13BD6A500".to_string(), parsed.address);
        assert_eq!(
            "04b28217096d8ad3dd25461404c3941a5196ac8f089f1be5bcb62df2ce08a71ba1ca4b879ee38217cced7ef1c9dc5c15cb804ab159503514f73559d1a1192ba1fc",
            hex::encode(parsed.pubkey.serialize_uncompressed()));
    }

    #[test]
    fn decode_std_address_2() {
        let resp = hex::decode("4104452ae4b222d10cb80c269d0677f7165c548e49113d91b26848ae01a7732f15ff88379573411237d1a9dfb9603d2f40d7a56bf12b1bf5f6ae3b69d7bfebd45689283364363634383362344361643335313838363130323946663836613338376542633437303531373290000000000000f5f60000000000000000000000000000000000000000000000000000000000000000").unwrap();
        let parsed = AddressResponse::try_from(resp);
        assert!(parsed.is_ok(), "{:?}", parsed);
        let parsed = parsed.unwrap();
        assert_eq!("0x3d66483b4Cad3518861029Ff86a387eBc4705172".to_string(), parsed.address);
        assert_eq!(
            "04452ae4b222d10cb80c269d0677f7165c548e49113d91b26848ae01a7732f15ff88379573411237d1a9dfb9603d2f40d7a56bf12b1bf5f6ae3b69d7bfebd45689",
            hex::encode(parsed.pubkey.serialize_uncompressed()));
    }

    #[test]
    fn test_from_ledger_bytes_to_vrs_roundtrip() {
        let original_data: [u8; ECDSA_SIGNATURE_BYTES] = [
            0x1f, // V
            0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
            0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
            0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31,
            0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
        ];

        let signature = SignatureBytes::from_ledger_bytes(&original_data).expect("Failed to create SignatureBytes");

        assert_eq!(original_data, signature.to_ledger_bytes());
    }
}