Skip to content

Commit 04e9d9d

Browse files
committed
Fix error message when importing desc for wrong network
close #781
1 parent 0e295ac commit 04e9d9d

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

gui/src/installer/step/descriptor.rs

+27-22
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,7 @@ pub struct ImportDescriptor {
12411241
change_network: bool,
12421242
data_dir: Option<PathBuf>,
12431243
imported_descriptor: form::Value<String>,
1244+
wrong_network: bool,
12441245
error: Option<String>,
12451246
}
12461247

@@ -1252,21 +1253,35 @@ impl ImportDescriptor {
12521253
network_valid: true,
12531254
data_dir: None,
12541255
imported_descriptor: form::Value::default(),
1256+
wrong_network: false,
12551257
error: None,
12561258
}
12571259
}
12581260

1259-
fn check_descriptor(&mut self) {
1261+
fn check_descriptor(&mut self, network: Network) -> Option<LianaDescriptor> {
12601262
if !self.imported_descriptor.value.is_empty() {
12611263
if let Ok(desc) = LianaDescriptor::from_str(&self.imported_descriptor.value) {
1262-
if self.network == Network::Bitcoin {
1263-
self.imported_descriptor.valid = desc.all_xpubs_net_is(self.network);
1264+
if network == Network::Bitcoin {
1265+
self.imported_descriptor.valid = desc.all_xpubs_net_is(network);
12641266
} else {
12651267
self.imported_descriptor.valid = desc.all_xpubs_net_is(Network::Testnet);
12661268
}
1269+
if self.imported_descriptor.valid {
1270+
self.wrong_network = false;
1271+
Some(desc)
1272+
} else {
1273+
self.wrong_network = true;
1274+
None
1275+
}
12671276
} else {
12681277
self.imported_descriptor.valid = false;
1278+
self.wrong_network = false;
1279+
None
12691280
}
1281+
} else {
1282+
self.wrong_network = false;
1283+
self.imported_descriptor.valid = true;
1284+
None
12701285
}
12711286
}
12721287
}
@@ -1281,18 +1296,21 @@ impl Step for ImportDescriptor {
12811296
let mut network_datadir = self.data_dir.clone().unwrap();
12821297
network_datadir.push(self.network.to_string());
12831298
self.network_valid = !network_datadir.exists();
1284-
self.check_descriptor();
1299+
self.check_descriptor(self.network);
12851300
}
12861301
Message::DefineDescriptor(message::DefineDescriptor::ImportDescriptor(desc)) => {
12871302
self.imported_descriptor.value = desc;
1288-
self.check_descriptor();
1303+
self.check_descriptor(self.network);
12891304
}
12901305
_ => {}
12911306
};
12921307
Command::none()
12931308
}
12941309

12951310
fn load_context(&mut self, ctx: &Context) {
1311+
if ctx.bitcoin_config.network != self.network {
1312+
self.check_descriptor(ctx.bitcoin_config.network);
1313+
}
12961314
self.network = ctx.bitcoin_config.network;
12971315
self.data_dir = Some(ctx.data_dir.clone());
12981316
let mut network_datadir = ctx.data_dir.clone();
@@ -1305,23 +1323,9 @@ impl Step for ImportDescriptor {
13051323
// Set to true in order to force the registration process to be shown to user.
13061324
ctx.hw_is_used = true;
13071325
// descriptor forms for import or creation cannot be both empty or filled.
1308-
if !self.imported_descriptor.value.is_empty() {
1309-
if let Ok(desc) = LianaDescriptor::from_str(&self.imported_descriptor.value) {
1310-
if self.network == Network::Bitcoin {
1311-
self.imported_descriptor.valid = desc.all_xpubs_net_is(self.network);
1312-
} else {
1313-
self.imported_descriptor.valid = desc.all_xpubs_net_is(Network::Testnet);
1314-
}
1315-
if self.imported_descriptor.valid {
1316-
ctx.descriptor = Some(desc);
1317-
true
1318-
} else {
1319-
false
1320-
}
1321-
} else {
1322-
self.imported_descriptor.valid = false;
1323-
false
1324-
}
1326+
if let Some(desc) = self.check_descriptor(self.network) {
1327+
ctx.descriptor = Some(desc);
1328+
true
13251329
} else {
13261330
false
13271331
}
@@ -1334,6 +1338,7 @@ impl Step for ImportDescriptor {
13341338
self.network,
13351339
self.network_valid,
13361340
&self.imported_descriptor,
1341+
self.wrong_network,
13371342
self.error.as_ref(),
13381343
)
13391344
}

gui/src/installer/view.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ pub fn import_descriptor<'a>(
364364
network: bitcoin::Network,
365365
network_valid: bool,
366366
imported_descriptor: &form::Value<String>,
367+
wrong_network: bool,
367368
error: Option<&String>,
368369
) -> Element<'a, Message> {
369370
let row_network = Row::new()
@@ -392,7 +393,11 @@ pub fn import_descriptor<'a>(
392393
form::Form::new_trimmed("Descriptor", imported_descriptor, |msg| {
393394
Message::DefineDescriptor(message::DefineDescriptor::ImportDescriptor(msg))
394395
})
395-
.warning("Incompatible descriptor.")
396+
.warning(if wrong_network {
397+
"The descriptor is for an other network, you can change the network in the previous steps."
398+
} else {
399+
"Incompatible descriptor."
400+
})
396401
.size(20)
397402
.padding(10),
398403
)

0 commit comments

Comments
 (0)