-
Notifications
You must be signed in to change notification settings - Fork 7
/
rpc.go
78 lines (67 loc) · 2.37 KB
/
rpc.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
package main
import (
"strings"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
)
//go:generate mockery --name rpcClient --exported
type rpcClient interface {
CreateWallet(
name string,
opts ...rpcclient.CreateWalletOpt,
) (*btcjson.CreateWalletResult, error)
LoadWallet(name string) (*btcjson.LoadWalletResult, error)
GenerateToAddress(
numBlocks int64,
address btcutil.Address,
maxTries *int64,
) ([]*chainhash.Hash, error)
GetBalance(account string) (btcutil.Amount, error)
GetNewAddress(account string) (btcutil.Address, error)
GetWalletInfo() (*btcjson.GetWalletInfoResult, error)
SendToAddress(address btcutil.Address, amount btcutil.Amount) (*chainhash.Hash, error)
}
func initialiseBitcoind(client rpcClient) error {
walletInfo, err := client.GetWalletInfo()
if err != nil && !strings.Contains(err.Error(), "No wallet is loaded") {
return errors.Wrap(err, "Getting wallet info")
}
if walletInfo == nil || walletInfo.WalletName == "" {
log.Info().Msg("No wallet loaded, trying to load scalinglightning wallet")
loadWalletResult, err := client.LoadWallet(walletName)
if err != nil {
log.Info().Msg("Couldn't load scalinglightning wallet, trying to create it")
if err != nil {
log.Info().Msgf("Load wallet err was: %v", err.Error())
}
_, err := client.CreateWallet(walletName)
if err != nil {
return errors.Wrap(err, "Creating bitcoind wallet")
}
}
if loadWalletResult != nil && loadWalletResult.Warning != "" {
log.Info().Msgf("Load wallet warning was: %v", loadWalletResult.Warning)
}
}
walletInfo, err = client.GetWalletInfo()
if err != nil {
return errors.Wrap(err, "Getting wallet info, wallet should exist by now")
}
log.Info().Msgf("Loaded wallet: %v", walletInfo.WalletName)
address, err := client.GetNewAddress(walletInfo.WalletName)
if err != nil {
return errors.Wrap(err, "Getting new address")
}
log.Info().Msgf("New address created to receive mined coins: %v", address.String())
// maxTries := int64(1000000)
_, err = client.GenerateToAddress(1000, address, nil)
if err != nil {
return errors.Wrapf(err, "Generating blocks to address: %v", address.String())
}
log.Info().Msg("Bitcoind is setup and ready to serve clients")
return nil
}