-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
337 lines (298 loc) · 13.8 KB
/
test.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
<script type="text/javascript" src="libpastel_wasm.js"></script>
<style>
.waiting {
cursor: wait;
}
</style>
<script>
let pastelInstance;
let indNewAddresses = 0;
let indNewPastelID = 0;
function setCursorToWait() {
document.body.classList.add('waiting');
}
function setCursorToDefault() {
document.body.classList.remove('waiting');
}
Module.onRuntimeInitialized = function() {
pastelInstance = new Module.Pastel();
console.log(pastelInstance);
document.getElementById("makeNewWallet").addEventListener("click", function() {
setCursorToWait();
let password = prompt("Please enter password", "");
if (!password) {
alert("Password is required!");
setCursorToDefault();
return;
}
let filename = prompt("Please enter the file name", "wallet.dat");
if (!filename) {
alert("File name is required!");
setCursorToDefault();
return;
}
let mnemonic = pastelInstance.CreateNewWallet(password);
document.getElementById("mnemonic").innerText = mnemonic;
setCursorToDefault();
});
document.getElementById('exportWalletButton').addEventListener('click', () => {
setCursorToWait();
let filename = prompt("Please enter the file name", "wallet.dat");
if (!filename) {
alert("File name is required!");
setCursorToDefault();
return;
}
let content = pastelInstance.ExportWallet();
let blob = new Blob([content], {type: "text/plain;charset=utf-8"});
let url = window.URL.createObjectURL(blob);
let fileLink = document.createElement('a');
fileLink.href = url;
fileLink.download = filename;
fileLink.click(); // start download
setCursorToDefault();
});
document.getElementById('importWalletButton').addEventListener('click', () => {
setCursorToWait();
let fileSelector = document.createElement('input');
fileSelector.setAttribute('type', 'file');
fileSelector.click();
fileSelector.onchange = (e) => {
let file = e.target.files[0];
let reader = new FileReader();
reader.onload = () => {
let content = reader.result; // encode the content to base64
let password = window.prompt("Please enter your password");
if (password) {
pastelInstance.ImportWallet(content);
pastelInstance.UnlockWallet(password);
} else {
alert("Password is required!");
}
setCursorToDefault();
};
reader.readAsText(file,'UTF-8');
};
});
function getMode() {
let selectedMode = document.getElementById("networkMode").value;
console.log(selectedMode);
console.log(Module.NetworkMode);
let mode = Module.NetworkMode[selectedMode];
console.log(mode);
return mode;
}
document.getElementById("getAddress").addEventListener("click", function() {
setCursorToWait();
let mode = getMode();
let address = pastelInstance.MakeNewAddress(mode);
let addressElement = document.createElement("div");
addressElement.innerText = `Address ${indNewAddresses}: ${address}`;
document.getElementById("addressList").appendChild(addressElement);
// Update select options
let selectElement = document.getElementById("addressIndexList");
let optionElement = document.createElement("option");
optionElement.text = indNewAddresses;
optionElement.value = indNewAddresses;
selectElement.appendChild(optionElement);
indNewAddresses++;
setCursorToDefault();
});
document.getElementById("getAddressWInd").addEventListener("click", function() {
setCursorToWait();
let mode = getMode();
let index = parseInt(document.getElementById("addressIndexList").value);
try {
let address = pastelInstance.GetAddress(index, mode);
let addressElement = document.createElement("div");
addressElement.innerText = `Address ${index}: ${address}`;
document.getElementById("addressListWInd").appendChild(addressElement);
} catch (e) {
alert(`No more addresses - ${index}`);
}
setCursorToDefault();
});
document.getElementById("getPastelID").addEventListener("click", function() {
setCursorToWait();
let paslteID = pastelInstance.MakeNewPastelID(true);
let paslteIDElement = document.createElement("div");
paslteIDElement.innerText = `Address ${indNewPastelID}: ${paslteID}`;
document.getElementById("pastelidList").appendChild(paslteIDElement);
// Update selects options
let selectElement = document.getElementById("pastelIDIndexList");
let optionElement = document.createElement("option");
optionElement.text = indNewPastelID;
optionElement.value = indNewPastelID;
selectElement.appendChild(optionElement);
indNewPastelID++;
setCursorToDefault();
});
document.getElementById("getPastelIDWInd").addEventListener("click", function() {
setCursorToWait();
let index = parseInt(document.getElementById("pastelIDIndexList").value);
let selectedType = document.getElementById("pastelIDType").value;
try {
console.log(index, selectedType)
console.log(Module.PastelIDType);
let type = Module.PastelIDType[selectedType];
let pastelID = pastelInstance.GetPastelIDByIndex(index, type);
let pastelIDElement = document.createElement("div");
pastelIDElement.innerText = `PastelID ${index}: ${pastelID}`;
document.getElementById("pastelIDListWInd").appendChild(pastelIDElement);
} catch (e) {
alert(`No more pastelIDs - ${index}`);
}
setCursorToDefault();
});
let dirPath = '/wallet_data'; // This should match the directory used in your C++ code
let pastelID, passPhrase;
document.getElementById('importPastelIDKeysButtonFS').addEventListener('click', () => {
setCursorToWait();
// Create a file input element dynamically
let fileSelector = document.createElement('input');
fileSelector.setAttribute('type', 'file');
fileSelector.click();
fileSelector.onchange = (e) => {
let file = e.target.files[0];
let reader = new FileReader();
reader.onload = () => {
let content = reader.result; // This is an ArrayBuffer of the file content
// Use the file name from the selected file as the PastelID (filename in the FS)
pastelID = file.name;
if (!pastelID) {
alert("File name is required!");
setCursorToDefault();
return;
}
// Prompt the user for the passphrase
passPhrase = prompt("Please enter the passphrase", "");
if (!passPhrase) {
alert("Passphrase is required!");
setCursorToDefault();
return;
}
// Ensure the directory exists in the Emscripten FS
try {
Module.FS.mkdir(dirPath);
} catch (e) {
if (e.code !== 'EEXIST') throw e;
}
// The full file path in the Emscripten FS
let filePath = dirPath + '/' + pastelID;
// Convert the ArrayBuffer to a Uint8Array
let data = new Uint8Array(content);
// Write the file content to the Emscripten FS
Module.FS.writeFile(filePath, data);
setCursorToDefault();
Module.FS.syncfs(false, function(err) {
if (err) {
console.error('Error syncing file system:', err);
} else {
console.log('File system synced successfully.');
}
setCursorToDefault();
});
};
reader.readAsArrayBuffer(file);
};
});
document.getElementById('importPastelIDKeysButtonWallet').addEventListener('click', () => {
setCursorToWait();
try {
let result = pastelInstance.ImportPastelIDKeys(pastelID, passPhrase, dirPath);
console.log('ImportPastelIDKeys result:', result);
} catch (e) {
console.error('Error calling ImportPastelIDKeys:', e);
alert(`Error calling ImportPastelIDKeys: ${e}`);
}
setCursorToDefault();
});
document.getElementById('getPastelIDFromWallet').addEventListener('click', () => {
setCursorToWait();
try {
let pastel_id = pastelInstance.GetPastelID(pastelID, Module.PastelIDType["PastelID"]);
let legroast = pastelInstance.GetPastelID(pastelID, Module.PastelIDType["LegRoast"]);
console.log('Imported PastelID:', pastel_id);
console.log('Imported LegRoast:', legroast);
} catch (e) {
console.error('Error calling GetPastelID:', e);
alert(`Error calling GetPastelID: ${e}`);
}
setCursorToDefault();
});
let signature;
document.getElementById('signWithPastelID').addEventListener('click', () => {
setCursorToWait();
try {
let result = pastelInstance.SignWithPastelID(pastelID, "test", Module.PastelIDType["PastelID"], true)
const parsedData = JSON.parse(result);
signature = parsedData.data;
console.log('Signature:', signature);
} catch (e) {
console.error('Error calling SignWithPastelID:', e);
alert(`Error calling SignWithPastelID: ${e}`);
}
setCursorToDefault();
});
document.getElementById('verifyWithPastelID').addEventListener('click', () => {
setCursorToWait();
try {
result = pastelInstance.VerifyWithPastelID(pastelID, "test", signature, true)
console.log('Verify result:', result);
} catch (e) {
console.error('Error calling VerifyWithPastelID:', e);
alert(`Error calling VerifyWithPastelID: ${e}`);
}
setCursorToDefault();
});
};
</script>
</head>
<body>
<button id="makeNewWallet">Make New Wallet</button>
<button id="exportWalletButton">Export Wallet</button>
<button id="importWalletButton">Import Wallet</button>
<div id="mnemonic"></div>
<hr/>
<select id="networkMode">
<option value="Mainnet">Mainnet</option>
<option value="Testnet">Testnet</option>
<option value="Devnet">Devnet</option>
</select>
<br/>
<br/>
<button id="getAddress">Make New Address</button>
<div id="addressList"></div>
<br/>
<select id="addressIndexList"></select>
<button id="getAddressWInd">Get Existing Address by index</button>
<div id="addressListWInd"></div><br/>
<hr/>
<hr/>
<button id="getPastelID">Make New PastelID</button>
<div id="pastelidList"></div>
<br/>
<select id="pastelIDIndexList"></select>
<select id="pastelIDType">
<option value="PastelID">PastelID</option>
<option value="LegRoast">LegRoast</option>
</select>
<button id="getPastelIDWInd">Get Existing PastelID by index</button>
<div id="pastelIDListWInd"></div><br/>
<hr/>
<hr/>
<button id="importPastelIDKeysButtonFS">Import PastelID file into FS</button>
<button id="importPastelIDKeysButtonWallet">Import PastelID file into Wallet</button>
<br/>
<button id="getPastelIDFromWallet">Get exported PastelID from Wallet</button><br/>
<button id="signWithPastelID">Sign with exported PastelID</button>
<button id="verifyWithPastelID">Verify with exported PastelID</button>
<hr/>
<hr/>
</body>
</html>