forked from zombie466851/Ethereum-Web-Wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
179 lines (139 loc) · 5.59 KB
/
app.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
App = {
provider: null,
cancelScrypt: false,
activeWallet: null,
contract: null,
setupWallet: function (wallet) {
//showWallet(); ///@dev
// connect to local Geth
//App.provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545");
// connect to ropsten
App.provider = ethers.getDefaultProvider('ropsten');
App.activeWallet = wallet.connect(App.provider);
var inputWalletAddress = $('#wallet-address');
inputWalletAddress.val(wallet.address);
App.refreshUI();
App.setupSendEther();
//App.initToken();
//App.setupSendToken();
},
init: function() {
App.initLoadKey();
App.initMnemonic();
},
//============================= Method for Load Wallet ==============================//
// Load PriKey
initLoadKey: function() {
var inputPrivatekey = $('#select-prikey');
var submit = $('#submit-prikey');
// generate prikey
let randomNumber = ethers.utils.bigNumberify(ethers.utils.randomBytes(32));
inputPrivatekey.val(randomNumber._hex);
// when click "Load Private Key"
submit.click(function() {
if (submit.hasClass('disable')) return;
var privateKey = inputPrivatekey.val();
if (privateKey.substring(0, 2) !== '0x') {
privateKey = '0x' + privateKey;
}
App.setupWallet(new ethers.Wallet(privateKey));
});
inputPrivatekey.on("input", function() {
if (inputPrivatekey.val().match(/^(0x)?[0-9A-fa-f]{64}$/)) {
submit.removeClass('disable');
} else {
submit.addClass('disable');
}
});
},
// Load Memonic Phrase
initMnemonic: function() {
var inputPhrase = $('#select-mnemonic-phrase');
var inputPath = $('#select-mnemonic-path');
var submit = $('#submit-mnemonic');
// generate
let mnemonic = ethers.utils.HDNode.entropyToMnemonic(ethers.utils.randomBytes(16));
inputPhrase.val(mnemonic);
function check() {
if (ethers.utils.HDNode.isValidMnemonic(inputPhrase.val())) {
submit.removeClass('disable');
} else {
submit.addClass('disable');
}
}
inputPhrase.on("input", check);
inputPath.on("input", check);
submit.click(function() {
if (submit.hasClass('disable')) return;
App.setupWallet(ethers.Wallet.fromMnemonic(inputPhrase.val(), inputPath.val()));
});
},
//============================= Method for ETH info ==============================//
refreshUI: function() {
var inputBalance = $('#wallet-balance');
var inputTransactionCount = $('#wallet-transaction-count');
$("#wallet-submit-refresh").click(function() {
App.addActivity('> Refreshing details...');
// get balance include pending TXs
App.activeWallet.getBalance('pending').then(function(balance) {
App.addActivity('< Balance: ' + balance.toString(10));
inputBalance.val(ethers.utils.formatEther(balance, { commify: true }));
}, function(error) {
console.log(error);
});
App.activeWallet.getTransactionCount('pending').then(function(transactionCount) {
App.addActivity('< TransactionCount: ' + transactionCount);
inputTransactionCount.val(transactionCount);
}, function(error) {
console.log(error);
});
});
$("#wallet-submit-refresh").click();
},
setupSendEther: function() {
var inputTargetAddress = $('#wallet-send-to-address');
var inputAmount = $('#wallet-send-amount');
var submit = $('#wallet-submit-send');
// Validate the address and value (to enable the send button)
function check() {
try {
ethers.utils.getAddress(inputTargetAddress.val());
ethers.utils.parseEther(inputAmount.val());
} catch (error) {
submit.addClass('disable');
return;
}
submit.removeClass('disable');
}
inputTargetAddress.on("input", check);
inputAmount.on("input", check);
// Send ether
submit.click(function() {
// get address with base transform
var targetAddress = ethers.utils.getAddress(inputTargetAddress.val());
/// ether -> wei
var amountWei = ethers.utils.parseEther(inputAmount.val());
App.activeWallet.sendTransaction({
to: targetAddress,
value: amountWei,
gasPrice: activeWallet.provider.getGasPrice(), // update from net
gasLimit: 21000,
}).then(function(tx) {
console.log(tx);
App.addActivity('< Transaction sent: ' + tx.hash.substring(0, 20) + '...');
alert('Success!');
inputTargetAddress.val('');
inputAmount.val('');
submit.addClass('disable');
App.refreshUI(); // update the balance
}, function(error) {
console.log(error);
});
})
},
addActivity: function(message) {
var activity = $('#wallet-activity');
activity.append("</br>" + message);
},
}
App.init();