-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
262 lines (200 loc) · 10.5 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ton Coin Wallet-Send Ton with Paper Wallet</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/tonweb/dist/tonweb.min.js"></script>
<style>
.hidden {
display: none;
}
.spinner-border {
width: 1rem;
height: 1rem;
border-width: 0.2em;
}
.spinner-hidden {
display: none;
}
</style>
</head>
<body>
<div class="container mt-5">
<h1 class="mb-4">Ton Coin Wallet</h1>
<a target="_blank" href="https://cryptodapprun.github.io/Cryptocurrency-Paper-Wallet-Generator/"> Generate Wallet </a><br><br>
<div class="mb-3">
<input type="password" id="privateKeyInput" class="form-control" placeholder="Enter Private Key (Hex)">
<div id="BalanceID"></div>
<div class="form-check form-switch mt-2">
<input class="form-check-input" type="checkbox" id="togglePrivateKey">
<label class="form-check-label" for="togglePrivateKey">Show Private Key</label>
</div>
</div>
<div class="mb-3">
<input type="text" id="recipientAddressInput" class="form-control" placeholder="Enter Recipient Address">
</div>
<div class="mb-3">
<input type="text" id="amountInput" class="form-control" placeholder="Enter Amount to Send">
</div>
<div class="mb-3">
<div class="form-check">
<input class="form-check-input" type="radio" name="transactionType" id="sendCoinsRadio" value="coins" checked>
<label class="form-check-label" for="sendCoinsRadio">
Send TON Coins
</label>
</div>
<!--
<div class="form-check">
<input class="form-check-input" type="radio" name="transactionType" id="sendTokensRadio" value="tokens">
<label class="form-check-label" for="sendTokensRadio">
Send TON Tokens
</label>
</div>
-->
</div>
<div class="mb-3 hidden" id="tokenAddressDiv">
<input type="text" id="tokenAddressInput" class="form-control" placeholder="Enter Token Address (for token transfer)">
</div>
<button id="sendButton" class="btn btn-primary">
Send
<span id="spinner" class="spinner-border spinner-hidden" role="status" aria-hidden="true"></span>
</button>
<p id="output" class="mt-3"></p>
</div>
<script>
document.getElementById('privateKeyInput').addEventListener('input', async () => {
const privateKeyInput = document.getElementById('privateKeyInput').value.trim();
if (!privateKeyInput) return;
document.getElementById('BalanceID').innerText=""
try {
const TonWeb = window.TonWeb;
const nacl = TonWeb.utils.nacl;
const privateKey = TonWeb.utils.hexToBytes(privateKeyInput);
const keyPair = nacl.sign.keyPair.fromSecretKey(privateKey);
const publicKey = keyPair.publicKey;
const tonweb = new TonWeb(new TonWeb.HttpProvider('https://toncenter.com/api/v2/jsonRPC'));
const wallet = tonweb.wallet.create({ publicKey, wc: 0 });
const walletAddress = await wallet.getAddress();
const addressString = walletAddress.toString(true, true, false);
const balance = await tonweb.provider.getBalance(addressString);
const balanceTon = TonWeb.utils.fromNano(balance);
console.log("balanceTon:",balanceTon)
document.getElementById('BalanceID').innerText = `Address: ${addressString}\nBalance: ${balanceTon} TON`;
} catch (error) {
document.getElementById('BalanceID').innerText = `Wrone HEX key`;
}
});
// Toggle the private key input field between text and password
document.getElementById('togglePrivateKey').addEventListener('change', function() {
const privateKeyInput = document.getElementById('privateKeyInput');
if (this.checked) {
privateKeyInput.type = 'text';
} else {
privateKeyInput.type = 'password';
}
});
// Show or hide token address input based on selected transaction type
document.querySelectorAll('input[name="transactionType"]').forEach((elem) => {
elem.addEventListener('change', function(event) {
const value = event.target.value;
if (value === 'tokens') {
document.getElementById('tokenAddressDiv').classList.remove('hidden');
} else {
document.getElementById('tokenAddressDiv').classList.add('hidden');
}
});
});
// Send transaction based on the selected transaction type
document.getElementById('sendButton').addEventListener('click', async () => {
const privateKeyHex = document.getElementById('privateKeyInput').value.trim();
const recipientAddress = document.getElementById('recipientAddressInput').value.trim();
const amount = document.getElementById('amountInput').value.trim();
const transactionType = document.querySelector('input[name="transactionType"]:checked').value;
if (!privateKeyHex || !recipientAddress || !amount) {
document.getElementById('output').innerText = 'Please fill in all fields.';
return;
}
document.getElementById('spinner').classList.remove('spinner-hidden');
document.getElementById('sendButton').disabled = true;
try {
if (transactionType === 'coins') {
await sendTonCoins(privateKeyHex, recipientAddress, amount);
} else {
const tokenAddress = document.getElementById('tokenAddressInput').value.trim();
if (!tokenAddress) {
document.getElementById('output').innerText = 'Please fill in all fields.';
return;
}
await sendTonTokens(privateKeyHex, recipientAddress, amount, tokenAddress);
document.getElementById('output').innerText = 'Token transfer successful!';
}
} catch (error) {
document.getElementById('output').innerText = `Transaction failed: ${error.message}`;
} finally {
document.getElementById('spinner').classList.add('spinner-hidden');
document.getElementById('sendButton').disabled = false;
}
});
async function sendTonCoins(privateKeyHex, recipientAddress, amount) {
const TonWeb = window.TonWeb;
const nacl = TonWeb.utils.nacl;
// Convert the private key from hex to Uint8Array
const privateKey = TonWeb.utils.hexToBytes(privateKeyHex);
const keyPair = nacl.sign.keyPair.fromSecretKey(privateKey);
const publicKey = keyPair.publicKey;
if (!TonWeb.utils.Address.isValid(recipientAddress)) {
console.log("address is not valid")
return
}
var Address = new TonWeb.utils.Address(recipientAddress);
// toString arguments: isUserFriendly, isUrlSafe, isBounceable, isTestOnly
console.log(Address.toString(true, true, true, false)); // EQDKbjIcfM6ezt8KjKJJLshZJJSqX7XOA4ff-W72r5gqPrHF
console.log(Address.toString(true, false, true, false)); // EQDKbjIcfM6ezt8KjKJJLshZJJSqX7XOA4ff+W72r5gqPrHF
console.log(Address.toString(true, true, false, false)); // UQDKbjIcfM6ezt8KjKJJLshZJJSqX7XOA4ff-W72r5gqPuwA
console.log(Address.toString(true, true, true, true)); // kQDKbjIcfM6ezt8KjKJJLshZJJSqX7XOA4ff-W72r5gqPgpP
console.log(Address.toString(true, true, false, true)); // 0QDKbjIcfM6ezt8KjKJJLshZJJSqX7XOA4ff-W72r5gqPleK
var RecipientFormatAddress=Address.toString(true, true, false, false)
console.log("RecipientFormatAddress:",RecipientFormatAddress)
const tonweb = new TonWeb(new TonWeb.HttpProvider('https://toncenter.com/api/v2/jsonRPC'));
const wallet = tonweb.wallet.create({ publicKey, wc: 0 });
let seqno = await wallet.methods.seqno().call();
seqno = Number(seqno) || 0; // Convert seqno to number or set to 0 if not valid
console.log("seqno:",seqno)
await wallet.methods.transfer({
secretKey: privateKey,
toAddress: RecipientFormatAddress,
amount: TonWeb.utils.toNano(amount),
seqno: seqno,
payload: "Hello", // Optional message
sendMode: 3
}).send();
document.getElementById('output').innerText = 'Transaction successful!';
}
async function sendTonTokens(privateKeyHex, recipientAddress, amount, tokenAddress) {
const TonWeb = window.TonWeb;
const nacl = TonWeb.utils.nacl;
// Convert the private key from hex to Uint8Array
const privateKey = TonWeb.utils.hexToBytes(privateKeyHex);
const keyPair = nacl.sign.keyPair.fromSecretKey(privateKey);
const publicKey = keyPair.publicKey;
const tonweb = new TonWeb(new TonWeb.HttpProvider('https://toncenter.com/api/v2/jsonRPC'));
// Token-specific parameters
const amountNano = TonWeb.utils.toNano(amount);
const WalletClass = tonweb.token.Wallet;
const wallet = new WalletClass(tonweb.provider, { publicKey, address: tokenAddress });
let seqno = await wallet.methods.seqno().call();
seqno = Number(seqno) || 0; // Convert seqno to number or set to 0 if not valid
const transfer = wallet.methods.transfer({
secretKey: privateKey,
toAddress: recipientAddress,
amount: amountNano,
seqno: seqno,
sendMode: 3
});
await transfer.send();
}
</script>
</body>
</html>