gun-eth 1.1.5 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- package/index.js +62 -171
- package/package.json +1 -1
package/index.js
CHANGED
@@ -101,11 +101,6 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
|
|
101
101
|
return this;
|
102
102
|
}
|
103
103
|
|
104
|
-
if (typeof data !== "object" || data === null) {
|
105
|
-
callback({ err: "Data must be a non-null object" });
|
106
|
-
return this;
|
107
|
-
}
|
108
|
-
|
109
104
|
const gun = this;
|
110
105
|
|
111
106
|
// Seleziona l'indirizzo basato sulla catena
|
@@ -126,22 +121,6 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
|
|
126
121
|
}
|
127
122
|
};
|
128
123
|
|
129
|
-
// Funzione per scrivere on-chain
|
130
|
-
const writeOnChain = async (nodeId, contentHash) => {
|
131
|
-
const signer = await getSigner();
|
132
|
-
const contract = new ethers.Contract(
|
133
|
-
SHINE_CONTRACT_ADDRESS,
|
134
|
-
SHINE_ABI,
|
135
|
-
signer
|
136
|
-
);
|
137
|
-
const tx = await contract.updateData(
|
138
|
-
ethers.toUtf8Bytes(nodeId),
|
139
|
-
contentHash
|
140
|
-
);
|
141
|
-
await tx.wait();
|
142
|
-
return tx;
|
143
|
-
};
|
144
|
-
|
145
124
|
// Funzione per verificare on-chain
|
146
125
|
const verifyOnChain = async (nodeId, contentHash) => {
|
147
126
|
const signer = await getSigner();
|
@@ -154,185 +133,97 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
|
|
154
133
|
ethers.toUtf8Bytes(nodeId),
|
155
134
|
contentHash
|
156
135
|
);
|
157
|
-
return isValid;
|
136
|
+
return { isValid, timestamp, updater };
|
158
137
|
};
|
159
138
|
|
160
|
-
// Funzione per
|
161
|
-
const
|
139
|
+
// Funzione per scrivere on-chain
|
140
|
+
const writeOnChain = async (nodeId, contentHash) => {
|
162
141
|
const signer = await getSigner();
|
163
142
|
const contract = new ethers.Contract(
|
164
143
|
SHINE_CONTRACT_ADDRESS,
|
165
144
|
SHINE_ABI,
|
166
145
|
signer
|
167
146
|
);
|
168
|
-
const
|
169
|
-
ethers.toUtf8Bytes(nodeId)
|
147
|
+
const tx = await contract.updateData(
|
148
|
+
ethers.toUtf8Bytes(nodeId),
|
149
|
+
contentHash
|
170
150
|
);
|
171
|
-
|
151
|
+
await tx.wait();
|
152
|
+
return tx;
|
172
153
|
};
|
173
154
|
|
174
155
|
// Processo SHINE
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
// Ottieni l'ultimo record on-chain
|
182
|
-
const latestRecord = await getLatestRecord(nodeId);
|
183
|
-
console.log("Ultimo record on-chain:", latestRecord);
|
184
|
-
|
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
|
-
});
|
156
|
+
if (nodeId && !data) {
|
157
|
+
// Caso 1: Utente passa solo il nodo
|
158
|
+
gun.get(nodeId).once(async (existingData) => {
|
159
|
+
if (!existingData) {
|
160
|
+
if (callback) callback({ err: "Node not found in GunDB" });
|
192
161
|
return;
|
193
162
|
}
|
194
163
|
|
195
|
-
|
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
|
-
});
|
205
|
-
} else {
|
206
|
-
// Il dato non è verificato on-chain, procedi alla scrittura
|
207
|
-
if (!existingData) {
|
208
|
-
gun.get(nodeId).put(dataToVerify);
|
209
|
-
}
|
210
|
-
|
211
|
-
await writeOnChain(nodeId, contentHash);
|
164
|
+
console.log("existingData", existingData);
|
212
165
|
|
213
|
-
|
214
|
-
|
166
|
+
const dataString = JSON.stringify(existingData);
|
167
|
+
console.log("dataString", dataString);
|
168
|
+
const contentHash = dataString;
|
169
|
+
console.log("contentHash", contentHash);
|
215
170
|
|
216
|
-
|
171
|
+
try {
|
172
|
+
const { isValid, timestamp, updater } = await verifyOnChain(
|
173
|
+
nodeId,
|
174
|
+
contentHash
|
175
|
+
);
|
176
|
+
if (isValid) {
|
217
177
|
if (callback)
|
218
178
|
callback({
|
219
179
|
ok: true,
|
220
|
-
message: "Data
|
180
|
+
message: "Data verified on blockchain",
|
181
|
+
timestamp,
|
182
|
+
updater,
|
221
183
|
});
|
222
184
|
} else {
|
223
185
|
if (callback)
|
224
|
-
callback({
|
186
|
+
callback({
|
187
|
+
ok: false,
|
188
|
+
message: "Data not verified on blockchain",
|
189
|
+
});
|
225
190
|
}
|
191
|
+
} catch (error) {
|
192
|
+
if (callback) callback({ err: error.message });
|
226
193
|
}
|
227
|
-
} catch (error) {
|
228
|
-
if (callback) callback({ err: error.message });
|
229
|
-
}
|
230
|
-
});
|
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
194
|
});
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
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
|
-
|
286
|
-
return gun;
|
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
|
-
};
|
195
|
+
} else if (data && !nodeId) {
|
196
|
+
// Caso 2: Utente passa solo il testo (data)
|
197
|
+
const newNodeId = Gun.text.random();
|
198
|
+
const dataString = JSON.stringify(data);
|
199
|
+
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
|
304
200
|
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
}
|
201
|
+
gun.get(newNodeId).put(data, async (ack) => {
|
202
|
+
console.log("ack", ack);
|
203
|
+
if (ack.err) {
|
204
|
+
if (callback) callback({ err: "Error saving data to GunDB" });
|
205
|
+
return;
|
206
|
+
}
|
312
207
|
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
208
|
+
try {
|
209
|
+
const tx = await writeOnChain(newNodeId, contentHash);
|
210
|
+
if (callback)
|
211
|
+
callback({
|
212
|
+
ok: true,
|
213
|
+
message: "Data written to GunDB and blockchain",
|
214
|
+
nodeId: newNodeId,
|
215
|
+
txHash: tx.hash,
|
216
|
+
});
|
217
|
+
} catch (error) {
|
218
|
+
if (callback) callback({ err: error.message });
|
219
|
+
}
|
321
220
|
});
|
221
|
+
} else {
|
222
|
+
if (callback)
|
223
|
+
callback({
|
224
|
+
err: "Invalid input. Provide either nodeId or data, not both.",
|
225
|
+
});
|
322
226
|
}
|
323
227
|
|
324
|
-
return
|
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
|
-
}
|
228
|
+
return gun;
|
338
229
|
};
|