Can I create a BIP32Key object using a private key?
Asked Answered
F

0

1

in the excellent reply to this question: How to generate Bitcoin keys/addresses from a seed in Python? there is a discussion of 3 ways to generate a BIP32Key from mnemonics or extended private keys. but how can I generate one from a private key? I have the hex version of one

there doesn't seem to be a method to do this

Florentinaflorentine answered 6/4, 2023 at 0:51 Comment(8)
BIP32Key encapsulates an extended key that contains various components (private/public key, chain code, version, depth, fingerprint, index, see here) that have to be specified during import (either individually via constructor or serialized via fromExtendedKey()). So a private key alone is not enough, even for the master key at least the chain code is needed in addition to the private key.Handgun
@Handgun how is it then that a BIP39 phrase is sufficient to instantiate the class when none of that information is provided?Planula
from what I can tell, the .fromEntropy method produces a digest of a hash of the BIP39 and takes the left 32 bytes for the secret and the rest as a chain code. so if I read you right, the private key would be the secret fed to BIP32Key() and the chain code could be anything? if I know that the pk is for an ethereum address, can I deduce the chain code?Planula
@Planula - S. here: BIP39 describes the generation of a mnemonic and its conversion into a binary seed, BIP32 hierarchical deterministic wallets (HD Wallets), among others the generation of a master extended key, which is the HMAC/SHA512 value (64 bytes) of the seed. The first 32 bytes are the private key, the last 32 bytes the chain code (s. also here, section 1. Master Extended Keys). This basically outlines the relationship between seed, private key, and chain code.Handgun
@Planula - If you have the seed, you can of course create a BIP32Key object from it (with fromEntropy()). But if you only have the private key (and both your original comment and this question talk about a private key and not a seed), this is not possible because the related chain code is missing (and you need the seed to derive the chain code).Handgun
@Handgun ok. so I get that I cannot recreate the root key from a private (derived) key (go back up) without the chain code. that makes sense since the compromise of one key is limited to that one key. but does mean that in my call to the BIP32Key initialiser I could then just provide an empty array for the chaincode? that would give me an object I could work with i.e. a valid node that itself is a root key, no?Florentinaflorentine
so if you want to post a reply that it cannot be done, I'm happy to mark it as resolved. and thank youFlorentinaflorentine
@ErickCalder - ...I could then just provide an empty array for the chaincode?... Even with correct size (e.g. a chain code consisting of 32 0x00 values), it makes actually no sense to use a BIP32Key object if you are not BIP32 compliant, and the latter is the case for an arbitrary chain code (according to BIP32 a private key is related to a specific chain code via the seed). Another point is the lack of entropy. I.e. technically BIP32Key may allow this, but it is not intended for this, and therefore it is not advisable.Handgun

© 2022 - 2024 — McMap. All rights reserved.