gun-eth 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/SHINE.sol +48 -0
- package/index.js +224 -0
- package/package.json +1 -1
package/SHINE.sol
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
pragma solidity ^0.8.0;
|
3
|
+
|
4
|
+
contract SHINE {
|
5
|
+
struct DataInfo {
|
6
|
+
bool exists;
|
7
|
+
address storer;
|
8
|
+
bytes nodeId;
|
9
|
+
}
|
10
|
+
|
11
|
+
mapping(bytes32 => DataInfo) public dataHashes;
|
12
|
+
|
13
|
+
event DataHashStored(
|
14
|
+
bytes32 indexed dataHash,
|
15
|
+
address indexed storer,
|
16
|
+
bytes nodeId
|
17
|
+
);
|
18
|
+
|
19
|
+
function storeDataHash(
|
20
|
+
bytes32 dataHash,
|
21
|
+
bytes memory nodeId
|
22
|
+
) public returns (bytes memory, bytes32) {
|
23
|
+
require(!dataHashes[dataHash].exists, "Hash already stored");
|
24
|
+
dataHashes[dataHash] = DataInfo(true, msg.sender, nodeId);
|
25
|
+
emit DataHashStored(dataHash, msg.sender, nodeId);
|
26
|
+
return (nodeId, dataHash);
|
27
|
+
}
|
28
|
+
|
29
|
+
function verifyDataHash(
|
30
|
+
bytes32 dataHash
|
31
|
+
) public view returns (bool exists, address storer, bytes memory nodeId) {
|
32
|
+
DataInfo memory info = dataHashes[dataHash];
|
33
|
+
return (info.exists, info.storer, info.nodeId);
|
34
|
+
}
|
35
|
+
|
36
|
+
function batchStoreDataHashes(
|
37
|
+
bytes32[] memory hashes,
|
38
|
+
bytes[] memory nodeIds
|
39
|
+
) public {
|
40
|
+
require(hashes.length == nodeIds.length, "Arrays length mismatch");
|
41
|
+
for (uint i = 0; i < hashes.length; i++) {
|
42
|
+
if (!dataHashes[hashes[i]].exists) {
|
43
|
+
dataHashes[hashes[i]] = DataInfo(true, msg.sender, nodeIds[i]);
|
44
|
+
emit DataHashStored(hashes[i], msg.sender, nodeIds[i]);
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
}
|
package/index.js
CHANGED
@@ -86,3 +86,227 @@ Gun.chain.getAndDecryptPair = async function (address, signature) {
|
|
86
86
|
return null;
|
87
87
|
}
|
88
88
|
};
|
89
|
+
|
90
|
+
const SHINE_ABI = [
|
91
|
+
{
|
92
|
+
anonymous: false,
|
93
|
+
inputs: [
|
94
|
+
{
|
95
|
+
indexed: true,
|
96
|
+
internalType: "bytes32",
|
97
|
+
name: "dataHash",
|
98
|
+
type: "bytes32",
|
99
|
+
},
|
100
|
+
{
|
101
|
+
indexed: true,
|
102
|
+
internalType: "address",
|
103
|
+
name: "storer",
|
104
|
+
type: "address",
|
105
|
+
},
|
106
|
+
{
|
107
|
+
indexed: false,
|
108
|
+
internalType: "bytes",
|
109
|
+
name: "nodeId",
|
110
|
+
type: "bytes",
|
111
|
+
},
|
112
|
+
],
|
113
|
+
name: "DataHashStored",
|
114
|
+
type: "event",
|
115
|
+
},
|
116
|
+
{
|
117
|
+
inputs: [
|
118
|
+
{
|
119
|
+
internalType: "bytes32[]",
|
120
|
+
name: "hashes",
|
121
|
+
type: "bytes32[]",
|
122
|
+
},
|
123
|
+
{
|
124
|
+
internalType: "bytes[]",
|
125
|
+
name: "nodeIds",
|
126
|
+
type: "bytes[]",
|
127
|
+
},
|
128
|
+
],
|
129
|
+
name: "batchStoreDataHashes",
|
130
|
+
outputs: [],
|
131
|
+
stateMutability: "nonpayable",
|
132
|
+
type: "function",
|
133
|
+
},
|
134
|
+
{
|
135
|
+
inputs: [
|
136
|
+
{
|
137
|
+
internalType: "bytes32",
|
138
|
+
name: "",
|
139
|
+
type: "bytes32",
|
140
|
+
},
|
141
|
+
],
|
142
|
+
name: "dataHashes",
|
143
|
+
outputs: [
|
144
|
+
{
|
145
|
+
internalType: "bool",
|
146
|
+
name: "exists",
|
147
|
+
type: "bool",
|
148
|
+
},
|
149
|
+
{
|
150
|
+
internalType: "address",
|
151
|
+
name: "storer",
|
152
|
+
type: "address",
|
153
|
+
},
|
154
|
+
{
|
155
|
+
internalType: "bytes",
|
156
|
+
name: "nodeId",
|
157
|
+
type: "bytes",
|
158
|
+
},
|
159
|
+
],
|
160
|
+
stateMutability: "view",
|
161
|
+
type: "function",
|
162
|
+
},
|
163
|
+
{
|
164
|
+
inputs: [
|
165
|
+
{
|
166
|
+
internalType: "bytes32",
|
167
|
+
name: "dataHash",
|
168
|
+
type: "bytes32",
|
169
|
+
},
|
170
|
+
{
|
171
|
+
internalType: "bytes",
|
172
|
+
name: "nodeId",
|
173
|
+
type: "bytes",
|
174
|
+
},
|
175
|
+
],
|
176
|
+
name: "storeDataHash",
|
177
|
+
outputs: [
|
178
|
+
{
|
179
|
+
internalType: "bytes",
|
180
|
+
name: "",
|
181
|
+
type: "bytes",
|
182
|
+
},
|
183
|
+
{
|
184
|
+
internalType: "bytes32",
|
185
|
+
name: "",
|
186
|
+
type: "bytes32",
|
187
|
+
},
|
188
|
+
],
|
189
|
+
stateMutability: "nonpayable",
|
190
|
+
type: "function",
|
191
|
+
},
|
192
|
+
{
|
193
|
+
inputs: [
|
194
|
+
{
|
195
|
+
internalType: "bytes32",
|
196
|
+
name: "dataHash",
|
197
|
+
type: "bytes32",
|
198
|
+
},
|
199
|
+
],
|
200
|
+
name: "verifyDataHash",
|
201
|
+
outputs: [
|
202
|
+
{
|
203
|
+
internalType: "bool",
|
204
|
+
name: "exists",
|
205
|
+
type: "bool",
|
206
|
+
},
|
207
|
+
{
|
208
|
+
internalType: "address",
|
209
|
+
name: "storer",
|
210
|
+
type: "address",
|
211
|
+
},
|
212
|
+
{
|
213
|
+
internalType: "bytes",
|
214
|
+
name: "nodeId",
|
215
|
+
type: "bytes",
|
216
|
+
},
|
217
|
+
],
|
218
|
+
stateMutability: "view",
|
219
|
+
type: "function",
|
220
|
+
},
|
221
|
+
];
|
222
|
+
|
223
|
+
// Indirizzo del contratto SHINE deployato
|
224
|
+
const SHINE_OPTIMISM_SEPOLIA = "0xb78E3E846FAf57Db15FfF17d8200a7736A7EDfBF"; // Inserisci qui l'indirizzo del contratto deployato
|
225
|
+
|
226
|
+
let SHINE_CONTRACT_ADDRESS;
|
227
|
+
|
228
|
+
Gun.chain.shine = function (chain, data, callback) {
|
229
|
+
const gun = this;
|
230
|
+
const sea = Gun.SEA;
|
231
|
+
|
232
|
+
// select address based on chain
|
233
|
+
if (chain === "optimismSepolia") {
|
234
|
+
SHINE_CONTRACT_ADDRESS = SHINE_OPTIMISM_SEPOLIA;
|
235
|
+
} else {
|
236
|
+
throw new Error("Chain not supported");
|
237
|
+
}
|
238
|
+
|
239
|
+
// Funzione per ottenere il signer
|
240
|
+
const getSigner = async () => {
|
241
|
+
if (typeof window.ethereum !== "undefined") {
|
242
|
+
await window.ethereum.request({ method: "eth_requestAccounts" });
|
243
|
+
const provider = new ethers.BrowserProvider(window.ethereum);
|
244
|
+
return provider.getSigner();
|
245
|
+
} else {
|
246
|
+
throw new Error("Ethereum provider not found");
|
247
|
+
}
|
248
|
+
};
|
249
|
+
|
250
|
+
// Funzione per scrivere on-chain
|
251
|
+
const writeOnChain = async (hash, nodeId) => {
|
252
|
+
const signer = await getSigner();
|
253
|
+
const contract = new ethers.Contract(
|
254
|
+
SHINE_CONTRACT_ADDRESS,
|
255
|
+
SHINE_ABI,
|
256
|
+
signer
|
257
|
+
);
|
258
|
+
const tx = await contract.storeDataHash(hash, ethers.toUtf8Bytes(nodeId));
|
259
|
+
await tx.wait();
|
260
|
+
};
|
261
|
+
|
262
|
+
// Funzione per verificare on-chain
|
263
|
+
const verifyOnChain = async (hash) => {
|
264
|
+
const signer = await getSigner();
|
265
|
+
const contract = new ethers.Contract(
|
266
|
+
SHINE_CONTRACT_ADDRESS,
|
267
|
+
SHINE_ABI,
|
268
|
+
signer
|
269
|
+
);
|
270
|
+
return await contract.verifyDataHash(hash);
|
271
|
+
};
|
272
|
+
|
273
|
+
// Processo SHINE
|
274
|
+
gun.put(data, async (ack) => {
|
275
|
+
if (ack.err) {
|
276
|
+
if (callback) callback({ err: ack.err });
|
277
|
+
return;
|
278
|
+
}
|
279
|
+
|
280
|
+
try {
|
281
|
+
// Calcola l'hash del dato
|
282
|
+
const dataString = JSON.stringify(data);
|
283
|
+
const dataHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
|
284
|
+
|
285
|
+
// Ottieni l'ID del nodo corrente
|
286
|
+
const nodeId = gun.back("opt.pid") || "unknown";
|
287
|
+
|
288
|
+
// Scrivi on-chain
|
289
|
+
await writeOnChain(dataHash, nodeId);
|
290
|
+
|
291
|
+
// Verifica GunDB e chain
|
292
|
+
const gunData = await gun.get(ack.get).then();
|
293
|
+
const [exists, storer, storedNodeId] = await verifyOnChain(dataHash);
|
294
|
+
|
295
|
+
if (JSON.stringify(gunData) === dataString && exists) {
|
296
|
+
if (callback)
|
297
|
+
callback({
|
298
|
+
ok: true,
|
299
|
+
message: "Data verified on GunDB and blockchain",
|
300
|
+
storer: storer,
|
301
|
+
nodeId: ethers.toUtf8String(storedNodeId),
|
302
|
+
});
|
303
|
+
} else {
|
304
|
+
if (callback) callback({ err: "Data verification failed" });
|
305
|
+
}
|
306
|
+
} catch (error) {
|
307
|
+
if (callback) callback({ err: error.message });
|
308
|
+
}
|
309
|
+
});
|
310
|
+
|
311
|
+
return gun;
|
312
|
+
};
|