Skip to content

Commit

Permalink
Merge pull request #171 from lightninglabs/signpsbt-improvements
Browse files Browse the repository at this point in the history
signpsbt: implement Taproot keyspend signing
  • Loading branch information
guggero authored Dec 27, 2024
2 parents 5011fd5 + 6a9addb commit 567fdb2
Show file tree
Hide file tree
Showing 45 changed files with 256 additions and 165 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ env:
# go needs absolute directories, using the $HOME variable doesn't work here.
GOCACHE: /home/runner/work/go/pkg/build
GOPATH: /home/runner/work/go
GO_VERSION: 1.22.3
GO_VERSION: 1.22.6

jobs:
########################
Expand Down
8 changes: 7 additions & 1 deletion cmd/chantools/derivekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
const deriveKeyFormat = `
Path: %s
Network: %s
Master Fingerprint: %x
Public key: %x
Extended public key (xpub): %v
Address: %v
Expand Down Expand Up @@ -110,8 +111,13 @@ func deriveKey(extendedKey *hdkeychain.ExtendedKey, path string,
privKey, xPriv = wif.String(), child.String()
}

_, fingerPrintBytes, err := fingerprint(extendedKey)
if err != nil {
return fmt.Errorf("could not get fingerprint: %w", err)
}

result := fmt.Sprintf(
deriveKeyFormat, path, chainParams.Name,
deriveKeyFormat, path, chainParams.Name, fingerPrintBytes,
pubKey.SerializeCompressed(), neutered, addrP2WKH, addrP2PKH,
addrP2TR, privKey, xPriv,
)
Expand Down
6 changes: 3 additions & 3 deletions cmd/chantools/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ const (
// version is the current version of the tool. It is set during build.
// NOTE: When changing this, please also update the version in the
// download link shown in the README.
version = "0.13.4"
version = "0.13.5"
na = "n/a"

// lndVersion is the current version of lnd that we support. This is
// shown in some commands that affect the database and its migrations.
// Run "make docs" after changing this value.
lndVersion = "v0.18.3-beta"
lndVersion = "v0.18.4-beta"

Commit = ""
)
Expand Down Expand Up @@ -162,7 +162,7 @@ func newRootKey(cmd *cobra.Command, desc string) *rootKey {
)
cmd.Flags().StringVar(
&r.WalletDB, "walletdb", "", "read the seed/master root key "+
"to use fro "+desc+" from an lnd wallet.db file "+
"to use for "+desc+" from an lnd wallet.db file "+
"instead of asking for a seed or providing the "+
"--rootkey flag",
)
Expand Down
45 changes: 33 additions & 12 deletions cmd/chantools/signpsbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,37 @@ func signPsbt(rootKey *hdkeychain.ExtendedKey,
}
utxo := pIn.WitnessUtxo

localPrivateKey, err := localKey.ECPrivKey()
if err != nil {
return fmt.Errorf("error getting private key: %w", err)
}

// The signing is a bit different for P2WPKH, we need to specify
// the pk script as the witness script.
var witnessScript []byte
if txscript.IsPayToWitnessPubKeyHash(utxo.PkScript) {
switch {
case txscript.IsPayToWitnessPubKeyHash(utxo.PkScript):
witnessScript = utxo.PkScript
} else {

case txscript.IsPayToTaproot(utxo.PkScript):
err := signer.AddTaprootSignature(
packet, inputIndex, utxo, localPrivateKey,
)
if err != nil {
return fmt.Errorf("error adding taproot "+
"signature: %w", err)
}

continue

default:
if len(pIn.WitnessScript) == 0 {
return fmt.Errorf("invalid PSBT, input %d is "+
"missing witness script", inputIndex)
}
witnessScript = pIn.WitnessScript
}

localPrivateKey, err := localKey.ECPrivKey()
if err != nil {
return fmt.Errorf("error getting private key: %w", err)
}

// Do we already have a partial signature for our key?
localPubKey := localPrivateKey.PubKey().SerializeCompressed()
haveSig := false
Expand Down Expand Up @@ -221,14 +234,11 @@ func signPsbt(rootKey *hdkeychain.ExtendedKey,
func findMatchingDerivationPath(rootKey *hdkeychain.ExtendedKey,
pIn *psbt.PInput) ([]uint32, error) {

pubKey, err := rootKey.ECPubKey()
masterFingerprint, _, err := fingerprint(rootKey)
if err != nil {
return nil, fmt.Errorf("error getting public key: %w", err)
}

pubKeyHash := btcutil.Hash160(pubKey.SerializeCompressed())
fingerprint := binary.LittleEndian.Uint32(pubKeyHash[:4])

if len(pIn.Bip32Derivation) == 0 {
return nil, errNoPathFound
}
Expand All @@ -246,10 +256,21 @@ func findMatchingDerivationPath(rootKey *hdkeychain.ExtendedKey,

// The normal case, where a derivation path has the master
// fingerprint set.
if derivation.MasterKeyFingerprint == fingerprint {
if derivation.MasterKeyFingerprint == masterFingerprint {
return derivation.Bip32Path, nil
}
}

return nil, errNoPathFound
}

func fingerprint(rootKey *hdkeychain.ExtendedKey) (uint32, []byte, error) {
pubKey, err := rootKey.ECPubKey()
if err != nil {
return 0, nil, fmt.Errorf("error getting public key: %w", err)
}

pubKeyHash := btcutil.Hash160(pubKey.SerializeCompressed())
fpBytes := pubKeyHash[:4]
return binary.LittleEndian.Uint32(fpBytes), fpBytes, nil
}
6 changes: 5 additions & 1 deletion cmd/chantools/zombierecovery_makeoffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightninglabs/chantools/lnd"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwallet"
Expand Down Expand Up @@ -650,7 +652,9 @@ func matchScript(address string, key1, key2 *btcec.PublicKey,
pkScript, nil

case *btcutil.AddressTaproot:
pkScript, _, err := input.GenTaprootFundingScript(key1, key2, 0)
pkScript, _, err := input.GenTaprootFundingScript(
key1, key2, 0, fn.None[chainhash.Hash](),
)
if err != nil {
return false, nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_chanbackup.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ chantools chanbackup \
-h, --help help for chanbackup
--multi_file string lnd channel.backup file to create
--rootkey string BIP32 HD root key of the wallet to use for creating the backup; leave empty to prompt for lnd 24 word aezeed
--walletdb string read the seed/master root key to use fro creating the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for creating the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_closepoolaccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ chantools closepoolaccount \
--publish publish sweep TX to the chain API instead of just printing the TX
--rootkey string BIP32 HD root key of the wallet to use for deriving keys; leave empty to prompt for lnd 24 word aezeed
--sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically
--walletdb string read the seed/master root key to use fro deriving keys from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for deriving keys from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_createwallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ chantools createwallet \
--generateseed generate a new seed instead of using an existing one
-h, --help help for createwallet
--rootkey string BIP32 HD root key of the wallet to use for creating the new wallet; leave empty to prompt for lnd 24 word aezeed
--walletdb string read the seed/master root key to use fro creating the new wallet from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for creating the new wallet from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdbdir string the folder to create the new wallet.db file in
```

Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_deletepayments.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ If only the failed payments should be deleted (and not the successful ones), the

CAUTION: Running this command will make it impossible to use the channel DB
with an older version of lnd. Downgrading is not possible and you'll need to
run lnd v0.18.3-beta or later after using this command!'
run lnd v0.18.4-beta or later after using this command!'

```
chantools deletepayments [flags]
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_derivekey.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ chantools derivekey --identity
--neuter don't output private key(s), only public key(s)
--path string BIP32 derivation path to derive; must start with "m/"
--rootkey string BIP32 HD root key of the wallet to use for decrypting the backup; leave empty to prompt for lnd 24 word aezeed
--walletdb string read the seed/master root key to use fro decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_doublespendinputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ chantools doublespendinputs \
--recoverywindow uint32 number of keys to scan per internal/external branch; output will consist of double this amount of keys (default 2500)
--rootkey string BIP32 HD root key of the wallet to use for deriving the input keys; leave empty to prompt for lnd 24 word aezeed
--sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically
--walletdb string read the seed/master root key to use fro deriving the input keys from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for deriving the input keys from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_dropchannelgraph.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ without removing any other data.

CAUTION: Running this command will make it impossible to use the channel DB
with an older version of lnd. Downgrading is not possible and you'll need to
run lnd v0.18.3-beta or later after using this command!'
run lnd v0.18.4-beta or later after using this command!'

```
chantools dropchannelgraph [flags]
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_dropgraphzombies.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ be helpful to fix a graph that is out of sync with the network.

CAUTION: Running this command will make it impossible to use the channel DB
with an older version of lnd. Downgrading is not possible and you'll need to
run lnd v0.18.3-beta or later after using this command!'
run lnd v0.18.4-beta or later after using this command!'

```
chantools dropgraphzombies [flags]
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_dumpbackup.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ chantools dumpbackup \
-h, --help help for dumpbackup
--multi_file string lnd channel.backup file to dump
--rootkey string BIP32 HD root key of the wallet to use for decrypting the backup; leave empty to prompt for lnd 24 word aezeed
--walletdb string read the seed/master root key to use fro decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
4 changes: 2 additions & 2 deletions doc/chantools_fakechanbackup.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ chantools fakechanbackup --from_channel_graph lncli_describegraph.json \
--channelpoint string funding transaction outpoint of the channel to rescue (<txid>:<txindex>) as it is displayed on 1ml.com
--from_channel_graph string the full LN channel graph in the JSON format that the 'lncli describegraph' returns
-h, --help help for fakechanbackup
--multi_file string the fake channel backup file to create (default "results/fake-2024-10-28-13-17-27.backup")
--multi_file string the fake channel backup file to create (default "results/fake-2024-12-27-13-52-07.backup")
--remote_node_addr string the remote node connection information in the format pubkey@host:port
--rootkey string BIP32 HD root key of the wallet to use for encrypting the backup; leave empty to prompt for lnd 24 word aezeed
--short_channel_id string the short channel ID in the format <blockheight>x<transactionindex>x<outputindex>
--walletdb string read the seed/master root key to use fro encrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for encrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_filterbackup.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ chantools filterbackup \
-h, --help help for filterbackup
--multi_file string lnd channel.backup file to filter
--rootkey string BIP32 HD root key of the wallet to use for decrypting the backup; leave empty to prompt for lnd 24 word aezeed
--walletdb string read the seed/master root key to use fro decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_fixoldbackup.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ chantools fixoldbackup \
-h, --help help for fixoldbackup
--multi_file string lnd channel.backup file to fix
--rootkey string BIP32 HD root key of the wallet to use for decrypting the backup; leave empty to prompt for lnd 24 word aezeed
--walletdb string read the seed/master root key to use fro decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_forceclose.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ chantools forceclose \
--pendingchannels string channel input is in the format of lncli's pendingchannels format; specify '-' to read from stdin
--publish publish force-closing TX to the chain API instead of just printing the TX
--rootkey string BIP32 HD root key of the wallet to use for decrypting the backup; leave empty to prompt for lnd 24 word aezeed
--walletdb string read the seed/master root key to use fro decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_genimportscript.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ chantools genimportscript --format bitcoin-cli \
--rescanfrom uint32 block number to rescan from; will be set automatically from the wallet birthday if the lnd 24 word aezeed is entered (default 500000)
--rootkey string BIP32 HD root key of the wallet to use for decrypting the backup; leave empty to prompt for lnd 24 word aezeed
--stdout write generated import script to standard out instead of writing it to a file
--walletdb string read the seed/master root key to use fro decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_migratedb.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ needs to read the database content.

CAUTION: Running this command will make it impossible to use the channel DB
with an older version of lnd. Downgrading is not possible and you'll need to
run lnd v0.18.3-beta or later after using this command!'
run lnd v0.18.4-beta or later after using this command!'

```
chantools migratedb [flags]
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_pullanchor.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ chantools pullanchor \
-h, --help help for pullanchor
--rootkey string BIP32 HD root key of the wallet to use for deriving keys; leave empty to prompt for lnd 24 word aezeed
--sponsorinput string the input to use to sponsor the CPFP transaction; must be owned by the lnd node that owns the anchor output
--walletdb string read the seed/master root key to use fro deriving keys from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for deriving keys from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_recoverloopin.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ chantools recoverloopin \
--sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically
--txid string transaction id of the on-chain transaction that created the HTLC
--vout uint32 output index of the on-chain transaction that created the HTLC
--walletdb string read the seed/master root key to use fro deriving starting key from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for deriving starting key from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_removechannel.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ channel was never confirmed on chain!

CAUTION: Running this command will make it impossible to use the channel DB
with an older version of lnd. Downgrading is not possible and you'll need to
run lnd v0.18.3-beta or later after using this command!
run lnd v0.18.4-beta or later after using this command!

```
chantools removechannel [flags]
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_rescueclosed.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ chantools rescueclosed --fromsummary results/summary-xxxxxx.json \
--num_keys uint32 the number of keys to derive for the brute force attack (default 5000)
--pendingchannels string channel input is in the format of lncli's pendingchannels format; specify '-' to read from stdin
--rootkey string BIP32 HD root key of the wallet to use for decrypting the backup; leave empty to prompt for lnd 24 word aezeed
--walletdb string read the seed/master root key to use fro decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for decrypting the backup from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion doc/chantools_rescuefunding.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ chantools rescuefunding \
--remotepubkey string in case a channel DB is not available (but perhaps a channel backup file), the remote multisig public key can be specified manually
--rootkey string BIP32 HD root key of the wallet to use for deriving keys; leave empty to prompt for lnd 24 word aezeed
--sweepaddr string address to recover the funds to; specify 'fromseed' to derive a new address from the seed automatically
--walletdb string read the seed/master root key to use fro deriving keys from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
--walletdb string read the seed/master root key to use for deriving keys from an lnd wallet.db file instead of asking for a seed or providing the --rootkey flag
```

### Options inherited from parent commands
Expand Down
Loading

0 comments on commit 567fdb2

Please sign in to comment.