This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
spark-methods.go
148 lines (125 loc) · 3.7 KB
/
spark-methods.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
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
// Methods required by spark-wallet client.
// https://github.com/shesek/spark-wallet/blob/master/src/cmd.js
package main
import (
"crypto/sha256"
"encoding/hex"
"errors"
"strings"
"time"
"github.com/fiatjaf/lightningd-gjson-rpc/plugin"
"github.com/tidwall/gjson"
)
var connectFund = plugin.RPCMethod{
"connectfund",
"peeruri satoshi feerate",
"",
"",
func(p *plugin.Plugin, params plugin.Params) (resp interface{}, errCode int, err error) {
peeruri := params.Get("peeruri").String()
peerid := strings.Split(peeruri, "@")[0]
p.Client.Call("connect", peeruri)
satoshi := params.Get("satoshi").String()
feerate := params.Get("feerate").String()
res, err := p.Client.Call("fundchannel", peerid, satoshi, feerate)
if err != nil {
return nil, 37, errors.New("cannot open channel")
}
return getChannel(p, peerid, res.Get("channel_id").String())
},
}
var closeGet = plugin.RPCMethod{
"closeget",
"peeruri chanid force timeout",
"",
"",
func(p *plugin.Plugin, params plugin.Params) (resp interface{}, errCode int, err error) {
peerid := params.Get("peerid").String()
chanid := params.Get("chanid").String()
force := params.Get("force").String()
timeout := params.Get("timeout").String()
res, err := p.Client.Call("close", chanid, force, timeout)
if err != nil {
return nil, 37, errors.New("cannot close channel")
}
retval, errCode, err := getChannel(p, peerid, res.Get("channel_id").String())
if err != nil {
return
}
retval["closing"] = res.Value()
return retval, 0, nil
},
}
var listpaysExt = plugin.RPCMethod{
"listpaysext",
"",
"",
"",
func(p *plugin.Plugin, params plugin.Params) (resp interface{}, errCode int, err error) {
res, err := p.Client.CallWithCustomTimeout(time.Second*30, "listpays")
if err != nil {
return nil, 37, errors.New("cannot listpays -- enable the pay plugin")
}
pays := res.Get("pays").Array()
// more than 30, we filter
if len(pays) > 30 {
pays = pays[len(pays)-30:]
}
retval := make([]interface{}, len(pays))
if len(pays) == 0 {
return map[string]interface{}{"pays": retval}, 0, nil
}
// these are not currently available, but be prepared for when they are
if pays[0].Get("payment_hash").Exists() && pays[0].Get("created_at").Exists() {
for i, pay := range pays {
retval[i] = pay.Value()
}
} else {
filled := make(chan interface{}, len(pays))
for _, pay := range pays {
go fillPay(p, pay, filled)
}
i := 0
for fpay := range filled {
retval[i] = fpay
i++
}
}
return map[string]interface{}{"pays": retval}, 0, nil
},
}
func fillPay(p *plugin.Plugin, pay gjson.Result, filled chan<- interface{}) {
payv := pay.Value().(map[string]interface{})
if pay.Get("status").String() != "complete" {
filled <- payv
return
}
preimage, _ := hex.DecodeString(pay.Get("preimage").String())
hash := sha256.Sum256(preimage)
hexhash := hex.EncodeToString(hash[:])
payv["payment_hash"] = hexhash
res, _ := p.Client.CallNamed("listsendpays", "payment_hash", hexhash)
payv["created_at"] = res.Get("payments.0.created_at").Int()
filled <- payv
}
func getChannel(p *plugin.Plugin, peerid string, channel_id string) (resp map[string]interface{}, errCode int, err error) {
res, err := p.Client.Call("listpeers")
if err != nil || len(res.Array()) == 0 {
return nil, 38, errors.New("cannot find peer")
}
peer := res.Array()[0]
var channel gjson.Result
for _, channel = range peer.Get("channels").Array() {
if channel.Get("channel_id").String() == channel_id {
goto found
}
}
return nil, 39, errors.New("cannot find channel")
found:
ipeer := peer.Value().(map[string]interface{})
delete(ipeer, "channels")
return map[string]interface{}{
"peer": ipeer,
"chan": channel,
}, 0, nil
}