diff --git a/src/chain.rs b/src/chain.rs index 51925ec841..8145578ea6 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -66,6 +66,13 @@ impl Chain { Self::Regtest => data_dir.join("regtest"), } } + + pub(crate) fn check_address_is_valid_for_network(self, address: &Address) -> Result { + if !address.is_valid_for_network(self.network()) { + bail!("Address `{address}` is not valid for {self}"); + } + Ok(()) + } } impl Display for Chain { diff --git a/src/subcommand/wallet/inscribe.rs b/src/subcommand/wallet/inscribe.rs index 1df5c15061..9f9c9bf1fb 100644 --- a/src/subcommand/wallet/inscribe.rs +++ b/src/subcommand/wallet/inscribe.rs @@ -67,10 +67,15 @@ impl Inscribe { let commit_tx_change = [get_change_address(&client)?, get_change_address(&client)?]; - let reveal_tx_destination = self - .destination - .map(Ok) - .unwrap_or_else(|| get_change_address(&client))?; + let reveal_tx_destination = match self.destination { + Some(address) => { + options + .chain() + .check_address_is_valid_for_network(&address)?; + address + } + None => get_change_address(&client)?, + }; let (unsigned_commit_tx, reveal_tx, recovery_key_pair) = Inscribe::create_inscription_transactions( diff --git a/src/subcommand/wallet/send.rs b/src/subcommand/wallet/send.rs index 85a22f3c09..b0622150b4 100644 --- a/src/subcommand/wallet/send.rs +++ b/src/subcommand/wallet/send.rs @@ -15,13 +15,9 @@ pub struct Output { impl Send { pub(crate) fn run(self, options: Options) -> Result { - if !self.address.is_valid_for_network(options.chain().network()) { - bail!( - "Address `{}` is not valid for {}", - self.address, - options.chain() - ); - } + options + .chain() + .check_address_is_valid_for_network(&self.address)?; let index = Index::open(&options)?; index.update()?; diff --git a/tests/wallet/inscribe.rs b/tests/wallet/inscribe.rs index 50ecca7dda..22b300791c 100644 --- a/tests/wallet/inscribe.rs +++ b/tests/wallet/inscribe.rs @@ -389,6 +389,22 @@ fn inscribe_to_specific_destination() { ); } +#[test] +fn inscribe_to_address_on_different_network() { + let rpc_server = test_bitcoincore_rpc::spawn(); + create_wallet(&rpc_server); + rpc_server.mine_blocks(1); + + CommandBuilder::new( + "wallet inscribe --destination tb1qsgx55dp6gn53tsmyjjv4c2ye403hgxynxs0dnm degenerate.png --fee-rate 1" + ) + .write("degenerate.png", [1; 520]) + .rpc_server(&rpc_server) + .expected_exit_code(1) + .stderr_regex("error: Address `tb1qsgx55dp6gn53tsmyjjv4c2ye403hgxynxs0dnm` is not valid for mainnet\n") + .run(); +} + #[test] fn inscribe_with_no_limit() { let rpc_server = test_bitcoincore_rpc::spawn();