gun-eth 1.0.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/README.md +55 -0
- package/index.js +88 -0
- package/package.json +34 -0
package/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# gun-eth
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
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
|
+
|
7
|
+
## Key Features
|
8
|
+
|
9
|
+
- Ethereum signature verification
|
10
|
+
- Signature-based password generation
|
11
|
+
- Signature creation using Ethereum providers (e.g., MetaMask)
|
12
|
+
|
13
|
+
## Example
|
14
|
+
|
15
|
+
[gun-eth dapp](https://gun-eth.vercel.app/)
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
```bash
|
20
|
+
npm install gun-eth
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```javascript
|
26
|
+
const Gun = require("gun");
|
27
|
+
require("gun-eth");
|
28
|
+
|
29
|
+
const gun = Gun();
|
30
|
+
|
31
|
+
// Now you can use the new features
|
32
|
+
```
|
33
|
+
|
34
|
+
## Main Functions
|
35
|
+
|
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
|
39
|
+
|
40
|
+
## Dependencies
|
41
|
+
|
42
|
+
- gun: ^0.2020.1239
|
43
|
+
- ethers: ^6.0.0
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
We welcome contributions! Please open an issue or submit a pull request on GitHub.
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
This project is released under the MIT license.
|
52
|
+
|
53
|
+
## Contact
|
54
|
+
|
55
|
+
For questions or support, please open an issue on GitHub: https://github.com/scobru/gun-eth
|
package/index.js
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
import Gun from "gun/gun";
|
2
|
+
import SEA from "gun/sea";
|
3
|
+
import { ethers } from "ethers";
|
4
|
+
|
5
|
+
// Aggiungi il metodo alla catena di Gun
|
6
|
+
Gun.chain.verifySignature = async function (message, signature) {
|
7
|
+
try {
|
8
|
+
const recoveredAddress = ethers.verifyMessage(message, signature);
|
9
|
+
return recoveredAddress;
|
10
|
+
} catch (error) {
|
11
|
+
console.error("Errore durante la verifica della firma:", error);
|
12
|
+
return null;
|
13
|
+
}
|
14
|
+
};
|
15
|
+
|
16
|
+
Gun.chain.generatePassword = function (signature) {
|
17
|
+
try {
|
18
|
+
// Usa SHA-256 per derivare una password dalla firma
|
19
|
+
const hexSignature = ethers.hexlify(signature);
|
20
|
+
const hash = ethers.keccak256(hexSignature);
|
21
|
+
|
22
|
+
console.log("Password generata:", hash);
|
23
|
+
return hash;
|
24
|
+
} catch (error) {
|
25
|
+
console.error("Errore nella generazione della password:", error);
|
26
|
+
return null;
|
27
|
+
}
|
28
|
+
};
|
29
|
+
|
30
|
+
Gun.chain.createSignature = async function (message) {
|
31
|
+
try {
|
32
|
+
// Controlla se window.ethereum è disponibile (metamask o altro provider)
|
33
|
+
if (typeof window.ethereum !== "undefined") {
|
34
|
+
await window.ethereum.request({ method: "eth_requestAccounts" });
|
35
|
+
const provider = new ethers.BrowserProvider(window.ethereum);
|
36
|
+
const signer = await provider.getSigner();
|
37
|
+
const signature = await signer.signMessage(message);
|
38
|
+
console.log("Firma creata:", signature);
|
39
|
+
return signature;
|
40
|
+
} else {
|
41
|
+
throw new Error("Provider Ethereum non trovato");
|
42
|
+
}
|
43
|
+
} catch (error) {
|
44
|
+
console.error("Errore durante la creazione della firma:", error);
|
45
|
+
return null;
|
46
|
+
}
|
47
|
+
};
|
48
|
+
|
49
|
+
Gun.chain.createAndStoreEncryptedPair = async function (address, signature) {
|
50
|
+
try {
|
51
|
+
const gun = this;
|
52
|
+
const pair = await SEA.pair();
|
53
|
+
const encryptedPair = await SEA.encrypt(JSON.stringify(pair), signature);
|
54
|
+
|
55
|
+
await gun.get("users").get(address).put({ encryptedPair });
|
56
|
+
console.log("Pair crittografato e archiviato per:", address);
|
57
|
+
} catch (error) {
|
58
|
+
console.error(
|
59
|
+
"Errore durante la creazione e l'archiviazione del pair crittografato:",
|
60
|
+
error
|
61
|
+
);
|
62
|
+
}
|
63
|
+
};
|
64
|
+
|
65
|
+
Gun.chain.getAndDecryptPair = async function (address, signature) {
|
66
|
+
try {
|
67
|
+
const gun = this;
|
68
|
+
const encryptedData = await gun
|
69
|
+
.get("users")
|
70
|
+
.get(address)
|
71
|
+
.get("encryptedPair")
|
72
|
+
.then();
|
73
|
+
if (!encryptedData) {
|
74
|
+
throw new Error("Nessun dato crittografato trovato per questo indirizzo");
|
75
|
+
}
|
76
|
+
|
77
|
+
const decryptedPair = await SEA.decrypt(encryptedData, signature);
|
78
|
+
|
79
|
+
console.log(decryptedPair);
|
80
|
+
return decryptedPair;
|
81
|
+
} catch (error) {
|
82
|
+
console.error(
|
83
|
+
"Errore durante il recupero e la decrittazione del pair:",
|
84
|
+
error
|
85
|
+
);
|
86
|
+
return null;
|
87
|
+
}
|
88
|
+
};
|
package/package.json
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
{
|
2
|
+
"name": "gun-eth",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "A GunDB plugin for Ethereum, and Web3",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
+
},
|
9
|
+
"dependencies": {
|
10
|
+
"gun": "^0.2020.1239",
|
11
|
+
"ethers": "^6.0.0"
|
12
|
+
},
|
13
|
+
"author": "scobru",
|
14
|
+
"repository": {
|
15
|
+
"type": "git",
|
16
|
+
"url": "git+https://github.com/scobru/gun-eth.git"
|
17
|
+
},
|
18
|
+
"license": "MIT",
|
19
|
+
"devDependencies": {},
|
20
|
+
"keywords": [
|
21
|
+
"gundb",
|
22
|
+
"web3",
|
23
|
+
"evm",
|
24
|
+
"ethereum",
|
25
|
+
"decentralized",
|
26
|
+
"storage",
|
27
|
+
"gun",
|
28
|
+
"db"
|
29
|
+
],
|
30
|
+
"bugs": {
|
31
|
+
"url": "https://github.com/scobru/gun-eth/issues"
|
32
|
+
},
|
33
|
+
"homepage": "https://github.com/scobru/gun-eth#readme"
|
34
|
+
}
|