cardano-serialization-lib icon indicating copy to clipboard operation
cardano-serialization-lib copied to clipboard

convert from_bech32

Open rezagazeri opened this issue 3 years ago • 3 comments

Hi, first I want to say thanks due to this library. I use @emurgo/cardano-serialization-lib-nodejs and created PrivateKey and convert it to bech32 like below: privKey=PrivateKey { ptr: 1180592 } and privKey.to_bech32()="ed25519e_sk1qqns8r2m54r7u5wtm27......" and now i need to convert it from_bech32 but i can not find this function ->CardanoWasm.PrivateKey.from_bech32("ed25519e_sk16rl5fq....") how can I convert from bech32 to privatekey ? thanks in advance

rezagazeri avatar Sep 16 '21 13:09 rezagazeri

If I recall this was never added because there was no standard for differentiating extended ed25519 keys from non-extended ones. Although serialization-lib ended up using ed25519e and ed25519 as different prefixes to differentiate these cases, I don't remember if cardano-cli ever adopted this standard in the end. Issues like this is what prompted the creation of CIP 5 but I see that neither prefixes are on that registry. There was discussion about standardizing bech32 for keys but as you can see it's not simple

If you want a workaround, you can use your own bech32 parsing library to turn the bech32 string into a byte array and then call the right constructor for PrivateKey yourself.

We could add a from_bech32 function to this library that checks the prefix and if it doesn't match it throws an error. This could be an awkward breaking change in the future though so maybe it's best to add the prefixes to CIP5 first.

SebastienGllmt avatar Sep 17 '21 15:09 SebastienGllmt

in the lib doc on below address : https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/generating-transactions.md

you used (const prvKey = CardanoWasm.PrivateKey.from_bech32("ed25519e_sk16rl5fqqf4mg27syjzjrq8h3vq44jnnv52mvyzdttldszjj7a64xtmjwgjtfy25lu0xmv40306lj9pcqpa6slry9eh3mtlqvfjz93vuq0grl80");) however I need to convert PrivateKey format to string format because I want to save it in db and so read from db and again convert to privateKey.

rezagazeri avatar Sep 19 '21 14:09 rezagazeri

I have existing wallets and keys generated by cardano-cli stake-address build, which produces .skey and .pkey files.

From this generating-transactions example, we need bech32 like so:

const rootKey = CardanoWasm.BIP32PrivateKey.from_bech32("xprv17qx9vxm6060qjn5fgazfue9nwyf448w7upk60c3epln82vumg9r9kxzsud9uv5rfscxp382j2aku254zj3qfx9fx39t6hjwtmwq85uunsd8x0st3j66lzf5yn30hwq5n75zeuplepx8vxc502txx09ygjgx06n0p");

How do I read and load in these .pkey files into cardano-serialization-lib?

jinglescode avatar Oct 16 '21 02:10 jinglescode