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 chain flags #961

Merged
merged 3 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 10 additions & 10 deletions docs/src/guides/inscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ Receiving Satoshis
Inscriptions are made on individual satoshis, using normal Bitcoin transactions
that pay fees in satoshis, so your wallet will need some sats.

Get a new address from your `ord` wallet by running `ord --chain signet wallet
Get a new address from your `ord` wallet by running `ord --signet wallet
receive`

Use a signet faucet to send satoshis to the address you generated. Two faucets
you might try are [signet.bc-2.jp](https://signet.bc-2.jp/) and
[alt.signetfaucet.com](https://alt.signetfaucet.com/).

Once the faucet transaction confirms, you should be able to see the
transactions outputs with `ord --chain signet wallet utxos`.
transactions outputs with `ord --signet wallet utxos`.

Creating Inscription Content
----------------------------
Expand All @@ -155,7 +155,7 @@ Creating Inscriptions
To create an inscription with the contents of `FILE`, run:

```
ord --chain signet wallet inscribe --file FILE
ord --signet wallet inscribe --file FILE
```

Ord will output two transactions IDs, one for the commit transaction, and one
Expand All @@ -174,7 +174,7 @@ Once the reveal transaction has been mined, the inscription ID should be
printed when you run:

```
ord --chain signet wallet inscriptions
ord --signet wallet inscriptions
```

And when you visit [the signet ordinals explorer](https://signet.ordinals.com/)
Expand All @@ -187,20 +187,20 @@ Sending Inscriptions
Ask the recipient to generate a new address by running:

```
ord --chain signet wallet receive
ord --signet wallet receive
```

Send the inscription by running:

```
ord --chain signet wallet send INSCRIPTION_ID ADDRESS
ord --signet wallet send INSCRIPTION_ID ADDRESS
```

Once the send transaction confirms, the recipient can confirm receipt by
running:

```
ord --chain signet wallet inscriptions
ord --signet wallet inscriptions
```

Receiving Inscriptions
Expand All @@ -209,17 +209,17 @@ Receiving Inscriptions
Generate a new receive address using:

```
ord --chain signet wallet receive
ord --signet wallet receive
```

The sender can transfer the inscription to your address using:

```
ord --chain signet wallet send INSCRIPTION_ID ADDRESS
ord --signet wallet send INSCRIPTION_ID ADDRESS
```

Once the send transaction confirms, you can can confirm receipt by running:

```
ord --chain signet wallet inscriptions
ord --signet wallet inscriptions
```
6 changes: 3 additions & 3 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Index {
// The default max database size is 10 MiB for Regtest and 1 TiB
// for all other networks. A larger database takes longer to
// initialize, so unit tests should use the regtest network.
assert_eq!(options.chain, Chain::Regtest);
assert_eq!(options.chain(), Chain::Regtest);
}

log::info!(
Expand Down Expand Up @@ -222,12 +222,12 @@ impl Index {
};

let genesis_block_coinbase_transaction =
options.chain.genesis_block().coinbase().unwrap().clone();
options.chain().genesis_block().coinbase().unwrap().clone();

Ok(Self {
genesis_block_coinbase_txid: genesis_block_coinbase_transaction.txid(),
auth,
chain: options.chain,
chain: options.chain(),
client,
database,
database_path,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use {
bitcoincore_rpc::RpcApi,
chain::Chain,
chrono::{NaiveDateTime, TimeZone, Utc},
clap::Parser,
clap::{ArgGroup, Parser},
derive_more::{Display, FromStr},
html_escaper::{Escape, Trusted},
regex::Regex,
Expand Down
69 changes: 61 additions & 8 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ use {
};

#[derive(Debug, Parser)]
#[clap(group(
ArgGroup::new("chains")
.required(false)
.args(&["chain", "signet", "regtest", "testnet"]),
))]
pub(crate) struct Options {
#[clap(long, help = "Load Bitcoin Core data dir from <BITCOIN_DATA_DIR>.")]
bitcoin_data_dir: Option<PathBuf>,
#[clap(long, arg_enum, default_value = "mainnet", help = "Index <CHAIN>.")]
pub(crate) chain: Chain,
#[clap(long, arg_enum, default_value = "mainnet", help = "Use <CHAIN>.")]
chain: Chain,
#[clap(long, help = "Load Bitcoin Core RPC cookie file from <COOKIE_FILE>.")]
cookie_file: Option<PathBuf>,
#[clap(long, help = "Store index in <DATA_DIR>.")]
Expand All @@ -19,16 +24,34 @@ pub(crate) struct Options {
pub(crate) index: Option<PathBuf>,
#[clap(long, help = "Index current location of all satoshis.")]
pub(crate) index_satoshis: bool,
#[clap(long, help = "Use regtest.")]
regtest: bool,
#[clap(long, help = "Connect to Bitcoin Core RPC at <RPC_URL>.")]
rpc_url: Option<String>,
#[clap(long, help = "Use signet.")]
signet: bool,
#[clap(long, help = "Use testnet.")]
testnet: bool,
}

impl Options {
pub(crate) fn chain(&self) -> Chain {
if self.signet {
Chain::Signet
} else if self.regtest {
Chain::Regtest
} else if self.testnet {
Chain::Testnet
} else {
self.chain
}
}

pub(crate) fn rpc_url(&self) -> String {
self
.rpc_url
.as_ref()
.unwrap_or(&format!("127.0.0.1:{}", self.chain.default_rpc_port(),))
.unwrap_or(&format!("127.0.0.1:{}", self.chain().default_rpc_port(),))
.into()
}

Expand All @@ -49,7 +72,7 @@ impl Options {
.join("Bitcoin")
};

let path = self.chain.join_with_data_dir(&path);
let path = self.chain().join_with_data_dir(&path);

Ok(path.join(".cookie"))
}
Expand All @@ -62,7 +85,7 @@ impl Options {
.join("ord"),
};

Ok(self.chain.join_with_data_dir(&base))
Ok(self.chain().join_with_data_dir(&base))
}

pub(crate) fn bitcoin_rpc_client(&self) -> Result<Client> {
Expand All @@ -84,7 +107,7 @@ impl Options {
other => bail!("Bitcoin RPC server on unknown chain: {other}"),
};

let ord_chain = self.chain;
let ord_chain = self.chain();

if rpc_chain != ord_chain {
bail!("Bitcoin RPC server is on {rpc_chain} but ord is on {ord_chain}");
Expand All @@ -96,7 +119,7 @@ impl Options {
pub(crate) fn bitcoin_rpc_client_mainnet_forbidden(&self, command: &str) -> Result<Client> {
let client = self.bitcoin_rpc_client()?;

if self.chain == Chain::Mainnet {
if self.chain() == Chain::Mainnet {
bail!("`{command}` is unstable and not yet supported on mainnet.");
}
Ok(client)
Expand All @@ -105,7 +128,7 @@ impl Options {
pub(crate) fn bitcoin_rpc_client_for_wallet_command(&self, command: &str) -> Result<Client> {
let client = self.bitcoin_rpc_client()?;

if self.chain == Chain::Mainnet {
if self.chain() == Chain::Mainnet {
let wallet_info = client.get_wallet_info()?;

if !(wallet_info.wallet_name == "ord" || wallet_info.wallet_name.starts_with("ord-")) {
Expand Down Expand Up @@ -371,4 +394,34 @@ mod tests {
"Bitcoin RPC server is on testnet but ord is on mainnet"
);
}

#[test]
fn chain_flags() {
Arguments::try_parse_from(["ord", "--signet", "--chain", "signet", "index"]).unwrap_err();
assert_eq!(
Arguments::try_parse_from(["ord", "--signet", "index"])
.unwrap()
.options
.chain(),
Chain::Signet
);

Arguments::try_parse_from(["ord", "--regtest", "--chain", "signet", "index"]).unwrap_err();
assert_eq!(
Arguments::try_parse_from(["ord", "--regtest", "index"])
.unwrap()
.options
.chain(),
Chain::Regtest
);

Arguments::try_parse_from(["ord", "--testnet", "--chain", "signet", "index"]).unwrap_err();
assert_eq!(
Arguments::try_parse_from(["ord", "--testnet", "index"])
.unwrap()
.options
.chain(),
Chain::Testnet
);
}
}
2 changes: 1 addition & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Server {
.route("/status", get(Self::status))
.route("/tx/:txid", get(Self::transaction))
.layer(Extension(index))
.layer(Extension(options.chain))
.layer(Extension(options.chain()))
.layer(
CorsLayer::new()
.allow_methods([http::Method::GET])
Expand Down
6 changes: 3 additions & 3 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Inscribe {
pub(crate) fn run(self, options: Options) -> Result {
let client = options.bitcoin_rpc_client_mainnet_forbidden("ord wallet inscribe")?;

let inscription = Inscription::from_file(options.chain, &self.file)?;
let inscription = Inscription::from_file(options.chain(), &self.file)?;

let index = Index::open(&options)?;
index.update()?;
Expand All @@ -46,13 +46,13 @@ impl Inscribe {
self.satpoint,
inscription,
inscriptions,
options.chain.network(),
options.chain().network(),
utxos,
commit_tx_change,
reveal_tx_destination,
)?;

Inscribe::backup_recovery_key(&client, recovery_key_pair, options.chain.network())?;
Inscribe::backup_recovery_key(&client, recovery_key_pair, options.chain().network())?;

let signed_raw_commit_tx = client
.sign_raw_transaction_with_wallet(&unsigned_commit_tx, None, None)?
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ impl Send {
pub(crate) fn run(self, options: Options) -> Result {
let client = options.bitcoin_rpc_client_for_wallet_command("ord wallet send")?;

if !self.address.is_valid_for_network(options.chain.network()) {
if !self.address.is_valid_for_network(options.chain().network()) {
bail!(
"Address `{}` is not valid for {}",
self.address,
options.chain
options.chain()
);
}

Expand Down