gun-eth 2.0.1 → 3.0.1
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 +320 -141
- package/dist/gun-eth.bundle.js +6809 -0
- package/dist/gun-eth.cjs +2468 -0
- package/dist/types/browser.d.ts +6 -0
- package/dist/types/config/local.d.ts +7 -0
- package/dist/types/constants/abis.d.ts +37 -0
- package/dist/types/constants/index.d.ts +1 -0
- package/dist/types/core/gun-eth.d.ts +419 -0
- package/dist/types/features/bubbles/client/bubble-client.d.ts +184 -0
- package/dist/types/features/bubbles/providers/base-bubble-provider.d.ts +303 -0
- package/dist/types/features/bubbles/providers/gun-bubble-provider.d.ts +173 -0
- package/dist/types/features/bubbles/providers/hybrid-bubble-provider.d.ts +124 -0
- package/dist/types/features/proof/ProofChain.d.ts +225 -0
- package/dist/types/features/stealth/StealthChain.d.ts +200 -0
- package/dist/types/index.d.ts +61 -0
- package/dist/types/utils/common.d.ts +11 -0
- package/dist/types/utils/encryption.d.ts +32 -0
- package/package.json +110 -26
- package/dist/gun-eth-protocol.cjs.js +0 -11528
- package/dist/gun-eth-protocol.esm.js +0 -11503
- package/dist/gun-eth-protocol.js +0 -18
- package/dist/gun-eth-protocol.react.js +0 -11503
- package/dist/gun-eth-protocol.umd.js +0 -18
- package/jsdoc.json +0 -7
- package/rollup.config.js +0 -80
- package/src/index.js +0 -181
- package/src/lib/authentication/index.js +0 -13
- package/src/lib/authentication/isAuthenticated.js +0 -20
- package/src/lib/authentication/login.js +0 -25
- package/src/lib/authentication/register.js +0 -58
- package/src/lib/blockchain/abis/SHINE.json +0 -262
- package/src/lib/blockchain/contracts/SHINE.sol +0 -52
- package/src/lib/blockchain/ethereum.js +0 -74
- package/src/lib/blockchain/shine.js +0 -204
- package/src/lib/certificates/friendsCertificates.js +0 -92
- package/src/lib/certificates/index.js +0 -44
- package/src/lib/certificates/messagingCertificates.js +0 -94
- package/src/lib/friends/acceptFriendRequest.js +0 -69
- package/src/lib/friends/addFriendRequest.js +0 -49
- package/src/lib/friends/friendRequests.js +0 -51
- package/src/lib/friends/friendsList.js +0 -57
- package/src/lib/friends/index.js +0 -36
- package/src/lib/friends/rejectFriendRequest.js +0 -31
- package/src/lib/messaging/chatsList.js +0 -42
- package/src/lib/messaging/createChat.js +0 -132
- package/src/lib/messaging/index.js +0 -36
- package/src/lib/messaging/messageList.js +0 -106
- package/src/lib/messaging/sendMessage.js +0 -132
- package/src/lib/messaging/sendVoiceMessage.js +0 -119
- package/src/lib/notes/createNote.js +0 -41
- package/src/lib/notes/deleteNote.js +0 -12
- package/src/lib/notes/getNote.js +0 -25
- package/src/lib/notes/getUserNote.js +0 -59
- package/src/lib/notes/index.js +0 -8
- package/src/lib/notes/updateNotes.js +0 -35
- package/src/lib/post/createPost.js +0 -17
- package/src/lib/post/decryptPost.js +0 -14
- package/src/lib/post/deletePost.js +0 -13
- package/src/lib/post/encryptPost,js +0 -16
- package/src/lib/post/getPost.js +0 -36
- package/src/lib/post/index.js +0 -9
- package/src/lib/post/updatePost.js +0 -16
- package/src/state/gun.js +0 -33
- package/types/types.d.ts +0 -244
@@ -1,74 +0,0 @@
|
|
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
|
-
}
|
@@ -1,204 +0,0 @@
|
|
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
|
-
}
|
@@ -1,92 +0,0 @@
|
|
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 };
|
@@ -1,44 +0,0 @@
|
|
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 };
|
@@ -1,94 +0,0 @@
|
|
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 };
|
@@ -1,69 +0,0 @@
|
|
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;
|
@@ -1,49 +0,0 @@
|
|
1
|
-
import { gun, user } from "../../state/gun";
|
2
|
-
import { generateAddFriendCertificate } from "../certificates";
|
3
|
-
|
4
|
-
/**
|
5
|
-
* Invia una richiesta di amicizia a un utente specifico.
|
6
|
-
*
|
7
|
-
* @async
|
8
|
-
* @param {string} publicKey - La chiave pubblica dell'utente a cui inviare la richiesta di amicizia.
|
9
|
-
* @param {Function} [callback] - Funzione di callback opzionale chiamata al completamento dell'operazione.
|
10
|
-
* @returns {Promise<void>}
|
11
|
-
*/
|
12
|
-
let addFriendRequest = async (publicKey, callback = () => {}) => {
|
13
|
-
let addFriendRequestCertificate = await gun
|
14
|
-
.user(publicKey)
|
15
|
-
.get("certificates")
|
16
|
-
.get("friendRequests");
|
17
|
-
|
18
|
-
gun
|
19
|
-
.user(publicKey)
|
20
|
-
.get("friendRequests")
|
21
|
-
.set(
|
22
|
-
user.is.pub,
|
23
|
-
({ err }) => {
|
24
|
-
if (err)
|
25
|
-
return callback({
|
26
|
-
errMessage: err,
|
27
|
-
errCode: "friend-request-error",
|
28
|
-
success: undefined,
|
29
|
-
});
|
30
|
-
else {
|
31
|
-
generateAddFriendCertificate(
|
32
|
-
publicKey,
|
33
|
-
({ errMessage, errCode, success }) => {
|
34
|
-
if (errMessage) return callback({ errMessage, errCode, success });
|
35
|
-
else
|
36
|
-
return callback({
|
37
|
-
errMessage: undefined,
|
38
|
-
errCode: undefined,
|
39
|
-
success: "Friend request sent successfully.",
|
40
|
-
});
|
41
|
-
}
|
42
|
-
);
|
43
|
-
}
|
44
|
-
},
|
45
|
-
{ opt: { cert: addFriendRequestCertificate } }
|
46
|
-
);
|
47
|
-
};
|
48
|
-
|
49
|
-
export default addFriendRequest;
|
@@ -1,51 +0,0 @@
|
|
1
|
-
import { Observable } from "rxjs";
|
2
|
-
import { gun } from "../../state/gun";
|
3
|
-
|
4
|
-
/**
|
5
|
-
* Observable che gestisce le richieste di amicizia dell'utente corrente.
|
6
|
-
*
|
7
|
-
* @type {Observable}
|
8
|
-
* @description Questo Observable emette oggetti contenenti informazioni sulle richieste di amicizia.
|
9
|
-
* Ogni oggetto emesso può avere le seguenti proprietà:
|
10
|
-
* - key: La chiave della richiesta di amicizia
|
11
|
-
* - pub: La chiave pubblica dell'utente che ha inviato la richiesta
|
12
|
-
* - alias: L'alias dell'utente che ha inviato la richiesta
|
13
|
-
* - displayName: Il nome visualizzato dell'utente (se disponibile)
|
14
|
-
* - about: Informazioni aggiuntive sull'utente (se disponibili)
|
15
|
-
*
|
16
|
-
* Se non ci sono richieste di amicizia, l'Observable emetterà `undefined`.
|
17
|
-
*/
|
18
|
-
let friendRequests = new Observable((subscriber) => {
|
19
|
-
gun
|
20
|
-
.user()
|
21
|
-
.get("friendRequests")
|
22
|
-
.map(
|
23
|
-
(publicKey, key) => {
|
24
|
-
if (publicKey) {
|
25
|
-
gun.user(publicKey).once((_user) => {
|
26
|
-
if (_user && _user.info && _user.pub && _user.alias) {
|
27
|
-
gun.get(_user.info["#"]).on((data) => {
|
28
|
-
subscriber.next({
|
29
|
-
key,
|
30
|
-
pub: _user.pub,
|
31
|
-
alias: _user.alias,
|
32
|
-
displayName: data.displayName,
|
33
|
-
about: data.about || undefined,
|
34
|
-
});
|
35
|
-
});
|
36
|
-
} else if (_user && _user.pub && _user.alias) {
|
37
|
-
subscriber.next({
|
38
|
-
key,
|
39
|
-
pub: _user.pub,
|
40
|
-
alias: _user.alias,
|
41
|
-
});
|
42
|
-
}
|
43
|
-
});
|
44
|
-
} else {
|
45
|
-
subscriber.next(undefined);
|
46
|
-
}
|
47
|
-
}
|
48
|
-
);
|
49
|
-
});
|
50
|
-
|
51
|
-
export default friendRequests;
|