2016-08-13 01:29:38 +03:00
|
|
|
package btcwallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2016-12-27 08:30:24 +03:00
|
|
|
"errors"
|
2017-05-25 03:34:31 +03:00
|
|
|
"fmt"
|
2016-08-13 01:29:38 +03:00
|
|
|
|
2017-05-25 03:34:31 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
|
|
|
|
"github.com/lightninglabs/neutrino"
|
2016-12-22 23:53:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2017-05-25 03:34:31 +03:00
|
|
|
"github.com/roasbeef/btcwallet/chain"
|
|
|
|
"github.com/roasbeef/btcwallet/waddrmgr"
|
2016-08-13 01:29:38 +03:00
|
|
|
)
|
|
|
|
|
2016-12-27 08:30:24 +03:00
|
|
|
var (
|
|
|
|
// ErrOutputSpent is returned by the GetUtxo method if the target output
|
|
|
|
// for lookup has already been spent.
|
|
|
|
ErrOutputSpent = errors.New("target output has been spent")
|
|
|
|
)
|
|
|
|
|
2016-12-22 23:34:51 +03:00
|
|
|
// GetBestBlock returns the current height and hash of the best known block
|
|
|
|
// within the main chain.
|
2016-08-13 01:29:38 +03:00
|
|
|
//
|
|
|
|
// This method is a part of the lnwallet.BlockChainIO interface.
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcWallet) GetBestBlock() (*chainhash.Hash, int32, error) {
|
2017-05-25 03:34:31 +03:00
|
|
|
switch backend := b.chain.(type) {
|
|
|
|
|
2017-06-05 07:06:52 +03:00
|
|
|
case *chain.NeutrinoClient:
|
|
|
|
header, height, err := backend.CS.BlockHeaders.ChainTip()
|
2017-05-25 03:34:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blockHash := header.BlockHash()
|
|
|
|
return &blockHash, int32(height), nil
|
|
|
|
|
|
|
|
case *chain.RPCClient:
|
|
|
|
return backend.GetBestBlock()
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, -1, fmt.Errorf("unknown backend")
|
|
|
|
}
|
2016-08-13 01:29:38 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// GetUtxo returns the original output referenced by the passed outpoint.
|
2016-08-13 01:29:38 +03:00
|
|
|
//
|
|
|
|
// This method is a part of the lnwallet.BlockChainIO interface.
|
2017-05-25 03:34:31 +03:00
|
|
|
func (b *BtcWallet) GetUtxo(op *wire.OutPoint, heightHint uint32) (*wire.TxOut, error) {
|
|
|
|
switch backend := b.chain.(type) {
|
2016-08-13 01:29:38 +03:00
|
|
|
|
2017-06-05 07:06:52 +03:00
|
|
|
case *chain.NeutrinoClient:
|
2017-05-25 03:34:31 +03:00
|
|
|
spendReport, err := backend.CS.GetUtxo(
|
|
|
|
neutrino.WatchOutPoints(*op),
|
|
|
|
neutrino.StartBlock(&waddrmgr.BlockStamp{
|
|
|
|
Height: int32(heightHint),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-13 01:29:38 +03:00
|
|
|
|
2017-05-25 03:34:31 +03:00
|
|
|
if spendReport != nil && spendReport.SpendingTx != nil {
|
|
|
|
return nil, ErrOutputSpent
|
|
|
|
}
|
2016-08-13 01:29:38 +03:00
|
|
|
|
2017-05-25 03:34:31 +03:00
|
|
|
return spendReport.Output, nil
|
|
|
|
|
|
|
|
case *chain.RPCClient:
|
|
|
|
txout, err := backend.GetTxOut(&op.Hash, op.Index, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if txout == nil {
|
|
|
|
return nil, ErrOutputSpent
|
|
|
|
}
|
2016-08-13 01:29:38 +03:00
|
|
|
|
2017-05-25 03:34:31 +03:00
|
|
|
pkScript, err := hex.DecodeString(txout.ScriptPubKey.Hex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &wire.TxOut{
|
|
|
|
// Sadly, gettxout returns the output value in BTC
|
|
|
|
// instead of satoshis.
|
|
|
|
Value: int64(txout.Value * 1e8),
|
|
|
|
PkScript: pkScript,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown backend")
|
|
|
|
}
|
2016-08-13 01:29:38 +03:00
|
|
|
}
|
2016-12-07 18:49:58 +03:00
|
|
|
|
|
|
|
// GetBlock returns a raw block from the server given its hash.
|
|
|
|
//
|
|
|
|
// This method is a part of the lnwallet.BlockChainIO interface.
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcWallet) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error) {
|
2017-05-25 03:34:31 +03:00
|
|
|
switch backend := b.chain.(type) {
|
|
|
|
|
2017-06-05 07:06:52 +03:00
|
|
|
case *chain.NeutrinoClient:
|
2017-05-25 03:34:31 +03:00
|
|
|
block, err := backend.CS.GetBlockFromNetwork(*blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block.MsgBlock(), nil
|
|
|
|
|
|
|
|
case *chain.RPCClient:
|
|
|
|
block, err := backend.GetBlock(blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block, nil
|
2016-12-07 18:49:58 +03:00
|
|
|
|
2017-05-25 03:34:31 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown backend")
|
|
|
|
}
|
2016-12-07 18:49:58 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// GetBlockHash returns the hash of the block in the best blockchain at the
|
2016-12-07 18:49:58 +03:00
|
|
|
// given height.
|
|
|
|
//
|
|
|
|
// This method is a part of the lnwallet.BlockChainIO interface.
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcWallet) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
|
2017-05-25 03:34:31 +03:00
|
|
|
switch backend := b.chain.(type) {
|
|
|
|
|
2017-06-05 07:06:52 +03:00
|
|
|
case *chain.NeutrinoClient:
|
|
|
|
height := uint32(blockHeight)
|
|
|
|
blockHeader, err := backend.CS.BlockHeaders.FetchHeaderByHeight(height)
|
2017-05-25 03:34:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blockHash := blockHeader.BlockHash()
|
|
|
|
return &blockHash, nil
|
2016-12-07 18:49:58 +03:00
|
|
|
|
2017-05-25 03:34:31 +03:00
|
|
|
case *chain.RPCClient:
|
|
|
|
blockHash, err := backend.GetBlockHash(blockHeight)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockHash, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown backend")
|
|
|
|
}
|
2016-12-07 18:49:58 +03:00
|
|
|
}
|
2016-12-22 23:53:20 +03:00
|
|
|
|
|
|
|
// A compile time check to ensure that BtcWallet implements the BlockChainIO
|
|
|
|
// interface.
|
|
|
|
var _ lnwallet.WalletController = (*BtcWallet)(nil)
|