3

Objective

To load a private key generated by cardano-cli into cardano-serialization lib

Details

Using cardano-cli cardano-cli address key-gen, we get this:

{
    "type": "PaymentSigningKeyShelley_ed25519",
    "description": "Payment Signing Key",
    "cborHex": "582099n3n930d9n31b5b102h3vd9csa290c14e0898sn1d278f1939f3e4bed890675a"
}

Are we able to derive the root key? for example in this format:

xprv17qx9vxm6060qjn5fgazfue9nwyf448w7upk60c3epln82vumg9r9kxzsud9uv5rfscxp382j2aku254zj3qfx9fx39t6hjwtmwq85uunsd8x0st3j66lzf5yn30hwq5n75zeuplepx8vxc502txx09ygjgx06n0p

or seed phrase? for example:

bawdy identify skin design ... squash elbow aloof plain nerve verse reply near

1 Answer 1

3

You can use that key to sign transactions in cardano-serialization lib, but I believe you can't go back to a seed phrase or root key / other key derivations from it.

To use it for signing transactions, you can unwrap and create the PrivateKey like so:

const cbor_hex_key = "582019544c6b8de8e697c6efa3a1ea728559b063f77f52314fcd2fbce3b692c3e99f";
const unhex = Buffer.from(cbor_hex_key, "hex");
const decode = cbor.decode(unhex);
const privKey = PrivateKey.from_normal_bytes(decode)
console.log(privKey.to_bech32()) // ed25519_sk1r92yc6udarnf03h05ws75u59txcx8aml2gc5lnf0hn3mdykrax0smqv6uc

(the cbor package for decoding is here: https://www.npmjs.com/package/cbor, the serialization lib pkg I use from here: https://www.npmjs.com/package/@emurgo/cardano-serialization-lib-nodejs)

If you do need to start from a root key starting with "xprv...", you can generate an extended key with the CLI using

cardano-cli address key-gen --extended-key

and then use that cborHex value in the serialization lib like so (it will be much longer than the one above):

const cbor_hex_ext_key = "5880a83391da55604678e05489ccb29cee99c238b224b29ef21f98dfd2b121155d5c9dfe7157214cd61c6522aaabb3a5707ae176b220516a51c2cc4f651a3c0dc21c0596b0c11795a9a42cf7ba0014fa402195900813942d66ba3c9b8c175e58fd5ac9e1dfa7008b77af7616ab23a9250374d7c4d39041874cfad9116c204e9b372a"
const ext_unhex = Buffer.from(cbor_hex_ext_key, "hex");
const ext_uncbor = cbor.decode(ext_unhex);
const ext_key = Bip32PrivateKey.from_128_xprv(ext_uncbor)
console.log(ext_key.to_bech32()) // xprv14qeerkj4vpr83cz538xt988wn8pr3v3yk200y8ucmlftzgg4t4wfmln32us5e4suv53242an54c84ctkkgs9z6j3ctxy7eg68sxuy8xfu806wqytw7hhv94tyw5j2qm56lzd8yzpsax04kg3dssyaxeh9gtrw423

But from there also you won't be able to go back to a seed phrase, but in case you're coming from a seed phrase (eg generated via "cardano-address recovery-phrase generate") you can decode it in the lib like so:

const entropy = mnemonicToEntropy("theme debate amateur pudding young deal wealth bag neck victory language abandon tribe unlock rabbit fix visual spawn broom advice anger foam strong document");
const root = Bip32PrivateKey.from_bip39_entropy(
    Buffer.from(entropy, 'hex'),
    Buffer.from(''),
  )
console.log(root.to_bech32()); //xprv1xqdaf53cvc8cdnmx6wf0gz3pxq0kk7rfu7sfkac99tadxs2j0prqe3n20emyw8vv7r8435lw2h4lgar2jwzdyfha7facnln0mhw22dlxgpgw38pt0vus0lyq9rs5rnws79x4dswwyklyklvyuyslh65c4ykl0hr4

Good source for all this: https://github.com/Emurgo/cardano-serialization-lib/blob/4f717ac97bd768168ab273dacd9e43cb6b4aa4ec/doc/getting-started/generating-keys.md)

1
  • 1
    thanks for the comprehensive answer, it confirmed my understanding.
    – Jingles
    Apr 5, 2022 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.