Emurgo / cardano-serialization-lib

This is a library, written in Rust, for serialization & deserialization of data structures used in Cardano's Haskell implementation of Alonzo along with useful utility functions.
Other
235 stars 125 forks source link

How to get the '.skey' from PrivateKey object using '@emurgo/cardano-serialization-lib' ? #391

Open code-brewer opened 2 years ago

code-brewer commented 2 years ago

Use cardano-serialization-lib, we can derive spend/signing key, just as:

const deriveRootKey = (mnemonic) =>
    Bip32PrivateKey.from_bip39_entropy(Buffer.from(mnemonicToEntropy(mnemonic), "hex"), Buffer.from(""));

const deriveAccountKey = (rootKey, account_index) =>
    rootKey
        .derive(harden(SHELLEY_COIN_PURPOSE)) // CIP1852
        .derive(harden(SHELLEY_COIN_TYPE))
        .derive(harden(account_index))

let account = deriveAccountKey(rootKey, 0)

let spendKey = accountKey.derive(0).derive(0)   // then how to generate key string as '.skey' that created by cardano-cli

But, then, can spendKey be convert to the cbor_hex string (leading with '58') like value in xxx.vkey that generate by cardano-cli ? if can, then how to this '58' leading cbor_hex string?

Thanks

grim-i-am commented 2 years ago

Hi code-brewer. Here you go:

.skey: spendKey.to_128_xprv() .vkey: spendKey.to_public().as_bytes()