-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete-Account.js
230 lines (229 loc) · 14.7 KB
/
Delete-Account.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const JSsha3=require("js-sha3");
const sha3_256=JSsha3.sha3_256;
const authPad=localStorage.getItem("walletAuthStatus");
const passwordHash=authPad.slice(0,64);
const statusPad=authPad.slice(64);
const status="b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12bfcbcf165908dd18a9e49f7ff27810176db8e9f63b4352213741664245224f8aa";
if(sha3_256(passwordHash+status.slice(0,64))==statusPad){
const Web3=require("web3");
const web3=new Web3(new Web3.providers.WebsocketProvider("wss://ropsten.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae"));
const createAccount=document.getElementById("createAccount");
const addAccount=document.getElementById("addAccount");
const deleteAccount=document.getElementById("deleteAccount");
const sendTransaction=document.getElementById("sendTransaction");
const viewAccounts=document.getElementById("viewAccounts");
const viewNetwork=document.getElementById("viewNetwork");
const history=document.getElementById("history");
const clearWallet=document.getElementById("clearWallet");
const removeWallet=document.getElementById("removeWallet");
const passwordReset=document.getElementById("passwordReset");
const logOut=document.getElementById("logOut");
const availableNetworks=document.getElementById("availableNetworks");
const sentTransactions=document.getElementById("sentTransactions");
const pendingTransactions=document.getElementById("pendingTransactions");
const viewNetworkItem=document.getElementsByClassName("viewNetworkItem");
const accountsTable=document.getElementById("accountsList");
const deleteButton=document.getElementById("deleteButton");
const mainWallet=web3.eth.accounts.wallet;
mainWallet.load(statusPad);
const connectedNetwork=document.getElementById("connectedNetwork");
const walletAccount=document.getElementById("walletAccount");
const walletAccountsList=document.getElementById("walletAccountsList");
const networkComponents=document.getElementsByClassName("horizontalItem");
const balanceCells=document.getElementsByClassName("balances");
const nonceCells=document.getElementsByClassName("nonces");
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Setting the horizontal bar values:
//Map to set network based on provider and vice versa:
const providerToNetwork=new Map();
providerToNetwork.set("wss://mainnet.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae", "Ethereum Mainnet");
providerToNetwork.set("wss://ropsten.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae", "Ropsten (Test Network)");
providerToNetwork.set("wss://kovan.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae", "Kovan (Test Network)");
providerToNetwork.set("wss://rinkeby.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae", "Rinkeby (Test Network)");
providerToNetwork.set("wss://goerli.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae", "Goerli (Test Network)");
const networkToProvider=new Map();
networkToProvider.set("Ethereum Mainnet","wss://mainnet.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae");
networkToProvider.set("Ropsten (Test Network)","wss://ropsten.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae");
networkToProvider.set("Kovan (Test Network)","wss://kovan.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae");
networkToProvider.set("Rinkeby (Test Network)","wss://rinkeby.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae");
networkToProvider.set("Goerli (Test Network)","wss://goerli.infura.io/ws/v3/57574a72c7bb464dbf287dfadeb940ae");
//Set provider gateway to the horizontal bar by current provider url:
connectedNetwork.value=providerToNetwork.get(web3.eth.currentProvider.url);
//Set chain id on horizontal bar:
web3.eth.getChainId().then(function(data){networkComponents[1].innerHTML+=" "+data});
//Set provider on horizontal bar:
networkComponents[2].innerHTML+=" "+web3.eth.currentProvider.url;
//Setting datalist of accessable accounts:
if(mainWallet.length>0){
for(let i=0; i<mainWallet.length; i++){
const listOption=document.createElement("option");
listOption.value="Index: "+mainWallet[i].index+" >> Address: "+mainWallet[i].address;
walletAccountsList.appendChild(listOption);
}
//Setting default account and show that on horizontal bar:
walletAccount.value=walletAccountsList.firstChild.value.slice(21);
web3.eth.defaultAccount=walletAccount.value;
//Setting balance and nonce of the default account:
web3.eth.getBalance(web3.eth.defaultAccount).then(function(data){networkComponents[4].innerHTML+=" "+web3.utils.fromWei(data)+" ETH"});
web3.eth.getTransactionCount(web3.eth.defaultAccount).then(function(data){networkComponents[5].innerHTML+=" "+data});
}else{
walletAccount.value="There is no account!";
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Setting events:
connectedNetwork.addEventListener("change", function(){networkShifting(connectedNetwork.value)});
walletAccount.addEventListener("change", function(){walletAccount.value=walletAccount.value.slice(21); defaultAccountSetting(walletAccount.value);});
createAccount.addEventListener("click", gotoCreateAccount);
addAccount.addEventListener("click", gotoAddAccount);
deleteAccount.addEventListener("click", gotoDeleteAccount);
sendTransaction.addEventListener("click", gotoSendTransaction);
viewAccounts.addEventListener("click", gotoViewAccounts);
viewNetwork.addEventListener("click", networkSubmenuSetting);
availableNetworks.addEventListener("click", function(){mainWallet.save(statusPad); window.location.href="Available-Networks.html"});
sentTransactions.addEventListener("click", function(){mainWallet.save(statusPad); window.location.href="Sent-Transactions.html"});
pendingTransactions.addEventListener("click", function(){mainWallet.save(statusPad); window.location.href="Pending-Transactions.html"});
clearWallet.addEventListener("click", walletClearing);
removeWallet.addEventListener("click", function(){window.alert("This option will be operatable whenever this wallet upgraded to a HD wallet!")});
passwordReset.addEventListener("click",function(){window.location.href="Password-Reset.html"});
logOut.addEventListener("click", walletLogout);
deleteButton.addEventListener("click", deleteCheckedAccounts);
for(let i=0; i<mainWallet.length; i++){
let accountCheckbox=document.createElement("input");
accountCheckbox.type="checkbox";
accountCheckbox.className="checkBox";
accountCheckbox.value=mainWallet[i].index;
let newRow=accountsTable.insertRow();
newRow.insertCell().appendChild(accountCheckbox);
newRow.insertCell().textContent=mainWallet[i].index;
newRow.insertCell().textContent=mainWallet[i].address;
newRow.insertCell().className="balances";
newRow.insertCell().className="nonces"
web3.eth.getBalance(mainWallet[i].address).then(function(data){balanceCells[i].textContent=parseFloat(web3.utils.fromWei(data)).toFixed(3)});
web3.eth.getTransactionCount(mainWallet[i].address).then(function(data){nonceCells[i].textContent=data});
}
function gotoCreateAccount(){
try{
if(mainWallet.save(statusPad)){
window.location.href="Create-Account.html";
}else{
throw "The process failed and stopped!\nYou was about to lose this wallet forever!\nTry again!";
}
}
catch(error){
window.alert(error);
}
}
function gotoAddAccount(){
try{
if(mainWallet.save(statusPad)){
window.location.href="Add-Account.html";
}else{
throw "The process failed and stopped!\nYou was about to lose this wallet forever!\nTry again!";
}
}
catch(error){
window.alert(error);
}
}
function gotoDeleteAccount(){
try{
if(mainWallet.save(statusPad)){
window.location.href="Delete-Account.html";
}else{
throw "The process failed and stopped!\nYou was about to lose this wallet forever!\nTry again!";
}
}
catch(error){
window.alert(error);
}
}
function deleteCheckedAccounts(){
if(window.confirm("Are you sure about the selected accounts?\nA removed account will not be available more!")){
const checkboxes=document.getElementsByClassName("checkBox");
for(let i=0; i<checkboxes.length; i++){
if(checkboxes[i].checked){
mainWallet.remove(checkboxes[i].value);
}
}
mainWallet.save(statusPad);
window.location.href="Delete-Account.html";
}
}
function gotoSendTransaction(){
try{
if(mainWallet.save(statusPad)){
window.location.href="Send-Transaction.html";
}else{
throw "The process failed and stopped!\nYou was about to lose this wallet forever!\nTry again!";
}
}
catch(error){
window.alert(error);
}
}
function gotoViewAccounts(){
try{
if(mainWallet.save(statusPad)){
window.location.href="View-Accounts.html";
}else{
throw "The process failed and stopped!\nYou was about to lose this wallet forever!\nTry again!";
}
}
catch(error){
window.alert(error);
}
}
function networkSubmenuSetting(){
if(viewNetworkItem[0].style.display=="none"||viewNetworkItem[0].style.display==""){
for(let i=0; i<viewNetworkItem.length; i++){
viewNetworkItem[i].style.display="block";
}
}else{
for(let i=0; i<viewNetworkItem.length; i++){
viewNetworkItem[i].style.display="none";
}
}
}
function walletClearing(){
if(confirm("You are deleting all accounts from this wallet!\nWe suggest you take a backup from the accounts in first then delete them!")){
if(confirm("Did You take a backup from the accouts?\nClick 'Cancel' to prevent delete accounts!")){
mainWallet.clear();
window.alert("The wallet was cleared and there is no account here!");
}
}
}
function walletLogout(){
if(confirm("Are you sure about the exit?")){
const authPad=localStorage.getItem("walletAuthStatus");
const passwordHash=authPad.slice(0,64);
localStorage.setItem("walletAuthStatus",passwordHash+sha3_256(passwordHash+status.slice(64)));
window.location.href="Login.html";
}
}
function networkShifting(networkName){
try{
if(web3.setProvider(networkToProvider.get(networkName))){
web3.eth.getChainId().then(function(ID){networkComponents[1].innerHTML="Chain ID: "+ID;})
networkComponents[2].innerHTML="Provider: "+web3.eth.currentProvider.url;
web3.eth.getBalance(web3.eth.defaultAccount).then(function(balance){networkComponents[4].innerHTML="Balance: "+web3.utils.fromWei(balance)+" ETH"});
web3.eth.getTransactionCount(web3.eth.defaultAccount).then(function(nonce){networkComponents[5].innerHTML="Nonce: "+nonce});
for(let i=0; i<mainWallet.length; i++){
web3.eth.getBalance(mainWallet[i].address).then(function(data){balanceCells[i].textContent=parseFloat(web3.utils.fromWei(data)).toFixed(3)});
web3.eth.getTransactionCount(mainWallet[i].address).then(function(data){nonceCells[i].textContent=data});
}
}else{
throw "The given network can not be set!"
}
}
catch(error){
window.alert(error);
}
}
function defaultAccountSetting(address){
web3.eth.defaultAccount=address;
web3.eth.getBalance(web3.eth.defaultAccount).then(function(balance){networkComponents[4].innerHTML="Balance: "+web3.utils.fromWei(balance)+" ETH"});
web3.eth.getTransactionCount(web3.eth.defaultAccount).then(function(nonce){networkComponents[5].innerHTML="Nonce: "+nonce});
}
}else{
window.location.href="Login.html";
}