Skip to content

Instantly share code, notes, and snippets.

@learntheropes
Last active February 5, 2026 18:43
Show Gist options
  • Select an option

  • Save learntheropes/5541903c73346ccd165f7b8e1104cc78 to your computer and use it in GitHub Desktop.

Select an option

Save learntheropes/5541903c73346ccd165f7b8e1104cc78 to your computer and use it in GitHub Desktop.
derive publickey from seed phrase - this script derives the pubkey stored in the db from the seed phrase backup without revealing any private or confidential info
// From the terminal in the save directory where this script is saved:
// npm i bip39 bip32 @bitcoinerlab/secp256k1
// MNEMONIC="your mnemonic here" node derive-pubkey.cjs
const bip39 = require('bip39')
const BIP32Factory = require('bip32').default
const ecc = require('@bitcoinerlab/secp256k1')
const bip32 = BIP32Factory(ecc)
function derivePublicKeyHexFromMnemonic(
mnemonic,
derivationPath = `m/46'/0'/0'/0`
) {
if (!bip39.validateMnemonic(mnemonic)) {
throw new Error('Invalid BIP39 mnemonic')
}
const seed = bip39.mnemonicToSeedSync(mnemonic)
const root = bip32.fromSeed(seed)
const child = root.derivePath(derivationPath)
return child.publicKey.toString('hex')
}
// example usage
const mnemonic = process.env.MNEMONIC
if (!mnemonic) throw new Error('Set MNEMONIC env var')
console.log(derivePublicKeyHexFromMnemonic(mnemonic))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment