Skip to content

Commit

Permalink
tappsbt: add test for decoding packet from file
Browse files Browse the repository at this point in the history
This test is helpful for inspecting/debugging a vPSBT packet from a unit
or integration test.
  • Loading branch information
guggero committed May 20, 2024
1 parent 5501299 commit 4ff1b62
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tappsbt/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ package tappsbt
import (
"bytes"
"encoding/base64"
"encoding/hex"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/lightninglabs/taproot-assets/address"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/commitment"
"github.com/lightninglabs/taproot-assets/internal/test"
"github.com/lightninglabs/taproot-assets/mssmt"
"github.com/stretchr/testify/require"
)

var (
generatedTestVectorName = "psbt_encoding_generated.json"

packetHexFileName = filepath.Join(testDataFileName, "packet.hex")

allTestVectorFiles = []string{
generatedTestVectorName,
"psbt_encoding_error_cases.json",
Expand Down Expand Up @@ -220,3 +227,40 @@ func runBIPTestVector(t *testing.T, testVectors *TestVectors) {
})
}
}

// TestFileDecoding ensures that we can decode a vPSBT packet from a hex encoded
// file. This is useful for quickly inspecting the contents of a packet while
// debugging.
func TestFileDecoding(t *testing.T) {
packetHex, err := os.ReadFile(packetHexFileName)
require.NoError(t, err)

packetBytes, err := hex.DecodeString(
strings.Trim(string(packetHex), "\n"),
)
require.NoError(t, err)

packet, err := NewFromRawBytes(bytes.NewReader(packetBytes), false)
require.NoError(t, err)

rootAsset := packet.Outputs[1].Asset
splitAsset := packet.Outputs[0].Asset
splitWitness := splitAsset.PrevWitnesses[0]

locator := &commitment.SplitLocator{
OutputIndex: splitAsset.OutputIndex,
AssetID: splitAsset.Genesis.ID(),
ScriptKey: asset.ToSerialized(splitAsset.ScriptKey.PubKey),
Amount: splitAsset.Amount,
}
splitNoWitness := splitAsset.Copy()
splitNoWitness.PrevWitnesses[0].SplitCommitment = nil
splitLeaf, err := splitNoWitness.Leaf()
require.NoError(t, err)

verify := mssmt.VerifyMerkleProof(
locator.Hash(), splitLeaf, &splitWitness.SplitCommitment.Proof,
rootAsset.SplitCommitmentRoot,
)
require.True(t, verify)
}
1 change: 1 addition & 0 deletions tappsbt/testdata/packet.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
70736274ff0100890200000001000000000000000000000000000000000000000000000000000000000000000000000000000000000002e80300000000000022512050aaeb166f4234650d84a2d8a130987aeaf6950206e0905401ee74ff3f8d18e6586b40010000000022512039aaa9222ece28ea91bfca35e60696faf5ba1f337c83bebb3b321aafd86617690000000001700101017105746170727401720100002206030fbec72ce7feaa3b508cd4c77cb69d583c49b6cabb00e4d44542d6119aa677081800000000f903008001000080d4000080000000000000000021160fbec72ce7feaa3b508cd4c77cb69d583c49b6cabb00e4d44542d6119aa67708190000000000f903008001000080d400008000000000000000000117200fbec72ce7feaa3b508cd4c77cb69d583c49b6cabb00e4d44542d6119aa6770801706551018281976f5457f46b03be7b9a8219e7f6be729b70055c18ccb7552a8bae1e00000000a88b7aafdfd8ab188b7af80d5f72ee76fd41a1b3f280641db86e0c9b8fd7efa702d338fdc34b902512b774d2d5fe1654b4f78f5c23d01ac62e367769953a3aa38d01710800000000000003e801722251208b81ed915a5fb2235c2282102f6fa528e67b7385b3d4198069b95c27b8355e450173080000000000000000017421022dffd67a43c1a4b89fd0b626a845a31cea96d9fadcd38c8e7cc609aee2d093ba017520297bcbf4d7de9110c3126aff2b08d0b51253fd2e88e957b2b0b7f9aed716b3ae2276022dffd67a43c1a4b89fd0b626a845a31cea96d9fadcd38c8e7cc609aee2d093ba1800000000f903008001000080d4000080000000000100000021772dffd67a43c1a4b89fd0b626a845a31cea96d9fadcd38c8e7cc609aee2d093ba190000000000f903008001000080d400008000000000010000000178000179f1000100025070b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000066c6e62757878000000000000000000000000000000000000000000000000000000000000000000000000000401000605fe01406f400b690167016500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e020000102102d338fdc34b902512b774d2d5fe1654b4f78f5c23d01ac62e367769953a3aa38d017afdbc0354415050000400000000022470b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000045000000030a4ea41403036f1dea0e59c7001ab7e8e9f4b156f9ed174f25e47bf1c82f4467e4a01d8586b06be519b112402f4a64810450fa64d58b903f71b1b102c7d70bec5f54e2266ffff7f200200000006f60200000000010170b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000000000000002e8030000000000002251208b81ed915a5fb2235c2282102f6fa528e67b7385b3d4198069b95c27b8355e45ebbcf50500000000225120576263de7b1aefc6dde61860cd30f58a5fc78e956063cdc546c0cdf603b7727f02473044022019474fcbce206610781f6510215d4b3ee79dabcdeecdc09b437df5d7813c4b5f02202c54324b98db998dd527ccf0ccd7c43009c9f5db8b356b60aaefc4cdb0d0283d0121033db20c2e91928c20a23edb62aae1e08eeb512783b1285b968ebe34a733dbe3d100000000082201010dfda6dc511d6486afb7649afc6e35a2c599b4f6f62bf1375b45684ab2b09e000af1000100025070b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000066c6e62757878000000000000000000000000000000000000000000000000000000000000000000000000000401000605fe01406f400b690167016500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e020000102102d338fdc34b902512b774d2d5fe1654b4f78f5c23d01ac62e367769953a3aa38d0c9f0004000000000221022dffd67a43c1a4b89fd0b626a845a31cea96d9fadcd38c8e7cc609aee2d093ba037401490001000220a88b7aafdfd8ab188b7af80d5f72ee76fd41a1b3f280641db86e0c9b8fd7efa704220000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff022700010002220000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0d30012e0004000000010221025ca488479d50a6f31083140db5c51752f2a1721a9a0ace532f5e0f5dcbb08b9305030401011604000000a8175070b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000066c6e6275787800000000000000000000000000000000000000000000000000000000000000000000000000000170010001710101017208000000000000000001732102f6a72eb0bc6a7cadbdbfcdcfb20532d115eb851f3a9ccc269166be1d5cddc0ee22740365b00d8f70a4875d522bbef8d449126a9c914ef027c39372a89d9abd911fa5ff1800000000f903008001000080d4000080000000000c000000227402f6a72eb0bc6a7cadbdbfcdcfb20532d115eb851f3a9ccc269166be1d5cddc0ee1800000000f903008001000080000000800000000000000000217565b00d8f70a4875d522bbef8d449126a9c914ef027c39372a89d9abd911fa5ff190000000000f903008001000080d4000080000000000c0000002175f6a72eb0bc6a7cadbdbfcdcfb20532d115eb851f3a9ccc269166be1d5cddc0ee190000000000f9030080010000800000008000000000000000000176fd6002000101025070b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000066c6e62757878000000000000000000000000000000000000000000000000000000000000000000000000000401000603fd03e80bfd01d801fd01d40165000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005fd01694a0001f6b2e42c5636b2f626a471b3e4857cafeb9f68be3b039531a894dc3636e2e0430000000001406b58ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7ffd011b000100025070b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000066c6e62757878000000000000000000000000000000000000000000000000000000000000000000000000000401000605fe01406b580b690167016551018281976f5457f46b03be7b9a8219e7f6be729b70055c18ccb7552a8bae1e00000000a88b7aafdfd8ab188b7af80d5f72ee76fd41a1b3f280641db86e0c9b8fd7efa702d338fdc34b902512b774d2d5fe1654b4f78f5c23d01ac62e367769953a3aa38d0d2836b85d1ec92b3c2f6749180976ebba4d2f0f7e25a3f38813d52f266efa5b5c1d0000000001406f400e02000010210239aaa9222ece28ea91bfca35e60696faf5ba1f337c83bebb3b321aafd86617690e02000010210250aaeb166f4234650d84a2d8a130987aeaf6950206e0905401ee74ff3f8d18e6017901010022020308cc338d5eb72977e833bcb06f4d64db94edca770a2f0288ab29e37ddf6bb33f1800000000f903008001000080d4000080000000000b00000001052008cc338d5eb72977e833bcb06f4d64db94edca770a2f0288ab29e37ddf6bb33f210708cc338d5eb72977e833bcb06f4d64db94edca770a2f0288ab29e37ddf6bb33f190000000000f903008001000080d4000080000000000b00000001700101017101010172080000000000000001017321035f6e111e838aff37e4ff5b6b44c83da66953e8197f620bb7c313e1dea6644fb02274035f6e111e838aff37e4ff5b6b44c83da66953e8197f620bb7c313e1dea6644fb01800000000f903008001000080d4000080000000000d00000021755f6e111e838aff37e4ff5b6b44c83da66953e8197f620bb7c313e1dea6644fb0190000000000f903008001000080d4000080000000000d0000000176fd1b01000100025070b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000066c6e62757878000000000000000000000000000000000000000000000000000000000000000000000000000401000605fe01406b580b690167016551018281976f5457f46b03be7b9a8219e7f6be729b70055c18ccb7552a8bae1e00000000a88b7aafdfd8ab188b7af80d5f72ee76fd41a1b3f280641db86e0c9b8fd7efa702d338fdc34b902512b774d2d5fe1654b4f78f5c23d01ac62e367769953a3aa38d0d2836b85d1ec92b3c2f6749180976ebba4d2f0f7e25a3f38813d52f266efa5b5c1d0000000001406f400e02000010210239aaa9222ece28ea91bfca35e60696faf5ba1f337c83bebb3b321aafd86617690177fd6202000100025070b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000066c6e62757878000000000000000000000000000000000000000000000000000000000000000000000000000401000605fe01406b580bfd01d801fd01d40165000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005fd01694a000123f7794e970a10b26b6af0b184fd7ead7ed622f5311de5699448371388772a9700000000000003e8ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7ffd011b000100025070b8b9dc9c3c63a3318ba6b902e0632d6c58b3540fa4938a45ced6176a7d09bd00000000066c6e62757878000000000000000000000000000000000000000000000000000000000000000000000000000401000605fe01406b580b690167016551018281976f5457f46b03be7b9a8219e7f6be729b70055c18ccb7552a8bae1e00000000a88b7aafdfd8ab188b7af80d5f72ee76fd41a1b3f280641db86e0c9b8fd7efa702d338fdc34b902512b774d2d5fe1654b4f78f5c23d01ac62e367769953a3aa38d0d2836b85d1ec92b3c2f6749180976ebba4d2f0f7e25a3f38813d52f266efa5b5c1d0000000001406f400e02000010210239aaa9222ece28ea91bfca35e60696faf5ba1f337c83bebb3b321aafd86617690e02000010210239aaa9222ece28ea91bfca35e60696faf5ba1f337c83bebb3b321aafd86617690179010000

0 comments on commit 4ff1b62

Please sign in to comment.