-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrefresh_operator_rotation_test.go
More file actions
119 lines (95 loc) · 3.23 KB
/
Copy pathrefresh_operator_rotation_test.go
File metadata and controls
119 lines (95 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package wallet
import (
"bytes"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/lightninglabs/wavelength/lib/arkscript"
"github.com/stretchr/testify/require"
)
// TestComposeRefreshTemplate is the wallet-side regression, parallel to
// vtxo.TestRefreshEmissionUsesJoinTimeOperatorKey. handleRefreshVTXOs
// hoists a single GetInfo fetch out of its per-outpoint loop and feeds
// the resolved key into composeRefreshTemplate per VTXO; this test
// exercises that helper directly.
func TestComposeRefreshTemplate(t *testing.T) {
t.Parallel()
t.Run("rebuilds against resolved key", func(t *testing.T) {
t.Parallel()
ownerKey := newTestPubKey(t)
k1 := newTestPubKey(t)
k2 := newTestPubKey(t)
const exitDelay = uint32(144)
storedTemplate, err := arkscript.EncodeStandardVTXOTemplate(
ownerKey, k1, exitDelay,
)
require.NoError(t, err)
descriptor := &VTXODescriptor{PolicyTemplate: storedTemplate}
rebuilt, err := composeRefreshTemplate(descriptor, k2)
require.NoError(t, err)
params := decodeWalletStandardParams(t, rebuilt)
require.True(
t, xOnlyPubKeyEqual(params.OperatorKey, k2),
"emitted template must commit to the resolved key K2",
)
require.False(
t, xOnlyPubKeyEqual(params.OperatorKey, k1),
"emitted template must no longer carry K1",
)
require.True(
t, xOnlyPubKeyEqual(params.OwnerKey, ownerKey),
"owner key must be preserved across the rebuild",
)
require.Equal(
t, exitDelay, params.ExitDelay,
"exit delay must be preserved across the rebuild",
)
})
t.Run("returns stored bytes when key unresolved", func(t *testing.T) {
t.Parallel()
ownerKey := newTestPubKey(t)
k1 := newTestPubKey(t)
storedTemplate, err := arkscript.EncodeStandardVTXOTemplate(
ownerKey, k1, 144,
)
require.NoError(t, err)
descriptor := &VTXODescriptor{PolicyTemplate: storedTemplate}
out, err := composeRefreshTemplate(descriptor, nil)
require.NoError(t, err)
require.True(
t, bytes.Equal(out, storedTemplate),
"with no resolved key the helper must return the "+
"stored template verbatim (harness path)",
)
})
}
// newTestPubKey returns a freshly generated secp256k1 public key.
func newTestPubKey(t *testing.T) *btcec.PublicKey {
t.Helper()
priv, err := btcec.NewPrivateKey()
require.NoError(t, err)
return priv.PubKey()
}
// xOnlyPubKeyEqual compares two secp256k1 keys by their Schnorr (x-only)
// serialization. Ark policy templates commit to the x-only form, so y-
// parity differences between the encoded and freshly-derived pubkey must
// not be treated as a mismatch.
func xOnlyPubKeyEqual(a, b *btcec.PublicKey) bool {
if a == nil || b == nil {
return false
}
return bytes.Equal(
schnorr.SerializePubKey(a), schnorr.SerializePubKey(b),
)
}
// decodeWalletStandardParams decodes a serialized standard VTXO policy
// template into its (owner, operator, exit-delay) triple.
func decodeWalletStandardParams(t *testing.T,
raw []byte) *arkscript.StandardVTXOParams {
t.Helper()
template, err := arkscript.DecodePolicyTemplate(raw)
require.NoError(t, err, "decode policy template")
params, err := arkscript.DecodeStandardVTXOParams(template)
require.NoError(t, err, "decode standard VTXO params")
return params
}