Coder Social home page Coder Social logo

eth-wallet-light's Introduction

eth-wallet-light

A lightweight, pure JS Ethereum wallet optimized for mobile. Inspired in part by the Consensys eth-lightwallet. This code has not been independently audited, use at your own risk. Features include:

  • No dependency on the Node crypto module. This fact makes the library ideal for use in e.g. React Native.
  • BIP39 seed words, from a fork of the canonical library that removes dependency on Node crypto.
  • Keystores, when stored in-memory or as serialized keystore objects, are always securely encrypted with PBKDF2. All functions accessing sensitive information require a password.
  • Keystores can optionally be initialized with a custom RNG for additional randomness. THIS IS HIGHLY RECOMMENDED, as the default RNG used is not a CSPRNG.

Installation

npm install NoahZinsmeister/eth-wallet-light

const wallet = require('eth-wallet-light')

General Functions

wallet.isMnemonicValid(mnemonic)

Checks the validity of the passed mnemonic.

Options

  • mnemonic (required): A 12-word BIP39 seed phrase.

wallet.concatSignature(signature)

Concatenates the output of keystore.signMessageHash into a single hex string.

Options

  • signature (required): A signature object.

Keystore functions

new wallet.Keystore(rng)

This is the constructor for new keystores. Does not create a keypair.

Options

  • rng (optional): A function with one argument (the number of bytes), which must return a random hex string of that many bytes (0x prefix optional). Defaults to a non-secure RNG provided by crypto-js if not passed.

Returns Keystore

keystore.initializeFromEntropy(entropy, password)

This method initializes a keystore with a new random keypair. The password is used to encrypt the initialized keystore.

Options

  • entropy (required): A string of entropy. Will be hashed with 32 bytes of output from the keystore's rng to produce 16 bytes of randomness that is fed to the BIP39 mnemonic generator.
  • password (required): A string password that will be fed to PBKDF2 to produce a key that will encrypt the sensitive contents of the keystore.

Returns Promise(Keystore)

keystore.restoreFromMnemonic(mnemonic, password)

This method initializes a keystore, restoring a keypair from a mnemonic. The password is used to encrypt the initialized keystore.

Options

  • menemonic (required): 12 BIP39-compliant seed words. Can be used to recover backed-up or new accounts.
  • password (required): A string password that will be fed to PBKDF2 to produce a key that will encrypt the sensitive contents of the keystore.

Returns Promise(Keystore)

keystore.restorefromSerialized(serializedKeystore)

This method restores a keystore from serialization. Note that when restoring from a serialized keystore, the rng argument to the keystore constructor is unnecessary, and can safely be left as undefined.

Options

  • serializedKeystore (required): The output of keystore.serialize().

Returns Keystore

keystore.serialize()

This method serializes a keystore into a string.

Returns String

keystore.signMessageHash(messageHash, password)

Sign a message with the keystore's private key.

Options

  • messageHash (required): A hex-encoded 32-byte message. The 0x prefix is optional (it is stripped out).
  • password (required): The password that encrypts the contents of the keystore.

Returns String

keystore.getMnemonic(password)

Get the mnemonic from the keystore.

Options

  • password (required): The password that encrypts the contents of the keystore.

Returns String

keystore.getPrivateKey(password)

Get the private key from the keystore.

Options

  • password (required): The password that encrypts the contents of the keystore.

Returns String

keystore.getAddress()

Get the public address from the keystore.

Returns String

Sample Code

Check test/test.js for exhaustive usage examples. Some starter code:

const wallet = require('eth-wallet-light')

const password = 'mypassword' // this should be a real password

var keystore = await new wallet.Keystore().initializeFromEntropy(entropy, password)
console.log('Address: ', keystore.getAddress())

var messageHash = '0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658'
var signature = wallet.concatSignature(keystore.signMessageHash(messageHash, password))
console.log('Signature:', signature)

In Node, here are two example rng functions that are both CSPRNGs. In React Native, this code should instead rely on something like react-native-securerandom.

const crypto = require('crypto')

const csprng = (bytes) => { return crypto.randomBytes(bytes).toString('hex') }
const csprngPromise = (bytes) => {
  return new Promise(function(resolve, reject) {
    crypto.randomBytes(bytes, (err, buf) => {
      err ? reject(err) : resolve(buf.toString('hex'))
    })
  })
}

eth-wallet-light's People

Stargazers

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

Watchers

 avatar  avatar  avatar

eth-wallet-light's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

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.