-
Notifications
You must be signed in to change notification settings - Fork 25
/
chain.go
90 lines (80 loc) · 2.51 KB
/
chain.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
package account
import (
"context"
"errors"
breezservice "github.com/breez/breez/breez"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
)
const (
defaultSatPerByteFee = 50
)
/*
ValidateAddress validates a bitcoin address based on the network type
*/
func (a *Service) ValidateAddress(address string) error {
var network *chaincfg.Params
if a.cfg.Network == "testnet" {
network = &chaincfg.TestNet3Params
} else if a.cfg.Network == "simnet" {
network = &chaincfg.SimNetParams
} else if a.cfg.Network == "mainnet" {
network = &chaincfg.MainNetParams
} else {
return errors.New("unknown network type " + a.cfg.Network)
}
addr, err := btcutil.DecodeAddress(address, network)
if err != nil {
a.log.Errorf("Error parsing %s as address\t", address)
return err
}
if _, ok := addr.(*btcutil.AddressPubKey); ok {
a.log.Errorf("Using pay-to-pubkey address %s is considered as an error\t", address)
return btcutil.ErrUnknownAddressType
}
return nil
}
/*
SendWalletCoins executes a request to send wallet coins to a particular address.
*/
func (a *Service) SendWalletCoins(address string, satPerByteFee int64) (string, error) {
lnclient := a.daemonAPI.APIClient()
res, err := lnclient.SendCoins(context.Background(), &lnrpc.SendCoinsRequest{
Addr: address, SatPerByte: satPerByteFee, SendAll: true})
if err != nil {
return "", err
}
return res.Txid, nil
}
/*
GetDefaultSatPerByteFee returns the default sat per byte fee for on chain transactions
*/
func (a *Service) GetDefaultSatPerByteFee() (int64, error) {
walletKityClient := a.daemonAPI.WalletKitClient()
if walletKityClient == nil {
return 0, errors.New("API not ready")
}
feeResponse, err := walletKityClient.EstimateFee(context.Background(),
&walletrpc.EstimateFeeRequest{ConfTarget: 6})
if err != nil {
return 0, err
}
return int64(chainfee.SatPerKWeight(feeResponse.SatPerKw).FeePerKVByte() / 1000), nil
}
/*
RegisterPeriodicSync registeres this token for periodic sync notifications.
*/
func (a *Service) RegisterPeriodicSync(token string) error {
c, ctx, cancel := a.breezAPI.NewSyncNotifierClient()
defer cancel()
_, err := c.RegisterPeriodicSync(ctx, &breezservice.RegisterPeriodicSyncRequest{NotificationToken: token})
if err != nil {
a.log.Errorf("fail to register for periodic sync: %v", err)
} else {
a.log.Info("registered successfuly for periodic sync")
}
return err
}