Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tappsbt: add AltLeaf support to vPacket #1180

Merged
merged 11 commits into from
Nov 14, 2024
4 changes: 2 additions & 2 deletions address/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func RandAddr(t testing.TB, params *ChainParams,
proofCourierAddr url.URL) (*AddrWithKeyInfo,
*asset.Genesis, *asset.GroupKey) {

scriptKeyPriv := test.RandPrivKey(t)
scriptKeyPriv := test.RandPrivKey()
scriptKey := asset.NewScriptKeyBip86(keychain.KeyDescriptor{
PubKey: scriptKeyPriv.PubKey(),
KeyLocator: keychain.KeyLocator{
Expand All @@ -42,7 +42,7 @@ func RandAddr(t testing.TB, params *ChainParams,
},
})

internalKey := test.RandPrivKey(t)
internalKey := test.RandPrivKey()

genesis := asset.RandGenesis(t, asset.Type(test.RandInt31n(2)))
amount := test.RandInt[uint64]()
Expand Down
2 changes: 1 addition & 1 deletion asset/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ func TestAssetGroupKey(t *testing.T) {
func TestDeriveGroupKey(t *testing.T) {
t.Parallel()

groupPriv := test.RandPrivKey(t)
groupPriv := test.RandPrivKey()
groupPub := groupPriv.PubKey()
groupKeyDesc := test.PubToKeyDesc(groupPub)
genSigner := NewMockGenesisSigner(groupPriv)
Expand Down
24 changes: 16 additions & 8 deletions asset/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/tlv"
"github.com/stretchr/testify/require"
"pgregory.net/rapid"
)

// RandGenesis creates a random genesis for testing.
Expand All @@ -38,17 +39,24 @@ func RandGenesis(t testing.TB, assetType Type) Genesis {
}

// RandGroupKey creates a random group key for testing.
func RandGroupKey(t testing.TB, genesis Genesis, newAsset *Asset) *GroupKey {
groupKey, _ := RandGroupKeyWithSigner(t, genesis, newAsset)
func RandGroupKey(t rapid.TB, genesis Genesis, newAsset *Asset) *GroupKey {
groupKey, _ := RandGroupKeyWithSigner(t, nil, genesis, newAsset)
return groupKey
}

// RandGroupKeyWithSigner creates a random group key for testing, and provides
// the signer for reissuing assets into the same group.
func RandGroupKeyWithSigner(t testing.TB, genesis Genesis,
newAsset *Asset) (*GroupKey, []byte) {
func RandGroupKeyWithSigner(t rapid.TB, privKey *btcec.PrivateKey,
genesis Genesis, newAsset *Asset) (*GroupKey, []byte) {

privateKey := test.RandPrivKey(t)
var privateKey *btcec.PrivateKey
switch {
case privKey != nil:
privateKey = privKey

default:
privateKey = test.RandPrivKey()
}

genSigner := NewMockGenesisSigner(privateKey)
genBuilder := MockGroupTxBuilder{}
Expand Down Expand Up @@ -352,7 +360,7 @@ func AssetCustomGroupKey(t *testing.T, useHashLock, BIP86, keySpend,
scriptKey := RandScriptKey(t)
protoAsset := RandAssetWithValues(t, gen, nil, scriptKey)

groupPrivKey := test.RandPrivKey(t)
groupPrivKey := test.RandPrivKey()
groupInternalKey := groupPrivKey.PubKey()
genSigner := NewMockGenesisSigner(groupPrivKey)
genBuilder := MockGroupTxBuilder{}
Expand Down Expand Up @@ -430,12 +438,12 @@ func AssetCustomGroupKey(t *testing.T, useHashLock, BIP86, keySpend,

// RandScriptKey creates a random script key for testing.
func RandScriptKey(t testing.TB) ScriptKey {
return NewScriptKey(test.RandPrivKey(t).PubKey())
return NewScriptKey(test.RandPrivKey().PubKey())
}

// RandSerializedKey creates a random serialized key for testing.
func RandSerializedKey(t testing.TB) SerializedKey {
return ToSerialized(test.RandPrivKey(t).PubKey())
return ToSerialized(test.RandPrivKey().PubKey())
}

// RandID creates a random asset ID.
Expand Down
8 changes: 4 additions & 4 deletions commitment/commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func randAssetDetails(t *testing.T, assetType asset.Type) *AssetDetails {
Version: assetVersion,
Type: assetType,
ScriptKey: keychain.KeyDescriptor{
PubKey: test.RandPrivKey(t).PubKey(),
PubKey: test.RandPrivKey().PubKey(),
},
Amount: &amount,
LockTime: rand.Uint64(),
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestNewAssetCommitment(t *testing.T) {
)
group1Anchor := randAsset(t, genesis1, nil)
groupKey1, group1PrivBytes := asset.RandGroupKeyWithSigner(
t, genesis1, group1Anchor,
t, nil, genesis1, group1Anchor,
)
group1Anchor = asset.NewAssetNoErr(
t, genesis1, group1Anchor.Amount, group1Anchor.LockTime,
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestMintTapCommitment(t *testing.T) {
genesisNormal := asset.RandGenesis(t, asset.Normal)
genesisCollectible := asset.RandGenesis(t, asset.Collectible)
pubKey := keychain.KeyDescriptor{
PubKey: test.RandPrivKey(t).PubKey(),
PubKey: test.RandPrivKey().PubKey(),
}

testCases := []struct {
Expand Down Expand Up @@ -1008,7 +1008,7 @@ func TestUpdateAssetCommitment(t *testing.T) {
genesis1collect.Type = asset.Collectible
group1Anchor := randAsset(t, genesis1, nil)
groupKey1, group1PrivBytes := asset.RandGroupKeyWithSigner(
t, genesis1, group1Anchor,
t, nil, genesis1, group1Anchor,
)
group1Anchor = asset.NewAssetNoErr(
t, genesis1, group1Anchor.Amount, group1Anchor.LockTime,
Expand Down
2 changes: 1 addition & 1 deletion commitment/taproot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestTapscriptPreimage(t *testing.T) {

// Create a script tree that we'll use for our tapscript sibling test
// cases.
scriptInternalKey := test.RandPrivKey(t).PubKey()
scriptInternalKey := test.RandPrivKey().PubKey()
leaf1 := test.ScriptHashLock(t, []byte("foobar"))
leaf1Hash := leaf1.TapHash()
leaf2 := test.ScriptSchnorrSig(t, scriptInternalKey)
Expand Down
4 changes: 2 additions & 2 deletions internal/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func RandOp(t testing.TB) wire.OutPoint {
return op
}

func RandPrivKey(_ testing.TB) *btcec.PrivateKey {
jharveyb marked this conversation as resolved.
Show resolved Hide resolved
func RandPrivKey() *btcec.PrivateKey {
priv, _ := btcec.PrivKeyFromBytes(RandBytes(32))
return priv
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func SchnorrKeysEqual(t testing.TB, a, b *btcec.PublicKey) bool {
}

func RandPubKey(t testing.TB) *btcec.PublicKey {
return SchnorrPubKey(t, RandPrivKey(t))
return SchnorrPubKey(t, RandPrivKey())
}

func RandCommitmentKeyRing(t *testing.T) lnwallet.CommitmentKeyRing {
Expand Down
2 changes: 1 addition & 1 deletion itest/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func testMintAssetsWithTapscriptSibling(t *harnessTest) {
defer cancel()

// Build the tapscript tree.
sigLockPrivKey := test.RandPrivKey(t.t)
sigLockPrivKey := test.RandPrivKey()
hashLockPreimage := []byte("foobar")
hashLockLeaf := test.ScriptHashLock(t.t, hashLockPreimage)
sigLeaf := test.ScriptSchnorrSig(t.t, sigLockPrivKey.PubKey())
Expand Down
10 changes: 5 additions & 5 deletions proof/append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func runAppendTransitionTest(t *testing.T, assetType asset.Type, amt uint64,
require.NoError(t, err)

// Transfer the asset to a new owner.
recipientPrivKey := test.RandPrivKey(t)
recipientPrivKey := test.RandPrivKey()
newAsset := *genesisProof.Asset.Copy()
newAsset.ScriptKey = asset.NewScriptKeyBip86(
test.PubToKeyDesc(recipientPrivKey.PubKey()),
Expand Down Expand Up @@ -156,7 +156,7 @@ func runAppendTransitionTest(t *testing.T, assetType asset.Type, amt uint64,
// Add a P2TR change output to test the exclusion proof.
var changeInternalKey *btcec.PublicKey
if withBip86Change {
changeInternalKey = test.RandPrivKey(t).PubKey()
changeInternalKey = test.RandPrivKey().PubKey()
changeTaprootKey := txscript.ComputeTaprootKeyNoScript(
changeInternalKey,
)
Expand Down Expand Up @@ -221,9 +221,9 @@ func runAppendTransitionTest(t *testing.T, assetType asset.Type, amt uint64,
}

// If we want to test splitting, we do that now, as a second transfer.
split1PrivKey := test.RandPrivKey(t)
split2PrivKey := test.RandPrivKey(t)
split3PrivKey := test.RandPrivKey(t)
split1PrivKey := test.RandPrivKey()
split2PrivKey := test.RandPrivKey()
split3PrivKey := test.RandPrivKey()
transitionOutpoint := wire.OutPoint{
Hash: transitionProof.AnchorTx.TxHash(),
Index: transitionProof.InclusionProof.OutputIndex,
Expand Down
4 changes: 2 additions & 2 deletions proof/mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNewMintingBlobs(t *testing.T) {

// First, we'll create a fake, but legit looking set of minting params
// to generate a proof with.
genesisPrivKey := test.RandPrivKey(t)
genesisPrivKey := test.RandPrivKey()
genesisScriptKey := test.PubToKeyDesc(genesisPrivKey.PubKey())

// We'll modify the returned genesis to instead commit to some actual
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestNewMintingBlobs(t *testing.T) {
)
taprootScript := test.ComputeTaprootScript(t, taprootKey)

changeInternalKey := test.RandPrivKey(t).PubKey()
changeInternalKey := test.RandPrivKey().PubKey()
changeTaprootKey := txscript.ComputeTaprootKeyNoScript(
changeInternalKey,
)
Expand Down
4 changes: 2 additions & 2 deletions proof/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func genRandomGenesisWithProof(t testing.TB, assetType asset.Type,

t.Helper()

genesisPrivKey := test.RandPrivKey(t)
genesisPrivKey := test.RandPrivKey()
genesisPubKey := test.PubToKeyDesc(genesisPrivKey.PubKey())

// If we have a specified meta reveal, then we'll replace the meta hash
Expand Down Expand Up @@ -371,7 +371,7 @@ func TestGenesisProofVerification(t *testing.T) {

// Create a script tree that we'll use for our tapscript sibling test
// cases.
scriptInternalKey := test.RandPrivKey(t).PubKey()
scriptInternalKey := test.RandPrivKey().PubKey()
leaf1 := test.ScriptHashLock(t, []byte("foobar"))
leaf2 := test.ScriptSchnorrSig(t, scriptInternalKey)
testLeafPreimage, err := commitment.NewPreimageFromLeaf(leaf1)
Expand Down
2 changes: 1 addition & 1 deletion rfqmsg/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestRequestMsgDataEncodeDecode(t *testing.T) {
assetId := asset.ID(randomAssetIdBytes)

// Create a random asset group key.
assetGroupKey := test.RandPrivKey(t).PubKey()
assetGroupKey := test.RandPrivKey().PubKey()

// Create a zero asset ID. An asset ID of all zeros indicates BTC in the
// context of the request message.
Expand Down
4 changes: 2 additions & 2 deletions tapdb/assets_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func defaultAssetGenOpts(t *testing.T) *assetGenOptions {
return &assetGenOptions{
version: asset.Version(rand.Int31n(2)),
assetGen: gen,
groupKeyPriv: test.RandPrivKey(t),
groupKeyPriv: test.RandPrivKey(),
amt: uint64(test.RandInt[uint32]()),
genesisPoint: test.RandOp(t),
scriptKey: asset.NewScriptKeyBip86(keychain.KeyDescriptor{
Expand Down Expand Up @@ -520,7 +520,7 @@ func newAssetGenerator(t *testing.T,

groupKeys := make([]*btcec.PrivateKey, numGroupKeys)
for i := 0; i < numGroupKeys; i++ {
groupKeys[i] = test.RandPrivKey(t)
groupKeys[i] = test.RandPrivKey()
}

return &assetGenerator{
Expand Down
6 changes: 3 additions & 3 deletions tapsend/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2020,11 +2020,11 @@ func TestPayToAddrScript(t *testing.T) {
sendAmt = 2
)
gen := asset.RandGenesis(t, asset.Normal)
ownerDescriptor := test.PubToKeyDesc(test.RandPrivKey(t).PubKey())
ownerDescriptor := test.PubToKeyDesc(test.RandPrivKey().PubKey())

internalKey := test.RandPrivKey(t).PubKey()
internalKey := test.RandPrivKey().PubKey()
recipientScriptKey := asset.NewScriptKeyBip86(test.PubToKeyDesc(
test.RandPrivKey(t).PubKey(),
test.RandPrivKey().PubKey(),
))

// Create an asset and derive a commitment for sending 2 of the 5 asset
Expand Down
18 changes: 9 additions & 9 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func invalidGenesisStateTransitionWitness(assetType asset.Type,
func collectibleStateTransition(t *testing.T) (*asset.Asset,
commitment.SplitSet, commitment.InputSet, uint32) {

privKey := test.RandPrivKey(t)
privKey := test.RandPrivKey()
scriptKey := txscript.ComputeTaprootKeyNoScript(privKey.PubKey())

genesisOutPoint := wire.OutPoint{}
Expand All @@ -206,7 +206,7 @@ func collectibleStateTransition(t *testing.T) (*asset.Asset,
ScriptKey: asset.ToSerialized(genesisAsset.ScriptKey.PubKey),
}
newAsset := genesisAsset.Copy()
newAsset.ScriptKey = asset.NewScriptKey(test.RandPrivKey(t).PubKey())
newAsset.ScriptKey = asset.NewScriptKey(test.RandPrivKey().PubKey())
newAsset.PrevWitnesses = []asset.Witness{{
PrevID: prevID,
TxWitness: nil,
Expand Down Expand Up @@ -245,12 +245,12 @@ func normalStateTransition(t *testing.T, currentHeight uint32, sequence,
lockTime uint64, addCsvScript, addCltvScript bool) (*asset.Asset,
commitment.SplitSet, commitment.InputSet, uint32) {

privKey1 := test.RandPrivKey(t)
privKey1 := test.RandPrivKey()
scriptKey1 := txscript.ComputeTaprootKeyNoScript(
privKey1.PubKey(),
)

privKey2 := test.RandPrivKey(t)
privKey2 := test.RandPrivKey()
builder := txscript.NewScriptBuilder().
AddData(schnorr.SerializePubKey(privKey2.PubKey())).
AddOp(txscript.OP_CHECKSIG)
Expand Down Expand Up @@ -357,7 +357,7 @@ func customScriptStateTransition(t *testing.T, currentHeight uint32, sequence,
lockTime uint64, tapLeaf txscript.TapLeaf) (*asset.Asset,
commitment.SplitSet, commitment.InputSet, uint32) {

privKey := test.RandPrivKey(t)
privKey := test.RandPrivKey()
tapTree := txscript.AssembleTaprootScriptTree(tapLeaf)
tapTreeRoot := tapTree.RootNode.TapHash()
scriptKey := txscript.ComputeTaprootOutputKey(
Expand Down Expand Up @@ -414,7 +414,7 @@ func customScriptStateTransition(t *testing.T, currentHeight uint32, sequence,
func splitStateTransition(t *testing.T) (*asset.Asset, commitment.SplitSet,
commitment.InputSet, uint32) {

privKey := test.RandPrivKey(t)
privKey := test.RandPrivKey()
scriptKey := txscript.ComputeTaprootKeyNoScript(privKey.PubKey())

genesisOutPoint := wire.OutPoint{}
Expand Down Expand Up @@ -470,7 +470,7 @@ func splitFullValueStateTransition(validRootLocator,
return func(t *testing.T) (*asset.Asset, commitment.SplitSet,
commitment.InputSet, uint32) {

privKey := test.RandPrivKey(t)
privKey := test.RandPrivKey()
scriptKey := txscript.ComputeTaprootKeyNoScript(privKey.PubKey())

genesisOutPoint := wire.OutPoint{}
Expand Down Expand Up @@ -530,7 +530,7 @@ func splitCollectibleStateTransition(validRoot bool) stateTransitionFunc {
return func(t *testing.T) (*asset.Asset, commitment.SplitSet,
commitment.InputSet, uint32) {

privKey := test.RandPrivKey(t)
privKey := test.RandPrivKey()
scriptKey := txscript.ComputeTaprootKeyNoScript(privKey.PubKey())

genesisOutPoint := wire.OutPoint{}
Expand Down Expand Up @@ -595,7 +595,7 @@ func groupAnchorStateTransition(useHashLock, BIP86, keySpend, valid bool,
func scriptTreeSpendStateTransition(t *testing.T, useHashLock,
valid bool, sigHashType txscript.SigHashType) stateTransitionFunc {

scriptPrivKey := test.RandPrivKey(t)
scriptPrivKey := test.RandPrivKey()
usedLeaf, testTapScript, _, _, scriptWitness := test.BuildTapscriptTree(
t, useHashLock, valid, scriptPrivKey.PubKey(),
)
Expand Down