-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
217 lines (194 loc) · 6.34 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"fmt"
"math/rand"
"github.com/btcsuite/btcd/rpcclient"
"github.com/fiatjaf/lightningd-gjson-rpc/plugin"
)
const version = "0.8.2"
var (
network string
esplora = map[string][]string{
"bitcoin": {
"https://mempool.space/api",
"https://blockstream.info/api",
"https://mempool.emzy.de/api",
},
"testnet": {
"https://mempool.space/testnet/api",
"https://blockstream.info/testnet/api",
},
"signet": {
"https://mempool.space/signet/api",
},
"liquid": {
"https://liquid.network/api",
"https://blockstream.info/liquid/api",
},
}
defaultBitcoindRPCPorts = map[string]string{
"bitcoin": "8332",
"testnet": "18332",
"signet": "38332",
"regtest": "18443",
}
bitcoind *rpcclient.Client
)
func esploras(network string) (ss []string) {
ss = make([]string, len(esplora[network]))
copy(ss, esplora[network])
rand.Shuffle(len(ss), func(i, j int) {
ss[i], ss[j] = ss[j], ss[i]
})
return ss
}
func main() {
p := plugin.Plugin{
Name: "trustedcoin",
Version: version,
Options: []plugin.Option{
{Name: "bitcoin-rpcconnect", Type: "string", Description: "Hostname (IP) to bitcoind RPC (optional).", Default: ""},
{Name: "bitcoin-rpcport", Type: "string", Description: "Port to bitcoind RPC (optional).", Default: ""},
{Name: "bitcoin-rpcuser", Type: "string", Description: "Username to bitcoind RPC (optional).", Default: ""},
{Name: "bitcoin-rpcpassword", Type: "string", Description: "Password to bitcoind RPC (optional).", Default: ""},
{Name: "bitcoin-datadir", Type: "string", Description: "-datadir arg for bitcoin-cli. For compatibility with bcli, not actually used.", Default: ""},
},
RPCMethods: []plugin.RPCMethod{
{
Name: "getrawblockbyheight",
Usage: "height",
Description: "Get the bitcoin block at a given height",
LongDescription: "",
Handler: func(p *plugin.Plugin, params plugin.Params) (resp any, errCode int, err error) {
height := params.Get("height").Int()
blockUnavailable := map[string]any{
"blockhash": nil,
"block": nil,
}
block, hash, err := getBlock(height)
if err != nil {
p.Logf("getblock error: %s", err.Error())
return blockUnavailable, 0, nil
}
if block == "" {
return blockUnavailable, 0, nil
}
p.Logf("returning block %d, %s…, %d bytes",
height, string(hash[:26]), len(block)/2)
return struct {
BlockHash string `json:"blockhash"`
Block string `json:"block"`
}{hash, string(block)}, 0, nil
},
}, {
Name: "getchaininfo",
Usage: "",
Description: "Get the chain id, the header count, the block count and whether this is IBD.",
LongDescription: "",
Handler: func(p *plugin.Plugin, params plugin.Params) (resp any, errCode int, err error) {
tip, err := getTip()
if err != nil {
return nil, 20, fmt.Errorf("failed to get tip: %s", err.Error())
}
p.Logf("tip: %d", tip)
var bip70network string
switch network {
case "bitcoin":
bip70network = "main"
case "testnet":
bip70network = "test"
case "signet":
bip70network = "signet"
case "regtest":
bip70network = "regtest"
case "liquid":
bip70network = "liquidv1"
}
return struct {
Chain string `json:"chain"`
HeaderCount int64 `json:"headercount"`
BlockCount int64 `json:"blockcount"`
IBD bool `json:"ibd"`
}{bip70network, tip, tip, false}, 0, nil
},
}, {
Name: "estimatefees",
Usage: "",
Description: "Get the Bitcoin feerate in sat/kilo-vbyte.",
LongDescription: "",
Handler: func(p *plugin.Plugin, params plugin.Params) (resp any, errCode int, err error) {
estfees, err := getFeeRates(p.Network)
if err != nil {
p.Logf("estimatefees error: %s", err.Error())
estfees = &EstimatedFees{}
}
return *estfees, 0, nil
},
}, {
Name: "sendrawtransaction",
Usage: "tx",
Description: "Send a raw transaction to the Bitcoin network.",
LongDescription: "",
Handler: func(p *plugin.Plugin, params plugin.Params) (resp any, errCode int, err error) {
hex := params.Get("tx").String()
return sendRawTransaction(hex), 0, nil
},
}, {
Name: "getutxout",
Usage: "txid vout",
Description: "Get informations about an output, identified by a {txid} an a {vout}",
LongDescription: "",
Handler: func(p *plugin.Plugin, params plugin.Params) (resp any, errCode int, err error) {
txid := params.Get("txid").String()
vout := params.Get("vout").Int()
tx, err := getTransaction(txid)
if err != nil {
p.Logf("failed to get tx %s: %s", txid, err.Error())
return UTXOResponse{nil, nil}, 0, nil
}
output := tx.Vout[vout]
return UTXOResponse{&output.Value, &output.ScriptPubKey}, 0, nil
},
},
},
OnInit: func(p *plugin.Plugin) {
network = p.Network
// we will try to use a local bitcoind
user := p.Args.Get("bitcoin-rpcuser").String()
pass := p.Args.Get("bitcoin-rpcpassword").String()
if user != "" && pass != "" {
hostname := p.Args.Get("bitcoin-rpcconnect").String()
if hostname == "" {
hostname = "127.0.0.1"
}
port := p.Args.Get("bitcoin-rpcport").String()
if port == "" {
port = defaultBitcoindRPCPorts[network]
if port == "" {
port = "8332"
}
}
client, err := rpcclient.New(&rpcclient.ConnConfig{
Host: hostname + ":" + port,
User: user,
Pass: pass,
HTTPPostMode: true,
DisableTLS: true,
}, nil)
if err != nil {
p.Log("bitcoind RPC backend settings detected but invalid, will only use block explorers.")
return
}
bitcoind = client
if _, err := bitcoind.GetBlockChainInfo(); err == nil {
p.Log("bitcoind RPC working, will use that with highest priority and fall back to block explorers if it fails.")
} else {
p.Log("bitcoind RPC backend settings detected, but failed to connect, will keep trying to use it though.")
}
return
}
p.Log("bitcoind RPC settings not detected (looked for 'bitcoin-rpcuser', 'bitcoin-rpcpassword' and optionally 'bitcoin-rpcconnect' and 'bitcoin-rpcport'), will only use block explorers.")
},
}
p.Run()
}