Skip to content

Instantly share code, notes, and snippets.

@mesquka
Created August 16, 2021 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mesquka/1e0456e7f10825a45e950d4cd6508aea to your computer and use it in GitHub Desktop.
Save mesquka/1e0456e7f10825a45e950d4cd6508aea to your computer and use it in GitHub Desktop.
Derive near keys from seed
const bip32 = require('ed25519-hd-key');
const nacl = require('tweetnacl');
const bip39 = require('bip39');
const bs58 = require('bs58')
const path = "m/44'/397'/0'"
const mnemonic = 'test test test test test test test test test test junk';
const seed = bip39.mnemonicToSeedSync(mnemonic);
const keySeed = bip32.derivePath(path, seed).key;
const { publicKey, secretKey } = nacl.sign.keyPair.fromSeed(keySeed);
const privateKeyHex = Buffer.from(secretKey).toString('hex');
const privateKeybs58= bs58.encode(secretKey);
const publicKeyHex = Buffer.from(publicKey).toString('hex');
const publicKeybs58 = bs58.encode(publicKey);
console.log(`Key (hex): ${privateKeyHex}`);
console.log(`Key (bs58): ${privateKeybs58}`);
console.log(`Public Key (hex): ${publicKeyHex}`);
console.log(`Public Key (bs58): ${publicKeybs58}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment