Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion commitment/commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commitment
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"math/rand"
"testing"
Expand Down Expand Up @@ -1483,7 +1484,9 @@ func TestIsTaprootAssetCommitmentScript(t *testing.T) {
t.Parallel()

require.True(t, IsTaprootAssetCommitmentScript(testTapCommitmentScript))
require.False(t, IsTaprootAssetCommitmentScript(TaprootAssetsMarker[:]))

tag := sha256.Sum256(markerV2)
require.False(t, IsTaprootAssetCommitmentScript(tag[:]))
}

// TestAssetCommitmentNoWitness tests that an asset commitment of a v1 asset is
Expand Down
79 changes: 79 additions & 0 deletions commitment/tap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package commitment

import (
"crypto/sha256"
"testing"

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

var (
// markerV2 is the marker tag for Taproot Assets used in V2 commitments.
markerV2 = []byte(taprootAssetsMarkerTag + ":194243")
)

// randTapCommitment generates a random Taproot commitment for the given
// assetVersion.
func randTapCommitment(t *testing.T, commitVersion *TapCommitmentVersion,
assetVersion asset.Version) *TapCommitment {

t.Helper()
randAssetType := test.RandFlip(asset.Normal, asset.Collectible)
randGenesis := asset.RandGenesis(t, randAssetType)
randAsset := randAsset(t, randGenesis, nil)

randAsset.Version = assetVersion

tapCommitment, err := FromAssets(commitVersion, randAsset)
require.NoError(t, err)

return tapCommitment
}

// TestTaprootAssetsMarkerV0 tests if it can find the TaprootAssetsMarker V0 at
// the correct spot according to the legacy MarkerV0 digest with assetVersion 0
// and 1.
func TestTaprootAssetsMarkerV0(t *testing.T) {
t.Parallel()

assetVersions := []asset.Version{asset.V0, asset.V1}
for _, assetVersion := range assetVersions {
// Create a random Taproot commitment, and extract the tapLeaf
// script.
randTapCommitment := randTapCommitment(t, nil, assetVersion)
tapLeaf := randTapCommitment.TapLeaf()
script := tapLeaf.Script

require.Equal(t, byte(assetVersion), script[0])
require.Equal(t, TaprootAssetsMarker[:], script[1:33])
}
}

// TestTaprootAssetsMarkerV1 tests if it can find the TaprootAssetsMarker V2 at
// the correct spot according to the MarkerV1 digest with assetVersion 0 and 1.
func TestTaprootAssetsMarkerV1(t *testing.T) {
t.Parallel()

assetVersions := []asset.Version{asset.V0, asset.V1}
for _, assetVersion := range assetVersions {
// Create a random Taproot commitment, and extract the tapLeaf
// script.
randTapCommitment := randTapCommitment(
t, fn.Ptr(TapCommitmentV2), assetVersion,
)

// Check if MarkerVersion is set to MarkerV2, which should be
// the default.
require.Equal(t, randTapCommitment.Version, TapCommitmentV2)

tapLeaf := randTapCommitment.TapLeaf()
script := tapLeaf.Script

tag := sha256.Sum256(markerV2)
require.Equal(t, tag[:], script[:32])
require.Equal(t, byte(TapCommitmentV2), script[32])
}
}
49 changes: 44 additions & 5 deletions commitment/taproot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commitment

import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"testing"
Expand Down Expand Up @@ -38,14 +39,26 @@ func TestTapscriptPreimage(t *testing.T) {

// Create a random byte slice with the same structure as a Taproot
// Asset commitment root, that can be used in a TapLeaf.
randTapCommitmentRoot := func(version asset.Version) []byte {

randTapCommitmentRoot := func(version asset.Version,
legacyCommitment bool) []byte {

var dummyRootSum [8]byte
binary.BigEndian.PutUint64(
dummyRootSum[:], test.RandInt[uint64](),
)
dummyRootHashParts := [][]byte{
{byte(version)}, TaprootAssetsMarker[:],
fn.ByteSlice(test.RandHash()), dummyRootSum[:],
var dummyRootHashParts [][]byte
if legacyCommitment {
dummyRootHashParts = [][]byte{
{byte(version)}, TaprootAssetsMarker[:],
fn.ByteSlice(test.RandHash()), dummyRootSum[:],
}
} else {
tag := sha256.Sum256(markerV2)
dummyRootHashParts = [][]byte{
tag[:], {byte(version)},
fn.ByteSlice(test.RandHash()), dummyRootSum[:],
}
}
return bytes.Join(dummyRootHashParts, nil)
}
Expand Down Expand Up @@ -115,7 +128,33 @@ func TestTapscriptPreimage(t *testing.T) {
}, {
name: "tap commitment leaf pre-image",
makePreimage: func(t *testing.T) *TapscriptPreimage {
tapCommitmentRoot := randTapCommitmentRoot(asset.V0)
tapCommitmentRoot := randTapCommitmentRoot(
asset.V0, false,
)
var encodedLeaf bytes.Buffer

_ = encodedLeaf.WriteByte(
byte(txscript.BaseLeafVersion),
)
_ = wire.WriteVarBytes(
&encodedLeaf, 0, tapCommitmentRoot,
)

return &TapscriptPreimage{
siblingType: LeafPreimage,
siblingPreimage: encodedLeaf.Bytes(),
}
},
expectedType: LeafPreimage,
expectedName: "LeafPreimage",
expectedEmpty: false,
expectedHashErr: ErrPreimageIsTapCommitment.Error(),
}, {
name: "tap commitment leaf pre-image legacy commitment",
makePreimage: func(t *testing.T) *TapscriptPreimage {
tapCommitmentRoot := randTapCommitmentRoot(
asset.V0, true,
)
var encodedLeaf bytes.Buffer

_ = encodedLeaf.WriteByte(
Expand Down
21 changes: 20 additions & 1 deletion itest/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ type publishAndLogTransferOptions struct {

// label is the label to use for the transfer.
label string

// expectedErr is the expected error when calling PublishAndLogTransfer.
expectedErr string
}

// defaultPublishAndLogTransferOptions returns the default options for
Expand All @@ -525,6 +528,14 @@ func withLabel(label string) PublishAndLogTransferOption {
}
}

// withExpectedErr is an option for PublishAndLogTransfer that sets the
// expected error.
func withExpectedErr(expectedErr string) PublishAndLogTransferOption {
return func(opts *publishAndLogTransferOptions) {
opts.expectedErr = expectedErr
}
}

// PublishAndLogTransfer is a helper function that invokes the
// PublishAndLogTransfer RPC endpoint on the specified tapd node. This endpoint
// performs a pre-anchored transfer.
Expand Down Expand Up @@ -572,7 +583,15 @@ func PublishAndLogTransfer(t *testing.T, tapd commands.RpcClientsBundle,
}

resp, err := tapd.PublishAndLogTransfer(ctxt, request)
require.NoError(t, err)

if options.expectedErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), options.expectedErr,
"unexpected error: %v", err)
return nil
} else {
require.NoError(t, err)
}

return resp
}
Expand Down
Loading
Loading