I'm trying to generate address and create transaction using nodejs and @emurgo/cardano-serialization-lib-nodejs lib (CardanoWasm). Following the docs I'm trying this:
const rootkey = CardanoWasm.Bip32PrivateKey.from_bip39_entropy(
Buffer.from(someEntropy, 'hex'), // entropy is generated from mnemonic
Buffer.from('')
);
const account = rootkey
.derive(harden(1852)) // harden is a function returning 0x80000000+arg
.derive(harden(1815))
.derive(harden(0));
const utxokey = account
.derive(0)
.derive(0)
.to_public();
const stake1 = account
.derive(2)
.derive(0)
.to_public();
const address = CardanoWasm.BaseAddress.new(
CardanoWasm.NetworkInfo.mainnet().network_id(),
CardanoWasm.StakeCredential.from_keyhash(utxokey.to_raw_key().hash()),
CardanoWasm.StakeCredential.from_keyhash(stakekey.to_raw_key().hash())
);
const addressBech32 = address.to_address().to_bech32();
So in my example addressBech32 is an actual public address of a wallet. When I import the wallet to the guarda (for example) by mnemonic it works fine. But what is actually rootkey and account? What is private key and signing key in my example? What key should i use to sign the transaction and how to get that key using cardano wasm? What private key should I use to import the wallet (if I don't want to use mnemonic for some reason)?
When I import the wallet to the guarda (for example) by mnemonic it works fine. But what is actually rootkey and account?
rootKey
is a representation of your xpriv, your master private key from which all staking and spending, private and public keys are generated from.
The process for generating staking and signing keys is described in CIP-0003, basically piggybacking off the Bitcoin BIP32 and BIP44 standard.
In short, the scheme allows a master key to be used to generate a number of separate wallet 'accounts'. In your example:
const account = rootkey
.derive(harden(1852)) // harden is a function returning 0x80000000+arg
.derive(harden(1815))
.derive(harden(0));
generates the private key for Cardano account zero.
Going further:
account
.derive(0) # external chain (designated for receive addresses)
.derive(0); # index (the first address)
would generate a private key for receive address zero in that account.
What is private key and signing key in my example? What key should i use to sign the transaction and how to get that key using cardano wasm?
Your example creates a bech32 address for (Cardano account 0) receive address 0. So the private key to spend from this address would be the same as the above:
const utxo_privkey = account
.derive(0)
.derive(0);
What private key should I use to import the wallet (if I don't want to use mnemonic for some reason)?
You can supply the entropy as bytes, as you have done in your example for rootKey
, or you can use a bech32 encoded private key like so:
const rootKey = CardanoWasm.BIP32PrivateKey.from_bech32("xprv17qx9vxm6060qjn5fgazfue9nwyf448w7upk60c3epln82vumg9r9kxzsud9uv5rfscxp382j2aku254zj3qfx9fx39t6hjwtmwq85uunsd8x0st3j66lzf5yn30hwq5n75zeuplepx8vxc502txx09ygjgx06n0p");