-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwallet.php
292 lines (258 loc) · 9.8 KB
/
wallet.php
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
session_start();
error_reporting(0);
include('includes/config.php');
if (strlen($_SESSION['alogin']) == 0) {
header('location:login.php');
} else {
$email = $_SESSION['alogin'];
}
?>
<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<meta name="theme-color" content="#3e454c">
<title>HTHW Wallet</title>
<!-- Font awesome -->
<link rel="stylesheet" href="css/font-awesome.min.css">
<!-- Sandstone Bootstrap CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css">
<!-- Bootstrap social button library -->
<link rel="stylesheet" href="css/bootstrap-social.css">
<!-- Bootstrap select -->
<link rel="stylesheet" href="css/bootstrap-select.css">
<!-- Bootstrap file input -->
<link rel="stylesheet" href="css/fileinput.min.css">
<!-- Awesome Bootstrap checkbox -->
<link rel="stylesheet" href="css/awesome-bootstrap-checkbox.css">
<!-- Admin Style -->
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/homepage.css">
<style>
.errorWrap {
padding: 10px;
margin: 0 0 20px 0;
background: #dd3d36;
color: #fff;
-webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
}
.succWrap {
padding: 10px;
margin: 0 0 20px 0;
background: #5cb85c;
color: #fff;
-webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
}
/* Style for the page title */
h1, h2 {
text-align: center;
color: #debf12;
}
/* Style for the buttons */
button {
display: inline-block;
background-color: #34bcaa;
color: #fff;
padding: 10px 20px;
text-decoration: none;
border-radius: 25px;
font-weight: bold;
margin: 0 auto;
box-shadow: 0 0 10px #debf12;
}
/* Style for the form and labels */
form {
max-width: 300px;
margin: 0 auto;
}
label {
display: block;
margin-top: 10px;
color: #fff;
}
input[type="text"],
input[type="number"] {
width: 100%;
padding: 10px;
margin-top: 5px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
/* Style for the submit button */
button[type="button"] display: inline-block;
background-color: #34bcaa;
color: #fff;
padding: 10px 20px;
text-decoration: none;
border-radius: 25px;
font-weight: bold;
margin: 10px;
box-shadow: 0 0 10px #debf12;
}
/* Style for the wallet information */
#walletInfo {
text-align: center;
margin: 20px auto;
padding: 10px;
max-width: 300px;
color: #fff;
}
/* Style for the balance and wallet address */
#walletAddress,
#balance {
font-weight: bold;
color: #fff;
}
/* Style for error messages */
.alert {
background-color: #f44336;
color: white;
padding: 10px;
text-align: center;
border-radius: 4px;
margin-top: 10px;
}
</style>
</head>
<body>
<?php include('includes/header.php'); ?>
<div class="ts-main-content">
<?php include('includes/leftbar.php'); ?>
<div class="content-wrapper">
<h1>HTHW Wallet</h1>
</br>
</br>
<button id="connectWallet">Connect to Wallet</button>
<button id="disconnectWallet" style="display: none;">Disconnect Wallet</button>
</br>
<div id="walletInfo" style="display: none;">
<p>Wallet Address: <span id="walletAddress"></span></p>
<p>Balance: <br><span id="balance"></span> HTHW</p>
</div>
<h2>Send Funds</h2>
<form id="sendFundsForm">
<label for="recipient">Recipient Address:</label>
<input type="text" id="recipient" name="recipient" required><br>
<label for="amount">Amount (ETH):</label>
<input type="number" step="0.01" id="amount" name="amount" required><br>
<button type="button" id="send">Send</button>
</form>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap-select.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.dataTables.min.js"></script>
<script src="js/dataTables.bootstrap.min.js"></script>
<script src="js/Chart.min.js"></script>
<script src="js/fileinput.js"></script>
<script src="js/chartData.js"></script>
<script src="js/main.js"></script>
<script type="text/javascript">
$(document).ready(function () {
setTimeout(function () {
$('.succWrap').slideUp("slow");
}, 3000);
});
</script>
<!-- Include the JavaScript code for wallet interaction -->
<script src="https://cdn.jsdelivr.net/npm/web3@1.5.2/dist/web3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
let web3;
let connected = false;
async function initWallet() {
if (typeof window.ethereum !== 'undefined') {
web3 = new Web3(window.ethereum);
try {
// Enable the wallet and get accounts
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
if (accounts.length > 0) {
const walletAddress = accounts[0];
document.getElementById('walletAddress').textContent = walletAddress;
document.getElementById('walletInfo').style.display = 'block';
getBalance(walletAddress);
connected = true;
document.getElementById('connectWallet').style.display = 'none';
document.getElementById('disconnectWallet').style.display = 'block';
} else {
alert("No accounts found. Please unlock your wallet.");
}
} catch (error) {
console.error(error);
alert("Error connecting to the wallet. Please check your MetaMask.");
}
} else {
alert("MetaMask is not installed. Please install MetaMask.");
}
}
async function disconnectWallet() {
if (typeof window.ethereum !== 'undefined') {
try {
await ethereum.request({ method: 'eth_requestAccounts' });
connected = false;
document.getElementById('walletInfo').style.display = 'none';
document.getElementById('connectWallet').style.display = 'block';
document.getElementById('disconnectWallet').style.display = 'none';
document.getElementById('walletAddress').textContent = '';
document.getElementById('balance').textContent = '';
} catch (error) {
console.error(error);
}
}
}
window.addEventListener('load', () => {
initWallet();
});
document.getElementById('connectWallet').addEventListener('click', () => {
initWallet();
});
document.getElementById('disconnectWallet').addEventListener('click', () => {
disconnectWallet();
});
async function getBalance(walletAddress) {
const balance = await web3.eth.getBalance(walletAddress);
const balanceInEth = web3.utils.fromWei(balance, 'ether');
document.getElementById('balance').textContent = balanceInEth;
}
document.getElementById('send').addEventListener('click', async () => {
const recipient = document.getElementById('recipient').value;
const amount = document.getElementById('amount').value;
if (!connected) {
alert("Please connect your wallet first.");
return;
}
if (!web3.utils.isAddress(recipient)) {
alert("Invalid recipient address.");
return;
}
const weiAmount = web3.utils.toWei(amount, 'ether');
const walletAddress = document.getElementById('walletAddress').textContent;
const transactionObject = {
from: walletAddress,
to: recipient,
value: weiAmount,
};
await web3.eth.sendTransaction(transactionObject)
.on('transactionHash', (hash) => {
alert("Transaction sent! Transaction hash: " + hash);
})
.on('confirmation', (confirmationNumber, receipt) => {
alert("Transaction confirmed! Block number: " + receipt.blockNumber);
})
.on('error', (error) => {
console.error(error);
alert("Error sending transaction.");
});
});
</script>
</body>
</html>