2015-12-20 09:00:50 +03:00
|
|
|
package lnwallet
|
2015-10-28 01:50:30 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-11-05 23:36:19 +03:00
|
|
|
"fmt"
|
2015-11-29 03:16:00 +03:00
|
|
|
"math"
|
2015-11-19 01:38:57 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-10-28 01:50:30 +03:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2015-12-24 21:42:03 +03:00
|
|
|
"li.lan/labs/plasma/shachain"
|
2015-12-21 02:13:14 +03:00
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2015-11-05 23:36:19 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2015-10-28 01:50:30 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcutil/coinset"
|
2015-11-05 23:36:19 +03:00
|
|
|
"github.com/btcsuite/btcutil/txsort"
|
2015-11-27 09:48:42 +03:00
|
|
|
"github.com/btcsuite/btcwallet/chain"
|
2015-10-28 01:50:30 +03:00
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
|
|
|
btcwallet "github.com/btcsuite/btcwallet/wallet"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
|
|
|
)
|
|
|
|
|
2015-11-13 06:34:35 +03:00
|
|
|
const (
|
|
|
|
msgBufferSize = 100
|
|
|
|
)
|
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
var (
|
|
|
|
// Error types
|
2015-11-05 23:36:19 +03:00
|
|
|
ErrInsufficientFunds = errors.New("not enough available outputs to " +
|
|
|
|
"create funding transaction")
|
|
|
|
|
|
|
|
// Which bitcoin network are we using?
|
|
|
|
ActiveNetParams = &chaincfg.TestNet3Params
|
|
|
|
)
|
|
|
|
|
|
|
|
type FundingType uint16
|
|
|
|
|
|
|
|
const (
|
2015-12-16 22:04:51 +03:00
|
|
|
// Use SegWit, assumes CSV+CLTV
|
|
|
|
SEGWIT FundingType = iota
|
2015-11-05 23:36:19 +03:00
|
|
|
|
2015-12-16 22:04:51 +03:00
|
|
|
// Use SIGHASH_NOINPUT, assumes CSV+CLTV
|
|
|
|
SIGHASH
|
|
|
|
|
|
|
|
// Use CSV without reserve
|
2015-11-05 23:36:19 +03:00
|
|
|
CSV
|
|
|
|
|
|
|
|
// Use CSV with reserve
|
|
|
|
// Reserve is a permanent amount of funds locked and the capacity.
|
|
|
|
CSV_RESERVE
|
|
|
|
|
|
|
|
// CLTV with reserve.
|
|
|
|
CLTV_RESERVE
|
2015-10-28 01:50:30 +03:00
|
|
|
)
|
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// initFundingReserveReq...
|
|
|
|
type initFundingReserveMsg struct {
|
2015-10-28 01:50:30 +03:00
|
|
|
fundingAmount btcutil.Amount
|
2015-11-05 23:36:19 +03:00
|
|
|
fundingType FundingType
|
|
|
|
minFeeRate btcutil.Amount
|
|
|
|
|
2015-12-26 09:05:07 +03:00
|
|
|
nodeID [32]byte
|
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// TODO(roasbeef): optional reserve for CLTV, etc.
|
2015-10-28 01:50:30 +03:00
|
|
|
|
|
|
|
// Insuffcient funds etc..
|
|
|
|
err chan error // Buffered
|
|
|
|
|
2015-11-13 05:43:32 +03:00
|
|
|
resp chan *ChannelReservation // Buffered
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// FundingReserveCancelMsg...
|
2015-11-05 23:36:19 +03:00
|
|
|
type fundingReserveCancelMsg struct {
|
|
|
|
pendingFundingID uint64
|
|
|
|
|
|
|
|
// Buffered, used for optionally synchronization.
|
2015-12-16 23:40:44 +03:00
|
|
|
err chan error // Buffered
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 00:53:34 +03:00
|
|
|
// addContributionMsg...
|
|
|
|
type addContributionMsg struct {
|
2015-11-05 23:36:19 +03:00
|
|
|
pendingFundingID uint64
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// TODO(roasbeef): Should also carry SPV proofs in we're in SPV mode
|
2015-12-23 07:31:17 +03:00
|
|
|
contribution *ChannelContribution
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-11-13 05:43:32 +03:00
|
|
|
err chan error // Buffered
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
|
|
|
|
2015-11-13 05:43:32 +03:00
|
|
|
// partiallySignedFundingState...
|
|
|
|
type partiallySignedFundingState struct {
|
2015-11-05 23:36:19 +03:00
|
|
|
// 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.
|
|
|
|
OurSigs [][]byte
|
|
|
|
|
|
|
|
NormalizedTxID wire.ShaHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// addCounterPartySigsMsg...
|
|
|
|
type addCounterPartySigsMsg struct {
|
|
|
|
pendingFundingID uint64
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// Should be order of sorted inputs that are theirs. Sorting is done in accordance
|
|
|
|
// to BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
|
2015-12-22 00:54:33 +03:00
|
|
|
theirFundingSigs [][]byte
|
|
|
|
|
|
|
|
// This should be 1/2 of the signatures needed to succesfully spend our
|
|
|
|
// version of the commitment transaction.
|
|
|
|
theirCommitmentSig []byte
|
2015-11-05 23:36:19 +03:00
|
|
|
|
2015-11-13 05:43:32 +03:00
|
|
|
err chan error // Buffered
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// FundingCompleteResp...
|
2015-11-13 05:43:32 +03:00
|
|
|
type finalizedFundingState struct {
|
2015-11-05 23:36:19 +03:00
|
|
|
FundingTxId wire.ShaHash
|
|
|
|
NormalizedFundingTXID wire.ShaHash
|
|
|
|
|
|
|
|
CompletedFundingTx *btcutil.Tx
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// LightningWallet....
|
|
|
|
// responsible for internal global (from the point of view of a user/node)
|
|
|
|
// channel state. Requests to modify this state come in via messages over
|
|
|
|
// channels, same with replies.
|
|
|
|
// Embedded wallet backed by boltdb...
|
|
|
|
type LightningWallet struct {
|
2015-11-19 01:38:57 +03:00
|
|
|
// TODO(roasbeef): add btcwallet/chain for notifications initially, then
|
|
|
|
// abstract out in order to accomodate zeroMQ/BitcoinCore
|
2015-11-27 09:48:20 +03:00
|
|
|
lmtx sync.RWMutex
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-11-13 06:34:35 +03:00
|
|
|
db walletdb.DB
|
2015-10-28 01:50:30 +03:00
|
|
|
|
|
|
|
// A namespace within boltdb reserved for ln-based wallet meta-data.
|
|
|
|
// TODO(roasbeef): which possible other namespaces are relevant?
|
2015-11-13 06:34:35 +03:00
|
|
|
lnNamespace walletdb.Namespace
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-12-03 03:51:46 +03:00
|
|
|
wallet *btcwallet.Wallet
|
|
|
|
rpc *chain.Client
|
2015-10-28 01:50:30 +03:00
|
|
|
|
|
|
|
msgChan chan interface{}
|
|
|
|
|
2015-11-05 23:34:11 +03:00
|
|
|
// TODO(roasbeef): zombie garbage collection routine to solve
|
|
|
|
// lost-object/starvation problem/attack.
|
|
|
|
limboMtx sync.RWMutex
|
2015-11-13 06:34:35 +03:00
|
|
|
nextFundingID uint64
|
2015-11-13 05:43:32 +03:00
|
|
|
fundingLimbo map[uint64]*ChannelReservation
|
2015-11-05 23:34:11 +03:00
|
|
|
|
2015-11-19 01:38:57 +03:00
|
|
|
started int32
|
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
shutdown int32
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
2015-11-19 01:38:57 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): handle wallet lock/unlock
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-11-05 23:34:11 +03:00
|
|
|
// NewLightningWallet...
|
2015-11-27 09:46:59 +03:00
|
|
|
// TODO(roasbeef): fin...add config
|
|
|
|
func NewLightningWallet(privWalletPass, pubWalletPass, hdSeed []byte, dataDir string) (*LightningWallet, error) {
|
2015-11-19 01:38:57 +03:00
|
|
|
// Ensure the wallet exists or create it when the create flag is set.
|
2015-11-27 09:48:42 +03:00
|
|
|
netDir := networkDir(dataDir, ActiveNetParams)
|
2015-11-19 01:38:57 +03:00
|
|
|
dbPath := filepath.Join(netDir, walletDbName)
|
|
|
|
|
|
|
|
var pubPass []byte
|
|
|
|
if pubWalletPass == nil {
|
|
|
|
pubPass = defaultPubPassphrase
|
|
|
|
} else {
|
|
|
|
pubPass = pubWalletPass
|
2015-11-13 06:34:35 +03:00
|
|
|
}
|
|
|
|
|
2015-11-19 01:38:57 +03:00
|
|
|
// Wallet has never been created, perform initial set up.
|
|
|
|
if !fileExists(dbPath) {
|
|
|
|
// Ensure the data directory for the network exists.
|
|
|
|
if err := checkCreateDir(netDir); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to create a new wallet
|
2015-11-27 09:46:59 +03:00
|
|
|
if err := createWallet(privWalletPass, pubPass, hdSeed, dbPath); err != nil {
|
2015-11-19 01:38:57 +03:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-13 06:34:35 +03:00
|
|
|
}
|
|
|
|
|
2015-11-19 01:38:57 +03:00
|
|
|
// Wallet has been created and been initialized at this point, open it
|
|
|
|
// along with all the required DB namepsaces, and the DB itself.
|
2015-11-27 09:46:59 +03:00
|
|
|
wallet, db, err := openWallet(pubPass, netDir)
|
2015-11-13 06:34:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:38:57 +03:00
|
|
|
// Create a special namespace for our unique payment channel related
|
|
|
|
// meta-data.
|
|
|
|
lnNamespace, err := db.Namespace(lightningNamespaceKey)
|
2015-11-13 06:34:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:38:57 +03:00
|
|
|
// TODO(roasbeef): logging
|
|
|
|
|
2015-11-13 06:34:35 +03:00
|
|
|
return &LightningWallet{
|
2015-12-16 01:18:43 +03:00
|
|
|
db: db,
|
|
|
|
wallet: wallet,
|
|
|
|
lnNamespace: lnNamespace,
|
|
|
|
msgChan: make(chan interface{}, msgBufferSize),
|
|
|
|
// TODO(roasbeef): make this atomic.Uint32 instead? Which is
|
|
|
|
// faster, locks or CAS? I'm guessing CAS because assembly:
|
|
|
|
// * https://golang.org/src/sync/atomic/asm_amd64.s
|
2015-11-13 06:34:35 +03:00
|
|
|
nextFundingID: 0,
|
|
|
|
fundingLimbo: make(map[uint64]*ChannelReservation),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}, nil
|
2015-11-05 23:34:11 +03:00
|
|
|
}
|
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
// Start...
|
|
|
|
func (l *LightningWallet) Start() error {
|
|
|
|
// Already started?
|
|
|
|
if atomic.AddInt32(&l.started, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-27 09:48:42 +03:00
|
|
|
// TODO(roasbeef): config...
|
|
|
|
|
|
|
|
rpcc, err := chain.NewClient(ActiveNetParams,
|
|
|
|
"host", "user", "pass", []byte("cert"), true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the goroutines in the underlying wallet.
|
2015-12-03 03:51:46 +03:00
|
|
|
l.rpc = rpcc
|
2015-11-27 09:48:42 +03:00
|
|
|
l.wallet.Start(rpcc)
|
2015-10-28 01:50:30 +03:00
|
|
|
|
|
|
|
l.wg.Add(1)
|
2015-11-27 09:50:17 +03:00
|
|
|
// TODO(roasbeef): multiple request handlers?
|
2015-10-28 01:50:30 +03:00
|
|
|
go l.requestHandler()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop...
|
|
|
|
func (l *LightningWallet) Stop() error {
|
|
|
|
if atomic.AddInt32(&l.shutdown, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-28 03:07:22 +03:00
|
|
|
l.wallet.Stop()
|
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
close(l.quit)
|
|
|
|
l.wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// requestHandler....
|
|
|
|
func (l *LightningWallet) requestHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-l.msgChan:
|
|
|
|
switch msg := m.(type) {
|
2015-11-05 23:36:19 +03:00
|
|
|
case *initFundingReserveMsg:
|
2015-10-28 01:50:30 +03:00
|
|
|
l.handleFundingReserveRequest(msg)
|
2015-11-05 23:36:19 +03:00
|
|
|
case *fundingReserveCancelMsg:
|
2015-10-28 01:50:30 +03:00
|
|
|
l.handleFundingCancelRequest(msg)
|
2015-12-22 00:53:34 +03:00
|
|
|
case *addContributionMsg:
|
|
|
|
l.handleContributionMsg(msg)
|
2015-11-05 23:36:19 +03:00
|
|
|
case *addCounterPartySigsMsg:
|
|
|
|
l.handleFundingCounterPartySigs(msg)
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
case <-l.quit:
|
|
|
|
// TODO: do some clean up
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.wg.Done()
|
|
|
|
}
|
|
|
|
|
2015-12-23 07:31:17 +03:00
|
|
|
// InitChannelReservation...
|
2015-12-24 21:42:29 +03:00
|
|
|
// fields set after completion:
|
|
|
|
// * ourInputs
|
|
|
|
// * ourChange
|
|
|
|
// * ourMultisigKey
|
|
|
|
// * ourCommitKey
|
|
|
|
// * ourDeliveryAddress
|
|
|
|
// * ourShaChain
|
2015-12-26 09:05:07 +03:00
|
|
|
func (l *LightningWallet) InitChannelReservation(a btcutil.Amount, t FundingType, theirID [32]byte) (*ChannelReservation, error) {
|
2015-11-05 23:36:19 +03:00
|
|
|
errChan := make(chan error, 1)
|
2015-11-13 05:43:32 +03:00
|
|
|
respChan := make(chan *ChannelReservation, 1)
|
2015-11-05 23:36:19 +03:00
|
|
|
|
|
|
|
l.msgChan <- &initFundingReserveMsg{
|
|
|
|
fundingAmount: a,
|
|
|
|
fundingType: t,
|
2015-12-26 09:05:07 +03:00
|
|
|
nodeID: theirID,
|
2015-11-05 23:36:19 +03:00
|
|
|
err: errChan,
|
|
|
|
resp: respChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
return <-respChan, <-errChan
|
|
|
|
}
|
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
// handleFundingReserveRequest...
|
2015-11-05 23:36:19 +03:00
|
|
|
func (l *LightningWallet) handleFundingReserveRequest(req *initFundingReserveMsg) {
|
|
|
|
// Create a limbo and record entry for this newly pending funding request.
|
|
|
|
l.limboMtx.Lock()
|
2015-11-13 05:43:32 +03:00
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
id := l.nextFundingID
|
2015-12-19 06:39:51 +03:00
|
|
|
reservation := newChannelReservation(req.fundingType, req.fundingAmount, req.minFeeRate, l, id)
|
2015-11-05 23:36:19 +03:00
|
|
|
l.nextFundingID++
|
2015-12-19 06:39:51 +03:00
|
|
|
l.fundingLimbo[id] = reservation
|
2015-11-13 05:43:32 +03:00
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
l.limboMtx.Unlock()
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-11-14 22:52:07 +03:00
|
|
|
// Grab the mutex on the ChannelReservation to ensure thead-safety
|
2015-12-19 06:39:51 +03:00
|
|
|
reservation.Lock()
|
|
|
|
defer reservation.Unlock()
|
2015-11-14 22:52:07 +03:00
|
|
|
|
2015-12-26 09:05:07 +03:00
|
|
|
reservation.partialState.TheirLNID = req.nodeID
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution := reservation.ourContribution
|
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
// Find all unlocked unspent outputs with greater than 6 confirmations.
|
2015-12-23 07:31:17 +03:00
|
|
|
// TODO(roasbeef): make 6 a config paramter?
|
2015-11-29 02:28:48 +03:00
|
|
|
maxConfs := int32(math.MaxInt32)
|
2015-11-27 09:48:20 +03:00
|
|
|
unspentOutputs, err := l.wallet.ListUnspent(6, maxConfs, nil)
|
2015-10-28 01:50:30 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
2015-11-27 09:50:17 +03:00
|
|
|
req.resp <- nil
|
2015-10-28 01:50:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the outputs to coins for coin selection below.
|
|
|
|
coins, err := outputsToCoins(unspentOutputs)
|
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
2015-11-27 09:50:17 +03:00
|
|
|
req.resp <- nil
|
2015-10-28 01:50:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peform coin selection over our available, unlocked unspent outputs
|
|
|
|
// in order to find enough coins to meet the funding amount requirements.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): Should extend coinset with optimal coin selection
|
|
|
|
// heuristics for our use case.
|
|
|
|
// TODO(roasbeef): factor in fees..
|
2015-11-27 09:51:01 +03:00
|
|
|
// TODO(roasbeef): possibly integrate the fee prediction project? if
|
|
|
|
// results hold up...
|
2015-10-28 01:50:30 +03:00
|
|
|
// NOTE: this current selection assumes "priority" is still a thing.
|
|
|
|
selector := &coinset.MaxValueAgeCoinSelector{
|
|
|
|
MaxInputs: 10,
|
|
|
|
MinChangeAmount: 10000,
|
|
|
|
}
|
2015-11-05 23:36:19 +03:00
|
|
|
selectedCoins, err := selector.CoinSelect(req.fundingAmount, coins)
|
2015-11-27 09:50:17 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
req.resp <- nil
|
|
|
|
return
|
|
|
|
}
|
2015-10-28 01:50:30 +03:00
|
|
|
|
|
|
|
// Lock the selected coins. These coins are now "reserved", this
|
|
|
|
// prevents concurrent funding requests from referring to and this
|
|
|
|
// double-spending the same set of coins.
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution.Inputs = make([]*wire.TxIn, len(selectedCoins.Coins()))
|
2015-10-28 01:50:30 +03:00
|
|
|
for i, coin := range selectedCoins.Coins() {
|
|
|
|
txout := wire.NewOutPoint(coin.Hash(), coin.Index())
|
2015-11-27 09:48:20 +03:00
|
|
|
l.wallet.LockOutpoint(*txout)
|
2015-10-28 01:50:30 +03:00
|
|
|
|
|
|
|
// Empty sig script, we'll actually sign if this reservation is
|
|
|
|
// queued up to be completed (the other side accepts).
|
|
|
|
outPoint := wire.NewOutPoint(coin.Hash(), coin.Index())
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution.Inputs[i] = wire.NewTxIn(outPoint, nil)
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create some possibly neccessary change outputs.
|
2015-11-29 03:16:00 +03:00
|
|
|
selectedTotalValue := coinset.NewCoinSet(selectedCoins.Coins()).TotalValue()
|
2015-11-05 23:36:19 +03:00
|
|
|
if selectedTotalValue > req.fundingAmount {
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution.ChangeOutputs = make([]*wire.TxOut, 1)
|
2015-10-28 01:50:30 +03:00
|
|
|
// Change is necessary. Query for an available change address to
|
|
|
|
// send the remainder to.
|
2015-11-05 23:36:19 +03:00
|
|
|
changeAmount := selectedTotalValue - req.fundingAmount
|
2015-11-29 03:16:00 +03:00
|
|
|
addrs, err := l.wallet.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
2015-10-28 01:50:30 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
2015-11-27 09:50:17 +03:00
|
|
|
req.resp <- nil
|
2015-11-05 23:36:19 +03:00
|
|
|
return
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
2015-12-16 00:23:56 +03:00
|
|
|
changeAddrScript, err := txscript.PayToAddrScript(addrs[0].Address())
|
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
req.resp <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO(roasbeef): re-enable after tests are connected to real node.
|
|
|
|
// * or the change to btcwallet is made to reverse the dependancy
|
|
|
|
// between chain-client and wallet.
|
2015-11-29 23:19:47 +03:00
|
|
|
//changeAddr, err := l.wallet.NewChangeAddress(waddrmgr.DefaultAccountNum)
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution.ChangeOutputs[0] = wire.NewTxOut(int64(changeAmount),
|
|
|
|
changeAddrScript)
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-11-13 06:34:35 +03:00
|
|
|
// TODO(roasbeef): re-calculate fees here to minFeePerKB, may need more inputs
|
2015-11-05 23:36:19 +03:00
|
|
|
|
2015-12-21 02:13:14 +03:00
|
|
|
// TODO(roasbeef): use wallet.CurrentAddress() here instead? Solves the
|
|
|
|
// problem of 'wasted' unused addrtesses.
|
|
|
|
// Grab two fresh keys from out HD chain, one will be used for the
|
|
|
|
// multi-sig funding transaction, and the other for the commitment
|
|
|
|
// transaction.
|
|
|
|
multiSigKey, err := l.getNextRawKey()
|
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
req.resp <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
commitKey, err := l.getNextRawKey()
|
2015-11-05 23:36:19 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
2015-11-27 09:50:17 +03:00
|
|
|
req.resp <- nil
|
2015-11-05 23:36:19 +03:00
|
|
|
return
|
|
|
|
}
|
2015-12-24 21:41:15 +03:00
|
|
|
reservation.partialState.MultiSigKey = multiSigKey
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution.MultiSigKey = multiSigKey.PubKey()
|
2015-12-24 21:41:15 +03:00
|
|
|
reservation.partialState.OurCommitKey = commitKey
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution.CommitKey = commitKey.PubKey()
|
2015-12-21 02:13:14 +03:00
|
|
|
|
|
|
|
// Generate a fresh address to be used in the case of a cooperative
|
|
|
|
// channel close.
|
|
|
|
// TODO(roasbeef): same here
|
|
|
|
//deliveryAddress, err := l.wallet.NewChangeAddress(waddrmgr.DefaultAccountNum)
|
|
|
|
addrs, err := l.wallet.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(roasbeef): make into func sendErorr()
|
|
|
|
req.err <- err
|
|
|
|
req.resp <- nil
|
|
|
|
return
|
|
|
|
}
|
2015-12-24 21:41:15 +03:00
|
|
|
reservation.partialState.OurDeliveryAddress = addrs[0].Address()
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution.DeliveryAddress = addrs[0].Address()
|
2015-12-21 02:13:14 +03:00
|
|
|
|
|
|
|
// Create a new shaChain for verifiable transaction revocations.
|
2015-12-24 21:42:03 +03:00
|
|
|
shaChain, err := shachain.NewFromSeed(nil, 0)
|
2015-12-21 02:13:14 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
req.resp <- nil
|
|
|
|
return
|
|
|
|
}
|
2015-12-24 21:41:15 +03:00
|
|
|
reservation.partialState.OurShaChain = shaChain
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution.RevocationHash = shaChain.CurrentRevocationHash()
|
2015-11-13 05:43:32 +03:00
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
// Funding reservation request succesfully handled. The funding inputs
|
|
|
|
// will be marked as unavailable until the reservation is either
|
|
|
|
// completed, or cancecled.
|
2015-12-19 06:39:51 +03:00
|
|
|
req.resp <- reservation
|
2015-10-28 01:50:30 +03:00
|
|
|
req.err <- nil
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleFundingReserveCancel...
|
|
|
|
func (l *LightningWallet) handleFundingCancelRequest(req *fundingReserveCancelMsg) {
|
|
|
|
// TODO(roasbeef): holding lock too long
|
2015-12-16 22:04:51 +03:00
|
|
|
// RLOCK?
|
2015-11-05 23:36:19 +03:00
|
|
|
l.limboMtx.Lock()
|
|
|
|
defer l.limboMtx.Unlock()
|
|
|
|
|
|
|
|
pendingReservation, ok := l.fundingLimbo[req.pendingFundingID]
|
|
|
|
if !ok {
|
|
|
|
// TODO(roasbeef): make new error, "unkown funding state" or something
|
|
|
|
req.err <- fmt.Errorf("attempted to cancel non-existant funding state")
|
|
|
|
return
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
2015-11-05 23:36:19 +03:00
|
|
|
|
2015-11-14 22:52:07 +03:00
|
|
|
// Grab the mutex on the ChannelReservation to ensure thead-safety
|
|
|
|
pendingReservation.Lock()
|
|
|
|
defer pendingReservation.Unlock()
|
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// Mark all previously locked outpoints as usuable for future funding
|
|
|
|
// requests.
|
2015-12-23 07:31:17 +03:00
|
|
|
for _, unusedInput := range pendingReservation.ourContribution.Inputs {
|
2015-11-27 09:48:20 +03:00
|
|
|
l.wallet.UnlockOutpoint(unusedInput.PreviousOutPoint)
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
|
|
|
|
2015-11-13 04:45:09 +03:00
|
|
|
// TODO(roasbeef): is it even worth it to keep track of unsed keys?
|
2015-11-05 23:36:19 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): Is it possible to mark the unused change also as
|
|
|
|
// available?
|
|
|
|
|
|
|
|
delete(l.fundingLimbo, req.pendingFundingID)
|
|
|
|
|
|
|
|
req.err <- nil
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// handleFundingCounterPartyFunds...
|
2015-12-22 00:53:34 +03:00
|
|
|
func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
|
2015-11-27 09:48:20 +03:00
|
|
|
l.limboMtx.Lock()
|
2015-11-05 23:36:19 +03:00
|
|
|
pendingReservation, ok := l.fundingLimbo[req.pendingFundingID]
|
2015-11-27 09:48:20 +03:00
|
|
|
l.limboMtx.Unlock()
|
2015-11-05 23:36:19 +03:00
|
|
|
if !ok {
|
|
|
|
req.err <- fmt.Errorf("attempted to update non-existant funding state")
|
|
|
|
return
|
|
|
|
}
|
2015-11-05 23:32:15 +03:00
|
|
|
|
2015-11-14 22:52:07 +03:00
|
|
|
// Grab the mutex on the ChannelReservation to ensure thead-safety
|
|
|
|
pendingReservation.Lock()
|
|
|
|
defer pendingReservation.Unlock()
|
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// Create a blank, fresh transaction. Soon to be a complete funding
|
|
|
|
// transaction which will allow opening a lightning channel.
|
2015-12-24 21:41:15 +03:00
|
|
|
pendingReservation.partialState.FundingTx = wire.NewMsgTx()
|
|
|
|
fundingTx := pendingReservation.partialState.FundingTx
|
2015-12-23 07:31:17 +03:00
|
|
|
|
|
|
|
pendingReservation.theirContribution = req.contribution
|
|
|
|
theirContribution := req.contribution
|
|
|
|
ourContribution := pendingReservation.ourContribution
|
2015-11-05 23:32:15 +03:00
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// First, add all multi-party inputs to the transaction
|
|
|
|
// TODO(roasbeef); handle case that tx doesn't exist, fake input
|
|
|
|
// TODO(roasbeef): validate SPV proof from other side if in SPV mode.
|
2015-11-27 09:51:01 +03:00
|
|
|
// * actually, pure SPV would need fraud proofs right? must prove input
|
|
|
|
// is unspent
|
|
|
|
// * or, something like getutxo?
|
2015-12-23 07:31:17 +03:00
|
|
|
for _, ourInput := range ourContribution.Inputs {
|
|
|
|
fundingTx.AddTxIn(ourInput)
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
2015-12-23 07:31:17 +03:00
|
|
|
for _, theirInput := range theirContribution.Inputs {
|
|
|
|
fundingTx.AddTxIn(theirInput)
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// Next, add all multi-party outputs to the transaction. This includes
|
|
|
|
// change outputs for both side.
|
2015-12-23 07:31:17 +03:00
|
|
|
for _, ourChangeOutput := range ourContribution.ChangeOutputs {
|
|
|
|
fundingTx.AddTxOut(ourChangeOutput)
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
2015-12-23 07:31:17 +03:00
|
|
|
for _, theirChangeOutput := range theirContribution.ChangeOutputs {
|
|
|
|
fundingTx.AddTxOut(theirChangeOutput)
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, add the 2-of-2 multi-sig output which will set up the lightning
|
2015-11-27 09:51:01 +03:00
|
|
|
// channel.
|
2015-12-24 21:41:15 +03:00
|
|
|
ourKey := pendingReservation.partialState.MultiSigKey
|
2015-12-23 07:31:17 +03:00
|
|
|
theirKey := theirContribution.MultiSigKey
|
2015-12-21 02:11:21 +03:00
|
|
|
|
2015-12-24 21:41:15 +03:00
|
|
|
channelCapacity := int64(pendingReservation.partialState.Capacity)
|
2015-12-22 00:53:34 +03:00
|
|
|
redeemScript, multiSigOut, err := fundMultiSigOut(ourKey.PubKey().SerializeCompressed(),
|
|
|
|
theirKey.SerializeCompressed(), channelCapacity)
|
2015-10-28 01:50:30 +03:00
|
|
|
if err != nil {
|
2015-11-05 23:36:19 +03:00
|
|
|
req.err <- err
|
|
|
|
return
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
2015-12-24 21:42:29 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): do Manager.ImportScript(..) here, gives us a
|
|
|
|
// ManagedScriptAddress to play around with if we need it.
|
|
|
|
pendingReservation.partialState.FundingRedeemScript = redeemScript
|
2015-12-23 07:31:17 +03:00
|
|
|
fundingTx.AddTxOut(multiSigOut)
|
2015-11-05 23:36:19 +03:00
|
|
|
|
|
|
|
// Sort the transaction. Since both side agree to a cannonical
|
|
|
|
// ordering, by sorting we no longer need to send the entire
|
|
|
|
// transaction. Only signatures will be exchanged.
|
2015-12-24 21:41:15 +03:00
|
|
|
txsort.InPlaceSort(pendingReservation.partialState.FundingTx)
|
2015-11-05 23:36:19 +03:00
|
|
|
|
|
|
|
// Now that the transaction has been cannonically sorted, compute the
|
|
|
|
// normalized transation ID before we attach our signatures.
|
2015-11-27 09:51:01 +03:00
|
|
|
// TODO(roasbeef): this isn't the normalized txid, this isn't recursive...
|
2015-12-19 06:39:51 +03:00
|
|
|
// pendingReservation.normalizedTxID = pendingReservation.fundingTx.TxSha()
|
2015-11-05 23:36:19 +03:00
|
|
|
|
|
|
|
// Now, sign all inputs that are ours, collecting the signatures in
|
|
|
|
// order of the inputs.
|
2015-12-23 07:31:17 +03:00
|
|
|
pendingReservation.ourFundingSigs = make([][]byte, 0, len(ourContribution.Inputs))
|
|
|
|
for i, txIn := range fundingTx.TxIn {
|
2015-11-05 23:36:19 +03:00
|
|
|
// Does the wallet know about the txin?
|
2015-11-27 09:48:20 +03:00
|
|
|
txDetail, _ := l.wallet.TxStore.TxDetails(&txIn.PreviousOutPoint.Hash)
|
2015-11-05 23:36:19 +03:00
|
|
|
if txDetail == nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// Is this our txin? TODO(roasbeef): assumes all inputs are P2PKH...
|
|
|
|
prevIndex := txIn.PreviousOutPoint.Index
|
|
|
|
prevOut := txDetail.TxRecord.MsgTx.TxOut[prevIndex]
|
|
|
|
_, addrs, _, _ := txscript.ExtractPkScriptAddrs(prevOut.PkScript, ActiveNetParams)
|
|
|
|
apkh, ok := addrs[0].(*btcutil.AddressPubKeyHash)
|
|
|
|
if !ok {
|
|
|
|
req.err <- btcwallet.ErrUnsupportedTransactionType
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-27 09:48:20 +03:00
|
|
|
ai, err := l.wallet.Manager.Address(apkh)
|
2015-11-05 23:36:19 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- fmt.Errorf("cannot get address info: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pka := ai.(waddrmgr.ManagedPubKeyAddress)
|
|
|
|
privkey, err := pka.PrivKey()
|
|
|
|
if err != nil {
|
|
|
|
req.err <- fmt.Errorf("cannot get private key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-12-24 21:41:15 +03:00
|
|
|
sigscript, err := txscript.SignatureScript(pendingReservation.partialState.FundingTx, i,
|
2015-11-05 23:36:19 +03:00
|
|
|
prevOut.PkScript, txscript.SigHashAll, privkey,
|
|
|
|
ai.Compressed())
|
2015-10-28 01:50:30 +03:00
|
|
|
if err != nil {
|
2015-11-05 23:36:19 +03:00
|
|
|
req.err <- fmt.Errorf("cannot create sigscript: %s", err)
|
|
|
|
return
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-12-24 21:41:15 +03:00
|
|
|
fundingTx.TxIn[i].SignatureScript = sigscript
|
2015-12-19 06:39:51 +03:00
|
|
|
pendingReservation.ourFundingSigs = append(pendingReservation.ourFundingSigs, sigscript)
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 00:53:34 +03:00
|
|
|
// Initialize an empty sha-chain for them, tracking the current pending
|
|
|
|
// revocation hash (we don't yet know the pre-image so we can't add it
|
|
|
|
// to the chain).
|
2015-12-24 21:42:03 +03:00
|
|
|
pendingReservation.partialState.TheirShaChain = shachain.New()
|
|
|
|
pendingReservation.partialState.TheirCurrentRevocation = theirContribution.RevocationHash
|
2015-12-22 00:53:34 +03:00
|
|
|
|
|
|
|
// Grab the hash of the current pre-image in our chain, this is needed
|
|
|
|
// for out commitment tx.
|
|
|
|
// TODO(roasbeef): grab partial state above to avoid long attr chain
|
2015-12-24 21:41:15 +03:00
|
|
|
ourCurrentRevokeHash := pendingReservation.partialState.OurShaChain.CurrentRevocationHash()
|
2015-12-23 07:31:17 +03:00
|
|
|
ourContribution.RevocationHash = ourCurrentRevokeHash
|
2015-12-22 00:53:34 +03:00
|
|
|
|
|
|
|
// Create the txIn to our commitment transaction. In the process, we
|
|
|
|
// need to locate the index of the multi-sig output on the funding tx
|
|
|
|
// since the outputs are cannonically sorted.
|
2015-12-24 21:41:15 +03:00
|
|
|
fundingNTxid := fundingTx.TxSha() // NOTE: assumes testnet-L
|
|
|
|
_, multiSigIndex := findScriptOutputIndex(fundingTx, multiSigOut.PkScript)
|
2015-12-22 00:53:34 +03:00
|
|
|
fundingTxIn := wire.NewTxIn(wire.NewOutPoint(&fundingNTxid, multiSigIndex), nil)
|
|
|
|
|
|
|
|
// With the funding tx complete, create both commitment transactions.
|
2015-12-23 07:31:17 +03:00
|
|
|
initialBalance := ourContribution.FundingAmount
|
|
|
|
pendingReservation.fundingLockTime = theirContribution.CsvDelay
|
|
|
|
ourCommitKey := ourContribution.CommitKey
|
|
|
|
theirCommitKey := theirContribution.CommitKey
|
2015-12-22 00:53:34 +03:00
|
|
|
ourCommitTx, err := createCommitTx(fundingTxIn, ourCommitKey, theirCommitKey,
|
2015-12-23 07:31:17 +03:00
|
|
|
ourCurrentRevokeHash, theirContribution.CsvDelay, initialBalance)
|
2015-12-22 00:53:34 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
theirCommitTx, err := createCommitTx(fundingTxIn, theirCommitKey, ourCommitKey,
|
2015-12-23 07:31:17 +03:00
|
|
|
theirContribution.RevocationHash, theirContribution.CsvDelay, initialBalance)
|
2015-12-22 00:53:34 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-26 09:03:53 +03:00
|
|
|
pendingReservation.partialState.CsvDelay = theirContribution.CsvDelay
|
|
|
|
pendingReservation.partialState.TheirDeliveryAddress = theirContribution.DeliveryAddress
|
|
|
|
pendingReservation.partialState.ChanID = fundingNTxid
|
2015-12-24 21:41:15 +03:00
|
|
|
pendingReservation.partialState.TheirCommitKey = theirCommitKey
|
|
|
|
pendingReservation.partialState.TheirCommitTx = theirCommitTx
|
|
|
|
pendingReservation.partialState.OurCommitTx = ourCommitTx
|
2015-12-22 00:53:34 +03:00
|
|
|
|
|
|
|
// Generate a signature for their version of the initial commitment
|
|
|
|
// transaction.
|
2015-12-23 07:31:17 +03:00
|
|
|
sigTheirCommit, err := txscript.RawTxInSignature(theirCommitTx, 0, multiSigOut.PkScript,
|
2015-12-22 00:53:34 +03:00
|
|
|
txscript.SigHashAll, ourKey)
|
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
return
|
|
|
|
}
|
2015-12-23 07:31:17 +03:00
|
|
|
pendingReservation.ourCommitmentSig = sigTheirCommit
|
2015-12-22 00:53:34 +03:00
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
req.err <- nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleFundingCounterPartySigs...
|
|
|
|
func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigsMsg) {
|
2015-11-13 05:43:32 +03:00
|
|
|
l.limboMtx.RLock()
|
2015-11-05 23:36:19 +03:00
|
|
|
pendingReservation, ok := l.fundingLimbo[msg.pendingFundingID]
|
2015-11-13 05:43:32 +03:00
|
|
|
l.limboMtx.RUnlock()
|
2015-11-05 23:36:19 +03:00
|
|
|
if !ok {
|
|
|
|
msg.err <- fmt.Errorf("attempted to update non-existant funding state")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-14 22:52:07 +03:00
|
|
|
// Grab the mutex on the ChannelReservation to ensure thead-safety
|
|
|
|
pendingReservation.Lock()
|
|
|
|
defer pendingReservation.Unlock()
|
|
|
|
|
2015-11-05 23:36:19 +03:00
|
|
|
// Now we can complete the funding transaction by adding their
|
|
|
|
// signatures to their inputs.
|
2015-12-22 00:54:33 +03:00
|
|
|
pendingReservation.theirFundingSigs = msg.theirFundingSigs
|
2015-12-24 21:41:15 +03:00
|
|
|
fundingTx := pendingReservation.partialState.FundingTx
|
2015-12-03 03:51:46 +03:00
|
|
|
for i, txin := range fundingTx.TxIn {
|
2015-11-05 23:36:19 +03:00
|
|
|
if txin.SignatureScript == nil {
|
2015-12-19 06:39:51 +03:00
|
|
|
txin.SignatureScript = pendingReservation.theirFundingSigs[i]
|
2015-12-03 03:51:46 +03:00
|
|
|
|
2015-12-16 00:24:19 +03:00
|
|
|
/*// Fetch the alleged previous output along with the
|
2015-12-03 03:51:46 +03:00
|
|
|
// pkscript referenced by this input.
|
|
|
|
prevOut := txin.PreviousOutPoint
|
|
|
|
output, err := l.rpc.GetTxOut(&prevOut.Hash, prevOut.Index, false)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(roasbeef): do this at the start to avoid wasting out time?
|
|
|
|
// 8 or a set of nodes "we" run with exposed unauthenticated RPC?
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pkscript, err := hex.DecodeString(output.ScriptPubKey.Hex)
|
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the signature is valid.
|
|
|
|
vm, err := txscript.NewEngine(pkscript,
|
|
|
|
fundingTx, i, txscript.StandardVerifyFlags, nil)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(roasbeef): cancel at this stage if invalid sigs?
|
|
|
|
msg.err <- fmt.Errorf("cannot create script engine: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = vm.Execute(); err != nil {
|
|
|
|
msg.err <- fmt.Errorf("cannot validate transaction: %s", err)
|
|
|
|
return
|
2015-12-16 00:24:19 +03:00
|
|
|
}*/
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 00:54:33 +03:00
|
|
|
// At this point, wen calso record and verify their isgnature for our
|
|
|
|
// commitment transaction.
|
2015-12-24 21:41:15 +03:00
|
|
|
pendingReservation.partialState.TheirCommitSig = msg.theirCommitmentSig
|
2015-12-23 07:31:17 +03:00
|
|
|
|
2015-12-22 00:54:33 +03:00
|
|
|
// TODO(roasbeef): verify
|
|
|
|
//commitSig := msg.theirCommitmentSig
|
|
|
|
|
2015-12-19 06:39:51 +03:00
|
|
|
// Funding complete, this entry can be removed from limbo.
|
2015-11-13 05:43:32 +03:00
|
|
|
l.limboMtx.Lock()
|
|
|
|
delete(l.fundingLimbo, pendingReservation.reservationID)
|
2015-12-19 06:39:51 +03:00
|
|
|
// TODO(roasbeef): unlock outputs here, Store.InsertTx will handle marking
|
|
|
|
// input in unconfirmed tx, so future coin selects don't pick it up
|
|
|
|
// * also record location of change address so can use AddCredit
|
2015-11-13 05:43:32 +03:00
|
|
|
l.limboMtx.Unlock()
|
2015-11-05 23:36:19 +03:00
|
|
|
|
2015-12-03 03:51:21 +03:00
|
|
|
// Add the complete funding transaction to the DB, in it's open bucket
|
|
|
|
// which will be used for the lifetime of this channel.
|
2015-12-24 21:42:29 +03:00
|
|
|
// TODO(roasbeef): serialize OpenChannelState state to disk instead now
|
2015-12-03 03:51:21 +03:00
|
|
|
writeErr := l.lnNamespace.Update(func(tx walletdb.Tx) error {
|
|
|
|
// Get the bucket dedicated to storing the meta-data for open
|
|
|
|
// channels.
|
2015-12-16 01:18:43 +03:00
|
|
|
// TODO(roasbeef): CHECKSUMS, REDUNDANCY, etc etc.
|
2015-12-03 03:51:21 +03:00
|
|
|
rootBucket := tx.RootBucket()
|
|
|
|
openChanBucket, err := rootBucket.CreateBucketIfNotExists(openChannelBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new sub-bucket within the open channel bucket
|
|
|
|
// specifically for this channel.
|
2015-12-16 01:18:43 +03:00
|
|
|
// TODO(roasbeef): should def be indexed by LNID, cuz mal etc.
|
2015-12-24 21:41:15 +03:00
|
|
|
txID := fundingTx.TxSha()
|
2015-12-03 03:51:21 +03:00
|
|
|
chanBucket, err := openChanBucket.CreateBucketIfNotExists(txID.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): sync.Pool of buffers in the future.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
fundingTx.Serialize(&buf)
|
|
|
|
return chanBucket.Put(fundingTxKey, buf.Bytes())
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(roasbeef): broadcast now?
|
2015-12-16 00:24:43 +03:00
|
|
|
// * create goroutine, listens on blockconnected+blockdisconnected channels
|
|
|
|
// * after six blocks, then will create an LightningChannel struct and
|
|
|
|
// send over reservation.
|
|
|
|
// * will need a multi-plexer to fan out, to listen on ListenConnectedBlocks
|
|
|
|
// * should prob be a separate struct/modele
|
|
|
|
// * use NotifySpent in order to catch non-cooperative spends of revoked
|
2015-12-24 21:42:29 +03:00
|
|
|
// * NotifySpent(outpoints []*wire.OutPoint)
|
2015-12-16 00:24:43 +03:00
|
|
|
// commitment txns. Hmm using p2sh or bare multi-sig?
|
2015-12-03 03:51:21 +03:00
|
|
|
msg.err <- writeErr
|
2015-11-13 05:43:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// nextMultiSigKey...
|
|
|
|
// TODO(roasbeef): on shutdown, write state of pending keys, then read back?
|
2015-12-21 02:13:14 +03:00
|
|
|
func (l *LightningWallet) getNextRawKey() (*btcec.PrivateKey, error) {
|
2015-12-03 03:50:00 +03:00
|
|
|
l.lmtx.Lock()
|
|
|
|
defer l.lmtx.Unlock()
|
|
|
|
|
2015-11-27 09:48:20 +03:00
|
|
|
nextAddr, err := l.wallet.Manager.NextExternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
2015-11-13 05:43:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
|
|
|
|
2015-11-13 05:43:32 +03:00
|
|
|
pkAddr := nextAddr[0].(waddrmgr.ManagedPubKeyAddress)
|
|
|
|
|
|
|
|
return pkAddr.PrivKey()
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|