0

I want to retrieve the public key from a private key created via a mnemonic key.

const { mnemonicToEntropy } = require('bip39');
const CardanoWasm = require('@emurgo/cardano-serialization-lib-nodejs')

const { WalletServer, Seed } = require("cardano-wallet-js");

const entropy = mnemonicToEntropy(
  [ "test", "walk", "nut", "penalty", "hip", "pave", "soap", "entry", "language", "right", "filter", "choice" ].join(' ')
);

const rootKey = CardanoWasm.Bip32PrivateKey.from_bip39_entropy(
    Buffer.from(entropy, 'hex'),
    Buffer.from(''),
  );

let privateKey = Seed.deriveKey(rootKey, ['1852H','1815H','0H','0','0']).to_raw_key();
console.log(privateKey.to_bech32());

How do I get a proper public key format like "addr..."?

1 Answer 1

1

You can follow the instructions here to derive different addresses from the rootKey you've already created:

https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/generating-keys.md#use-in-addresses

You'll probably want to 1) create the accountKey + utxoPubKey + stakeKey, then 2) create the baseAddress, like so:

  const CardanoWasm = require("@emurgo/cardano-serialization-lib-nodejs");

  const harden = (num: number): number => {
    return 0x80000000 + num;
  }

  const rootKey = CardanoWasm.Bip32PrivateKey.generate_ed25519_bip32();

  // Start from here

  const accountKey = rootKey
    .derive(harden(1852)) // purpose
    .derive(harden(1815)) // coin type
    .derive(harden(0)); // account #0

  const utxoPubKey = accountKey
    .derive(0) // external
    .derive(0)
    .to_public();

  const stakeKey = accountKey
    .derive(2) // chimeric
    .derive(0)
    .to_public();

  // base address with staking key
  const baseAddress = CardanoWasm.BaseAddress.new(
    CardanoWasm.NetworkInfo.mainnet().network_id(),
    CardanoWasm.StakeCredential.from_keyhash(utxoPubKey.to_raw_key().hash()),
    CardanoWasm.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash()),
  );

  const address = baseAddress.to_address();

  console.log(address.to_bech32()) // "addr..."

Edit:

Since you seem to be using your own cardano-wallet backend with this cardano-wallet-js client, the above is not needed. You create your wallet from the seed phrase via walletServer.createOrRestoreShelleyWallet() (see here) and list wallets (and their IDs) via walletServer.wallets(). The wallet id here is just something that this particular tool creates to let you distinguish between multiple wallets that you create with it; not something required by the network or derived from the private key. To get the addresses of a wallet you can use getAddresses() or getUnusedAddresses(), see https://github.com/tango-crypto/cardano-wallet-js#wallet-addresses

5
  • Thank you! Is it possible to retrieve the wallet id via private key? I'd like to send a transaction but it seems not possible without using the wallet id :/
    – korimusk
    Aug 26, 2022 at 10:02
  • Edited the answer to include the info on the wallet id. Basically there's many ways to create wallets & transactions and submit them to the network. When using the cardano-wallet backend, you don't need to think about how to derive payment addresses from private keys and that sort. The wallet id is just a wallet references created by this particular tool for convenience.
    – sean
    Aug 26, 2022 at 10:50
  • Yes, but I cannot create a wallet with only one address in it. What I want is for each user needs to have a unique identifier like a private key but I cannot send transactions by using only the PK, a wallet id is required in each doc that I see.
    – korimusk
    Aug 26, 2022 at 11:57
  • Not exactly sure what you mean, but using cardano-wallet, why not give every user a wallet with a "wallet id"? You shouldn't need to touch any private keys. It handles those for you.
    – sean
    Aug 26, 2022 at 12:26
  • Regarding your first point: Every wallet can have multiple addresses, but you don't technically have to use them, if you don't want to. You can also use a single address per wallet.
    – sean
    Aug 26, 2022 at 12:50

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.