Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow generate multiple addresses #3277

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/subcommand/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,19 @@ rpcport={bitcoind_port}
let receive =
serde_json::from_slice::<crate::subcommand::wallet::receive::Output>(&output.stdout)?;

let address = receive.address.require_network(Network::Regtest)?;

let status = Command::new("bitcoin-cli")
.arg(format!("-datadir={relative}"))
.arg("generatetoaddress")
.arg("200")
.arg(address.to_string())
.arg(
receive
.addresses
.first()
.cloned()
.unwrap()
.require_network(Network::Regtest)?
.to_string(),
)
.status()?;

ensure!(status.success(), "failed to create wallet: {status}");
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) enum Subcommand {
#[command(about = "List wallet inscriptions")]
Inscriptions,
#[command(about = "Generate receive address")]
Receive,
Receive(receive::Receive),
#[command(about = "Restore wallet")]
Restore(restore::Restore),
#[command(about = "List wallet satoshis")]
Expand Down Expand Up @@ -96,7 +96,7 @@ impl WalletCommand {
Subcommand::Etch(etch) => etch.run(wallet),
Subcommand::Inscribe(inscribe) => inscribe.run(wallet),
Subcommand::Inscriptions => inscriptions::run(wallet),
Subcommand::Receive => receive::run(wallet),
Subcommand::Receive(receive) => receive.run(wallet),
Subcommand::Sats(sats) => sats.run(wallet),
Subcommand::Send(send) => send.run(wallet),
Subcommand::Transactions(transactions) => transactions.run(wallet),
Expand Down
26 changes: 20 additions & 6 deletions src/subcommand/wallet/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ use super::*;

#[derive(Deserialize, Serialize)]
pub struct Output {
pub address: Address<NetworkUnchecked>,
pub addresses: Vec<Address<NetworkUnchecked>>,
}

pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let address = wallet
.bitcoin_client()
.get_new_address(None, Some(bitcoincore_rpc::json::AddressType::Bech32m))?;
#[derive(Debug, Parser)]
pub(crate) struct Receive {
#[arg(short, long, help = "Generate <NUMBER> addresses.")]
number: Option<u64>,
}

impl Receive {
pub(crate) fn run(self, wallet: Wallet) -> SubcommandResult {
let mut addresses: Vec<Address<NetworkUnchecked>> = Vec::new();

for _ in 0..self.number.unwrap_or(1) {
addresses.push(
wallet
.bitcoin_client()
.get_new_address(None, Some(bitcoincore_rpc::json::AddressType::Bech32m))?,
);
}

Ok(Some(Box::new(Output { address })))
Ok(Some(Box::new(Output { addresses })))
}
}
6 changes: 4 additions & 2 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,13 @@ fn inscribe_to_specific_destination() {

bitcoin_rpc_server.mine_blocks(1);

let destination = CommandBuilder::new("wallet receive")
let addresses = CommandBuilder::new("wallet receive")
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<receive::Output>()
.address;
.addresses;

let destination = addresses.first().unwrap();

let txid = CommandBuilder::new(format!(
"wallet inscribe --destination {} --file degenerate.png --fee-rate 1",
Expand Down
16 changes: 10 additions & 6 deletions tests/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ fn inscriptions() {
format!("https://ordinals.com/inscription/{inscription}")
);

let address = CommandBuilder::new("wallet receive")
let addresses = CommandBuilder::new("wallet receive")
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<receive::Output>()
.address;
.addresses;

let destination = addresses.first().unwrap();

let txid = CommandBuilder::new(format!(
"wallet send --fee-rate 1 {} {inscription}",
address.assume_checked()
destination.clone().assume_checked()
))
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
Expand Down Expand Up @@ -105,15 +107,17 @@ fn inscriptions_with_postage() {

assert_eq!(output[0].postage, 10000);

let address = CommandBuilder::new("wallet receive")
let addresses = CommandBuilder::new("wallet receive")
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<receive::Output>()
.address;
.addresses;

let destination = addresses.first().unwrap();

CommandBuilder::new(format!(
"wallet send --fee-rate 1 {} {inscription}",
address.assume_checked()
destination.clone().assume_checked()
))
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
Expand Down
6 changes: 5 additions & 1 deletion tests/wallet/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ fn receive() {
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<receive::Output>();

assert!(output.address.is_valid_for_network(Network::Bitcoin));
assert!(output
.addresses
.first()
.unwrap()
.is_valid_for_network(Network::Bitcoin));
}
Loading