Hi, I’ve derived payment private key and stake private key using HdWallet in python, But Hdwallet returns 64 bytes extended private key(xprivate_key) which I can’t use in Pycardano library. How can I get normal 32 bytes private key from 64 bytes extended private key. I tried getting 32 first bytes but it doesn’t work
Learned something new today: You cannot go back from the extended private key to the private key.
Section III-B of BIP32-Ed25519 or equivalently Section 5.1.5 of RFC 8032 describe how a private key is extended. And that involves hashing which is not reversible.
BIP32-Ed25519 even works only with extended keys and there are no non-extended ones.
PyCardano also seems to have some classes to deal with extended keys: https://pycardano.readthedocs.io/en/latest/api/pycardano.key.html
Unfortunately, the documentation (and also what is described at https://pycardano.readthedocs.io/en/latest/guides/address.html#keys) is not very detailed.
You’d have to experiment around which of those to use how to get the correct objects.
I tried ExtendedSigningKey.from_primitive but it couldn’t validate the transaction. tried importing from_hdwallet method just now and it works. thanks for the help
I am also having an issue with the 64-byte key. Could you provide me with a sample code? Here is my current code.
def recover_cardano_wallet(request):
from pycardano import HDWallet
mnemonic = ‘gloom turkey …’
from pycardano import HDWallet, Network, ExtendedSigningKey,StakeExtendedSigningKey, PaymentExtendedSigningKey
hdwallet = HDWallet.from_mnemonic(mnemonic)
payment_hdwallet = hdwallet.derive_from_path("m/1852'/1815'/0'/0/0")
payment_private_key = payment_hdwallet.xprivate_key[:32].hex()
payment_public_key = payment_hdwallet.public_key.hex()
stake_hdwallet = hdwallet.derive_from_path("m/1852'/1815'/0'/2/0")
stake_private_key = stake_hdwallet.xprivate_key[:32].hex()
stake_public_key = stake_hdwallet.public_key.hex()
payment_skey = PaymentSigningKey(bytes.fromhex(payment_private_key))
payment_vkey = PaymentVerificationKey(bytes.fromhex(payment_public_key))
stake_skey = StakeSigningKey(bytes.fromhex(stake_private_key))
stake_vkey = StakeVerificationKey(bytes.fromhex(stake_public_key))
address = Address(payment_part=payment_vkey.hash(), staking_part=stake_vkey.hash(), network=Network.TESTNET)
payment_skey.save(f"{address}_payment.skey")
payment_vkey.save(f"{address}_payment.vkey")
stake_skey.save(f"{address}_stake.skey")
stake_vkey.save(f"{address}_stake.vkey")