-
Notifications
You must be signed in to change notification settings - Fork 0
/
getchannel.go
73 lines (63 loc) · 1.35 KB
/
getchannel.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type ChannelResponse struct {
Outputs map[string]*ChannelResponseOutput `json:"outputs"`
}
type ChannelResponseOutput struct {
Capacity int64 `json:"capacity"`
NodeLeft struct {
Alias string `json:"alias"`
PublicKey string `json:"public_key"`
} `json:"node_left"`
NodeRight struct {
Alias string `json:"alias"`
PublicKey string `json:"public_key"`
} `json:"node_right"`
}
type Channel struct {
TxID string
NodeA string
NodeB string
Capacity int64
IsMulti bool
}
var emptyChannelData = fmt.Errorf("empty channel data")
func getChannel(txid string, ch *Channel) error {
w, err := http.Get("https://mempool.space/api/v1/lightning/channels/txids?txId[]=" + txid)
if err != nil {
return err
}
defer w.Body.Close()
wb, err := io.ReadAll(w.Body)
if err != nil {
return err
}
var res []ChannelResponse
err = json.Unmarshal(wb, &res)
if err != nil {
return err
}
var output *ChannelResponseOutput
for _, o := range res[0].Outputs {
output = o
break
}
if output == nil {
return emptyChannelData
}
ch.Capacity = output.Capacity
ch.NodeA = output.NodeLeft.Alias
if ch.NodeA == "" {
ch.NodeA = output.NodeLeft.PublicKey[0:6]
}
ch.NodeB = output.NodeRight.Alias
if ch.NodeB == "" {
ch.NodeB = output.NodeRight.PublicKey[0:6]
}
return nil
}