Giter Site home page Giter Site logo

bls-keygen's Introduction

BLS Keygen

npm (tag) Discord GitHub ES Version Node Version

Utility functions for generating BLS secret keys, built for NodeJs and Browser.

  • Create a master key from BIP-39 mnemonic or entropy.
  • Create a derived child key from BIP-39 mnemonic, entropy, or a master key.
  • Create Eth2 validator keys from a master key.

Implementation follows EIPS: EIP-2334, EIP-2333

For low-level EIP-2333 and EIP-2334 functionality, see @chainsafe/bls-hd-key.

Examples

import {
    generateRandomSecretKey,
    deriveKeyFromMnemonic,
    deriveKeyFromEntropy,
    deriveKeyFromMaster,
    deriveEth2ValidatorKeys,
} from "@chainsafe/bls-keygen";

// random secret key
const secretKey = generateRandomSecretKey();

...

// secret key from mnemonic and optional EIP-2334 path
const masterSecretKey = deriveKeyFromMnemonic(
  "impact exit example acquire drastic cement usage float mesh source private bulb twenty guitar neglect",
);

const childSecretKey = deriveKeyFromMnemonic(
  "impact exit example acquire drastic cement usage float mesh source private bulb twenty guitar neglect",
  "m/12381/3600/0/0"
);

...

// secret key from entropy and optional EIP-2334 path
const masterSecretKey = deriveKeyFromEntropy(entropy);

const childSecretKey = deriveKeyFromEntropy(
  entropy,
  "m/12381/3600/0/0"
);


...

// child secret key from master secret key and EIP-2334 path
const childSecretKey = deriveKeyFromMaster(
  masterSecretKey,
  "m/12381/3600/0/0"
);

...

// create multiple eth2 validator keys from a master secret key
const keys0 = deriveEth2ValidatorKeys(masterSecretKey, 0);
const keys1 = deriveEth2ValidatorKeys(masterSecretKey, 1);

const { signing, withdrawal } = keys0;

Contribution

Requirements:

  • nodejs
  • yarn
    yarn install
    yarn run test

Audit

This repo was audited by Least Authority as part of this security audit, released 2020-03-23. Commit 32b068 verified in the report.

License

Apache-2.0

bls-keygen's People

Contributors

3xtr4t3rr3str14l avatar dapplion avatar gregthegreek avatar mpetrunic avatar paulmillr avatar philknows avatar wemeetagain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bls-keygen's Issues

Derivation from Mnemonic Mismatch

I am having confusing results testing key generation using your library vs https://bls-keygen.com/

For example, using the generated mnemonic:
stereo hub fog giraffe street suspect foil scorpion dirt brave eyebrow always

The following code block does not match the generated master key from the website

const { validateMnemonic, mnemonicToSeedSync } = require('bip39');
const {
    deriveKeyFromMnemonic
} = require('@chainsafe/bls-keygen');

const {
    initBLS,
    generatePublicKey
} = require('@chainsafe/bls');

const mnemonic = 'stereo hub fog giraffe street suspect foil scorpion dirt brave eyebrow always';

async function run() {
    await initBLS();
    validateMnemonic(mnemonic);
    const masterPrivateKey = deriveKeyFromMnemonic(mnemonic);
    const masterPubkey = generatePublicKey(masterPrivateKey);
    console.log(`Seed: ${seed.toString('hex')}`);
    console.log(`Master Private Key: ${masterPrivateKey.toString('hex')}`);
    console.log(`Master Public Key: ${masterPubkey.toString('hex')}`);
}

run();

If I instead use

    const seed = bip39.mnemonicToSeedSync(mnemonic);
    const masterPrivateKey = deriveMasterSK(seed);

Then I get results that match the website.

I would have expected deriveKeyFromMnemonic(mnemonic) to do the job, and your documentation here suggests that, but if I actually took the mnemonic from bls-keygen.com and plug it directly into the code above, I get different results than keystore generated on the site.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.