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

add identify command to wallet #586

Merged
merged 8 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod range;
mod server;
mod supply;
mod traits;
mod wallet;

#[derive(Debug, Parser)]
pub(crate) enum Subcommand {
Expand All @@ -23,6 +24,8 @@ pub(crate) enum Subcommand {
Server(server::Server),
Supply,
Traits(traits::Traits),
#[clap(subcommand)]
Wallet(wallet::Wallet),
}

impl Subcommand {
Expand All @@ -43,6 +46,7 @@ impl Subcommand {
}
Self::Supply => supply::run(),
Self::Traits(traits) => traits.run(),
Self::Wallet(wallet) => wallet.run(options),
}
}
}
22 changes: 22 additions & 0 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use {
super::*,
bitcoincore_rpc::{Auth, Client, RpcApi},
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
};

mod identify;

#[derive(Debug, Parser)]
pub(crate) enum Wallet {
Identify,
}

impl Wallet {
pub(crate) fn run(self, options: Options) -> Result<()> {
match self {
Self::Identify => identify::run(options),
}
}
}

#[cfg(test)]
mod tests {}
30 changes: 30 additions & 0 deletions src/subcommand/wallet/identify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use super::*;

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

let client = Client::new(&options.rpc_url(), Auth::CookieFile(options.cookie_file()?))
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
.context("Failed to connect to RPC URL")?;

let utxos = client.list_unspent(None, None, None, None, None).unwrap();
for utxo in utxos {
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
let output = OutPoint::new(utxo.txid, utxo.vout);
match index.list(output).unwrap() {
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
Some(List::Unspent(ordinal_ranges)) => {
for (offset, ordinal_range) in ordinal_ranges.iter().enumerate() {
let ordinal = Ordinal(ordinal_range.0);
let rarity = ordinal.rarity();
match rarity {
Rarity::Common => (),
_ => println!("{ordinal}\t{output}\t{offset}\t{rarity}"),
}
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
}
}
Some(_) => (),
None => (),
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
}
}

Ok(())
}
1 change: 1 addition & 0 deletions test-bitcoincore-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/casey/ord"

[dependencies]
bitcoin = { version = "0.29.1", features = ["serde"] }
bitcoincore-rpc = "0.16.0"
hex = "0.4.3"
jsonrpc-core = "18.0.0"
jsonrpc-core-client = "18.0.0"
Expand Down
62 changes: 60 additions & 2 deletions test-bitcoincore-rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use {
bitcoin::{
blockdata::constants::COIN_VALUE, blockdata::script, consensus::encode::serialize,
hash_types::BlockHash, hashes::Hash, Block, BlockHeader, Network, OutPoint, PackedLockTime,
Script, Sequence, Transaction, TxIn, TxMerkleNode, TxOut, Txid, Witness,
hash_types::BlockHash, hashes::Hash, util::amount::Amount, Block, BlockHeader, Network,
OutPoint, PackedLockTime, Script, Sequence, Transaction, TxIn, TxMerkleNode, TxOut, Txid,
Witness,
},
bitcoincore_rpc::json::ListUnspentResultEntry,
jsonrpc_core::IoHandler,
jsonrpc_http_server::{CloseHandle, ServerBuilder},
std::collections::BTreeMap,
Expand Down Expand Up @@ -204,6 +206,16 @@ pub trait Api {
verbose: bool,
blockhash: Option<BlockHash>,
) -> Result<String, jsonrpc_core::Error>;

#[rpc(name = "listunspent")]
fn list_unspent(
&self,
minconf: Option<usize>,
maxconf: Option<usize>,
address: Option<bitcoin::Address>,
include_unsage: Option<bool>,
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
query_options: Option<String>,
) -> Result<Vec<ListUnspentResultEntry>, jsonrpc_core::Error>;
}

impl Api for Server {
Expand Down Expand Up @@ -255,6 +267,52 @@ impl Api for Server {
)),
}
}

fn list_unspent(
&self,
minconf: Option<usize>,
maxconf: Option<usize>,
address: Option<bitcoin::Address>,
include_unsafe: Option<bool>,
query_options: Option<String>,
) -> Result<Vec<ListUnspentResultEntry>, jsonrpc_core::Error> {
assert_eq!(minconf, None, "");
assert_eq!(maxconf, None, "");
assert_eq!(address, None, "");
assert_eq!(include_unsafe, None, "");
assert_eq!(query_options, None, "");
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
let all_outpoints = self
.state
.lock()
.unwrap()
.transactions
.iter()
.flat_map(|(txid, tx)| {
(0..tx.output.len()).map(|vout| bitcoin::OutPoint::new(*txid, vout as u32))
})
.collect::<Vec<OutPoint>>();

Ok(
all_outpoints
.iter()
.map(|outpoint| ListUnspentResultEntry {
txid: outpoint.txid,
vout: outpoint.vout,
address: None,
label: None,
redeem_script: None,
witness_script: None,
script_pub_key: Script::new(),
amount: Amount::default(),
confirmations: 0,
spendable: true,
solvable: true,
descriptor: None,
safe: true,
})
.collect(),
)
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub struct Handle {
Expand Down
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ mod server;
mod supply;
mod traits;
mod version;
mod wallet;
19 changes: 19 additions & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use {
super::*,
bitcoin::{blockdata::constants::COIN_VALUE, OutPoint},
};

#[test]
fn show_second_uncommon_ordinal() {
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
let rpc_server = test_bitcoincore_rpc::spawn();
let second_coinbase = rpc_server.mine_blocks(1)[0].txdata[0].txid();

CommandBuilder::new("wallet identify")
.rpc_server(&rpc_server)
.expected_stdout(format!(
"{}\t{}\t0\tuncommon\n",
50 * COIN_VALUE,
OutPoint::new(second_coinbase, 0)
))
.run();
}