Using go hdwallet

Hi,

I’m trying to use the hdwallet code (github.com/incognitochain/incognito-chain/wallet) to replicate behaviour of the mobile app. I can generate child accounts with the following code, however, the keys/addresses do not match the ones generated via the mobile app. I would expect these to be the same, if the same mnemonic phrase is used to seed the master key:

package main

import (
	"fmt"
	"github.com/incognitochain/incognito-chain/wallet"
)

func main() {
	// 12 words, backup phrase as exported from mobile app - redacted
	phrase := "one two ... ..."

	mnemonic := wallet.MnemonicGenerator{}
	seedBytes := mnemonic.NewSeed(phrase, "")
	masterKey, err := wallet.NewMasterKey(seedBytes)

	if err != nil {
		fmt.Printf("Failed to create new master key: %s", err)
	}

	for i := uint32(0); i <= 2; i++ {
		childKey, err := masterKey.NewChildKey(i)
		if err != nil {
			fmt.Printf("Failed to create new child key: %s", err)
		}

		privKeyBytes := childKey.Base58CheckSerialize(wallet.PriKeyType)
		fmt.Printf("private key %d: %v\n", i, privKeyBytes)

		paymentAddrBytes := childKey.Base58CheckSerialize(wallet.PaymentAddressType)
		fmt.Printf("payment addr %d: %v\n", i, paymentAddrBytes)
	}
}

Any pointers/tips on what am I doing wrong? Thanks! :pray:

The hdwallet code on the incognito-chain repo is deprecated and is not correct anymore. This file use new method to generate hd wallet

async function NewKey(seed, index = 0, depth = -1) {
  const hdKey = hdkey.fromMasterSeed(seed);

  const childHdKey = hdKey.derive(`m/44'/${BIP44_COIN_TYPE}'/0'/0/${index}`);
  const incognitoKeySet = new KeySet();
  await incognitoKeySet.generateKey(childHdKey.privateKey);

  const incognitoChildKey = new KeyWallet();
  incognitoChildKey.ChildNumber = (new bn(index)).toArray("be", ChildNumberSize);
  incognitoChildKey.ChainCode = childHdKey.chainCode;
  incognitoChildKey.Depth = depth + 1;
  incognitoChildKey.KeySet = incognitoKeySet;

  return incognitoChildKey;
}
1 Like

Welcome to the community @st2 and I hope that @tien’s response assisted you in resolving your issue or answering your question…thank you tien for responding to st2’s post… :sunglasses:

1 Like

Hi @tien, thanks a lot for clarifying this. One more question, if I may - what’s the difference between incognito-chain-web-js and sdk-v2?

I was under the impression that sdk-v2 would is the recommended/preferred library. Would it make sense to use that? My use case is a server/cli application, no UI.

Thansk a lot for your help!

sdk-v2 is the newer version of incognito-chain-web-js. The app will replace incognito-chain-web-js with sdk-v2 soon.

1 Like

Thanks again for clarifying this for me :clap: :rocket: