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"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/go-errors/errors"
|
2019-01-16 17:47:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2018-07-31 10:17:17 +03:00
|
|
|
"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.
|
2019-08-20 00:26:36 +03:00
|
|
|
func (b *BtcWallet) FetchInputInfo(prevOut *wire.OutPoint) (*lnwallet.Utxo, error) {
|
2021-03-10 01:12:26 +03:00
|
|
|
_, txOut, _, confirmations, err := b.wallet.FetchInputInfo(prevOut)
|
2016-08-13 01:29:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-11-10 04:38:01 +03:00
|
|
|
}
|
2016-08-13 01:29:38 +03:00
|
|
|
|
2019-08-20 00:26:36 +03:00
|
|
|
// Then, we'll populate all of the information required by the struct.
|
|
|
|
addressType := lnwallet.UnknownAddressType
|
|
|
|
switch {
|
2020-10-01 17:21:42 +03:00
|
|
|
case txscript.IsPayToWitnessPubKeyHash(txOut.PkScript):
|
2019-08-20 00:26:36 +03:00
|
|
|
addressType = lnwallet.WitnessPubKey
|
2020-10-01 17:21:42 +03:00
|
|
|
case txscript.IsPayToScriptHash(txOut.PkScript):
|
2019-08-20 00:26:36 +03:00
|
|
|
addressType = lnwallet.NestedWitnessPubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnwallet.Utxo{
|
2020-10-01 17:21:42 +03:00
|
|
|
AddressType: addressType,
|
|
|
|
Value: btcutil.Amount(txOut.Value),
|
|
|
|
PkScript: txOut.PkScript,
|
|
|
|
Confirmations: confirmations,
|
2019-08-20 00:26:36 +03:00
|
|
|
OutPoint: *prevOut,
|
|
|
|
}, nil
|
2016-08-13 01:29:38 +03:00
|
|
|
}
|
|
|
|
|
lnwallet: when signing create account if not found
In this commit, we address an edge case that can happen a user rescans
w/ their seed, while retaining their existing `channel.db`. Once they
rescan, if they go to sign for a channel sweep for example, the
commitment key family (actually an account) may not yet have been
created, causing the signing attempt to fail.
We remedy this always creating the account if we go to sign, and the
account isn't found. The change has been structured to make this the
exception, so we'll avoid always needing to do 2 DB hits (check if
account exists, sign), each time we sign.
A new test has been added to exercise this behavior. If the diff from
the `signer.go` file is removed, then the test will fail.
2019-08-21 05:08:03 +03:00
|
|
|
// deriveFromKeyLoc attempts to derive a private key using a fully specified
|
|
|
|
// KeyLocator.
|
|
|
|
func deriveFromKeyLoc(scopedMgr *waddrmgr.ScopedKeyManager,
|
|
|
|
addrmgrNs walletdb.ReadWriteBucket,
|
|
|
|
keyLoc keychain.KeyLocator) (*btcec.PrivateKey, error) {
|
|
|
|
|
|
|
|
path := waddrmgr.DerivationPath{
|
2021-03-10 01:12:26 +03:00
|
|
|
InternalAccount: uint32(keyLoc.Family),
|
|
|
|
Account: uint32(keyLoc.Family),
|
|
|
|
Branch: 0,
|
|
|
|
Index: keyLoc.Index,
|
lnwallet: when signing create account if not found
In this commit, we address an edge case that can happen a user rescans
w/ their seed, while retaining their existing `channel.db`. Once they
rescan, if they go to sign for a channel sweep for example, the
commitment key family (actually an account) may not yet have been
created, causing the signing attempt to fail.
We remedy this always creating the account if we go to sign, and the
account isn't found. The change has been structured to make this the
exception, so we'll avoid always needing to do 2 DB hits (check if
account exists, sign), each time we sign.
A new test has been added to exercise this behavior. If the diff from
the `signer.go` file is removed, then the test will fail.
2019-08-21 05:08:03 +03:00
|
|
|
}
|
|
|
|
addr, err := scopedMgr.DeriveFromKeyPath(addrmgrNs, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr.(waddrmgr.ManagedPubKeyAddress).PrivKey()
|
|
|
|
}
|
|
|
|
|
lnwallet: fix key derivation for very first key in family
In this commit, we fix an existing bug that would cause us to be unable
to derive the very first key in a key family if the wallet hadn't
already derived it in the past. This can happen if a user keeps their
same `channel.db`, but restores their wallet resulting in fresh
`wallet.db` state.
This is an existing issue due to the fact that we don't properly
distinguish between an empty key locator, and the very first key in a
`KeyFamily`: `(0, 0)`. Atm, `KeyLoactor{0, 0}.IsEmpty() == True`,
causing us to be unable to retrieve this key in certain cases since we
fall through and attempt address based derivation.
In order to remedy this, we add a new special case (until we upgrade
`KeyLoactor` formats, but needed for legacy reasons) to _try_ a regular
`KeyLoactor` based derivation if we fail to derive via address, and this
is an "empty" key loc. This has been tested in the field and shown to
work, with the one downside that in this "hot swap restoration" case,
we'll hit the database twice to derive the key.
2019-07-09 02:58:54 +03:00
|
|
|
// deriveKeyByLocator attempts to derive a key stored in the wallet given a
|
|
|
|
// valid key locator.
|
|
|
|
func (b *BtcWallet) deriveKeyByLocator(keyLoc keychain.KeyLocator) (*btcec.PrivateKey, error) {
|
|
|
|
// We'll assume the special lightning key scope in this case.
|
|
|
|
scopedMgr, err := b.wallet.Manager.FetchScopedKeyManager(
|
|
|
|
b.chainKeyScope,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var key *btcec.PrivateKey
|
lnwallet: when signing create account if not found
In this commit, we address an edge case that can happen a user rescans
w/ their seed, while retaining their existing `channel.db`. Once they
rescan, if they go to sign for a channel sweep for example, the
commitment key family (actually an account) may not yet have been
created, causing the signing attempt to fail.
We remedy this always creating the account if we go to sign, and the
account isn't found. The change has been structured to make this the
exception, so we'll avoid always needing to do 2 DB hits (check if
account exists, sign), each time we sign.
A new test has been added to exercise this behavior. If the diff from
the `signer.go` file is removed, then the test will fail.
2019-08-21 05:08:03 +03:00
|
|
|
err = walletdb.Update(b.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
key, err = deriveFromKeyLoc(scopedMgr, addrmgrNs, keyLoc)
|
|
|
|
if waddrmgr.IsError(err, waddrmgr.ErrAccountNotFound) {
|
|
|
|
// If we've reached this point, then the account
|
|
|
|
// doesn't yet exist, so we'll create it now to ensure
|
|
|
|
// we can sign.
|
|
|
|
acctErr := scopedMgr.NewRawAccount(
|
|
|
|
addrmgrNs, uint32(keyLoc.Family),
|
|
|
|
)
|
|
|
|
if acctErr != nil {
|
|
|
|
return acctErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we know the account exists, we'll attempt
|
|
|
|
// to re-derive the private key.
|
|
|
|
key, err = deriveFromKeyLoc(
|
|
|
|
scopedMgr, addrmgrNs, keyLoc,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
lnwallet: fix key derivation for very first key in family
In this commit, we fix an existing bug that would cause us to be unable
to derive the very first key in a key family if the wallet hadn't
already derived it in the past. This can happen if a user keeps their
same `channel.db`, but restores their wallet resulting in fresh
`wallet.db` state.
This is an existing issue due to the fact that we don't properly
distinguish between an empty key locator, and the very first key in a
`KeyFamily`: `(0, 0)`. Atm, `KeyLoactor{0, 0}.IsEmpty() == True`,
causing us to be unable to retrieve this key in certain cases since we
fall through and attempt address based derivation.
In order to remedy this, we add a new special case (until we upgrade
`KeyLoactor` formats, but needed for legacy reasons) to _try_ a regular
`KeyLoactor` based derivation if we fail to derive via address, and this
is an "empty" key loc. This has been tested in the field and shown to
work, with the one downside that in this "hot swap restoration" case,
we'll hit the database twice to derive the key.
2019-07-09 02:58:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
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.
|
lnwallet: fix key derivation for very first key in family
In this commit, we fix an existing bug that would cause us to be unable
to derive the very first key in a key family if the wallet hadn't
already derived it in the past. This can happen if a user keeps their
same `channel.db`, but restores their wallet resulting in fresh
`wallet.db` state.
This is an existing issue due to the fact that we don't properly
distinguish between an empty key locator, and the very first key in a
`KeyFamily`: `(0, 0)`. Atm, `KeyLoactor{0, 0}.IsEmpty() == True`,
causing us to be unable to retrieve this key in certain cases since we
fall through and attempt address based derivation.
In order to remedy this, we add a new special case (until we upgrade
`KeyLoactor` formats, but needed for legacy reasons) to _try_ a regular
`KeyLoactor` based derivation if we fail to derive via address, and this
is an "empty" key loc. This has been tested in the field and shown to
work, with the one downside that in this "hot swap restoration" case,
we'll hit the database twice to derive the key.
2019-07-09 02:58:54 +03:00
|
|
|
emptyLocator := keyDesc.KeyLocator.IsEmpty()
|
|
|
|
if !emptyLocator {
|
|
|
|
return b.deriveKeyByLocator(keyDesc.KeyLocator)
|
2018-02-18 02:10:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
lnwallet: fix key derivation for very first key in family
In this commit, we fix an existing bug that would cause us to be unable
to derive the very first key in a key family if the wallet hadn't
already derived it in the past. This can happen if a user keeps their
same `channel.db`, but restores their wallet resulting in fresh
`wallet.db` state.
This is an existing issue due to the fact that we don't properly
distinguish between an empty key locator, and the very first key in a
`KeyFamily`: `(0, 0)`. Atm, `KeyLoactor{0, 0}.IsEmpty() == True`,
causing us to be unable to retrieve this key in certain cases since we
fall through and attempt address based derivation.
In order to remedy this, we add a new special case (until we upgrade
`KeyLoactor` formats, but needed for legacy reasons) to _try_ a regular
`KeyLoactor` based derivation if we fail to derive via address, and this
is an "empty" key loc. This has been tested in the field and shown to
work, with the one downside that in this "hot swap restoration" case,
we'll hit the database twice to derive the key.
2019-07-09 02:58:54 +03:00
|
|
|
// Otherwise, we'll attempt to derive the key based on the address.
|
|
|
|
// This will only work if we've already derived this address in the
|
|
|
|
// past, since the wallet relies on a mapping of addr -> key.
|
|
|
|
key, err := b.wallet.PrivKeyForAddress(addr)
|
|
|
|
switch {
|
|
|
|
// If we didn't find this key in the wallet, then there's a chance that
|
|
|
|
// this is actually an "empty" key locator. The legacy KeyLocator
|
|
|
|
// format failed to properly distinguish an empty key locator from the
|
|
|
|
// very first in the index (0, 0).IsEmpty() == true.
|
|
|
|
case waddrmgr.IsError(err, waddrmgr.ErrAddressNotFound) && emptyLocator:
|
|
|
|
return b.deriveKeyByLocator(keyDesc.KeyLocator)
|
|
|
|
|
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
default:
|
|
|
|
return key, nil
|
|
|
|
}
|
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.
|
2019-01-16 17:47:43 +03:00
|
|
|
func maybeTweakPrivKey(signDesc *input.SignDescriptor,
|
2017-07-30 21:44:48 +03:00
|
|
|
privKey *btcec.PrivateKey) (*btcec.PrivateKey, error) {
|
|
|
|
|
|
|
|
var retPriv *btcec.PrivateKey
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case signDesc.SingleTweak != nil:
|
2019-01-16 17:47:43 +03:00
|
|
|
retPriv = input.TweakPrivKey(privKey,
|
2017-07-30 21:44:48 +03:00
|
|
|
signDesc.SingleTweak)
|
|
|
|
|
|
|
|
case signDesc.DoubleTweak != nil:
|
2019-01-16 17:47:43 +03:00
|
|
|
retPriv = input.DeriveRevocationPrivKey(privKey,
|
2017-07-30 21:44:48 +03:00
|
|
|
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,
|
2020-04-06 03:06:38 +03:00
|
|
|
signDesc *input.SignDescriptor) (input.Signature, 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
|
2018-11-18 07:46:51 +03:00
|
|
|
sig, err := txscript.RawTxInWitnessSignature(
|
|
|
|
tx, signDesc.SigHashes, signDesc.InputIndex, amt,
|
|
|
|
witnessScript, signDesc.HashType, 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.
|
2020-04-06 03:06:38 +03:00
|
|
|
return btcec.ParseDERSignature(sig[:len(sig)-1], btcec.S256())
|
2016-08-13 01:29:38 +03:00
|
|
|
}
|
|
|
|
|
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,
|
2019-01-16 17:47:43 +03:00
|
|
|
signDesc *input.SignDescriptor) (*input.Script, error) {
|
2016-08-13 01:29:38 +03:00
|
|
|
|
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.
|
2020-10-01 17:21:42 +03:00
|
|
|
privKeyTweaker := func(k *btcec.PrivateKey) (*btcec.PrivateKey, error) {
|
|
|
|
return maybeTweakPrivKey(signDesc, k)
|
2017-07-30 21:44:48 +03:00
|
|
|
}
|
|
|
|
|
2020-10-01 17:21:42 +03:00
|
|
|
// Let the wallet compute the input script now.
|
|
|
|
witness, sigScript, err := b.wallet.ComputeInputScript(
|
|
|
|
tx, signDesc.Output, signDesc.InputIndex, signDesc.SigHashes,
|
|
|
|
signDesc.HashType, privKeyTweaker,
|
2018-11-18 07:46:51 +03:00
|
|
|
)
|
2016-08-13 01:29:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:21:42 +03:00
|
|
|
return &input.Script{
|
|
|
|
Witness: witness,
|
|
|
|
SigScript: sigScript,
|
|
|
|
}, nil
|
2016-08-13 01:29:38 +03:00
|
|
|
}
|
2016-12-22 23:53:20 +03:00
|
|
|
|
|
|
|
// A compile time check to ensure that BtcWallet implements the Signer
|
|
|
|
// interface.
|
2019-01-16 17:47:43 +03:00
|
|
|
var _ input.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,
|
2020-04-06 03:06:38 +03:00
|
|
|
msg []byte) (input.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)
|