lightwallet

Source Lightwallet

The lightwallet lib is a wrapper for the original lightwallet.keystore and bitcore-mnemonic. It handles logged in users key vaults and encryption.


getKeyStore

lightwallet.getKeyStore();

returns CoreBundle.keystore (eth-lightwallet/lib/keystore)

Returns

lightwallet.keystore: keystore library

Example

const KeyStore = lightwallet.getKeyStore();
KeyStore.isSeedValid('word1 word2 ... word12');

getMnemonicLib

lightwallet.getMnemonicLib();

returns CoreBundle.Mnemonic

Returns

bitcore-mnemonic: the Mnemonic lib

Example

const Mnemonic = lightwallet.getMnemonicLib();
var code = new Mnemonic(Mnemonic.Words.SPANISH);
code.toString(); // natal hada sutil año sólido papel jamón combate aula flota ver esfera...
var xpriv = code.toHDPrivateKey();

createVault

lightwallet.createVault(mnemonic, password);

Creates a new vault instance to handle lightwallet interactions.

Parameters

  1. mnemonic - string: mnemonic to create new vault.
  2. password - string: password to encrypt the vault.

Returns

Promise returns lightwallet.keystore: vault created using mnemonic, encrypted via password

Example

const vault = await lightwallet.createVault(mnemonic, password);
const pwDerivedKey = await lightwallet.keyFromPassword(vault, password);

setVaultActive

lightwallet.setVaultActive(vault);

Serializes a specific vault and saves it to the local storage.

Parameters

  1. vault - lightwallet.keystore: vault to save locally

Example

const vault = await lightwallet.createVault(mnemonic, password);
setVaultActive(vault);

createVaultAndSetActive

lightwallet.createVaultAndSetActive(mnemonic, password);

Create new vault, set it active and set first account id as active one

Parameters

  1. mnemonic - lightwallet.keystore: mnemonic to create new vault.
  2. password - string: password to encrypt the vault.

Returns

Promise returns void: resolved when done

Example

await lightwallet.createVaultAndSetActive(mnemonic, password);
const vault = lightwallet.loadVault()

keyFromPassword

lightwallet.keyFromPassword(vault, password);

Gets the pwDerivedKey to interact with the vault.

Parameters

  1. vault - object: The options used for calling
  2. password - string: this can be set if required, defaults to "latest"

Returns

Promise returns Uint8Array: the pwDerivedKey

Example

const vault = await createVault(mnemonic, password);
const pwDerivedKey = await keyFromPassword(vault, password);

getNewVault

lightwallet.getNewVault(mnemonic, password);

Creates an new vault and unlocks it

Parameters

  1. mnemonic - lightwallet.keystore: mnemonic to create new vault.
  2. password - string: password to encrypt the vault.

Returns

Promise returns lightwallet.keystore: The new vault.

Example

const vault = await lightwallet.getNewVault(mnemonic, password);

getAccounts

lightwallet.getAccounts(vault, amount);

Get an specific amount of accounts from the vault.

Parameters

  1. vault - lightwallet.keystore: vault to get accounts from
  2. amount - number: number of accounts to return

Returns

Array<string>: The accounts.

Example

const accounts = lightwallet.getAccounts(vault, 1); // => [ '0x0...' ]

getPrivateKey

lightwallet.getPrivateKey(vault, accountId);

Gets the private key for an account. Given the derived key, 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 signing.signTx, so there is normally no need to export private keys.

Parameters

  1. vault - object: vault where the account lives
  2. accountId - string: account to get the private key from

Returns

string: accounts private key for submitting transactions, etc.

Example

lightwallet.getPrivateKey(vault, '0x00...');

loadVault

lightwallet.loadVault();

Load locked vault from localStorage or unlocked memory vault.

Returns

lightwallet.keystore: deserialized, cached vault

Example

const lockedVault = lightwallet.loadVault();

setPasswordFunction

lightwallet.setPasswordFunction(arguments);

Sets the password function. The dapp-browser does not includes any library / framework / css that handles a good and nice ui development (e.g. angular, react, bootstrap, …). To handle coporate design and a better DApp development freedom, each DApp must specify its own password dialog. In case of Angular 5 development have a look at the default one, provided by the angular-core: globalPasswordDialog https://github.com/evannetwork/ui-angular-core/blob/4f539a2f5492b137d6be82c133427871073c3929/src/services/evan/bcc.ts#L300

Parameters

  1. newPasswordFunction - Function: The new password function

Example

lightwallet.setPasswordFunction(() => {
  return prompt("Please enter your password");
});

getPassword

lightwallet.getPassword(accountId);

Shows the global-password modal that should return the users password as a string. Be sure that the “lightwallet.setPasswordFunction” was called before, to setup the password function that should be used.

Parameters

  1. accountId - string: additional account id to get the password from

Returns

Promise returns string: password input

Example

const pw = await lightwallet.getPassword('0x00');

loadUnlockedVault

lightwallet.loadUnlockedVault();

Return current unlocked vault. Asks for password when vault is locked.

Returns

lightwallet.keystore: unlocked vault

Example

const vault = await lightwallet.loadUnlockedVault();
console.log(vault.encryptionKey);

getEncryptionKey

lightwallet.getEncryptionKey();

Returns the encryption key for the current password.

Returns

Promise returns string: encryption key

Example

const encryptionKey = await lightwallet.getEncryptionKey();

getEncryptionKeyFromPassword

lightwallet.getEncryptionKeyFromPassword(accountId, password);

Hashes a password using sha3.

Parameters

  1. accountId - string: accountId for that the privateKey should be generated
  2. password - string: password that should be hashed

Returns

string: resolved when done

Example

profile.ipld.keyProvider.setKeysForAccount(
  accountId,
  lightwallet.getEncryptionKeyFromPassword(accountId, this.password)
);

deleteActiveVault

lightwallet.deleteActiveVault();

Remove current active vault from browser.

Example

lightwallet.deleteActiveVault();

isValidMnemonic

lightwallet.isValidMnemonic(mnemonic);

Returns if an mnemonic is a valid mnemonic. (wrapper for lightwallet.keystore.isSeedValid)

Parameters

  1. mnemonic - string: The options used for calling

Returns

void: True if valid mnemonic, False otherwise.

Example

lightwallet.isSeedValid('word1 word2 ... word12');

isValidMnemonicWord

lightwallet.isValidMnemonicWord(word);

Returns if an word is a valid mnemonic word.

Parameters

  1. word - string: word to check

Returns

boolean: True if valid mnemonic word, False otherwise.

Example

lightwallet.isValidMnemonicWord('cucumber'); // => false