forked from mit-dci/lit
/
basewallet.go
137 lines (107 loc) · 4.7 KB
/
basewallet.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
package qln
import (
"fmt"
"github.com/rjected/lit/btcutil/chaincfg/chainhash"
"github.com/rjected/lit/coinparam"
"github.com/rjected/lit/crypto/koblitz"
"github.com/rjected/lit/lnutil"
"github.com/rjected/lit/portxo"
"github.com/rjected/lit/uspv"
"github.com/rjected/lit/wire"
)
// The UWallet interface are the functions needed to work with the LnNode
// Verbs are from the perspective of the LnNode, not the underlying wallet
type UWallet interface {
// Ask for a pubkey based on a bip32 path
GetPub(k portxo.KeyGen) *koblitz.PublicKey
// Have GetPriv for now. Maybe later get rid of this and have
// the underlying wallet sign?
GetPriv(k portxo.KeyGen) (*koblitz.PrivateKey, error)
// Send a tx out to the network. Maybe could replace? Maybe not.
// Needed for channel break / cooperative close. Maybe grabs.
// export the chainhook that the UWallet uses, for pushTx and fullblock
ExportHook() uspv.ChainHook
PushTx(tx *wire.MsgTx) error
// ExportUtxo gives a utxo to the underlying wallet; that wallet saves it
// and can spend it later. Doesn't return errors; error will exist only in
// base wallet.
ExportUtxo(txo *portxo.PorTxo)
// MaybeSend makes an unsigned tx, populated with inputs and outputs.
// The specified txouts are in there somewhere.
// Only segwit txins are in the generated tx. (txid won't change)
// There's probably an extra change txout in there which is OK.
// The inputs are "frozen" until ReallySend / NahDontSend / program restart.
// Retruns the txid, and then the txout indexes of the specified txos.
// The outpoints returned will all have the same hash (txid)
// So if you (as usual) just give one txo, you basically get back an outpoint.
MaybeSend(txos []*wire.TxOut, onlyWit bool) ([]*wire.OutPoint, error)
// ReallySend really sends the transaction specified previously in MaybeSend.
// Underlying wallet does all needed signing.
// Once you call ReallySend, the outpoint is tracked and responses are
// sent through LetMeKnow
ReallySend(txid *chainhash.Hash) error
// NahDontSend cancels the MaybeSend transaction.
NahDontSend(txid *chainhash.Hash) error
// Return a new address
NewAdr() ([20]byte, error)
// Dump all the utxos in the sub wallet
UtxoDump() ([]*portxo.PorTxo, error)
// Dump all the addresses the sub wallet is watching
AdrDump() ([][20]byte, error)
// Return current height the wallet is synced to
CurrentHeight() int32
// This is redundand... just use UtxoDump and figure it out yourself.
// Feels like helper functions shouldn't be in the interface.
// how much utxo the wallet has -- only confirmed segwit outputs
// HowMuchWitConf() int64
// How much utxo the sub wallet has, including non-segwit, unconfirmed, immature
// HowMuchTotal() int64
// WatchThis tells the basewallet to watch an outpoint
WatchThis(wire.OutPoint) error
// StopWatchingThis tells the basewallet to stop watching an outpoint
StopWatchingThis(wire.OutPoint) error
// LetMeKnow opens the chan where OutPointEvent flows from the underlying
// wallet up to the LN module.
LetMeKnow() chan lnutil.OutPointEvent
// LetMeKnowHeight opens the chan where the blockheight flows from the underlying
// wallet up to the LN module. Used for monitoring HTLC timeouts
LetMeKnowHeight() chan lnutil.HeightEvent
// Ask for network parameters
Params() *coinparam.Params
// Get current fee rate.
Fee() int64
// Set fee rate
SetFee(int64) int64
// ===== TESTING / SPAMMING ONLY, these funcs will not be in the real interface
// Sweep sends lots of txs (uint32 of them) to the specified address.
Sweep([]byte, uint32) ([]*chainhash.Hash, error)
PickUtxos(amtWanted, outputByteSize,
feePerByte int64, ow bool) (portxo.TxoSliceByBip69, int64, error)
SignMyInputs(tx *wire.MsgTx) error
DirectSendTx(tx *wire.MsgTx) error
}
// GetUsePub gets a pubkey from the base wallet, but first modifies
// the "use" step
func (nd *LitNode) GetUsePub(k portxo.KeyGen, use uint32) (pubArr [33]byte, err error) {
coin := k.Step[1] & 0x7fffffff // de-assert MSB
if nd.SubWallet[coin] == nil {
err = fmt.Errorf("coin type %d not in wallet", k.Step[1]&0x7fffffff)
return // fail if that wallet isn't attached
}
k.Step[2] = use
pub := nd.SubWallet[coin].GetPub(k)
copy(pubArr[:], pub.SerializeCompressed())
return
}
// GetElkremRoot returns the Elkrem root for a given key path
// gets the use-pub for elkrems and hashes it.
// A little weird because it's a "pub" key you shouldn't reveal.
// either do this or export privkeys... or signing empty txs or something.
func (nd *LitNode) GetElkremRoot(k portxo.KeyGen) (chainhash.Hash, error) {
pubArr, err := nd.GetUsePub(k, UseChannelElkrem)
if err != nil {
var empty [32]byte
return empty, err
}
return chainhash.DoubleHashH(pubArr[:]), nil
}