Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

derivekey: support xpubs in addition to xprvs #179

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

starius
Copy link
Contributor

@starius starius commented Jan 9, 2025

Now it is possible to derive a public key from xpub if --neuter is passed:

chantools derivekey --rootkey xpub... --path "m/0/0" --neuter

Example:

$ chantools derivekey --testnet --rootkey tpubDDfTBtwwqxXuCej7pKYfbXeCW3inAtv1cw4knmvYTTHkw3NoKaeCNH5XdY6n6fnBPc1gWEgeurfmBVzJLfBB1hGU64LsHFzJv4ASqaHyALH --path "m/0/0" --neuter
2025-01-09 15:33:00.047 [INF] CHAN: chantools version v0.13.5 commit 

Path:                           m/0/0
Network:                        testnet3
Master Fingerprint:             dfe0654d
Public key:                     0280a3fcbeb7f770af6dd45cb0f4d02e1044eafe0d8b05bcaec79dc0478c7fa0da
Extended public key (xpub):     tpubDGveUteHeiGeoiYFcusMzsUZzEwhdT6jEG3Xuz7GXeV3QRchMoMb1dfGMkaLFn7uvQqMiUKkRwtS7d3r1CmhqGHuUpZ5ZgWuo2zNZsgLbg9
Address:                        tb1q6hk6zfzu3plh07pygqh26y48amgf0ru3fl8k6q
Legacy address:                 n126thTNJ7xUAJwiEUikXak8nUoQzxcfSk
Taproot address:                tb1ptdj7h5476hyfswvwtctqxrnwhnldgjdw5yvxe6m72ltyj6k38ajsl7g648
Private key (WIF):              n/a
Extended private key (xprv):    n/a

Also added tests TestDeriveKeyXprv and TestDeriveKeyXpub.

This test checks that "chantools derivekey --rootkey xpriv..." works.
starius added a commit to starius/taproot-assets that referenced this pull request Jan 9, 2025
Added more derivation paths and corresponding public keys, check in the test
that the values returned by the method match to expected values. Also added
tpub (testnet xpub).

To run "chantools derivekey" with xpubs, use PR
lightninglabs/chantools#179
It can work with xpub and xprv to derive child xpub or xpriv and public key.
It can be used when only xpub is available.
Now it is possible to derive a public key from xpub:

chantools derivekey --rootkey xpub... --path "m/0/0" --neuter
Comment on lines +90 to +98
if neuter {
child, pubKey, err = lnd.DerivePubKey(
extendedKey, path,
)
} else {
child, pubKey, wif, err = lnd.DeriveKey(
extendedKey, path, chainParams,
)
}
Copy link

@ffranr ffranr Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pubKey is the same in both cases. Which means we can just keep the lnd.DeriveKey call, remove the lnd.DerivePubKey calling clause, and then later ignore wif if neuter == true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeriveKey fails if an xpub is passed to it (in derivedKey.ECPrivKey call):

Error: could not derive keys: could not derive private key: unable to create private keys from a public extended key

ffranr pushed a commit to lightninglabs/taproot-assets that referenced this pull request Jan 10, 2025
Added more derivation paths and corresponding public keys, check in the test
that the values returned by the method match to expected values. Also added
tpub (testnet xpub).

To run "chantools derivekey" with xpubs, use PR
lightninglabs/chantools#179
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants