Skip to main content
adjusting answer to using cardano-wallet instead of raw address derivation
Source Link
sean
  • 1.5k
  • 5
  • 14

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

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

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

Source Link
sean
  • 1.5k
  • 5
  • 14

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