-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sent-Transactions.js
278 lines (276 loc) · 18.8 KB
/
Sent-Transactions.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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 searchBox=document.getElementById("searchBox");
const searchButton=document.getElementById("searchButton");
const txTable=document.getElementById("txTable");
const tableValues=document.getElementsByClassName("Values");
const timestampValue=document.getElementById("timestampValue");
const moreDetails=document.getElementById("moreDetails");
const mainWallet=web3.eth.accounts.wallet;//To create horizontal bar, Account information should be gotten from this.
mainWallet.load(statusPad);
const connectedNetwork=document.getElementById("connectedNetwork");
const walletAccount=document.getElementById("walletAccount");
const walletAccountsList=document.getElementById("walletAccountsList");
const networkComponents=document.getElementsByClassName("horizontalItem");
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//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");
//Map from network to Etherscan:
const networkToEtherscan=new Map();
networkToEtherscan.set("Ethereum Mainnet","https://etherscan.io/tx/");
networkToEtherscan.set("Ropsten (Test Network)","https://ropsten.etherscan.io/tx/");
networkToEtherscan.set("Kovan (Test Network)","https://kovan.etherscan.io/tx/");
networkToEtherscan.set("Rinkeby (Test Network)","https://rinkeby.etherscan.io/tx/");
networkToEtherscan.set("Goerli (Test Network)","https://goerli.etherscan.io/tx/");
//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);
searchButton.addEventListener("click", function(){ txSearch((searchBox.value).toString())});
moreDetails.addEventListener("click", function(){window.location.href=networkToEtherscan.get(providerToNetwork.get(web3.eth.currentProvider.url))+tableValues[3].innerHTML});
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 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 txSearch(pointer){
pointer=pointer.trim();
txTable.style.visibility="visible";
try{
if(pointer.includes(":")){
const seperator=pointer.indexOf(":");
const blockHash=pointer.slice(0,seperator);
const txIndex=pointer.slice(seperator+1);
try{
if((web3.utils.isHexStrict(blockHash))||(Number.isInteger(Number(blockHash))&&blockHash>=0)){
web3.eth.getTransactionFromBlock(blockHash,txIndex).then(function(txObject){
web3.eth.getBlock(txObject.blockHash).then(function(block){
const blockTimestamp=new Date(block.timestamp*1000);
timestampValue.innerHTML=blockTimestamp.toUTCString();
tableValues[0].innerHTML=txObject.blockHash;
tableValues[1].innerHTML=txObject.blockNumber;
tableValues[2].innerHTML=parseInt(txObject.chainId,16);
tableValues[3].innerHTML=txObject.hash;
tableValues[4].innerHTML=txObject.transactionIndex;
tableValues[5].innerHTML=txObject.from;
tableValues[6].innerHTML=txObject.to;
tableValues[7].innerHTML=web3.utils.fromWei(txObject.value)+" ETH";
tableValues[8].innerHTML=txObject.gas;
tableValues[9].innerHTML=web3.utils.fromWei(txObject.gasPrice)+" ETH";
tableValues[10].innerHTML=web3.utils.fromWei(txObject.gas*txObject.gasPrice+txObject.value)+" ETH";
tableValues[11].innerHTML=block.miner;
});
});
}else{
throw "Input is invalid!";
}
}catch(error){
window.alert(error);
}
}else
try{
if(web3.utils.isHexStrict(pointer)){
web3.eth.getTransaction(pointer).then(function(txObject){
web3.eth.getBlock(txObject.blockHash).then(function(block){
const blockTimestamp=new Date(block.timestamp*1000);
timestampValue.innerHTML=blockTimestamp.toUTCString();
tableValues[0].innerHTML=txObject.blockHash;
tableValues[1].innerHTML=txObject.blockNumber;
tableValues[2].innerHTML=parseInt(txObject.chainId,16);
tableValues[3].innerHTML=txObject.hash;
tableValues[4].innerHTML=txObject.transactionIndex;
tableValues[5].innerHTML=txObject.from;
tableValues[6].innerHTML=txObject.to;
tableValues[7].innerHTML=web3.utils.fromWei(txObject.value)+" ETH";
tableValues[8].innerHTML=txObject.gas;
tableValues[9].innerHTML=web3.utils.fromWei(txObject.gasPrice)+" ETH";
tableValues[10].innerHTML=web3.utils.fromWei(txObject.gas*txObject.gasPrice+txObject.value)+" ETH";
tableValues[11].innerHTML=block.miner;
})
});
}else{
throw "Input is invalid!";
}
}catch(error){
window.alert(error);
}
}
catch(error){
window.alert(error.name+":\n"+error.message);
}
moreDetails.style.visibility="visible";
}
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});
}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";
}