Skip to content

Commit

Permalink
Add wallet identify subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
terror committed Aug 10, 2022
1 parent 6c6b354 commit 361bf28
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;

mod balance;
mod fund;
mod identify;
mod init;
mod utxos;

Expand Down Expand Up @@ -57,6 +58,7 @@ fn get_wallet(options: Options) -> Result<bdk::wallet::Wallet<SqliteDatabase>> {
pub(crate) enum Wallet {
Balance,
Fund,
Identify,
Init,
Utxos,
}
Expand All @@ -66,6 +68,7 @@ impl Wallet {
match self {
Self::Balance => balance::run(options),
Self::Fund => fund::run(options),
Self::Identify => identify::run(options),
Self::Init => init::run(options),
Self::Utxos => utxos::run(options),
}
Expand Down
19 changes: 19 additions & 0 deletions src/subcommand/wallet/identify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::*;

pub(crate) fn run(options: Options) -> Result {
let index = Index::index(&options)?;

let ranges = get_wallet(options)?
.list_unspent()?
.iter()
.map(|utxo| index.list(utxo.outpoint))
.collect::<Result<Vec<Option<Vec<(u64, u64)>>>, _>>()?;

for range in ranges.into_iter().flatten() {
for (start, end) in range {
println!("[{}, {})", start, end);
}
}

Ok(())
}
37 changes: 37 additions & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,40 @@ fn balance() {
.expected_stdout("5000000000\n")
.run()
}

#[test]
fn identify() {
let state = Test::new()
.command("--network regtest wallet init")
.expected_status(0)
.expected_stderr("Wallet initialized.\n")
.output()
.state;

let output = Test::with_state(state)
.command("--network regtest wallet fund")
.stdout_regex("^bcrt1.*\n")
.output();

output
.state
.client
.generate_to_address(
101,
&Address::from_str(
output
.stdout
.strip_suffix('\n')
.ok_or("Failed to strip suffix")
.unwrap(),
)
.unwrap(),
)
.unwrap();

Test::with_state(output.state)
.command("--network regtest wallet identify")
.expected_status(0)
.expected_stdout("[5000000000, 10000000000)\n")
.run()
}

0 comments on commit 361bf28

Please sign in to comment.