This repository contains two Node.js scripts:
-
keyGenaration.js
- Generates a BIP39 mnemonic (or uses an existing mnemonic).
- Derives a SECP256K1 private key using BIP32 path
m/0. - Generates the corresponding uncompressed public key.
-
signatureverification.js
- Signs a Base64-encoded message using a SECP256K1 private key.
- Verifies the generated signature using the corresponding public key.
Ensure the following are installed:
- Node.js (v18 or later recommended)
- npm
Verify installation:
node -v
npm -vgit clone https://github.com/rubixchain/BIP39JS
cd BIP39JSCreate a Node.js project:
npm init -yInstall all required packages:
npm install bip39 secp256k1 tiny-secp256k1 bip32 @noble/curves.
├── keyGenaration.js
├── signatureverification.js
├── package.json
└── README.md
Run:
node keyGenaration.jsThe script outputs:
Mnemonic: ...
Private Key (hex): ...
Public Key (hex): ...
If you already have a mnemonic, replace:
const mnemonic = 'drip pudding region dress youth team vague black web deer hurdle limit device worry federal dizzy curious trash segment leaf decline fuel strike trick';with your own mnemonic:
const mnemonic = 'your existing mnemonic words here';Example:
const mnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';Then run:
node keyGenaration.jsTo generate a fresh mnemonic from randomly generated entropy:
Comment out:
const mnemonic = 'drip pudding region dress youth team vague black web deer hurdle limit device worry federal dizzy curious trash segment leaf decline fuel strike trick';and uncomment:
const mnemonic = bip39.entropyToMnemonic(entropy.toString('hex'));The code should look like:
const entropy = crypto.randomBytes(32);
const mnemonic = bip39.entropyToMnemonic(entropy.toString('hex'));Then run:
node keyGenaration.jsA new mnemonic and key pair will be generated.
Open:
signatureverification.js
Update the following variables:
const privKeyStr = "";
const pubKeyStr = "";
const message = "";Use the private key generated by keyGenaration.js.
Example:
const privKeyStr = "4f5d3a4b1e2c...";The value should be a hexadecimal string.
Use the public key generated by keyGenaration.js.
Example:
const pubKeyStr = "04f9a1b2c3d4...";The value should be the full uncompressed public key generated by the script.
The script expects the message to be Base64 encoded.
Example:
const message = "5tZJVstcCmq3Cxum7u52y+nieWHma3Wawrbyi0tl0GQ=";Example:
const text = "Hello World";
const base64 = Buffer.from(text).toString('base64');
console.log(base64);Output:
SGVsbG8gV29ybGQ=
Use that value as:
const message = "SGVsbG8gV29ybGQ=";Execute:
node signatureverification.jsExample output:
signature (Base64): MEQCIF...
signature size (toDERRawBytes): 71
Is signature valid? true
node keyGenaration.jsOutput:
Mnemonic: ...
Private Key (hex): ...
Public Key (hex): ...
Update:
const privKeyStr = "<generated private key>";
const pubKeyStr = "<generated public key>";inside:
signatureverification.js
Update:
const message = "<base64 encoded message>";node signatureverification.jsExpected result:
Is signature valid? true
- Never commit real mnemonics to source control.
- Never share private keys.
- Store mnemonics securely in an offline location.
- Treat anyone with access to the mnemonic as having access to the private key.
- Use test mnemonics only for development and testing purposes.