3737 )
3838)
3939
40+ // RandAltLeaf generates a random Asset that is a valid AltLeaf.
41+ func RandAltLeaf (t testing.TB ) * asset.Asset {
42+ randWitness := []asset.Witness {
43+ {TxWitness : test .RandTxWitnesses (t )},
44+ }
45+ randKey := asset .RandScriptKey (t )
46+ randVersion := asset .ScriptVersion (test .RandInt [uint16 ]())
47+ randLeaf , err := asset .NewAltLeaf (randKey , randVersion , randWitness )
48+ require .NoError (t , err )
49+
50+ require .NoError (t , randLeaf .ValidateAltLeaf ())
51+
52+ return randLeaf
53+ }
54+
55+ // RandAltLeaves generates a set of random number of random alt leaves.
56+ func RandAltLeaves (t testing.TB ) []AltLeafAsset {
57+ // Limit the number of leaves to keep the test vectors small.
58+ maxLeaves := int32 (4 )
59+ numLeaves := test .RandInt31n (maxLeaves )
60+ if numLeaves == 0 {
61+ return nil
62+ }
63+
64+ altLeaves := make ([]AltLeafAsset , 0 , numLeaves )
65+ for range numLeaves {
66+ altLeaves = append (altLeaves , AltLeafAsset (RandAltLeaf (t )))
67+ }
68+
69+ return altLeaves
70+ }
4071
4172func RandAssetForPacket (t testing.TB , assetType asset.Type ,
4273 desc keychain.KeyDescriptor ) * asset.Asset {
@@ -54,7 +85,7 @@ func RandAssetForPacket(t testing.TB, assetType asset.Type,
5485}
5586
5687// RandPacket generates a random virtual packet for testing purposes.
57- func RandPacket (t testing.TB , setVersion bool ) * VPacket {
88+ func RandPacket (t testing.TB , setVersion , altLeaves bool ) * VPacket {
5889 testPubKey := test .RandPubKey (t )
5990 op := test .RandOp (t )
6091 keyDesc := keychain.KeyDescriptor {
@@ -117,61 +148,72 @@ func RandPacket(t testing.TB, setVersion bool) *VPacket {
117148 courierAddress , err := url .Parse ("https://example.com" )
118149 require .NoError (t , err )
119150
151+ randVInput := VInput {
152+ PrevID : asset.PrevID {
153+ OutPoint : op ,
154+ ID : asset .RandID (t ),
155+ ScriptKey : asset .RandSerializedKey (t ),
156+ },
157+ Anchor : Anchor {
158+ Value : 777 ,
159+ PkScript : []byte ("anchor pkscript" ),
160+ SigHashType : txscript .SigHashSingle ,
161+ InternalKey : testPubKey ,
162+ MerkleRoot : []byte ("merkle root" ),
163+ TapscriptSibling : []byte ("sibling" ),
164+ Bip32Derivation : bip32Derivations ,
165+ TrBip32Derivation : trBip32Derivations ,
166+ },
167+ Proof : & inputProof ,
168+ }
169+
170+ randVOutput1 := VOutput {
171+ Amount : 123 ,
172+ AssetVersion : asset .Version (
173+ test .RandIntn (2 ),
174+ ),
175+ Type : TypeSplitRoot ,
176+ Interactive : true ,
177+ AnchorOutputIndex : 0 ,
178+ AnchorOutputInternalKey : testPubKey ,
179+ AnchorOutputBip32Derivation : bip32Derivations ,
180+ AnchorOutputTaprootBip32Derivation : trBip32Derivations ,
181+ Asset : testOutputAsset ,
182+ ScriptKey : testOutputAsset .ScriptKey ,
183+ SplitAsset : testOutputAsset ,
184+ AnchorOutputTapscriptSibling : testPreimage1 ,
185+ ProofDeliveryAddress : courierAddress ,
186+ ProofSuffix : & inputProof ,
187+ RelativeLockTime : 345 ,
188+ LockTime : 456 ,
189+ }
190+
191+ randVOutput2 := VOutput {
192+ Amount : 345 ,
193+ AssetVersion : asset .Version (
194+ test .RandIntn (2 ),
195+ ),
196+ Type : TypeSplitRoot ,
197+ Interactive : false ,
198+ AnchorOutputIndex : 1 ,
199+ AnchorOutputInternalKey : testPubKey ,
200+ AnchorOutputBip32Derivation : bip32Derivations ,
201+ AnchorOutputTaprootBip32Derivation : trBip32Derivations ,
202+ Asset : testOutputAsset ,
203+ ScriptKey : testOutputAsset .ScriptKey ,
204+ AnchorOutputTapscriptSibling : & testPreimage2 ,
205+ }
206+
207+ if altLeaves {
208+ randVInput .AltLeaves = RandAltLeaves (t )
209+ randVOutput1 .AltLeaves = RandAltLeaves (t )
210+ randVOutput2 .AltLeaves = RandAltLeaves (t )
211+ }
212+
120213 vPacket := & VPacket {
121- Inputs : []* VInput {{
122- PrevID : asset.PrevID {
123- OutPoint : op ,
124- ID : asset .RandID (t ),
125- ScriptKey : asset .RandSerializedKey (t ),
126- },
127- Anchor : Anchor {
128- Value : 777 ,
129- PkScript : []byte ("anchor pkscript" ),
130- SigHashType : txscript .SigHashSingle ,
131- InternalKey : testPubKey ,
132- MerkleRoot : []byte ("merkle root" ),
133- TapscriptSibling : []byte ("sibling" ),
134- Bip32Derivation : bip32Derivations ,
135- TrBip32Derivation : trBip32Derivations ,
136- },
137- Proof : & inputProof ,
138- }, {
139- // Empty input.
140- }},
141- Outputs : []* VOutput {{
142- Amount : 123 ,
143- AssetVersion : asset .Version (
144- test .RandIntn (2 ),
145- ),
146- Type : TypeSplitRoot ,
147- Interactive : true ,
148- AnchorOutputIndex : 0 ,
149- AnchorOutputInternalKey : testPubKey ,
150- AnchorOutputBip32Derivation : bip32Derivations ,
151- AnchorOutputTaprootBip32Derivation : trBip32Derivations ,
152- Asset : testOutputAsset ,
153- ScriptKey : testOutputAsset .ScriptKey ,
154- SplitAsset : testOutputAsset ,
155- AnchorOutputTapscriptSibling : testPreimage1 ,
156- ProofDeliveryAddress : courierAddress ,
157- ProofSuffix : & inputProof ,
158- RelativeLockTime : 345 ,
159- LockTime : 456 ,
160- }, {
161- Amount : 345 ,
162- AssetVersion : asset .Version (
163- test .RandIntn (2 ),
164- ),
165- Type : TypeSplitRoot ,
166- Interactive : false ,
167- AnchorOutputIndex : 1 ,
168- AnchorOutputInternalKey : testPubKey ,
169- AnchorOutputBip32Derivation : bip32Derivations ,
170- AnchorOutputTaprootBip32Derivation : trBip32Derivations ,
171- Asset : testOutputAsset ,
172- ScriptKey : testOutputAsset .ScriptKey ,
173- AnchorOutputTapscriptSibling : & testPreimage2 ,
174- }},
214+ // Empty input.
215+ Inputs : []* VInput {& randVInput , {}},
216+ Outputs : []* VOutput {& randVOutput1 , & randVOutput2 },
175217 ChainParams : testParams ,
176218 }
177219 vPacket .SetInputAsset (0 , testAsset )
0 commit comments