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

Allow arbitrary wallet name #1207

Merged
merged 5 commits into from
Jan 12, 2023
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
2 changes: 1 addition & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,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();
let rpc_url = options.rpc_url(None);
let cookie_file = options.cookie_file()?;

log::info!(
Expand Down
52 changes: 33 additions & 19 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub(crate) struct Options {
pub(crate) signet: bool,
#[clap(long, short, help = "Use testnet. Equivalent to `--chain testnet`.")]
pub(crate) testnet: bool,
#[clap(long, default_value = "ord", help = "Use <WALLET> wallet.")]
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) wallet: String,
}

impl Options {
Expand Down Expand Up @@ -66,12 +68,24 @@ impl Options {
}
}

pub(crate) fn rpc_url(&self) -> String {
self
.rpc_url
.as_ref()
.unwrap_or(&format!("127.0.0.1:{}", self.chain().default_rpc_port(),))
.into()
pub(crate) fn rpc_url(&self, path: Option<String>) -> String {
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
if let Some(path) = path {
self
.rpc_url
.as_ref()
.unwrap_or(&format!(
"127.0.0.1:{}{}",
self.chain().default_rpc_port(),
path
))
.into()
} else {
self
.rpc_url
.as_ref()
.unwrap_or(&format!("127.0.0.1:{}", self.chain().default_rpc_port()))
.into()
}
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
}

pub(crate) fn cookie_file(&self) -> Result<PathBuf> {
Expand Down Expand Up @@ -116,9 +130,15 @@ impl Options {
)
}

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

let rpc_url = self.rpc_url(if with_wallet {
Some(format!("/wallet/{}", &self.wallet))
} else {
None
});
raphjaph marked this conversation as resolved.
Show resolved Hide resolved

log::info!(
"Connecting to Bitcoin Core RPC server at {rpc_url} using credentials from `{}`",
cookie_file.display()
Expand All @@ -145,7 +165,7 @@ impl Options {
}

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

const MIN_VERSION: usize = 240000;

Expand All @@ -159,12 +179,6 @@ impl Options {
}

if !create {
let wallet_info = client.get_wallet_info()?;

if !(wallet_info.wallet_name == "ord" || wallet_info.wallet_name.starts_with("ord-")) {
bail!("wallet commands may only be used on mainnet with a wallet named `ord` or whose name starts with `ord-`");
}

let descriptors = client.list_descriptors(None)?.descriptors;

let tr = descriptors
Expand Down Expand Up @@ -196,7 +210,7 @@ mod tests {
Arguments::try_parse_from(["ord", "--rpc-url=127.0.0.1:1234", "--chain=signet", "index"])
.unwrap()
.options
.rpc_url(),
.rpc_url(None),
"127.0.0.1:1234"
);
}
Expand All @@ -217,7 +231,7 @@ mod tests {
fn use_default_network() {
let arguments = Arguments::try_parse_from(["ord", "index"]).unwrap();

assert_eq!(arguments.options.rpc_url(), "127.0.0.1:8332");
assert_eq!(arguments.options.rpc_url(None), "127.0.0.1:8332");

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

assert_eq!(arguments.options.rpc_url(), "127.0.0.1:38332");
assert_eq!(arguments.options.rpc_url(None), "127.0.0.1:38332");

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

assert_eq!(
options.bitcoin_rpc_client().unwrap_err().to_string(),
options.bitcoin_rpc_client(false).unwrap_err().to_string(),
"Bitcoin RPC server is on testnet but ord is on mainnet"
);
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
}
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().is_ok() {
if options.bitcoin_rpc_client(false).is_ok() {
break;
}

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

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

super::wallet::create::Create::run(
&super::wallet::create::Create { name: "ord".into() },
Expand Down
4 changes: 0 additions & 4 deletions src/subcommand/wallet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ pub(crate) struct Create {

impl Create {
pub(crate) fn run(&self, options: Options) -> Result {
if !(self.name == "ord" || self.name.starts_with("ord-")) {
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
bail!("`ord wallet create` may only be used with a wallet named `ord` or whose name starts with `ord-`");
}

let client = options.bitcoin_rpc_client_for_wallet_command(true)?;

client.create_wallet(&self.name, None, Some(true), None, None)?;
Expand Down
12 changes: 6 additions & 6 deletions tests/wallet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ fn detect_wrong_descriptors() {
}

#[test]
fn create_wallet_with_wrong_name() {
fn create_with_different_name() {
let rpc_server = test_bitcoincore_rpc::spawn();

CommandBuilder::new("wallet create --name nft-wallet")
assert!(!rpc_server.wallets().contains("inscription-wallet"));

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

assert!(rpc_server.wallets().contains("inscription-wallet"));
}
17 changes: 17 additions & 0 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,20 @@ fn inscribe_with_fee_rate() {

pretty_assert_eq!(fee_rate, 2.0);
}

#[test]
fn inscribe_with_wallet_named_foo() {
let rpc_server = test_bitcoincore_rpc::spawn();

CommandBuilder::new("wallet create --name foo")
.rpc_server(&rpc_server)
.run();

rpc_server.mine_blocks(1);

CommandBuilder::new("--wallet foo wallet inscribe degenerate.png")
.write("degenerate.png", [1; 520])
.rpc_server(&rpc_server)
.stdout_regex("commit\t[[:xdigit:]]{64}\nreveal\t[[:xdigit:]]{64}\n")
.run();
}
17 changes: 10 additions & 7 deletions tests/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,19 @@ fn send_inscribed_sat() {
}

#[test]
fn send_on_mainnnet_refuses_to_work_with_wallet_name_foo() {
let rpc_server = test_bitcoincore_rpc::builder().wallet_name("foo").build();
fn send_on_mainnnet_works_with_wallet_named_foo() {
let rpc_server = test_bitcoincore_rpc::spawn();
let txid = rpc_server.mine_blocks(1)[0].txdata[0].txid();

CommandBuilder::new(
format!("wallet send bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 {txid}:0:0")
)
CommandBuilder::new("wallet create --name foo")
.rpc_server(&rpc_server)
.run();

CommandBuilder::new(format!(
"--wallet foo wallet send bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 {txid}:0:0"
))
.rpc_server(&rpc_server)
.expected_stderr("error: wallet commands may only be used on mainnet with a wallet named `ord` or whose name starts with `ord-`\n")
.expected_exit_code(1)
.stdout_regex(r"[[:xdigit:]]{64}\n")
.run();
}

Expand Down