forked from lightninglabs/taproot-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_anchor.go
176 lines (148 loc) · 5 KB
/
wallet_anchor.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
package taprootassets
import (
"bytes"
"context"
"fmt"
"math"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/taproot-assets/tapfreighter"
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
)
// LndRpcWalletAnchor is an implementation of the tapgarden.WalletAnchor
// interfaced backed by an active remote lnd node.
type LndRpcWalletAnchor struct {
lnd *lndclient.LndServices
}
// NewLndRpcWalletAnchor returns a new wallet anchor instance using the passed
// lnd node.
func NewLndRpcWalletAnchor(lnd *lndclient.LndServices) *LndRpcWalletAnchor {
return &LndRpcWalletAnchor{
lnd: lnd,
}
}
const (
// defaultChangeType is the default change type we'll use when using the
// PSBT APIs.
defaultChangeType = walletrpc.ChangeAddressType_CHANGE_ADDRESS_TYPE_P2TR
)
// FundPsbt attaches enough inputs to the target PSBT packet for it to be
// valid.
func (l *LndRpcWalletAnchor) FundPsbt(ctx context.Context, packet *psbt.Packet,
minConfs uint32, feeRate chainfee.SatPerKWeight) (tapgarden.FundedPsbt,
error) {
var psbtBuf bytes.Buffer
if err := packet.Serialize(&psbtBuf); err != nil {
return tapgarden.FundedPsbt{}, fmt.Errorf("unable to encode "+
"psbt: %w", err)
}
pkt, changeIndex, leasedUtxos, err := l.lnd.WalletKit.FundPsbt(
ctx, &walletrpc.FundPsbtRequest{
Template: &walletrpc.FundPsbtRequest_Psbt{
Psbt: psbtBuf.Bytes(),
},
Fees: &walletrpc.FundPsbtRequest_SatPerVbyte{
SatPerVbyte: uint64(feeRate.FeePerKVByte()) / 1000,
},
MinConfs: int32(minConfs),
ChangeType: defaultChangeType,
},
)
if err != nil {
return tapgarden.FundedPsbt{}, fmt.Errorf("unable to fund "+
"psbt: %w", err)
}
lockedUtxos := make([]wire.OutPoint, len(leasedUtxos))
for i, utxo := range leasedUtxos {
txid, err := chainhash.NewHash(utxo.Outpoint.TxidBytes)
if err != nil {
return tapgarden.FundedPsbt{}, err
}
lockedUtxos[i] = wire.OutPoint{
Hash: *txid,
Index: utxo.Outpoint.OutputIndex,
}
}
return tapgarden.FundedPsbt{
Pkt: pkt,
ChangeOutputIndex: changeIndex,
LockedUTXOs: lockedUtxos,
}, nil
}
// SignPsbt...
func (l *LndRpcWalletAnchor) SignPsbt(ctx context.Context,
packet *psbt.Packet) (*psbt.Packet, error) {
pkt, err := l.lnd.WalletKit.SignPsbt(ctx, packet)
if err != nil {
return nil, err
}
return pkt, nil
}
// SignAndFinalizePsbt fully signs and finalizes the target PSBT packet.
func (l *LndRpcWalletAnchor) SignAndFinalizePsbt(ctx context.Context,
pkt *psbt.Packet) (*psbt.Packet, error) {
pkt, _, err := l.lnd.WalletKit.FinalizePsbt(ctx, pkt, "")
if err != nil {
return nil, err
}
return pkt, nil
}
// ImportTaprootOutput imports a new public key into the wallet, as a P2TR
// output.
func (l *LndRpcWalletAnchor) ImportTaprootOutput(ctx context.Context,
pub *btcec.PublicKey) (btcutil.Address, error) {
addr, err := l.lnd.WalletKit.ImportTaprootScript(
ctx, &waddrmgr.Tapscript{
Type: waddrmgr.TaprootFullKeyOnly,
FullOutputKey: pub,
},
)
if err != nil {
return nil, err
}
return addr, nil
}
// UnlockInput unlocks the set of target inputs after a batch is abandoned.
func (l *LndRpcWalletAnchor) UnlockInput(ctx context.Context) error {
return nil
}
// ListUnspentImportScripts lists all UTXOs of the imported Taproot scripts.
func (l *LndRpcWalletAnchor) ListUnspentImportScripts(
ctx context.Context) ([]*lnwallet.Utxo, error) {
return l.lnd.WalletKit.ListUnspent(
ctx, 0, math.MaxInt32,
lndclient.WithUnspentAccount(waddrmgr.ImportedAddrAccountName),
)
}
// SubscribeTransactions creates a uni-directional stream from the server to the
// client in which any newly discovered transactions relevant to the wallet are
// sent over.
func (l *LndRpcWalletAnchor) SubscribeTransactions(
ctx context.Context) (<-chan lndclient.Transaction, <-chan error,
error) {
return l.lnd.Client.SubscribeTransactions(ctx)
}
// ListTransactions returns all known transactions of the backing lnd node. It
// takes a start and end block height which can be used to limit the block range
// that we query over. These values can be left as zero to include all blocks.
// To include unconfirmed transactions in the query, endHeight must be set to
// -1.
func (l *LndRpcWalletAnchor) ListTransactions(ctx context.Context, startHeight,
endHeight int32, account string) ([]lndclient.Transaction, error) {
return l.lnd.Client.ListTransactions(
ctx, startHeight, endHeight,
lndclient.WithTransactionsAccount(account),
)
}
// A compile time assertion to ensure LndRpcWalletAnchor meets the
// tapgarden.WalletAnchor interface.
var _ tapgarden.WalletAnchor = (*LndRpcWalletAnchor)(nil)
var _ tapfreighter.WalletAnchor = (*LndRpcWalletAnchor)(nil)