A minimal ethereum javascript wallet.
LightWallet is a HD wallet that can store your private keys encrypted in the browser to allow you to run Ethereum dapps even if you're not running a local Ethereum node. It uses BIP32 and BIP39 to generate an HD tree of addresses from a randomly generated 12-word seed.
LightWallet is primarily intended to be a signing provider for the [Hooked Web3 provider](https://github.com/ConsenSys/hooked-web3 provider) through the keystore
module. Moreover, the txutils
functions can be used to construct transactions when offline, for use in e.g. air-gapped coldwallet implementations.
The default BIP32 HD derivation path is m/0'/0'/0'/i
.
git clone https://github.com/ConsenSys/LightWallet.git
cd LightWallet
npm install
npm run build-js
This will create the file ethlightjs.min.js
that can be included in an HTML page:
<html>
<body>
<script src="ethlightjs.min.js"></script>
</body>
</html>
The file ethlightjs
exposes the global object ethlightjs
to the browser which has the two main modules ethlightjs.keystore
and ethlightjs.txutils
.
To build a node package:
npm install path/to/LightWallet
Sample usage:
// generate a new BIP32 12-word seed
var secretSeed = ethlightjs.keystore.generateRandomSeed();
// the seed is stored encrypted by a user-defined password
var password = prompt('Enter password for encryption', 'password');
var ks = new ethlightjs.keystore(secretSeed, password);
// generate five new address/private key pairs
// the corresponding private keys are also encrypted
ks.generateNewAddress(password, 5);
var addr = ks.getAddresses();
// Create a custom passwordProvider to prompt the user to enter their
// password whenever the hooked web3 provider issues a sendTransaction
// call.
ks.passwordProvider = function (callback) {
var pw = prompt("Please enter password", "Password");
callback(null, pw);
};
// Now set ks as transaction_signer in the hooked web3 provider
// and you can start using web3 using the keys/addresses in ks!
These are the interface functions for the keystore object. The keystore object holds a 12-word seed according to BIP39 spec. From this seed you can generate addresses and private keys, and use the private keys to sign transactions.
Note: Addresses and RLP encoded data are in the form of hex-strings. Hex-strings do not start with 0x
.
Constructor of the keystore object. The seed seed
is encrypted with password
and stored encrypted in the keystore.
- words: string defining a 12-word seed according to BIP39
- password: password to encrypt the seed
Generates a string consisting of a random 12-word seed and returns it. If the optional argument string extraEntropy
is present the random data from the Javascript RNG will be concatenated with extraEntropy
and then hashed to produce the final seed. The string extraEntropy
can be something like entropy from mouse movements or keyboard presses, or a string representing dice throws.
Checks if seed
is a valid 12-word seed according to the BIP39 specification.
Generates a new address/private key pair from the seed and stores them in the keystore. The private key is stored encrypted with the users password. If the integer n
is supplied a batch of n
address/keypairs is generated.
Takes a serialized keystore string serialized_keystore
and returns a new keystore object.
Serializes the current keystore object into a JSON-encoded string and returns that string.
Returns a list of hex-string addresses currently stored in the keystore.
Given the password, decrypts and returns the users 12-word seed.
Given the password, decrypts and returns the private key corresponding to address
. This should be done sparingly as the recommended practice is for the keystore
to sign transactions using keystore.signTx
, so there is normally no need to export private keys.
Signs a transaction with the private key corresponding to signingAddress
rawTx
: Hex-string defining an RLP-encoded raw transaction.password
: the users password (string)fromAddress
: hex-string defining the address to send the transaction from.
Hex-string corresponding to the RLP-encoded raw transaction.
Using the data in txObject
, creates an RLP-encoded transaction that will create the contract with compiled bytecode defined by txObject.data
. Also computes the address of the created contract.
fromAddress
: Address to send the transaction fromtxObject.gasLimit
: Gas limittxObject.gasPrice
: Gas pricetxObject.value
: Endowment (optional)txObject.nonce
: Nonce offromAddress
txObject.data
: Compiled code of the contract
Object obj
with fields
obj.tx
: RLP encoded transaction (hex string)obj.addr
: Address of the created contract
Creates a transaction calling a function with name functionName
, with arguments args
conforming to abi
. The function is defined in a contract with address txObject.to
.
abi
: Json-formatted ABI as returned from thesolc
compilerfunctionName
: string with the function nameargs
: Array with the arguments to the functiontxObject.to
: Address of the contracttxObject.gasLimit
: Gas limittxObject.gasPrice
: Gas pricetxObject.value
: Value to sendtxObject.nonce
: Nonce of sending address
RLP-encoded hex string defining the transaction.
Creates a transaction sending value to txObject.to
.
txObject.to
: Address to send totxObject.gasLimit
: Gas limittxObject.gasPrice
: Gas pricetxObject.value
: Value to sendtxObject.nonce
: Nonce of sending address
RLP-encoded hex string defining the transaction.
See the file example_usage.js
for usage of keystore
and txutils
.
See the file example_helpers.js
for using the helpers
functions.
See the file example_web.html
for an example of how to use the LightWallet functionality in the browser.
Run all tests:
npm run test
npm run coverage