<p>You can follow the instructions here to derive different addresses from the rootKey you've already created:</p>
<p><a href="https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/generating-keys.md#use-in-addresses" rel="nofollow noreferrer">https://github.com/Emurgo/cardano-serialization-lib/blob/master/doc/getting-started/generating-keys.md#use-in-addresses</a></p>
<p>You'll probably want to 1) create the accountKey + utxoPubKey + stakeKey, then 2) create the baseAddress, like so:</p>
<pre><code>  const CardanoWasm = require(&quot;@emurgo/cardano-serialization-lib-nodejs&quot;);

  const harden = (num: number): number =&gt; {
    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()) // &quot;addr...&quot;
</code></pre>
<p><em>Edit:</em></p>
<p>Since you seem to be using your own <a href="https://github.com/input-output-hk/cardano-wallet" rel="nofollow noreferrer">cardano-wallet</a> backend with this <a href="https://github.com/tango-crypto/cardano-wallet-js" rel="nofollow noreferrer">cardano-wallet-js</a> client, the above is not needed. You create your wallet from the seed phrase via <code>walletServer.createOrRestoreShelleyWallet()</code> (see <a href="https://github.com/tango-crypto/cardano-wallet-js#create-or-restore-a-wallet" rel="nofollow noreferrer">here</a>) and list wallets <em>(and their IDs)</em> via <code>walletServer.wallets()</code>. The <em>wallet id</em> 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 <code>getAddresses()</code> or <code>getUnusedAddresses()</code>, see <a href="https://github.com/tango-crypto/cardano-wallet-js#wallet-addresses" rel="nofollow noreferrer">https://github.com/tango-crypto/cardano-wallet-js#wallet-addresses</a></p>

CC BY-SA 4.0
2022-08-26T10:02:16.827Z
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
CC BY-SA 4.0
2022-08-26T10:50:45.160Z
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
CC BY-SA 4.0
2022-08-26T11:57:32.110Z
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
CC BY-SA 4.0
2022-08-26T12:26:47.610Z
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
CC BY-SA 4.0
2022-08-26T12:50:48.070Z
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
CC BY-SA 4.0
2022-08-25T07:55:24.953Z
2022-08-26T10:45:56.417Z
2022-08-26T10:45:56.417Z
sean
<p>I want to retrieve the public key from a private key created via a mnemonic key.</p>
<pre><code>const { mnemonicToEntropy } = require('bip39');
const CardanoWasm = require('@emurgo/cardano-serialization-lib-nodejs')

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

const entropy = mnemonicToEntropy(
  [ &quot;test&quot;, &quot;walk&quot;, &quot;nut&quot;, &quot;penalty&quot;, &quot;hip&quot;, &quot;pave&quot;, &quot;soap&quot;, &quot;entry&quot;, &quot;language&quot;, &quot;right&quot;, &quot;filter&quot;, &quot;choice&quot; ].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());
</code></pre>
<p>How do I get a proper public key format like &quot;addr...&quot;?</p>

<wallet><cardano-serialization-lib><private-key><public-key>
How to get PublicKey from PrivateKey
# How to get PublicKey from PrivateKey

(Asked by: username_0 on 2022-08-23)

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..."?

**Tags:** wallet, cardano-serialization-lib, private-key, public-key


---

## 1 answers

### Answer by username_1 on 2022-08-25. Score: 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](https://github.com/input-output-hk/cardano-wallet) backend with this [cardano-wallet-js](https://github.com/tango-crypto/cardano-wallet-js) client, the above is not needed. You create your wallet from the seed phrase via `walletServer.createOrRestoreShelleyWallet()` (see [here](https://github.com/tango-crypto/cardano-wallet-js#create-or-restore-a-wallet)) 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>

> **Comments:**
> **username_0**: 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 :/  
> **username_1**: 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.  
> **username_0**: 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.  
> **username_1**: 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.  
> **username_1**: 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.  

---

