separate out channel init functionality into reservation
* Reservation has synchronous interface, wallet and only wallet should mutate any of the attributes
This commit is contained in:
parent
f775e130e3
commit
8583814f1f
@ -1 +1,91 @@
|
|||||||
package wallet
|
package wallet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
"github.com/btcsuite/btcutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChannelReservation...
|
||||||
|
type ChannelReservation struct {
|
||||||
|
FundingType FundingType
|
||||||
|
|
||||||
|
FundingAmount btcutil.Amount
|
||||||
|
ReserveAmount btcutil.Amount
|
||||||
|
MinFeePerKb btcutil.Amount
|
||||||
|
|
||||||
|
TheirInputs []*wire.TxIn
|
||||||
|
OurInputs []*wire.TxIn
|
||||||
|
|
||||||
|
TheirChange []*wire.TxOut
|
||||||
|
OurChange []*wire.TxOut
|
||||||
|
|
||||||
|
OurKey *btcec.PrivateKey
|
||||||
|
TheirKey *btcec.PublicKey
|
||||||
|
|
||||||
|
// In order of sorted inputs. Sorting is done in accordance
|
||||||
|
// to BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
|
||||||
|
TheirSigs [][]byte
|
||||||
|
OurSigs [][]byte
|
||||||
|
|
||||||
|
NormalizedTxID wire.ShaHash
|
||||||
|
|
||||||
|
FundingTx *wire.MsgTx
|
||||||
|
// TODO(roasbef): time locks, who pays fee etc.
|
||||||
|
// TODO(roasbeef): record Bob's ln-ID?
|
||||||
|
|
||||||
|
CompletedFundingTx *btcutil.Tx
|
||||||
|
|
||||||
|
reservationID uint64
|
||||||
|
wallet *LightningWallet
|
||||||
|
}
|
||||||
|
|
||||||
|
// newChannelReservation...
|
||||||
|
func newChannelReservation(t FundingType, fundingAmt btcutil.Amount,
|
||||||
|
minFeeRate btcutil.Amount, wallet *LightningWallet, id uint64) *ChannelReservation {
|
||||||
|
return &ChannelReservation{
|
||||||
|
FundingType: t,
|
||||||
|
FundingAmount: fundingAmt,
|
||||||
|
MinFeePerKb: minFeeRate,
|
||||||
|
wallet: wallet,
|
||||||
|
reservationID: id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddCounterPartyFunds...
|
||||||
|
func (r *ChannelReservation) AddFunds(theirInputs []*wire.TxIn, theirChangeOutputs []*wire.TxOut, multiSigKey *btcec.PublicKey) error {
|
||||||
|
errChan := make(chan error, 1)
|
||||||
|
|
||||||
|
r.wallet.msgChan <- &addCounterPartyFundsMsg{
|
||||||
|
pendingFundingID: r.reservationID,
|
||||||
|
theirInputs: theirInputs,
|
||||||
|
theirChangeOutputs: theirChangeOutputs,
|
||||||
|
theirKey: multiSigKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
return <-errChan
|
||||||
|
}
|
||||||
|
|
||||||
|
// CompleteFundingReservation...
|
||||||
|
func (r *ChannelReservation) CompleteReservation(reservationID uint64, theirSigs [][]byte) error {
|
||||||
|
errChan := make(chan error, 1)
|
||||||
|
|
||||||
|
r.wallet.msgChan <- &addCounterPartySigsMsg{
|
||||||
|
pendingFundingID: reservationID,
|
||||||
|
theirSigs: theirSigs,
|
||||||
|
err: errChan,
|
||||||
|
}
|
||||||
|
|
||||||
|
return <-errChan
|
||||||
|
}
|
||||||
|
|
||||||
|
// RequestFundingReserveCancellation...
|
||||||
|
func (r *ChannelReservation) Cancel(reservationID uint64) {
|
||||||
|
doneChan := make(chan struct{}, 1)
|
||||||
|
r.wallet.msgChan <- &fundingReserveCancelMsg{
|
||||||
|
pendingFundingID: reservationID,
|
||||||
|
done: doneChan,
|
||||||
|
}
|
||||||
|
|
||||||
|
<-doneChan
|
||||||
|
}
|
||||||
|
261
wallet/wallet.go
261
wallet/wallet.go
@ -47,45 +47,9 @@ const (
|
|||||||
CLTV_RESERVE
|
CLTV_RESERVE
|
||||||
)
|
)
|
||||||
|
|
||||||
// partialFundingState...
|
|
||||||
type partialFundingState struct {
|
|
||||||
fundingType FundingType
|
|
||||||
|
|
||||||
fundingAmount btcutil.Amount
|
|
||||||
reserveAmount btcutil.Amount
|
|
||||||
minFeePerKb btcutil.Amount
|
|
||||||
|
|
||||||
theirInputs []*wire.TxIn
|
|
||||||
ourInputs []*wire.TxIn
|
|
||||||
|
|
||||||
theirChange []*wire.TxOut
|
|
||||||
ourChange []*wire.TxOut
|
|
||||||
|
|
||||||
ourKey *btcec.PrivateKey
|
|
||||||
theirKey *btcec.PublicKey
|
|
||||||
|
|
||||||
theirSigs [][]byte
|
|
||||||
ourSigs [][]byte
|
|
||||||
|
|
||||||
normalizedTxID wire.ShaHash
|
|
||||||
|
|
||||||
fundingTx *wire.MsgTx
|
|
||||||
// TODO(roasbef): time locks, who pays fee etc.
|
|
||||||
// TODO(roasbeef): record Bob's ln-ID?
|
|
||||||
}
|
|
||||||
|
|
||||||
// newPartialFundingState...
|
|
||||||
func newPartialFundingState(t FundingType, fundingAmt btcutil.Amount, minFeeRate btcutil.Amount) *partialFundingState {
|
|
||||||
return &partialFundingState{
|
|
||||||
fundingType: t,
|
|
||||||
fundingAmount: fundingAmt,
|
|
||||||
minFeePerKb: minFeeRate,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// completedFundingState...
|
// completedFundingState...
|
||||||
type completedFundingState struct {
|
type completedFundingState struct {
|
||||||
partialFundingState
|
finalizedReservation *ChannelReservation
|
||||||
|
|
||||||
regularTxID wire.ShaHash
|
regularTxID wire.ShaHash
|
||||||
}
|
}
|
||||||
@ -101,20 +65,7 @@ type initFundingReserveMsg struct {
|
|||||||
// Insuffcient funds etc..
|
// Insuffcient funds etc..
|
||||||
err chan error // Buffered
|
err chan error // Buffered
|
||||||
|
|
||||||
resp chan *FundingReservation // Buffered
|
resp chan *ChannelReservation // Buffered
|
||||||
}
|
|
||||||
|
|
||||||
// FundingResponse...
|
|
||||||
type FundingReservation struct {
|
|
||||||
FundingInputs []*wire.TxIn
|
|
||||||
ChangeOutputs []*wire.TxOut
|
|
||||||
|
|
||||||
FundingAmount btcutil.Amount
|
|
||||||
|
|
||||||
// To be used in the 2-of-2 output.
|
|
||||||
OurKey *btcec.PrivateKey
|
|
||||||
|
|
||||||
ReservationID uint64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FundingReserveCancelMsg...
|
// FundingReserveCancelMsg...
|
||||||
@ -135,13 +86,11 @@ type addCounterPartyFundsMsg struct {
|
|||||||
theirChangeOutputs []*wire.TxOut
|
theirChangeOutputs []*wire.TxOut
|
||||||
theirKey *btcec.PublicKey
|
theirKey *btcec.PublicKey
|
||||||
|
|
||||||
err chan error // Buffered
|
err chan error // Buffered
|
||||||
resp chan *PartiallySignedFundingState // Buffered
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PartiallySignedFundingState struct {
|
// partiallySignedFundingState...
|
||||||
ReservationID uint64
|
type partiallySignedFundingState struct {
|
||||||
|
|
||||||
// In order of sorted inputs that are ours. Sorting is done in accordance
|
// In order of sorted inputs that are ours. Sorting is done in accordance
|
||||||
// to BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
|
// to BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
|
||||||
OurSigs [][]byte
|
OurSigs [][]byte
|
||||||
@ -157,12 +106,11 @@ type addCounterPartySigsMsg struct {
|
|||||||
// to BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
|
// to BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
|
||||||
theirSigs [][]byte
|
theirSigs [][]byte
|
||||||
|
|
||||||
err chan error // Buffered
|
err chan error // Buffered
|
||||||
resp chan *FinalizedFundingState // Buffered
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FundingCompleteResp...
|
// FundingCompleteResp...
|
||||||
type FinalizedFundingState struct {
|
type finalizedFundingState struct {
|
||||||
FundingTxId wire.ShaHash
|
FundingTxId wire.ShaHash
|
||||||
NormalizedFundingTXID wire.ShaHash
|
NormalizedFundingTXID wire.ShaHash
|
||||||
|
|
||||||
@ -191,7 +139,7 @@ type LightningWallet struct {
|
|||||||
// lost-object/starvation problem/attack.
|
// lost-object/starvation problem/attack.
|
||||||
limboMtx sync.RWMutex
|
limboMtx sync.RWMutex
|
||||||
nextFundingID uint64 // TODO(roasbeef): monotonic or random?
|
nextFundingID uint64 // TODO(roasbeef): monotonic or random?
|
||||||
fundingLimbo map[uint64]*partialFundingState
|
fundingLimbo map[uint64]*ChannelReservation
|
||||||
|
|
||||||
started int32
|
started int32
|
||||||
shutdown int32
|
shutdown int32
|
||||||
@ -256,23 +204,10 @@ out:
|
|||||||
l.wg.Done()
|
l.wg.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
// nextMultiSigKey...
|
|
||||||
// TODO(roasbeef): on shutdown, write state of pending keys, then read back?
|
|
||||||
func (l *LightningWallet) getNextMultiSigKey() (*btcec.PrivateKey, error) {
|
|
||||||
nextAddr, err := l.Manager.NextExternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
pkAddr := nextAddr[0].(waddrmgr.ManagedPubKeyAddress)
|
|
||||||
|
|
||||||
return pkAddr.PrivKey()
|
|
||||||
}
|
|
||||||
|
|
||||||
// RequestFundingReservation...
|
// RequestFundingReservation...
|
||||||
func (l *LightningWallet) RequestFundingReservation(a btcutil.Amount, t FundingType) (*FundingReservation, error) {
|
func (l *LightningWallet) RequestChannelReservation(a btcutil.Amount, t FundingType) (*ChannelReservation, error) {
|
||||||
errChan := make(chan error, 1)
|
errChan := make(chan error, 1)
|
||||||
respChan := make(chan *FundingReservation, 1)
|
respChan := make(chan *ChannelReservation, 1)
|
||||||
|
|
||||||
l.msgChan <- &initFundingReserveMsg{
|
l.msgChan <- &initFundingReserveMsg{
|
||||||
fundingAmount: a,
|
fundingAmount: a,
|
||||||
@ -287,11 +222,13 @@ func (l *LightningWallet) RequestFundingReservation(a btcutil.Amount, t FundingT
|
|||||||
// handleFundingReserveRequest...
|
// handleFundingReserveRequest...
|
||||||
func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg) {
|
func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg) {
|
||||||
// Create a limbo and record entry for this newly pending funding request.
|
// Create a limbo and record entry for this newly pending funding request.
|
||||||
partialState := newPartialFundingState(req.fundingType, req.fundingAmount, req.minFeeRate)
|
|
||||||
l.limboMtx.Lock()
|
l.limboMtx.Lock()
|
||||||
|
|
||||||
id := l.nextFundingID
|
id := l.nextFundingID
|
||||||
|
partialState := newChannelReservation(req.fundingType, req.fundingAmount, req.minFeeRate, l, id)
|
||||||
l.nextFundingID++
|
l.nextFundingID++
|
||||||
l.fundingLimbo[id] = partialState
|
l.fundingLimbo[id] = partialState
|
||||||
|
|
||||||
l.limboMtx.Unlock()
|
l.limboMtx.Unlock()
|
||||||
|
|
||||||
// Find all unlocked unspent outputs with greater than 6 confirmations.
|
// Find all unlocked unspent outputs with greater than 6 confirmations.
|
||||||
@ -299,7 +236,6 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
unspentOutputs, err := l.ListUnspent(6, maxConfs, nil)
|
unspentOutputs, err := l.ListUnspent(6, maxConfs, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- err
|
req.err <- err
|
||||||
req.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -307,7 +243,6 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
coins, err := outputsToCoins(unspentOutputs)
|
coins, err := outputsToCoins(unspentOutputs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- err
|
req.err <- err
|
||||||
req.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -327,7 +262,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
// Lock the selected coins. These coins are now "reserved", this
|
// Lock the selected coins. These coins are now "reserved", this
|
||||||
// prevents concurrent funding requests from referring to and this
|
// prevents concurrent funding requests from referring to and this
|
||||||
// double-spending the same set of coins.
|
// double-spending the same set of coins.
|
||||||
partialState.ourInputs = make([]*wire.TxIn, len(selectedCoins.Coins()))
|
partialState.OurInputs = make([]*wire.TxIn, len(selectedCoins.Coins()))
|
||||||
for i, coin := range selectedCoins.Coins() {
|
for i, coin := range selectedCoins.Coins() {
|
||||||
txout := wire.NewOutPoint(coin.Hash(), coin.Index())
|
txout := wire.NewOutPoint(coin.Hash(), coin.Index())
|
||||||
l.LockOutpoint(*txout)
|
l.LockOutpoint(*txout)
|
||||||
@ -335,12 +270,12 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
// Empty sig script, we'll actually sign if this reservation is
|
// Empty sig script, we'll actually sign if this reservation is
|
||||||
// queued up to be completed (the other side accepts).
|
// queued up to be completed (the other side accepts).
|
||||||
outPoint := wire.NewOutPoint(coin.Hash(), coin.Index())
|
outPoint := wire.NewOutPoint(coin.Hash(), coin.Index())
|
||||||
partialState.ourInputs[i] = wire.NewTxIn(outPoint, nil)
|
partialState.OurInputs[i] = wire.NewTxIn(outPoint, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create some possibly neccessary change outputs.
|
// Create some possibly neccessary change outputs.
|
||||||
selectedTotalValue := coinset.NewCoinSet(coins).TotalValue()
|
selectedTotalValue := coinset.NewCoinSet(coins).TotalValue()
|
||||||
partialState.ourChange = make([]*wire.TxOut, 0, len(selectedCoins.Coins()))
|
partialState.OurChange = make([]*wire.TxOut, 0, len(selectedCoins.Coins()))
|
||||||
if selectedTotalValue > req.fundingAmount {
|
if selectedTotalValue > req.fundingAmount {
|
||||||
// Change is necessary. Query for an available change address to
|
// Change is necessary. Query for an available change address to
|
||||||
// send the remainder to.
|
// send the remainder to.
|
||||||
@ -348,11 +283,10 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
changeAddr, err := l.NewChangeAddress(waddrmgr.DefaultAccountNum)
|
changeAddr, err := l.NewChangeAddress(waddrmgr.DefaultAccountNum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- err
|
req.err <- err
|
||||||
req.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
partialState.ourChange = append(partialState.ourChange,
|
partialState.OurChange = append(partialState.OurChange,
|
||||||
wire.NewTxOut(int64(changeAmount), changeAddr.ScriptAddress()))
|
wire.NewTxOut(int64(changeAmount), changeAddr.ScriptAddress()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -361,31 +295,15 @@ func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg
|
|||||||
multiSigKey, err := l.getNextMultiSigKey()
|
multiSigKey, err := l.getNextMultiSigKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- err
|
req.err <- err
|
||||||
req.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
partialState.OurKey = multiSigKey
|
||||||
|
|
||||||
// Funding reservation request succesfully handled. The funding inputs
|
// Funding reservation request succesfully handled. The funding inputs
|
||||||
// will be marked as unavailable until the reservation is either
|
// will be marked as unavailable until the reservation is either
|
||||||
// completed, or cancecled.
|
// completed, or cancecled.
|
||||||
req.err <- nil
|
req.err <- nil
|
||||||
req.resp <- &FundingReservation{
|
|
||||||
FundingInputs: partialState.ourInputs,
|
|
||||||
ChangeOutputs: partialState.ourChange,
|
|
||||||
FundingAmount: partialState.fundingAmount,
|
|
||||||
OurKey: multiSigKey,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RequestFundingReserveCancellation...
|
|
||||||
func (l *LightningWallet) CancelFundingReservation(reservationID uint64) {
|
|
||||||
doneChan := make(chan struct{}, 1)
|
|
||||||
l.msgChan <- &fundingReserveCancelMsg{
|
|
||||||
pendingFundingID: reservationID,
|
|
||||||
done: doneChan,
|
|
||||||
}
|
|
||||||
|
|
||||||
<-doneChan
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleFundingReserveCancel...
|
// handleFundingReserveCancel...
|
||||||
@ -404,7 +322,7 @@ func (l *LightningWallet) handleFundingCancelRequest(req *fundingReserveCancelMs
|
|||||||
|
|
||||||
// Mark all previously locked outpoints as usuable for future funding
|
// Mark all previously locked outpoints as usuable for future funding
|
||||||
// requests.
|
// requests.
|
||||||
for _, unusedInput := range pendingReservation.ourInputs {
|
for _, unusedInput := range pendingReservation.OurInputs {
|
||||||
l.UnlockOutpoint(unusedInput.PreviousOutPoint)
|
l.UnlockOutpoint(unusedInput.PreviousOutPoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,27 +337,6 @@ func (l *LightningWallet) handleFundingCancelRequest(req *fundingReserveCancelMs
|
|||||||
req.done <- struct{}{}
|
req.done <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddCounterPartyFunds...
|
|
||||||
// TODO(roasbeef): abstract out "Reservations" to own struct, with ref to wallet?
|
|
||||||
func (l *LightningWallet) AddCounterPartyFundsForReservation(
|
|
||||||
reservationId uint64,
|
|
||||||
theirInputs []*wire.TxIn,
|
|
||||||
theirChangeOutputs []*wire.TxOut,
|
|
||||||
multiSigKey *btcec.PublicKey) (*FinalizedFundingState, error) {
|
|
||||||
|
|
||||||
errChan := make(chan error, 1)
|
|
||||||
respChan := make(chan *FinalizedFundingState, 1)
|
|
||||||
|
|
||||||
l.msgChan <- &addCounterPartyFundsMsg{
|
|
||||||
pendingFundingID: reservationId,
|
|
||||||
theirInputs: theirInputs,
|
|
||||||
theirChangeOutputs: theirChangeOutputs,
|
|
||||||
theirKey: multiSigKey,
|
|
||||||
}
|
|
||||||
|
|
||||||
return <-respChan, <-errChan
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleFundingCounterPartyFunds...
|
// handleFundingCounterPartyFunds...
|
||||||
func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFundsMsg) {
|
func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFundsMsg) {
|
||||||
l.RLock()
|
l.RLock()
|
||||||
@ -452,60 +349,59 @@ func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFun
|
|||||||
|
|
||||||
// Create a blank, fresh transaction. Soon to be a complete funding
|
// Create a blank, fresh transaction. Soon to be a complete funding
|
||||||
// transaction which will allow opening a lightning channel.
|
// transaction which will allow opening a lightning channel.
|
||||||
pendingReservation.fundingTx = wire.NewMsgTx()
|
pendingReservation.FundingTx = wire.NewMsgTx()
|
||||||
|
|
||||||
// First, add all multi-party inputs to the transaction
|
// First, add all multi-party inputs to the transaction
|
||||||
// TODO(roasbeef); handle case that tx doesn't exist, fake input
|
// TODO(roasbeef); handle case that tx doesn't exist, fake input
|
||||||
// TODO(roasbeef): validate SPV proof from other side if in SPV mode.
|
// TODO(roasbeef): validate SPV proof from other side if in SPV mode.
|
||||||
for _, ourInput := range pendingReservation.ourInputs {
|
for _, ourInput := range pendingReservation.OurInputs {
|
||||||
pendingReservation.fundingTx.AddTxIn(ourInput)
|
pendingReservation.FundingTx.AddTxIn(ourInput)
|
||||||
}
|
}
|
||||||
pendingReservation.theirInputs = req.theirInputs
|
pendingReservation.TheirInputs = req.theirInputs
|
||||||
for _, theirInput := range pendingReservation.theirInputs {
|
for _, theirInput := range pendingReservation.TheirInputs {
|
||||||
pendingReservation.fundingTx.AddTxIn(theirInput)
|
pendingReservation.FundingTx.AddTxIn(theirInput)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next, add all multi-party outputs to the transaction. This includes
|
// Next, add all multi-party outputs to the transaction. This includes
|
||||||
// change outputs for both side.
|
// change outputs for both side.
|
||||||
for _, ourChangeOutput := range pendingReservation.ourChange {
|
for _, ourChangeOutput := range pendingReservation.OurChange {
|
||||||
pendingReservation.fundingTx.AddTxOut(ourChangeOutput)
|
pendingReservation.FundingTx.AddTxOut(ourChangeOutput)
|
||||||
}
|
}
|
||||||
pendingReservation.theirChange = req.theirChangeOutputs
|
pendingReservation.TheirChange = req.theirChangeOutputs
|
||||||
for _, theirChangeOutput := range pendingReservation.theirChange {
|
for _, theirChangeOutput := range pendingReservation.TheirChange {
|
||||||
pendingReservation.fundingTx.AddTxOut(theirChangeOutput)
|
pendingReservation.FundingTx.AddTxOut(theirChangeOutput)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, add the 2-of-2 multi-sig output which will set up the lightning
|
// Finally, add the 2-of-2 multi-sig output which will set up the lightning
|
||||||
// channel. TODO(roasbeef): Cannonical sorting of keys here?
|
// channel. TODO(roasbeef): Cannonical sorting of keys here?
|
||||||
keys := make([]*btcutil.AddressPubKey, 2)
|
keys := make([]*btcutil.AddressPubKey, 2)
|
||||||
ourKey := pendingReservation.ourKey.PubKey().SerializeCompressed()
|
ourKey := pendingReservation.OurKey.PubKey().SerializeCompressed()
|
||||||
keys[0], _ = btcutil.NewAddressPubKey(ourKey, ActiveNetParams)
|
keys[0], _ = btcutil.NewAddressPubKey(ourKey, ActiveNetParams)
|
||||||
pendingReservation.theirKey = req.theirKey
|
pendingReservation.TheirKey = req.theirKey
|
||||||
keys[1], _ = btcutil.NewAddressPubKey(pendingReservation.theirKey.SerializeCompressed(), ActiveNetParams)
|
keys[1], _ = btcutil.NewAddressPubKey(pendingReservation.TheirKey.SerializeCompressed(), ActiveNetParams)
|
||||||
multiSigScript, err := txscript.MultiSigScript(keys, 2)
|
multiSigScript, err := txscript.MultiSigScript(keys, 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- err
|
req.err <- err
|
||||||
req.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
multiSigOut := wire.NewTxOut(int64(pendingReservation.fundingAmount),
|
multiSigOut := wire.NewTxOut(int64(pendingReservation.FundingAmount),
|
||||||
multiSigScript)
|
multiSigScript)
|
||||||
pendingReservation.fundingTx.AddTxOut(multiSigOut)
|
pendingReservation.FundingTx.AddTxOut(multiSigOut)
|
||||||
|
|
||||||
// Sort the transaction. Since both side agree to a cannonical
|
// Sort the transaction. Since both side agree to a cannonical
|
||||||
// ordering, by sorting we no longer need to send the entire
|
// ordering, by sorting we no longer need to send the entire
|
||||||
// transaction. Only signatures will be exchanged.
|
// transaction. Only signatures will be exchanged.
|
||||||
txsort.InPlaceSort(pendingReservation.fundingTx)
|
txsort.InPlaceSort(pendingReservation.FundingTx)
|
||||||
|
|
||||||
// Now that the transaction has been cannonically sorted, compute the
|
// Now that the transaction has been cannonically sorted, compute the
|
||||||
// normalized transation ID before we attach our signatures.
|
// normalized transation ID before we attach our signatures.
|
||||||
// TODO(roasbeef): this isn't the normalized txid, this isn't recursive
|
// TODO(roasbeef): this isn't the normalized txid, this isn't recursive
|
||||||
pendingReservation.normalizedTxID = pendingReservation.fundingTx.TxSha()
|
pendingReservation.NormalizedTxID = pendingReservation.FundingTx.TxSha()
|
||||||
|
|
||||||
// Now, sign all inputs that are ours, collecting the signatures in
|
// Now, sign all inputs that are ours, collecting the signatures in
|
||||||
// order of the inputs.
|
// order of the inputs.
|
||||||
pendingReservation.ourSigs = make([][]byte, len(pendingReservation.ourInputs))
|
pendingReservation.OurSigs = make([][]byte, len(pendingReservation.OurInputs))
|
||||||
for i, txIn := range pendingReservation.fundingTx.TxIn {
|
for i, txIn := range pendingReservation.FundingTx.TxIn {
|
||||||
// Does the wallet know about the txin?
|
// Does the wallet know about the txin?
|
||||||
txDetail, _ := l.TxStore.TxDetails(&txIn.PreviousOutPoint.Hash)
|
txDetail, _ := l.TxStore.TxDetails(&txIn.PreviousOutPoint.Hash)
|
||||||
if txDetail == nil {
|
if txDetail == nil {
|
||||||
@ -519,102 +415,83 @@ func (l *LightningWallet) handleFundingCounterPartyFunds(req *addCounterPartyFun
|
|||||||
apkh, ok := addrs[0].(*btcutil.AddressPubKeyHash)
|
apkh, ok := addrs[0].(*btcutil.AddressPubKeyHash)
|
||||||
if !ok {
|
if !ok {
|
||||||
req.err <- btcwallet.ErrUnsupportedTransactionType
|
req.err <- btcwallet.ErrUnsupportedTransactionType
|
||||||
req.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ai, err := l.Manager.Address(apkh)
|
ai, err := l.Manager.Address(apkh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- fmt.Errorf("cannot get address info: %v", err)
|
req.err <- fmt.Errorf("cannot get address info: %v", err)
|
||||||
req.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pka := ai.(waddrmgr.ManagedPubKeyAddress)
|
pka := ai.(waddrmgr.ManagedPubKeyAddress)
|
||||||
privkey, err := pka.PrivKey()
|
privkey, err := pka.PrivKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- fmt.Errorf("cannot get private key: %v", err)
|
req.err <- fmt.Errorf("cannot get private key: %v", err)
|
||||||
req.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sigscript, err := txscript.SignatureScript(pendingReservation.fundingTx, i,
|
sigscript, err := txscript.SignatureScript(pendingReservation.FundingTx, i,
|
||||||
prevOut.PkScript, txscript.SigHashAll, privkey,
|
prevOut.PkScript, txscript.SigHashAll, privkey,
|
||||||
ai.Compressed())
|
ai.Compressed())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.err <- fmt.Errorf("cannot create sigscript: %s", err)
|
req.err <- fmt.Errorf("cannot create sigscript: %s", err)
|
||||||
req.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pendingReservation.fundingTx.TxIn[i].SignatureScript = sigscript
|
pendingReservation.FundingTx.TxIn[i].SignatureScript = sigscript
|
||||||
pendingReservation.ourSigs[i] = sigscript
|
pendingReservation.OurSigs[i] = sigscript
|
||||||
}
|
|
||||||
|
|
||||||
// Import the key we're using for the 2-of-2 multi-sig into the wallet,
|
|
||||||
// so we can sign to spend from the funding tx later.
|
|
||||||
// TODO(roasbeef): remove this after drawing pool key from HD.
|
|
||||||
wif, _ := btcutil.NewWIF(pendingReservation.ourKey, ActiveNetParams, true)
|
|
||||||
if _, err := l.ImportPrivateKey(wif, nil, false); err != nil {
|
|
||||||
req.err <- err
|
|
||||||
req.resp <- nil
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req.err <- nil
|
req.err <- nil
|
||||||
req.resp <- &PartiallySignedFundingState{
|
|
||||||
OurSigs: pendingReservation.ourSigs,
|
|
||||||
NormalizedTxID: pendingReservation.normalizedTxID,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CompleteFundingReservation...
|
|
||||||
func (l *LightningWallet) CompleteFundingReservation(reservationID uint64, theirSigs [][]byte) (*FinalizedFundingState, error) {
|
|
||||||
errChan := make(chan error, 1)
|
|
||||||
respChan := make(chan *FinalizedFundingState, 1)
|
|
||||||
|
|
||||||
l.msgChan <- &addCounterPartySigsMsg{
|
|
||||||
pendingFundingID: reservationID,
|
|
||||||
theirSigs: theirSigs,
|
|
||||||
err: errChan,
|
|
||||||
resp: respChan,
|
|
||||||
}
|
|
||||||
|
|
||||||
return <-respChan, <-errChan
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleFundingCounterPartySigs...
|
// handleFundingCounterPartySigs...
|
||||||
func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigsMsg) {
|
func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigsMsg) {
|
||||||
l.RLock()
|
l.limboMtx.RLock()
|
||||||
pendingReservation, ok := l.fundingLimbo[msg.pendingFundingID]
|
pendingReservation, ok := l.fundingLimbo[msg.pendingFundingID]
|
||||||
l.RUnlock()
|
l.limboMtx.RUnlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
msg.err <- fmt.Errorf("attempted to update non-existant funding state")
|
msg.err <- fmt.Errorf("attempted to update non-existant funding state")
|
||||||
msg.resp <- nil
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we can complete the funding transaction by adding their
|
// Now we can complete the funding transaction by adding their
|
||||||
// signatures to their inputs.
|
// signatures to their inputs.
|
||||||
i := 0
|
i := 0
|
||||||
pendingReservation.theirSigs = msg.theirSigs
|
pendingReservation.TheirSigs = msg.theirSigs
|
||||||
for _, txin := range pendingReservation.fundingTx.TxIn {
|
for _, txin := range pendingReservation.FundingTx.TxIn {
|
||||||
if txin.SignatureScript == nil {
|
if txin.SignatureScript == nil {
|
||||||
// TODO(roasbeef): use txscript.Engine to make sure each sig is
|
// TODO(roasbeef): use txscript.Engine to make sure each sig is
|
||||||
// valid, txn complete.
|
// valid, txn complete.
|
||||||
txin.SignatureScript = pendingReservation.theirSigs[i]
|
txin.SignatureScript = pendingReservation.TheirSigs[i]
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(roasbeef): write the funding transaction to disk
|
||||||
// Now that all signatures are in place and valid record the regular txid
|
// Now that all signatures are in place and valid record the regular txid
|
||||||
finalTxID := pendingReservation.fundingTx.TxSha()
|
//completedReservation := &finalizedFundingState{
|
||||||
|
// FundingTxId: pendingReservation.fundingTx.TxSha(),
|
||||||
|
// NormalizedFundingTXID: pendingReservation.normalizedTxID,
|
||||||
|
// CompletedFundingTx: btcutil.NewTx(pendingReservation.fundingTx),
|
||||||
|
//}
|
||||||
|
|
||||||
|
l.limboMtx.Lock()
|
||||||
|
delete(l.fundingLimbo, pendingReservation.reservationID)
|
||||||
|
l.limboMtx.Unlock()
|
||||||
|
|
||||||
msg.err <- nil
|
msg.err <- nil
|
||||||
msg.resp <- &FinalizedFundingState{
|
}
|
||||||
FundingTxId: finalTxID,
|
|
||||||
NormalizedFundingTXID: pendingReservation.normalizedTxID,
|
// nextMultiSigKey...
|
||||||
CompletedFundingTx: btcutil.NewTx(pendingReservation.fundingTx),
|
// TODO(roasbeef): on shutdown, write state of pending keys, then read back?
|
||||||
|
func (l *LightningWallet) getNextMultiSigKey() (*btcec.PrivateKey, error) {
|
||||||
|
nextAddr, err := l.Manager.NextExternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(roasbeef): write the funding transaction to disk, delete from pending
|
pkAddr := nextAddr[0].(waddrmgr.ManagedPubKeyAddress)
|
||||||
|
|
||||||
|
return pkAddr.PrivKey()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user