-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrpc_custom_refresh_metadata.go
More file actions
189 lines (158 loc) · 4.8 KB
/
Copy pathrpc_custom_refresh_metadata.go
File metadata and controls
189 lines (158 loc) · 4.8 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package waved
import (
"bytes"
"context"
"fmt"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightninglabs/wavelength/arkrpc"
"github.com/lightninglabs/wavelength/indexer"
"github.com/lightninglabs/wavelength/vtxo"
"github.com/lightninglabs/wavelength/wallet"
"github.com/lightningnetwork/lnd/keychain"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
customRefreshMetadataPageSize uint32 = 128
customRefreshAmountMismatch = "custom refresh input %s amount_sat " +
"does not match indexed VTXO"
customRefreshPkScriptMismatch = "custom refresh input %s pk_script " +
"does not match indexed VTXO"
)
func (r *RPCServer) enrichCustomRefreshInputs(ctx context.Context,
inputs []wallet.CustomRefreshInput, clientKey keychain.KeyDescriptor,
operatorKey *btcec.PublicKey) error {
if r.server.indexer == nil {
return status.Error(
codes.Internal, "indexer client not initialized",
)
}
for i := range inputs {
inputs[i].ClientKey = clientKey
inputs[i].OperatorKey = operatorKey
meta, err := r.resolveCustomRefreshMetadata(ctx, inputs[i])
if err != nil {
return err
}
inputs[i].RoundID = meta.RoundID
inputs[i].CommitmentTxID = meta.CommitmentTxID
inputs[i].BatchExpiry = meta.BatchExpiry
inputs[i].ChainDepth = meta.ChainDepth
inputs[i].CreatedHeight = meta.CreatedHeight
inputs[i].Ancestry = meta.Ancestry
}
return nil
}
func (r *RPCServer) resolveCustomRefreshMetadata(ctx context.Context,
input wallet.CustomRefreshInput) (customRefreshMetadata, error) {
var cursor []byte
statusFilter := []arkrpc.VTXOStatus{
arkrpc.VTXOStatus_VTXO_STATUS_LIVE,
}
for {
resp, err := r.server.indexer.ListVTXOsByScriptsTaproot(
ctx,
[]indexer.TaprootScriptScope{{
PkScript: append(
[]byte(nil), input.PkScript...,
),
}},
cursor, customRefreshMetadataPageSize, statusFilter,
)
if err != nil {
return customRefreshMetadata{}, status.Errorf(
codes.Internal, "custom refresh metadata "+
"query failed: %v", err)
}
candidates := vtxo.FlattenListVTXOsByScriptsResponse(resp)
for _, candidate := range candidates {
op, ok := indexerVTXOOutpoint(candidate)
if !ok || op != input.Outpoint {
continue
}
if candidate.GetValueSat() != uint64(input.Amount) {
return customRefreshMetadata{}, status.Errorf(
codes.InvalidArgument,
customRefreshAmountMismatch,
input.Outpoint)
}
if !bytes.Equal(
candidate.GetPkScript(), input.PkScript,
) {
return customRefreshMetadata{}, status.Errorf(
codes.InvalidArgument,
customRefreshPkScriptMismatch,
input.Outpoint)
}
meta, err := customRefreshMetadataFromRPC(candidate)
if err != nil {
return customRefreshMetadata{}, status.Errorf(
codes.Internal, "custom refresh "+
"indexed metadata for %s: %v",
input.Outpoint, err)
}
return meta, nil
}
nextCursor := resp.GetNextCursor()
if len(nextCursor) == 0 {
break
}
cursor = append(cursor[:0], nextCursor...)
}
return customRefreshMetadata{}, status.Errorf(codes.FailedPrecondition,
"custom refresh input %s not found in live indexer inventory",
input.Outpoint)
}
type customRefreshMetadata struct {
RoundID string
CommitmentTxID chainhash.Hash
BatchExpiry int32
ChainDepth int
CreatedHeight int32
Ancestry []vtxo.Ancestry
}
func customRefreshMetadataFromRPC(candidate *arkrpc.VTXO) (
customRefreshMetadata, error) {
if candidate == nil {
return customRefreshMetadata{}, fmt.Errorf("indexer vtxo " +
"must be provided")
}
if candidate.GetRoundId() == "" {
return customRefreshMetadata{}, fmt.Errorf("indexer vtxo " +
"missing round id")
}
if len(candidate.GetCommitmentTxid()) != chainhash.HashSize {
return customRefreshMetadata{}, fmt.Errorf("indexer vtxo " +
"missing commitment txid")
}
ancestry, err := vtxo.AncestryFromRPC(candidate.GetAncestryPaths())
if err != nil {
return customRefreshMetadata{}, fmt.Errorf("convert ancestry "+
"paths: %w", err)
}
var commitmentTxID chainhash.Hash
copy(commitmentTxID[:], candidate.GetCommitmentTxid())
return customRefreshMetadata{
RoundID: candidate.GetRoundId(),
CommitmentTxID: commitmentTxID,
BatchExpiry: candidate.GetBatchExpiryHeight(),
ChainDepth: int(candidate.GetChainDepth()),
CreatedHeight: candidate.GetCreatedHeight(),
Ancestry: ancestry,
}, nil
}
func indexerVTXOOutpoint(candidate *arkrpc.VTXO) (wire.OutPoint, bool) {
if candidate == nil || candidate.GetOutpoint() == nil {
return wire.OutPoint{}, false
}
op := candidate.GetOutpoint()
if len(op.GetTxid()) != chainhash.HashSize {
return wire.OutPoint{}, false
}
var outpoint wire.OutPoint
copy(outpoint.Hash[:], op.GetTxid())
outpoint.Index = op.GetVout()
return outpoint, true
}