/
client.go
383 lines (327 loc) · 10.1 KB
/
client.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package lbrycrd
import (
"encoding/hex"
"net/url"
"os"
"strconv"
c "github.com/lbryio/lbry.go/v3/schema/stake"
"github.com/lbryio/lbcd/btcec"
"github.com/lbryio/lbcd/btcjson"
"github.com/lbryio/lbcd/chaincfg"
"github.com/lbryio/lbcd/chaincfg/chainhash"
"github.com/lbryio/lbcd/rpcclient"
"github.com/lbryio/lbcd/wire"
"github.com/lbryio/lbcutil"
"github.com/cockroachdb/errors"
"github.com/go-ini/ini"
)
const DefaultPort = 9245
var GenesisHash = chainhash.Hash([chainhash.HashSize]byte{ // Make go vet happy.
0x9c, 0x89, 0x28, 0x3b, 0xa0, 0xf3, 0x22, 0x7f,
0x6c, 0x03, 0xb7, 0x02, 0x16, 0xb9, 0xf6, 0x65,
0xf0, 0x11, 0x8d, 0x5e, 0x0f, 0xa7, 0x29, 0xce,
0xdf, 0x4f, 0xb3, 0x4d, 0x6a, 0x34, 0xf4, 0x63,
})
// MainNetParams define the lbrycrd network. See https://github.com/lbryio/lbrycrd/blob/master/src/chainparams.cpp
var MainNetParams = chaincfg.Params{
PubKeyHashAddrID: 0x55,
ScriptHashAddrID: 0x7a,
PrivateKeyID: 0x1c,
Bech32HRPSegwit: "lbc",
//WitnessPubKeyHashAddrID: , // i cant find these in bitcoin codebase either
//WitnessScriptHashAddrID:,
GenesisHash: &GenesisHash,
Name: "mainnet",
Net: wire.BitcoinNet(0xfae4aaf1),
DefaultPort: "9246",
BIP0034Height: 1,
BIP0065Height: 200000,
BIP0066Height: 200000,
}
const (
lbrycrdMainPubkeyPrefix = byte(85)
lbrycrdMainScriptPrefix = byte(122)
lbrycrdTestnetPubkeyPrefix = byte(111)
lbrycrdTestnetScriptPrefix = byte(196)
lbrycrdRegtestPubkeyPrefix = byte(111)
lbrycrdRegtestScriptPrefix = byte(196)
LbrycrdMain = "lbrycrd_main"
LbrycrdTestnet = "lbrycrd_testnet"
LbrycrdRegtest = "lbrycrd_regtest"
)
var mainNetParams = chaincfg.Params{
PubKeyHashAddrID: lbrycrdMainPubkeyPrefix,
ScriptHashAddrID: lbrycrdMainScriptPrefix,
PrivateKeyID: 0x1c,
}
var testNetParams = chaincfg.Params{
PubKeyHashAddrID: lbrycrdTestnetPubkeyPrefix,
ScriptHashAddrID: lbrycrdTestnetScriptPrefix,
PrivateKeyID: 0x1c,
Bech32HRPSegwit: "tlbc",
}
var regTestNetParams = chaincfg.Params{
PubKeyHashAddrID: lbrycrdRegtestPubkeyPrefix,
ScriptHashAddrID: lbrycrdRegtestScriptPrefix,
PrivateKeyID: 0x1c,
Bech32HRPSegwit: "rlbc",
}
var ChainParamsMap = map[string]chaincfg.Params{LbrycrdMain: mainNetParams, LbrycrdTestnet: testNetParams, LbrycrdRegtest: regTestNetParams}
func init() {
// Register lbrycrd network
err := chaincfg.Register(&MainNetParams)
if err != nil {
panic("failed to register lbrycrd network: " + err.Error())
}
}
// Client connects to a lbrycrd instance
type Client struct {
*rpcclient.Client
}
// New initializes a new Client
func New(lbrycrdURL string, chainParams string) (*Client, error) {
// Connect to local bitcoin core RPC server using HTTP POST mode.
u, err := url.Parse(lbrycrdURL)
if err != nil {
return nil, errors.WithStack(err)
}
if u.User == nil {
return nil, errors.WithStack(errors.New("no userinfo"))
}
password, _ := u.User.Password()
connCfg := &rpcclient.ConnConfig{
Host: u.Host,
User: u.User.Username(),
Pass: password,
Params: chainParams,
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
DisableTLS: true, // Bitcoin core does not provide TLS by default
}
// Notice the notification parameter is nil since notifications are not supported in HTTP POST mode.
client, err := rpcclient.New(connCfg, nil)
if err != nil {
return nil, errors.WithStack(err)
}
// make sure lbrycrd is running and responsive
_, err = client.GetBlockChainInfo()
if err != nil {
return nil, errors.WithStack(err)
}
return &Client{client}, nil
}
func NewWithDefaultURL() (*Client, error) {
url, err := getLbrycrdURLFromConfFile()
if err != nil {
return nil, err
}
return New(url, "")
}
var errInsufficientFunds = errors.New("insufficient funds")
// SimpleSend is a convenience function to send credits to an address (0 min confirmations)
func (c *Client) SimpleSend(toAddress string, amount float64) (*chainhash.Hash, error) {
decodedAddress, err := DecodeAddress(toAddress, &MainNetParams)
if err != nil {
return nil, errors.WithStack(err)
}
lbcAmount, err := lbcutil.NewAmount(amount)
if err != nil {
return nil, errors.WithStack(err)
}
hash, err := c.Client.SendToAddress(decodedAddress, lbcAmount)
if err != nil {
if err.Error() == "-6: Insufficient funds" {
err = errors.WithStack(errInsufficientFunds)
}
return nil, errors.WithStack(err)
}
return hash, nil
}
func getLbrycrdURLFromConfFile() (string, error) {
if os.Getenv("HOME") == "" {
return "", errors.WithStack(errors.New("no $HOME var found"))
}
defaultConfFile := os.Getenv("HOME") + "/.lbrycrd/lbrycrd.conf"
if os.Getenv("REGTEST") == "true" {
defaultConfFile = os.Getenv("HOME") + "/.lbrycrd_regtest/lbrycrd.conf"
}
if _, err := os.Stat(defaultConfFile); os.IsNotExist(err) {
return "", errors.WithStack(errors.New("default lbrycrd conf file not found"))
}
cfg, err := ini.Load(defaultConfFile)
if err != nil {
return "", errors.WithStack(err)
}
section, err := cfg.GetSection("")
if err != nil {
return "", errors.WithStack(err)
}
username := section.Key("rpcuser").String()
password := section.Key("rpcpassword").String()
host := section.Key("rpchost").String()
if host == "" {
host = "localhost"
}
port := section.Key("rpcport").String()
if port == "" {
port = strconv.Itoa(DefaultPort)
}
userpass := ""
if username != "" || password != "" {
userpass = username + ":" + password + "@"
}
return "rpc://" + userpass + host + ":" + port, nil
}
func (c *Client) CreateBaseRawTx(inputs []btcjson.TransactionInput, change float64) (*wire.MsgTx, error) {
addresses := make(map[lbcutil.Address]lbcutil.Amount)
changeAddress, err := c.GetNewAddress("")
if err != nil {
return nil, errors.WithStack(err)
}
changeAmount, err := lbcutil.NewAmount(change)
if err != nil {
return nil, errors.WithStack(err)
}
addresses[changeAddress] = changeAmount
lockTime := int64(0)
return c.CreateRawTransaction(inputs, addresses, &lockTime)
}
func (c *Client) GetEmptyTx(totalOutputSpend float64) (*wire.MsgTx, error) {
totalFees := 0.1
unspentResults, err := c.ListUnspentMin(1)
if err != nil {
return nil, errors.WithStack(err)
}
finder := newOutputFinder(unspentResults)
outputs, err := finder.nextBatch(totalOutputSpend + totalFees)
if err != nil {
return nil, err
}
if len(outputs) == 0 {
return nil, errors.WithStack(errors.New("Not enough spendable outputs to create transaction"))
}
inputs := make([]btcjson.TransactionInput, len(outputs))
var totalInputSpend float64
for i, output := range outputs {
inputs[i] = btcjson.TransactionInput{Txid: output.TxID, Vout: output.Vout}
totalInputSpend = totalInputSpend + output.Amount
}
change := totalInputSpend - totalOutputSpend - totalFees
return c.CreateBaseRawTx(inputs, change)
}
func (c *Client) SignTxAndSend(rawTx *wire.MsgTx) (*chainhash.Hash, error) {
signedTx, allInputsSigned, err := c.SignRawTransaction(rawTx)
if err != nil {
return nil, errors.WithStack(err)
}
if !allInputsSigned {
return nil, errors.WithStack(errors.New("Not all inputs for the tx could be signed!"))
}
return c.SendRawTransaction(signedTx, false)
}
type ScriptType int
const (
ClaimName ScriptType = iota
ClaimUpdate
ClaimSupport
)
func (c *Client) AddStakeToTx(rawTx *wire.MsgTx, claim *c.Helper, name string, claimAmount float64, scriptType ScriptType) error {
address, err := c.GetNewAddress("")
if err != nil {
return errors.WithStack(err)
}
amount, err := lbcutil.NewAmount(claimAmount)
if err != nil {
return errors.WithStack(err)
}
value, err := claim.CompileValue()
if err != nil {
return errors.WithStack(err)
}
var claimID string
if len(claim.ClaimID) > 0 {
claimID = hex.EncodeToString(rev(claim.ClaimID))
}
var script []byte
switch scriptType {
case ClaimName:
script, err = GetClaimNamePayoutScript(name, value, address)
if err != nil {
return errors.WithStack(err)
}
case ClaimUpdate:
script, err = GetUpdateClaimPayoutScript(name, claimID, value, address)
if err != nil {
return errors.WithStack(err)
}
case ClaimSupport:
script, err = GetUpdateClaimPayoutScript(name, claimID, value, address)
if err != nil {
return errors.WithStack(err)
}
}
rawTx.AddTxOut(wire.NewTxOut(int64(amount), script))
return nil
}
func (c *Client) CreateChannel(name string, amount float64) (*c.Helper, *btcec.PrivateKey, error) {
channel, key, err := NewChannel()
if err != nil {
return nil, nil, err
}
rawTx, err := c.GetEmptyTx(amount)
if err != nil {
return nil, nil, err
}
err = c.AddStakeToTx(rawTx, channel, name, amount, ClaimName)
if err != nil {
return nil, nil, err
}
_, err = c.SignTxAndSend(rawTx)
if err != nil {
return nil, nil, err
}
return channel, key, nil
}
func (c *Client) SupportClaim(name, claimID, address, blockchainName string, claimAmount float64) (*chainhash.Hash, error) {
const DefaultFeePerSupport = float64(0.0001)
unspentResults, err := c.ListUnspentMin(1)
if err != nil {
return nil, errors.WithStack(err)
}
finder := newOutputFinder(unspentResults)
outputs, err := finder.nextBatch(claimAmount + DefaultFeePerSupport)
if err != nil {
return nil, err
}
if len(outputs) == 0 {
return nil, errors.WithStack(errors.New("Not enough spendable outputs to create transaction"))
}
inputs := make([]btcjson.TransactionInput, len(outputs))
var totalInputSpend float64
for i, output := range outputs {
inputs[i] = btcjson.TransactionInput{Txid: output.TxID, Vout: output.Vout}
totalInputSpend = totalInputSpend + output.Amount
}
change := totalInputSpend - claimAmount - DefaultFeePerSupport
rawTx, err := c.CreateBaseRawTx(inputs, change)
if err != nil {
return nil, err
}
chainParams, ok := ChainParamsMap[blockchainName]
if !ok {
return nil, errors.WithStack(errors.Newf("invalid blockchain name %s", blockchainName))
}
decodedAddress, err := DecodeAddress(address, &chainParams)
if err != nil {
return nil, errors.WithStack(err)
}
amount, err := lbcutil.NewAmount(claimAmount)
if err != nil {
return nil, errors.WithStack(err)
}
script, err := GetClaimSupportPayoutScript(name, claimID, decodedAddress)
if err != nil {
return nil, errors.WithStack(err)
}
rawTx.AddTxOut(wire.NewTxOut(int64(amount), script))
return c.SignTxAndSend(rawTx)
}