Skip to content

Commit

Permalink
add wallet path to all rpc paths
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Jan 16, 2023
1 parent f3e1771 commit 45a55fc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<T> BitcoinCoreRpcResultExt<T> for Result<T, bitcoincore_rpc::Error> {

impl Index {
pub(crate) fn open(options: &Options) -> Result<Self> {
let rpc_url = options.rpc_url(false);
let rpc_url = options.rpc_url();
let cookie_file = options.cookie_file()?;

log::info!(
Expand Down
33 changes: 15 additions & 18 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,14 @@ impl Options {
}
}

pub(crate) fn rpc_url(&self, with_wallet: bool) -> String {
let mut rpc_url = self
.rpc_url
.clone()
.unwrap_or_else(|| format!("127.0.0.1:{}", self.chain().default_rpc_port()));

if with_wallet {
rpc_url.push_str(&format!("/wallet/{}", self.wallet));
}

rpc_url.to_string()
pub(crate) fn rpc_url(&self) -> String {
self.rpc_url.clone().unwrap_or_else(|| {
format!(
"127.0.0.1:{}/wallet/{}",
self.chain().default_rpc_port(),
self.wallet
)
})
}

pub(crate) fn cookie_file(&self) -> Result<PathBuf> {
Expand Down Expand Up @@ -123,10 +120,10 @@ impl Options {
)
}

pub(crate) fn bitcoin_rpc_client(&self, with_wallet: bool) -> Result<Client> {
pub(crate) fn bitcoin_rpc_client(&self) -> Result<Client> {
let cookie_file = self.cookie_file()?;

let rpc_url = self.rpc_url(with_wallet);
let rpc_url = self.rpc_url();

log::info!(
"Connecting to Bitcoin Core RPC server at {rpc_url} using credentials from `{}`",
Expand Down Expand Up @@ -154,7 +151,7 @@ impl Options {
}

pub(crate) fn bitcoin_rpc_client_for_wallet_command(&self, create: bool) -> Result<Client> {
let client = self.bitcoin_rpc_client(true)?;
let client = self.bitcoin_rpc_client()?;

const MIN_VERSION: usize = 240000;

Expand Down Expand Up @@ -201,7 +198,7 @@ mod tests {
Arguments::try_parse_from(["ord", "--rpc-url=127.0.0.1:1234", "--chain=signet", "index"])
.unwrap()
.options
.rpc_url(false),
.rpc_url(),
"127.0.0.1:1234"
);
}
Expand All @@ -222,7 +219,7 @@ mod tests {
fn use_default_network() {
let arguments = Arguments::try_parse_from(["ord", "index"]).unwrap();

assert_eq!(arguments.options.rpc_url(false), "127.0.0.1:8332");
assert_eq!(arguments.options.rpc_url(), "127.0.0.1:8332/wallet/ord");

assert!(arguments
.options
Expand All @@ -235,7 +232,7 @@ mod tests {
fn uses_network_defaults() {
let arguments = Arguments::try_parse_from(["ord", "--chain=signet", "index"]).unwrap();

assert_eq!(arguments.options.rpc_url(false), "127.0.0.1:38332");
assert_eq!(arguments.options.rpc_url(), "127.0.0.1:38332/wallet/ord");

assert!(arguments
.options
Expand Down Expand Up @@ -434,7 +431,7 @@ mod tests {
.unwrap();

assert_eq!(
options.bitcoin_rpc_client(false).unwrap_err().to_string(),
options.bitcoin_rpc_client().unwrap_err().to_string(),
"Bitcoin RPC server is on testnet but ord is on mainnet"
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Preview {
};

for attempt in 0.. {
if options.bitcoin_rpc_client(false).is_ok() {
if options.bitcoin_rpc_client().is_ok() {
break;
}

Expand All @@ -60,7 +60,7 @@ impl Preview {
thread::sleep(Duration::from_millis(50));
}

let rpc_client = options.bitcoin_rpc_client(false)?;
let rpc_client = options.bitcoin_rpc_client()?;

super::wallet::create::run(options.clone())?;

Expand Down

0 comments on commit 45a55fc

Please sign in to comment.