terra-money / terra.js

JavaScript SDK for Terra, written in TypeScript
MIT License
262 stars 151 forks source link

Create child wallet. #203

Closed mits87 closed 2 years ago

mits87 commented 2 years ago

Hi there,

Currently I can easily create a new wallet using the SDK:

import { LCDClient, MnemonicKey } from '@terra-money/terra.js';

const terra = new LCDClient({
  URL: 'https://lcd.terra.dev',
  chainId: 'columbus-3'
});

const mk = new MnemonicKey();
const wallet = terra.wallet(mk);

which works great!

But I would like to generate Hierarchical Deterministic wallets (child wallets) in a similar way how BSC is allowing.

Do you know how can achieve it using SDK?

Vritra4 commented 2 years ago

please see https://docs.terra.money/SDKs/Terra-js/Keys.html#mnemonickey :)

mits87 commented 2 years ago

@Vritra4 I saw that before but still I can't see a clear solution for that. Let me give an example how I'm creating child wallets for BSC:

const { hdkey } = require('ethereumjs-wallet');
const bip39 = require('bip39');

const mnemonic = 'XXX';

bip39.mnemonicToSeed(mnemonic).then((seed) => {
  const hdWallet = hdkey.fromMasterSeed(seed);

  const masterNode = hdWallet.derivePath("m/44'/60'/0'/0");
  const mainWallet = hdkey.fromExtendedKey(masterNode.privateExtendedKey());

  const node = mainWallet.derivePath("m/0");

  console.log("Main Wallet / Account");
  console.log('Address:', node.getWallet().getAddressString());

  const childHdWallet = hdkey.fromExtendedKey(node.privateExtendedKey());
  const childHdNode = childHdWallet.derivePath("m/44'/60'/0'/0");

  const childWallet = hdkey.fromExtendedKey(childHdNode.publicExtendedKey());
  const childNode = childWallet.derivePath("m/1");

  console.log("Child (USER) Wallet / Account");
  console.log('address:', childNode.getWallet().getAddressString());
});

As you can see all nested wallets (child wallets) are creating based on the previous public / private extended key. How can I do that with terra-js?

Vritra4 commented 2 years ago

i don't think there's a way to create child wallet based on previous key.. https://docs.terra.money/SDKs/Terra-js/Keys.html#specifying-hd-path needs whole hdpath i guess.