2

I am using ethereumjs-wallet npm package to generate a unique wallet address for each user. However instead of using the mnemonic/seed, a public extended key xpub will be used instead to keep the private keys secure.

However the following code which I used is throwing the error

AssertionError [ERR_ASSERTION]: Could not derive hardened child key

The code works fine if we were to replace

const hdWallet = hdkey.fromExtendedKey("xpub...")

with

const hdWallet = hdkey.fromSeed("seed or mnemonic ...")

It appears to be a problem with the hdPath, but which exactly is the problem? And how can we solve it?

JS Code

var hdkey = require('ethereumjs-wallet/hdkey')

const hdWallet = hdkey.fromExtendedKey("xpub...")
const hdPath  = "m/44'/60'/0'/1/0"
const node = hdWallet.derivePath(hdPath)
const nodeWallet = node.getWallet()
const address = nodeWallet.getAddressString()
return address
1

I know its not exactly what you're after, but you can derive form non hardened:

var hdkey = require('ethereumjs-wallet/hdkey')

const hdWallet = hdkey.fromExtendedKey("xpub6Cy7dUR4ZKF22HEuVq7epRgRsoXfL2MK1RE81CSvp1ZySySoYGXk5PUY9y9Cc5ExpnSwXyimQAsVhyyPDNDrfj4xjDsKZJNYgsHXoEPNCYQ")
const hdPath  = "m/44/60/0/1/0"
const node = hdWallet.derivePath(hdPath)
const nodeWallet = node.getWallet()
const address = nodeWallet.getAddressString()
console.log(address)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.