2016-08-13 01:29:38 +03:00
|
|
|
package btcwallet
|
|
|
|
|
|
|
|
import (
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
|
|
|
base "github.com/btcsuite/btcwallet/wallet"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/go-errors/errors"
|
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2016-08-13 01:29:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// FetchInputInfo queries for the WalletController's knowledge of the passed
|
|
|
|
// outpoint. If the base wallet determines this output is under its control,
|
|
|
|
// then the original txout should be returned. Otherwise, a non-nil error value
|
|
|
|
// of ErrNotMine should be returned instead.
|
|
|
|
//
|
|
|
|
// This is a part of the WalletController interface.
|
|
|
|
func (b *BtcWallet) FetchInputInfo(prevOut *wire.OutPoint) (*wire.TxOut, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
output *wire.TxOut
|
|
|
|
)
|
|
|
|
|
|
|
|
// First check to see if the output is already within the utxo cache.
|
2016-11-19 04:11:59 +03:00
|
|
|
// If so we can return directly saving a disk access.
|
2016-08-13 01:29:38 +03:00
|
|
|
b.cacheMtx.RLock()
|
|
|
|
if output, ok := b.utxoCache[*prevOut]; ok {
|
|
|
|
b.cacheMtx.RUnlock()
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
b.cacheMtx.RUnlock()
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// Otherwise, we manually look up the output within the tx store.
|
2017-04-24 05:19:17 +03:00
|
|
|
txid := &prevOut.Hash
|
|
|
|
txDetail, err := base.UnstableAPI(b.wallet).TxDetails(txid)
|
2016-08-13 01:29:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if txDetail == nil {
|
|
|
|
return nil, lnwallet.ErrNotMine
|
|
|
|
}
|
|
|
|
|
|
|
|
output = txDetail.TxRecord.MsgTx.TxOut[prevOut.Index]
|
|
|
|
|
|
|
|
b.cacheMtx.Lock()
|
|
|
|
b.utxoCache[*prevOut] = output
|
|
|
|
b.cacheMtx.Unlock()
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
2018-11-08 22:54:02 +03:00
|
|
|
// fetchOutputAddr attempts to fetch the managed address corresponding to the
|
2016-08-13 01:29:38 +03:00
|
|
|
// passed output script. This function is used to look up the proper key which
|
|
|
|
// should be used to sign a specified input.
|
|
|
|
func (b *BtcWallet) fetchOutputAddr(script []byte) (waddrmgr.ManagedAddress, error) {
|
|
|
|
_, addrs, _, err := txscript.ExtractPkScriptAddrs(script, b.netParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the case of a multi-sig output, several address may be extracted.
|
|
|
|
// Therefore, we simply select the key for the first address we know
|
|
|
|
// of.
|
|
|
|
for _, addr := range addrs {
|
2017-04-24 05:19:17 +03:00
|
|
|
addr, err := b.wallet.AddressInfo(addr)
|
2016-08-13 01:29:38 +03:00
|
|
|
if err == nil {
|
2017-04-24 05:19:17 +03:00
|
|
|
return addr, nil
|
2016-08-13 01:29:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 07:09:05 +03:00
|
|
|
return nil, errors.Errorf("address not found")
|
2016-08-13 01:29:38 +03:00
|
|
|
}
|
|
|
|
|
2016-11-19 04:11:59 +03:00
|
|
|
// fetchPrivKey attempts to retrieve the raw private key corresponding to the
|
2018-02-18 02:10:51 +03:00
|
|
|
// passed public key if populated, or the key descriptor path (if non-empty).
|
|
|
|
func (b *BtcWallet) fetchPrivKey(keyDesc *keychain.KeyDescriptor) (*btcec.PrivateKey, error) {
|
|
|
|
// If the key locator within the descriptor *isn't* empty, then we can
|
|
|
|
// directly derive the keys raw.
|
|
|
|
if !keyDesc.KeyLocator.IsEmpty() {
|
|
|
|
// We'll assume the special lightning key scope in this case.
|
|
|
|
scopedMgr, err := b.wallet.Manager.FetchScopedKeyManager(
|
2018-03-13 05:20:07 +03:00
|
|
|
b.chainKeyScope,
|
2018-02-18 02:10:51 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var key *btcec.PrivateKey
|
|
|
|
err = walletdb.View(b.db, func(tx walletdb.ReadTx) error {
|
|
|
|
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
path := waddrmgr.DerivationPath{
|
|
|
|
Account: uint32(keyDesc.Family),
|
|
|
|
Branch: 0,
|
|
|
|
Index: uint32(keyDesc.Index),
|
|
|
|
}
|
|
|
|
addr, err := scopedMgr.DeriveFromKeyPath(addrmgrNs, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err = addr.(waddrmgr.ManagedPubKeyAddress).PrivKey()
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hash160 := btcutil.Hash160(keyDesc.PubKey.SerializeCompressed())
|
2016-09-10 23:55:05 +03:00
|
|
|
addr, err := btcutil.NewAddressWitnessPubKeyHash(hash160, b.netParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-24 05:19:17 +03:00
|
|
|
return b.wallet.PrivKeyForAddress(addr)
|
2016-09-10 23:55:05 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:44:48 +03:00
|
|
|
// maybeTweakPrivKey examines the single and double tweak parameters on the
|
|
|
|
// passed sign descriptor and may perform a mapping on the passed private key
|
|
|
|
// in order to utilize the tweaks, if populated.
|
|
|
|
func maybeTweakPrivKey(signDesc *lnwallet.SignDescriptor,
|
|
|
|
privKey *btcec.PrivateKey) (*btcec.PrivateKey, error) {
|
|
|
|
|
|
|
|
var retPriv *btcec.PrivateKey
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case signDesc.SingleTweak != nil:
|
|
|
|
retPriv = lnwallet.TweakPrivKey(privKey,
|
|
|
|
signDesc.SingleTweak)
|
|
|
|
|
|
|
|
case signDesc.DoubleTweak != nil:
|
|
|
|
retPriv = lnwallet.DeriveRevocationPrivKey(privKey,
|
|
|
|
signDesc.DoubleTweak)
|
|
|
|
|
|
|
|
default:
|
|
|
|
retPriv = privKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return retPriv, nil
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:29:38 +03:00
|
|
|
// SignOutputRaw generates a signature for the passed transaction according to
|
|
|
|
// the data within the passed SignDescriptor.
|
|
|
|
//
|
|
|
|
// This is a part of the WalletController interface.
|
2017-07-30 21:44:48 +03:00
|
|
|
func (b *BtcWallet) SignOutputRaw(tx *wire.MsgTx,
|
|
|
|
signDesc *lnwallet.SignDescriptor) ([]byte, error) {
|
2018-02-18 02:10:51 +03:00
|
|
|
|
2016-10-16 02:02:09 +03:00
|
|
|
witnessScript := signDesc.WitnessScript
|
2016-08-13 01:29:38 +03:00
|
|
|
|
2016-11-19 04:11:59 +03:00
|
|
|
// First attempt to fetch the private key which corresponds to the
|
|
|
|
// specified public key.
|
2018-02-18 02:10:51 +03:00
|
|
|
privKey, err := b.fetchPrivKey(&signDesc.KeyDesc)
|
2016-08-13 01:29:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-30 21:44:48 +03:00
|
|
|
// If a tweak (single or double) is specified, then we'll need to use
|
|
|
|
// this tweak to derive the final private key to be used for signing
|
|
|
|
// this output.
|
|
|
|
privKey, err = maybeTweakPrivKey(signDesc, privKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-19 04:11:59 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:44:48 +03:00
|
|
|
// TODO(roasbeef): generate sighash midstate if not present?
|
|
|
|
|
2016-08-13 01:29:38 +03:00
|
|
|
amt := signDesc.Output.Value
|
2016-11-19 04:11:59 +03:00
|
|
|
sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes,
|
2017-11-06 16:22:00 +03:00
|
|
|
signDesc.InputIndex, amt, witnessScript, signDesc.HashType,
|
2016-11-19 04:11:59 +03:00
|
|
|
privKey)
|
2016-08-13 01:29:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chop off the sighash flag at the end of the signature.
|
|
|
|
return sig[:len(sig)-1], nil
|
|
|
|
}
|
|
|
|
|
2018-11-08 22:54:02 +03:00
|
|
|
// ComputeInputScript generates a complete InputScript for the passed
|
2016-08-13 01:29:38 +03:00
|
|
|
// transaction with the signature as defined within the passed SignDescriptor.
|
|
|
|
// This method is capable of generating the proper input script for both
|
2016-09-10 23:55:05 +03:00
|
|
|
// regular p2wkh output and p2wkh outputs nested within a regular p2sh output.
|
2016-08-13 01:29:38 +03:00
|
|
|
//
|
|
|
|
// This is a part of the WalletController interface.
|
|
|
|
func (b *BtcWallet) ComputeInputScript(tx *wire.MsgTx,
|
|
|
|
signDesc *lnwallet.SignDescriptor) (*lnwallet.InputScript, error) {
|
|
|
|
|
|
|
|
outputScript := signDesc.Output.PkScript
|
|
|
|
walletAddr, err := b.fetchOutputAddr(outputScript)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pka := walletAddr.(waddrmgr.ManagedPubKeyAddress)
|
|
|
|
privKey, err := pka.PrivKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var witnessProgram []byte
|
|
|
|
inputScript := &lnwallet.InputScript{}
|
|
|
|
|
2017-07-30 21:44:48 +03:00
|
|
|
switch {
|
|
|
|
|
2016-08-13 01:29:38 +03:00
|
|
|
// If we're spending p2wkh output nested within a p2sh output, then
|
|
|
|
// we'll need to attach a sigScript in addition to witness data.
|
2018-02-18 02:10:51 +03:00
|
|
|
case pka.AddrType() == waddrmgr.NestedWitnessPubKey:
|
2016-08-13 01:29:38 +03:00
|
|
|
pubKey := privKey.PubKey()
|
|
|
|
pubKeyHash := btcutil.Hash160(pubKey.SerializeCompressed())
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// Next, we'll generate a valid sigScript that will allow us to
|
2016-08-13 01:29:38 +03:00
|
|
|
// spend the p2sh output. The sigScript will contain only a
|
|
|
|
// single push of the p2wkh witness program corresponding to
|
|
|
|
// the matching public key of this address.
|
|
|
|
p2wkhAddr, err := btcutil.NewAddressWitnessPubKeyHash(pubKeyHash,
|
|
|
|
b.netParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
witnessProgram, err = txscript.PayToAddrScript(p2wkhAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bldr := txscript.NewScriptBuilder()
|
|
|
|
bldr.AddData(witnessProgram)
|
|
|
|
sigScript, err := bldr.Script()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
inputScript.ScriptSig = sigScript
|
2017-07-30 21:44:48 +03:00
|
|
|
|
2016-08-13 01:29:38 +03:00
|
|
|
// Otherwise, this is a regular p2wkh output, so we include the
|
|
|
|
// witness program itself as the subscript to generate the proper
|
|
|
|
// sighash digest. As part of the new sighash digest algorithm, the
|
|
|
|
// p2wkh witness program will be expanded into a regular p2kh
|
|
|
|
// script.
|
|
|
|
default:
|
|
|
|
witnessProgram = outputScript
|
|
|
|
}
|
|
|
|
|
2017-07-30 21:44:48 +03:00
|
|
|
// If a tweak (single or double) is specified, then we'll need to use
|
|
|
|
// this tweak to derive the final private key to be used for signing
|
|
|
|
// this output.
|
|
|
|
privKey, err = maybeTweakPrivKey(signDesc, privKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:29:38 +03:00
|
|
|
// Generate a valid witness stack for the input.
|
2016-09-10 23:55:05 +03:00
|
|
|
// TODO(roasbeef): adhere to passed HashType
|
2017-08-25 04:55:27 +03:00
|
|
|
witnessScript, err := txscript.WitnessSignature(tx, signDesc.SigHashes,
|
2016-08-13 01:29:38 +03:00
|
|
|
signDesc.InputIndex, signDesc.Output.Value, witnessProgram,
|
2017-11-06 16:22:00 +03:00
|
|
|
signDesc.HashType, privKey, true)
|
2016-08-13 01:29:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
inputScript.Witness = witnessScript
|
|
|
|
|
|
|
|
return inputScript, nil
|
|
|
|
}
|
2016-12-22 23:53:20 +03:00
|
|
|
|
|
|
|
// A compile time check to ensure that BtcWallet implements the Signer
|
|
|
|
// interface.
|
|
|
|
var _ lnwallet.Signer = (*BtcWallet)(nil)
|
2017-03-27 20:13:40 +03:00
|
|
|
|
2017-04-14 21:05:18 +03:00
|
|
|
// SignMessage attempts to sign a target message with the private key that
|
|
|
|
// corresponds to the passed public key. If the target private key is unable to
|
|
|
|
// be found, then an error will be returned. The actual digest signed is the
|
|
|
|
// double SHA-256 of the passed message.
|
2017-04-01 15:33:17 +03:00
|
|
|
//
|
2017-04-14 21:05:18 +03:00
|
|
|
// NOTE: This is a part of the MessageSigner interface.
|
|
|
|
func (b *BtcWallet) SignMessage(pubKey *btcec.PublicKey,
|
|
|
|
msg []byte) (*btcec.Signature, error) {
|
2017-03-27 20:13:40 +03:00
|
|
|
|
|
|
|
// First attempt to fetch the private key which corresponds to the
|
|
|
|
// specified public key.
|
2018-02-18 02:10:51 +03:00
|
|
|
privKey, err := b.fetchPrivKey(&keychain.KeyDescriptor{
|
|
|
|
PubKey: pubKey,
|
|
|
|
})
|
2017-03-27 20:13:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-14 21:05:18 +03:00
|
|
|
// Double hash and sign the data.
|
|
|
|
msgDigest := chainhash.DoubleHashB(msg)
|
|
|
|
sign, err := privKey.Sign(msgDigest)
|
2017-03-27 20:13:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("unable sign the message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sign, nil
|
|
|
|
}
|
2017-04-14 21:05:18 +03:00
|
|
|
|
|
|
|
// A compile time check to ensure that BtcWallet implements the MessageSigner
|
|
|
|
// interface.
|
|
|
|
var _ lnwallet.MessageSigner = (*BtcWallet)(nil)
|