Skip to content

Instantly share code, notes, and snippets.

@bitsoko-services
Last active November 16, 2017 20:27
Show Gist options
  • Save bitsoko-services/3956e8e9606541182c0d949d9f6150ab to your computer and use it in GitHub Desktop.
Save bitsoko-services/3956e8e9606541182c0d949d9f6150ab to your computer and use it in GitHub Desktop.
managing wallets
ethHost='https://mainnet.infura.io/f9T..........';
/*
usage:
createEth('username').then(function(e){
console.log(e);// wallet information
});
*/
function transferTokenValue(to,coin,fiat){
return new Promise(function(resolve, reject) {
var myWalt='0x..';// sender wallet address
var tokenRate = ;
var privateKey = '0x..';
var seAmt=(parseFloat(fiat)/(tokenRate)*Math.pow(10,allTokens[coin].decimals));// amount to send
var eth = new Eth(new SignerProvider(ethHost, {
signTransaction: (rawTx, cb) => cb(null, ethSigner.sign(rawTx, privateKey)),
accounts: (cb) => cb(null, [myWalt]),
}));
var chID=0x03;
// result null <BigNumber ...>
var trnDat={
"from": myWalt,
"nonce": web3.toHex(eth.getTransactionCount(myWalt)),
"gasPrice": web3.toHex(3000000000),
"gasLimit": web3.toHex(250000),
"chainId": chID
}
if(coin=='eth'){
trnDat.to=to;
trnDat.data='0x0';
trnDat.value=web3.toHex(parseInt(seAmt));
}else{
trnDat.value='0x0';
trnDat.data=allTokens[coin].contract.transfer.getData(to, parseInt(seAmt), {from: myWalt});
trnDat.to=allTokens[coin].contract.address;
}
eth.sendTransaction(trnDat).then(function(e){resolve(e)}).catch(function(r){reject(r)});
})
}
function createETH(user){
return new Promise(function(resolve, reject) {
var randomSeed = lightwallet.keystore.generateRandomSeed(randomString(300));
var randomSalt = lightwallet.keystore.generateSalt();
password = "";
while (password.length < 4) {
password = prompt("Create a new wallet to safely store your digital tokens. Enter a new password that you will use to unlock your new wallet", "");
}
// var password = prompt("Please enter password", "");
lightwallet.keystore.deriveKeyFromPassword(password, randomSalt, function (err, pwDerivedKey) {
// This is the default recommended hdPathString value.
var hdPathString = "m/0'/0'/0'";
// When specifying a salt, the hdPathString is required.
console.log(randomSeed, pwDerivedKey, hdPathString,randomSalt);
gks= new lightwallet.keystore(randomSeed, pwDerivedKey, hdPathString,randomSalt);
if(global_keystore==undefined){
global_keystore = gks;
// generate new address/private key pairs
// the corresponding private keys are also encrypted
global_keystore.generateNewAddress(pwDerivedKey, 1);
}
var addr = global_keystore.getAddresses();
var walData={publicAddress:JSON.stringify(addr), created:moment().valueOf(), coin:'eth'};
// console.log("your random seed is: " +JSON.stringify(walData));
var wd=walData;
//send to server
doFetch({action:'saveUserWallet', data: JSON.stringify(wd), user:user }).then(function(e){
if (e.status=="ok"){
walData.walletSeed=randomSeed;
walData.walletSalt=randomSalt;
var walSaving = getObjectStore('data', 'readwrite').put(JSON.stringify(walData), 'bits-wallets-'+user);
walSaving.onsuccess = function (event) {
resolve(walData);
Materialize.toast('created new Ethereum wallet', 3000);
}
} else{
reject('failed to create wallet, please try again');
Materialize.toast('failed to create wallet, please try again', 3000);
}
});
// getBalances();
// Create a custom passwordProvider to prompt the user to enter their
// password whenever the hooked web3 provider issues a sendTransaction
// call.
global_keystore.passwordProvider = function (callback) {
pw = "";
while (pw.length < 6) {
pw = prompt("Please enter 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!
});
})
}
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;
}
function getBalance(){
var myContract = web3.eth.contract(JSON.parse('[....]')).at('0x..');
myContract.balanceOf('0x..'),function(r,e){
if(!r)console.log(e.toNumber()); //100
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment