-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbuild_test.go
More file actions
142 lines (117 loc) · 3.57 KB
/
Copy pathbuild_test.go
File metadata and controls
142 lines (117 loc) · 3.57 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package checkpoint
import (
"crypto/rand"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/txscript/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightninglabs/wavelength/lib/arkscript"
"github.com/lightninglabs/wavelength/lib/tx/arktx"
"github.com/stretchr/testify/require"
)
// randomP2TRScript returns a P2TR pkScript with a random key.
func randomP2TRScript(t *testing.T) []byte {
t.Helper()
var key [32]byte
_, err := rand.Read(key[:])
require.NoError(t, err)
return append([]byte{txscript.OP_1, 0x20}, key[:]...)
}
// TestBuildPSBTHappyPath asserts BuildPSBT creates a signable checkpoint PSBT
// and returns the corresponding tap tree encoding.
func TestBuildPSBTHappyPath(t *testing.T) {
t.Parallel()
operatorKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
policy := arkscript.CheckpointPolicy{
OperatorKey: operatorKey.PubKey(),
CSVDelay: 10,
}
witnessUtxo := &wire.TxOut{
Value: 5000,
PkScript: randomP2TRScript(t),
}
in := Input{
SpentVTXO: SpentVTXORef{
Outpoint: wire.OutPoint{
Hash: [32]byte{
1,
},
Index: 0,
},
Output: witnessUtxo,
},
OwnerLeafScript: []byte{
txscript.OP_1,
txscript.OP_1,
txscript.OP_ADD,
txscript.OP_2,
txscript.OP_EQUAL,
},
}
result, err := BuildPSBT(policy, in)
require.NoError(t, err)
require.NotNil(t, result)
require.NotNil(t, result.PSBT)
require.NotNil(t, result.PSBT.UnsignedTx)
tx := result.PSBT.UnsignedTx
require.Equal(t, int32(arktx.TxVersion), tx.Version)
require.Len(t, tx.TxIn, 1)
require.Equal(t, in.SpentVTXO.Outpoint, tx.TxIn[0].PreviousOutPoint)
require.Len(t, tx.TxOut, 2)
require.Equal(t, witnessUtxo.Value, tx.TxOut[0].Value)
require.True(t, arktx.IsAnchorOutput(tx.TxOut[1]))
expectedPkScript, err := arkscript.CheckpointPkScript(
policy, in.OwnerLeafScript,
)
require.NoError(t, err)
require.Equal(t, expectedPkScript, tx.TxOut[0].PkScript)
require.NotNil(t, result.PSBT.Inputs[0].WitnessUtxo)
require.Equal(t, witnessUtxo, result.PSBT.Inputs[0].WitnessUtxo)
decoded, err := DecodeTapTree(result.TapTreeEncoded)
require.NoError(t, err)
tapscript, err := arkscript.CheckpointTapScript(
policy, in.OwnerLeafScript,
)
require.NoError(t, err)
expectedLeaves := make([][]byte, 0, len(tapscript.Leaves))
for _, leaf := range tapscript.Leaves {
expectedLeaves = append(expectedLeaves, leaf.Script)
}
require.Equal(t, expectedLeaves, decoded)
}
// TestBuildPSBTRejectsMissingWitness asserts missing witness data is rejected.
func TestBuildPSBTRejectsMissingWitness(t *testing.T) {
t.Parallel()
operatorKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
policy := arkscript.CheckpointPolicy{
OperatorKey: operatorKey.PubKey(),
CSVDelay: 10,
}
_, err = BuildPSBT(policy, Input{})
require.Error(t, err)
}
// TestBuildPSBTRejectsNonCanonicalCSV verifies that the checkpoint builder
// propagates the shared arkscript CSV invariant instead of constructing an
// operator timeout leaf whose apparent delay differs from consensus.
func TestBuildPSBTRejectsNonCanonicalCSV(t *testing.T) {
t.Parallel()
operatorKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
result, err := BuildPSBT(arkscript.CheckpointPolicy{
OperatorKey: operatorKey.PubKey(),
CSVDelay: uint32(wire.SequenceLockTimeMask) + 1,
}, Input{
SpentVTXO: SpentVTXORef{
Outpoint: wire.OutPoint{Index: 1},
Output: &wire.TxOut{
Value: 5000,
PkScript: randomP2TRScript(t),
},
},
OwnerLeafScript: []byte{txscript.OP_TRUE},
})
require.ErrorContains(t, err, "canonical block delay")
require.Nil(t, result)
}