gun-eth 1.4.31 → 1.4.33
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/browser/gun-eth.min.js +13 -0
- package/dist/browser/gun-eth.min.js.map +1 -0
- package/dist/gun-eth.cjs.js +164 -126
- package/dist/gun-eth.mjs +158 -121
- package/package.json +9 -8
- package/src/index.js +173 -135
- package/dist/gun-eth.cjs.js.map +0 -1
- package/dist/gun-eth.esm.js +0 -1369
- package/dist/gun-eth.esm.js.map +0 -1
- package/dist/gun-eth.min.js +0 -2
- package/dist/gun-eth.min.js.map +0 -1
- package/dist/gun-eth.mjs.map +0 -1
package/src/index.js
CHANGED
@@ -1,60 +1,71 @@
|
|
1
|
-
// =============================================
|
2
|
-
// IMPORTS AND GLOBAL VARIABLES
|
3
|
-
// =============================================
|
4
1
|
import Gun from "gun";
|
5
|
-
import SEA from "gun/sea.js";
|
6
2
|
import { ethers } from "ethers";
|
7
|
-
import {
|
8
|
-
PROOF_OF_INTEGRITY_ABI,
|
9
|
-
STEALTH_ANNOUNCER_ABI,
|
10
|
-
getAddressesForChain,
|
3
|
+
import {
|
4
|
+
PROOF_OF_INTEGRITY_ABI,
|
5
|
+
STEALTH_ANNOUNCER_ABI,
|
6
|
+
getAddressesForChain,
|
11
7
|
isLocalEnvironment,
|
12
|
-
CHAIN_CONFIG
|
8
|
+
CHAIN_CONFIG,
|
13
9
|
} from "./abis/abis.js";
|
14
10
|
import { LOCAL_CONFIG } from "./config/local.js";
|
15
|
-
import { fileURLToPath } from 'url';
|
16
|
-
import { dirname } from 'path';
|
17
|
-
import { readFileSync } from 'fs';
|
18
|
-
import {
|
19
|
-
JsonRpcProvider,
|
20
|
-
BrowserProvider,
|
21
|
-
Wallet,
|
22
|
-
Contract,
|
23
|
-
concat,
|
24
|
-
keccak256,
|
25
|
-
verifyMessage,
|
26
|
-
getBytes
|
27
|
-
} from 'ethers';
|
28
|
-
|
29
|
-
let PROOF_CONTRACT_ADDRESS;
|
30
|
-
let rpcUrl = "";
|
31
|
-
let privateKey = "";
|
32
11
|
|
33
|
-
|
12
|
+
import 'gun/sea.js'; // Questo modifica Gun globalmente
|
13
|
+
const SEA = Gun.SEA;
|
34
14
|
|
15
|
+
let PROOF_CONTRACT_ADDRESS;
|
35
16
|
let contractAddresses = {
|
36
|
-
PROOF_OF_INTEGRITY_ADDRESS:
|
37
|
-
|
17
|
+
PROOF_OF_INTEGRITY_ADDRESS:
|
18
|
+
CHAIN_CONFIG.optimismSepolia.PROOF_OF_INTEGRITY_ADDRESS,
|
19
|
+
STEALTH_ANNOUNCER_ADDRESS:
|
20
|
+
CHAIN_CONFIG.optimismSepolia.STEALTH_ANNOUNCER_ADDRESS,
|
38
21
|
};
|
39
22
|
|
40
|
-
//
|
41
|
-
|
42
|
-
|
43
|
-
const __filename = fileURLToPath(import.meta.url);
|
44
|
-
const __dirname = dirname(__filename);
|
45
|
-
|
23
|
+
// Funzione di inizializzazione per Node.js
|
24
|
+
const initNodeEnvironment = async () => {
|
25
|
+
if (typeof window === "undefined") {
|
46
26
|
try {
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
27
|
+
// Importazioni dinamiche
|
28
|
+
const { fileURLToPath } = await import("url");
|
29
|
+
const { dirname } = await import("path");
|
30
|
+
const { readFile } = await import("fs/promises");
|
31
|
+
const { join } = await import("path");
|
32
|
+
|
33
|
+
const __filename = fileURLToPath(import.meta.url);
|
34
|
+
const __dirname = dirname(__filename);
|
35
|
+
|
36
|
+
try {
|
37
|
+
const rawdata = await readFile(
|
38
|
+
join(__dirname, "contract-address.json"),
|
39
|
+
"utf8"
|
40
|
+
);
|
41
|
+
contractAddresses = JSON.parse(rawdata);
|
42
|
+
console.log("Loaded contract addresses:", contractAddresses);
|
43
|
+
} catch (err) {
|
44
|
+
console.warn("Warning: contract-address.json not found or invalid");
|
45
|
+
}
|
46
|
+
} catch (error) {
|
47
|
+
console.error("Error loading Node.js modules:", error);
|
52
48
|
}
|
53
|
-
} catch (error) {
|
54
|
-
console.error('Error loading Node.js modules:', error);
|
55
49
|
}
|
50
|
+
};
|
51
|
+
|
52
|
+
// Inizializza solo in ambiente Node.js
|
53
|
+
if (typeof window === "undefined") {
|
54
|
+
initNodeEnvironment();
|
56
55
|
}
|
57
56
|
|
57
|
+
// ... resto del codice ...
|
58
|
+
|
59
|
+
// Esporta tutto ciò che serve
|
60
|
+
export {
|
61
|
+
Gun,
|
62
|
+
initNodeEnvironment,
|
63
|
+
// ... altre esportazioni ...
|
64
|
+
};
|
65
|
+
|
66
|
+
// Export default
|
67
|
+
export default Gun;
|
68
|
+
|
58
69
|
// =============================================
|
59
70
|
// UTILITY FUNCTIONS
|
60
71
|
// =============================================
|
@@ -74,7 +85,7 @@ export function generateRandomId() {
|
|
74
85
|
*/
|
75
86
|
export function generatePassword(signature) {
|
76
87
|
try {
|
77
|
-
const signatureBytes = ethers.
|
88
|
+
const signatureBytes = ethers.toUtf8Bytes(signature);
|
78
89
|
const hash = ethers.keccak256(signatureBytes);
|
79
90
|
console.log("Generated password:", hash);
|
80
91
|
return hash;
|
@@ -125,7 +136,7 @@ export const getSigner = async () => {
|
|
125
136
|
if (rpcUrl && privateKey) {
|
126
137
|
const provider = new ethers.JsonRpcProvider(rpcUrl, {
|
127
138
|
chainId: LOCAL_CONFIG.CHAIN_ID,
|
128
|
-
name: "localhost"
|
139
|
+
name: "localhost",
|
129
140
|
});
|
130
141
|
return new Wallet(privateKey, provider);
|
131
142
|
} else if (
|
@@ -133,7 +144,7 @@ export const getSigner = async () => {
|
|
133
144
|
typeof window.ethereum !== "undefined"
|
134
145
|
) {
|
135
146
|
await window.ethereum.request({ method: "eth_requestAccounts" });
|
136
|
-
const provider = new BrowserProvider(window.ethereum);
|
147
|
+
const provider = new ethers.BrowserProvider(window.ethereum);
|
137
148
|
return provider.getSigner();
|
138
149
|
} else {
|
139
150
|
throw new Error("No valid Ethereum provider found");
|
@@ -148,29 +159,26 @@ export const getSigner = async () => {
|
|
148
159
|
*/
|
149
160
|
export function deriveStealthAddress(sharedSecret, spendingPublicKey) {
|
150
161
|
try {
|
151
|
-
const sharedSecretBytes =
|
152
|
-
const spendingPublicKeyBytes =
|
153
|
-
|
154
|
-
const stealthPrivateKey = keccak256(
|
155
|
-
concat([
|
156
|
-
sharedSecretBytes,
|
157
|
-
spendingPublicKeyBytes
|
158
|
-
])
|
162
|
+
const sharedSecretBytes = ethers.toUtf8Bytes(sharedSecret);
|
163
|
+
const spendingPublicKeyBytes = ethers.toUtf8Bytes(spendingPublicKey);
|
164
|
+
|
165
|
+
const stealthPrivateKey = ethers.keccak256(
|
166
|
+
ethers.concat([sharedSecretBytes, spendingPublicKeyBytes])
|
159
167
|
);
|
160
|
-
|
168
|
+
|
161
169
|
const stealthWallet = new Wallet(stealthPrivateKey);
|
162
170
|
|
163
171
|
console.log("Debug deriveStealthAddress:", {
|
164
172
|
sharedSecretHex: stealthPrivateKey,
|
165
173
|
spendingPublicKey,
|
166
174
|
stealthPrivateKey,
|
167
|
-
stealthAddress: stealthWallet.address
|
175
|
+
stealthAddress: stealthWallet.address,
|
168
176
|
});
|
169
177
|
|
170
178
|
return {
|
171
179
|
stealthPrivateKey,
|
172
180
|
stealthAddress: stealthWallet.address,
|
173
|
-
wallet: stealthWallet
|
181
|
+
wallet: stealthWallet,
|
174
182
|
};
|
175
183
|
} catch (error) {
|
176
184
|
console.error("Error in deriveStealthAddress:", error);
|
@@ -208,7 +216,7 @@ Gun.chain.getSigner = getSigner();
|
|
208
216
|
*/
|
209
217
|
Gun.chain.verifySignature = async function (message, signature) {
|
210
218
|
try {
|
211
|
-
const recoveredAddress = verifyMessage(message, signature);
|
219
|
+
const recoveredAddress = ethers.verifyMessage(message, signature);
|
212
220
|
return recoveredAddress;
|
213
221
|
} catch (error) {
|
214
222
|
console.error("Error verifying signature:", error);
|
@@ -272,17 +280,21 @@ Gun.chain.createAndStoreEncryptedPair = async function (address, signature) {
|
|
272
280
|
const viewingAccount = gunToEthAccount(v_pair.priv);
|
273
281
|
const spendingAccount = gunToEthAccount(s_pair.priv);
|
274
282
|
|
275
|
-
gun
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
283
|
+
gun
|
284
|
+
.get("gun-eth")
|
285
|
+
.get("users")
|
286
|
+
.get(address)
|
287
|
+
.put({
|
288
|
+
pair: encryptedPair,
|
289
|
+
v_pair: encryptedV_pair,
|
290
|
+
s_pair: encryptedS_pair,
|
291
|
+
publicKeys: {
|
292
|
+
viewingPublicKey: v_pair.epub, // Use SEA encryption public key
|
293
|
+
viewingPublicKey: v_pair.epub, // Use SEA encryption public key
|
294
|
+
spendingPublicKey: spendingAccount.publicKey, // Use Ethereum address
|
295
|
+
ethViewingAddress: viewingAccount.publicKey, // Also save Ethereum address
|
296
|
+
},
|
297
|
+
});
|
286
298
|
|
287
299
|
console.log("Encrypted pairs and public keys stored for:", address);
|
288
300
|
} catch (error) {
|
@@ -340,13 +352,13 @@ Gun.chain.proof = function (chain, nodeId, data, callback) {
|
|
340
352
|
|
341
353
|
try {
|
342
354
|
// Se siamo in localhost e in development, usa automaticamente la chain locale
|
343
|
-
const targetChain = isLocalEnvironment() ?
|
355
|
+
const targetChain = isLocalEnvironment() ? "localhost" : chain;
|
344
356
|
const chainConfig = getAddressesForChain(targetChain);
|
345
|
-
|
357
|
+
|
346
358
|
console.log(`Using ${targetChain} configuration:`, chainConfig);
|
347
359
|
|
348
360
|
// Usa gli indirizzi dalla configurazione
|
349
|
-
const contract = new Contract(
|
361
|
+
const contract = new ethers.Contract(
|
350
362
|
chainConfig.PROOF_OF_INTEGRITY_ADDRESS,
|
351
363
|
PROOF_OF_INTEGRITY_ABI,
|
352
364
|
signer
|
@@ -362,7 +374,7 @@ Gun.chain.proof = function (chain, nodeId, data, callback) {
|
|
362
374
|
signer
|
363
375
|
);
|
364
376
|
const [isValid, timestamp, updater] = await contract.verifyData(
|
365
|
-
toUtf8Bytes(nodeId),
|
377
|
+
ethers.toUtf8Bytes(nodeId),
|
366
378
|
contentHash
|
367
379
|
);
|
368
380
|
console.log("Verification result:", { isValid, timestamp, updater });
|
@@ -378,10 +390,7 @@ Gun.chain.proof = function (chain, nodeId, data, callback) {
|
|
378
390
|
PROOF_OF_INTEGRITY_ABI,
|
379
391
|
signer
|
380
392
|
);
|
381
|
-
const tx = await contract.updateData(
|
382
|
-
toUtf8Bytes(nodeId),
|
383
|
-
contentHash
|
384
|
-
);
|
393
|
+
const tx = await contract.updateData(toUtf8Bytes(nodeId), contentHash);
|
385
394
|
console.log("Transaction sent:", tx.hash);
|
386
395
|
const receipt = await tx.wait();
|
387
396
|
console.log("Transaction confirmed:", receipt);
|
@@ -408,7 +417,6 @@ Gun.chain.proof = function (chain, nodeId, data, callback) {
|
|
408
417
|
return { contentHash, timestamp, updater };
|
409
418
|
};
|
410
419
|
|
411
|
-
|
412
420
|
if (nodeId && !data) {
|
413
421
|
// Case 1: User passes only node
|
414
422
|
gun.get(nodeId).once(async (existingData) => {
|
@@ -424,7 +432,8 @@ Gun.chain.proof = function (chain, nodeId, data, callback) {
|
|
424
432
|
console.log("contentHash", contentHash);
|
425
433
|
|
426
434
|
if (!contentHash) {
|
427
|
-
if (callback)
|
435
|
+
if (callback)
|
436
|
+
callback({ err: "No content hash found for this node" });
|
428
437
|
return;
|
429
438
|
}
|
430
439
|
|
@@ -460,7 +469,7 @@ Gun.chain.proof = function (chain, nodeId, data, callback) {
|
|
460
469
|
// Case 2: User passes only text (data)
|
461
470
|
const newNodeId = generateRandomId();
|
462
471
|
const dataString = JSON.stringify(data);
|
463
|
-
const contentHash = keccak256(toUtf8Bytes(dataString));
|
472
|
+
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
|
464
473
|
|
465
474
|
gun
|
466
475
|
.get(newNodeId)
|
@@ -516,10 +525,13 @@ Gun.chain.gunToEthAccount = function (gunPrivateKey) {
|
|
516
525
|
* @param {string} signature - The sender's signature to access their keys
|
517
526
|
* @returns {Promise<Object>} Object containing stealth addresses and keys
|
518
527
|
*/
|
519
|
-
Gun.chain.generateStealthAddress = async function (
|
528
|
+
Gun.chain.generateStealthAddress = async function (
|
529
|
+
recipientAddress,
|
530
|
+
signature
|
531
|
+
) {
|
520
532
|
try {
|
521
533
|
const gun = this;
|
522
|
-
|
534
|
+
|
523
535
|
// Get recipient's public keys
|
524
536
|
const recipientData = await gun
|
525
537
|
.get("gun-eth")
|
@@ -528,14 +540,21 @@ Gun.chain.generateStealthAddress = async function (recipientAddress, signature)
|
|
528
540
|
.get("publicKeys")
|
529
541
|
.then();
|
530
542
|
|
531
|
-
if (
|
543
|
+
if (
|
544
|
+
!recipientData ||
|
545
|
+
!recipientData.viewingPublicKey ||
|
546
|
+
!recipientData.spendingPublicKey
|
547
|
+
) {
|
532
548
|
throw new Error("Recipient's public keys not found");
|
533
549
|
}
|
534
550
|
|
535
551
|
// Get sender's keys
|
536
|
-
const senderAddress = await this.verifySignature(
|
552
|
+
const senderAddress = await this.verifySignature(
|
553
|
+
MESSAGE_TO_SIGN,
|
554
|
+
signature
|
555
|
+
);
|
537
556
|
const password = generatePassword(signature);
|
538
|
-
|
557
|
+
|
539
558
|
const senderData = await gun
|
540
559
|
.get("gun-eth")
|
541
560
|
.get("users")
|
@@ -550,16 +569,20 @@ Gun.chain.generateStealthAddress = async function (recipientAddress, signature)
|
|
550
569
|
let spendingKeyPair;
|
551
570
|
try {
|
552
571
|
const decryptedData = await SEA.decrypt(senderData.s_pair, password);
|
553
|
-
spendingKeyPair =
|
554
|
-
|
555
|
-
|
572
|
+
spendingKeyPair =
|
573
|
+
typeof decryptedData === "string"
|
574
|
+
? JSON.parse(decryptedData)
|
575
|
+
: decryptedData;
|
556
576
|
} catch (error) {
|
557
577
|
console.error("Error decrypting spending pair:", error);
|
558
578
|
throw new Error("Unable to decrypt spending pair");
|
559
579
|
}
|
560
580
|
|
561
581
|
// Generate shared secret using SEA ECDH with encryption public key
|
562
|
-
const sharedSecret = await SEA.secret(
|
582
|
+
const sharedSecret = await SEA.secret(
|
583
|
+
recipientData.viewingPublicKey,
|
584
|
+
spendingKeyPair
|
585
|
+
);
|
563
586
|
|
564
587
|
if (!sharedSecret) {
|
565
588
|
throw new Error("Unable to generate shared secret");
|
@@ -575,9 +598,8 @@ Gun.chain.generateStealthAddress = async function (recipientAddress, signature)
|
|
575
598
|
return {
|
576
599
|
stealthAddress,
|
577
600
|
senderPublicKey: spendingKeyPair.epub, // Use encryption public key
|
578
|
-
spendingPublicKey: recipientData.spendingPublicKey
|
601
|
+
spendingPublicKey: recipientData.spendingPublicKey,
|
579
602
|
};
|
580
|
-
|
581
603
|
} catch (error) {
|
582
604
|
console.error("Error generating stealth address:", error);
|
583
605
|
throw error;
|
@@ -665,10 +687,14 @@ Gun.chain.recoverStealthFunds = async function (
|
|
665
687
|
// Decrypt viewing and spending pairs
|
666
688
|
let viewingKeyPair;
|
667
689
|
try {
|
668
|
-
const decryptedViewingData = await SEA.decrypt(
|
669
|
-
|
670
|
-
|
671
|
-
|
690
|
+
const decryptedViewingData = await SEA.decrypt(
|
691
|
+
encryptedData.v_pair,
|
692
|
+
password
|
693
|
+
);
|
694
|
+
viewingKeyPair =
|
695
|
+
typeof decryptedViewingData === "string"
|
696
|
+
? JSON.parse(decryptedViewingData)
|
697
|
+
: decryptedViewingData;
|
672
698
|
} catch (error) {
|
673
699
|
console.error("Error decrypting keys:", error);
|
674
700
|
throw new Error("Unable to decrypt keys");
|
@@ -693,7 +719,7 @@ Gun.chain.recoverStealthFunds = async function (
|
|
693
719
|
console.error("Mismatch:", {
|
694
720
|
recovered: recoveredAddress,
|
695
721
|
expected: stealthAddress,
|
696
|
-
sharedSecret
|
722
|
+
sharedSecret,
|
697
723
|
});
|
698
724
|
throw new Error("Recovered stealth address does not match");
|
699
725
|
}
|
@@ -721,11 +747,14 @@ Gun.chain.announceStealthPayment = async function (
|
|
721
747
|
senderPublicKey,
|
722
748
|
spendingPublicKey,
|
723
749
|
signature,
|
724
|
-
options = { onChain: false, chain:
|
750
|
+
options = { onChain: false, chain: "optimismSepolia" }
|
725
751
|
) {
|
726
752
|
try {
|
727
753
|
const gun = this;
|
728
|
-
const senderAddress = await this.verifySignature(
|
754
|
+
const senderAddress = await this.verifySignature(
|
755
|
+
MESSAGE_TO_SIGN,
|
756
|
+
signature
|
757
|
+
);
|
729
758
|
|
730
759
|
if (options.onChain) {
|
731
760
|
// On-chain announcement
|
@@ -752,24 +781,21 @@ Gun.chain.announceStealthPayment = async function (
|
|
752
781
|
stealthAddress,
|
753
782
|
{ value: devFee }
|
754
783
|
);
|
755
|
-
|
784
|
+
|
756
785
|
console.log("Transaction sent:", tx.hash);
|
757
786
|
const receipt = await tx.wait();
|
758
787
|
console.log("Transaction confirmed:", receipt.hash);
|
759
|
-
|
788
|
+
|
760
789
|
console.log("Stealth payment announced on-chain (dev fee paid)");
|
761
790
|
} else {
|
762
791
|
// Off-chain announcement (GunDB)
|
763
|
-
gun
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
spendingPublicKey,
|
771
|
-
timestamp: Date.now(),
|
772
|
-
});
|
792
|
+
gun.get("gun-eth").get("stealth-payments").set({
|
793
|
+
stealthAddress,
|
794
|
+
senderAddress,
|
795
|
+
senderPublicKey,
|
796
|
+
spendingPublicKey,
|
797
|
+
timestamp: Date.now(),
|
798
|
+
});
|
773
799
|
console.log("Stealth payment announced off-chain");
|
774
800
|
}
|
775
801
|
} catch (error) {
|
@@ -784,13 +810,18 @@ Gun.chain.announceStealthPayment = async function (
|
|
784
810
|
* @param {string} signature - The signature to authenticate the user
|
785
811
|
* @returns {Promise<Array>} List of stealth payments
|
786
812
|
*/
|
787
|
-
Gun.chain.getStealthPayments = async function (
|
813
|
+
Gun.chain.getStealthPayments = async function (
|
814
|
+
signature,
|
815
|
+
options = { source: "both" }
|
816
|
+
) {
|
788
817
|
try {
|
789
818
|
const payments = [];
|
790
819
|
|
791
|
-
if (options.source ===
|
820
|
+
if (options.source === "onChain" || options.source === "both") {
|
792
821
|
const signer = await getSigner();
|
793
|
-
const chainConfig = getAddressesForChain(
|
822
|
+
const chainConfig = getAddressesForChain(
|
823
|
+
options.chain || "optimismSepolia"
|
824
|
+
);
|
794
825
|
const contractAddress = chainConfig.STEALTH_ANNOUNCER_ADDRESS;
|
795
826
|
|
796
827
|
const contract = new Contract(
|
@@ -798,28 +829,32 @@ Gun.chain.getStealthPayments = async function (signature, options = { source: 'b
|
|
798
829
|
STEALTH_ANNOUNCER_ABI,
|
799
830
|
signer
|
800
831
|
);
|
801
|
-
|
832
|
+
|
802
833
|
try {
|
803
834
|
// Get total number of announcements
|
804
835
|
const totalAnnouncements = await contract.getAnnouncementsCount();
|
805
836
|
const totalCount = Number(totalAnnouncements.toString());
|
806
837
|
console.log("Total on-chain announcements:", totalCount);
|
807
|
-
|
838
|
+
|
808
839
|
if (totalCount > 0) {
|
809
840
|
// Get announcements in batches of 100
|
810
841
|
const batchSize = 100;
|
811
842
|
const lastIndex = totalCount - 1;
|
812
|
-
|
813
|
-
for(let i = 0; i <= lastIndex; i += batchSize) {
|
843
|
+
|
844
|
+
for (let i = 0; i <= lastIndex; i += batchSize) {
|
814
845
|
const toIndex = Math.min(i + batchSize - 1, lastIndex);
|
815
846
|
const batch = await contract.getAnnouncementsInRange(i, toIndex);
|
816
|
-
|
847
|
+
|
817
848
|
// For each announcement, try to decrypt
|
818
|
-
for(const announcement of batch) {
|
849
|
+
for (const announcement of batch) {
|
819
850
|
try {
|
820
851
|
// Verify announcement is valid
|
821
|
-
if (
|
822
|
-
|
852
|
+
if (
|
853
|
+
!announcement ||
|
854
|
+
!announcement.stealthAddress ||
|
855
|
+
!announcement.senderPublicKey ||
|
856
|
+
!announcement.spendingPublicKey
|
857
|
+
) {
|
823
858
|
console.log("Invalid announcement:", announcement);
|
824
859
|
continue;
|
825
860
|
}
|
@@ -831,20 +866,21 @@ Gun.chain.getStealthPayments = async function (signature, options = { source: 'b
|
|
831
866
|
signature,
|
832
867
|
announcement.spendingPublicKey
|
833
868
|
);
|
834
|
-
|
869
|
+
|
835
870
|
// If no errors thrown, announcement is for us
|
836
871
|
payments.push({
|
837
872
|
stealthAddress: announcement.stealthAddress,
|
838
873
|
senderPublicKey: announcement.senderPublicKey,
|
839
874
|
spendingPublicKey: announcement.spendingPublicKey,
|
840
875
|
timestamp: Number(announcement.timestamp),
|
841
|
-
source:
|
842
|
-
wallet: recoveredWallet
|
876
|
+
source: "onChain",
|
877
|
+
wallet: recoveredWallet,
|
843
878
|
});
|
844
|
-
|
845
879
|
} catch (e) {
|
846
880
|
// Not for us, continue
|
847
|
-
console.log(
|
881
|
+
console.log(
|
882
|
+
`Announcement not for us: ${announcement.stealthAddress}`
|
883
|
+
);
|
848
884
|
continue;
|
849
885
|
}
|
850
886
|
}
|
@@ -855,7 +891,7 @@ Gun.chain.getStealthPayments = async function (signature, options = { source: 'b
|
|
855
891
|
}
|
856
892
|
}
|
857
893
|
|
858
|
-
if (options.source ===
|
894
|
+
if (options.source === "offChain" || options.source === "both") {
|
859
895
|
// Get off-chain payments
|
860
896
|
const gun = this;
|
861
897
|
const offChainPayments = await new Promise((resolve) => {
|
@@ -867,12 +903,12 @@ Gun.chain.getStealthPayments = async function (signature, options = { source: 'b
|
|
867
903
|
.map()
|
868
904
|
.once((payment, id) => {
|
869
905
|
if (payment?.stealthAddress) {
|
870
|
-
p.push({ ...payment, id, source:
|
906
|
+
p.push({ ...payment, id, source: "offChain" });
|
871
907
|
}
|
872
908
|
});
|
873
909
|
setTimeout(() => resolve(p), 2000);
|
874
910
|
});
|
875
|
-
|
911
|
+
|
876
912
|
payments.push(...offChainPayments);
|
877
913
|
}
|
878
914
|
|
@@ -889,7 +925,7 @@ Gun.chain.getStealthPayments = async function (signature, options = { source: 'b
|
|
889
925
|
* @param {string} recipientAddress - The recipient's address
|
890
926
|
* @returns {Promise<void>}
|
891
927
|
*/
|
892
|
-
Gun.chain.cleanStealthPayments = async function(recipientAddress) {
|
928
|
+
Gun.chain.cleanStealthPayments = async function (recipientAddress) {
|
893
929
|
try {
|
894
930
|
const gun = this;
|
895
931
|
const payments = await gun
|
@@ -904,7 +940,12 @@ Gun.chain.cleanStealthPayments = async function(recipientAddress) {
|
|
904
940
|
if (payments) {
|
905
941
|
Object.keys(payments).forEach(async (key) => {
|
906
942
|
const payment = payments[key];
|
907
|
-
if (
|
943
|
+
if (
|
944
|
+
!payment ||
|
945
|
+
!payment.stealthAddress ||
|
946
|
+
!payment.senderPublicKey ||
|
947
|
+
!payment.spendingPublicKey
|
948
|
+
) {
|
908
949
|
await gun
|
909
950
|
.get("gun-eth")
|
910
951
|
.get("stealth-payments")
|
@@ -931,7 +972,7 @@ export class GunEth {
|
|
931
972
|
static gunToEthAccount = gunToEthAccount;
|
932
973
|
static getSigner = getSigner;
|
933
974
|
static deriveStealthAddress = deriveStealthAddress;
|
934
|
-
|
975
|
+
|
935
976
|
// Chain methods
|
936
977
|
static chainMethods = {
|
937
978
|
setSigner: Gun.chain.setSigner,
|
@@ -948,7 +989,7 @@ export class GunEth {
|
|
948
989
|
recoverStealthFunds: Gun.chain.recoverStealthFunds,
|
949
990
|
announceStealthPayment: Gun.chain.announceStealthPayment,
|
950
991
|
getStealthPayments: Gun.chain.getStealthPayments,
|
951
|
-
cleanStealthPayments: Gun.chain.cleanStealthPayments
|
992
|
+
cleanStealthPayments: Gun.chain.cleanStealthPayments,
|
952
993
|
};
|
953
994
|
|
954
995
|
// Constants
|
@@ -956,6 +997,3 @@ export class GunEth {
|
|
956
997
|
static PROOF_CONTRACT_ADDRESS = PROOF_CONTRACT_ADDRESS;
|
957
998
|
static LOCAL_CONFIG = LOCAL_CONFIG;
|
958
999
|
}
|
959
|
-
|
960
|
-
// Esporta Gun come default
|
961
|
-
export default Gun;
|