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.
- package/.yarn/cache/@adraffy-ens-normalize-npm-1.10.1-e60d7ca58d-0836f394ea.zip +0 -0
- package/.yarn/cache/@noble-curves-npm-1.2.0-9b40ee1239-bb798d7a66.zip +0 -0
- package/.yarn/cache/@noble-hashes-npm-1.3.2-1e619f9da0-fe23536b43.zip +0 -0
- package/.yarn/cache/@peculiar-asn1-schema-npm-2.3.13-7e52f15480-245cf39899.zip +0 -0
- package/.yarn/cache/@peculiar-json-schema-npm-1.1.12-f914d2ea65-b26ececdc2.zip +0 -0
- package/.yarn/cache/@peculiar-webcrypto-npm-1.5.0-4d84865c57-9022d7452d.zip +0 -0
- package/.yarn/cache/@types-node-npm-18.15.13-14d4d1b98f-79cc5a2b5f.zip +0 -0
- package/.yarn/cache/aes-js-npm-4.0.0-beta.5-c70da65547-cc2ea969d7.zip +0 -0
- package/.yarn/cache/asn1js-npm-3.0.5-cf5558af33-3b6af1bbad.zip +0 -0
- package/.yarn/cache/ethers-npm-6.13.2-177cd791bd-981860c736.zip +0 -0
- package/.yarn/cache/gun-npm-0.2020.1240-e808e9ee90-0effb70596.zip +0 -0
- package/.yarn/cache/pvtsutils-npm-1.3.5-b3122eabea-e734516b3c.zip +0 -0
- package/.yarn/cache/pvutils-npm-1.1.3-da8b07d6cf-2ee26a9e51.zip +0 -0
- package/.yarn/cache/tslib-npm-2.4.0-9cb6dc5030-8c4aa6a3c5.zip +0 -0
- package/.yarn/cache/tslib-npm-2.7.0-21668f5c21-1606d5c89f.zip +0 -0
- package/.yarn/cache/webcrypto-core-npm-1.8.0-b7c655d14a-4f128f5283.zip +0 -0
- package/.yarn/cache/ws-npm-7.5.10-878ccb886b-f9bb062abf.zip +0 -0
- package/.yarn/cache/ws-npm-8.17.1-f57fb24a2c-442badcce1.zip +0 -0
- package/.yarn/install-state.gz +0 -0
- package/README.md +101 -21
- package/index.js +66 -25
- package/package.json +3 -4
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
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
|
-
|
7
|
+
SHINE Smart Contract deployed on Optimism Sepolia: [0x43D838b683F772F08f321E5FA265ad3e333BE9C2](https://sepolia-optimism.etherscan.io/address/0x43D838b683F772F08f321E5FA265ad3e333BE9C2)
|
8
8
|
|
9
|
-
|
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
|
-
|
11
|
+
### Key Features
|
14
12
|
|
15
|
-
|
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
|
-
|
19
|
+
### How to install
|
18
20
|
|
19
21
|
```bash
|
20
22
|
npm install gun-eth
|
21
23
|
```
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
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 =
|
29
|
+
const gun = gun();
|
30
30
|
|
31
|
-
|
31
|
+
await gun.generatePassword("YOUR_SIGNATURE");
|
32
32
|
```
|
33
33
|
|
34
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
118
|
+
### Security Considerations
|
41
119
|
|
42
|
-
-
|
43
|
-
-
|
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
|
-
|
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
|
-
|
167
|
-
|
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
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
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
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
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.
|
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
|
-
"
|
11
|
-
"
|
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",
|