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 options to inscribe straight to an address, and/or send commit change to an address #1512

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ impl Preview {
no_backup: true,
satpoint: None,
dry_run: false,
commit_change: None,
inscription_destination: None,
},
)),
}
Expand Down
111 changes: 102 additions & 9 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::subcommand::wallet::transaction_builder::ChangeAddresses;
use {
super::*,
bitcoin::{
Expand Down Expand Up @@ -41,10 +42,19 @@ pub(crate) struct Inscribe {
pub(crate) no_backup: bool,
#[clap(long, help = "Don't sign or broadcast transactions.")]
pub(crate) dry_run: bool,
#[clap(long, help = "Send commit change to <COMMIT_CHANGE>")]
pub(crate) commit_change: Option<Address>,
#[clap(long, help = "Send reveal output to <INSCRIPTION_DESTINATION>")]
pub(crate) inscription_destination: Option<Address>,
}

impl Inscribe {
pub(crate) fn run(self, options: Options) -> Result {
if self.satpoint.is_some() && self.commit_change.is_some() {
// inscribing a specific satspoint might require splitting a UTXO. don't allow specifying a single
// change address if a specific satspoint is being inscribed.
bail!("Can not specify satspoint and commit-change in the same command.")
}
let client = options.bitcoin_rpc_client_for_wallet_command(false)?;

let inscription = Inscription::from_file(options.chain(), &self.file)?;
Expand All @@ -56,9 +66,14 @@ impl Inscribe {

let inscriptions = index.get_inscriptions(None)?;

let commit_tx_change = [get_change_address(&client)?, get_change_address(&client)?];
let commit_tx_change = match self.commit_change {
Some(address) => ChangeAddresses::Single(address),
None => ChangeAddresses::Double([get_change_address(&client)?, get_change_address(&client)?]),
};

let reveal_tx_destination = get_change_address(&client)?;
let reveal_tx_destination = self
.inscription_destination
.unwrap_or(get_change_address(&client)?);

let (unsigned_commit_tx, reveal_tx, recovery_key_pair) =
Inscribe::create_inscription_transactions(
Expand Down Expand Up @@ -129,10 +144,15 @@ impl Inscribe {
inscriptions: BTreeMap<SatPoint, InscriptionId>,
network: Network,
utxos: BTreeMap<OutPoint, Amount>,
change: [Address; 2],
change: ChangeAddresses,
destination: Address,
fee_rate: FeeRate,
) -> Result<(Transaction, Transaction, TweakedKeyPair)> {
if let ChangeAddresses::Single(_) = change {
if satpoint.is_some() {
bail!("Can not specify satpoint and only provide a single change address")
}
}
let satpoint = if let Some(satpoint) = satpoint {
satpoint
} else {
Expand Down Expand Up @@ -365,7 +385,7 @@ mod tests {
BTreeMap::new(),
Network::Bitcoin,
utxos.into_iter().collect(),
[commit_address, change(1)],
[commit_address, change(1)].into(),
reveal_address,
FeeRate::try_from(1.0).unwrap(),
)
Expand Down Expand Up @@ -394,7 +414,7 @@ mod tests {
BTreeMap::new(),
Network::Bitcoin,
utxos.into_iter().collect(),
[commit_address, change(1)],
[commit_address, change(1)].into(),
reveal_address,
FeeRate::try_from(1.0).unwrap(),
)
Expand Down Expand Up @@ -427,7 +447,7 @@ mod tests {
inscriptions,
Network::Bitcoin,
utxos.into_iter().collect(),
[commit_address, change(1)],
[commit_address, change(1)].into(),
reveal_address,
FeeRate::try_from(1.0).unwrap(),
)
Expand Down Expand Up @@ -467,7 +487,7 @@ mod tests {
inscriptions,
Network::Bitcoin,
utxos.into_iter().collect(),
[commit_address, change(1)],
[commit_address, change(1)].into(),
reveal_address,
FeeRate::try_from(1.0).unwrap(),
)
Expand Down Expand Up @@ -501,7 +521,7 @@ mod tests {
inscriptions,
bitcoin::Network::Signet,
utxos.into_iter().collect(),
[commit_address, change(1)],
[commit_address, change(1)].into(),
reveal_address,
FeeRate::try_from(fee_rate).unwrap(),
)
Expand Down Expand Up @@ -533,7 +553,7 @@ mod tests {
BTreeMap::new(),
Network::Bitcoin,
utxos.into_iter().collect(),
[commit_address, change(1)],
[commit_address, change(1)].into(),
reveal_address,
FeeRate::try_from(1.0).unwrap(),
)
Expand All @@ -546,4 +566,77 @@ mod tests {
error
);
}

#[test]
fn inscribe_with_specific_destination() {
let utxos = vec![(outpoint(1), Amount::from_sat(20000))];
let inscription = inscription("text/plain", "ord");
let commit_address = change(0);
let reveal_address = recipient();

let (_, reveal_tx, _) = Inscribe::create_inscription_transactions(
Some(satpoint(1, 0)),
inscription,
BTreeMap::new(),
Network::Bitcoin,
utxos.into_iter().collect(),
[commit_address, change(1)].into(),
reveal_address,
FeeRate::try_from(1.0).unwrap(),
)
.unwrap();

assert_eq!(
reveal_tx.output.first().unwrap().script_pubkey,
recipient().script_pubkey()
);
}

#[test]
fn inscribe_with_specific_commit_change_address() {
let utxos = vec![(outpoint(1), Amount::from_sat(20000))];
let inscription = inscription("text/plain", "ord");
let change_address = change(1);
let reveal_address = recipient();

let (commit_tx, _, _) = Inscribe::create_inscription_transactions(
None,
inscription,
BTreeMap::new(),
Network::Bitcoin,
utxos.into_iter().collect(),
change_address.clone().into(),
reveal_address,
FeeRate::try_from(1.0).unwrap(),
)
.unwrap();
assert_eq!(
commit_tx.output.get(1).unwrap().script_pubkey,
change_address.script_pubkey()
);
}

#[test]
fn cant_inscribe_satpoint_with_single_change_address() {
let utxos = vec![(outpoint(1), Amount::from_sat(50 * COIN_VALUE))];

let inscription = inscription("text/plain", [0; MAX_STANDARD_TX_WEIGHT as usize]);
let change_address = change(0);
let reveal_address = recipient();

let error = Inscribe::create_inscription_transactions(
Some(satpoint(1, 0)),
inscription,
BTreeMap::new(),
Network::Bitcoin,
utxos.into_iter().collect(),
change_address.into(),
reveal_address,
FeeRate::try_from(1.0).unwrap(),
)
.unwrap_err()
.to_string();

assert!(error.contains("Can not specify satpoint and only provide a single change address"));
}
}
4 changes: 3 additions & 1 deletion src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::subcommand::wallet::transaction_builder::ChangeAddresses;

#[derive(Debug, Parser)]
pub(crate) struct Send {
Expand Down Expand Up @@ -73,7 +74,8 @@ impl Send {
}
};

let change = [get_change_address(&client)?, get_change_address(&client)?];
let change =
ChangeAddresses::Double([get_change_address(&client)?, get_change_address(&client)?]);

let unsigned_transaction = TransactionBuilder::build_transaction_with_postage(
satpoint,
Expand Down
Loading