Skip to content

Instantly share code, notes, and snippets.

@PavelPolyakov
Created June 7, 2017 20:36
Show Gist options
  • Save PavelPolyakov/a89bc59f8fe34abe70ef189da8472def to your computer and use it in GitHub Desktop.
Save PavelPolyakov/a89bc59f8fe34abe70ef189da8472def to your computer and use it in GitHub Desktop.
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
const lightwallet = require('eth-lightwallet');
const {txutils, signing} = lightwallet;
const fs = require('fs');
const Tx = require('ethereumjs-tx');
let ks = lightwallet.keystore.deserialize(fs.readFileSync(`${__dirname}/ks`));
let i = 0;
ks.keyFromPassword('hello', (err, pwDerivedKey) => {
i++;
const FROM = `0x${ks.getAddresses()[0]}`;
const TO = '0xf1b40f9a11da308d9ee78ecbe04cd83115cf79ea';
console.log('=======================');
console.log(`== INITIAL STATE [${i}] ==`);
console.log('=======================');
console.log('from: ', FROM, web3.eth.getBalance(FROM).toNumber());
console.log('to: ', TO, web3.eth.getBalance(TO).toNumber());
/**
* 2. Transfer using the RAW transaction, signing by the eth-lightwallet
*/
console.log('=======');
console.log('== 2 ==');
console.log('=======');
let txOptions = {
to: '0xf1b40f9a11da308d9ee78ecbe04cd83115cf79ea',
value: 100
};
var valueTx = txutils.valueTx(txOptions)
var signedValueTx = signing.signTx(ks, pwDerivedKey, valueTx, FROM)
web3.eth.sendRawTransaction(signedValueTx);
console.log('from: ', FROM, web3.eth.getBalance(FROM).toNumber());
console.log('to: ', TO, web3.eth.getBalance(TO).toNumber());
});
[~/Sites/smart-kt]$ node var/test/get-balance-eth-lightwallet.js [master]
The new KeyStore constructor has been deprecated in favor of KeyStore.createVault(), and will be removed in a future version. Read issue #109 for info. https://github.com/ConsenSys/eth-lightwallet/issues/109
=======================
== INITIAL STATE [1] ==
=======================
from: 0xac4a5e4d788f2e3e6823466d834bb1d3f00b8b61 22000
to: 0xf1b40f9a11da308d9ee78ecbe04cd83115cf79ea 99999999999999510000
=======
== 2 ==
=======
=======================
== INITIAL STATE [2] ==
=======================
from: 0xac4a5e4d788f2e3e6823466d834bb1d3f00b8b61 22000
to: 0xf1b40f9a11da308d9ee78ecbe04cd83115cf79ea 99999999999999510000
=======
== 2 ==
=======
path/smart-kt/node_modules/eth-lightwallet/node_modules/tweetnacl/nacl-fast.js:2152
throw new TypeError('unexpected type ' + t + ', use Uint8Array');
^
TypeError: unexpected type [object Undefined], use Uint8Array
at checkArrayTypes (path/smart-kt/node_modules/eth-lightwallet/node_modules/tweetnacl/nacl-fast.js:2152:14)
at Function.nacl.secretbox.open (path/smart-kt/node_modules/eth-lightwallet/node_modules/tweetnacl/nacl-fast.js:2211:3)
at Function.KeyStore._decryptString (path/smart-kt/node_modules/eth-lightwallet/lib/keystore.js:179:37)
at KeyStore.isDerivedKeyCorrect (path/smart-kt/node_modules/eth-lightwallet/lib/keystore.js:157:29)
at Object.signTx (path/smart-kt/node_modules/eth-lightwallet/lib/signing.js:6:16)
at ks.keyFromPassword (path/smart-kt/var/test/get-balance-eth-lightwallet.js:38:33)
at cb (path/smart-kt/node_modules/eth-lightwallet/lib/keystore.js:557:7)
at path/smart-kt/node_modules/scrypt-async/scrypt-async.js:518:13
at Immediate.<anonymous> (path/smart-kt/node_modules/scrypt-async/scrypt-async.js:481:11)
at runCallback (timers.js:570:20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment