• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13707895824

06 Mar 2025 08:49PM UTC coverage: 56.554% (-1.7%) from 58.205%
13707895824

Pull #9458

github

web-flow
Merge d69f7f75d into 7d7e1872c
Pull Request #9458: multi+server.go: add initial permissions for some peers

276 of 549 new or added lines in 10 files covered. (50.27%)

22271 existing lines in 266 files now uncovered.

104853 of 185404 relevant lines covered (56.55%)

24158.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

17.16
/lnrpc/marshall_utils.go
1
package lnrpc
2

3
import (
4
        "encoding/hex"
5
        "errors"
6
        "fmt"
7

8
        "github.com/btcsuite/btcd/btcutil"
9
        "github.com/btcsuite/btcd/chaincfg"
10
        "github.com/btcsuite/btcd/txscript"
11
        "github.com/btcsuite/btcd/wire"
12
        "github.com/btcsuite/btcwallet/wallet"
13
        "github.com/lightningnetwork/lnd/aliasmgr"
14
        "github.com/lightningnetwork/lnd/fn/v2"
15
        "github.com/lightningnetwork/lnd/lnwallet"
16
        "github.com/lightningnetwork/lnd/lnwire"
17
        "golang.org/x/exp/maps"
18
)
19

20
var (
21
        // ErrSatMsatMutualExclusive is returned when both a sat and an msat
22
        // amount are set.
23
        ErrSatMsatMutualExclusive = errors.New(
24
                "sat and msat arguments are mutually exclusive",
25
        )
26

27
        // ErrNegativeAmt is returned when a negative amount is specified.
28
        ErrNegativeAmt = errors.New("amount cannot be negative")
29
)
30

31
// CalculateFeeLimit returns the fee limit in millisatoshis. If a percentage
32
// based fee limit has been requested, we'll factor in the ratio provided with
33
// the amount of the payment.
34
func CalculateFeeLimit(feeLimit *FeeLimit,
35
        amount lnwire.MilliSatoshi) lnwire.MilliSatoshi {
4✔
36

4✔
37
        switch feeLimit.GetLimit().(type) {
4✔
38
        case *FeeLimit_Fixed:
3✔
39
                return lnwire.NewMSatFromSatoshis(
3✔
40
                        btcutil.Amount(feeLimit.GetFixed()),
3✔
41
                )
3✔
42

43
        case *FeeLimit_FixedMsat:
1✔
44
                return lnwire.MilliSatoshi(feeLimit.GetFixedMsat())
1✔
45

UNCOV
46
        case *FeeLimit_Percent:
×
UNCOV
47
                return amount * lnwire.MilliSatoshi(feeLimit.GetPercent()) / 100
×
48

UNCOV
49
        default:
×
UNCOV
50
                // Fall back to a sane default value that is based on the amount
×
UNCOV
51
                // itself.
×
UNCOV
52
                return lnwallet.DefaultRoutingFeeLimitForAmount(amount)
×
53
        }
54
}
55

56
// UnmarshallAmt returns a strong msat type for a sat/msat pair of rpc fields.
57
func UnmarshallAmt(amtSat, amtMsat int64) (lnwire.MilliSatoshi, error) {
4✔
58
        if amtSat != 0 && amtMsat != 0 {
4✔
59
                return 0, ErrSatMsatMutualExclusive
×
60
        }
×
61

62
        if amtSat < 0 || amtMsat < 0 {
4✔
63
                return 0, ErrNegativeAmt
×
64
        }
×
65

66
        if amtSat != 0 {
7✔
67
                return lnwire.NewMSatFromSatoshis(btcutil.Amount(amtSat)), nil
3✔
68
        }
3✔
69

70
        return lnwire.MilliSatoshi(amtMsat), nil
1✔
71
}
72

73
// ParseConfs validates the minimum and maximum confirmation arguments of a
74
// ListUnspent request.
UNCOV
75
func ParseConfs(min, max int32) (int32, int32, error) {
×
UNCOV
76
        switch {
×
77
        // Ensure that the user didn't attempt to specify a negative number of
78
        // confirmations, as that isn't possible.
79
        case min < 0:
×
80
                return 0, 0, fmt.Errorf("min confirmations must be >= 0")
×
81

82
        // We'll also ensure that the min number of confs is strictly less than
83
        // or equal to the max number of confs for sanity.
84
        case min > max:
×
85
                return 0, 0, fmt.Errorf("max confirmations must be >= min " +
×
86
                        "confirmations")
×
87

UNCOV
88
        default:
×
UNCOV
89
                return min, max, nil
×
90
        }
91
}
92

93
// MarshalUtxos translates a []*lnwallet.Utxo into a []*lnrpc.Utxo.
94
func MarshalUtxos(utxos []*lnwallet.Utxo, activeNetParams *chaincfg.Params) (
UNCOV
95
        []*Utxo, error) {
×
UNCOV
96

×
UNCOV
97
        res := make([]*Utxo, 0, len(utxos))
×
UNCOV
98
        for _, utxo := range utxos {
×
UNCOV
99
                // Translate lnwallet address type to the proper gRPC proto
×
UNCOV
100
                // address type.
×
UNCOV
101
                var addrType AddressType
×
UNCOV
102
                switch utxo.AddressType {
×
UNCOV
103
                case lnwallet.WitnessPubKey:
×
UNCOV
104
                        addrType = AddressType_WITNESS_PUBKEY_HASH
×
105

UNCOV
106
                case lnwallet.NestedWitnessPubKey:
×
UNCOV
107
                        addrType = AddressType_NESTED_PUBKEY_HASH
×
108

UNCOV
109
                case lnwallet.TaprootPubkey:
×
UNCOV
110
                        addrType = AddressType_TAPROOT_PUBKEY
×
111

112
                case lnwallet.UnknownAddressType:
×
113
                        continue
×
114

115
                default:
×
116
                        return nil, fmt.Errorf("invalid utxo address type")
×
117
                }
118

119
                // Now that we know we have a proper mapping to an address,
120
                // we'll convert the regular outpoint to an lnrpc variant.
UNCOV
121
                outpoint := MarshalOutPoint(&utxo.OutPoint)
×
UNCOV
122

×
UNCOV
123
                utxoResp := Utxo{
×
UNCOV
124
                        AddressType:   addrType,
×
UNCOV
125
                        AmountSat:     int64(utxo.Value),
×
UNCOV
126
                        PkScript:      hex.EncodeToString(utxo.PkScript),
×
UNCOV
127
                        Outpoint:      outpoint,
×
UNCOV
128
                        Confirmations: utxo.Confirmations,
×
UNCOV
129
                }
×
UNCOV
130

×
UNCOV
131
                // Finally, we'll attempt to extract the raw address from the
×
UNCOV
132
                // script so we can display a human friendly address to the end
×
UNCOV
133
                // user.
×
UNCOV
134
                _, outAddresses, _, err := txscript.ExtractPkScriptAddrs(
×
UNCOV
135
                        utxo.PkScript, activeNetParams,
×
UNCOV
136
                )
×
UNCOV
137
                if err != nil {
×
138
                        return nil, err
×
139
                }
×
140

141
                // If we can't properly locate a single address, then this was
142
                // an error in our mapping, and we'll return an error back to
143
                // the user.
UNCOV
144
                if len(outAddresses) != 1 {
×
145
                        return nil, fmt.Errorf("an output was unexpectedly " +
×
146
                                "multisig")
×
147
                }
×
UNCOV
148
                utxoResp.Address = outAddresses[0].String()
×
UNCOV
149

×
UNCOV
150
                res = append(res, &utxoResp)
×
151
        }
152

UNCOV
153
        return res, nil
×
154
}
155

156
// MarshallOutputType translates a txscript.ScriptClass into a
157
// lnrpc.OutputScriptType.
UNCOV
158
func MarshallOutputType(o txscript.ScriptClass) OutputScriptType {
×
UNCOV
159
        // Translate txscript ScriptClass type to the proper gRPC proto
×
UNCOV
160
        // output script type.
×
UNCOV
161
        switch o {
×
162
        case txscript.ScriptHashTy:
×
163
                return OutputScriptType_SCRIPT_TYPE_SCRIPT_HASH
×
UNCOV
164
        case txscript.WitnessV0PubKeyHashTy:
×
UNCOV
165
                return OutputScriptType_SCRIPT_TYPE_WITNESS_V0_PUBKEY_HASH
×
UNCOV
166
        case txscript.WitnessV0ScriptHashTy:
×
UNCOV
167
                return OutputScriptType_SCRIPT_TYPE_WITNESS_V0_SCRIPT_HASH
×
168
        case txscript.PubKeyTy:
×
169
                return OutputScriptType_SCRIPT_TYPE_PUBKEY
×
170
        case txscript.MultiSigTy:
×
171
                return OutputScriptType_SCRIPT_TYPE_MULTISIG
×
172
        case txscript.NullDataTy:
×
173
                return OutputScriptType_SCRIPT_TYPE_NULLDATA
×
174
        case txscript.NonStandardTy:
×
175
                return OutputScriptType_SCRIPT_TYPE_NON_STANDARD
×
176
        case txscript.WitnessUnknownTy:
×
177
                return OutputScriptType_SCRIPT_TYPE_WITNESS_UNKNOWN
×
UNCOV
178
        case txscript.WitnessV1TaprootTy:
×
UNCOV
179
                return OutputScriptType_SCRIPT_TYPE_WITNESS_V1_TAPROOT
×
UNCOV
180
        default:
×
UNCOV
181
                return OutputScriptType_SCRIPT_TYPE_PUBKEY_HASH
×
182
        }
183
}
184

185
// MarshalOutPoint converts a wire.OutPoint to its proto counterpart.
186
func MarshalOutPoint(op *wire.OutPoint) *OutPoint {
2✔
187
        return &OutPoint{
2✔
188
                TxidBytes:   op.Hash[:],
2✔
189
                TxidStr:     op.Hash.String(),
2✔
190
                OutputIndex: op.Index,
2✔
191
        }
2✔
192
}
2✔
193

194
// UnmarshallCoinSelectionStrategy converts a lnrpc.CoinSelectionStrategy proto
195
// type to its wallet.CoinSelectionStrategy counterpart type, considering
196
// a global default strategy if necessary.
197
//
198
// The globalStrategy parameter specifies the default coin selection strategy
199
// to use if the strategy is set to STRATEGY_USE_GLOBAL_CONFIG. This allows
200
// flexibility in defining a default strategy at a global level.
201
func UnmarshallCoinSelectionStrategy(strategy CoinSelectionStrategy,
202
        globalStrategy wallet.CoinSelectionStrategy) (
UNCOV
203
        wallet.CoinSelectionStrategy, error) {
×
UNCOV
204

×
UNCOV
205
        switch strategy {
×
UNCOV
206
        case CoinSelectionStrategy_STRATEGY_USE_GLOBAL_CONFIG:
×
UNCOV
207
                return globalStrategy, nil
×
208

209
        case CoinSelectionStrategy_STRATEGY_LARGEST:
×
210
                return wallet.CoinSelectionLargest, nil
×
211

212
        case CoinSelectionStrategy_STRATEGY_RANDOM:
×
213
                return wallet.CoinSelectionRandom, nil
×
214

215
        default:
×
216
                return nil, fmt.Errorf("unknown coin selection strategy "+
×
217
                        "%v", strategy)
×
218
        }
219
}
220

221
// MarshalAliasMap converts a ScidAliasMap to its proto counterpart. This is
222
// used in various RPCs that handle scid alias mappings.
UNCOV
223
func MarshalAliasMap(scidMap aliasmgr.ScidAliasMap) []*AliasMap {
×
UNCOV
224
        return fn.Map(
×
UNCOV
225
                maps.Keys(scidMap),
×
UNCOV
226
                func(base lnwire.ShortChannelID) *AliasMap {
×
UNCOV
227
                        return &AliasMap{
×
UNCOV
228
                                BaseScid: base.ToUint64(),
×
UNCOV
229
                                Aliases: fn.Map(
×
UNCOV
230
                                        scidMap[base],
×
UNCOV
231
                                        func(a lnwire.ShortChannelID) uint64 {
×
UNCOV
232
                                                return a.ToUint64()
×
UNCOV
233
                                        },
×
234
                                ),
235
                        }
236
                },
237
        )
238
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc