|
15 | 15 | use bitcoin; |
16 | 16 | use bitcoin::blockdata::witness::Witness; |
17 | 17 | use bitcoin::hashes::{hash160, sha256, Hash}; |
| 18 | +use bitcoin::schnorr::TapTweak; |
| 19 | +use bitcoin::util::taproot::{ControlBlock, TAPROOT_ANNEX_PREFIX}; |
18 | 20 |
|
19 | | -use {BareCtx, Legacy, Segwitv0}; |
| 21 | +use {BareCtx, Legacy, Segwitv0, Tap}; |
20 | 22 |
|
21 | 23 | use super::{stack, BitcoinKey, Error, Stack, TaggedHash160}; |
22 | 24 | use miniscript::context::{NoChecksEcdsa, ScriptContext}; |
@@ -209,15 +211,48 @@ pub(super) fn from_txdata<'txin>( |
209 | 211 | .map_err(|_| Error::XOnlyPublicKeyParseError)?; |
210 | 212 | if wit_stack.len() == 1 { |
211 | 213 | // Key spend |
212 | | - // let sig = |
213 | | - // Tr inference to be done in future commit |
214 | | - panic!("TODO"); |
| 214 | + Ok(( |
| 215 | + Inner::PublicKey(output_key.into(), PubkeyType::Tr), |
| 216 | + wit_stack, |
| 217 | + None, // Tr script code None |
| 218 | + )) |
| 219 | + } else { |
| 220 | + // wit_stack.len() >=2 |
| 221 | + // Check for annex |
| 222 | + let ctrl_blk = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?; |
| 223 | + let ctrl_blk = ctrl_blk.as_push()?; |
| 224 | + let tap_script = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?; |
| 225 | + if ctrl_blk.len() > 0 && ctrl_blk[0] == TAPROOT_ANNEX_PREFIX { |
| 226 | + // Annex is non-standard, bitcoin consensus rules ignore it. |
| 227 | + // Our sighash structure and signature verification |
| 228 | + // does not support annex, return error |
| 229 | + return Err(Error::TapAnnexUnsupported); |
| 230 | + } else if wit_stack.len() >= 2 { |
| 231 | + let ctrl_blk = ControlBlock::from_slice(ctrl_blk) |
| 232 | + .map_err(|e| Error::ControlBlockParse(e))?; |
| 233 | + let tap_script = script_from_stackelem::<Tap>(&tap_script)?; |
| 234 | + let ms = taproot_to_no_checks(&tap_script); |
| 235 | + // Creating new contexts is cheap |
| 236 | + let secp = bitcoin::secp256k1::Secp256k1::verification_only(); |
| 237 | + // Should not really need to call dangerous assumed tweaked here. |
| 238 | + // Should be fixed after RC |
| 239 | + if ctrl_blk.verify_taproot_commitment( |
| 240 | + &secp, |
| 241 | + &output_key.dangerous_assume_tweaked(), |
| 242 | + &tap_script.encode(), |
| 243 | + ) { |
| 244 | + Ok(( |
| 245 | + Inner::Script(ms, ScriptType::Tr), |
| 246 | + wit_stack, |
| 247 | + None, // Tr script code None |
| 248 | + )) |
| 249 | + } else { |
| 250 | + return Err(Error::ControlBlockVerificationError); |
| 251 | + } |
| 252 | + } else { |
| 253 | + return Err(Error::UnexpectedStackBoolean); |
| 254 | + } |
215 | 255 | } |
216 | | - Ok(( |
217 | | - Inner::PublicKey(output_key.into(), PubkeyType::Tr), |
218 | | - ssig_stack, |
219 | | - None, // Tr script code None |
220 | | - )) |
221 | 256 | } |
222 | 257 | // ** pay to scripthash ** |
223 | 258 | } else if spk.is_p2sh() { |
|
0 commit comments