forked from lightninglabs/taproot-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_bridge.go
86 lines (70 loc) · 2.47 KB
/
chain_bridge.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
package taro
import (
"context"
"fmt"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/taro/tarogarden"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
)
// LndRpcChainBridge is an implementation of the tarogarden.ChainBridge
// interface backed by an active remote lnd node.
type LndRpcChainBridge struct {
lnd *lndclient.LndServices
}
// NewLndRpcChainBridge creates a new chain bridge from an active lnd services
// client.
func NewLndRpcChainBridge(lnd *lndclient.LndServices) *LndRpcChainBridge {
return &LndRpcChainBridge{
lnd: lnd,
}
}
// RegisterConfirmationsNtfn registers an intent to be notified once
// txid reaches numConfs confirmations.
func (l *LndRpcChainBridge) RegisterConfirmationsNtfn(ctx context.Context,
txid *chainhash.Hash, pkScript []byte, numConfs, heightHint uint32,
includeBlock bool) (*chainntnfs.ConfirmationEvent, chan error, error) {
var opts []lndclient.NotifierOption
if includeBlock {
opts = append(opts, lndclient.WithIncludeBlock())
}
ctx, cancel := context.WithCancel(ctx) // nolint:govet
confChan, errChan, err := l.lnd.ChainNotifier.RegisterConfirmationsNtfn(
ctx, txid, pkScript, int32(numConfs), int32(heightHint),
opts...,
)
if err != nil {
cancel()
return nil, nil, fmt.Errorf("unable to register for conf: %w",
err)
}
return &chainntnfs.ConfirmationEvent{
Confirmed: confChan,
Cancel: cancel,
}, errChan, nil
}
// CurrentHeight return the current height of the main chain.
func (l *LndRpcChainBridge) CurrentHeight(ctx context.Context) (uint32, error) {
info, err := l.lnd.Client.GetInfo(ctx)
if err != nil {
return 0, fmt.Errorf("unable to grab block height: %w", err)
}
return info.BlockHeight, nil
}
// PublishTransaction attempts to publish a new transaction to the
// network.
func (l *LndRpcChainBridge) PublishTransaction(ctx context.Context,
tx *wire.MsgTx) error {
label := "tarod-asset-minting"
return l.lnd.WalletKit.PublishTransaction(ctx, tx, label)
}
// EstimateFee returns a fee estimate for the confirmation target.
func (l *LndRpcChainBridge) EstimateFee(ctx context.Context,
confTarget uint32) (chainfee.SatPerKWeight, error) {
return l.lnd.WalletKit.EstimateFeeRate(ctx, int32(confTarget))
}
// A compile time assertion to ensure LndRpcChainBridge meets the
// tarogarden.ChainBridge interface.
var _ tarogarden.ChainBridge = (*LndRpcChainBridge)(nil)