Skip to content

Commit

Permalink
Ensure addresses are valid for network (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Oct 24, 2022
1 parent 65316f7 commit bb8b71f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) enum Chain {
}

impl Chain {
fn network(self) -> bitcoin::Network {
pub(crate) fn network(self) -> bitcoin::Network {
match self {
Self::Mainnet => bitcoin::Network::Bitcoin,
Self::Testnet => bitcoin::Network::Testnet,
Expand Down
8 changes: 8 additions & 0 deletions src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ impl Send {
pub(crate) fn run(self, options: Options) -> Result {
let client = options.bitcoin_rpc_client_for_wallet_command("ord wallet send")?;

if !self.address.is_valid_for_network(options.chain.network()) {
bail!(
"Address `{}` is not valid for {}",
self.address,
options.chain
);
}

let index = Index::open(&options)?;
index.index()?;

Expand Down
24 changes: 20 additions & 4 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,25 @@ fn send_on_mainnnet_refuses_to_work_with_wallet_name_foo() {
let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Bitcoin, "foo");
rpc_server.mine_blocks(1);

CommandBuilder::new(
"wallet send 5000000000 bc1qzjeg3h996kw24zrg69nge97fw8jc4v7v7yznftzk06j3429t52vse9tkp9",
)
.rpc_server(&rpc_server)
.expected_stderr("error: `ord wallet send` may only be used on mainnet with a wallet named `ord` or whose name starts with `ord-`\n")
.expected_exit_code(1)
.run();
}

#[test]
fn send_addresses_must_be_valid_for_network() {
let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Bitcoin, "ord");
rpc_server.mine_blocks_with_subsidy(1, 1_000_000);

CommandBuilder::new("wallet send 5000000000 tb1qx4gf3ya0cxfcwydpq8vr2lhrysneuj5d7lqatw")
.rpc_server(&rpc_server)
.expected_stderr("error: `ord wallet send` may only be used on mainnet with a wallet named `ord` or whose name starts with `ord-`\n")
.expected_stderr(
"error: Address `tb1qx4gf3ya0cxfcwydpq8vr2lhrysneuj5d7lqatw` is not valid for mainnet\n",
)
.expected_exit_code(1)
.run();
}
Expand All @@ -66,7 +82,7 @@ fn send_on_mainnnet_works_with_wallet_named_ord() {
rpc_server.mine_blocks_with_subsidy(1, 1_000_000);

let output =
CommandBuilder::new("wallet send 5000000000 tb1qx4gf3ya0cxfcwydpq8vr2lhrysneuj5d7lqatw")
CommandBuilder::new("wallet send 5000000000 bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
.rpc_server(&rpc_server)
.stdout_regex(r".*")
.run();
Expand All @@ -81,7 +97,7 @@ fn send_on_mainnnet_works_with_wallet_whose_name_starts_with_ord() {
rpc_server.mine_blocks_with_subsidy(1, 1_000_000);

let output =
CommandBuilder::new("wallet send 5000000000 tb1qx4gf3ya0cxfcwydpq8vr2lhrysneuj5d7lqatw")
CommandBuilder::new("wallet send 5000000000 bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
.rpc_server(&rpc_server)
.stdout_regex(r".*")
.run();
Expand All @@ -95,7 +111,7 @@ fn send_on_mainnnet_refuses_to_work_with_wallet_with_high_balance() {
let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Bitcoin, "ord");
rpc_server.mine_blocks_with_subsidy(1, 1_000_001);

CommandBuilder::new("wallet send 5000000000 tb1qx4gf3ya0cxfcwydpq8vr2lhrysneuj5d7lqatw")
CommandBuilder::new("wallet send 5000000000 bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")
.rpc_server(&rpc_server)
.expected_stderr(
"error: `ord wallet send` may not be used on mainnet with wallets containing more than 1,000,000 sats\n",
Expand Down

0 comments on commit bb8b71f

Please sign in to comment.