From 4dd1340236bb36f7dec82cf508fa4161a22ed9d1 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 9 Mar 2020 15:44:17 -0700 Subject: [PATCH] Limit waiting-message to single- or last-chunk apdus (#8730) (#8733) automerge --- remote-wallet/src/ledger.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/remote-wallet/src/ledger.rs b/remote-wallet/src/ledger.rs index ced0defa68..d179525f7d 100644 --- a/remote-wallet/src/ledger.rs +++ b/remote-wallet/src/ledger.rs @@ -227,7 +227,7 @@ impl LedgerWallet { data: &[u8], ) -> Result, RemoteWalletError> { self.write(command, p1, p2, data)?; - if p1 == P1_CONFIRM { + if p1 == P1_CONFIRM && is_last_part(p2) { println!("Waiting for remote wallet to approve..."); } self.read() @@ -441,3 +441,37 @@ pub fn get_ledger_from_info( }; wallet_manager.get_ledger(&wallet_base_pubkey) } + +// +fn is_last_part(p2: u8) -> bool { + p2 & P2_MORE == 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_is_last_part() { + // Bytes with bit-2 set to 0 should return true + assert!(is_last_part(0b00)); + assert!(is_last_part(0b01)); + assert!(is_last_part(0b101)); + assert!(is_last_part(0b1001)); + assert!(is_last_part(0b1101)); + + // Bytes with bit-2 set to 1 should return false + assert!(!is_last_part(0b10)); + assert!(!is_last_part(0b11)); + assert!(!is_last_part(0b110)); + assert!(!is_last_part(0b111)); + assert!(!is_last_part(0b1010)); + + // Test implementation-specific uses + let p2 = 0; + assert!(is_last_part(p2)); + let p2 = P2_EXTEND | P2_MORE; + assert!(!is_last_part(p2)); + assert!(is_last_part(p2 & !P2_MORE)); + } +}