GithubHelp home page GithubHelp logo

paulmillr / scure-bip39 Goto Github PK

View Code? Open in Web Editor NEW
108.0 5.0 28.0 614 KB

Secure, audited & minimal implementation of BIP39 mnemonic phrases

Home Page: https://paulmillr.com/noble/#scure

License: MIT License

TypeScript 98.96% JavaScript 1.04%
bip39 bip32 mnemonic mnemonic-phrase code

scure-bip39's Introduction

scure-bip39

Audited & minimal JS implementation of BIP39 mnemonic phrases.

  • ๐Ÿ”’ Audited by an independent security firm
  • ๐Ÿ”ป Tree-shaking-friendly: use only what's necessary, other code won't be included
  • ๐Ÿ“ฆ ESM and common.js
  • โžฐ Only 2 audited dependencies by the same author: noble-hashes and scure-base
  • ๐Ÿชถ 37KB with all deps bundled and 279KB with wordlists: much smaller than similar libraries

Check out scure-bip32 if you need hierarchical deterministic wallets ("HD Wallets").

This library belongs to scure

scure โ€” audited micro-libraries.

Usage

npm install @scure/bip39

import * as bip39 from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';

// Generate x random words. Uses Cryptographically-Secure Random Number Generator.
const mn = bip39.generateMnemonic(wordlist);
console.log(mn);

// Reversible: Converts mnemonic string to raw entropy in form of byte array.
const ent = bip39.mnemonicToEntropy(mn, wordlist)

// Reversible: Converts raw entropy in form of byte array to mnemonic string.
bip39.entropyToMnemonic(ent, wordlist);

// Validates mnemonic for being 12-24 words contained in `wordlist`.
bip39.validateMnemonic(mn, wordlist);

// Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
await bip39.mnemonicToSeed(mn, 'password');
bip39.mnemonicToSeedSync(mn, 'password');

This submodule contains the word lists defined by BIP39 for Czech, English, French, Italian, Japanese, Korean, Portuguese, Simplified and Traditional Chinese, and Spanish. These are not imported by default, as that would increase bundle sizes too much. Instead, you should import and use them explicitly.

function generateMnemonic(wordlist: string[], strength?: number): string;
function mnemonicToEntropy(mnemonic: string, wordlist: string[]): Uint8Array;
function entropyToMnemonic(entropy: Uint8Array, wordlist: string[]): string;
function validateMnemonic(mnemonic: string, wordlist: string[]): boolean;
function mnemonicToSeed(mnemonic: string, passphrase?: string): Promise<Uint8Array>;
function mnemonicToSeedSync(mnemonic: string, passphrase?: string): Uint8Array;

All wordlists:

import { wordlist as czech } from '@scure/bip39/wordlists/czech';
import { wordlist as english } from '@scure/bip39/wordlists/english';
import { wordlist as french } from '@scure/bip39/wordlists/french';
import { wordlist as italian } from '@scure/bip39/wordlists/italian';
import { wordlist as japanese } from '@scure/bip39/wordlists/japanese';
import { wordlist as korean } from '@scure/bip39/wordlists/korean';
import { wordlist as portuguese } from '@scure/bip39/wordlists/portuguese';
import { wordlist as simplifiedChinese } from '@scure/bip39/wordlists/simplified-chinese';
import { wordlist as spanish } from '@scure/bip39/wordlists/spanish';
import { wordlist as traditionalChinese } from '@scure/bip39/wordlists/traditional-chinese';

Security

To audit wordlist content, run node scripts/fetch-wordlist.js.

The library has been independently audited:

The library was initially developed for js-ethereum-cryptography. At commit ae00e6d7, it was extracted to a separate package called micro-bip39. After the audit we've decided to use @scure NPM namespace for security.

License

MIT License

Copyright (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com)

scure-bip39's People

Contributors

jeetiss avatar mikeobank avatar paulmillr avatar tmm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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

scure-bip39's Issues

Expected Uint8Array - React Native

Great library!

But in react native, its giving the following error.

when

const mn = bip39.generateMnemonic(wordlist);
console.log(mn);

Simulator Screen Shot - iPhone 12 Pro - 2022-07-31 at 21 00 15

randomBytes error React Native

Hello!
I am getting an error when I try to use this package on React Native 0.7 generating the mnemonic seed:
image
Using react-native-random-bytes instead of the one provided with the package kinda solves the issue, but that's of course
something I really don't want to do.

Am I missing something obvious here? ๐Ÿ˜…

Unexpected token * error

Hi,
I am trying to do truffle compile on my linux machine but i am getting below error. I tried reinstalling node modules and also tried reinstalling bip39 separaeltely but it didn't worked.

/home/ubuntu///node_modules/@scure/bip39/index.js:54
if (!Array.isArray(wordlist) || wordlist.length !== 2 ** 11 || typeof wordlist[0] !== 'string')
^

SyntaxError: Unexpected token *
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/home/ubuntu///node_modules/@truffle/hdwallet-provider/node_modules/ethereum-cryptography/bip39/index.js:4:15)

Can we override the random source?

The official bip39 in JS supports overriding the random source. Can we override it here too?

This can help use this library in environments where neither node crypto nor webcrypto is available.

Update @noble/hashes

I'm not sure about why JavaScript package managers are so bad or maybe I'm missing something, but it seems that this package uses @noble/hashes 0.5.5 while other packages of the same suite are using @noble/hashes 0.5.7, which means the dependency is fetched and bundled twice?

Convert to ESM.

NodeJS now supports ES Modules, as well as all browsers. Currently, this package doesn't work natively in a browser without some extra bundling/transpiling step and it would be nice if it was brought up to date with modern best practices by distributing it as an ES Module.

Note: I believe I have spoken with you about this in other places, but I wanted to get an issue filed for tracking (and so I stop trying to use this package only to remember why I had to drop it previously!)

Salt prepending "mnemonic"

Great library, really appreciate your work!

I'm wondering if the salt() should be transforming the passphrase by prepending "mnemonic"?

const salt = (passphrase: string) => nfkd(`mnemonic${passphrase}`);

As far as I can tell this is undocumented, and could catch developers unawares, if they haven't dived in to the code, creating a passphrase different to what they expect.

Some ideas I've had for possible alternatives:

  • fallback if no passphrase is provided
  • force a passphrase to be provided
  • provide a separate function that uses the exact password given

Happy to make a PR if it helps :)

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.