gun-eth 1.1.3 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +140 -27
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -108,7 +108,7 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
108
108
 
109
109
  const gun = this;
110
110
 
111
- // select address based on chain
111
+ // Seleziona l'indirizzo basato sulla catena
112
112
  if (chain === "optimismSepolia") {
113
113
  SHINE_CONTRACT_ADDRESS = SHINE_OPTIMISM_SEPOLIA;
114
114
  } else {
@@ -150,7 +150,11 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
150
150
  SHINE_ABI,
151
151
  signer
152
152
  );
153
- return await contract.verifyData(ethers.toUtf8Bytes(nodeId), contentHash);
153
+ const [isValid, timestamp, updater] = await contract.verifyData(
154
+ ethers.toUtf8Bytes(nodeId),
155
+ contentHash
156
+ );
157
+ return isValid;
154
158
  };
155
159
 
156
160
  // Funzione per ottenere l'ultimo record on-chain
@@ -161,7 +165,10 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
161
165
  SHINE_ABI,
162
166
  signer
163
167
  );
164
- return await contract.getLatestRecord(ethers.toUtf8Bytes(nodeId));
168
+ const [contentHash, timestamp, updater] = await contract.getLatestRecord(
169
+ ethers.toUtf8Bytes(nodeId)
170
+ );
171
+ return { contentHash, timestamp, updater };
165
172
  };
166
173
 
167
174
  // Processo SHINE
@@ -172,27 +179,31 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
172
179
  const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
173
180
 
174
181
  // Ottieni l'ultimo record on-chain
175
- const [lastContentHash, lastTimestamp, lastUpdater] =
176
- await getLatestRecord(nodeId);
182
+ const latestRecord = await getLatestRecord(nodeId);
183
+ console.log("Ultimo record on-chain:", latestRecord);
177
184
 
178
- if (lastContentHash !== ethers.ZeroHash) {
179
- // Il dato esiste on-chain
180
- if (lastContentHash === contentHash) {
181
- // Il dato on-chain corrisponde al dato in GunDB
182
- if (callback)
183
- callback({
184
- ok: true,
185
- message: "Data verified on GunDB and blockchain",
186
- updater: lastUpdater,
187
- timestamp: lastTimestamp.toString(),
188
- });
189
- } else {
190
- // Il dato on-chain è diverso dal dato in GunDB
191
- if (callback)
192
- callback({ err: "Data mismatch between GunDB and blockchain" });
193
- }
185
+ // Verifica se il contenuto corrisponde all'ultimo record on-chain
186
+ if (latestRecord.contentHash === contentHash) {
187
+ if (callback)
188
+ callback({
189
+ ok: true,
190
+ message: "Data already up-to-date on blockchain",
191
+ });
192
+ return;
193
+ }
194
+
195
+ // Verifica on-chain prima di procedere
196
+ const isVerified = await verifyOnChain(nodeId, contentHash);
197
+
198
+ if (isVerified) {
199
+ // Il dato è già verificato on-chain
200
+ if (callback)
201
+ callback({
202
+ ok: true,
203
+ message: "Data already verified on blockchain",
204
+ });
194
205
  } else {
195
- // Il dato non esiste on-chain, procedi alla scrittura
206
+ // Il dato non è verificato on-chain, procedi alla scrittura
196
207
  if (!existingData) {
197
208
  gun.get(nodeId).put(dataToVerify);
198
209
  }
@@ -200,16 +211,13 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
200
211
  await writeOnChain(nodeId, contentHash);
201
212
 
202
213
  // Verifica dopo la scrittura
203
- const [newContentHash, newTimestamp, newUpdater] =
204
- await getLatestRecord(nodeId);
214
+ const verifiedAfterWrite = await verifyOnChain(nodeId, contentHash);
205
215
 
206
- if (newContentHash === contentHash) {
216
+ if (verifiedAfterWrite) {
207
217
  if (callback)
208
218
  callback({
209
219
  ok: true,
210
220
  message: "Data written and verified on GunDB and blockchain",
211
- updater: newUpdater,
212
- timestamp: newTimestamp.toString(),
213
221
  });
214
222
  } else {
215
223
  if (callback)
@@ -221,5 +229,110 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
221
229
  }
222
230
  });
223
231
 
232
+ // Aggiungi questa funzione all'interno di shine
233
+ gun.batchShine = async function (nodeIds, dataArray) {
234
+ if (nodeIds.length !== dataArray.length) {
235
+ throw new Error(
236
+ "Il numero di nodeIds deve corrispondere al numero di elementi dati"
237
+ );
238
+ }
239
+
240
+ const contentHashes = [];
241
+ for (let i = 0; i < nodeIds.length; i++) {
242
+ const dataString = JSON.stringify(dataArray[i]);
243
+ const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
244
+ contentHashes.push(contentHash);
245
+ gun.get(nodeIds[i]).put(dataArray[i]);
246
+ }
247
+
248
+ try {
249
+ const tx = await batchWriteOnChain(nodeIds, contentHashes);
250
+ console.log("Batch update completato:", tx.hash);
251
+ return tx;
252
+ } catch (error) {
253
+ console.error("Errore durante il batch update:", error);
254
+ throw error;
255
+ }
256
+ };
257
+
258
+ // Aggiungi un listener per l'evento DataUpdated
259
+ const provider = new ethers.BrowserProvider(window.ethereum);
260
+ const contract = new ethers.Contract(
261
+ SHINE_CONTRACT_ADDRESS,
262
+ SHINE_ABI,
263
+ provider
264
+ );
265
+ contract.on("DataUpdated", (nodeId, contentHash, updater) => {
266
+ console.log("Evento DataUpdated ricevuto:", {
267
+ nodeId: ethers.toUtf8String(nodeId),
268
+ contentHash,
269
+ updater,
270
+ });
271
+ // Qui puoi aggiungere la logica per gestire l'evento, ad esempio aggiornare l'interfaccia utente
272
+ });
273
+
274
+ // Aggiungi questa funzione all'interno di shine
275
+ gun.getLatestRecordFromChain = async function (nodeId) {
276
+ try {
277
+ const latestRecord = await getLatestRecord(nodeId);
278
+ console.log("Ultimo record on-chain per", nodeId, ":", latestRecord);
279
+ return latestRecord;
280
+ } catch (error) {
281
+ console.error("Errore nel recupero dell'ultimo record:", error);
282
+ throw error;
283
+ }
284
+ };
285
+
224
286
  return gun;
225
287
  };
288
+
289
+ // Aggiungi questa funzione dopo writeOnChain
290
+ const batchWriteOnChain = async (nodeIds, contentHashes) => {
291
+ const signer = await getSigner();
292
+ const contract = new ethers.Contract(
293
+ SHINE_CONTRACT_ADDRESS,
294
+ SHINE_ABI,
295
+ signer
296
+ );
297
+ const tx = await contract.batchUpdateData(
298
+ nodeIds.map((id) => ethers.toUtf8Bytes(id)),
299
+ contentHashes
300
+ );
301
+ await tx.wait();
302
+ return tx;
303
+ };
304
+
305
+ // Aggiungi questa funzione per verificare i dati batch
306
+ Gun.chain.batchVerify = async function (nodeIds, dataArray) {
307
+ if (nodeIds.length !== dataArray.length) {
308
+ throw new Error(
309
+ "Il numero di nodeIds deve corrispondere al numero di elementi dati"
310
+ );
311
+ }
312
+
313
+ const results = [];
314
+ for (let i = 0; i < nodeIds.length; i++) {
315
+ const dataString = JSON.stringify(dataArray[i]);
316
+ const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
317
+ const isVerified = await verifyOnChain(nodeIds[i], contentHash);
318
+ results.push({
319
+ nodeId: nodeIds[i],
320
+ isVerified: isVerified,
321
+ });
322
+ }
323
+
324
+ return results;
325
+ };
326
+
327
+ // Aggiungi questa funzione per la verifica singola
328
+ Gun.chain.verifyOnly = async function (nodeId, data) {
329
+ try {
330
+ const dataString = JSON.stringify(data);
331
+ const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
332
+ const isVerified = await verifyOnChain(nodeId, contentHash);
333
+ return isVerified;
334
+ } catch (error) {
335
+ console.error("Errore durante la verifica:", error);
336
+ return null;
337
+ }
338
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gun-eth",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "A GunDB plugin for Ethereum, and Web3",
5
5
  "main": "index.js",
6
6
  "scripts": {