gun-eth 1.1.4 → 1.2.0
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.
- package/index.js +58 -159
- 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,172 +133,92 @@ 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
|
|
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);
|
|
212
|
-
|
|
213
|
-
// Verifica dopo la scrittura
|
|
214
|
-
const verifiedAfterWrite = await verifyOnChain(nodeId, contentHash);
|
|
164
|
+
const dataString = JSON.stringify(existingData);
|
|
165
|
+
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
|
|
215
166
|
|
|
216
|
-
|
|
167
|
+
try {
|
|
168
|
+
const { isValid, timestamp, updater } = await verifyOnChain(
|
|
169
|
+
nodeId,
|
|
170
|
+
contentHash
|
|
171
|
+
);
|
|
172
|
+
if (isValid) {
|
|
217
173
|
if (callback)
|
|
218
174
|
callback({
|
|
219
175
|
ok: true,
|
|
220
|
-
message: "Data
|
|
176
|
+
message: "Data verified on blockchain",
|
|
177
|
+
timestamp,
|
|
178
|
+
updater,
|
|
221
179
|
});
|
|
222
180
|
} else {
|
|
223
181
|
if (callback)
|
|
224
|
-
callback({
|
|
182
|
+
callback({
|
|
183
|
+
ok: false,
|
|
184
|
+
message: "Data not verified on blockchain",
|
|
185
|
+
});
|
|
225
186
|
}
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (callback) callback({ err: error.message });
|
|
226
189
|
}
|
|
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
190
|
});
|
|
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
|
-
};
|
|
191
|
+
} else if (data && !nodeId) {
|
|
192
|
+
// Caso 2: Utente passa solo il testo (data)
|
|
193
|
+
const newNodeId = Gun.text.random();
|
|
194
|
+
const dataString = JSON.stringify(data);
|
|
195
|
+
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
|
|
304
196
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
);
|
|
311
|
-
}
|
|
197
|
+
gun.get(newNodeId).put(data, async (ack) => {
|
|
198
|
+
if (ack.err) {
|
|
199
|
+
if (callback) callback({ err: "Error saving data to GunDB" });
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
312
202
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
203
|
+
try {
|
|
204
|
+
const tx = await writeOnChain(newNodeId, contentHash);
|
|
205
|
+
if (callback)
|
|
206
|
+
callback({
|
|
207
|
+
ok: true,
|
|
208
|
+
message: "Data written to GunDB and blockchain",
|
|
209
|
+
nodeId: newNodeId,
|
|
210
|
+
txHash: tx.hash,
|
|
211
|
+
});
|
|
212
|
+
} catch (error) {
|
|
213
|
+
if (callback) callback({ err: error.message });
|
|
214
|
+
}
|
|
321
215
|
});
|
|
216
|
+
} else {
|
|
217
|
+
if (callback)
|
|
218
|
+
callback({
|
|
219
|
+
err: "Invalid input. Provide either nodeId or data, not both.",
|
|
220
|
+
});
|
|
322
221
|
}
|
|
323
222
|
|
|
324
|
-
return
|
|
223
|
+
return gun;
|
|
325
224
|
};
|