-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
60 lines (51 loc) · 1.5 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const web3 = new Web3("https://ropsten.infura.io/v3/"+INFURA_API_KEY)
var address;
var privateKey;
function createNewWallet(){
newWallet = web3.eth.accounts.create();
address = newWallet.address;
privateKey = newWallet.privateKey;
saveWallet();
updateKeys();
}
function updateKeys(){
addr = document.getElementById('addr');
private = document.getElementById('private');
addr.innerHTML = address;
private.innerHTML = privateKey;
}
function updateBalance(){
web3.eth.getBalance(address, (err, wei) => {
balance = web3.utils.fromWei(wei, 'ether')
balanceDiv = document.getElementById('balance');
balanceDiv.innerHTML = balance;
})
}
function saveWallet(){
Cookies.set('address', address);
Cookies.set('privateKey', privateKeyImport);
}
function loadWallet(){
address = Cookies.get('address');
privateKey = Cookies.get('privateKey');
}
function importWallet(){
privateKeyImport = document.getElementById("privateKeyImport");
privateKeyImport = privateKeyImport.value;
importedWallet = web3.eth.accounts.privateKeyToAccount(privateKeyImport, true);
address = importedWallet.address;
privateKey = importedWallet.privateKey;
saveWallet();
updateKeys();
updateBalance();
}
if(Cookies.get('address') == undefined){
console.log("Wallet not found. Creating.");
createNewWallet();
updateBalance();
} else {
console.log("Loading existing wallet.");
loadWallet();
updateKeys();
updateBalance();
}