Skip to content

Commit 2364172

Browse files
committed
fixup! eth: add confirmation screen for known L2s and sidechains
1 parent 5a6b43e commit 2364172

File tree

2 files changed

+82
-65
lines changed

2 files changed

+82
-65
lines changed

src/rust/bitbox02-rust/src/hww/api/ethereum/params.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,8 @@ const PARAMS: &[Params] = &[
9393
},
9494
];
9595

96-
/// Check if the chain_id corresponds to a known non-mainnet network.
97-
/// Returns true for specific L2s and sidechains we want to show confirmations for,
98-
/// false for mainnet (chain_id=1) and unknown networks.
99-
pub fn is_known_non_mainnet(chain_id: u64) -> bool {
100-
if chain_id == 1 {
101-
return false;
102-
}
96+
/// Check if the chain_id corresponds to a known network (to show an additional confirmations for).
97+
pub fn is_known_network(chain_id: u64) -> bool {
10398
PARAMS.iter().any(|p| p.chain_id == chain_id)
10499
}
105100

src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rs

+80-58
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,10 @@ pub async fn process(request: &Transaction<'_>) -> Result<Response, Error> {
299299
}
300300
super::keypath::warn_unusual_keypath(&params, params.name, request.keypath()).await?;
301301

302-
// Show chain confirmation only for known non-mainnet networks
303-
if super::params::is_known_non_mainnet(params.chain_id) {
302+
// Show chain confirmation only for known networks
303+
if super::params::is_known_network(params.chain_id) {
304304
confirm::confirm(&confirm::Params {
305-
title: "Confirm Chain",
306-
body: &format!("Sign transaction on\n{}", params.name),
305+
body: &format!("Sign transaction on\n\n{}", params.name),
307306
accept_is_nextarrow: true,
308307
..Default::default()
309308
})
@@ -483,6 +482,11 @@ mod tests {
483482
const KEYPATH: &[u32] = &[44 + HARDENED, 60 + HARDENED, 0 + HARDENED, 0, 0];
484483

485484
mock(Data {
485+
ui_confirm_create: Some(Box::new(|params| {
486+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
487+
assert!(params.accept_is_nextarrow);
488+
true
489+
})),
486490
ui_transaction_address_create: Some(Box::new(|amount, address| {
487491
assert_eq!(amount, "0.530564 ETH");
488492
assert_eq!(address, "0x04F264Cf34440313B4A0192A352814FBe927b885");
@@ -557,6 +561,11 @@ mod tests {
557561
UI_COUNTER
558562
} {
559563
1 => {
564+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
565+
assert!(params.accept_is_nextarrow);
566+
true
567+
}
568+
2 => {
560569
assert_eq!(params.title, "High fee");
561570
assert_eq!(params.body, "The fee is 12.0%\nthe send amount.\nProceed?");
562571
assert!(params.longtouch);
@@ -586,7 +595,7 @@ mod tests {
586595
address_case: pb::EthAddressCase::Mixed as _,
587596
})))
588597
.is_ok());
589-
assert_eq!(unsafe { UI_COUNTER }, 1);
598+
assert_eq!(unsafe { UI_COUNTER }, 2);
590599
}
591600

592601
/// Test an EIP-1559 transaction with an unusually high fee.
@@ -609,6 +618,11 @@ mod tests {
609618
UI_COUNTER
610619
} {
611620
1 => {
621+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
622+
assert!(params.accept_is_nextarrow);
623+
true
624+
}
625+
2 => {
612626
assert_eq!(params.title, "High fee");
613627
assert_eq!(params.body, "The fee is 12.0%\nthe send amount.\nProceed?");
614628
assert!(params.longtouch);
@@ -638,7 +652,7 @@ mod tests {
638652
address_case: pb::EthAddressCase::Mixed as _,
639653
})))
640654
.is_ok());
641-
assert_eq!(unsafe { UI_COUNTER }, 1);
655+
assert_eq!(unsafe { UI_COUNTER }, 2);
642656
}
643657

644658
/// Standard ETH transaction on an unusual keypath (Sepolia on mainnet keypath)
@@ -659,8 +673,7 @@ mod tests {
659673
true
660674
}
661675
2 => {
662-
assert_eq!(params.title, "Confirm Chain");
663-
assert_eq!(params.body, "Sign transaction on\nSepolia");
676+
assert_eq!(params.body, "Sign transaction on\n\nSepolia");
664677
true
665678
}
666679
_ => panic!("too many user confirmations"),
@@ -708,8 +721,12 @@ mod tests {
708721
mock(Data {
709722
ui_confirm_create: Some(Box::new(|params| {
710723
match unsafe { CONFIRM_COUNTER } {
711-
0 | 1 => assert_eq!(params.title, "Unknown\ncontract"),
712-
2 => {
724+
0 => {
725+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
726+
assert!(params.accept_is_nextarrow);
727+
}
728+
1 | 2 => assert_eq!(params.title, "Unknown\ncontract"),
729+
3 => {
713730
assert_eq!(params.title, "Transaction\ndata");
714731
assert_eq!(params.body, "666f6f20626172"); // "foo bar" in hex.
715732
assert!(params.scrollable);
@@ -764,8 +781,12 @@ mod tests {
764781
mock(Data {
765782
ui_confirm_create: Some(Box::new(|params| {
766783
match unsafe { CONFIRM_COUNTER } {
767-
0 | 1 => assert_eq!(params.title, "Unknown\ncontract"),
768-
2 => {
784+
0 => {
785+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
786+
assert!(params.accept_is_nextarrow);
787+
}
788+
1 | 2 => assert_eq!(params.title, "Unknown\ncontract"),
789+
3 => {
769790
assert_eq!(params.title, "Transaction\ndata");
770791
assert_eq!(params.body, "666f6f20626172"); // "foo bar" in hex.
771792
assert!(params.scrollable);
@@ -819,6 +840,11 @@ mod tests {
819840
const KEYPATH: &[u32] = &[44 + HARDENED, 60 + HARDENED, 0 + HARDENED, 0, 0];
820841

821842
mock(Data {
843+
ui_confirm_create: Some(Box::new(|params| {
844+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
845+
assert!(params.accept_is_nextarrow);
846+
true
847+
})),
822848
ui_transaction_address_create: Some(Box::new(|amount, address| {
823849
assert_eq!(amount, "57 USDT");
824850
assert_eq!(address, "0xE6CE0a092A99700CD4ccCcBb1fEDc39Cf53E6330");
@@ -879,6 +905,11 @@ mod tests {
879905
const KEYPATH: &[u32] = &[44 + HARDENED, 60 + HARDENED, 0 + HARDENED, 0, 0];
880906

881907
mock(Data {
908+
ui_confirm_create: Some(Box::new(|params| {
909+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
910+
assert!(params.accept_is_nextarrow);
911+
true
912+
})),
882913
ui_transaction_address_create: Some(Box::new(|amount, address| {
883914
assert_eq!(amount, "Unknown token");
884915
assert_eq!(address, "0x857B3D969eAcB775a9f79cabc62Ec4bB1D1cd60e");
@@ -954,6 +985,7 @@ mod tests {
954985
{
955986
// Check that the above is valid before making invalid variants.
956987
mock(Data {
988+
ui_confirm_create: Some(Box::new(|_| true)),
957989
ui_transaction_address_create: Some(Box::new(|_, _| true)),
958990
ui_transaction_fee_create: Some(Box::new(|_, _, _| true)),
959991
..Default::default()
@@ -1022,9 +1054,30 @@ mod tests {
10221054
);
10231055
}
10241056

1057+
{
1058+
// User rejects chain confirmation
1059+
mock(Data {
1060+
ui_confirm_create: Some(Box::new(|params| {
1061+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
1062+
assert!(params.accept_is_nextarrow);
1063+
false
1064+
})),
1065+
..Default::default()
1066+
});
1067+
assert_eq!(
1068+
block_on(process(&Transaction::Legacy(&valid_request))),
1069+
Err(Error::UserAbort)
1070+
);
1071+
}
1072+
10251073
{
10261074
// User rejects recipient/value.
10271075
mock(Data {
1076+
ui_confirm_create: Some(Box::new(|params| {
1077+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
1078+
assert!(params.accept_is_nextarrow);
1079+
true
1080+
})),
10281081
ui_transaction_address_create: Some(Box::new(|amount, address| {
10291082
assert_eq!(amount, "0.530564 ETH");
10301083
assert_eq!(address, "0x04F264Cf34440313B4A0192A352814FBe927b885");
@@ -1040,6 +1093,11 @@ mod tests {
10401093
{
10411094
// User rejects total/fee.
10421095
mock(Data {
1096+
ui_confirm_create: Some(Box::new(|params| {
1097+
assert_eq!(params.body, "Sign transaction on\n\nEthereum");
1098+
assert!(params.accept_is_nextarrow);
1099+
true
1100+
})),
10431101
ui_transaction_address_create: Some(Box::new(|amount, address| {
10441102
assert_eq!(amount, "0.530564 ETH");
10451103
assert_eq!(address, "0x04F264Cf34440313B4A0192A352814FBe927b885");
@@ -1061,6 +1119,7 @@ mod tests {
10611119
{
10621120
// Keystore locked.
10631121
mock(Data {
1122+
ui_confirm_create: Some(Box::new(|_| true)),
10641123
ui_transaction_address_create: Some(Box::new(|_, _| true)),
10651124
ui_transaction_fee_create: Some(Box::new(|_, _, _| true)),
10661125
..Default::default()
@@ -1093,6 +1152,7 @@ mod tests {
10931152
{
10941153
// Check that the above is valid before making invalid variants.
10951154
mock(Data {
1155+
ui_confirm_create: Some(Box::new(|_| true)),
10961156
ui_transaction_address_create: Some(Box::new(|_, _| true)),
10971157
ui_transaction_fee_create: Some(Box::new(|_, _, _| true)),
10981158
..Default::default()
@@ -1194,16 +1254,16 @@ mod tests {
11941254

11951255
/// Test that the chain confirmation screen appears for known non-mainnet networks.
11961256
#[test]
1197-
pub fn test_chain_confirmation_for_l2_networks() {
1257+
pub fn test_chain_confirmation() {
11981258
const KEYPATH: &[u32] = &[44 + HARDENED, 60 + HARDENED, 0 + HARDENED, 0, 0];
11991259
static mut CONFIRM_COUNTER: u32 = 0;
12001260
// Test with Arbitrum (chain_id 42161)
12011261
mock(Data {
12021262
ui_confirm_create: Some(Box::new(|params| {
12031263
unsafe {
12041264
if CONFIRM_COUNTER == 0 {
1205-
assert_eq!(params.title, "Confirm Chain");
1206-
assert_eq!(params.body, "Sign transaction on\nArbitrum One");
1265+
// assert_eq!(params.title, "Confirm Chain");
1266+
assert_eq!(params.body, "Sign transaction on\n\nArbitrum One");
12071267
CONFIRM_COUNTER += 1;
12081268
}
12091269
}
@@ -1235,7 +1295,7 @@ mod tests {
12351295
assert_eq!(unsafe { CONFIRM_COUNTER }, 1);
12361296
}
12371297

1238-
/// Test that EIP-1559 transactions also get the chain confirmation screen for L2 networks
1298+
/// Test that EIP-1559 transactions also get the chain confirmation screen
12391299
#[test]
12401300
pub fn test_chain_confirmation_for_eip1559() {
12411301
const KEYPATH: &[u32] = &[44 + HARDENED, 60 + HARDENED, 0 + HARDENED, 0, 0];
@@ -1246,8 +1306,8 @@ mod tests {
12461306
ui_confirm_create: Some(Box::new(|params| {
12471307
unsafe {
12481308
if CONFIRM_COUNTER == 0 {
1249-
assert_eq!(params.title, "Confirm Chain");
1250-
assert_eq!(params.body, "Sign transaction on\nPolygon");
1309+
// assert_eq!(params.title, "Confirm Chain");
1310+
assert_eq!(params.body, "Sign transaction on\n\nPolygon");
12511311
CONFIRM_COUNTER += 1;
12521312
}
12531313
}
@@ -1278,7 +1338,7 @@ mod tests {
12781338
assert_eq!(unsafe { CONFIRM_COUNTER }, 1);
12791339
}
12801340

1281-
/// Test that unknown networks and mainnet do NOT get the chain confirmation screen
1341+
/// Test that unknown networks do NOT get the chain confirmation screen
12821342
#[test]
12831343
pub fn test_no_chain_confirmation_for_unknown_networks() {
12841344
const KEYPATH: &[u32] = &[44 + HARDENED, 60 + HARDENED, 0 + HARDENED, 0, 0];
@@ -1289,7 +1349,7 @@ mod tests {
12891349
ui_confirm_create: Some(Box::new(|params| {
12901350
unsafe {
12911351
// Only the warning confirmations should appear, not chain confirmation
1292-
if params.title == "Confirm Chain" {
1352+
if params.body.starts_with("Sign transaction on") {
12931353
CONFIRM_COUNTER += 1;
12941354
}
12951355
}
@@ -1319,43 +1379,5 @@ mod tests {
13191379
.unwrap();
13201380

13211381
assert_eq!(unsafe { CONFIRM_COUNTER }, 0);
1322-
1323-
// Reset counter for mainnet test
1324-
unsafe { CONFIRM_COUNTER = 0 };
1325-
1326-
// Test with Ethereum mainnet (chain_id 1)
1327-
mock(Data {
1328-
ui_confirm_create: Some(Box::new(|params| {
1329-
unsafe {
1330-
if params.title == "Confirm Chain" {
1331-
CONFIRM_COUNTER += 1;
1332-
}
1333-
}
1334-
true
1335-
})),
1336-
ui_transaction_address_create: Some(Box::new(|_, _| true)),
1337-
ui_transaction_fee_create: Some(Box::new(|_, _, _| true)),
1338-
..Default::default()
1339-
});
1340-
mock_unlocked();
1341-
1342-
block_on(process(&Transaction::Legacy(&pb::EthSignRequest {
1343-
coin: pb::EthCoin::Eth as _,
1344-
keypath: KEYPATH.to_vec(),
1345-
nonce: b"\x1f\xdc".to_vec(),
1346-
gas_price: b"\x01\x65\xa0\xbc\x00".to_vec(),
1347-
gas_limit: b"\x52\x08".to_vec(),
1348-
recipient:
1349-
b"\x04\xf2\x64\xcf\x34\x44\x03\x13\xb4\xa0\x19\x2a\x35\x28\x14\xfb\xe9\x27\xb8\x85"
1350-
.to_vec(),
1351-
value: b"\x07\x5c\xf1\x25\x9e\x9c\x40\x00".to_vec(),
1352-
data: b"".to_vec(),
1353-
host_nonce_commitment: None,
1354-
chain_id: 1,
1355-
address_case: pb::EthAddressCase::Mixed as _,
1356-
})))
1357-
.unwrap();
1358-
1359-
assert_eq!(unsafe { CONFIRM_COUNTER }, 0);
13601382
}
13611383
}

0 commit comments

Comments
 (0)