-
-
Save allenhark/9046012ca67aefc31eed9fcb94cb9c44 to your computer and use it in GitHub Desktop.
managing wallets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('document').ready(function () { | |
var web3 = new Web3(); | |
var global_keystore; | |
function setWeb3Provider(keystore) { | |
var web3Provider = new HookedWeb3Provider({ | |
host: "https://mainnet.infura.io/-.....", | |
transaction_signer: keystore | |
}); | |
web3.setProvider(web3Provider); | |
} | |
function newAddresses(password, numAddr) { | |
if (password == '') { | |
password = prompt('Enter password to retrieve addresses', 'Password'); | |
} | |
global_keystore.keyFromPassword(password, function (err, pwDerivedKey) { | |
global_keystore.generateNewAddress(pwDerivedKey, numAddr); | |
var addresses = global_keystore.getAddresses(); | |
console.log(addresses) | |
for (var i = 0; i < addresses.length; ++i) { | |
console.log(addresses[i]) | |
} | |
getBalances(); | |
}); | |
} | |
function getBalances() { | |
var addresses = global_keystore.getAddresses(); | |
async.map(addresses, web3.eth.getBalance, function (err, balances) { | |
async.map(addresses, web3.eth.getTransactionCount, function (err, nonces) { | |
for (var i = 0; i < addresses.length; ++i) { | |
console.log(addresses[i] + ' (Bal: ' + (balances[i] / 1.0e18) + ' ETH, Nonce: ' + nonces[i] + ')'); | |
// document.getElementById('addr').innerHTML += '<div>' + addresses[i] + ' (Bal: ' + (balances[i] / 1.0e18) + ' ETH, Nonce: ' + nonces[i] + ')' + '</div>' | |
} | |
}) | |
}) | |
} | |
function setSeed() { | |
var password = prompt('Enter Password to encrypt your seed', 'Password'); | |
lightwallet.keystore.createVault({ | |
password: password, | |
seedPhrase: document.getElementById('seed').value, | |
//random salt | |
hdPathString: "m/0'/0'/0'" | |
}, function (err, ks) { | |
global_keystore = ks | |
document.getElementById('seed').value = '' | |
newAddresses(password); | |
setWeb3Provider(global_keystore); | |
getBalances(); | |
}) | |
} | |
/** New Wallet **/ | |
newWallet('password'); | |
function newWallet(password) { | |
var randomSeed = lightwallet.keystore.generateRandomSeed(randomString(300)); | |
console.log(randomSeed); | |
var infoString = 'Your new wallet seed is: "' + randomSeed + | |
'". Please write it down on paper or in a password manager, you will need it to access your wallet. Do not let anyone see this seed or they can take your Ether. ' + | |
'Please enter a password to encrypt your seed while in the browser.' | |
lightwallet.keystore.createVault({ | |
password: password, | |
seedPhrase: randomSeed, | |
//random salt | |
hdPathString: "m/0'/0'/0'" | |
}, function (err, ks) { | |
console.log(err, ks) | |
global_keystore = ks | |
newAddresses(password); | |
setWeb3Provider(global_keystore); | |
getBalances(); | |
}) | |
} | |
function showSeed() { | |
var password = prompt('Enter password to show your seed. Do not let anyone else see your seed.', 'Password'); | |
global_keystore.keyFromPassword(password, function (err, pwDerivedKey) { | |
var seed = global_keystore.getSeed(pwDerivedKey); | |
alert('Your seed is: "' + seed + '". Please write it down.'); | |
}); | |
} | |
function sendEth(fromAddr, toAddr, valueEth) { | |
var value = parseFloat(valueEth) * 1.0e18 | |
var gasPrice = 18000000000 | |
var gas = 50000 | |
web3.eth.sendTransaction({from: fromAddr, to: toAddr, value: value, gasPrice: gasPrice, gas: gas}, function (err, txhash) { | |
console.log('error: ' + err) | |
console.log('txhash: ' + txhash) | |
}) | |
} | |
function functionCall() { | |
var fromAddr = document.getElementById('functionCaller').value | |
var contractAddr = document.getElementById('contractAddr').value | |
var abi = JSON.parse(document.getElementById('contractAbi').value) | |
var contract = web3.eth.contract(abi).at(contractAddr) | |
var functionName = document.getElementById('functionName').value | |
var args = JSON.parse('[' + document.getElementById('functionArgs').value + ']') | |
var valueEth = document.getElementById('sendValueAmount').value | |
var value = parseFloat(valueEth) * 1.0e18 | |
var gasPrice = 50000000000 | |
var gas = 4541592 | |
args.push({from: fromAddr, value: value, gasPrice: gasPrice, gas: gas}) | |
var callback = function (err, txhash) { | |
console.log('error: ' + err) | |
console.log('txhash: ' + txhash) | |
} | |
args.push(callback) | |
contract[functionName].apply(this, args) | |
} | |
function randomString(length) { | |
var text = ""; | |
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
for (var i = 0; i < length; i++) { | |
text += possible.charAt(Math.floor(Math.random() * possible.length)); | |
} | |
return text; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment