-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpreimage.go
More file actions
137 lines (108 loc) · 2.99 KB
/
Copy pathpreimage.go
File metadata and controls
137 lines (108 loc) · 2.99 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
package swaps
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"github.com/btcsuite/btcd/psbt/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightninglabs/wavelength/lib/arkscript"
"github.com/lightningnetwork/lnd/lntypes"
)
// preimageMatchesHash reports whether preimage hashes to paymentHash.
func preimageMatchesHash(preimage *lntypes.Preimage,
paymentHash lntypes.Hash) bool {
if preimage == nil {
return false
}
return lntypes.Hash(sha256.Sum256(preimage[:])) ==
paymentHash
}
// extractPreimageFromCheckpoint extracts a 32-byte preimage from a finalized
// checkpoint PSBT when the spend used the vHTLC claim path.
func extractPreimageFromCheckpoint(psbtBytes []byte) (*lntypes.Preimage,
error) {
pkt, err := psbt.NewFromRawBytes(
bytes.NewReader(psbtBytes), false,
)
if err != nil {
return nil, fmt.Errorf("parse checkpoint PSBT: %w", err)
}
if len(pkt.Inputs) == 0 {
return nil, nil
}
inp := pkt.Inputs[0]
if len(inp.FinalScriptWitness) > 0 {
items, err := deserializeWitness(inp.FinalScriptWitness)
if err != nil {
return nil, fmt.Errorf("deserialize witness: %w", err)
}
if p := findPreimage(items); p != nil {
return p, nil
}
}
conditionWitness, err := arkscript.GetConditionWitnessPSBTInput(inp)
switch {
case err == nil:
if p := findPreimage(conditionWitness); p != nil {
return p, nil
}
case err != nil &&
!errors.Is(err, arkscript.ErrConditionWitnessNotFound):
return nil, fmt.Errorf("decode condition witness: %w", err)
}
for _, sig := range inp.TaprootScriptSpendSig {
if len(sig.Signature) == lntypes.HashSize {
var preimage lntypes.Preimage
copy(preimage[:], sig.Signature)
return &preimage, nil
}
}
for _, leafScript := range inp.TaprootLeafScript {
if len(leafScript.Script) == lntypes.HashSize {
var preimage lntypes.Preimage
copy(preimage[:], leafScript.Script)
return &preimage, nil
}
}
if pkt.UnsignedTx != nil &&
len(pkt.UnsignedTx.TxIn) > 0 {
if p := findPreimage(pkt.UnsignedTx.TxIn[0].Witness); p != nil {
return p, nil
}
}
return nil, nil
}
// findPreimage scans witness items for a 32-byte preimage candidate.
func findPreimage(items [][]byte) *lntypes.Preimage {
for _, item := range items {
if len(item) == lntypes.HashSize {
var preimage lntypes.Preimage
copy(preimage[:], item)
return &preimage
}
}
return nil
}
// deserializeWitness splits a serialized final witness into stack items.
func deserializeWitness(data []byte) ([][]byte, error) {
r := bytes.NewReader(data)
count, err := wire.ReadVarInt(r, 0)
if err != nil {
return nil, fmt.Errorf("read witness count: %w", err)
}
items := make([][]byte, 0, count)
for i := uint64(0); i < count; i++ {
itemLen, err := wire.ReadVarInt(r, 0)
if err != nil {
return nil, fmt.Errorf("read item %d length: %w", i,
err)
}
item := make([]byte, itemLen)
if _, err := r.Read(item); err != nil {
return nil, fmt.Errorf("read item %d data: %w", i, err)
}
items = append(items, item)
}
return items, nil
}