commit efed72b8c6845578641b82667607da7c8b3c2558
parent 7f3e147b2f8144918c1efb16659f73f8c7566691
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 10 Oct 2026 19:02:19 +0400
test, bench: port to the new TxId and witness API
Diffstat:
3 files changed, 112 insertions(+), 89 deletions(-)
diff --git a/bench/Main.hs b/bench/Main.hs
@@ -11,13 +11,14 @@ import Data.Word (Word64)
import Bitcoin.Prim.Tx
import Bitcoin.Prim.Tx.Sighash
--- NFData instances ------------------------------------------------------------
+-- NFData instances -----------------------------------------------------------
--- sample data -----------------------------------------------------------------
+-- sample data ----------------------------------------------------------------
-- | Sample outpoint (references a dummy txid).
sampleOutPoint :: OutPoint
-sampleOutPoint = OutPoint (TxId (BS.replicate 32 0xab)) 0
+sampleOutPoint =
+ OutPoint (maybe null_txid id (mk_txid (BS.replicate 32 0xab))) 0
-- | Sample input with typical P2PKH signature (~107 bytes).
sampleInput :: TxIn
@@ -25,14 +26,16 @@ sampleInput = TxIn
{ txin_prevout = sampleOutPoint
, txin_script_sig = BS.replicate 107 0x00 -- typical P2PKH sig
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
--- | Sample input for segwit (empty scriptSig).
+-- | Sample input for segwit (empty scriptSig, P2WPKH-style witness).
sampleSegwitInput :: TxIn
sampleSegwitInput = TxIn
{ txin_prevout = sampleOutPoint
, txin_script_sig = BS.empty
, txin_sequence = 0xffffffff
+ , txin_witness = sampleWitness
}
-- | Sample output with typical P2PKH script (25 bytes).
@@ -56,7 +59,6 @@ mkLegacyTx !numInputs !numOutputs = Tx
{ tx_version = 1
, tx_inputs = sampleInput :| replicate (numInputs - 1) sampleInput
, tx_outputs = sampleOutput :| replicate (numOutputs - 1) sampleOutput
- , tx_witnesses = []
, tx_locktime = 0
}
@@ -65,13 +67,13 @@ mkLegacyTx !numInputs !numOutputs = Tx
mkSegwitTx :: Int -> Int -> Tx
mkSegwitTx !numInputs !numOutputs = Tx
{ tx_version = 2
- , tx_inputs = sampleSegwitInput :| replicate (numInputs - 1) sampleSegwitInput
+ , tx_inputs =
+ sampleSegwitInput :| replicate (numInputs - 1) sampleSegwitInput
, tx_outputs = sampleOutput :| replicate (numOutputs - 1) sampleOutput
- , tx_witnesses = replicate numInputs sampleWitness
, tx_locktime = 0
}
--- sample transactions ---------------------------------------------------------
+-- sample transactions --------------------------------------------------------
smallLegacyTx, mediumLegacyTx, largeLegacyTx :: Tx
smallLegacyTx = mkLegacyTx 1 1
@@ -83,7 +85,7 @@ smallSegwitTx = mkSegwitTx 1 1
mediumSegwitTx = mkSegwitTx 5 5
largeSegwitTx = mkSegwitTx 20 20
--- serialised bytes ------------------------------------------------------------
+-- serialised bytes -----------------------------------------------------------
smallLegacyBytes, mediumLegacyBytes, largeLegacyBytes :: BS.ByteString
smallLegacyBytes = to_bytes smallLegacyTx
@@ -95,7 +97,7 @@ smallSegwitBytes = to_bytes smallSegwitTx
mediumSegwitBytes = to_bytes mediumSegwitTx
largeSegwitBytes = to_bytes largeSegwitTx
--- sighash inputs --------------------------------------------------------------
+-- sighash inputs -------------------------------------------------------------
-- | Typical P2PKH scriptPubKey (25 bytes).
sampleScriptPubKey :: BS.ByteString
@@ -125,7 +127,7 @@ taprootSpks n = replicate n sampleTaprootSpk
sampleTapLeaf :: BS.ByteString
sampleTapLeaf = BS.replicate 32 0x00
--- benchmarks ------------------------------------------------------------------
+-- benchmarks -----------------------------------------------------------------
main :: IO ()
main = defaultMain
diff --git a/bench/Weight.hs b/bench/Weight.hs
@@ -11,13 +11,14 @@ import qualified Weigh as W
import Bitcoin.Prim.Tx
import Bitcoin.Prim.Tx.Sighash
--- NFData instances ------------------------------------------------------------
+-- NFData instances -----------------------------------------------------------
--- sample data -----------------------------------------------------------------
+-- sample data ----------------------------------------------------------------
-- | Sample outpoint (references a dummy txid).
sampleOutPoint :: OutPoint
-sampleOutPoint = OutPoint (TxId (BS.replicate 32 0xab)) 0
+sampleOutPoint =
+ OutPoint (maybe null_txid id (mk_txid (BS.replicate 32 0xab))) 0
-- | Sample input with typical P2PKH signature (~107 bytes).
sampleInput :: TxIn
@@ -25,14 +26,16 @@ sampleInput = TxIn
{ txin_prevout = sampleOutPoint
, txin_script_sig = BS.replicate 107 0x00 -- typical P2PKH sig
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
--- | Sample input for segwit (empty scriptSig).
+-- | Sample input for segwit (empty scriptSig, P2WPKH-style witness).
sampleSegwitInput :: TxIn
sampleSegwitInput = TxIn
{ txin_prevout = sampleOutPoint
, txin_script_sig = BS.empty
, txin_sequence = 0xffffffff
+ , txin_witness = sampleWitness
}
-- | Sample output with typical P2PKH script (25 bytes).
@@ -56,7 +59,6 @@ mkLegacyTx !numInputs !numOutputs = Tx
{ tx_version = 1
, tx_inputs = sampleInput :| replicate (numInputs - 1) sampleInput
, tx_outputs = sampleOutput :| replicate (numOutputs - 1) sampleOutput
- , tx_witnesses = []
, tx_locktime = 0
}
@@ -65,13 +67,13 @@ mkLegacyTx !numInputs !numOutputs = Tx
mkSegwitTx :: Int -> Int -> Tx
mkSegwitTx !numInputs !numOutputs = Tx
{ tx_version = 2
- , tx_inputs = sampleSegwitInput :| replicate (numInputs - 1) sampleSegwitInput
+ , tx_inputs =
+ sampleSegwitInput :| replicate (numInputs - 1) sampleSegwitInput
, tx_outputs = sampleOutput :| replicate (numOutputs - 1) sampleOutput
- , tx_witnesses = replicate numInputs sampleWitness
, tx_locktime = 0
}
--- sample transactions ---------------------------------------------------------
+-- sample transactions --------------------------------------------------------
smallLegacyTx, mediumLegacyTx, largeLegacyTx :: Tx
smallLegacyTx = mkLegacyTx 1 1
@@ -83,7 +85,7 @@ smallSegwitTx = mkSegwitTx 1 1
mediumSegwitTx = mkSegwitTx 5 5
largeSegwitTx = mkSegwitTx 20 20
--- serialised bytes ------------------------------------------------------------
+-- serialised bytes -----------------------------------------------------------
smallLegacyBytes, mediumLegacyBytes, largeLegacyBytes :: BS.ByteString
smallLegacyBytes = to_bytes smallLegacyTx
@@ -95,7 +97,7 @@ smallSegwitBytes = to_bytes smallSegwitTx
mediumSegwitBytes = to_bytes mediumSegwitTx
largeSegwitBytes = to_bytes largeSegwitTx
--- sighash inputs --------------------------------------------------------------
+-- sighash inputs -------------------------------------------------------------
sampleScriptPubKey :: BS.ByteString
sampleScriptPubKey = BS.replicate 25 0x00
@@ -118,7 +120,7 @@ taprootSpks n = replicate n sampleTaprootSpk
sampleTapLeaf :: BS.ByteString
sampleTapLeaf = BS.replicate 32 0x00
--- allocation benchmarks -------------------------------------------------------
+-- allocation benchmarks ------------------------------------------------------
main :: IO ()
main = W.mainWith $ do
diff --git a/test/Main.hs b/test/Main.hs
@@ -14,7 +14,7 @@ import Test.Tasty
import qualified Test.Tasty.HUnit as H
import Test.Tasty.QuickCheck as QC hiding (Witness)
--- main ------------------------------------------------------------------------
+-- main -----------------------------------------------------------------------
main :: IO ()
main = defaultMain $
@@ -46,10 +46,10 @@ main = defaultMain $
, edge_multi_witness
]
, testGroup "validation" [
- test_mkTxId_valid
- , test_mkTxId_short
- , test_mkTxId_long
- , test_mkTxId_empty
+ test_mk_txid_valid
+ , test_mk_txid_short
+ , test_mk_txid_long
+ , test_mk_txid_empty
, test_from_bytes_truncated
, test_from_bytes_trailing
, test_from_bytes_garbage
@@ -151,7 +151,7 @@ main = defaultMain $
]
]
--- helpers ---------------------------------------------------------------------
+-- helpers --------------------------------------------------------------------
-- | Decode a hex literal. Total: an invalid literal decodes to empty
-- bytes, which no vector expects, and the "fixtures parse" test
@@ -176,7 +176,7 @@ assertParses rawHex =
Nothing -> H.assertFailure "from_base16 returned Nothing"
Just _ -> pure ()
--- round-trip tests ------------------------------------------------------------
+-- round-trip tests -----------------------------------------------------------
-- Simple legacy tx: 1 input, 1 output, no witnesses
roundtrip_legacy_simple :: TestTree
@@ -187,16 +187,16 @@ roundtrip_legacy_simple = H.testCase "simple legacy tx" $
{ tx_version = 1
, tx_inputs = txin :| []
, tx_outputs = txout :| []
- , tx_witnesses = []
, tx_locktime = 0
}
txin = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0xab)
+ { op_txid = tid 0xab
, op_vout = 0
}
, txin_script_sig = hex "483045022100abcd"
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
txout = TxOut
{ txout_value = 50000
@@ -212,16 +212,16 @@ roundtrip_segwit = H.testCase "segwit tx with witnesses" $
{ tx_version = 2
, tx_inputs = txin :| []
, tx_outputs = txout :| []
- , tx_witnesses = [wit]
, tx_locktime = 500000
}
txin = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0x12)
+ { op_txid = tid 0x12
, op_vout = 1
}
, txin_script_sig = BS.empty -- segwit: empty scriptSig
, txin_sequence = 0xfffffffe
+ , txin_witness = wit
}
txout = TxOut
{ txout_value = 100000000
@@ -229,7 +229,8 @@ roundtrip_segwit = H.testCase "segwit tx with witnesses" $
}
wit = Witness
[ hex "304402201234"
- , hex "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
+ , hex ("0279be667ef9dcbbac55a06295ce870b"
+ <> "07029bfcdb2dce28d959f2815b16f81798")
]
-- Multiple inputs and outputs
@@ -241,32 +242,34 @@ roundtrip_multi_io = H.testCase "multiple inputs/outputs" $
{ tx_version = 1
, tx_inputs = txin1 :| [txin2, txin3]
, tx_outputs = txout1 :| [txout2]
- , tx_witnesses = []
, tx_locktime = 123456
}
txin1 = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0x11)
+ { op_txid = tid 0x11
, op_vout = 0
}
, txin_script_sig = hex "4730440220"
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
txin2 = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0x22)
+ { op_txid = tid 0x22
, op_vout = 2
}
, txin_script_sig = hex "483045022100"
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
txin3 = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0x33)
+ { op_txid = tid 0x33
, op_vout = 5
}
, txin_script_sig = hex "00"
, txin_sequence = 0xfffffffe
+ , txin_witness = Witness []
}
txout1 = TxOut
{ txout_value = 10000000
@@ -277,7 +280,7 @@ roundtrip_multi_io = H.testCase "multiple inputs/outputs" $
, txout_script_pubkey = hex "a914"
}
--- known vector tests ----------------------------------------------------------
+-- known vector tests ---------------------------------------------------------
-- First Bitcoin transaction ever (block 170, Satoshi to Hal Finney)
-- TxId: f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16
@@ -293,7 +296,8 @@ satoshiHalRaw =
\2e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000"
satoshiHalTxId :: BS.ByteString
-satoshiHalTxId = "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"
+satoshiHalTxId =
+ "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"
parse_satoshi_hal :: TestTree
parse_satoshi_hal = H.testCase "parse Satoshi->Hal tx (block 170)" $
@@ -304,7 +308,7 @@ txid_satoshi_hal = H.testCase "txid of Satoshi->Hal tx" $ do
case from_base16 satoshiHalRaw of
Nothing -> H.assertFailure "failed to parse tx"
Just tx -> do
- let TxId computed = txid tx
+ let computed = un_txid (txid tx)
-- txid is displayed big-endian, but stored little-endian
expected = BS.reverse (hex satoshiHalTxId)
H.assertEqual "txid mismatch" expected computed
@@ -323,7 +327,7 @@ parse_first_segwit :: TestTree
parse_first_segwit = H.testCase "parse first segwit tx (block 481824)" $
assertParses firstSegwitRaw
--- edge case tests -------------------------------------------------------------
+-- edge case tests ------------------------------------------------------------
-- Empty scriptSig (common in segwit)
edge_empty_scriptsig :: TestTree
@@ -334,16 +338,16 @@ edge_empty_scriptsig = H.testCase "empty scriptSig" $
{ tx_version = 2
, tx_inputs = txin :| []
, tx_outputs = txout :| []
- , tx_witnesses = [wit]
, tx_locktime = 0
}
txin = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0xff)
+ { op_txid = tid 0xff
, op_vout = 0
}
, txin_script_sig = BS.empty
, txin_sequence = 0xffffffff
+ , txin_witness = wit
}
txout = TxOut
{ txout_value = 1000
@@ -360,16 +364,16 @@ edge_max_sequence = H.testCase "maximum sequence (0xffffffff)" $
{ tx_version = 1
, tx_inputs = txin :| []
, tx_outputs = txout :| []
- , tx_witnesses = []
, tx_locktime = 0
}
txin = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0x00)
+ { op_txid = tid 0x00
, op_vout = 0xffffffff -- max vout too
}
, txin_script_sig = hex "00"
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
txout = TxOut
{ txout_value = 0
@@ -385,16 +389,16 @@ edge_zero_locktime = H.testCase "zero locktime" $
{ tx_version = 1
, tx_inputs = txin :| []
, tx_outputs = txout :| []
- , tx_witnesses = []
, tx_locktime = 0
}
txin = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0xaa)
+ { op_txid = tid 0xaa
, op_vout = 0
}
, txin_script_sig = hex "51" -- OP_1
, txin_sequence = 0
+ , txin_witness = Witness []
}
txout = TxOut
{ txout_value = 100
@@ -410,24 +414,25 @@ edge_multi_witness = H.testCase "multiple witness items" $
{ tx_version = 2
, tx_inputs = txin1 :| [txin2]
, tx_outputs = txout :| []
- , tx_witnesses = [witness1, witness2]
, tx_locktime = 0
}
txin1 = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0x01)
+ { op_txid = tid 0x01
, op_vout = 0
}
, txin_script_sig = BS.empty
, txin_sequence = 0xffffffff
+ , txin_witness = witness1
}
txin2 = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0x02)
+ { op_txid = tid 0x02
, op_vout = 1
}
, txin_script_sig = BS.empty
, txin_sequence = 0xffffffff
+ , txin_witness = witness2
}
txout = TxOut
{ txout_value = 50000
@@ -449,30 +454,30 @@ edge_multi_witness = H.testCase "multiple witness items" $
-- validation tests -----------------------------------------------------------
--- mkTxId: valid 32-byte input accepted
-test_mkTxId_valid :: TestTree
-test_mkTxId_valid = H.testCase "mkTxId accepts 32 bytes" $
- case mkTxId (BS.replicate 32 0x00) of
- Nothing -> H.assertFailure "mkTxId returned Nothing"
+-- mk_txid: valid 32-byte input accepted
+test_mk_txid_valid :: TestTree
+test_mk_txid_valid = H.testCase "mk_txid accepts 32 bytes" $
+ case mk_txid (BS.replicate 32 0x00) of
+ Nothing -> H.assertFailure "mk_txid returned Nothing"
Just _ -> pure ()
--- mkTxId: 31 bytes rejected
-test_mkTxId_short :: TestTree
-test_mkTxId_short = H.testCase "mkTxId rejects 31 bytes" $
+-- mk_txid: 31 bytes rejected
+test_mk_txid_short :: TestTree
+test_mk_txid_short = H.testCase "mk_txid rejects 31 bytes" $
H.assertEqual "should be Nothing"
- Nothing (mkTxId (BS.replicate 31 0x00))
+ Nothing (mk_txid (BS.replicate 31 0x00))
--- mkTxId: 33 bytes rejected
-test_mkTxId_long :: TestTree
-test_mkTxId_long = H.testCase "mkTxId rejects 33 bytes" $
+-- mk_txid: 33 bytes rejected
+test_mk_txid_long :: TestTree
+test_mk_txid_long = H.testCase "mk_txid rejects 33 bytes" $
H.assertEqual "should be Nothing"
- Nothing (mkTxId (BS.replicate 33 0x00))
+ Nothing (mk_txid (BS.replicate 33 0x00))
--- mkTxId: empty input rejected
-test_mkTxId_empty :: TestTree
-test_mkTxId_empty = H.testCase "mkTxId rejects empty" $
+-- mk_txid: empty input rejected
+test_mk_txid_empty :: TestTree
+test_mk_txid_empty = H.testCase "mk_txid rejects empty" $
H.assertEqual "should be Nothing"
- Nothing (mkTxId BS.empty)
+ Nothing (mk_txid BS.empty)
-- from_bytes: truncated input rejected
test_from_bytes_truncated :: TestTree
@@ -543,8 +548,7 @@ test_from_bytes_superfluous_witness =
test_to_bytes_empty_witnesses :: TestTree
test_to_bytes_empty_witnesses =
H.testCase "to_bytes uses the legacy format for empty witnesses" $ do
- let tx = legacyTx1 { tx_witnesses = map (const (Witness []))
- (NE.toList (tx_inputs legacyTx1)) }
+ let tx = strip_witnesses legacyTx1
H.assertEqual "legacy" (to_bytes_legacy tx) (to_bytes tx)
-- every transaction fixture literal parses
@@ -589,17 +593,17 @@ legacyTx1 = Tx
{ tx_version = 1
, tx_inputs = txin :| []
, tx_outputs = txout :| []
- , tx_witnesses = []
, tx_locktime = 0
}
where
txin = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0x00)
+ { op_txid = tid 0x00
, op_vout = 0
}
, txin_script_sig = hex "00"
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
txout = TxOut
{ txout_value = 0
@@ -617,16 +621,16 @@ sighash_legacy_minimal =
{ tx_version = 1
, tx_inputs = txin :| []
, tx_outputs = txout :| []
- , tx_witnesses = []
, tx_locktime = 0
}
txin = TxIn
{ txin_prevout = OutPoint
- { op_txid = TxId (BS.replicate 32 0x00)
+ { op_txid = tid 0x00
, op_vout = 0
}
, txin_script_sig = hex "00"
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
txout = TxOut
{ txout_value = 0
@@ -696,7 +700,7 @@ bip143_p2sh_p2wpkh = H.testCase "P2SH-P2WPKH" $ do
-- Arbitrary instances --------------------------------------------------------
instance Arbitrary TxId where
- arbitrary = TxId . BS.pack <$> vectorOf 32 arbitrary
+ arbitrary = maybe null_txid id . mk_txid . BS.pack <$> vectorOf 32 arbitrary
instance Arbitrary OutPoint where
arbitrary = OutPoint <$> arbitrary <*> arbitrary
@@ -706,6 +710,7 @@ instance Arbitrary TxIn where
<$> arbitrary
<*> arbitraryScript
<*> arbitrary
+ <*> pure (Witness [])
instance Arbitrary TxOut where
arbitrary = TxOut
@@ -749,7 +754,7 @@ genLegacyTx = do
ins <- arbitraryNonEmpty
outs <- arbitraryNonEmpty
lt <- arbitrary
- pure $ Tx ver ins outs [] lt
+ pure $ Tx ver ins outs lt
-- | Generate a valid segwit transaction (with witnesses).
genSegwitTx :: Gen Tx
@@ -763,7 +768,10 @@ genSegwitTx = do
w0 <- Witness <$> listOf1 arbitraryScript
ws <- vectorOf (numInputs - 1) arbitrary
lt <- arbitrary
- pure $ Tx ver ins outs (w0 : ws) lt
+ let set i w = i { txin_witness = w }
+ ins' = case ins of
+ i :| is -> set i w0 :| zipWith set is ws
+ pure $ Tx ver ins' outs lt
-- | Generate any valid transaction.
instance Arbitrary Tx where
@@ -793,20 +801,19 @@ prop_segwit_longer :: TestTree
prop_segwit_longer =
QC.testProperty "segwit tx: to_bytes longer than to_bytes_legacy" $
forAll genSegwitTx $ \tx ->
- not (null (tx_witnesses tx)) ==>
- BS.length (to_bytes tx) > BS.length (to_bytes_legacy tx)
+ BS.length (to_bytes tx) > BS.length (to_bytes_legacy tx)
-- TxId is always 32 bytes
prop_txid_32_bytes :: TestTree
prop_txid_32_bytes = QC.testProperty "txid is always 32 bytes" $
- \tx -> let TxId bs = txid tx in BS.length bs === 32
+ \tx -> BS.length (un_txid (txid tx)) === 32
-- TxId ignores witnesses (same txid with or without witnesses)
prop_txid_ignores_witnesses :: TestTree
prop_txid_ignores_witnesses =
QC.testProperty "txid ignores witnesses" $
forAll genSegwitTx $ \tx ->
- let txNoWit = tx { tx_witnesses = [] }
+ let txNoWit = strip_witnesses tx
in txid tx === txid txNoWit
-- sighash_legacy always returns 32 bytes
@@ -969,8 +976,8 @@ txid_first_segwit = H.testCase "txid of first-segwit fixture" $
case from_base16 firstSegwitRaw of
Nothing -> H.assertFailure "failed to parse tx"
Just tx -> do
- let TxId computed = txid tx
- expected = BS.reverse (hex firstSegwitTxId)
+ let computed = un_txid (txid tx)
+ expected = BS.reverse (hex firstSegwitTxId)
H.assertEqual "txid mismatch" expected computed
-- BIP143 P2SH-P2WSH multi-sighash vectors -----------------------------------
@@ -992,8 +999,18 @@ p2shP2wshTxHex = mconcat
-- (which the "fixtures parse" test reports).
placeholder_tx :: Tx
placeholder_tx = Tx 1
- (TxIn (OutPoint (TxId (BS.replicate 32 0x00)) 0) BS.empty 0 :| [])
- (TxOut 0 BS.empty :| []) [] 0
+ (TxIn (OutPoint null_txid 0) BS.empty 0 (Witness []) :| [])
+ (TxOut 0 BS.empty :| []) 0
+
+-- | A fixture txid: 32 copies of a byte.
+tid :: Word8 -> TxId
+tid w = maybe null_txid id (mk_txid (BS.replicate 32 w))
+
+-- | Clear every input's witness.
+strip_witnesses :: Tx -> Tx
+strip_witnesses tx =
+ tx { tx_inputs = fmap (\i -> i { txin_witness = Witness [] })
+ (tx_inputs tx) }
p2shP2wshScriptCode :: BS.ByteString
p2shP2wshScriptCode = hex $ mconcat
@@ -1309,14 +1326,14 @@ tinyTaprootTx = Tx
{ tx_version = 2
, tx_inputs = txin :| []
, tx_outputs = txout :| []
- , tx_witnesses = []
, tx_locktime = 0
}
where
txin = TxIn
- { txin_prevout = OutPoint (TxId (BS.replicate 32 0xab)) 0
+ { txin_prevout = OutPoint (tid 0xab) 0
, txin_script_sig = BS.empty
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
txout = TxOut
{ txout_value = 1000
@@ -1381,9 +1398,10 @@ taproot_single_oob :: TestTree
taproot_single_oob =
H.testCase "rejects SIGHASH_SINGLE with idx >= n_outputs" $
let txin2 = TxIn
- { txin_prevout = OutPoint (TxId (BS.replicate 32 0xcd)) 0
+ { txin_prevout = OutPoint (tid 0xcd) 0
, txin_script_sig = BS.empty
, txin_sequence = 0xffffffff
+ , txin_witness = Witness []
}
tx2 = tinyTaprootTx
{ tx_inputs = (NE.head (tx_inputs tinyTaprootTx)) :| [txin2] }
@@ -1414,7 +1432,8 @@ rb_script_path_all = H.testCase
Just tx ->
case sighash_taproot_scriptpath tx 0 [amount] [spk] Nothing
leaf 0xffffffff 0x01 of
- Nothing -> H.assertFailure "sighash_taproot_scriptpath returned Nothing"
+ Nothing ->
+ H.assertFailure "sighash_taproot_scriptpath returned Nothing"
Just res -> H.assertEqual "sighash mismatch" expected res
-- taproot properties --------------------------------------------------------