Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

LND menu: show XPUBs and sign PSBT #2192

Open
rootzoll opened this issue Apr 17, 2021 · 12 comments
Open

LND menu: show XPUBs and sign PSBT #2192

rootzoll opened this issue Apr 17, 2021 · 12 comments
Labels
enhancement New feature or request

Comments

@rootzoll
Copy link
Collaborator

rootzoll commented Apr 17, 2021

Multiple people are asking how to get the xPub of LND on-chain wallet (to use with BTCPayServer for example). As far I was able to research its not a standard function of LND to export this. But Olli gave a hint that its a function of chantools.
lightningnetwork/lnd#2373 (comment)

So as a quick helper we can show the xpub in the SSH menu/status of chantools (once installed over SERVICES).

The derivation path of LND seems to be m/1017'/0'/6'/0/0 - see lightningnetwork/lnd#732 (comment)

@rootzoll rootzoll added the enhancement New feature or request label Apr 17, 2021
@rootzoll rootzoll added this to the 1.7.1 Release milestone Apr 17, 2021
@openoms
Copy link
Collaborator

openoms commented Apr 17, 2021

Be careful that the HD Root key is the master PRIVATE key of the onchain part of the LND wallet.
about chantools:

walletinfo          Shows info about an lnd wallet.db file and optionally extracts the BIP32 HD root key

For recovery scenarios there is the process described in:
https://openoms.gitbook.io/lightning-node-management/technicals/restorelndonchainfundsinelectrum
but since it involves handling sensitive keys I would not recommend it for routine use.

Also when funding the wallet from electrum the UTXO might not be picked up by LND without a rescan.
This can make it difficult to track funds.

Overall would discourage doing something like this.

To avoid this process entirely now the AEZEED seed can directly be imported to BlueWallet (available on Android, iOS and Mac: https://bluewallet.io/).

@laurentdebricon
Copy link
Contributor

laurentdebricon commented Aug 5, 2021

i just tried :
sudo service lnd stop
because of : lightninglabs/chantools#18

chantools walletinfo --walletdb ~/.lnd/data/chain/bitcoin/mainnet/wallet.db (you are asked to enter your C password to unlock the wallet)
you get the xprv and and also this output :

Wallet  #scopes:

Scope:	m/49'/0'
  Number of internal np2wkh addresses:	0
  Number of external np2wkh addresses: 	0

Scope:	m/84'/0'
  Number of internal p2wkh addresses:	5
  Number of external p2wkh addresses: 	17

Scope:	m/1017'/0'
  Number of internal p2wkh addresses:	0
  Number of external p2wkh addresses: 	8

Then to get the onchain raspiblitz xpub :

chantools derivekey --rootkey=your_xprv --path="m/84'/0'/0'"

and it works ! i can see, in a read only wallet for example, the correct funds of my raspiblitz on-chain wallet.

@rootzoll "The derivation path of LND seems to be m/1017'/0'/6'/0/0"

this was not working, maybe we don't talk about the same thing. :s

@rootzoll rootzoll modified the milestones: 1.7.1 Release, 1.8.1 Release Aug 17, 2021
@rootzoll
Copy link
Collaborator Author

This sounds like a good tool to add to the menu - but would need a bit of SSH-GUI and testing aorund it .. so I push this to v1.8.1 for now because of time prio. If some like to contribute a script or extend the bonus.chantools.sh for that feature earlier feel welcome.

@guggero
Copy link
Contributor

guggero commented Mar 22, 2022

Just leaving a hint here that with lncli wallet accounts list you can also get all xpubs of lnd's wallet.

@openoms
Copy link
Collaborator

openoms commented May 18, 2022

lncli wallet accounts list

@guggero this is a very useful new function to create watch only wallets and construct PSBTs externally.
Only the question is if we still only need the m/1017'/0'/6' path (because lncli wallet accounts list shows the accounts :

            "derivation_path": "m/49'/0'/0'",
            "derivation_path": "m/84'/0'/0'",
            "derivation_path": "m/1017'/0'/0'",
         
            ... down to 
            
            "derivation_path": "m/1017'/0'/255'",

and a number of them has used addresses and even private keys. The internal wallet has 5 UTXOs currently and shows keys on these paths:

            "derivation_path": "m/49'/0'/0'",
            "external_key_count": 13,
            "internal_key_count": 0,
--
            "derivation_path": "m/84'/0'/0'",
            "external_key_count": 410,
            "internal_key_count": 60,
--
            "derivation_path": "m/1017'/0'/0'",
            "external_key_count": 309,
            "internal_key_count": 2,
--
            "derivation_path": "m/1017'/0'/1'",
            "external_key_count": 308,
            "internal_key_count": 0,
--
            "derivation_path": "m/1017'/0'/2'",
            "external_key_count": 308,
            "internal_key_count": 0,
--
            "derivation_path": "m/1017'/0'/3'",
            "external_key_count": 308,
            "internal_key_count": 0,
--
            "derivation_path": "m/1017'/0'/4'",
            "external_key_count": 308,
            "internal_key_count": 0,
--
            "derivation_path": "m/1017'/0'/5'",
            "external_key_count": 308,
            "internal_key_count": 0,
--
            "derivation_path": "m/1017'/0'/6'",
            "external_key_count": 0,
            "internal_key_count": 0,

Update - after scanning with Sparrow Wallet:

            "derivation_path": "m/84'/0'/0'",
            "external_key_count": 410,
            "internal_key_count": 60,

has all the 5 UTXOs in the LND wallet - all from channel closes.

            "derivation_path": "m/1017'/0'/0'",
            "external_key_count": 309,
            "internal_key_count": 2,

has 3 transactions from 2019. One deposit and 2 channel opens (the second emptied the account with the change going to another account) - is this some deprecated behaviour of the LND onchain wallet?

@guggero
Copy link
Contributor

guggero commented May 18, 2022

You only need the following paths:

  • m/49'/0'/0': for nested segwit addresses (lncli newaddress np2wkh)
  • m/84'/0'/0': for native segwit addresses (lncli newaddress p2wkh)
  • m/86'/0'/0': for taproot addresses (lncli newaddress p2tr)

Everything below the m/1017'/0' is internal lnd stuff and there won't be any on-chain funds on those. The m/1017'/0'/6'/0/0 key is the node's identity key. That can be useful to derive but it will never contain any funds.

@openoms
Copy link
Collaborator

openoms commented May 18, 2022

Thank you for the explanation, that's a very useful new feature!
It think we can expose the 3 pubkeys in the menu to make starting an external reqd only wallet easy and and offer an option to sign a PSBT.

@openoms openoms changed the title Chantools Status menu show LND xPub LND menu: show XPUBs and sign PSBT May 18, 2022
@guggero
Copy link
Contributor

guggero commented May 18, 2022

has 3 transactions from 2019. One deposit and 2 channel opens (the second emptied the account with the change going to another account) - is this some deprecated behaviour of the LND onchain wallet?

I forgot to answer the last question. Am I correct in assuming that the three transactions were actually on the m/1017'/0'/3'/0/X path? That is where the funds go to if the remote node force closes. But lnd detects those funds and sweeps them to the "normal" on-chain wallet (m/84'). That is indeed some legacy behavior. Those keys used to be tweaked but since the static_to_remote feature was implemented a few years ago, they are normal keys that can directly be derived from the seed. So they could be keys under the default derivation path and we could avoid needing to sweep them. But we just didn't get to fixing that behavior.

@openoms
Copy link
Collaborator

openoms commented May 18, 2022

Am I correct in assuming that the three transactions were actually on the m/1017'/0'/3'/0/X path?

No, those 3 transactions are in `m/1017'/0'/0'.

The first is strange:
a transaction originating from m/84'/0'/0 pays to a P2SH address with seemingly change sent to m/1017'/0'/0' then there are 2 channel opens a month apart.
The last one sent the change back to the m/84'/0'/0' account.
As all this was in 2019 and no funds went missing (afaik) I am happy to not explore this further, but can continue in private if there are anything you'd want to know.

@openoms
Copy link
Collaborator

openoms commented May 18, 2022

Just leaving a hint here that with lncli wallet accounts list you can also get all xpubs of lnd's wallet.

lncli wallet accounts list --name default

shows only the accounts needed and no internals.

@openoms
Copy link
Collaborator

openoms commented May 18, 2022

To sign a PSBT:

# lncli wallet psbt finalize -h
NAME:
   lncli wallet psbt finalize - Finalize a Partially Signed Bitcoin Transaction (PSBT).

USAGE:
   lncli wallet psbt finalize [command options] funded_psbt

DESCRIPTION:
   
  The finalize command expects a partial transaction with all inputs
  and outputs fully declared and tries to sign all inputs that belong to
  the wallet. Lnd must be the last signer of the transaction. That means,
  if there are any unsigned non-witness inputs or inputs without UTXO
  information attached or inputs without witness data that do not belong
  to lnd's wallet, this method will fail. If no error is returned, the
  PSBT is ready to be extracted and the final TX within to be broadcast.

  This method does NOT publish the transaction after it's been finalized
  successfully.
  

OPTIONS:
   --funded_psbt value  the base64 encoded PSBT to finalize
   --account value      (optional) the name of the account to finalize the PSBT with

Will need to use:
lncli wallet psbt finalize --funded_psbt "base64_encoded_PSBT"

Then to publish (do it via LND to avoid any issues):

lncli wallet publishtx
NAME:
   lncli wallet publishtx - Attempts to publish the passed transaction to the network.

USAGE:
   lncli wallet publishtx [command options] tx_hex

DESCRIPTION:
   
  Publish a hex-encoded raw transaction to the on-chain network. The 
  wallet will continually attempt to re-broadcast the transaction on start up, until it 
  enters the chain. The label parameter is optional and limited to 500 characters. Note 
  that multi word labels must be contained in quotation marks ("").
  

OPTIONS:
   --label value  (optional) transaction label

Will use:
lncli wallet publishtx "tx_hex"

rootzoll added a commit that referenced this issue Dec 13, 2022
rootzoll added a commit that referenced this issue Dec 13, 2022
#2192 menu entry to show lnd xpubs
@rootzoll
Copy link
Collaborator Author

OK fpor RaspiBlitz v1.9.0 LND menu will simply show the information of

lncli wallet accounts list --name default

and tell people to report their experience bac to this issue. Pushing it to v1.10 milestone to improve on that feedback & for the PSBT part.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants