11package tappsbt
22
33import (
4+ "bytes"
45 "encoding/hex"
6+ "os"
7+ "path/filepath"
8+ "strings"
59 "testing"
610
711 "github.com/btcsuite/btcd/btcec/v2"
812 "github.com/btcsuite/btcd/btcutil"
913 "github.com/btcsuite/btcd/btcutil/psbt"
1014 "github.com/btcsuite/btcd/txscript"
15+ "github.com/btcsuite/btcd/wire"
1116 "github.com/lightninglabs/taproot-assets/address"
1217 "github.com/lightninglabs/taproot-assets/asset"
1318 "github.com/lightninglabs/taproot-assets/commitment"
1419 "github.com/lightninglabs/taproot-assets/internal/test"
20+ "github.com/lightninglabs/taproot-assets/proof"
1521 "github.com/lightningnetwork/lnd/keychain"
1622 "github.com/stretchr/testify/require"
1723)
1824
25+ const (
26+ // testDataFileName is the name of the directory with the test data.
27+ testDataFileName = "testdata"
28+ )
29+
1930var (
2031 testParams = & address .MainNetTap
32+
33+ // Block 100002 with 9 transactions on bitcoin mainnet.
34+ oddTxBlockHexFileName = filepath .Join (
35+ testDataFileName , "odd-block.hex" ,
36+ )
2137)
2238
2339// RandPacket generates a random virtual packet for testing purposes.
@@ -68,6 +84,22 @@ func RandPacket(t testing.TB) *VPacket {
6884 txscript .NewTapBranch (leaf1 , leaf1 ),
6985 )
7086
87+ oddTxBlockHex , err := os .ReadFile (oddTxBlockHexFileName )
88+ require .NoError (t , err )
89+
90+ oddTxBlockBytes , err := hex .DecodeString (
91+ strings .Trim (string (oddTxBlockHex ), "\n " ),
92+ )
93+ require .NoError (t , err )
94+
95+ var oddTxBlock wire.MsgBlock
96+ err = oddTxBlock .Deserialize (bytes .NewReader (oddTxBlockBytes ))
97+ require .NoError (t , err )
98+
99+ inputProof := proof .RandProof (
100+ t , testAsset .Genesis , inputScriptKey .PubKey , oddTxBlock , 1 , 0 ,
101+ )
102+
71103 vPacket := & VPacket {
72104 Inputs : []* VInput {{
73105 PrevID : asset.PrevID {
@@ -85,12 +117,15 @@ func RandPacket(t testing.TB) *VPacket {
85117 Bip32Derivation : bip32Derivations ,
86118 TrBip32Derivation : trBip32Derivations ,
87119 },
120+ Proof : & inputProof ,
88121 }, {
89122 // Empty input.
90123 }},
91124 Outputs : []* VOutput {{
92- Amount : 123 ,
93- AssetVersion : asset .Version (test .RandIntn (2 )),
125+ Amount : 123 ,
126+ AssetVersion : asset .Version (
127+ test .RandIntn (2 ),
128+ ),
94129 Type : TypeSplitRoot ,
95130 Interactive : true ,
96131 AnchorOutputIndex : 0 ,
@@ -102,8 +137,10 @@ func RandPacket(t testing.TB) *VPacket {
102137 SplitAsset : testOutputAsset ,
103138 AnchorOutputTapscriptSibling : testPreimage1 ,
104139 }, {
105- Amount : 345 ,
106- AssetVersion : asset .Version (test .RandIntn (2 )),
140+ Amount : 345 ,
141+ AssetVersion : asset .Version (
142+ test .RandIntn (2 ),
143+ ),
107144 Type : TypeSplitRoot ,
108145 Interactive : false ,
109146 AnchorOutputIndex : 1 ,
@@ -116,7 +153,7 @@ func RandPacket(t testing.TB) *VPacket {
116153 }},
117154 ChainParams : testParams ,
118155 }
119- vPacket .SetInputAsset (0 , testAsset , [] byte ( "this is a proof" ) )
156+ vPacket .SetInputAsset (0 , testAsset )
120157
121158 return vPacket
122159}
@@ -207,7 +244,6 @@ func NewTestFromVInput(t testing.TB, i *VInput) *TestVInput {
207244 TrMerkleRoot : hex .EncodeToString (i .TaprootMerkleRoot ),
208245 PrevID : asset .NewTestFromPrevID (& i .PrevID ),
209246 Anchor : NewTestFromAnchor (& i .Anchor ),
210- Proof : hex .EncodeToString (i .proof ),
211247 }
212248
213249 for idx := range i .Bip32Derivation {
@@ -230,6 +266,10 @@ func NewTestFromVInput(t testing.TB, i *VInput) *TestVInput {
230266 ti .Asset = asset .NewTestFromAsset (t , i .asset )
231267 }
232268
269+ if i .Proof != nil {
270+ ti .Proof = proof .NewTestFromProof (t , i .Proof )
271+ }
272+
233273 return ti
234274}
235275
@@ -241,7 +281,7 @@ type TestVInput struct {
241281 PrevID * asset.TestPrevID `json:"prev_id"`
242282 Anchor * TestAnchor `json:"anchor"`
243283 Asset * asset.TestAsset `json:"asset"`
244- Proof string `json:"proof"`
284+ Proof * proof. TestProof `json:"proof"`
245285}
246286
247287func (ti * TestVInput ) ToVInput (t testing.TB ) * VInput {
@@ -254,7 +294,6 @@ func (ti *TestVInput) ToVInput(t testing.TB) *VInput {
254294 },
255295 PrevID : * ti .PrevID .ToPrevID (t ),
256296 Anchor : * ti .Anchor .ToAnchor (t ),
257- proof : test .ParseHex (t , ti .Proof ),
258297 }
259298
260299 for idx := range ti .Bip32Derivation {
@@ -281,6 +320,10 @@ func (ti *TestVInput) ToVInput(t testing.TB) *VInput {
281320 require .NoError (t , err )
282321 }
283322
323+ if ti .Proof != nil {
324+ vi .Proof = ti .Proof .ToProof (t )
325+ }
326+
284327 return vi
285328}
286329
0 commit comments