forked from bolt-observer/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cln_helpers.go
118 lines (93 loc) · 2.29 KB
/
cln_helpers.go
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
package lightning
import (
"fmt"
"math/big"
"strconv"
"strings"
"github.com/golang/glog"
)
// ToLndChanID - converts from CLN to LND channel id.
func ToLndChanID(id string) (uint64, error) {
split := strings.Split(strings.ToLower(id), "x")
if len(split) != 3 {
return 0, fmt.Errorf("wrong channel id: %v", id)
}
blockID, err := strconv.ParseUint(split[0], 10, 64)
if err != nil {
return 0, err
}
txIdx, err := strconv.ParseUint(split[1], 10, 64)
if err != nil {
return 0, err
}
outputIdx, err := strconv.ParseUint(split[2], 10, 64)
if err != nil {
return 0, err
}
result := (blockID&0xffffff)<<40 | (txIdx&0xffffff)<<16 | (outputIdx & 0xffff)
return result, nil
}
// FromLndChanID - converts from LND to CLN channel id.
func FromLndChanID(chanID uint64) string {
blockID := int64((chanID & 0xffffff0000000000) >> 40)
txIdx := int((chanID & 0x000000ffffff0000) >> 16)
outputIdx := int(chanID & 0x000000000000ffff)
return fmt.Sprintf("%dx%dx%d", blockID, txIdx, outputIdx)
}
// ConvertAmount - converts string amount to satoshis.
func ConvertAmount(s string) uint64 {
x := strings.ReplaceAll(s, "msat", "")
if x == "" {
return 0
}
ret, err := strconv.ParseUint(x, 10, 64)
if err != nil {
glog.Warningf("Could not convert: %v %v", s, err)
return 0
}
return ret
}
// ConvertFeatures - convert bitmask to LND like feature map.
func ConvertFeatures(features string) map[string]NodeFeatureAPI {
n := new(big.Int)
n, ok := n.SetString(features, 16)
if !ok {
return nil
}
result := make(map[string]NodeFeatureAPI)
m := big.NewInt(0)
zero := big.NewInt(0)
two := big.NewInt(2)
bit := 0
for n.Cmp(zero) == 1 {
n.DivMod(n, two, m)
if m.Cmp(zero) != 1 {
// Bit is not set
bit++
continue
}
result[fmt.Sprintf("%d", bit)] = NodeFeatureAPI{
Name: "",
IsKnown: true,
IsRequired: bit%2 == 0,
}
bit++
}
return result
}
// SumCapacitySimple - get sum of channel capacity.
func SumCapacitySimple(channels []NodeChannelAPI) uint64 {
sum := uint64(0)
for _, channel := range channels {
sum += channel.Capacity
}
return sum
}
// SumCapacityExtended - get sum of channel capacity.
func SumCapacityExtended(channels []NodeChannelAPIExtended) uint64 {
sum := uint64(0)
for _, channel := range channels {
sum += channel.Capacity
}
return sum
}