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

Deduct fee before calculating signature #780

Merged
merged 1 commit into from
Nov 14, 2022
Merged
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
39 changes: 27 additions & 12 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use {
super::*,
bitcoin::{
blockdata::{opcodes, script},
secp256k1::{self, rand, KeyPair, Secp256k1, XOnlyPublicKey},
secp256k1::{
self, constants::SCHNORR_SIGNATURE_SIZE, rand, schnorr::Signature, KeyPair, Secp256k1,
XOnlyPublicKey,
},
util::sighash::{Prevouts, SighashCache},
util::taproot::{LeafVersion, TapLeafHash, TaprootBuilder},
PackedLockTime, SchnorrSighashType, Witness,
Expand Down Expand Up @@ -119,6 +122,29 @@ impl Inscribe {
version: 1,
};

let fee = {
let mut reveal_tx = reveal_tx.clone();

reveal_tx.input[0].witness.push(
Signature::from_slice(&[0; SCHNORR_SIGNATURE_SIZE])
.unwrap()
.as_ref(),
);
reveal_tx.input[0].witness.push(&script);
reveal_tx.input[0].witness.push(&control_block.serialize());

TransactionBuilder::TARGET_FEE_RATE * reveal_tx.vsize().try_into().unwrap()
};

reveal_tx.output[0].value = reveal_tx.output[0]
.value
.checked_sub(fee.to_sat())
.context("commit transaction output value insufficient to pay transaction fee")?;

if reveal_tx.output[0].value < reveal_tx.output[0].script_pubkey.dust_value().to_sat() {
bail!("commit transaction output would be dust");
}

let mut sighash_cache = SighashCache::new(&mut reveal_tx);

let signature_hash = sighash_cache
Expand All @@ -143,17 +169,6 @@ impl Inscribe {
witness.push(script);
witness.push(&control_block.serialize());

let fee = TransactionBuilder::TARGET_FEE_RATE * reveal_tx.vsize().try_into().unwrap();

reveal_tx.output[0].value = reveal_tx.output[0]
.value
.checked_sub(fee.to_sat())
.context("commit transaction output value insufficient to pay transaction fee")?;

if reveal_tx.output[0].value < reveal_tx.output[0].script_pubkey.dust_value().to_sat() {
bail!("commit transaction output would be dust");
}

Ok((unsigned_commit_tx, reveal_tx))
}
}
Expand Down