Skip to content

Commit 4915ff5

Browse files
committed
test+tappsbt: refactor asset gen for VPackets
1 parent 5c5fcf8 commit 4915ff5

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

internal/test/helpers.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,20 @@ func RandPrivKey(_ testing.TB) *btcec.PrivateKey {
103103
return priv
104104
}
105105

106+
func RandKeyLoc() keychain.KeyLocator {
107+
return keychain.KeyLocator{
108+
Index: RandInt[uint32](),
109+
Family: keychain.KeyFamily(RandInt[uint32]()),
110+
}
111+
}
112+
106113
func RandKeyDesc(t testing.TB) (keychain.KeyDescriptor, *btcec.PrivateKey) {
107114
priv, err := btcec.NewPrivateKey()
108115
require.NoError(t, err)
109116

110117
return keychain.KeyDescriptor{
111-
PubKey: priv.PubKey(),
112-
KeyLocator: keychain.KeyLocator{
113-
Index: RandInt[uint32](),
114-
Family: keychain.KeyFamily(RandInt[uint32]()),
115-
},
118+
PubKey: priv.PubKey(),
119+
KeyLocator: RandKeyLoc(),
116120
}, priv
117121
}
118122

tappsbt/mock.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ var (
3737
)
3838
)
3939

40+
41+
func RandAssetForPacket(t testing.TB, assetType asset.Type,
42+
desc keychain.KeyDescriptor) *asset.Asset {
43+
44+
randAsset := asset.RandAsset(t, assetType)
45+
randAsset.ScriptKey = asset.NewScriptKeyBip86(desc)
46+
47+
// The raw key won't be serialized within the asset, so let's blank it
48+
// out here to get a fully, byte-by-byte comparable PSBT.
49+
randAsset.GroupKey.RawKey = keychain.KeyDescriptor{}
50+
randAsset.GroupKey.Witness = nil
51+
randAsset.ScriptKey.TweakedScriptKey = nil
52+
53+
return randAsset
54+
}
55+
4056
// RandPacket generates a random virtual packet for testing purposes.
4157
func RandPacket(t testing.TB, setVersion bool) *VPacket {
4258
testPubKey := test.RandPubKey(t)
@@ -66,16 +82,12 @@ func RandPacket(t testing.TB, setVersion bool) *VPacket {
6682
testAsset := asset.RandAsset(t, asset.Normal)
6783
testAsset.ScriptKey = inputScriptKey
6884

69-
testOutputAsset := asset.RandAsset(t, asset.Normal)
70-
testOutputAsset.ScriptKey = asset.NewScriptKeyBip86(keyDesc)
71-
7285
// The raw key won't be serialized within the asset, so let's blank it
7386
// out here to get a fully, byte-by-byte comparable PSBT.
7487
testAsset.GroupKey.RawKey = keychain.KeyDescriptor{}
7588
testAsset.GroupKey.Witness = nil
76-
testOutputAsset.GroupKey.RawKey = keychain.KeyDescriptor{}
77-
testOutputAsset.GroupKey.Witness = nil
78-
testOutputAsset.ScriptKey.TweakedScriptKey = nil
89+
90+
testOutputAsset := RandAssetForPacket(t, asset.Normal, keyDesc)
7991
leaf1 := txscript.TapLeaf{
8092
LeafVersion: txscript.BaseLeafVersion,
8193
Script: []byte("not a valid script"),

0 commit comments

Comments
 (0)