2015-12-20 09:00:50 +03:00
|
|
|
package lnwallet
|
2015-10-28 01:50:30 +03:00
|
|
|
|
|
|
|
import (
|
2016-01-07 03:15:49 +03:00
|
|
|
"encoding/hex"
|
2015-10-28 01:50:30 +03:00
|
|
|
"errors"
|
2015-11-05 23:36:19 +03:00
|
|
|
"fmt"
|
2015-11-29 03:16:00 +03:00
|
|
|
"math"
|
2015-10-28 01:50:30 +03:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2016-01-16 21:45:54 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntfs"
|
|
|
|
"github.com/lightningnetwork/lnd/chainntfs/btcdnotify"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/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/txscript"
|
2015-10-28 01:50:30 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2016-02-27 03:35:04 +03:00
|
|
|
"github.com/btcsuite/btcrpcclient"
|
2015-10-28 01:50:30 +03:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2015-11-13 06:34:35 +03:00
|
|
|
const (
|
2015-12-28 23:14:00 +03:00
|
|
|
// The size of the buffered queue of request to the wallet from the
|
|
|
|
// outside word.
|
2015-11-13 06:34:35 +03:00
|
|
|
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")
|
|
|
|
|
2015-12-26 21:35:15 +03:00
|
|
|
// Namespace bucket keys.
|
|
|
|
lightningNamespaceKey = []byte("ln-wallet")
|
|
|
|
waddrmgrNamespaceKey = []byte("waddrmgr")
|
|
|
|
wtxmgrNamespaceKey = []byte("wtxmgr")
|
2015-11-05 23:36:19 +03:00
|
|
|
)
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// FundingType represents the type of the funding transaction. The type of
|
|
|
|
// funding transaction available depends entirely on the level of upgrades to
|
|
|
|
// Script on the current network. Across the network it's possible for asymmetric
|
|
|
|
// funding types to exist across hop. However, for direct links, the funding type
|
|
|
|
// supported by both parties must be identical. The most 'powerful' funding type
|
|
|
|
// is SEGWIT. This funding type also assumes that both CSV+CLTV are available on
|
|
|
|
// the network.
|
|
|
|
// NOTE: Ultimately, this will most likely be deprecated...
|
2015-11-05 23:36:19 +03:00
|
|
|
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-12-28 23:14:00 +03:00
|
|
|
// initFundingReserveReq is the first message sent to initiate the workflow
|
|
|
|
// required to open a payment channel with a remote peer. The initial required
|
|
|
|
// paramters are configurable accross channels. These paramters are to be chosen
|
|
|
|
// depending on the fee climate within the network, and time value of funds to
|
|
|
|
// be locked up within the channel. Upon success a ChannelReservation will be
|
|
|
|
// created in order to track the lifetime of this pending channel. Outputs
|
|
|
|
// selected will be 'locked', making them unavailable, for any other pending
|
|
|
|
// reservations. Therefore, all channels in reservation limbo will be periodically
|
|
|
|
// after a timeout period in order to avoid "exhaustion" attacks.
|
|
|
|
// NOTE: The workflow currently assumes fully balanced symmetric channels.
|
|
|
|
// Meaning both parties must encumber the same amount of funds.
|
|
|
|
// TODO(roasbeef): zombie reservation sweeper goroutine.
|
2015-11-05 23:36:19 +03:00
|
|
|
type initFundingReserveMsg struct {
|
2015-12-28 23:14:00 +03:00
|
|
|
// The type of the funding transaction. See above for further details.
|
|
|
|
fundingType FundingType
|
|
|
|
|
|
|
|
// The amount of funds requested for this channel.
|
2015-10-28 01:50:30 +03:00
|
|
|
fundingAmount btcutil.Amount
|
2015-11-05 23:36:19 +03:00
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// The minimum accepted satoshis/KB fee for the funding transaction. In
|
|
|
|
// order to ensure timely confirmation, it is recomened that this fee
|
|
|
|
// should be generous, paying some multiple of the accepted base fee
|
|
|
|
// rate of the network.
|
|
|
|
// TODO(roasbeef): integrate fee estimation project...
|
|
|
|
minFeeRate btcutil.Amount
|
|
|
|
|
|
|
|
// The ID of the remote node we would like to open a channel with.
|
2015-12-26 09:05:07 +03:00
|
|
|
nodeID [32]byte
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// The delay on the "pay-to-self" output(s) of the commitment transaction.
|
|
|
|
csvDelay uint32
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// A channel in which all errors will be sent accross. Will be nil if
|
|
|
|
// this initial set is succesful.
|
|
|
|
// NOTE: In order to avoid deadlocks, this channel MUST be buffered.
|
|
|
|
err chan error
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// A ChannelReservation with our contributions filled in will be sent
|
|
|
|
// accross this channel in the case of a succesfully reservation
|
|
|
|
// initiation. In the case of an error, this will read a nil pointer.
|
|
|
|
// NOTE: In order to avoid deadlocks, this channel MUST be buffered.
|
|
|
|
resp chan *ChannelReservation
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// fundingReserveCancelMsg is a message reserved for cancelling an existing
|
|
|
|
// channel reservation identified by its reservation ID. Cancelling a reservation
|
|
|
|
// frees its locked outputs up, for inclusion within further reservations.
|
2015-11-05 23:36:19 +03:00
|
|
|
type fundingReserveCancelMsg struct {
|
|
|
|
pendingFundingID uint64
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// NOTE: In order to avoid deadlocks, this channel MUST be buffered.
|
2015-12-16 23:40:44 +03:00
|
|
|
err chan error // Buffered
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// addContributionMsg represents a message executing the second phase of the
|
|
|
|
// channel reservation workflow. This message carries the counterparty's
|
|
|
|
// "contribution" to the payment channel. In the case that this message is
|
|
|
|
// processed without generating any errors, then channel reservation will then
|
|
|
|
// be able to construct the funding tx, both commitment transactions, and
|
|
|
|
// finally generate signatures for all our inputs to the funding transaction,
|
|
|
|
// and for the remote node's version of the commitment transaction.
|
2015-12-22 00:53:34 +03:00
|
|
|
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-12-28 23:14:00 +03:00
|
|
|
// NOTE: In order to avoid deadlocks, this channel MUST be buffered.
|
|
|
|
err chan error
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// addCounterPartySigsMsg represents the final message required to complete,
|
|
|
|
// and 'open' a payment channel. This message carries the counterparty's
|
|
|
|
// signatures for each of their inputs to the funding transaction, and also a
|
|
|
|
// signature allowing us to spend our version of the commitment transaction.
|
|
|
|
// If we're able to verify all the signatures are valid, the funding transaction
|
|
|
|
// will be broadcast to the network. After the funding transaction gains a
|
|
|
|
// configurable number of confirmations, the channel is officially considered
|
|
|
|
// 'open'.
|
2015-11-05 23:36:19 +03:00
|
|
|
type addCounterPartySigsMsg struct {
|
|
|
|
pendingFundingID uint64
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-12-28 23:14:00 +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-12-28 23:14:00 +03:00
|
|
|
// NOTE: In order to avoid deadlocks, this channel MUST be buffered.
|
|
|
|
err chan error
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// LightningWallet is a domain specific, yet general Bitcoin wallet capable of
|
|
|
|
// executing workflow required to interact with the Lightning Network. It is
|
|
|
|
// domain specific in the sense that it understands all the fancy scripts used
|
|
|
|
// within the Lightning Network, channel lifetimes, etc. However, it embedds a
|
|
|
|
// general purpose Bitcoin wallet within it. Therefore, it is also able to serve
|
|
|
|
// as a regular Bitcoin wallet which uses HD keys. The wallet is highly concurrent
|
|
|
|
// internally. All communication, and requests towards the wallet are
|
|
|
|
// dispatched as messages over channels, ensuring thread safety across all
|
|
|
|
// operations. Interaction has been designed independant of any peer-to-peer
|
|
|
|
// communication protocol, allowing the wallet to be self-contained and embeddable
|
|
|
|
// within future projects interacting with the Lightning Network.
|
|
|
|
// NOTE: At the moment the wallet requires a btcd full node, as it's dependant
|
|
|
|
// on btcd's websockets notifications as even triggers during the lifetime of
|
|
|
|
// a channel. However, once the chainntnfs package is complete, the wallet
|
|
|
|
// will be compatible with multiple RPC/notification services such as Electrum,
|
|
|
|
// Bitcoin Core + ZeroMQ, etc. Eventually, the wallet won't require a full-node
|
|
|
|
// at all, as SPV support is integrated inot btcwallet.
|
2015-10-28 01:50:30 +03:00
|
|
|
type LightningWallet struct {
|
2015-12-28 23:14:00 +03:00
|
|
|
// This mutex is to be held when generating external keys to be used
|
|
|
|
// as multi-sig, and commitment keys within the channel.
|
2015-12-30 02:09:32 +03:00
|
|
|
KeyGenMtx sync.RWMutex
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-12-28 23:12:18 +03:00
|
|
|
// This mutex MUST be held when performing coin selection in order to
|
|
|
|
// avoid inadvertently creating multiple funding transaction which
|
|
|
|
// double spend inputs accross each other.
|
|
|
|
coinSelectMtx sync.RWMutex
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-12-26 09:05:55 +03:00
|
|
|
// A wrapper around a namespace within boltdb reserved for ln-based
|
2015-12-28 23:14:00 +03:00
|
|
|
// wallet meta-data. See the 'channeldb' package for further
|
|
|
|
// information.
|
2016-03-23 04:48:18 +03:00
|
|
|
channelDB *channeldb.DB
|
2015-12-28 23:14:00 +03:00
|
|
|
|
2016-01-07 03:15:49 +03:00
|
|
|
// Used by in order to obtain notifications about funding transaction
|
|
|
|
// reaching a specified confirmation depth, and to catch
|
|
|
|
// counterparty's broadcasting revoked commitment states.
|
|
|
|
chainNotifier chainntnfs.ChainNotifier
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// The core wallet, all non Lightning Network specific interaction is
|
|
|
|
// proxied to the internal wallet.
|
2015-12-29 21:44:59 +03:00
|
|
|
*btcwallet.Wallet
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// An active RPC connection to a full-node. In the case of a btcd node,
|
|
|
|
// websockets are used for notifications. If using Bitcoin Core,
|
|
|
|
// notifications are either generated via long-polling or the usage of
|
|
|
|
// ZeroMQ.
|
2016-02-25 09:17:23 +03:00
|
|
|
// TODO(roasbeef): make into interface need: getrawtransaction + gettxout
|
|
|
|
// * getrawtransaction -> verify proof of channel links
|
|
|
|
// * gettxout -> verify inputs to funding tx exist and are unspent
|
2016-02-03 05:42:59 +03:00
|
|
|
rpc *chain.RPCClient
|
2015-12-28 23:14:00 +03:00
|
|
|
|
|
|
|
// All messages to the wallet are to be sent accross this channel.
|
2015-10-28 01:50:30 +03:00
|
|
|
msgChan chan interface{}
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// Incomplete payment channels are stored in the map below. An intent
|
|
|
|
// to create a payment channel is tracked as a "reservation" within
|
|
|
|
// limbo. Once the final signatures have been exchanged, a reservation
|
|
|
|
// is removed from limbo. Each reservation is tracked by a unique
|
|
|
|
// monotonically integer. All requests concerning the channel MUST
|
|
|
|
// carry a valid, active funding ID.
|
|
|
|
fundingLimbo map[uint64]*ChannelReservation
|
|
|
|
nextFundingID uint64
|
|
|
|
limboMtx sync.RWMutex
|
2015-11-05 23:34:11 +03:00
|
|
|
// TODO(roasbeef): zombie garbage collection routine to solve
|
|
|
|
// lost-object/starvation problem/attack.
|
2015-11-19 01:38:57 +03:00
|
|
|
|
2015-12-30 00:59:57 +03:00
|
|
|
cfg *Config
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
started int32
|
2015-10-28 01:50:30 +03:00
|
|
|
shutdown int32
|
2015-12-28 23:14:00 +03:00
|
|
|
quit chan struct{}
|
2015-10-28 01:50:30 +03:00
|
|
|
|
|
|
|
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-12-28 23:14:00 +03:00
|
|
|
// NewLightningWallet creates/opens and initializes a LightningWallet instance.
|
|
|
|
// If the wallet has never been created (according to the passed dataDir), first-time
|
|
|
|
// setup is executed.
|
2015-11-27 09:46:59 +03:00
|
|
|
// TODO(roasbeef): fin...add config
|
2016-03-23 04:48:18 +03:00
|
|
|
func NewLightningWallet(config *Config, cdb *channeldb.DB) (*LightningWallet, error) {
|
2015-11-19 01:38:57 +03:00
|
|
|
// Ensure the wallet exists or create it when the create flag is set.
|
2016-02-03 06:09:54 +03:00
|
|
|
netDir := networkDir(config.DataDir, config.NetParams)
|
2015-11-19 01:38:57 +03:00
|
|
|
|
|
|
|
var pubPass []byte
|
2015-12-30 00:59:57 +03:00
|
|
|
if config.PublicPass == nil {
|
2015-11-19 01:38:57 +03:00
|
|
|
pubPass = defaultPubPassphrase
|
|
|
|
} else {
|
2015-12-30 00:59:57 +03:00
|
|
|
pubPass = config.PublicPass
|
2015-11-13 06:34:35 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 23:00:28 +03:00
|
|
|
loader := btcwallet.NewLoader(config.NetParams, netDir)
|
|
|
|
walletExists, err := loader.WalletExists()
|
|
|
|
if err != nil {
|
2016-03-23 04:48:18 +03:00
|
|
|
return nil, err
|
2016-02-03 23:00:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 02:12:06 +03:00
|
|
|
var createID bool
|
2016-02-03 23:00:28 +03:00
|
|
|
var wallet *btcwallet.Wallet
|
|
|
|
if !walletExists {
|
|
|
|
// Wallet has never been created, perform initial set up.
|
|
|
|
wallet, err = loader.CreateNewWallet(pubPass, config.PrivatePass,
|
|
|
|
config.HdSeed)
|
|
|
|
if err != nil {
|
2016-03-23 04:48:18 +03:00
|
|
|
return nil, err
|
2015-11-19 01:38:57 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 23:00:28 +03:00
|
|
|
createID = true
|
|
|
|
} else {
|
|
|
|
// Wallet has been created and been initialized at this point, open it
|
|
|
|
// along with all the required DB namepsaces, and the DB itself.
|
|
|
|
wallet, err = loader.OpenExistingWallet(pubPass, false)
|
|
|
|
if err != nil {
|
2016-03-23 04:48:18 +03:00
|
|
|
return nil, err
|
2015-11-19 01:38:57 +03:00
|
|
|
}
|
2015-11-13 06:34:35 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 02:12:06 +03:00
|
|
|
if err := wallet.Manager.Unlock(config.PrivatePass); err != nil {
|
2016-03-23 04:48:18 +03:00
|
|
|
return nil, err
|
2016-01-07 02:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we just created the wallet, then reserve, and store a key for
|
|
|
|
// our ID within the Lightning Network.
|
|
|
|
if createID {
|
|
|
|
adrs, err := wallet.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
|
|
|
if err != nil {
|
2016-03-23 04:48:18 +03:00
|
|
|
return nil, err
|
2016-01-07 02:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
idPubkeyHash := adrs[0].Address().ScriptAddress()
|
|
|
|
if err := cdb.PutIdKey(idPubkeyHash); err != nil {
|
2016-03-23 04:48:18 +03:00
|
|
|
return nil, err
|
2016-01-07 02:12:06 +03:00
|
|
|
}
|
2016-03-23 04:48:18 +03:00
|
|
|
log.Infof("stored identity key pubkey hash in channeldb")
|
2015-11-13 06:34:35 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 03:35:04 +03:00
|
|
|
// Create a special websockets rpc client for btcd which will be used
|
|
|
|
// by the wallet for notifications, calls, etc.
|
|
|
|
rpcc, err := chain.NewRPCClient(config.NetParams, config.RpcHost,
|
|
|
|
config.RpcUser, config.RpcPass, config.CACert, false, 20)
|
2016-02-25 09:17:23 +03:00
|
|
|
if err != nil {
|
2016-03-23 04:48:18 +03:00
|
|
|
return nil, err
|
2016-02-25 09:17:23 +03:00
|
|
|
}
|
2016-02-27 03:35:04 +03:00
|
|
|
|
|
|
|
// Using the same authentication info, create a config for a second
|
|
|
|
// rpcclient which will be used by the current default chain
|
|
|
|
// notifier implemenation.
|
|
|
|
rpcConfig := &btcrpcclient.ConnConfig{
|
|
|
|
Host: config.RpcHost,
|
|
|
|
Endpoint: "ws",
|
|
|
|
User: config.RpcUser,
|
|
|
|
Pass: config.RpcPass,
|
|
|
|
Certificates: config.CACert,
|
|
|
|
DisableTLS: false,
|
|
|
|
DisableConnectOnNew: true,
|
|
|
|
DisableAutoReconnect: false,
|
|
|
|
}
|
|
|
|
chainNotifier, err := btcdnotify.NewBtcdNotifier(rpcConfig)
|
2016-01-07 03:15:49 +03:00
|
|
|
if err != nil {
|
2016-03-23 04:48:18 +03:00
|
|
|
return nil, err
|
2016-01-07 03:15:49 +03:00
|
|
|
}
|
2015-11-19 01:38:57 +03:00
|
|
|
|
2016-01-07 03:17:18 +03:00
|
|
|
// TODO(roasbeef): logging
|
2015-11-13 06:34:35 +03:00
|
|
|
return &LightningWallet{
|
2016-01-07 03:15:49 +03:00
|
|
|
chainNotifier: chainNotifier,
|
2016-02-25 09:17:23 +03:00
|
|
|
rpc: rpcc,
|
2016-01-07 03:15:49 +03:00
|
|
|
Wallet: wallet,
|
2016-03-23 04:48:18 +03:00
|
|
|
channelDB: cdb,
|
2016-01-07 03:15:49 +03:00
|
|
|
msgChan: make(chan interface{}, msgBufferSize),
|
2015-12-16 01:18:43 +03:00
|
|
|
// 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,
|
2015-12-30 00:59:57 +03:00
|
|
|
cfg: config,
|
2015-11-13 06:34:35 +03:00
|
|
|
fundingLimbo: make(map[uint64]*ChannelReservation),
|
|
|
|
quit: make(chan struct{}),
|
2016-03-23 04:48:18 +03:00
|
|
|
}, nil
|
2015-11-05 23:34:11 +03:00
|
|
|
}
|
|
|
|
|
2015-12-29 21:44:59 +03:00
|
|
|
// Startup establishes a connection to the RPC source, and spins up all
|
2015-12-28 23:14:00 +03:00
|
|
|
// goroutines required to handle incoming messages.
|
2015-12-29 21:44:59 +03:00
|
|
|
func (l *LightningWallet) Startup() error {
|
2015-10-28 01:50:30 +03:00
|
|
|
// Already started?
|
|
|
|
if atomic.AddInt32(&l.started, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-27 09:48:42 +03:00
|
|
|
|
2016-02-25 09:17:23 +03:00
|
|
|
// Establish an RPC connection in additino to starting the goroutines
|
|
|
|
// in the underlying wallet.
|
|
|
|
if err := l.rpc.Start(); err != nil {
|
2015-11-27 09:48:42 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-02-25 09:17:23 +03:00
|
|
|
l.Start()
|
2015-11-27 09:48:42 +03:00
|
|
|
|
2016-02-25 09:17:23 +03:00
|
|
|
// Start the notification server. This is used so channel managment
|
|
|
|
// goroutines can be notified when a funding transaction reaches a
|
|
|
|
// sufficient number of confirmations, or when the input for the funding
|
|
|
|
// transaction is spent in an attempt at an uncooperative close by the
|
|
|
|
// counter party.
|
|
|
|
if err := l.chainNotifier.Start(); err != nil {
|
2015-12-30 06:08:56 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-03 05:42:59 +03:00
|
|
|
// Pass the rpc client into the wallet so it can sync up to the current
|
|
|
|
// main chain.
|
|
|
|
l.SynchronizeRPC(l.rpc)
|
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
|
|
|
|
}
|
|
|
|
|
2015-12-29 21:44:59 +03:00
|
|
|
// Shutdown gracefully stops the wallet, and all active goroutines.
|
|
|
|
func (l *LightningWallet) Shutdown() error {
|
2015-10-28 01:50:30 +03:00
|
|
|
if atomic.AddInt32(&l.shutdown, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-29 21:44:59 +03:00
|
|
|
l.Stop()
|
2016-02-25 09:17:23 +03:00
|
|
|
|
2015-12-28 23:12:39 +03:00
|
|
|
l.rpc.Shutdown()
|
2015-11-28 03:07:22 +03:00
|
|
|
|
2016-02-25 09:17:23 +03:00
|
|
|
l.chainNotifier.Stop()
|
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
close(l.quit)
|
|
|
|
l.wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// requestHandler is the primary goroutine(s) resposible for handling, and
|
|
|
|
// dispatching relies to all messages.
|
2015-10-28 01:50:30 +03:00
|
|
|
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-28 23:14:00 +03:00
|
|
|
// InitChannelReservation kicks off the 3-step workflow required to succesfully
|
|
|
|
// open a payment channel with a remote node. As part of the funding
|
|
|
|
// reservation, the inputs selected for the funding transaction are 'locked'.
|
|
|
|
// This ensures that multiple channel reservations aren't double spending the
|
|
|
|
// same inputs in the funding transaction. If reservation initialization is
|
|
|
|
// succesful, a ChannelReservation containing our completed contribution is
|
|
|
|
// returned. Our contribution contains all the items neccessary to allow the
|
|
|
|
// counter party to build the funding transaction, and both versions of the
|
|
|
|
// commitment transaction. Otherwise, an error occured a nil pointer along with
|
|
|
|
// an error are returned.
|
|
|
|
//
|
|
|
|
// Once a ChannelReservation has been obtained, two
|
|
|
|
// additional steps must be processed before a payment channel can be considered
|
|
|
|
// 'open'. The second step validates, and processes the counterparty's channel
|
|
|
|
// contribution. The third, and final step verifies all signatures for the inputs
|
|
|
|
// of the funding transaction, and that the signature we records for our version
|
|
|
|
// of the commitment transaction is valid.
|
|
|
|
func (l *LightningWallet) InitChannelReservation(a btcutil.Amount, t FundingType,
|
|
|
|
theirID [32]byte, csvDelay uint32) (*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-28 23:14:00 +03:00
|
|
|
csvDelay: csvDelay,
|
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-12-28 23:14:00 +03:00
|
|
|
// handleFundingReserveRequest processes a message intending to create, and
|
|
|
|
// validate a funding reservation request.
|
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-12-28 23:14:00 +03:00
|
|
|
ourContribution.CsvDelay = req.csvDelay
|
|
|
|
|
2015-12-28 23:12:18 +03:00
|
|
|
// We hold the coin select mutex while querying for outputs, and
|
|
|
|
// performing coin selection in order to avoid inadvertent double spends
|
|
|
|
// accross funding transactions.
|
|
|
|
// NOTE: we don't use defer her so we can properly release the lock
|
|
|
|
// when we encounter an error condition.
|
|
|
|
l.coinSelectMtx.Lock()
|
2015-12-23 07:31:17 +03:00
|
|
|
|
2016-01-20 07:39:10 +03:00
|
|
|
// TODO(roasbeef): check if balance is insufficient, if so then select
|
|
|
|
// on two channels, one is a time.After that will bail out with
|
|
|
|
// insuffcient funds, the other is a notification that the balance has
|
|
|
|
// been updated make(chan struct{}, 1).
|
|
|
|
|
2015-10-28 01:50:30 +03:00
|
|
|
// Find all unlocked unspent outputs with greater than 6 confirmations.
|
2015-11-29 02:28:48 +03:00
|
|
|
maxConfs := int32(math.MaxInt32)
|
2015-12-28 23:12:18 +03:00
|
|
|
// TODO(roasbeef): make 6 a config paramter?
|
2015-12-29 21:44:59 +03:00
|
|
|
unspentOutputs, err := l.ListUnspent(6, maxConfs, nil)
|
2015-10-28 01:50:30 +03:00
|
|
|
if err != nil {
|
2015-12-28 23:12:18 +03:00
|
|
|
l.coinSelectMtx.Unlock()
|
2015-10-28 01:50:30 +03:00
|
|
|
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 {
|
2015-12-28 23:12:18 +03:00
|
|
|
l.coinSelectMtx.Unlock()
|
2015-10-28 01:50:30 +03:00
|
|
|
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 {
|
2015-12-28 23:12:18 +03:00
|
|
|
l.coinSelectMtx.Unlock()
|
2015-11-27 09:50:17 +03:00
|
|
|
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-12-29 21:44:59 +03:00
|
|
|
l.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())
|
2016-02-16 09:13:17 +03:00
|
|
|
ourContribution.Inputs[i] = wire.NewTxIn(outPoint, nil, nil)
|
2015-10-28 01:50:30 +03:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:12:18 +03:00
|
|
|
l.coinSelectMtx.Unlock()
|
|
|
|
|
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.
|
2016-01-07 03:15:49 +03:00
|
|
|
changeAddr, err := l.NewChangeAddress(waddrmgr.DefaultAccountNum)
|
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
|
|
|
}
|
2016-01-07 03:15:49 +03:00
|
|
|
|
|
|
|
changeAddrScript, err := txscript.PayToAddrScript(changeAddr)
|
2015-12-16 00:23:56 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
req.resp <- nil
|
|
|
|
return
|
|
|
|
}
|
2015-10-28 01:50:30 +03:00
|
|
|
|
2016-01-07 03:15:49 +03:00
|
|
|
changeAmount := selectedTotalValue - req.fundingAmount
|
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
|
|
|
// 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.
|
2016-01-07 03:15:49 +03:00
|
|
|
deliveryAddress, err := l.NewAddress(waddrmgr.DefaultAccountNum)
|
2015-12-21 02:13:14 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
req.resp <- nil
|
|
|
|
return
|
|
|
|
}
|
2016-01-07 03:15:49 +03:00
|
|
|
reservation.partialState.OurDeliveryAddress = deliveryAddress
|
|
|
|
ourContribution.DeliveryAddress = deliveryAddress
|
2015-12-21 02:13:14 +03:00
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// Create a new shaChain for verifiable transaction revocations. This
|
|
|
|
// will be used to generate revocation hashes for our past/current
|
|
|
|
// commitment transactions once we start to make payments within the
|
|
|
|
// channel.
|
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-31 09:30:52 +03:00
|
|
|
copy(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
|
|
|
}
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// handleFundingReserveCancel cancels an existing channel reservation. As part
|
|
|
|
// of the cancellation, outputs previously selected as inputs for the funding
|
|
|
|
// transaction via coin selection are freed allowing future reservations to
|
|
|
|
// include them.
|
2015-11-05 23:36:19 +03:00
|
|
|
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-12-29 21:44:59 +03:00
|
|
|
l.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-12-28 23:14:00 +03:00
|
|
|
// handleFundingCounterPartyFunds processes the second workflow step for the
|
|
|
|
// lifetime of a channel reservation. Upon completion, the reservation will
|
|
|
|
// carry a completed funding transaction (minus the counterparty's input
|
|
|
|
// signatures), both versions of the commitment transaction, and our signature
|
|
|
|
// for their version of the commitment transaction.
|
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
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// Some temporary variables to cut down on the resolution verbosity.
|
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
|
|
|
}
|
|
|
|
|
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-28 23:14:00 +03:00
|
|
|
// Finally, add the 2-of-2 multi-sig output which will set up the lightning
|
|
|
|
// channel.
|
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
|
|
|
|
2016-01-07 03:15:49 +03:00
|
|
|
// Register intent for notifications related to the funding output.
|
|
|
|
// This'll allow us to properly track the number of confirmations the
|
|
|
|
// funding tx has once it has been broadcasted.
|
|
|
|
lastBlock := l.Manager.SyncedTo()
|
|
|
|
scriptAddr, err := l.Manager.ImportScript(redeemScript, &lastBlock)
|
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := l.rpc.NotifyReceived([]btcutil.Address{scriptAddr.Address()}); err != nil {
|
|
|
|
req.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-24 21:42:29 +03:00
|
|
|
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
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// Next, sign all inputs that are ours, collecting the signatures in
|
2015-11-05 23:36:19 +03:00
|
|
|
// 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-12-29 21:44:59 +03:00
|
|
|
txDetail, _ := l.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]
|
2016-02-03 06:09:54 +03:00
|
|
|
_, addrs, _, _ := txscript.ExtractPkScriptAddrs(prevOut.PkScript, l.cfg.NetParams)
|
2015-11-05 23:36:19 +03:00
|
|
|
apkh, ok := addrs[0].(*btcutil.AddressPubKeyHash)
|
|
|
|
if !ok {
|
2016-03-23 04:48:18 +03:00
|
|
|
req.err <- fmt.Errorf("only p2pkh wallet outputs are supported")
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
|
|
|
|
2015-12-29 21:44:59 +03:00
|
|
|
ai, err := l.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
|
2015-12-28 23:14:00 +03:00
|
|
|
// for our commitment tx.
|
2015-12-22 00:53:34 +03:00
|
|
|
// TODO(roasbeef): grab partial state above to avoid long attr chain
|
2015-12-31 09:30:52 +03:00
|
|
|
ourCurrentRevokeHash := pendingReservation.ourContribution.RevocationHash
|
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)
|
2016-02-16 09:13:17 +03:00
|
|
|
fundingTxIn := wire.NewTxIn(wire.NewOutPoint(&fundingNTxid, multiSigIndex), nil, nil)
|
2015-12-22 00:53:34 +03:00
|
|
|
|
|
|
|
// 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-31 09:30:52 +03:00
|
|
|
ourCurrentRevokeHash[:], theirContribution.CsvDelay,
|
2015-12-31 09:36:01 +03:00
|
|
|
initialBalance, initialBalance)
|
2015-12-22 00:53:34 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
theirCommitTx, err := createCommitTx(fundingTxIn, theirCommitKey, ourCommitKey,
|
2015-12-31 09:36:01 +03:00
|
|
|
theirContribution.RevocationHash[:], theirContribution.CsvDelay,
|
|
|
|
initialBalance, initialBalance)
|
2015-12-22 00:53:34 +03:00
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-31 09:36:01 +03:00
|
|
|
// Sort both transactions according to the agreed upon cannonical
|
|
|
|
// ordering. This lets us skip sending the entire transaction over,
|
|
|
|
// instead we'll just send signatures.
|
|
|
|
txsort.InPlaceSort(ourCommitTx)
|
|
|
|
txsort.InPlaceSort(theirCommitTx)
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// Record newly available information witin the open channel state.
|
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-28 23:13:07 +03:00
|
|
|
sigTheirCommit, err := txscript.RawTxInSignature(theirCommitTx, 0, redeemScript,
|
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
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// handleFundingCounterPartySigs is the final step in the channel reservation
|
|
|
|
// workflow. During this setp, we validate *all* the received signatures for
|
|
|
|
// inputs to the funding transaction. If any of these are invalid, we bail,
|
|
|
|
// and forcibly cancel this funding request. Additionally, we ensure that the
|
|
|
|
// signature we received from the counterparty for our version of the commitment
|
|
|
|
// transaction allows us to spend from the funding output with the addition of
|
|
|
|
// our signature.
|
2015-11-05 23:36:19 +03:00
|
|
|
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
|
2016-02-03 10:59:27 +03:00
|
|
|
sigIndex := 0
|
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 {
|
2016-02-03 10:43:12 +03:00
|
|
|
// Attach the signature so we can verify it below.
|
2016-02-03 10:59:27 +03:00
|
|
|
txin.SignatureScript = pendingReservation.theirFundingSigs[sigIndex]
|
2016-02-03 10:43:12 +03:00
|
|
|
|
2016-01-07 03:15:49 +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)
|
2016-01-07 03:15:49 +03:00
|
|
|
if output == nil {
|
2015-12-03 03:51:46 +03:00
|
|
|
// TODO(roasbeef): do this at the start to avoid wasting out time?
|
|
|
|
// 8 or a set of nodes "we" run with exposed unauthenticated RPC?
|
2016-01-07 03:15:49 +03:00
|
|
|
msg.err <- fmt.Errorf("input to funding tx does not exist: %v", err)
|
2015-12-03 03:51:46 +03:00
|
|
|
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
|
2016-01-07 03:15:49 +03:00
|
|
|
}
|
2016-02-03 10:59:27 +03:00
|
|
|
|
|
|
|
sigIndex++
|
2015-11-05 23:36:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:13:41 +03:00
|
|
|
// At this point, we can also record and verify their signature for our
|
2015-12-22 00:54:33 +03:00
|
|
|
// commitment transaction.
|
2015-12-28 23:13:41 +03:00
|
|
|
pendingReservation.theirCommitmentSig = msg.theirCommitmentSig
|
|
|
|
commitTx := pendingReservation.partialState.OurCommitTx
|
|
|
|
theirKey := pendingReservation.theirContribution.MultiSigKey
|
|
|
|
ourKey := pendingReservation.partialState.MultiSigKey
|
|
|
|
|
|
|
|
// Re-generate both the redeemScript and p2sh output. We sign the
|
|
|
|
// redeemScript script, but include the p2sh output as the subscript
|
|
|
|
// for verification.
|
|
|
|
redeemScript := pendingReservation.partialState.FundingRedeemScript
|
|
|
|
p2sh, err := scriptHashPkScript(redeemScript)
|
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, we sign our copy of the commitment transaction ourselves.
|
|
|
|
ourCommitSig, err := txscript.RawTxInSignature(commitTx, 0, redeemScript,
|
|
|
|
txscript.SigHashAll, ourKey)
|
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, create the spending scriptSig, and then verify that the script
|
|
|
|
// is complete, allowing us to spend from the funding transaction.
|
|
|
|
theirCommitSig := msg.theirCommitmentSig
|
2016-02-05 23:33:22 +03:00
|
|
|
ourKeySer := ourKey.PubKey().SerializeCompressed()
|
|
|
|
theirKeySer := theirKey.SerializeCompressed()
|
|
|
|
scriptSig, err := spendMultiSig(redeemScript, ourKeySer, ourCommitSig,
|
2016-02-25 09:17:23 +03:00
|
|
|
theirKeySer, theirCommitSig)
|
2015-12-28 23:13:41 +03:00
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
2015-12-23 07:31:17 +03:00
|
|
|
|
2015-12-28 23:13:41 +03:00
|
|
|
// Finally, create an instance of a Script VM, and ensure that the
|
|
|
|
// Script executes succesfully.
|
|
|
|
commitTx.TxIn[0].SignatureScript = scriptSig
|
|
|
|
vm, err := txscript.NewEngine(p2sh, commitTx, 0,
|
|
|
|
txscript.StandardVerifyFlags, nil)
|
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := vm.Execute(); err != nil {
|
|
|
|
msg.err <- fmt.Errorf("counterparty's commitment signature is invalid: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2015-12-22 00:54:33 +03:00
|
|
|
|
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.
|
2016-03-23 04:48:18 +03:00
|
|
|
err = l.channelDB.PutOpenChannel(pendingReservation.partialState)
|
2015-12-03 03:51:21 +03:00
|
|
|
|
2016-01-07 03:17:18 +03:00
|
|
|
// Create a goroutine to watch the chain so we can open the channel once
|
|
|
|
// the funding tx has enough confirmations.
|
|
|
|
// TODO(roasbeef): add number of confs to the confi
|
|
|
|
go l.openChannelAfterConfirmations(pendingReservation, 3)
|
|
|
|
|
2015-12-26 09:05:55 +03:00
|
|
|
msg.err <- err
|
2015-11-13 05:43:32 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 03:17:18 +03:00
|
|
|
// openChannelAfterConfirmations creates, and opens a payment channel after
|
|
|
|
// the funding transaction created within the passed channel reservation
|
|
|
|
// obtains the specified number of confirmations.
|
|
|
|
func (l *LightningWallet) openChannelAfterConfirmations(res *ChannelReservation, numConfs uint32) {
|
|
|
|
// Register with the ChainNotifier for a notification once the funding
|
|
|
|
// transaction reaches `numConfs` confirmations.
|
|
|
|
txid := res.partialState.FundingTx.TxSha()
|
2016-02-27 03:35:04 +03:00
|
|
|
confNtfn, _ := l.chainNotifier.RegisterConfirmationsNtfn(&txid, numConfs)
|
2016-01-07 03:17:18 +03:00
|
|
|
|
2016-02-03 05:40:27 +03:00
|
|
|
// Wait until the specified number of confirmations has been reached,
|
|
|
|
// or the wallet signals a shutdown.
|
|
|
|
out:
|
|
|
|
select {
|
2016-02-27 03:35:04 +03:00
|
|
|
case <-confNtfn.Confirmed:
|
2016-02-03 05:40:27 +03:00
|
|
|
break out
|
|
|
|
case <-l.quit:
|
|
|
|
res.chanOpen <- nil
|
|
|
|
return
|
|
|
|
}
|
2016-01-07 03:17:18 +03:00
|
|
|
|
|
|
|
// Finally, create and officially open the payment channel!
|
|
|
|
// TODO(roasbeef): CreationTime once tx is 'open'
|
2016-03-23 04:48:18 +03:00
|
|
|
channel, _ := newLightningChannel(l, l.chainNotifier, l.channelDB,
|
2016-01-07 03:17:18 +03:00
|
|
|
res.partialState)
|
|
|
|
res.chanOpen <- channel
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:14:00 +03:00
|
|
|
// getNextRawKey retrieves the next key within our HD key-chain for use within
|
|
|
|
// as a multi-sig key within the funding transaction, or within the commitment
|
|
|
|
// transaction's outputs.
|
2015-11-13 05:43:32 +03:00
|
|
|
// 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-30 02:09:32 +03:00
|
|
|
l.KeyGenMtx.Lock()
|
|
|
|
defer l.KeyGenMtx.Unlock()
|
2015-12-03 03:50:00 +03:00
|
|
|
|
2015-12-29 21:44:59 +03:00
|
|
|
nextAddr, err := l.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
|
|
|
}
|