2016-08-13 01:29:38 +03:00
|
|
|
package btcwallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2016-12-27 08:30:24 +03:00
|
|
|
"errors"
|
2016-08-13 01:29:38 +03:00
|
|
|
|
2016-12-22 23:53:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2017-01-06 00:56:27 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-08-13 01:29:38 +03:00
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
)
|
|
|
|
|
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) {
|
2016-12-22 23:34:51 +03:00
|
|
|
return b.rpc.GetBestBlock()
|
2016-08-13 01:29:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxOut returns the original output referenced by the passed outpoint.
|
|
|
|
//
|
|
|
|
// This method is a part of the lnwallet.BlockChainIO interface.
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcWallet) GetUtxo(txid *chainhash.Hash, index uint32) (*wire.TxOut, error) {
|
2016-08-13 01:29:38 +03:00
|
|
|
txout, err := b.rpc.GetTxOut(txid, index, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-12-27 08:30:24 +03:00
|
|
|
} else if txout == nil {
|
|
|
|
return nil, ErrOutputSpent
|
2016-08-13 01:29:38 +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.
|
2016-12-25 03:26:44 +03:00
|
|
|
Value: int64(txout.Value * 1e8),
|
2016-08-13 01:29:38 +03:00
|
|
|
PkScript: pkScript,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransaction returns the full transaction identified by the passed
|
|
|
|
// transaction ID.
|
|
|
|
//
|
|
|
|
// This method is a part of the lnwallet.BlockChainIO interface.
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcWallet) GetTransaction(txid *chainhash.Hash) (*wire.MsgTx, error) {
|
2016-08-13 01:29:38 +03:00
|
|
|
tx, err := b.rpc.GetRawTransaction(txid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.MsgTx(), nil
|
|
|
|
}
|
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) {
|
2016-12-07 18:49:58 +03:00
|
|
|
block, err := b.rpc.GetBlock(blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockHash returns the hash of the block in the best block chain at the
|
|
|
|
// 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) {
|
2016-12-07 18:49:58 +03:00
|
|
|
blockHash, err := b.rpc.GetBlockHash(blockHeight)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockHash, nil
|
|
|
|
}
|
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)
|