gun-eth 1.3.5 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +18 -207
- package/dist/gun-eth-protocol.cjs.js +11528 -0
- package/dist/gun-eth-protocol.esm.js +11503 -0
- package/dist/gun-eth-protocol.js +18 -0
- package/dist/gun-eth-protocol.react.js +11503 -0
- package/dist/gun-eth-protocol.umd.js +18 -0
- package/jsdoc.json +7 -0
- package/package.json +28 -25
- package/rollup.config.js +80 -0
- package/src/index.js +160 -512
- package/src/lib/authentication/index.js +13 -0
- package/src/lib/authentication/isAuthenticated.js +20 -0
- package/src/lib/authentication/login.js +25 -0
- package/src/lib/authentication/register.js +58 -0
- package/src/lib/blockchain/ethereum.js +74 -0
- package/src/lib/blockchain/shine.js +204 -0
- package/src/lib/certificates/friendsCertificates.js +92 -0
- package/src/lib/certificates/index.js +44 -0
- package/src/lib/certificates/messagingCertificates.js +94 -0
- package/src/lib/friends/acceptFriendRequest.js +69 -0
- package/src/lib/friends/addFriendRequest.js +49 -0
- package/src/lib/friends/friendRequests.js +51 -0
- package/src/lib/friends/friendsList.js +57 -0
- package/src/lib/friends/index.js +36 -0
- package/src/lib/friends/rejectFriendRequest.js +31 -0
- package/src/lib/messaging/chatsList.js +42 -0
- package/src/lib/messaging/createChat.js +132 -0
- package/src/lib/messaging/index.js +36 -0
- package/src/lib/messaging/messageList.js +106 -0
- package/src/lib/messaging/sendMessage.js +132 -0
- package/src/lib/messaging/sendVoiceMessage.js +119 -0
- package/src/lib/notes/createNote.js +41 -0
- package/src/lib/notes/deleteNote.js +12 -0
- package/src/lib/notes/getNote.js +25 -0
- package/src/lib/notes/getUserNote.js +59 -0
- package/src/lib/notes/index.js +8 -0
- package/src/lib/notes/updateNotes.js +35 -0
- package/src/lib/post/createPost.js +17 -0
- package/src/lib/post/decryptPost.js +14 -0
- package/src/lib/post/deletePost.js +13 -0
- package/src/lib/post/encryptPost,js +16 -0
- package/src/lib/post/getPost.js +36 -0
- package/src/lib/post/index.js +9 -0
- package/src/lib/post/updatePost.js +16 -0
- package/src/state/gun.js +33 -0
- package/types/types.d.ts +244 -0
- package/TUTORIAL.md +0 -103
- package/src/examples/eth2gun.html +0 -163
- package/src/examples/gun2eth.html +0 -164
- package/src/examples/shine.html +0 -256
- /package/src/{abis → lib/blockchain/abis}/SHINE.json +0 -0
- /package/src/{contracts → lib/blockchain/contracts}/SHINE.sol +0 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
import { gun } from "../../state/gun";
|
2
|
+
import { checkAuth, isAuthenticated } from "./isAuthenticated";
|
3
|
+
import loginUser from "./login";
|
4
|
+
import registerUser from "./register";
|
5
|
+
|
6
|
+
let logout = () => {
|
7
|
+
gun.user().leave();
|
8
|
+
|
9
|
+
isAuthenticated.next(false);
|
10
|
+
};
|
11
|
+
|
12
|
+
export { checkAuth, isAuthenticated, loginUser, registerUser, logout };
|
13
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { Subject } from "rxjs";
|
2
|
+
import { gun, user } from "../../state/gun";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* This contains a subscribable function that will return a boolean of whether or not the user is authenticated or not.
|
6
|
+
*/
|
7
|
+
let isAuthenticated = new Subject();
|
8
|
+
|
9
|
+
/**
|
10
|
+
* This function will check and see whether the user is authenticated or not.
|
11
|
+
*/
|
12
|
+
let checkAuth = () => {
|
13
|
+
if (user.is) return isAuthenticated.next(true);
|
14
|
+
else return isAuthenticated.next(false);
|
15
|
+
};
|
16
|
+
|
17
|
+
gun.on("auth", () => isAuthenticated.next(true));
|
18
|
+
|
19
|
+
export { isAuthenticated, checkAuth };
|
20
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import { user } from "../../state/gun";
|
2
|
+
|
3
|
+
/**
|
4
|
+
* This function will authenticate a user who has registered.
|
5
|
+
*
|
6
|
+
* @param {Object} credentials The users authentication credentials that they used when registering.
|
7
|
+
* @param credentials.username
|
8
|
+
* @param credentials.password
|
9
|
+
*
|
10
|
+
* @param {Function} callback The callback function returns error messages or a success message.
|
11
|
+
*/
|
12
|
+
let loginUser = (credentials = {}, callback = () => {}) => {
|
13
|
+
user.auth(credentials.username, credentials.password, ({ err, pub }) => {
|
14
|
+
if (err) return callback({ errMessage: err, errCode: "gun-auth-error" });
|
15
|
+
else
|
16
|
+
return callback({
|
17
|
+
errMessage: undefined,
|
18
|
+
errCode: undefined,
|
19
|
+
pub,
|
20
|
+
message: "Successfully authenticated user.",
|
21
|
+
});
|
22
|
+
});
|
23
|
+
};
|
24
|
+
|
25
|
+
export default loginUser;
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import { gun, user } from "../../state/gun";
|
2
|
+
|
3
|
+
/**
|
4
|
+
* This function will check to see if the email is already in use.
|
5
|
+
*
|
6
|
+
* @param {string} username
|
7
|
+
* @returns Promise<boolean>
|
8
|
+
*/
|
9
|
+
let checkUsernameInUse = async (username) => {
|
10
|
+
let user = await gun.get(`~@${username}`);
|
11
|
+
|
12
|
+
return user !== undefined;
|
13
|
+
};
|
14
|
+
|
15
|
+
/**
|
16
|
+
* This function will create a user.
|
17
|
+
*
|
18
|
+
* @param {Object} credentials The users authentication credentials that they want to use when logging in.
|
19
|
+
* @param credentials.username
|
20
|
+
* @param credentials.password
|
21
|
+
*
|
22
|
+
* @param {Function} callback The callback function returns error messages or a success message.
|
23
|
+
*/
|
24
|
+
let registerUser = (credentials = {}, callback = () => {}) => {
|
25
|
+
console.log("Credentials", credentials);
|
26
|
+
console.log("Registering user...");
|
27
|
+
(async () => {
|
28
|
+
if (await checkUsernameInUse(credentials.username))
|
29
|
+
return callback({
|
30
|
+
errMessage: "Username in use.",
|
31
|
+
errCode: "username-inuse",
|
32
|
+
});
|
33
|
+
else {
|
34
|
+
user.create(credentials.username, credentials.password, ({ err, pub }) => {
|
35
|
+
if (err)
|
36
|
+
return callback({ errMessage: err, errCode: "gun-auth-error" });
|
37
|
+
else
|
38
|
+
return callback({
|
39
|
+
errMessage: undefined,
|
40
|
+
errCode: undefined,
|
41
|
+
pub,
|
42
|
+
message: "Successfully created user.",
|
43
|
+
});
|
44
|
+
});
|
45
|
+
}
|
46
|
+
})();
|
47
|
+
};
|
48
|
+
|
49
|
+
function str2ab(str) {
|
50
|
+
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
|
51
|
+
var bufView = new Uint16Array(buf);
|
52
|
+
for (var i = 0, strLen = str.length; i < strLen; i++) {
|
53
|
+
bufView[i] = str.charCodeAt(i);
|
54
|
+
}
|
55
|
+
return buf;
|
56
|
+
}
|
57
|
+
|
58
|
+
export default registerUser;
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import { ethers } from 'ethers';
|
2
|
+
|
3
|
+
let rpcUrl = "";
|
4
|
+
let privateKey = "";
|
5
|
+
|
6
|
+
/**
|
7
|
+
* @typedef {Object} Window
|
8
|
+
* @property {any} ethereum
|
9
|
+
*/
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Sets the configuration for standalone mode
|
13
|
+
* @param {string} newRpcUrl - The new RPC URL
|
14
|
+
* @param {string} newPrivateKey - The new private key
|
15
|
+
*/
|
16
|
+
export function setStandaloneConfig(newRpcUrl, newPrivateKey) {
|
17
|
+
rpcUrl = newRpcUrl;
|
18
|
+
privateKey = newPrivateKey;
|
19
|
+
}
|
20
|
+
|
21
|
+
/**
|
22
|
+
* Gets a signer for Ethereum transactions
|
23
|
+
* @returns {Promise<ethers.Signer>} A signer object
|
24
|
+
* @throws {Error} If no valid Ethereum provider is found
|
25
|
+
*/
|
26
|
+
export async function getSigner() {
|
27
|
+
if (rpcUrl && privateKey) {
|
28
|
+
const provider = new ethers.JsonRpcProvider(rpcUrl);
|
29
|
+
return new ethers.Wallet(privateKey, provider);
|
30
|
+
} else if (typeof window !== "undefined" && typeof /** @type {any} */(window).ethereum !== "undefined") {
|
31
|
+
await window.ethereum.request({ method: "eth_requestAccounts" });
|
32
|
+
const provider = new ethers.BrowserProvider(window.ethereum);
|
33
|
+
return provider.getSigner();
|
34
|
+
} else {
|
35
|
+
throw new Error("No valid Ethereum provider found");
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Gets an Ethereum provider
|
41
|
+
* @returns {Promise<ethers.Provider>} An Ethereum provider
|
42
|
+
* @throws {Error} If no valid Ethereum provider is found
|
43
|
+
*/
|
44
|
+
export async function getProvider() {
|
45
|
+
if (rpcUrl && privateKey) {
|
46
|
+
return new ethers.JsonRpcProvider(rpcUrl);
|
47
|
+
} else if (typeof window !== "undefined" && typeof window.ethereum !== "undefined") {
|
48
|
+
return new ethers.BrowserProvider(window.ethereum);
|
49
|
+
} else {
|
50
|
+
throw new Error("No valid Ethereum provider found");
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
/**
|
55
|
+
* Gets the ENS name for a given Ethereum address
|
56
|
+
* @param {string} address - The Ethereum address to look up
|
57
|
+
* @returns {Promise<string|null>} The ENS name if found, null otherwise
|
58
|
+
*/
|
59
|
+
export async function getEnsName(address) {
|
60
|
+
const provider = await getProvider();
|
61
|
+
try {
|
62
|
+
const ensName = await provider.lookupAddress(address);
|
63
|
+
if (ensName) {
|
64
|
+
console.log(`The ENS name for address ${address} is: ${ensName}`);
|
65
|
+
return ensName;
|
66
|
+
} else {
|
67
|
+
console.log(`No ENS name found for address ${address}`);
|
68
|
+
return null;
|
69
|
+
}
|
70
|
+
} catch (error) {
|
71
|
+
console.error("Error while looking up ENS name:", error);
|
72
|
+
return null;
|
73
|
+
}
|
74
|
+
}
|
@@ -0,0 +1,204 @@
|
|
1
|
+
import { SHINE_ABI, SHINE_OPTIMISM_SEPOLIA } from "../utils/utils.js";
|
2
|
+
|
3
|
+
/**
|
4
|
+
* @typedef {Object} ShineResult
|
5
|
+
* @property {boolean} ok - Indicates if the operation was successful
|
6
|
+
* @property {string} message - A descriptive message about the operation result
|
7
|
+
* @property {number} [timestamp] - The timestamp of the verified data (if applicable)
|
8
|
+
* @property {string} [updater] - The address of the last updater (if applicable)
|
9
|
+
* @property {Object} [latestRecord] - The latest record from the blockchain (if applicable)
|
10
|
+
* @property {string} [nodeId] - The ID of the newly created node (for write operations)
|
11
|
+
* @property {string} [txHash] - The transaction hash (for write operations)
|
12
|
+
*/
|
13
|
+
|
14
|
+
/**
|
15
|
+
* Creates a SHINE (Secure Hybrid Information and Node Ecosystem) plugin for Gun
|
16
|
+
* @param {any} Gun - The Gun instance
|
17
|
+
* @param {any} ethers - The ethers.js library
|
18
|
+
* @param {Function} getSigner - Function to get the signer
|
19
|
+
* @param {Function} getProvider - Function to get the provider
|
20
|
+
* @returns {Function} The SHINE plugin function
|
21
|
+
*/
|
22
|
+
export function shine(Gun, ethers, getSigner, getProvider) {
|
23
|
+
/**
|
24
|
+
* The SHINE plugin function
|
25
|
+
* @param {string} chain - The blockchain network to use
|
26
|
+
* @param {string} [nodeId] - The ID of the node to verify (for read operations)
|
27
|
+
* @param {Object} [data] - The data to write (for write operations)
|
28
|
+
* @param {Function} callback - The callback function to handle the result
|
29
|
+
* @returns {Object} The Gun instance
|
30
|
+
* @this {any}
|
31
|
+
*/
|
32
|
+
|
33
|
+
return function (chain, nodeId, data = null, callback = () => {}) {
|
34
|
+
console.log("SHINE plugin called with:", { chain, nodeId, data });
|
35
|
+
|
36
|
+
if (typeof callback !== "function") {
|
37
|
+
callback = () => {};
|
38
|
+
console.error("Callback must be a function");
|
39
|
+
return this;
|
40
|
+
}
|
41
|
+
|
42
|
+
const gun = this;
|
43
|
+
let SHINE_CONTRACT_ADDRESS;
|
44
|
+
|
45
|
+
if (chain === "optimismSepolia") {
|
46
|
+
SHINE_CONTRACT_ADDRESS = SHINE_OPTIMISM_SEPOLIA;
|
47
|
+
} else {
|
48
|
+
throw new Error("Chain not supported");
|
49
|
+
}
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Verifies data on-chain
|
53
|
+
* @param {string} nodeId - The ID of the node to verify
|
54
|
+
* @param {string} contentHash - The content hash to verify
|
55
|
+
* @returns {Promise<{isValid: boolean, timestamp: number, updater: string}>} The verification result
|
56
|
+
*/
|
57
|
+
const verifyOnChain = async (nodeId, contentHash) => {
|
58
|
+
console.log("Verifying on chain:", { nodeId, contentHash });
|
59
|
+
const signer = await getSigner();
|
60
|
+
const contract = new ethers.Contract(
|
61
|
+
SHINE_CONTRACT_ADDRESS,
|
62
|
+
SHINE_ABI,
|
63
|
+
signer
|
64
|
+
);
|
65
|
+
const [isValid, timestamp, updater] = await contract.verifyData(
|
66
|
+
ethers.toUtf8Bytes(nodeId),
|
67
|
+
contentHash
|
68
|
+
);
|
69
|
+
console.log("Verification result:", { isValid, timestamp, updater });
|
70
|
+
return { isValid, timestamp, updater };
|
71
|
+
};
|
72
|
+
|
73
|
+
/**
|
74
|
+
* Writes data on-chain
|
75
|
+
* @param {string} nodeId - The ID of the node to write
|
76
|
+
* @param {string} contentHash - The content hash to write
|
77
|
+
* @returns {Promise<any>} The transaction object
|
78
|
+
*/
|
79
|
+
const writeOnChain = async (nodeId, contentHash) => {
|
80
|
+
console.log("Writing on chain:", { nodeId, contentHash });
|
81
|
+
const signer = await getSigner();
|
82
|
+
const contract = new ethers.Contract(
|
83
|
+
SHINE_CONTRACT_ADDRESS,
|
84
|
+
SHINE_ABI,
|
85
|
+
signer
|
86
|
+
);
|
87
|
+
const tx = await contract.updateData(
|
88
|
+
ethers.toUtf8Bytes(nodeId),
|
89
|
+
contentHash
|
90
|
+
);
|
91
|
+
console.log("Transaction sent:", tx.hash);
|
92
|
+
const receipt = await tx.wait();
|
93
|
+
console.log("Transaction confirmed:", receipt);
|
94
|
+
return tx;
|
95
|
+
};
|
96
|
+
|
97
|
+
/**
|
98
|
+
* Gets the latest record from the blockchain
|
99
|
+
* @param {string} nodeId - The ID of the node to retrieve
|
100
|
+
* @returns {Promise<{contentHash: string, timestamp: number, updater: string}>} The latest record
|
101
|
+
*/
|
102
|
+
const getLatestRecord = async (nodeId) => {
|
103
|
+
const signer = await getSigner();
|
104
|
+
const contract = new ethers.Contract(
|
105
|
+
SHINE_CONTRACT_ADDRESS,
|
106
|
+
SHINE_ABI,
|
107
|
+
signer
|
108
|
+
);
|
109
|
+
const [contentHash, timestamp, updater] = await contract.getLatestRecord(
|
110
|
+
ethers.toUtf8Bytes(nodeId)
|
111
|
+
);
|
112
|
+
console.log("Latest record from blockchain:", {
|
113
|
+
nodeId,
|
114
|
+
contentHash,
|
115
|
+
timestamp,
|
116
|
+
updater,
|
117
|
+
});
|
118
|
+
return { contentHash, timestamp, updater };
|
119
|
+
};
|
120
|
+
|
121
|
+
// SHINE process
|
122
|
+
if (nodeId && !data) {
|
123
|
+
gun.get(nodeId).once(async (existingData) => {
|
124
|
+
if (!existingData) {
|
125
|
+
if (callback) callback({ err: "Node not found in GunDB" });
|
126
|
+
return;
|
127
|
+
}
|
128
|
+
|
129
|
+
console.log("existingData", existingData);
|
130
|
+
|
131
|
+
const contentHash = existingData._contentHash;
|
132
|
+
console.log("contentHash", contentHash);
|
133
|
+
|
134
|
+
if (!contentHash) {
|
135
|
+
if (callback)
|
136
|
+
callback({ err: "No content hash found for this node" });
|
137
|
+
return;
|
138
|
+
}
|
139
|
+
|
140
|
+
try {
|
141
|
+
const { isValid, timestamp, updater } = await verifyOnChain(
|
142
|
+
nodeId,
|
143
|
+
contentHash
|
144
|
+
);
|
145
|
+
const latestRecord = await getLatestRecord(nodeId);
|
146
|
+
|
147
|
+
if (isValid) {
|
148
|
+
if (callback)
|
149
|
+
callback({
|
150
|
+
ok: true,
|
151
|
+
message: "Data verified on blockchain",
|
152
|
+
timestamp,
|
153
|
+
updater,
|
154
|
+
latestRecord,
|
155
|
+
});
|
156
|
+
} else {
|
157
|
+
if (callback)
|
158
|
+
callback({
|
159
|
+
ok: false,
|
160
|
+
message: "Data not verified on blockchain",
|
161
|
+
latestRecord,
|
162
|
+
});
|
163
|
+
}
|
164
|
+
} catch (error) {
|
165
|
+
if (callback) callback({ err: error.message });
|
166
|
+
}
|
167
|
+
});
|
168
|
+
} else if (data && !nodeId) {
|
169
|
+
const newNodeId = Gun.text.random();
|
170
|
+
const dataString = JSON.stringify(data);
|
171
|
+
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(dataString));
|
172
|
+
|
173
|
+
gun
|
174
|
+
.get(newNodeId)
|
175
|
+
.put({ ...data, _contentHash: contentHash }, async (ack) => {
|
176
|
+
console.log("ack", ack);
|
177
|
+
if (ack.err) {
|
178
|
+
if (callback) callback({ err: "Error saving data to GunDB" });
|
179
|
+
return;
|
180
|
+
}
|
181
|
+
|
182
|
+
try {
|
183
|
+
const tx = await writeOnChain(newNodeId, contentHash);
|
184
|
+
if (callback)
|
185
|
+
callback({
|
186
|
+
ok: true,
|
187
|
+
message: "Data written to GunDB and blockchain",
|
188
|
+
nodeId: newNodeId,
|
189
|
+
txHash: tx.hash,
|
190
|
+
});
|
191
|
+
} catch (error) {
|
192
|
+
if (callback) callback({ err: error.message });
|
193
|
+
}
|
194
|
+
});
|
195
|
+
} else {
|
196
|
+
if (callback)
|
197
|
+
callback({
|
198
|
+
err: "Invalid input. Provide either nodeId or data, not both.",
|
199
|
+
});
|
200
|
+
}
|
201
|
+
|
202
|
+
return gun;
|
203
|
+
};
|
204
|
+
}
|
@@ -0,0 +1,92 @@
|
|
1
|
+
import { SEA } from "gun";
|
2
|
+
import { gun } from "../../state/gun";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Genera un certificato per le richieste di amicizia.
|
6
|
+
* @param {Function} callback - Funzione di callback chiamata al completamento dell'operazione.
|
7
|
+
* @returns {Promise<void>}
|
8
|
+
*/
|
9
|
+
let generateFriendRequestsCertificate = async (callback = () => {}) => {
|
10
|
+
let certificateExists = await gun
|
11
|
+
.user()
|
12
|
+
.get("certificates")
|
13
|
+
.get("friendRequests")
|
14
|
+
.once();
|
15
|
+
|
16
|
+
if (certificateExists) return;
|
17
|
+
|
18
|
+
let certificate = await SEA.certify(
|
19
|
+
["*"],
|
20
|
+
[{ "*": "friendRequests" }],
|
21
|
+
await gun.user().pair(),
|
22
|
+
null
|
23
|
+
);
|
24
|
+
|
25
|
+
gun
|
26
|
+
.user()
|
27
|
+
.get("certificates")
|
28
|
+
.get("friendRequests")
|
29
|
+
.put(certificate, ({ err }) => {
|
30
|
+
if (err)
|
31
|
+
return callback({
|
32
|
+
errMessage: err,
|
33
|
+
errCode: "gun-put-error",
|
34
|
+
success: undefined,
|
35
|
+
});
|
36
|
+
else
|
37
|
+
return callback({
|
38
|
+
errMessage: undefined,
|
39
|
+
errCode: undefined,
|
40
|
+
certificate,
|
41
|
+
success: "Generated new friend requests certificate.",
|
42
|
+
});
|
43
|
+
});
|
44
|
+
};
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Genera un certificato per aggiungere un amico.
|
48
|
+
* @param {string} publicKey - Chiave pubblica dell'amico da aggiungere.
|
49
|
+
* @param {Function} callback - Funzione di callback chiamata al completamento dell'operazione.
|
50
|
+
* @returns {Promise<void>}
|
51
|
+
*/
|
52
|
+
let generateAddFriendCertificate = async (publicKey, callback = () => {}) => {
|
53
|
+
let certificateExists = await gun
|
54
|
+
.user()
|
55
|
+
.get("certificates")
|
56
|
+
.get(publicKey)
|
57
|
+
.get("addFriend")
|
58
|
+
.once();
|
59
|
+
|
60
|
+
if (certificateExists) return;
|
61
|
+
|
62
|
+
let certificate = await SEA.certify(
|
63
|
+
[publicKey],
|
64
|
+
[{ "*": "friends" }],
|
65
|
+
await gun.user().pair(),
|
66
|
+
null
|
67
|
+
);
|
68
|
+
|
69
|
+
gun
|
70
|
+
.user()
|
71
|
+
.get("certificates")
|
72
|
+
.get(publicKey)
|
73
|
+
.get("addFriend")
|
74
|
+
.put(certificate, ({ err }) => {
|
75
|
+
if (err)
|
76
|
+
return callback({
|
77
|
+
errMessage: err,
|
78
|
+
errCode: "gun-put-error",
|
79
|
+
success: undefined,
|
80
|
+
});
|
81
|
+
else
|
82
|
+
return callback({
|
83
|
+
errMessage: undefined,
|
84
|
+
errCode: undefined,
|
85
|
+
certificate,
|
86
|
+
success:
|
87
|
+
"Generated certificate for requested friend to add user back.",
|
88
|
+
});
|
89
|
+
});
|
90
|
+
};
|
91
|
+
|
92
|
+
export { generateFriendRequestsCertificate, generateAddFriendCertificate };
|
@@ -0,0 +1,44 @@
|
|
1
|
+
/**
|
2
|
+
* Modulo per la gestione dei certificati nell'applicazione.
|
3
|
+
* @module certificates
|
4
|
+
*/
|
5
|
+
|
6
|
+
import {
|
7
|
+
generateAddFriendCertificate,
|
8
|
+
generateFriendRequestsCertificate
|
9
|
+
} from "./friendsCertificates";
|
10
|
+
import {
|
11
|
+
createChatsCertificate,
|
12
|
+
createMessagesCertificate
|
13
|
+
} from "./messagingCertificates";
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Genera un certificato per aggiungere un amico.
|
17
|
+
* @function
|
18
|
+
* @param {string} publicKey - Chiave pubblica dell'amico da aggiungere.
|
19
|
+
* @param {Function} [callback] - Funzione di callback opzionale.
|
20
|
+
*/
|
21
|
+
export { generateAddFriendCertificate };
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Genera un certificato per le richieste di amicizia.
|
25
|
+
* @function
|
26
|
+
* @param {Function} [callback] - Funzione di callback opzionale.
|
27
|
+
*/
|
28
|
+
export { generateFriendRequestsCertificate };
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Crea un certificato per le chat con un utente specifico.
|
32
|
+
* @function
|
33
|
+
* @param {string} publicKey - Chiave pubblica dell'utente.
|
34
|
+
* @param {Function} [callback] - Funzione di callback opzionale.
|
35
|
+
*/
|
36
|
+
export { createChatsCertificate };
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Crea un certificato per i messaggi con un utente specifico.
|
40
|
+
* @function
|
41
|
+
* @param {string} publicKey - Chiave pubblica dell'utente.
|
42
|
+
* @param {Function} [callback] - Funzione di callback opzionale.
|
43
|
+
*/
|
44
|
+
export { createMessagesCertificate };
|
@@ -0,0 +1,94 @@
|
|
1
|
+
import { SEA } from "gun";
|
2
|
+
import { gun } from "../../state/gun";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Crea un certificato per le chat con un utente specifico.
|
6
|
+
* @param {string} publicKey - La chiave pubblica dell'utente per cui creare il certificato.
|
7
|
+
* @param {function} [callback] - Funzione di callback opzionale chiamata al completamento dell'operazione.
|
8
|
+
* @returns {Promise<void>}
|
9
|
+
*/
|
10
|
+
let createChatsCertificate = async (publicKey, callback = () => {}) => {
|
11
|
+
let certificateExists = await gun
|
12
|
+
.user()
|
13
|
+
.get("certificates")
|
14
|
+
.get(publicKey)
|
15
|
+
.get("chats")
|
16
|
+
.once();
|
17
|
+
|
18
|
+
if (certificateExists) return;
|
19
|
+
|
20
|
+
let certificate = await SEA.certify(
|
21
|
+
[publicKey],
|
22
|
+
[{ "*": "chats" }],
|
23
|
+
await gun.user().pair(),
|
24
|
+
null
|
25
|
+
);
|
26
|
+
|
27
|
+
gun
|
28
|
+
.user()
|
29
|
+
.get("certificates")
|
30
|
+
.get(publicKey)
|
31
|
+
.get("chats")
|
32
|
+
.put(certificate, ({ err }) => {
|
33
|
+
if (err)
|
34
|
+
return callback({
|
35
|
+
errMessage: err,
|
36
|
+
errCode: "chats-certificate-creation-error",
|
37
|
+
success: undefined,
|
38
|
+
});
|
39
|
+
else
|
40
|
+
return callback({
|
41
|
+
errMessage: undefined,
|
42
|
+
errCode: undefined,
|
43
|
+
certificate,
|
44
|
+
success: "Generated new chats certificate.",
|
45
|
+
});
|
46
|
+
});
|
47
|
+
};
|
48
|
+
|
49
|
+
/**
|
50
|
+
* Crea un certificato per i messaggi con un utente specifico.
|
51
|
+
* @param {string} publicKey - La chiave pubblica dell'utente per cui creare il certificato.
|
52
|
+
* @param {function} [callback] - Funzione di callback opzionale chiamata al completamento dell'operazione.
|
53
|
+
* @returns {Promise<void>}
|
54
|
+
*/
|
55
|
+
let createMessagesCertificate = async (publicKey, callback = () => {}) => {
|
56
|
+
let certificateExists = await gun
|
57
|
+
.user()
|
58
|
+
.get("certificates")
|
59
|
+
.get(publicKey)
|
60
|
+
.get("messages")
|
61
|
+
.once();
|
62
|
+
|
63
|
+
if (certificateExists) return;
|
64
|
+
|
65
|
+
let certificate = await SEA.certify(
|
66
|
+
[publicKey],
|
67
|
+
[{ "*": "messages" }],
|
68
|
+
await gun.user().pair(),
|
69
|
+
null
|
70
|
+
);
|
71
|
+
|
72
|
+
gun
|
73
|
+
.user()
|
74
|
+
.get("certificates")
|
75
|
+
.get(publicKey)
|
76
|
+
.get("messages")
|
77
|
+
.put(certificate, ({ err }) => {
|
78
|
+
if (err)
|
79
|
+
return callback({
|
80
|
+
errMessage: err,
|
81
|
+
errCode: "messages-certificate-creation-error",
|
82
|
+
success: undefined,
|
83
|
+
});
|
84
|
+
else
|
85
|
+
return callback({
|
86
|
+
errMessage: undefined,
|
87
|
+
errCode: undefined,
|
88
|
+
certificate,
|
89
|
+
success: "Generated new messages certificate.",
|
90
|
+
});
|
91
|
+
});
|
92
|
+
};
|
93
|
+
|
94
|
+
export { createChatsCertificate, createMessagesCertificate };
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import { gun, user } from "../../state/gun";
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Accetta una richiesta di amicizia.
|
5
|
+
* @param {Object} params - Parametri della funzione.
|
6
|
+
* @param {string} params.key - Chiave della richiesta di amicizia.
|
7
|
+
* @param {string} params.publicKey - Chiave pubblica dell'utente che ha inviato la richiesta.
|
8
|
+
* @param {Function} [callback] - Funzione di callback opzionale.
|
9
|
+
* @returns {void}
|
10
|
+
*/
|
11
|
+
let acceptFriendRequest = ({ key, publicKey }, callback = () => {}) => {
|
12
|
+
gun
|
13
|
+
.user()
|
14
|
+
.get("friendRequests")
|
15
|
+
.get(key)
|
16
|
+
.put(null, async ({ err }) => {
|
17
|
+
if (err)
|
18
|
+
return callback({
|
19
|
+
errMessage: err,
|
20
|
+
errCode: "accept-friend-request-failed",
|
21
|
+
success: undefined,
|
22
|
+
});
|
23
|
+
else {
|
24
|
+
let addFriendCertificate = await gun
|
25
|
+
.user(publicKey)
|
26
|
+
.get("certificates")
|
27
|
+
.get(user.is.pub)
|
28
|
+
.get("addFriend");
|
29
|
+
|
30
|
+
gun
|
31
|
+
.user(publicKey)
|
32
|
+
.get("friends")
|
33
|
+
.set(
|
34
|
+
user.is.pub,
|
35
|
+
({ err }) => {
|
36
|
+
if (err)
|
37
|
+
return callback({
|
38
|
+
errMessage: err,
|
39
|
+
errCode: "add-friend-failed",
|
40
|
+
success: undefined,
|
41
|
+
});
|
42
|
+
else
|
43
|
+
gun
|
44
|
+
.user()
|
45
|
+
.get("friends")
|
46
|
+
.set(publicKey, ({ err }) => {
|
47
|
+
if (err)
|
48
|
+
return callback({
|
49
|
+
errMessage: err,
|
50
|
+
errCode: "add-friend-failed",
|
51
|
+
success: undefined,
|
52
|
+
});
|
53
|
+
else
|
54
|
+
return callback({
|
55
|
+
errMessage: undefined,
|
56
|
+
errCode: undefined,
|
57
|
+
success: "Added friend successfully.",
|
58
|
+
});
|
59
|
+
});
|
60
|
+
},
|
61
|
+
{
|
62
|
+
opt: { cert: addFriendCertificate },
|
63
|
+
}
|
64
|
+
);
|
65
|
+
}
|
66
|
+
});
|
67
|
+
};
|
68
|
+
|
69
|
+
export default acceptFriendRequest;
|