-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrowserRPCReplacement.js
856 lines (732 loc) · 23.9 KB
/
BrowserRPCReplacement.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
// rpc_functions_browser_client.js
class BrowserRPCReplacement {
constructor(apiBaseUrl = "https://opennode-fastapi.pastel.network") {
this.apiBaseUrl = apiBaseUrl;
this.pastelInstance = null;
this.isInitialized = false;
}
async initialize() {
if (!this.isInitialized) {
if (
typeof Module === "undefined" ||
typeof Module.Pastel === "undefined"
) {
throw new Error("WASM module not loaded");
}
this.pastelInstance = new Module.Pastel();
this.isInitialized = true;
}
}
async ensureInitialized() {
if (!this.isInitialized) {
await this.initialize();
}
}
// Helper methods
getNetworkMode(mode) {
const modeMap = {
Mainnet: Module.NetworkMode.Mainnet,
Testnet: Module.NetworkMode.Testnet,
Devnet: Module.NetworkMode.Devnet,
};
return modeMap[mode] || Module.NetworkMode.Mainnet;
}
executeWasmMethod(method) {
try {
const result = method();
return this.parseWasmResponse(result);
} catch (error) {
console.error("WASM method execution failed:", error);
throw new Error("WASM method execution failed: " + error.message);
}
}
parseWasmResponse(response) {
try {
const parsedResponse = JSON.parse(response);
if (parsedResponse.result) {
return parsedResponse.data;
} else {
throw new Error(
parsedResponse.error || "Unknown error in WASM response"
);
}
} catch (error) {
console.error("Error parsing WASM response:", error);
throw new Error("Error parsing WASM response: " + error.message);
}
}
async fetchJson(endpoint) {
try {
const response = await fetch(this.apiBaseUrl + endpoint);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error(`Error fetching from ${endpoint}:`, error);
throw error;
}
}
// RPC replacement functions
// Removed getLocalRPCSettings as it's not applicable in a browser context
async checkMasternodeTop() {
return this.fetchJson("/masternode/top");
}
async getCurrentPastelBlockHeight() {
return this.fetchJson("/getblockcount");
}
async getBestBlockHashAndMerkleRoot() {
const blockHeight = await this.getCurrentPastelBlockHeight();
const blockHash = await this.getBlockHash(blockHeight);
const block = await this.getBlock(blockHash);
return [blockHash, block.merkleroot, blockHeight];
}
async verifyMessageWithPastelID(
pastelid,
messageToVerify,
pastelIDSignatureOnMessage
) {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.VerifyWithPastelID(
pastelid,
messageToVerify,
pastelIDSignatureOnMessage,
"Mainnet"
)
);
}
async sendToAddress(address, amount, comment = "") {
await this.ensureInitialized();
const sendTo = [{ address, amount }];
const fromAddress = await this.getMyPslAddressWithLargestBalance();
return this.createSendToTransaction(sendTo, fromAddress);
}
async sendMany(amounts, minConf = 1, comment = "", changeAddress = "") {
await this.ensureInitialized();
const fromAddress = await this.getMyPslAddressWithLargestBalance();
return this.createSendToTransaction(amounts, fromAddress);
}
async checkPSLAddressBalance(addressToCheck) {
return this.fetchJson(`/get_address_balance?addresses=${addressToCheck}`);
}
async checkIfAddressIsAlreadyImportedInLocalWallet(addressToCheck) {
await this.ensureInitialized();
const addresses = await this.getAllAddresses();
return addresses.includes(addressToCheck);
}
async getAndDecodeRawTransaction(txid, blockhash = null) {
const rawTx = await this.fetchJson(`/getrawtransaction/${txid}`);
return this.fetchJson(`/decoderawtransaction/${rawTx}`);
}
async getTransactionDetails(txid, includeWatchonly = false) {
return this.fetchJson(
`/gettransaction/${txid}?includeWatchonly=${includeWatchonly}`
);
}
async sendTrackingAmountFromControlAddressToBurnAddressToConfirmInferenceRequest(
inferenceRequestId,
creditUsageTrackingPSLAddress,
creditUsageTrackingAmountInPSL,
burnAddress
) {
const sendTo = [
{ address: burnAddress, amount: creditUsageTrackingAmountInPSL },
];
const txid = await this.createSendToTransaction(
sendTo,
creditUsageTrackingPSLAddress
);
return txid;
}
async importAddress(address, label = "", rescan = false) {
await this.ensureInitialized();
// In a browser context, we can't directly import addresses.
// Instead, we'll store the address in localStorage for tracking.
const importedAddresses = JSON.parse(
localStorage.getItem("importedAddresses") || "[]"
);
if (!importedAddresses.includes(address)) {
importedAddresses.push(address);
localStorage.setItem(
"importedAddresses",
JSON.stringify(importedAddresses)
);
}
console.log(`Address ${address} has been tracked for monitoring.`);
}
async getBlockHash(blockHeight) {
return this.fetchJson(`/getblockhash/${blockHeight}`);
}
async getBlock(blockHash) {
return this.fetchJson(`/getblock/${blockHash}`);
}
async signMessageWithPastelID(pastelid, messageToSign) {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.SignWithPastelID(
pastelid,
messageToSign,
"PastelID",
"Mainnet"
)
);
}
async createAndFundNewPSLCreditTrackingAddress(amountOfPSLToFundAddressWith) {
await this.ensureInitialized();
const newAddress = await this.makeNewAddress();
const txid = await this.sendToAddress(
newAddress,
amountOfPSLToFundAddressWith,
"Funding new credit tracking address"
);
return { newCreditTrackingAddress: newAddress, txid };
}
async checkSupernodeList() {
return this.fetchJson("/supernode_data");
}
async createAndRegisterNewPastelID(passphraseForNewPastelID) {
await this.ensureInitialized();
const pastelID = await this.makeNewPastelID(true);
const fundingAddress = await this.getMyPslAddressWithLargestBalance();
const txid = await this.createRegisterPastelIdTransaction(
pastelID,
fundingAddress
);
return {
success: true,
PastelID: pastelID,
PastelIDRegistrationTXID: txid,
};
}
async getBalance(account = "*", minConf = 1, includeWatchOnly = false) {
await this.ensureInitialized();
const addresses = await this.getAllAddresses();
let totalBalance = 0;
for (const address of addresses) {
const balance = await this.checkPSLAddressBalance(address);
totalBalance += balance;
}
return totalBalance;
}
async getWalletInfo() {
await this.ensureInitialized();
const balance = await this.getBalance();
const unconfirmedBalance = 0; // We might need to calculate this separately
const immatureBalance = 0; // We might need to calculate this separately
return {
walletversion: 1,
balance,
unconfirmed_balance: unconfirmedBalance,
immature_balance: immatureBalance,
txcount: await this.getWalletTransactionCount(),
keypoololdest: 0, // Not applicable in this context
keypoolsize: 0, // Not applicable in this context
paytxfee: 0.001, // This should be configurable
seedfp: "Not available", // Not applicable in this context
};
}
async getNewAddress() {
await this.ensureInitialized();
return this.makeNewAddress();
}
async getMyPslAddressWithLargestBalance() {
await this.ensureInitialized();
const addresses = await this.getAllAddresses();
let maxBalance = -1;
let addressWithMaxBalance = null;
for (const address of addresses) {
const balance = await this.checkPSLAddressBalance(address);
if (balance > maxBalance) {
maxBalance = balance;
addressWithMaxBalance = address;
}
}
return addressWithMaxBalance;
}
// Additional helper methods
async getAllAddresses(mode = "Mainnet") {
await this.ensureInitialized();
const addressCount = await this.getAddressesCount();
const addresses = [];
for (let i = 0; i < addressCount; i++) {
addresses.push(await this.getAddress(i, mode));
}
return addresses;
}
async getWalletTransactionCount() {
// In a browser context, we need to keep track of transactions ourselves
const transactions = JSON.parse(
localStorage.getItem("transactions") || "[]"
);
return transactions.length;
}
// WASM wallet methods
async createNewWallet(password) {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.CreateNewWallet(password)
);
}
async importWallet(serializedWallet) {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.ImportWallet(serializedWallet)
);
}
async exportWallet() {
await this.ensureInitialized();
return this.executeWasmMethod(() => this.pastelInstance.ExportWallet());
}
async makeNewAddress(mode = "Mainnet") {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.MakeNewAddress(this.getNetworkMode(mode))
);
}
async getAddress(index, mode = "Mainnet") {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.GetAddress(index, this.getNetworkMode(mode))
);
}
async getAddressesCount() {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.GetAddressesCount()
);
}
async makeNewPastelID(makeFullPair = false) {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.MakeNewPastelID(makeFullPair)
);
}
async getPastelIDByIndex(index, type = "PastelID") {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.GetPastelIDByIndex(index, type)
);
}
async getPastelIDsCount() {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.GetPastelIDsCount()
);
}
async createSendToTransaction(sendTo, fromAddress, mode = "Mainnet") {
await this.ensureInitialized();
const utxos = await this.getAddressUtxos(fromAddress);
const blockHeight = await this.getCurrentPastelBlockHeight();
const networkMode = this.getNetworkMode(mode);
const sendToJson = JSON.stringify(sendTo);
const utxosJson = JSON.stringify(utxos);
return this.executeWasmMethod(() =>
this.pastelInstance.CreateSendToTransaction(
networkMode,
sendToJson,
fromAddress,
utxosJson,
blockHeight,
0
)
);
}
async createRegisterPastelIdTransaction(
pastelID,
fundingAddress,
mode = "Mainnet"
) {
await this.ensureInitialized();
const utxos = await this.getAddressUtxos(fundingAddress);
const blockHeight = await this.getCurrentPastelBlockHeight();
const networkMode = this.getNetworkMode(mode);
const utxosJson = JSON.stringify(utxos);
return this.executeWasmMethod(() =>
this.pastelInstance.CreateRegisterPastelIdTransaction(
networkMode,
pastelID,
fundingAddress,
utxosJson,
blockHeight,
0
)
);
}
async signWithWalletKey(message) {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.SignWithWalletKey(message)
);
}
async unlockWallet(password) {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.UnlockWallet(password)
);
}
async lockWallet() {
await this.ensureInitialized();
return this.executeWasmMethod(() => this.pastelInstance.LockWallet());
}
async getWalletPubKey() {
await this.ensureInitialized();
return this.executeWasmMethod(() => this.pastelInstance.GetWalletPubKey());
}
// Additional API methods
async getAddressUtxos(address) {
return this.fetchJson(`/get_address_utxos?addresses=${address}`);
}
async listPastelIDTickets(filter = "mine", minheight = null) {
let endpoint = `/tickets/id/list/${filter}`;
if (minheight !== null) {
endpoint += `/${minheight}`;
}
return this.fetchJson(endpoint);
}
async findPastelIDTicket(key) {
return this.fetchJson(`/tickets/id/find/${key}`);
}
async getPastelTicket(txid, decodeProperties = true) {
return this.fetchJson(
`/tickets/get/${txid}?decode_properties=${decodeProperties}`
);
}
async listContractTickets(ticketTypeIdentifier, startingBlockHeight = 0) {
return this.fetchJson(
`/tickets/contract/list/${ticketTypeIdentifier}/${startingBlockHeight}`
);
}
async findContractTicket(key) {
return this.fetchJson(`/tickets/contract/find/${key}`);
}
async getContractTicket(txid, decodeProperties = true) {
return this.fetchJson(
`/tickets/contract/get/${txid}?decode_properties=${decodeProperties}`
);
}
async isPastelIDRegistered(pastelID) {
return this.fetchJson(`/tickets/id/is_registered/${pastelID}`);
}
async dumpPrivKey(tAddr) {
await this.ensureInitialized();
// This is a sensitive operation that should be handled carefully in a browser environment
console.warn(
"dumpPrivKey called in browser context. This operation may expose sensitive information."
);
return this.executeWasmMethod(() => this.pastelInstance.DumpPrivKey(tAddr));
}
async importPrivKey(privKey, label = "", rescan = true) {
await this.ensureInitialized();
// This is a sensitive operation that should be handled carefully in a browser environment
console.warn(
"importPrivKey called in browser context. This operation may expose sensitive information."
);
return this.executeWasmMethod(() =>
this.pastelInstance.ImportPrivKey(privKey, label, rescan)
);
}
async listAddressAmounts(includeEmpty = false, isMineFilter = "all") {
await this.ensureInitialized();
const addresses = await this.getAllAddresses();
const result = {};
for (const address of addresses) {
const balance = await this.checkPSLAddressBalance(address);
if (includeEmpty || balance > 0) {
result[address] = balance;
}
}
return result;
}
async checkForRegisteredPastelID() {
await this.ensureInitialized();
const pastelIDs = await this.getAllPastelIDs();
for (const pastelID of pastelIDs) {
const isRegistered = await this.isPastelIDRegistered(pastelID);
if (isRegistered) {
return pastelID;
}
}
return null;
}
async getAllPastelIDs() {
await this.ensureInitialized();
const count = await this.getPastelIDsCount();
const pastelIDs = [];
for (let i = 0; i < count; i++) {
pastelIDs.push(await this.getPastelIDByIndex(i));
}
return pastelIDs;
}
getNetworkInfo(rpcport) {
let network = "";
let burnAddress = "";
if (rpcport === "9932") {
network = "mainnet";
burnAddress = "PtpasteLBurnAddressXXXXXXXXXXbJ5ndd";
} else if (rpcport === "19932") {
network = "testnet";
burnAddress = "tPpasteLBurnAddressXXXXXXXXXXX3wy7u";
} else if (rpcport === "29932") {
network = "devnet";
burnAddress = "44oUgmZSL997veFEQDq569wv5tsT6KXf9QY7";
} else {
throw new Error(`Unknown RPC port: ${rpcport}`);
}
return { network, burnAddress };
}
// This function doesn't make sense in a browser context, but we'll keep a placeholder
getPastelIDDirectory(network) {
console.warn(
"getPastelIDDirectory called in browser context. This operation is not applicable."
);
return null;
}
// This function doesn't make sense in a browser context, but we'll keep a placeholder
async getPastelIDsFromDirectory(directory) {
console.warn(
"getPastelIDsFromDirectory called in browser context. This operation is not applicable."
);
return [];
}
// This function doesn't make sense in a browser context, but we'll keep a placeholder
async promptUserConfirmation(message) {
console.warn(
"promptUserConfirmation called in browser context. This operation should be handled by the UI."
);
return false;
}
async createAndRegisterPastelID(burnAddress) {
await this.ensureInitialized();
const pastelID = await this.makeNewPastelID(true);
const fundingAddress = await this.getMyPslAddressWithLargestBalance();
const txid = await this.createRegisterPastelIdTransaction(
pastelID,
fundingAddress
);
return { pastelID, txid };
}
async isCreditPackConfirmed(txid) {
const ticket = await this.getPastelTicket(txid);
return ticket && ticket.height > 0;
}
async ensureTrackingAddressesHaveMinimalPSLBalance(addressesList = null) {
await this.ensureInitialized();
const addresses = addressesList || (await this.getAllAddresses());
const fundingAddress = await this.getMyPslAddressWithLargestBalance();
for (const address of addresses) {
const balance = await this.checkPSLAddressBalance(address);
if (balance < 1.0) {
const amountNeeded = Math.round((1.0 - balance) * 10000) / 10000;
if (amountNeeded > 0.0001) {
await this.sendToAddress(
address,
amountNeeded,
"Balancing PSL amount"
);
}
}
}
}
// Utility functions
formatNumberWithCommas(number) {
return new Intl.NumberFormat("en-US").format(number);
}
// Additional methods that might be needed
async getAddressHistory(address) {
return this.fetchJson(`/get_address_history/${address}`);
}
async getBestBlockHash() {
return this.fetchJson("/getbestblockhash");
}
async getMempoolInfo() {
return this.fetchJson("/getmempoolinfo");
}
async getRawMempool() {
return this.fetchJson("/getrawmempool");
}
async estimateFee(nblocks) {
return this.fetchJson(`/estimatefee/${nblocks}`);
}
async validateAddress(address) {
return this.fetchJson(`/validateaddress/${address}`);
}
async stopPastelDaemon() {
console.warn("stopPastelDaemon is not applicable in a browser environment");
throw new Error("Operation not supported in browser");
}
async startPastelDaemon() {
console.warn(
"startPastelDaemon is not applicable in a browser environment"
);
throw new Error("Operation not supported in browser");
}
async waitForRPCConnection(maxRetries = 5, interval = 1000) {
// In a browser context, we assume the connection is always available
return true;
}
async initializeRPCConnection() {
// In a browser context, this is handled by the class constructor
await this.initialize();
}
async getBlockchainInfo() {
return this.fetchJson("/getblockchaininfo");
}
async getTxOutSetInfo() {
return this.fetchJson("/gettxoutsetinfo");
}
async getChainTips() {
return this.fetchJson("/getchaintips");
}
async getDifficulty() {
return this.fetchJson("/getdifficulty");
}
async getBlockHeader(blockhash) {
return this.fetchJson(`/getblockheader/${blockhash}`);
}
async getTxOut(txid, vout_value, includemempool = true) {
return this.fetchJson(
`/gettxout/${txid}/${vout_value}?includemempool=${includemempool}`
);
}
async getTxOutProof(txid) {
return this.fetchJson(`/gettxoutproof/${txid}`);
}
async verifyTxOutProof(proof) {
return this.fetchJson(`/verifytxoutproof/${proof}`);
}
async getInfo() {
return this.fetchJson("/getinfo");
}
async getMemoryInfo() {
return this.fetchJson("/getmemoryinfo");
}
async getBlockSubsidy(height) {
return this.fetchJson(`/getblocksubsidy/${height}`);
}
async getBlockTemplate() {
return this.fetchJson("/getblocktemplate");
}
async getMiningInfo() {
return this.fetchJson("/getmininginfo");
}
async getNextBlockSubsidy() {
return this.fetchJson("/getnextblocksubsidy");
}
async getNetworkSolPs(blocks, height) {
return this.fetchJson(`/getnetworksolps/${blocks}/${height}`);
}
async getAddedNodeInfo() {
return this.fetchJson("/getaddednodeinfo");
}
async getPeerInfo() {
return this.fetchJson("/getpeerinfo");
}
async decodeRawTransaction(hexstring) {
return this.fetchJson(`/decoderawtransaction/${hexstring}`);
}
async decodeScript(hexstring) {
return this.fetchJson(`/decodescript/${hexstring}`);
}
async validateAddress(transparentAddress) {
return this.fetchJson(`/validateaddress/${transparentAddress}`);
}
async zValidateAddress(shieldedAddress) {
return this.fetchJson(`/z_validateaddress/${shieldedAddress}`);
}
async listPastelIDTicketsOld(filter = "mine", minheight = null) {
let endpoint = `/list_pastelid_tickets/${filter}`;
if (minheight !== null) {
endpoint += `/${minheight}`;
}
return this.fetchJson(endpoint);
}
async findPastelIDTicket(key) {
return this.fetchJson(`/find_pastelid_ticket/${key}`);
}
async registerPastelID(pastelid, passphrase, address) {
await this.ensureInitialized();
// This operation involves signing, so we'll use the WASM library
return this.executeWasmMethod(() =>
this.pastelInstance.RegisterPastelID(pastelid, passphrase, address)
);
}
async checkPSLAddressBalanceAlternative(addressToCheck) {
const addressAmounts = await this.listAddressAmounts();
return addressAmounts[addressToCheck] || 0;
}
async createNewWallet(password) {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.CreateNewWallet(password)
);
}
async createWalletFromMnemonic(password, mnemonic) {
await this.ensureInitialized();
return this.executeWasmMethod(() =>
this.pastelInstance.CreateWalletFromMnemonic(password, mnemonic)
);
}
async loadWallet(serializedWallet, password) {
await this.ensureInitialized();
await this.importWallet(serializedWallet);
if (password) {
await this.unlockWallet(password);
}
return true;
}
async downloadWallet(filename = "pastel_wallet.dat") {
await this.ensureInitialized();
const content = await this.exportWallet();
const blob = new Blob([content], { type: "text/plain;charset=utf-8" });
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
return true;
}
// Helper method to simulate file selection in a browser environment
async selectAndReadWalletFile() {
return new Promise((resolve) => {
const input = document.createElement("input");
input.type = "file";
input.onchange = (e) => {
const file = e.target.files[0];
const reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = (readerEvent) => {
const content = readerEvent.target.result;
resolve(content);
};
};
input.click();
});
}
// Methods related to IndexedDB operations
// Note: These methods would require a separate IndexedDBManager class implementation
async saveWalletToIndexedDB(id, serializedWallet) {
console.warn(
"saveWalletToIndexedDB is not implemented in this class. Consider using a separate IndexedDB manager."
);
}
async loadWalletFromIndexedDB(id) {
console.warn(
"loadWalletFromIndexedDB is not implemented in this class. Consider using a separate IndexedDB manager."
);
}
async deleteWalletFromIndexedDB(id) {
console.warn(
"deleteWalletFromIndexedDB is not implemented in this class. Consider using a separate IndexedDB manager."
);
}
async replaceWalletInIndexedDB(id) {
console.warn(
"replaceWalletInIndexedDB is not implemented in this class. Consider using a separate IndexedDB manager."
);
}
}
export default BrowserRPCReplacement;