gun-eth 1.2.1 → 1.2.3

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.
Binary file
package/README.md CHANGED
@@ -4,43 +4,123 @@
4
4
 
5
5
  gun-eth is a plugin for GunDB that integrates Ethereum and Web3 functionality. This plugin extends GunDB's capabilities by allowing interaction with the Ethereum blockchain and providing cryptographic and signature management features.
6
6
 
7
- ## Key Features
7
+ SHINE Smart Contract deployed on Optimism Sepolia: [0x43D838b683F772F08f321E5FA265ad3e333BE9C2](https://sepolia-optimism.etherscan.io/address/0x43D838b683F772F08f321E5FA265ad3e333BE9C2)
8
8
 
9
- - Ethereum signature verification
10
- - Signature-based password generation
11
- - Signature creation using Ethereum providers (e.g., MetaMask)
9
+ Currently, the contract is deployed only on Optimism Sepolia. In the future, it will be deployed on multiple chains.
12
10
 
13
- ## Example
11
+ ### Key Features
14
12
 
15
- [gun-eth dapp](https://gun-eth.vercel.app/)
13
+ - **Ethereum Signature Verification**: Verify Ethereum signatures for messages.
14
+ - **Password Generation**: Generate secure passwords from Ethereum signatures.
15
+ - **Signature Creation**: Create Ethereum signatures for messages.
16
+ - **Encrypted Key Pair Management**: Create, store, and retrieve encrypted key pairs.
17
+ - **SHINE Implementation**: Implement the SHINE for data verification on the blockchain.
16
18
 
17
- ## Installation
19
+ ### How to install
18
20
 
19
21
  ```bash
20
22
  npm install gun-eth
21
23
  ```
22
24
 
23
- ## Usage
24
-
25
- ```javascript
26
- const Gun = require("gun");
27
- require("gun-eth");
25
+ ```bash
26
+ import gun from "gun";
27
+ import "gun-eth";
28
28
 
29
- const gun = Gun();
29
+ const gun = gun();
30
30
 
31
- // Now you can use the new features
31
+ await gun.generatePassword("YOUR_SIGNATURE");
32
32
  ```
33
33
 
34
- ## Main Functions
34
+ ### How to use
35
+
36
+ Learn more about Gun.js [here](https://gun.eco/docs/Getting-Started).
37
+
38
+ Learn more about plugin implementation [here](https://github.com/amark/gun/wiki/Adding-Methods-to-the-Gun-Chain#abstraction-layers).
39
+
40
+ ### Core Functions
41
+
42
+ - `verifySignature(message, signature)`: Verifies an Ethereum signature for a given message.
43
+
44
+ ```js
45
+ const recoveredAddress = await gun.verifySignature(message, signature);
46
+ ```
47
+
48
+ - `generatePassword(signature)`: Generates a password from an Ethereum signature.
49
+
50
+ ```js
51
+ const password = gun.generatePassword(signature);
52
+ ```
53
+
54
+ - `createSignature(message)`: Creates an Ethereum signature for a message.
55
+
56
+ ```js
57
+ const signature = await gun.createSignature(message);
58
+ ```
59
+
60
+ - `createAndStoreEncryptedPair(address, signature)`: Creates and stores an encrypted key pair.
35
61
 
36
- - `gun.verifySignature(message, signature)`: Verifies an Ethereum signature
37
- - `gun.generatePassword(signature)`: Generates a password based on a signature
38
- - `gun.createSignature(message)`: Creates a signature using an Ethereum provider
62
+ ```js
63
+ await gun.createAndStoreEncryptedPair(address, signature);
64
+ ```
65
+
66
+ - `getAndDecryptPair(address, signature)`: Retrieves and decrypts a stored key pair.
67
+
68
+ ```js
69
+ const decryptedPair = await gun.getAndDecryptPair(address, signature);
70
+ ```
71
+
72
+ - `shine(chain, nodeId, data, callback)`: Implements SHINE for data verification and storage on the blockchain.
73
+ ```js
74
+ gun.shine("optimismSepolia", nodeId, data, callback);
75
+ ```
76
+
77
+ ### SHINE
78
+
79
+ SHINE (Secure Hash Integrity Network Ethereum) provides a mechanism for verifying data integrity using Ethereum and Gun.js.
80
+
81
+ 1. **Data Storage**: When saving data, a content hash is generated and stored in both Gun.js and on the Ethereum blockchain.
82
+ 2. **Data Verification**: To verify data, the stored hash is compared with a hash generated from the data retrieved from Gun.js.
83
+ 3. **Blockchain Interaction**: The plugin interacts with an Ethereum smart contract to store and verify data hashes.
84
+
85
+ ### Usage Examples
86
+
87
+ #### Verifying Data by NodeId
88
+
89
+ ```js
90
+ const nodeId = "your-node-id-here";
91
+ gun.shine("optimismSepolia", nodeId, null, (ack) => {
92
+ if (ack.ok) {
93
+ console.log("Data verified on blockchain", ack);
94
+ console.log("Timestamp:", ack.timestamp);
95
+ console.log("Updater:", ack.updater);
96
+ console.log("Latest Record:", ack.latestRecord);
97
+ } else {
98
+ console.log("Data not verified or not found", ack);
99
+ }
100
+ });
101
+ ```
102
+
103
+ #### Storing New Data
104
+
105
+ ```js
106
+ const data = { message: "Hello, blockchain!" };
107
+ gun.shine("optimismSepolia", null, data, (ack) => {
108
+ if (ack.ok) {
109
+ console.log("Data stored on Gun.js and blockchain", ack);
110
+ console.log("New Node ID:", ack.nodeId);
111
+ console.log("Transaction Hash:", ack.txHash);
112
+ } else {
113
+ console.log("Error storing data", ack);
114
+ }
115
+ });
116
+ ```
39
117
 
40
- ## Dependencies
118
+ ### Security Considerations
41
119
 
42
- - gun: ^0.2020.1239
43
- - ethers: ^6.0.0
120
+ - Use a secure Ethereum provider (e.g., MetaMask) when interacting with functions that require signatures.
121
+ - Generated passwords and key pairs are sensitive. Handle them carefully and avoid exposing them.
122
+ - Keep Gun.js and Ethereum dependencies up to date for security.
123
+ - Be aware of gas costs associated with blockchain interactions when using SHINE.
44
124
 
45
125
  ## Contributing
46
126
 
package/index.js CHANGED
@@ -1,8 +1,14 @@
1
- const Gun = require("gun/gun");
1
+ /* const Gun = require("gun/gun");
2
2
  const SEA = require("gun/sea");
3
3
  const ethers = require("ethers");
4
+ const SHINE = require("./SHINE.json"); */
5
+
6
+ import Gun from "gun";
7
+ import SEA from "gun/sea";
8
+ import { ethers } from "ethers";
9
+
10
+ import SHINE from "./SHINE.json";
4
11
 
5
- const SHINE = require("./SHINE.json");
6
12
  const SHINE_ABI = SHINE.abi;
7
13
  const SHINE_OPTIMISM_SEPOLIA = SHINE.address;
8
14
 
@@ -123,6 +129,7 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
123
129
 
124
130
  // Funzione per verificare on-chain
125
131
  const verifyOnChain = async (nodeId, contentHash) => {
132
+ console.log("Verifying on chain:", { nodeId, contentHash });
126
133
  const signer = await getSigner();
127
134
  const contract = new ethers.Contract(
128
135
  SHINE_CONTRACT_ADDRESS,
@@ -133,11 +140,13 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
133
140
  ethers.toUtf8Bytes(nodeId),
134
141
  contentHash
135
142
  );
143
+ console.log("Verification result:", { isValid, timestamp, updater });
136
144
  return { isValid, timestamp, updater };
137
145
  };
138
146
 
139
147
  // Funzione per scrivere on-chain
140
148
  const writeOnChain = async (nodeId, contentHash) => {
149
+ console.log("Writing on chain:", { nodeId, contentHash });
141
150
  const signer = await getSigner();
142
151
  const contract = new ethers.Contract(
143
152
  SHINE_CONTRACT_ADDRESS,
@@ -148,10 +157,32 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
148
157
  ethers.toUtf8Bytes(nodeId),
149
158
  contentHash
150
159
  );
151
- await tx.wait();
160
+ console.log("Transaction sent:", tx.hash);
161
+ const receipt = await tx.wait();
162
+ console.log("Transaction confirmed:", receipt);
152
163
  return tx;
153
164
  };
154
165
 
166
+ // Nuova funzione per ottenere l'ultimo record dalla blockchain
167
+ const getLatestRecord = async (nodeId) => {
168
+ const signer = await getSigner();
169
+ const contract = new ethers.Contract(
170
+ SHINE_CONTRACT_ADDRESS,
171
+ SHINE_ABI,
172
+ signer
173
+ );
174
+ const [contentHash, timestamp, updater] = await contract.getLatestRecord(
175
+ ethers.toUtf8Bytes(nodeId)
176
+ );
177
+ console.log("Latest record from blockchain:", {
178
+ nodeId,
179
+ contentHash,
180
+ timestamp,
181
+ updater,
182
+ });
183
+ return { contentHash, timestamp, updater };
184
+ };
185
+
155
186
  // Processo SHINE
156
187
  if (nodeId && !data) {
157
188
  // Caso 1: Utente passa solo il nodo
@@ -163,16 +194,22 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
163
194
 
164
195
  console.log("existingData", existingData);
165
196
 
166
- const dataString = JSON.stringify(existingData);
167
- console.log("dataString", dataString);
168
- const contentHash = dataString;
197
+ // Usa il contentHash memorizzato invece di ricalcolarlo
198
+ const contentHash = existingData._contentHash;
169
199
  console.log("contentHash", contentHash);
170
200
 
201
+ if (!contentHash) {
202
+ if (callback) callback({ err: "No content hash found for this node" });
203
+ return;
204
+ }
205
+
171
206
  try {
172
207
  const { isValid, timestamp, updater } = await verifyOnChain(
173
208
  nodeId,
174
209
  contentHash
175
210
  );
211
+ const latestRecord = await getLatestRecord(nodeId);
212
+
176
213
  if (isValid) {
177
214
  if (callback)
178
215
  callback({
@@ -180,12 +217,14 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
180
217
  message: "Data verified on blockchain",
181
218
  timestamp,
182
219
  updater,
220
+ latestRecord,
183
221
  });
184
222
  } else {
185
223
  if (callback)
186
224
  callback({
187
225
  ok: false,
188
226
  message: "Data not verified on blockchain",
227
+ latestRecord,
189
228
  });
190
229
  }
191
230
  } catch (error) {
@@ -198,26 +237,28 @@ Gun.chain.shine = function (chain, nodeId, data, callback) {
198
237
  const dataString = JSON.stringify(data);
199
238
  const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
200
239
 
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
- }
240
+ gun
241
+ .get(newNodeId)
242
+ .put({ ...data, _contentHash: contentHash }, async (ack) => {
243
+ console.log("ack", ack);
244
+ if (ack.err) {
245
+ if (callback) callback({ err: "Error saving data to GunDB" });
246
+ return;
247
+ }
207
248
 
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
- }
220
- });
249
+ try {
250
+ const tx = await writeOnChain(newNodeId, contentHash);
251
+ if (callback)
252
+ callback({
253
+ ok: true,
254
+ message: "Data written to GunDB and blockchain",
255
+ nodeId: newNodeId,
256
+ txHash: tx.hash,
257
+ });
258
+ } catch (error) {
259
+ if (callback) callback({ err: error.message });
260
+ }
261
+ });
221
262
  } else {
222
263
  if (callback)
223
264
  callback({
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "gun-eth",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "A GunDB plugin for Ethereum, and Web3",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"
8
8
  },
9
9
  "dependencies": {
10
- "gun": "^0.2020.1239",
11
- "ethers": "^6.0.0"
10
+ "ethers": "^6.0.0",
11
+ "gun": "^0.2020.1239"
12
12
  },
13
13
  "author": "scobru",
14
14
  "repository": {
@@ -16,7 +16,6 @@
16
16
  "url": "git+https://github.com/scobru/gun-eth.git"
17
17
  },
18
18
  "license": "MIT",
19
- "devDependencies": {},
20
19
  "keywords": [
21
20
  "gundb",
22
21
  "web3",