-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (169 loc) · 5.43 KB
/
main.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
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
package main
import (
"context"
"encoding/hex"
"errors"
"log"
"os"
"time"
"github.com/YusukeShimizu/c-neutrino/neutrino"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/neutrinorpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/niftynei/glightning/glightning"
)
const MaxFeeMultiple uint64 = 10
var neutrinoC *neutrino.Neutrino
func main() {
plugin := glightning.NewPlugin(onInit)
bb := glightning.NewBitcoinBackend(plugin)
bb.RegisterGetUtxOut(GetUtxOut)
bb.RegisterGetChainInfo(GetChainInfo)
bb.RegisterGetFeeRate(GetFeeRate)
bb.RegisterSendRawTransaction(SendRawTx)
bb.RegisterGetRawBlockByHeight(BlockByHeight)
bb.RegisterEstimateFees(EstimateFees)
plugin.RegisterNewOption("tls-cert-path", "tls cert path", "/tls.cert")
plugin.RegisterNewOption("macaroon-path", "macaroon path", "/admin.macaroon")
plugin.RegisterNewOption("grpc-dial", "gRPC port", "localhost:10009")
err := plugin.Start(os.Stdin, os.Stdout)
if err != nil {
log.Fatal(err)
}
}
func onInit(plugin *glightning.Plugin, options map[string]glightning.Option, config *glightning.Config) {
// info is set via plugin 'options'
tlsCertPath, _ := plugin.GetOption("tls-cert-path")
macaroonPath, _ := plugin.GetOption("macaroon-path")
grpcDial, _ := plugin.GetOption("grpc-dial")
var err error
// default startup
neutrinoC, err = neutrino.NewNeutrino(tlsCertPath, macaroonPath, grpcDial)
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
i, err := neutrinoC.LightningClient.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
log.Fatal(err)
}
log.Printf("The node alias is %s:", i.GetAlias())
log.Printf("successfully init'd! %s %s\n", config.LightningDir, config.RpcFile)
}
func GetUtxOut(txid string, vout uint32) (string, string, error) {
log.Printf("called getutxo")
var retout *lnrpc.Utxo
tnds, err := neutrinoC.LightningClient.ListUnspent(context.Background(), &lnrpc.ListUnspentRequest{})
if err != nil {
log.Printf("error returned: %s", err)
return "", "", err
}
for _, t := range tnds.GetUtxos() {
if t.GetOutpoint().GetTxidStr() == txid && int64(t.GetOutpoint().GetOutputIndex()) == int64(vout) {
retout = t
}
}
// gettxout sends back an empty if there's nothing found,
// which is ok, we just need to pass this info along
if retout == nil {
return "", "", nil
}
log.Printf("txout is %v", retout)
amt := glightning.NewSat64(uint64(retout.GetAmountSat()))
return amt.ConvertMsat().String(), retout.GetPkScript(), nil
}
func GetChainInfo() (*glightning.Btc_ChainInfo, error) {
log.Printf("called getchaininfo")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
i, err := neutrinoC.LightningClient.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
return nil, err
}
chain := ""
for _, c := range i.GetChains() {
if c.GetChain() == "bitcoin" {
if c.GetNetwork() == "regtest" {
chain = "regtest"
} else if c.GetNetwork() == "testnet" {
chain = "test"
} else if c.GetNetwork() == "mainnet" {
chain = "bitcoin"
}
}
}
if chain == "" {
log.Fatal("chain is not bitcoin")
}
return &glightning.Btc_ChainInfo{
Chain: chain,
HeaderCount: i.GetBlockHeight(),
BlockCount: i.GetBlockHeight(),
InitialBlockDownload: false,
}, nil
}
func GetFeeRate(blocks uint32, mode string) (uint64, error) {
log.Printf("called getfeerate %d %s", blocks, mode)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
f, err := neutrinoC.LightningClient.EstimateFee(ctx, &lnrpc.EstimateFeeRequest{})
if err != nil {
return 0, err
}
// feerate's response must be denominated in satoshi per kilo-vbyte
return uint64(f.GetFeeSat()), nil
}
func EstimateFees() (*glightning.Btc_EstimatedFees, error) {
log.Printf("called estimatefees")
f, err := neutrinoC.WalletClient.EstimateFee(context.Background(), &walletrpc.EstimateFeeRequest{
ConfTarget: 20,
})
if err != nil {
return nil, err
}
return &glightning.Btc_EstimatedFees{
Opening: uint64(f.GetSatPerKw()),
MutualClose: uint64(f.GetSatPerKw()),
UnilateralClose: uint64(f.GetSatPerKw()),
DelayedToUs: uint64(f.GetSatPerKw()),
HtlcResolution: uint64(f.GetSatPerKw()),
Penalty: uint64(f.GetSatPerKw()),
MinAcceptable: uint64(f.GetSatPerKw()),
MaxAcceptable: uint64(f.GetSatPerKw()) * MaxFeeMultiple,
}, nil
}
func SendRawTx(tx string) error {
log.Printf("called SendRawTx")
b, err := hex.DecodeString(tx)
if err != nil {
log.Fatal(err)
}
res, err := neutrinoC.WalletClient.PublishTransaction(context.Background(), &walletrpc.Transaction{
TxHex: b,
})
if err != nil {
log.Fatal(err)
}
log.Printf("called sendrawtransaction %s(%s)", res.GetPublishError(), err)
return err
}
// return a blockhash, block, error
func BlockByHeight(height uint32) (string, string, error) {
log.Printf("called blockbyheight %d", height)
h, err := neutrinoC.NeutrinoKitClient.GetBlockHash(context.Background(), &neutrinorpc.GetBlockHashRequest{
Height: int32(height),
})
if err != nil {
log.Println(err)
return "", "", errors.New("Block height out of range")
}
b, err := neutrinoC.NeutrinoKitClient.GetBlock(context.Background(), &neutrinorpc.GetBlockRequest{
Hash: h.GetHash(),
})
if err != nil {
log.Println(err)
return "", "", errors.New("Block height out of range")
}
return h.GetHash(), hex.EncodeToString(b.GetRawHex()), nil
}