Skip to content

Instantly share code, notes, and snippets.

@PavelPolyakov
Last active June 6, 2017 17:54
Show Gist options
  • Save PavelPolyakov/1a2bc0a2b97e363900c19710390211da to your computer and use it in GitHub Desktop.
Save PavelPolyakov/1a2bc0a2b97e363900c19710390211da 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`));
ks.keyFromPassword('hello', (err, pwDerivedKey) => {
const FROM = `0x${ks.getAddresses()[0]}`;
const TO = '0xa43491c1c043931e6b2f3a1857837a58407feb25';
console.log('===================');
console.log('== INITIAL STATE ==');
console.log('===================');
console.log('from: ', FROM, web3.eth.getBalance(FROM).toNumber());
console.log('to: ', TO, web3.eth.getBalance(TO).toNumber());
/**
* 1. Transfer from the unlocked account
*/
console.log('=======');
console.log('== 1 ==');
console.log('=======');
web3.eth.sendTransaction({from:TO, to:FROM, value: 1000});
console.log('from: ', TO, web3.eth.getBalance(TO).toNumber());
console.log('to: ', FROM, web3.eth.getBalance(FROM).toNumber());
/**
* 2. Transfer using the RAW transaction, signing by the eth-lightwallet
*/
console.log('=======');
console.log('== 2 ==');
console.log('=======');
let txOptions = {
to: '0xa43491c1c043931e6b2f3a1857837a58407feb25',
value: 2000
};
var valueData = txutils.valueTx(FROM, txOptions);
var signedTx = signing.signTx(ks, pwDerivedKey, valueData, FROM)
web3.eth.sendRawTransaction(signedTx);
console.log('from: ', FROM, web3.eth.getBalance(FROM).toNumber());
console.log('to: ', TO, web3.eth.getBalance(TO).toNumber());
/**
* 3. Transfer using the RAW transaction, signing like in the web3 example
*/
console.log('=======');
console.log('== 3 ==');
console.log('=======');
var rawTx = {
to: txOptions.to,
value: '3000',
}
var tx = new Tx(rawTx);
tx.sign(new Buffer(ks.exportPrivateKey(ks.getAddresses()[0], pwDerivedKey), 'hex'));
var serializedTx = tx.serialize();
web3.eth.sendRawTransaction(serializedTx);
console.log('from: ', FROM, web3.eth.getBalance(FROM).toNumber());
console.log('to: ', TO, web3.eth.getBalance(TO).toNumber());
process.exit();
});
===================
== INITIAL STATE ==
===================
from: 0xac4a5e4d788f2e3e6823466d834bb1d3f00b8b61 5999999999140234000
to: 0xa43491c1c043931e6b2f3a1857837a58407feb25 93997480000858690000
=======
== 1 ==
=======
from: 0xac4a5e4d788f2e3e6823466d834bb1d3f00b8b61 5999999999140235000
to: 0xa43491c1c043931e6b2f3a1857837a58407feb25 93997480000858670000
=======
== 2 ==
=======
from: 0xac4a5e4d788f2e3e6823466d834bb1d3f00b8b61 5999999999140182000
to: 0xa43491c1c043931e6b2f3a1857837a58407feb25 93997480000858670000
=======
== 3 ==
=======
from: 0xac4a5e4d788f2e3e6823466d834bb1d3f00b8b61 5999999998281364000
to: 0xa43491c1c043931e6b2f3a1857837a58407feb25 93997480001717450000
eth_sendTransaction
Transaction: 0x771fabbdb97f6a85411039249491495bee47dd42060c99ed724c3b977d585461
Gas usage: 0x5208
Block Number: 0x1f
Block Time: Tue Jun 06 2017 19:38:01 GMT+0200 (CEST)
eth_getBalance
eth_getBalance
eth_sendRawTransaction
Transaction: 0x9cb405834ece0b97b0bbac6261d954e23d879ae55a0cf75b5717c83f19dbc55d
Contract created: 0xc57c61da6bd6a98af62383730fbfc30f5d2281b0
Gas usage: 0xcf08
Block Number: 0x20
Block Time: Tue Jun 06 2017 19:38:01 GMT+0200 (CEST)
eth_getBalance
eth_getBalance
eth_sendRawTransaction
Transaction: 0xa5d2e5b59cc16fbce468724edf59131e2483c6e5e4163cfe8a288d55c1da1c95
Gas usage: 0x5208
Block Number: 0x21
Block Time: Tue Jun 06 2017 19:38:02 GMT+0200 (CEST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment