Socket
Socket
Sign inDemoInstall

@chainsafe/bls-keygen

Package Overview
Dependencies
4
Maintainers
5
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.0 to 0.4.0

.github/workflows/test.yml

6

CHANGELOG.md

@@ -0,1 +1,7 @@

## 0.4.0 (2022-02-22)
### BREAKING CHANGES
* Update dependencies - Replace `Buffer` with `Uint8Array` in public APIs [#30](https://github.com/ChainSafe/bls-keygen/pull/30)
## 0.3.0 (2020-11-06)

@@ -2,0 +8,0 @@

15

lib/index.d.ts

@@ -1,2 +0,1 @@

/// <reference types="node" />
/**

@@ -6,3 +5,3 @@ *

*/
export declare function generateRandomSecretKey(entropy?: Buffer): Buffer;
export declare function generateRandomSecretKey(entropy?: Uint8Array): Uint8Array;
/**

@@ -13,3 +12,3 @@ * Derive a secret key from a BIP39 mnemonic seed and optionally an EIP-2334 path.

*/
export declare function deriveKeyFromMnemonic(mnemonic: string, path?: string): Buffer;
export declare function deriveKeyFromMnemonic(mnemonic: string, path?: string): Uint8Array;
/**

@@ -20,3 +19,3 @@ * Derive a secret key from entropy and optionally an EIP-2334 path.

*/
export declare function deriveKeyFromEntropy(entropy: Buffer, path?: string): Buffer;
export declare function deriveKeyFromEntropy(entropy: Uint8Array, path?: string): Uint8Array;
/**

@@ -27,6 +26,6 @@ * Derive a child secret key from a master secret key

*/
export declare function deriveKeyFromMaster(masterKey: Buffer, path: string): Buffer;
export declare function deriveKeyFromMaster(masterKey: Uint8Array, path: string): Uint8Array;
export interface IEth2ValidatorKeys {
withdrawal: Buffer;
signing: Buffer;
withdrawal: Uint8Array;
signing: Uint8Array;
}

@@ -44,2 +43,2 @@ /**

*/
export declare function deriveEth2ValidatorKeys(masterKey: Buffer, validatorIndex: number): IEth2ValidatorKeys;
export declare function deriveEth2ValidatorKeys(masterKey: Uint8Array, validatorIndex: number): IEth2ValidatorKeys;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.generateRandomSecretKey = generateRandomSecretKey;
exports.deriveKeyFromMnemonic = deriveKeyFromMnemonic;
exports.deriveKeyFromEntropy = deriveKeyFromEntropy;
exports.deriveKeyFromMaster = deriveKeyFromMaster;
exports.eth2ValidatorPaths = eth2ValidatorPaths;
exports.deriveEth2ValidatorKeys = deriveEth2ValidatorKeys;
var _bip = require("bip39");
var _buffer = require("buffer");
var _randombytes = _interopRequireDefault(require("randombytes"));
var _blsHdKey = require("@chainsafe/bls-hd-key");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Object.defineProperty(exports, "__esModule", { value: true });
exports.deriveEth2ValidatorKeys = exports.eth2ValidatorPaths = exports.deriveKeyFromMaster = exports.deriveKeyFromEntropy = exports.deriveKeyFromMnemonic = exports.generateRandomSecretKey = void 0;
const bip39_1 = require("@scure/bip39");
const english_1 = require("@scure/bip39/wordlists/english");
const utils_1 = require("@noble/hashes/utils");
const bls_hd_key_1 = require("@chainsafe/bls-hd-key");
/**

@@ -28,10 +13,9 @@ *

function generateRandomSecretKey(entropy) {
let ikm = (0, _randombytes.default)(32);
if (entropy) {
ikm = _buffer.Buffer.concat([entropy, ikm]);
}
return deriveKeyFromEntropy(ikm);
let ikm = utils_1.randomBytes(32);
if (entropy) {
ikm = utils_1.concatBytes(entropy, ikm);
}
return deriveKeyFromEntropy(ikm);
}
exports.generateRandomSecretKey = generateRandomSecretKey;
/**

@@ -42,13 +26,10 @@ * Derive a secret key from a BIP39 mnemonic seed and optionally an EIP-2334 path.

*/
function deriveKeyFromMnemonic(mnemonic, path) {
if (!(0, _bip.validateMnemonic)(mnemonic)) {
throw new Error("invalid mnemonic");
}
const ikm = _buffer.Buffer.from((0, _bip.mnemonicToSeedSync)(mnemonic));
return deriveKeyFromEntropy(ikm, path);
if (!bip39_1.validateMnemonic(mnemonic, english_1.wordlist)) {
throw new Error("invalid mnemonic");
}
const ikm = Uint8Array.from(bip39_1.mnemonicToSeedSync(mnemonic));
return deriveKeyFromEntropy(ikm, path);
}
exports.deriveKeyFromMnemonic = deriveKeyFromMnemonic;
/**

@@ -59,13 +40,10 @@ * Derive a secret key from entropy and optionally an EIP-2334 path.

*/
function deriveKeyFromEntropy(entropy, path) {
const masterKey = (0, _blsHdKey.deriveMasterSK)(_buffer.Buffer.from(entropy));
if (path) {
return deriveKeyFromMaster(masterKey, path);
}
return masterKey;
const masterKey = bls_hd_key_1.deriveMasterSK(Uint8Array.from(entropy));
if (path) {
return deriveKeyFromMaster(masterKey, path);
}
return masterKey;
}
exports.deriveKeyFromEntropy = deriveKeyFromEntropy;
/**

@@ -76,8 +54,6 @@ * Derive a child secret key from a master secret key

*/
function deriveKeyFromMaster(masterKey, path) {
return (0, _blsHdKey.deriveChildSKMultiple)(masterKey, (0, _blsHdKey.pathToIndices)(path));
return bls_hd_key_1.deriveChildSKMultiple(masterKey, bls_hd_key_1.pathToIndices(path));
}
exports.deriveKeyFromMaster = deriveKeyFromMaster;
/**

@@ -87,7 +63,8 @@ * Return Eth2 validator HD paths

function eth2ValidatorPaths(validatorIndex) {
return {
withdrawal: `m/12381/3600/${validatorIndex}/0`,
signing: `m/12381/3600/${validatorIndex}/0/0`
};
return {
withdrawal: `m/12381/3600/${validatorIndex}/0`,
signing: `m/12381/3600/${validatorIndex}/0/0`,
};
}
exports.eth2ValidatorPaths = eth2ValidatorPaths;
/**

@@ -97,11 +74,10 @@ * Derive Eth2 validator secret keys from a single master secret key

*/
function deriveEth2ValidatorKeys(masterKey, validatorIndex) {
const paths = eth2ValidatorPaths(validatorIndex);
return {
withdrawal: deriveKeyFromMaster(masterKey, paths.withdrawal),
signing: deriveKeyFromMaster(masterKey, paths.signing)
};
const paths = eth2ValidatorPaths(validatorIndex);
return {
withdrawal: deriveKeyFromMaster(masterKey, paths.withdrawal),
signing: deriveKeyFromMaster(masterKey, paths.signing),
};
}
exports.deriveEth2ValidatorKeys = deriveEth2ValidatorKeys;
//# sourceMappingURL=index.js.map
{
"name": "@chainsafe/bls-keygen",
"version": "0.3.0",
"version": "0.4.0",
"description": "Typescript key management tool that works in the browser",

@@ -16,5 +16,3 @@ "main": "lib/index.js",

"prebuild": "yarn run clean",
"build": "yarn run build:lib && yarn run build:types",
"build:lib": "babel src -x .ts -d lib --source-maps",
"build:types": "tsc --emitDeclarationOnly --declaration --outDir lib -p tsconfig.build.json"
"build": "tsc -p tsconfig.build.json --outDir lib"
},

@@ -27,2 +25,9 @@ "repository": {

"license": "Apache-2.0",
"keywords": [
"ethereum",
"eth2",
"bls",
"eip-2333",
"eip-2334"
],
"bugs": {

@@ -33,7 +38,2 @@ "url": "https://github.com/ChainSafe/bls-keygen/issues"

"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.3",
"@babel/preset-env": "^7.3.1",
"@babel/preset-typescript": "^7.3.3",
"@babel/register": "^7.0.0",
"@types/bn.js": "^4.11.5",

@@ -63,7 +63,6 @@ "@types/chai": "^4.1.7",

"dependencies": {
"@chainsafe/bls-hd-key": "^0.2.0",
"bip39": "^3.0.2",
"buffer": "^5.4.3",
"randombytes": "^2.1.0"
"@chainsafe/bls-hd-key": "^0.3.0",
"@noble/hashes": "^1.0.0",
"@scure/bip39": "^1.0.0"
}
}

@@ -1,4 +0,4 @@

import {mnemonicToSeedSync, validateMnemonic} from "bip39";
import {Buffer} from "buffer";
import randomBytes from "randombytes";
import {mnemonicToSeedSync, validateMnemonic} from "@scure/bip39";
import {wordlist} from "@scure/bip39/wordlists/english";
import {concatBytes, randomBytes} from "@noble/hashes/utils";
import {deriveChildSKMultiple, deriveMasterSK, pathToIndices} from "@chainsafe/bls-hd-key";

@@ -10,6 +10,6 @@

*/
export function generateRandomSecretKey(entropy?: Buffer): Buffer {
export function generateRandomSecretKey(entropy?: Uint8Array): Uint8Array {
let ikm = randomBytes(32);
if(entropy) {
ikm = Buffer.concat([entropy, ikm]);
ikm = concatBytes(entropy, ikm);
}

@@ -24,7 +24,7 @@ return deriveKeyFromEntropy(ikm);

*/
export function deriveKeyFromMnemonic(mnemonic: string, path?: string): Buffer {
if(!validateMnemonic(mnemonic)) {
export function deriveKeyFromMnemonic(mnemonic: string, path?: string): Uint8Array {
if(!validateMnemonic(mnemonic, wordlist)) {
throw new Error("invalid mnemonic");
}
const ikm = Buffer.from(mnemonicToSeedSync(mnemonic));
const ikm = Uint8Array.from(mnemonicToSeedSync(mnemonic));
return deriveKeyFromEntropy(ikm, path);

@@ -38,4 +38,4 @@ }

*/
export function deriveKeyFromEntropy(entropy: Buffer, path?: string): Buffer {
const masterKey = deriveMasterSK(Buffer.from(entropy));
export function deriveKeyFromEntropy(entropy: Uint8Array, path?: string): Uint8Array {
const masterKey = deriveMasterSK(Uint8Array.from(entropy));
if(path) {

@@ -52,3 +52,3 @@ return deriveKeyFromMaster(masterKey, path);

*/
export function deriveKeyFromMaster(masterKey: Buffer, path: string): Buffer {
export function deriveKeyFromMaster(masterKey: Uint8Array, path: string): Uint8Array {
return deriveChildSKMultiple(masterKey, pathToIndices(path));

@@ -58,4 +58,4 @@ }

export interface IEth2ValidatorKeys {
withdrawal: Buffer;
signing: Buffer;
withdrawal: Uint8Array;
signing: Uint8Array;
}

@@ -80,3 +80,3 @@

*/
export function deriveEth2ValidatorKeys(masterKey: Buffer, validatorIndex: number): IEth2ValidatorKeys {
export function deriveEth2ValidatorKeys(masterKey: Uint8Array, validatorIndex: number): IEth2ValidatorKeys {
const paths = eth2ValidatorPaths(validatorIndex);

@@ -83,0 +83,0 @@ return {

import {deriveKeyFromMnemonic, generateRandomSecretKey, deriveKeyFromEntropy} from "../src";
import {generateMnemonic} from "@scure/bip39";
import {wordlist} from "@scure/bip39/wordlists/english";
import {expect} from "chai";
import {generateMnemonic} from "bip39";

@@ -8,11 +9,11 @@ describe("random private key", function () {

it("should generate with given entropy", function () {
const key = generateRandomSecretKey(Buffer.alloc(32)).toString("hex");
const key1 = generateRandomSecretKey(Buffer.alloc(32)).toString("hex");
expect(key).to.not.be.equal(key1);
const key = generateRandomSecretKey(Buffer.alloc(32));
const key1 = generateRandomSecretKey(Buffer.alloc(32));
expect(key).to.not.deep.equal(key1);
});
it("should generate without given entropy", function () {
const key = generateRandomSecretKey().toString("hex");
const key1 = generateRandomSecretKey().toString("hex");
expect(key).to.not.be.equal(key1);
const key = generateRandomSecretKey();
const key1 = generateRandomSecretKey();
expect(key).to.not.deep.equal(key1);
});

@@ -25,15 +26,15 @@

it("should generate master key", function () {
const mnemonic = generateMnemonic();
const mnemonic = generateMnemonic(wordlist);
const key = deriveKeyFromMnemonic(mnemonic);
const key1 = deriveKeyFromMnemonic(mnemonic);
expect(key).to.not.be.null;
expect(key.toString("hex")).to.be.equal(key1.toString("hex"));
expect(key).to.deep.equal(key1);
});
it("should generate using given path", function () {
const mnemonic = generateMnemonic();
const mnemonic = generateMnemonic(wordlist);
const key = deriveKeyFromMnemonic(mnemonic, "m/12381/3600/0/1");
const key1 = deriveKeyFromMnemonic(mnemonic, "m/12381/3600/0/2");
expect(key).to.not.be.null;
expect(key.toString("hex")).to.not.be.equal(key1.toString("hex"));
expect(key).to.not.deep.equal(key1);
});

@@ -50,3 +51,3 @@

expect(key).to.not.be.null;
expect(key.toString("hex")).to.be.equal(key1.toString("hex"));
expect(key).to.deep.equal(key1);
});

@@ -59,5 +60,5 @@

expect(key).to.not.be.null;
expect(key.toString("hex")).to.not.be.equal(key1.toString("hex"));
expect(key).to.not.deep.equal(key1);
});
});

@@ -12,3 +12,3 @@ {

// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */

@@ -25,3 +25,3 @@ // "composite": true, /* Enable project compilation */

// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": false, /* Enable strict null checks. */
"strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */

@@ -28,0 +28,0 @@ // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc