-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commitment+proof: add CreateTapscriptProof helper function
When creating a Tapscript proof (for a non-asset commitment output), we need to arrange the leaf/branch preimages slightly differently than we would for an actual sibling.
- Loading branch information
Showing
3 changed files
with
216 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package proof | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/btcsuite/btcd/btcec/v2/schnorr" | ||
"github.com/btcsuite/btcd/txscript" | ||
"github.com/lightninglabs/taproot-assets/fn" | ||
"github.com/lightninglabs/taproot-assets/internal/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestCreateTapscriptProof tests the creation of a TapscriptProof from a list | ||
// of leaves. | ||
func TestCreateTapscriptProof(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
leaves []txscript.TapLeaf | ||
}{ | ||
{ | ||
name: "empty tree", | ||
leaves: nil, | ||
}, | ||
{ | ||
name: "single leaf", | ||
leaves: []txscript.TapLeaf{ | ||
test.RandTapLeaf(nil), | ||
}, | ||
}, | ||
{ | ||
name: "two leaves", | ||
leaves: []txscript.TapLeaf{ | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
}, | ||
}, | ||
{ | ||
name: "three leaves", | ||
leaves: []txscript.TapLeaf{ | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
}, | ||
}, | ||
{ | ||
name: "four leaves", | ||
leaves: []txscript.TapLeaf{ | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
}, | ||
}, | ||
{ | ||
name: "more than four leaves", | ||
leaves: []txscript.TapLeaf{ | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
test.RandTapLeaf(nil), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tsProof, err := CreateTapscriptProof(tc.leaves) | ||
require.NoError(t, err) | ||
|
||
internalKey := test.RandPubKey(t) | ||
|
||
var merkleRoot []byte | ||
if len(tc.leaves) == 0 { | ||
merkleRoot = []byte{} | ||
} else { | ||
tree := txscript.AssembleTaprootScriptTree( | ||
tc.leaves..., | ||
) | ||
merkleRoot = fn.ByteSlice( | ||
tree.RootNode.TapHash(), | ||
) | ||
} | ||
|
||
expectedKey := txscript.ComputeTaprootOutputKey( | ||
internalKey, merkleRoot, | ||
) | ||
expectedKey, _ = schnorr.ParsePubKey( | ||
schnorr.SerializePubKey(expectedKey), | ||
) | ||
|
||
proofKey, err := tsProof.DeriveTaprootKeys(internalKey) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, expectedKey, proofKey) | ||
}) | ||
} | ||
} |