GithubHelp home page GithubHelp logo

pcaversaccio / mnemonic-to-private-key Goto Github PK

View Code? Open in Web Editor NEW
15.0 4.0 5.0 163 KB

A JavaScript script that converts the mnemonic phrase into a wallet private key using the ethers.js library.

License: Do What The F*ck You Want To Public License

JavaScript 100.00%
ethereum ethersjs javascript mnemonic-phrase private-key

mnemonic-to-private-key's Introduction

From Mnemonic Phrase to Private Key

The JavaScript-based file script.js converts the mnemonic phrase into a wallet private key using the ethers.js library and the default derivation path m/44'/60'/0'/0/0. First, create a secrets.json file that includes the mnemonic phrase. The file should look like the following (make sure to always .gitignore it!):

{
  "seedPhrase": "brain theme ..."
}

Then run npm i first and thereafter node .\script.js.

Background on Deriving Path

We define the following 5 levels in the BIP32 path:

m / purpose' / coin_type' / account' / change / address_index

m = Master

Each level has a special meaning, described in the sections below. Apostrophe in the path indicates that BIP32 hardened derivation is used. Generally, there are two posible types of BIP32 derivation, hardened or non-hardened. In standard BIP32 path notation, hardened derivation at a particular level is indicated by an apostrophe. For example, the following example of hardened derivation is used for the first three levels, while for the last two levels non-hardened derivation is used:

m / 44' / 0' / 1' / 1 / 33

With non-hardened keys, you can prove a child public key is linked to a parent public key using just the public keys. You can also derive public child keys from a public parent key, which enables watch-only wallets. With hardened child keys, you cannot prove that a child public key is linked to a parent public key.

purpose

purpose is a constant set to 44' (or 0x8000002C) following the BIP43 recommendation. It indicates that the subtree of this node is used according to this specification.

Hardened derivation is used at this level.

coin_type

One master node (seed) can be used for unlimited number of independent cryptocurrencies such as Bitcoin, Litecoin, or Ethereum. However, sharing the same space for various cryptocurrencies has some disadvantages.

This level creates a separate subtree for every cryptocurrency, avoiding reusing addresses across cryptocurrency and improving privacy issues.

coin_type is a constant, set for each cryptocurrency. Cryptocurrency developers can ask for registering unused number for their project.

Hardened derivation is used at this level.

account

This level splits the key space into independent user identities, so the wallet never mixes the coins across different accounts.

Users can use these accounts to organise the funds in the same fashion as bank accounts; for donation purposes (where all addresses are considered public), for saving purposes, for common expenses etc.

Accounts are numbered from index 0 in sequentially increasing manner. This number is used as child index in BIP32 derivation.

Hardened derivation is used at this level. Furthermore, the software in use should prevent a creation of an account if a previous account does not have a transaction history (meaning none of its addresses have been used before). It needs to discover all used accounts after importing the seed from an external source.

change

Constant 0 is used for external chain and constant 1 for internal chain (also known as change addresses). External chain is used for addresses that are meant to be visible outside of the wallet (e.g. for receiving payments). Internal chain is used for addresses which are not meant to be visible outside of the wallet and is used for return transaction change.

Non-hardened a.k.a public derivation is used at this level.

address_index

Addresses are numbered from index 0 in sequentially increasing manner. This number is used as child index in BIP32 derivation.

Non-hardened derivation is used at this level.

Visualisation

References

[1] https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki

[2] https://github.com/bitcoin/bips/blob/master/bip-0043.mediawiki

[3] https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#path-levels

[4] https://wiki.trezor.io/Hardened_and_non-hardened_derivation

[5] https://learnmeabitcoin.com/technical/derivation-paths

mnemonic-to-private-key's People

Contributors

pcaversaccio avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

mnemonic-to-private-key's Issues

BTC private key to address and balance check

I just stumbled on these your GitHub repo I was looking for a program that will help me convert my compressed Bitcoin private key to address and check the balance. Could you please help me update or create a python program that will help me convert my compressed private key to address and check the balance. The reason I needed this was to scan through a massive private key txt file and see if I can find my missing 6 characters private key. Please help me if I recover the wallet I will donate some BTC to you. Thanks

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.