From 325201f0840ba4e7113ce5ea8ef840c6e5f89a49 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 14 Nov 2022 12:51:48 -0800 Subject: [PATCH] Deduct fee before calculating reveal transaction signature (#780) --- src/subcommand/wallet/inscribe.rs | 39 +++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/subcommand/wallet/inscribe.rs b/src/subcommand/wallet/inscribe.rs index 1d5bebeb5c..332fbc38ff 100644 --- a/src/subcommand/wallet/inscribe.rs +++ b/src/subcommand/wallet/inscribe.rs @@ -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, @@ -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 @@ -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)) } }