@fnet/keygen
v0.1.4
Published
A simple utility to generate key pairs from a mnemonic seed phrase
Downloads
123
Readme
@fnet/keygen
Introduction
@fnet/keygen is a JavaScript library designed to help users derive cryptographic keys using a mnemonic seed phrase or a private key. It leverages industry-standard cryptographic methods to generate secure keys suitable for blockchain applications, including Bitcoin. The library also supports Shamir's Secret Sharing to provide an additional layer of security.
How It Works
The core functionality of @fnet/keygen revolves around deriving keys using the BIP39 and BIP32 standards. Given a mnemonic seed phrase, the library generates a cryptographic seed, which is then used to create a root key. From this root key, you can derive child keys using a specified hierarchical path. The library can output the private key, public key, and the corresponding public Bitcoin address. It also offers an optional feature to split a private key into multiple shares using Shamir's Secret Sharing, providing an extra security measure.
Key Features
- Generate cryptographic keys from a mnemonic seed phrase.
- Standard BIP44 derivation paths for hierarchical key generation.
- Output includes private key, public key, and Bitcoin address.
- Supports Shamir's Secret Sharing to divide a private key into shares with a defined threshold for reconstruction.
Conclusion
@fnet/keygen is a practical tool for users looking to implement secure key derivation in blockchain applications. Its straightforward approach to generating and managing keys ensures a robust solution without unnecessary complexity. Whether you need to create a simple wallet or implement an advanced key management solution, this library offers essential features to support your cryptographic needs.
@fnet/keygen Developer Guide
Overview
The @fnet/keygen
library provides a utility for generating cryptographic keys using mnemonic seed phrases following the BIP39 standard. Additionally, it supports deriving child keys based on custom derivation paths compatible with BIP32, and offers Shamir Secret Sharing to split private keys into secure shares for enhanced security. This library is ideal for developers looking to manage and secure cryptographic keys for blockchain or other secure applications.
Installation
To install @fnet/keygen
in your project, use npm or yarn:
npm install @fnet/keygen
or
yarn add @fnet/keygen
Usage
Key Derivation
You can derive cryptographic keys using a mnemonic phrase and a custom derivation path. Define the necessary parameters such as organization, account, scope, and index to derive specific keys in your application's context.
import keygen from '@fnet/keygen';
const derivedKey = await keygen({
phrase: 'your mnemonic seed phrase here',
organization: 0,
account: 0,
scope: 0,
index: 0
});
console.log('Private Key:', derivedKey.private_key);
console.log('Public Key:', derivedKey.public_key);
console.log('Address:', derivedKey.address);
console.log('Path:', derivedKey.path);
Shamir Secret Sharing
If you want to enable Shamir Secret Sharing to split the derived private key into multiple shares, you can specify the shares
, shares_total
, and shares_threshold
parameters.
const secretSharedKey = await keygen({
phrase: 'your mnemonic seed phrase here',
organization: 0,
account: 0,
scope: 0,
index: 0,
shares: true,
shares_total: 5,
shares_threshold: 3
});
console.log('Private Key Shares:', secretSharedKey.private_key_shares);
console.log('Total Shares:', secretSharedKey.private_key_shares_total);
console.log('Threshold Needed to Reconstruct:', secretSharedKey.private_key_shares_threshold);
Examples
Basic Key Derivation
import keygen from '@fnet/keygen';
async function deriveKeys() {
const keys = await keygen({
phrase: 'correct horse battery staple',
organization: 1,
account: 2,
scope: 3,
index: 4
});
console.log(keys);
}
deriveKeys();
Using Shamir Secret Sharing
import keygen from '@fnet/keygen';
async function secureKeyWithShares() {
const keys = await keygen({
phrase: 'correct horse battery staple',
shares: true,
shares_total: 7,
shares_threshold: 4
});
console.log('Shares:', keys.private_key_shares);
}
secureKeyWithShares();
Acknowledgement
This guide is based on the library's current functionalities and may evolve as the library is updated. The library utilizes tiny-secp256k1
, bip39
, bitcoinjs-lib
, and @fnet/key-shamir
for cryptographic operations and secret sharing.
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
phrase:
type: string
description: The mnemonic seed phrase (BIP39) used to generate the cryptographic seed.
organization:
type: number
description: The organization number to use in the key derivation path.
default: 0
account:
type: number
description: The account number to use in the key derivation path.
default: 0
scope:
type: number
description: The scope for custom key derivation (used to define the coin type
or specific application context).
default: 0
index:
type: number
description: The index of the key to derive within the account.
default: 0
shares:
type: boolean
description: Enabled Shamir Secret Sharing for private key.
default: false
shares_threshold:
type: number
description: The threshold of Shamir Secret Sharing.
default: 3
shares_total:
type: number
description: The number of shares for Shamir Secret Sharing.
default: 5
required:
- phrase