Skip to content

Commit

Permalink
Print PSBT for dry run inscribe (ordinals#3116)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Feb 27, 2024
1 parent f943248 commit 6d3065f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use {
super::*,
base64::{self, Engine},
bitcoin::secp256k1::{All, Secp256k1},
bitcoin::{
bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, Fingerprint},
psbt::Psbt,
Network,
},
bitcoincore_rpc::bitcoincore_rpc_json::{Descriptor, ImportDescriptors, Timestamp},
Expand Down Expand Up @@ -62,7 +64,6 @@ impl Wallet {
);

if let Some((username, password)) = options.credentials() {
use base64::Engine;
let credentials =
base64::engine::general_purpose::STANDARD.encode(format!("{username}:{password}"));
headers.insert(
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ pub struct InscriptionInfo {
#[derive(Serialize, Deserialize)]
pub struct Output {
pub commit: Txid,
pub commit_psbt: Option<String>,
pub inscriptions: Vec<InscriptionInfo>,
pub parent: Option<InscriptionId>,
pub reveal: Txid,
pub reveal_psbt: Option<String>,
pub total_fees: u64,
}

Expand Down
33 changes: 31 additions & 2 deletions src/wallet/inscribe/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,24 @@ impl Batch {
)?;

if self.dry_run {
let commit_psbt = wallet
.bitcoin_client()
.wallet_process_psbt(
&base64::engine::general_purpose::STANDARD
.encode(Psbt::from_unsigned_tx(Self::remove_witnesses(commit_tx.clone()))?.serialize()),
Some(false),
None,
None,
)?
.psbt;

let reveal_psbt = Psbt::from_unsigned_tx(Self::remove_witnesses(reveal_tx.clone()))?;

return Ok(Some(Box::new(self.output(
commit_tx.txid(),
Some(commit_psbt),
reveal_tx.txid(),
Some(base64::engine::general_purpose::STANDARD.encode(reveal_psbt.serialize())),
total_fees,
self.inscriptions.clone(),
))));
Expand Down Expand Up @@ -118,16 +133,28 @@ impl Batch {

Ok(Some(Box::new(self.output(
commit,
None,
reveal,
None,
total_fees,
self.inscriptions.clone(),
))))
}

fn remove_witnesses(mut transaction: Transaction) -> Transaction {
for txin in transaction.input.iter_mut() {
txin.witness = Witness::new();
}

transaction
}

fn output(
&self,
commit: Txid,
commit_psbt: Option<String>,
reveal: Txid,
reveal_psbt: Option<String>,
total_fees: u64,
inscriptions: Vec<Inscription>,
) -> Output {
Expand Down Expand Up @@ -174,7 +201,9 @@ impl Batch {

Output {
commit,
commit_psbt,
reveal,
reveal_psbt,
total_fees,
parent: self.parent_info.clone().map(|info| info.id),
inscriptions: inscriptions_output,
Expand Down Expand Up @@ -266,9 +295,9 @@ impl Batch {
reinscription = true;
if self.reinscribe {
continue;
} else {
bail!("sat at {} already inscribed", satpoint);
}

bail!("sat at {} already inscribed", satpoint);
}

if inscribed_satpoint.outpoint == satpoint.outpoint {
Expand Down
19 changes: 13 additions & 6 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,20 +416,27 @@ fn inscribe_with_dry_run_flag() {

bitcoin_rpc_server.mine_blocks(1);

CommandBuilder::new("wallet inscribe --dry-run --file degenerate.png --fee-rate 1")
.write("degenerate.png", [1; 520])
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<Inscribe>();
let inscribe =
CommandBuilder::new("wallet inscribe --dry-run --file degenerate.png --fee-rate 1")
.write("degenerate.png", [1; 520])
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<Inscribe>();

assert!(inscribe.commit_psbt.is_some());
assert!(inscribe.reveal_psbt.is_some());

assert!(bitcoin_rpc_server.mempool().is_empty());

CommandBuilder::new("wallet inscribe --file degenerate.png --fee-rate 1")
let inscribe = CommandBuilder::new("wallet inscribe --file degenerate.png --fee-rate 1")
.write("degenerate.png", [1; 520])
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<Inscribe>();

assert!(inscribe.commit_psbt.is_none());
assert!(inscribe.reveal_psbt.is_none());

assert_eq!(bitcoin_rpc_server.mempool().len(), 2);
}

Expand Down

0 comments on commit 6d3065f

Please sign in to comment.