Skip to content

Commit 303f3da

Browse files
authored
Merge pull request #1350 from lightninglabs/mv-groupkey-unit-tests
Refactor GroupKey unit tests.
2 parents 74f8e48 + 44f43a6 commit 303f3da

File tree

2 files changed

+291
-289
lines changed

2 files changed

+291
-289
lines changed

asset/asset_test.go

Lines changed: 0 additions & 286 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ import (
1111
"github.com/btcsuite/btcd/btcec/v2"
1212
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1313
"github.com/btcsuite/btcd/btcutil/hdkeychain"
14-
"github.com/btcsuite/btcd/chaincfg/chainhash"
1514
"github.com/btcsuite/btcd/txscript"
1615
"github.com/btcsuite/btcd/wire"
1716
"github.com/lightninglabs/taproot-assets/fn"
1817
"github.com/lightninglabs/taproot-assets/internal/test"
1918
"github.com/lightninglabs/taproot-assets/mssmt"
20-
"github.com/lightningnetwork/lnd/input"
21-
"github.com/lightningnetwork/lnd/keychain"
2219
"github.com/lightningnetwork/lnd/tlv"
2320
"github.com/stretchr/testify/require"
2421
"pgregory.net/rapid"
@@ -111,134 +108,6 @@ var (
111108
}
112109
)
113110

114-
// TestGroupKeyIsEqual tests that GroupKey.IsEqual is correct.
115-
func TestGroupKeyIsEqual(t *testing.T) {
116-
t.Parallel()
117-
118-
testKey := &GroupKey{
119-
RawKey: keychain.KeyDescriptor{
120-
// Fill in some non-defaults.
121-
KeyLocator: keychain.KeyLocator{
122-
Family: keychain.KeyFamilyMultiSig,
123-
Index: 1,
124-
},
125-
PubKey: pubKey,
126-
},
127-
GroupPubKey: *pubKey,
128-
Witness: sigWitness,
129-
}
130-
131-
pubKeyCopy := *pubKey
132-
133-
tests := []struct {
134-
a, b *GroupKey
135-
equal bool
136-
}{
137-
{
138-
a: nil,
139-
b: nil,
140-
equal: true,
141-
},
142-
{
143-
a: &GroupKey{},
144-
b: &GroupKey{},
145-
equal: true,
146-
},
147-
{
148-
a: nil,
149-
b: &GroupKey{},
150-
equal: false,
151-
},
152-
{
153-
a: testKey,
154-
b: &GroupKey{
155-
GroupPubKey: *pubKey,
156-
},
157-
equal: false,
158-
},
159-
{
160-
a: testKey,
161-
b: &GroupKey{
162-
GroupPubKey: testKey.GroupPubKey,
163-
Witness: testKey.Witness,
164-
},
165-
equal: false,
166-
},
167-
{
168-
a: testKey,
169-
b: &GroupKey{
170-
RawKey: keychain.KeyDescriptor{
171-
KeyLocator: testKey.RawKey.KeyLocator,
172-
PubKey: nil,
173-
},
174-
175-
GroupPubKey: testKey.GroupPubKey,
176-
Witness: testKey.Witness,
177-
},
178-
equal: false,
179-
},
180-
{
181-
a: testKey,
182-
b: &GroupKey{
183-
RawKey: keychain.KeyDescriptor{
184-
PubKey: &pubKeyCopy,
185-
},
186-
187-
GroupPubKey: testKey.GroupPubKey,
188-
Witness: testKey.Witness,
189-
},
190-
equal: false,
191-
},
192-
{
193-
a: testKey,
194-
b: &GroupKey{
195-
RawKey: keychain.KeyDescriptor{
196-
KeyLocator: testKey.RawKey.KeyLocator,
197-
PubKey: &pubKeyCopy,
198-
},
199-
200-
GroupPubKey: testKey.GroupPubKey,
201-
Witness: testKey.Witness,
202-
},
203-
equal: true,
204-
},
205-
{
206-
a: &GroupKey{
207-
GroupPubKey: testKey.GroupPubKey,
208-
Witness: testKey.Witness,
209-
},
210-
b: &GroupKey{
211-
GroupPubKey: testKey.GroupPubKey,
212-
Witness: testKey.Witness,
213-
},
214-
equal: true,
215-
},
216-
{
217-
a: &GroupKey{
218-
RawKey: keychain.KeyDescriptor{
219-
KeyLocator: testKey.RawKey.KeyLocator,
220-
},
221-
GroupPubKey: testKey.GroupPubKey,
222-
Witness: testKey.Witness,
223-
},
224-
b: &GroupKey{
225-
RawKey: keychain.KeyDescriptor{
226-
KeyLocator: testKey.RawKey.KeyLocator,
227-
},
228-
GroupPubKey: testKey.GroupPubKey,
229-
Witness: testKey.Witness,
230-
},
231-
equal: true,
232-
},
233-
}
234-
235-
for _, testCase := range tests {
236-
testCase := testCase
237-
require.Equal(t, testCase.equal, testCase.a.IsEqual(testCase.b))
238-
require.Equal(t, testCase.equal, testCase.b.IsEqual(testCase.a))
239-
}
240-
}
241-
242111
// TestGenesisAssetClassification tests that the multiple forms of genesis asset
243112
// are recognized correctly.
244113
func TestGenesisAssetClassification(t *testing.T) {
@@ -858,161 +727,6 @@ func TestAssetID(t *testing.T) {
858727
require.NotEqual(t, id[:], differentID[:])
859728
}
860729

861-
// TestAssetGroupKey tests that the asset key group is derived correctly.
862-
func TestAssetGroupKey(t *testing.T) {
863-
t.Parallel()
864-
865-
privKey, err := btcec.NewPrivateKey()
866-
groupPub := privKey.PubKey()
867-
require.NoError(t, err)
868-
privKeyCopy := btcec.PrivKeyFromScalar(&privKey.Key)
869-
genSigner := NewMockGenesisSigner(privKeyCopy)
870-
genBuilder := MockGroupTxBuilder{}
871-
fakeKeyDesc := test.PubToKeyDesc(groupPub)
872-
fakeScriptKey := NewScriptKeyBip86(fakeKeyDesc)
873-
874-
g := Genesis{
875-
FirstPrevOut: wire.OutPoint{
876-
Hash: hashBytes1,
877-
Index: 99,
878-
},
879-
Tag: "normal asset 1",
880-
MetaHash: [MetaHashLen]byte{1, 2, 3},
881-
OutputIndex: 21,
882-
Type: Collectible,
883-
}
884-
groupTweak := g.ID()
885-
886-
internalKey := input.TweakPrivKey(privKeyCopy, groupTweak[:])
887-
tweakedKey := txscript.TweakTaprootPrivKey(*internalKey, nil)
888-
889-
// TweakTaprootPrivKey modifies the private key that is passed in! We
890-
// need to provide a copy to arrive at the same result.
891-
protoAsset := NewAssetNoErr(t, g, 1, 0, 0, fakeScriptKey, nil)
892-
groupReq := NewGroupKeyRequestNoErr(
893-
t, fakeKeyDesc, fn.None[ExternalKey](), g, protoAsset, nil,
894-
fn.None[chainhash.Hash](),
895-
)
896-
genTx, err := groupReq.BuildGroupVirtualTx(&genBuilder)
897-
require.NoError(t, err)
898-
899-
keyGroup, err := DeriveGroupKey(genSigner, *genTx, *groupReq, nil)
900-
require.NoError(t, err)
901-
902-
require.Equal(
903-
t, schnorr.SerializePubKey(tweakedKey.PubKey()),
904-
schnorr.SerializePubKey(&keyGroup.GroupPubKey),
905-
)
906-
907-
// We should also be able to reproduce the correct tweak with a non-nil
908-
// tapscript root.
909-
tapTweak := test.RandBytes(32)
910-
tweakedKey = txscript.TweakTaprootPrivKey(*internalKey, tapTweak)
911-
912-
groupReq = NewGroupKeyRequestNoErr(
913-
t, test.PubToKeyDesc(privKey.PubKey()), fn.None[ExternalKey](),
914-
g, protoAsset, tapTweak, fn.None[chainhash.Hash](),
915-
)
916-
genTx, err = groupReq.BuildGroupVirtualTx(&genBuilder)
917-
require.NoError(t, err)
918-
919-
keyGroup, err = DeriveGroupKey(genSigner, *genTx, *groupReq, nil)
920-
require.NoError(t, err)
921-
922-
require.Equal(
923-
t, schnorr.SerializePubKey(tweakedKey.PubKey()),
924-
schnorr.SerializePubKey(&keyGroup.GroupPubKey),
925-
)
926-
927-
// Group key tweaking should fail when given invalid tweaks.
928-
badTweak := test.RandBytes(33)
929-
_, err = GroupPubKeyV0(groupPub, badTweak, badTweak)
930-
require.Error(t, err)
931-
932-
_, err = GroupPubKeyV0(groupPub, groupTweak[:], badTweak)
933-
require.Error(t, err)
934-
}
935-
936-
// TestDeriveGroupKey tests that group key derivation fails for assets that are
937-
// not eligible to be group anchors.
938-
func TestDeriveGroupKey(t *testing.T) {
939-
t.Parallel()
940-
941-
groupPriv := test.RandPrivKey()
942-
groupPub := groupPriv.PubKey()
943-
groupKeyDesc := test.PubToKeyDesc(groupPub)
944-
genSigner := NewMockGenesisSigner(groupPriv)
945-
genBuilder := MockGroupTxBuilder{}
946-
947-
baseGen := RandGenesis(t, Normal)
948-
collectGen := RandGenesis(t, Collectible)
949-
baseScriptKey := RandScriptKey(t)
950-
protoAsset := RandAssetWithValues(t, baseGen, nil, baseScriptKey)
951-
nonGenProtoAsset := protoAsset.Copy()
952-
nonGenProtoAsset.PrevWitnesses = []Witness{{
953-
PrevID: &PrevID{
954-
OutPoint: wire.OutPoint{
955-
Hash: hashBytes1,
956-
Index: 1,
957-
},
958-
ID: hashBytes1,
959-
ScriptKey: ToSerialized(pubKey),
960-
},
961-
TxWitness: sigWitness,
962-
SplitCommitment: nil,
963-
}}
964-
groupedProtoAsset := protoAsset.Copy()
965-
groupedProtoAsset.GroupKey = &GroupKey{
966-
GroupPubKey: *groupPub,
967-
}
968-
groupReq := GroupKeyRequest{
969-
RawKey: groupKeyDesc,
970-
AnchorGen: baseGen,
971-
}
972-
973-
// A prototype asset is required for building the genesis virtual TX.
974-
_, err := groupReq.BuildGroupVirtualTx(&genBuilder)
975-
require.ErrorContains(t, err, "grouped asset cannot be nil")
976-
977-
// The prototype asset must have a genesis witness.
978-
groupReq.NewAsset = nonGenProtoAsset
979-
_, err = groupReq.BuildGroupVirtualTx(&genBuilder)
980-
require.ErrorContains(t, err, "asset is not a genesis asset")
981-
982-
// The prototype asset must not have a group key set.
983-
groupReq.NewAsset = groupedProtoAsset
984-
_, err = groupReq.BuildGroupVirtualTx(&genBuilder)
985-
require.ErrorContains(t, err, "asset already has group key")
986-
987-
// The anchor genesis used for signing must have the same asset type
988-
// as the prototype asset being signed.
989-
groupReq.AnchorGen = collectGen
990-
groupReq.NewAsset = protoAsset
991-
_, err = groupReq.BuildGroupVirtualTx(&genBuilder)
992-
require.ErrorContains(t, err, "asset group type mismatch")
993-
994-
// The group key request must include an internal key.
995-
groupReq.AnchorGen = baseGen
996-
groupReq.RawKey.PubKey = nil
997-
_, err = groupReq.BuildGroupVirtualTx(&genBuilder)
998-
require.ErrorContains(t, err, "missing group internal key")
999-
1000-
// The tapscript root in the group key request must be exactly 32 bytes
1001-
// if present.
1002-
groupReq.RawKey = groupKeyDesc
1003-
groupReq.TapscriptRoot = test.RandBytes(33)
1004-
_, err = groupReq.BuildGroupVirtualTx(&genBuilder)
1005-
require.ErrorContains(t, err, "tapscript root must be 32 bytes")
1006-
1007-
groupReq.TapscriptRoot = test.RandBytes(32)
1008-
genTx, err := groupReq.BuildGroupVirtualTx(&genBuilder)
1009-
require.NoError(t, err)
1010-
1011-
groupKey, err := DeriveGroupKey(genSigner, *genTx, groupReq, nil)
1012-
require.NoError(t, err)
1013-
require.NotNil(t, groupKey)
1014-
}
1015-
1016730
// TestAssetWitnesses tests that the asset group witness can be serialized and
1017731
// parsed correctly, and that signature detection works correctly.
1018732
func TestAssetWitnesses(t *testing.T) {

0 commit comments

Comments
 (0)