Skip to content

rubixchain/BIP39JS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SECP256K1 Key Generation and Signature Verification

This repository contains two Node.js scripts:

  1. 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.
  2. signatureverification.js

    • Signs a Base64-encoded message using a SECP256K1 private key.
    • Verifies the generated signature using the corresponding public key.

Prerequisites

Ensure the following are installed:

  • Node.js (v18 or later recommended)
  • npm

Verify installation:

node -v
npm -v

Clone the Repository

git clone https://github.com/rubixchain/BIP39JS
cd BIP39JS

Initialize the Project

Create a Node.js project:

npm init -y

Install Dependencies

Install all required packages:

npm install bip39 secp256k1 tiny-secp256k1 bip32 @noble/curves

Repository Structure

.
├── keyGenaration.js
├── signatureverification.js
├── package.json
└── README.md

Step 1: Generate Key Pair

Run:

node keyGenaration.js

The script outputs:

Mnemonic: ...
Private Key (hex): ...
Public Key (hex): ...

Using an Existing Mnemonic

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.js

Generating a New Random Mnemonic

To 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.js

A new mnemonic and key pair will be generated.


Step 2: Sign and Verify a Message

Open:

signatureverification.js

Update the following variables:

const privKeyStr = "";
const pubKeyStr = "";
const message = "";

Provide the Private Key

Use the private key generated by keyGenaration.js.

Example:

const privKeyStr = "4f5d3a4b1e2c...";

The value should be a hexadecimal string.


Provide the Public Key

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.


Provide the Message

The script expects the message to be Base64 encoded.

Example:

const message = "5tZJVstcCmq3Cxum7u52y+nieWHma3Wawrbyi0tl0GQ=";

Converting Plain Text to Base64

Example:

const text = "Hello World";
const base64 = Buffer.from(text).toString('base64');

console.log(base64);

Output:

SGVsbG8gV29ybGQ=

Use that value as:

const message = "SGVsbG8gV29ybGQ=";

Run Signature Generation and Verification

Execute:

node signatureverification.js

Example output:

signature (Base64): MEQCIF...
signature size (toDERRawBytes): 71
Is signature valid? true

Complete Workflow

Generate Keys

node keyGenaration.js

Output:

Mnemonic: ...
Private Key (hex): ...
Public Key (hex): ...

Copy Generated Keys

Update:

const privKeyStr = "<generated private key>";
const pubKeyStr = "<generated public key>";

inside:

signatureverification.js

Provide Message

Update:

const message = "<base64 encoded message>";

Sign and Verify

node signatureverification.js

Expected result:

Is signature valid? true

Security Notes

  • 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.

About

This Repo generates BIP39 key pairs with mnemonic, performs signatutre and verification using the key pair

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors