gun-eth 2.0.1 → 3.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 +320 -141
- package/dist/gun-eth.bundle.js +6806 -0
- package/dist/gun-eth.cjs +2465 -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,57 +0,0 @@
|
|
1
|
-
import { Observable } from "rxjs";
|
2
|
-
import { gun } from "../../state/gun";
|
3
|
-
|
4
|
-
/**
|
5
|
-
* Modulo per la gestione della lista degli amici.
|
6
|
-
* @module friendsList
|
7
|
-
*/
|
8
|
-
|
9
|
-
/**
|
10
|
-
* Observable che gestisce la lista degli amici dell'utente corrente.
|
11
|
-
*
|
12
|
-
* @type {Observable}
|
13
|
-
* @description Questo Observable emette oggetti contenenti informazioni sugli amici dell'utente.
|
14
|
-
* Ogni oggetto emesso può avere le seguenti proprietà:
|
15
|
-
* - key: La chiave dell'amico
|
16
|
-
* - pub: La chiave pubblica dell'amico
|
17
|
-
* - alias: L'alias dell'amico
|
18
|
-
* - displayName: Il nome visualizzato dell'amico (se disponibile)
|
19
|
-
* - about: Informazioni aggiuntive sull'amico (se disponibili)
|
20
|
-
*
|
21
|
-
* Se non ci sono amici nella lista, l'Observable emetterà `undefined`.
|
22
|
-
*/
|
23
|
-
|
24
|
-
let friendsList = new Observable((subscriber) => {
|
25
|
-
gun
|
26
|
-
.user()
|
27
|
-
.get("friends")
|
28
|
-
.map(
|
29
|
-
(publicKey, key) => {
|
30
|
-
if (publicKey) {
|
31
|
-
gun.user(publicKey).once((_user) => {
|
32
|
-
if (_user && _user.info && _user.pub && _user.alias) {
|
33
|
-
gun.get(_user.info["#"]).on((data) => {
|
34
|
-
subscriber.next({
|
35
|
-
key,
|
36
|
-
pub: _user.pub,
|
37
|
-
alias: _user.alias,
|
38
|
-
displayName: data.displayName,
|
39
|
-
about: data.about || undefined,
|
40
|
-
});
|
41
|
-
});
|
42
|
-
} else if (_user && _user.pub && _user.alias) {
|
43
|
-
subscriber.next({
|
44
|
-
key,
|
45
|
-
pub: _user.pub,
|
46
|
-
alias: _user.alias,
|
47
|
-
});
|
48
|
-
}
|
49
|
-
});
|
50
|
-
} else {
|
51
|
-
subscriber.next(undefined);
|
52
|
-
}
|
53
|
-
}
|
54
|
-
);
|
55
|
-
});
|
56
|
-
|
57
|
-
export default friendsList;
|
package/src/lib/friends/index.js
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Modulo per la gestione delle amicizie
|
3
|
-
* @module friends
|
4
|
-
*/
|
5
|
-
|
6
|
-
import acceptFriendRequest from "./acceptFriendRequest";
|
7
|
-
import addFriendRequest from "./addFriendRequest";
|
8
|
-
import friendRequests from "./friendRequests";
|
9
|
-
import rejectFriendRequest from "./rejectFriendRequest";
|
10
|
-
import friendsList from "./friendsList";
|
11
|
-
|
12
|
-
/**
|
13
|
-
* Funzioni esportate per la gestione delle amicizie
|
14
|
-
*/
|
15
|
-
export {
|
16
|
-
/**
|
17
|
-
* Aggiunge una richiesta di amicizia
|
18
|
-
*/
|
19
|
-
addFriendRequest,
|
20
|
-
/**
|
21
|
-
* Accetta una richiesta di amicizia
|
22
|
-
*/
|
23
|
-
acceptFriendRequest,
|
24
|
-
/**
|
25
|
-
* Rifiuta una richiesta di amicizia
|
26
|
-
*/
|
27
|
-
rejectFriendRequest,
|
28
|
-
/**
|
29
|
-
* Ottiene l'elenco delle richieste di amicizia
|
30
|
-
*/
|
31
|
-
friendRequests,
|
32
|
-
/**
|
33
|
-
* Ottiene l'elenco degli amici
|
34
|
-
*/
|
35
|
-
friendsList
|
36
|
-
};
|
@@ -1,31 +0,0 @@
|
|
1
|
-
import { gun } from "../../state/gun";
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Rifiuta una richiesta di amicizia.
|
5
|
-
*
|
6
|
-
* @param {string} key - La chiave della richiesta di amicizia da rifiutare.
|
7
|
-
* @param {Function} [callback] - Funzione di callback opzionale chiamata al completamento dell'operazione.
|
8
|
-
* @returns {void}
|
9
|
-
*/
|
10
|
-
let rejectFriendRequest = (key, callback = () => {}) => {
|
11
|
-
gun
|
12
|
-
.user()
|
13
|
-
.get("friendRequests")
|
14
|
-
.get(key)
|
15
|
-
.put(null, async ({ err }) => {
|
16
|
-
if (err)
|
17
|
-
return callback({
|
18
|
-
errMessage: err,
|
19
|
-
errCode: "reject-friend-request-failed",
|
20
|
-
success: undefined,
|
21
|
-
});
|
22
|
-
else
|
23
|
-
return callback({
|
24
|
-
errMessage: undefined,
|
25
|
-
errCode: undefined,
|
26
|
-
success: "Friend request removed successfully.",
|
27
|
-
});
|
28
|
-
});
|
29
|
-
};
|
30
|
-
|
31
|
-
export default rejectFriendRequest;
|
@@ -1,42 +0,0 @@
|
|
1
|
-
import { Observable } from "rxjs";
|
2
|
-
import { gun } from "../../state/gun";
|
3
|
-
|
4
|
-
/**
|
5
|
-
* Observable che fornisce una lista di chat aggiornata in tempo reale.
|
6
|
-
*
|
7
|
-
* @type {Observable}
|
8
|
-
* @description Questo Observable si sottoscrive ai cambiamenti nella lista delle chat dell'utente
|
9
|
-
* e emette un nuovo oggetto per ogni chat aggiornata. Ogni oggetto chat contiene:
|
10
|
-
* - roomId: l'identificatore univoco della stanza di chat
|
11
|
-
* - pub: la chiave pubblica associata alla chat
|
12
|
-
* - latestMessage: l'ultimo messaggio nella chat
|
13
|
-
*
|
14
|
-
* @example
|
15
|
-
* chatsList.subscribe(chat => {
|
16
|
-
* console.log('Chat aggiornata:', chat);
|
17
|
-
* });
|
18
|
-
*/
|
19
|
-
let chatsList = new Observable((subscriber) => {
|
20
|
-
gun
|
21
|
-
.user()
|
22
|
-
.get("chats")
|
23
|
-
.on((chats, _) => {
|
24
|
-
for (let publicKey in chats) {
|
25
|
-
try {
|
26
|
-
let chatDetails = JSON.parse(chats[publicKey]);
|
27
|
-
|
28
|
-
if (chatDetails) {
|
29
|
-
subscriber.next({
|
30
|
-
roomId: chatDetails.roomId,
|
31
|
-
pub: chatDetails.pub,
|
32
|
-
latestMessage: chatDetails.latestMessage,
|
33
|
-
});
|
34
|
-
}
|
35
|
-
} catch (err) {
|
36
|
-
// Gestione silenziosa degli errori di parsing
|
37
|
-
}
|
38
|
-
}
|
39
|
-
});
|
40
|
-
});
|
41
|
-
|
42
|
-
export default chatsList;
|
@@ -1,132 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Modulo per la creazione di una nuova chat.
|
3
|
-
* @module createChat
|
4
|
-
*/
|
5
|
-
|
6
|
-
import { v4 } from "uuid";
|
7
|
-
import { gun } from "../../state/gun";
|
8
|
-
|
9
|
-
/**
|
10
|
-
* Crea una nuova chat con un utente specificato.
|
11
|
-
*
|
12
|
-
* @async
|
13
|
-
* @function createChat
|
14
|
-
* @param {string} publicKey - La chiave pubblica dell'utente con cui creare la chat.
|
15
|
-
* @param {Function} [callback] - Funzione di callback opzionale chiamata al completamento dell'operazione.
|
16
|
-
* @returns {Promise<void>}
|
17
|
-
*
|
18
|
-
* @description
|
19
|
-
* Questa funzione crea una nuova chat tra l'utente corrente e un altro utente specificato dalla sua chiave pubblica.
|
20
|
-
* Verifica prima se la chat esiste già, poi controlla i certificati necessari e infine crea la chat per entrambi gli utenti.
|
21
|
-
*
|
22
|
-
* @example
|
23
|
-
* createChat("chiavePubblicaUtente", (result) => {
|
24
|
-
* if (result.success) {
|
25
|
-
* console.log("Chat creata:", result.chat);
|
26
|
-
* } else {
|
27
|
-
* console.error("Errore:", result.errMessage);
|
28
|
-
* }
|
29
|
-
* });
|
30
|
-
*/
|
31
|
-
let createChat = async (publicKey, callback = () => {}) => {
|
32
|
-
gun
|
33
|
-
.user()
|
34
|
-
.get("chats")
|
35
|
-
.get(publicKey)
|
36
|
-
.once(async (chatExists) => {
|
37
|
-
if (chatExists) {
|
38
|
-
return callback({
|
39
|
-
errMessage: "The chat already exists. Opening it now.",
|
40
|
-
errCode: "chat-already-exists",
|
41
|
-
chat: JSON.parse(chatExists),
|
42
|
-
success: undefined,
|
43
|
-
});
|
44
|
-
}
|
45
|
-
|
46
|
-
let friend = await gun.user(publicKey).once();
|
47
|
-
|
48
|
-
let userPub = await gun.user().pair().pub;
|
49
|
-
|
50
|
-
if (!userPub)
|
51
|
-
return callback({
|
52
|
-
errMessage: "Could not find pub.",
|
53
|
-
errCode: "failed-to-find-pub",
|
54
|
-
success: undefined,
|
55
|
-
});
|
56
|
-
|
57
|
-
if (!friend)
|
58
|
-
return callback({
|
59
|
-
errMessage: "Could not find friend.",
|
60
|
-
errCode: "failed-to-find-friend",
|
61
|
-
success: undefined,
|
62
|
-
});
|
63
|
-
|
64
|
-
let createChatsCertificate = await gun
|
65
|
-
.user(publicKey)
|
66
|
-
.get("certificates")
|
67
|
-
.get(userPub)
|
68
|
-
.get("chats");
|
69
|
-
|
70
|
-
if (!createChatsCertificate)
|
71
|
-
return callback({
|
72
|
-
errMessage: "Could not find friend certificate to create chat",
|
73
|
-
errCode: "failed-to-find-friend-chats-certificate",
|
74
|
-
success: undefined,
|
75
|
-
});
|
76
|
-
|
77
|
-
let roomId = v4();
|
78
|
-
|
79
|
-
gun
|
80
|
-
.user(publicKey)
|
81
|
-
.get("chats")
|
82
|
-
.get(userPub)
|
83
|
-
.put(
|
84
|
-
JSON.stringify({
|
85
|
-
pub: userPub,
|
86
|
-
roomId,
|
87
|
-
latestMessage: {},
|
88
|
-
}),
|
89
|
-
({ err }) => {
|
90
|
-
if (err)
|
91
|
-
return callback({
|
92
|
-
errMessage: err,
|
93
|
-
errCode: "chat-creation-error",
|
94
|
-
success: undefined,
|
95
|
-
});
|
96
|
-
else
|
97
|
-
gun
|
98
|
-
.user()
|
99
|
-
.get("chats")
|
100
|
-
.get(publicKey)
|
101
|
-
.put(
|
102
|
-
JSON.stringify({
|
103
|
-
pub: friend.pub,
|
104
|
-
roomId,
|
105
|
-
latestMessage: {},
|
106
|
-
}),
|
107
|
-
({ err }) => {
|
108
|
-
if (err)
|
109
|
-
return callback({
|
110
|
-
errMessage: err,
|
111
|
-
errCode: "chat-creation-error",
|
112
|
-
success: undefined,
|
113
|
-
});
|
114
|
-
else
|
115
|
-
return callback({
|
116
|
-
errMessage: undefined,
|
117
|
-
errCode: undefined,
|
118
|
-
chat: {
|
119
|
-
pub: friend.pub,
|
120
|
-
roomId,
|
121
|
-
},
|
122
|
-
success: "Created a chat with friend.",
|
123
|
-
});
|
124
|
-
}
|
125
|
-
);
|
126
|
-
},
|
127
|
-
{ opt: { cert: createChatsCertificate } }
|
128
|
-
);
|
129
|
-
});
|
130
|
-
};
|
131
|
-
|
132
|
-
export default createChat;
|
@@ -1,36 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Modulo per la gestione dei messaggi e delle chat
|
3
|
-
* @module messaging
|
4
|
-
*/
|
5
|
-
|
6
|
-
import chatsList from "./chatsList";
|
7
|
-
import createChat from "./createChat";
|
8
|
-
import messageList from "./messageList";
|
9
|
-
import sendMessage from "./sendMessage";
|
10
|
-
import sendVoiceMessage from "./sendVoiceMessage";
|
11
|
-
|
12
|
-
/**
|
13
|
-
* Funzioni esportate per la gestione dei messaggi e delle chat
|
14
|
-
*/
|
15
|
-
export {
|
16
|
-
/**
|
17
|
-
* Crea una nuova chat
|
18
|
-
*/
|
19
|
-
createChat,
|
20
|
-
/**
|
21
|
-
* Ottiene la lista delle chat
|
22
|
-
*/
|
23
|
-
chatsList,
|
24
|
-
/**
|
25
|
-
* Ottiene la lista dei messaggi di una chat
|
26
|
-
*/
|
27
|
-
messageList,
|
28
|
-
/**
|
29
|
-
* Invia un messaggio di testo
|
30
|
-
*/
|
31
|
-
sendMessage,
|
32
|
-
/**
|
33
|
-
* Invia un messaggio vocale
|
34
|
-
*/
|
35
|
-
sendVoiceMessage
|
36
|
-
};
|
@@ -1,106 +0,0 @@
|
|
1
|
-
import { Observable } from "rxjs";
|
2
|
-
import { gun } from "../../state/gun";
|
3
|
-
|
4
|
-
/**
|
5
|
-
* Crea un Observable che fornisce una lista di messaggi per una specifica chat.
|
6
|
-
*
|
7
|
-
* @function messageList
|
8
|
-
* @param {string} roomId - L'identificatore univoco della stanza di chat.
|
9
|
-
* @param {string} pub - La chiave pubblica dell'altro utente nella chat.
|
10
|
-
* @returns {Observable} Un Observable che emette oggetti contenenti messaggi iniziali e individuali.
|
11
|
-
*
|
12
|
-
* @description
|
13
|
-
* Questa funzione crea un Observable che si sottoscrive ai messaggi di una specifica chat.
|
14
|
-
* Decripta i messaggi utilizzando la chiave pubblica dell'altro utente e emette sia un array iniziale
|
15
|
-
* di messaggi che singoli messaggi man mano che vengono ricevuti.
|
16
|
-
*
|
17
|
-
* @example
|
18
|
-
* const messages$ = messageList('room123', 'publicKey123');
|
19
|
-
* messages$.subscribe(({ initial, individual }) => {
|
20
|
-
* if (initial) {
|
21
|
-
* console.log('Messaggi iniziali:', initial);
|
22
|
-
* } else if (individual) {
|
23
|
-
* console.log('Nuovo messaggio:', individual);
|
24
|
-
* }
|
25
|
-
* });
|
26
|
-
*/
|
27
|
-
let messageList = (roomId, pub) =>
|
28
|
-
new Observable(async (subscriber) => {
|
29
|
-
let userPair = await gun.user()._.sea;
|
30
|
-
let friend = await gun.user(pub);
|
31
|
-
|
32
|
-
gun
|
33
|
-
.user()
|
34
|
-
.get("messages")
|
35
|
-
.get(roomId)
|
36
|
-
.once((messages) => {
|
37
|
-
(async () => {
|
38
|
-
let initial = [];
|
39
|
-
|
40
|
-
for (let key in messages) {
|
41
|
-
let message = messages[key].toString();
|
42
|
-
|
43
|
-
let decryptSecretFriend = await SEA.secret(friend.epub, userPair);
|
44
|
-
let decryptedMessageFriend = await SEA.decrypt(
|
45
|
-
message,
|
46
|
-
decryptSecretFriend
|
47
|
-
);
|
48
|
-
|
49
|
-
if (decryptedMessageFriend) {
|
50
|
-
let individual = {
|
51
|
-
...decryptedMessageFriend,
|
52
|
-
encrypted: true,
|
53
|
-
};
|
54
|
-
|
55
|
-
let exists =
|
56
|
-
initial.filter((current) => current.id === individual.id)[0] !==
|
57
|
-
undefined;
|
58
|
-
|
59
|
-
console.log(exists);
|
60
|
-
|
61
|
-
if (!exists) initial.push(individual);
|
62
|
-
}
|
63
|
-
}
|
64
|
-
|
65
|
-
subscriber.next({ initial, individual: undefined });
|
66
|
-
|
67
|
-
gun
|
68
|
-
.user()
|
69
|
-
.get("messages")
|
70
|
-
.get(roomId)
|
71
|
-
.map()
|
72
|
-
.once(async (message) => {
|
73
|
-
if (message.toString().startsWith("SEA")) {
|
74
|
-
let decryptSecretFriend = await SEA.secret(
|
75
|
-
friend.epub,
|
76
|
-
userPair
|
77
|
-
);
|
78
|
-
let decryptedMessageFriend = await SEA.decrypt(
|
79
|
-
message,
|
80
|
-
decryptSecretFriend
|
81
|
-
);
|
82
|
-
|
83
|
-
if (decryptedMessageFriend) {
|
84
|
-
let individual = {
|
85
|
-
...decryptedMessageFriend,
|
86
|
-
encrypted: true,
|
87
|
-
};
|
88
|
-
|
89
|
-
let exists =
|
90
|
-
initial.filter(
|
91
|
-
(current) => current.id === individual.id
|
92
|
-
)[0] !== undefined;
|
93
|
-
|
94
|
-
if (!exists)
|
95
|
-
return subscriber.next({
|
96
|
-
initial: undefined,
|
97
|
-
individual,
|
98
|
-
});
|
99
|
-
}
|
100
|
-
}
|
101
|
-
});
|
102
|
-
})();
|
103
|
-
});
|
104
|
-
});
|
105
|
-
|
106
|
-
export default messageList;
|
@@ -1,132 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Invia un messaggio crittografato a un amico.
|
3
|
-
*
|
4
|
-
* @param {string} roomId - L'ID della stanza di chat.
|
5
|
-
* @param {string} publicKey - La chiave pubblica del destinatario.
|
6
|
-
* @param {string} message - Il contenuto del messaggio da inviare.
|
7
|
-
* @param {function} callback - Funzione di callback chiamata al completamento dell'operazione.
|
8
|
-
* @returns {void}
|
9
|
-
*/
|
10
|
-
import { SEA } from "gun";
|
11
|
-
import { v4 } from "uuid";
|
12
|
-
import { gun } from "../../state/gun";
|
13
|
-
|
14
|
-
let sendMessage = (roomId, publicKey, message, callback = () => {}) => {
|
15
|
-
(async (callback = () => {}) => {
|
16
|
-
// Ottiene la chiave pubblica e la coppia di chiavi dell'utente corrente
|
17
|
-
let userPub = await gun.user().pair().pub;
|
18
|
-
let userPair = await gun.user()._.sea;
|
19
|
-
let friend = await gun.user(publicKey);
|
20
|
-
|
21
|
-
// Verifica se la chiave pubblica dell'utente è disponibile
|
22
|
-
if (!userPub)
|
23
|
-
return callback({
|
24
|
-
errMessage: "Impossibile trovare la chiave pubblica.",
|
25
|
-
errCode: "failed-to-find-pub",
|
26
|
-
success: undefined,
|
27
|
-
});
|
28
|
-
|
29
|
-
// Ottiene il certificato per creare messaggi
|
30
|
-
let createMessagesCertificate = await gun
|
31
|
-
.user(publicKey)
|
32
|
-
.get("certificates")
|
33
|
-
.get(userPub)
|
34
|
-
.get("messages");
|
35
|
-
|
36
|
-
// Verifica se il certificato per creare messaggi è disponibile
|
37
|
-
if (!createMessagesCertificate)
|
38
|
-
return callback({
|
39
|
-
errMessage: "Impossibile trovare il certificato dell'amico per creare il messaggio",
|
40
|
-
errCode: "failed-to-find-friend-messages-certificate",
|
41
|
-
success: undefined,
|
42
|
-
});
|
43
|
-
|
44
|
-
// Ottiene il certificato per aggiornare i metadati della chat
|
45
|
-
let updateMetaCertificate = await gun
|
46
|
-
.user(publicKey)
|
47
|
-
.get("certificates")
|
48
|
-
.get(userPub)
|
49
|
-
.get("chats");
|
50
|
-
|
51
|
-
// Verifica se il certificato per aggiornare i metadati della chat è disponibile
|
52
|
-
if (!updateMetaCertificate)
|
53
|
-
return callback({
|
54
|
-
errMessage: "Impossibile trovare il certificato dell'amico per aggiungere metadati alla chat",
|
55
|
-
errCode: "failed-to-find-friend-chats-certificate",
|
56
|
-
success: undefined,
|
57
|
-
});
|
58
|
-
|
59
|
-
// Genera un ID univoco per il messaggio e ottiene il timestamp corrente
|
60
|
-
let messageId = v4();
|
61
|
-
let timeSent = Date.now();
|
62
|
-
|
63
|
-
// Crea un segreto condiviso e crittografa il messaggio
|
64
|
-
let secret = await SEA.secret(friend.epub, userPair);
|
65
|
-
let encryptedMessage = await SEA.encrypt(
|
66
|
-
JSON.stringify({
|
67
|
-
id: messageId,
|
68
|
-
content: message,
|
69
|
-
timeSent,
|
70
|
-
sender: userPub,
|
71
|
-
type: "text",
|
72
|
-
}),
|
73
|
-
secret
|
74
|
-
);
|
75
|
-
|
76
|
-
// Aggiorna l'ultimo messaggio nella chat dell'utente corrente
|
77
|
-
gun
|
78
|
-
.user()
|
79
|
-
.get("chats")
|
80
|
-
.get(roomId)
|
81
|
-
.get("latestMessage")
|
82
|
-
.put(encryptedMessage);
|
83
|
-
|
84
|
-
// Aggiorna l'ultimo messaggio nella chat dell'amico
|
85
|
-
gun
|
86
|
-
.user(publicKey)
|
87
|
-
.get("chats")
|
88
|
-
.get(roomId)
|
89
|
-
.get("latestMessage")
|
90
|
-
.put(encryptedMessage, null, { opt: { cert: updateMetaCertificate } });
|
91
|
-
|
92
|
-
// Salva il messaggio crittografato per l'utente corrente
|
93
|
-
gun
|
94
|
-
.user()
|
95
|
-
.get("messages")
|
96
|
-
.get(roomId)
|
97
|
-
.set(encryptedMessage, ({ err }) => {
|
98
|
-
if (err)
|
99
|
-
return callback({
|
100
|
-
errMessage: err,
|
101
|
-
errCode: "message-creation-error",
|
102
|
-
success: undefined,
|
103
|
-
});
|
104
|
-
else
|
105
|
-
// Salva il messaggio crittografato per l'amico
|
106
|
-
gun
|
107
|
-
.user(publicKey)
|
108
|
-
.get("messages")
|
109
|
-
.get(roomId)
|
110
|
-
.set(
|
111
|
-
encryptedMessage,
|
112
|
-
({ err }) => {
|
113
|
-
if (err)
|
114
|
-
return callback({
|
115
|
-
errMessage: err,
|
116
|
-
errCode: "message-creation-error",
|
117
|
-
success: undefined,
|
118
|
-
});
|
119
|
-
else
|
120
|
-
return callback({
|
121
|
-
errMessage: undefined,
|
122
|
-
errCode: undefined,
|
123
|
-
success: "Messaggio creato con successo con l'amico.",
|
124
|
-
});
|
125
|
-
},
|
126
|
-
{ opt: { cert: createMessagesCertificate } }
|
127
|
-
);
|
128
|
-
});
|
129
|
-
})(callback);
|
130
|
-
};
|
131
|
-
|
132
|
-
export default sendMessage;
|
@@ -1,119 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Invia un messaggio vocale a un amico in una chat specifica.
|
3
|
-
*
|
4
|
-
* @param {string} roomId - L'ID della stanza di chat.
|
5
|
-
* @param {string} publicKey - La chiave pubblica dell'amico.
|
6
|
-
* @param {Object} voiceRecording - L'oggetto contenente i dati della registrazione vocale.
|
7
|
-
* @param {Function} [callback] - Funzione di callback opzionale per gestire il risultato dell'operazione.
|
8
|
-
*
|
9
|
-
* @returns {void}
|
10
|
-
*
|
11
|
-
* La funzione esegue le seguenti operazioni:
|
12
|
-
* 1. Verifica la presenza della chiave pubblica dell'utente corrente.
|
13
|
-
* 2. Controlla i certificati necessari per creare messaggi e aggiornare i metadati della chat.
|
14
|
-
* 3. Aggiorna il messaggio più recente nella chat per entrambi gli utenti.
|
15
|
-
* 4. Aggiunge il messaggio vocale alla lista dei messaggi per entrambi gli utenti.
|
16
|
-
*
|
17
|
-
* In caso di errore, la funzione di callback viene chiamata con un oggetto contenente
|
18
|
-
* informazioni sull'errore. In caso di successo, la callback viene chiamata con un
|
19
|
-
* messaggio di conferma.
|
20
|
-
*/
|
21
|
-
import { gun } from "../../state/gun";
|
22
|
-
|
23
|
-
let sendVoiceMessage = (
|
24
|
-
roomId,
|
25
|
-
publicKey,
|
26
|
-
voiceRecording,
|
27
|
-
callback = () => {}
|
28
|
-
) => {
|
29
|
-
(async (callback = () => {}) => {
|
30
|
-
let userPub = await gun.user().pair().pub;
|
31
|
-
let userPair = await gun.user()._.sea;
|
32
|
-
let friend = await gun.user(publicKey);
|
33
|
-
|
34
|
-
if (!userPub)
|
35
|
-
return callback({
|
36
|
-
errMessage: "Could not find pub.",
|
37
|
-
errCode: "failed-to-find-pub",
|
38
|
-
success: undefined,
|
39
|
-
});
|
40
|
-
|
41
|
-
let createMessagesCertificate = await gun
|
42
|
-
.user(publicKey)
|
43
|
-
.get("certificates")
|
44
|
-
.get(userPub)
|
45
|
-
.get("messages");
|
46
|
-
|
47
|
-
if (!createMessagesCertificate)
|
48
|
-
return callback({
|
49
|
-
errMessage: "Could not find friend certificate to create message",
|
50
|
-
errCode: "failed-to-find-friend-messages-certificate",
|
51
|
-
success: undefined,
|
52
|
-
});
|
53
|
-
|
54
|
-
let updateMetaCertificate = await gun
|
55
|
-
.user(publicKey)
|
56
|
-
.get("certificates")
|
57
|
-
.get(userPub)
|
58
|
-
.get("chats");
|
59
|
-
|
60
|
-
if (!updateMetaCertificate)
|
61
|
-
return callback({
|
62
|
-
errMessage: "Could not find friend certificate to add meta to chat",
|
63
|
-
errCode: "failed-to-find-friend-chats-certificate",
|
64
|
-
success: undefined,
|
65
|
-
});
|
66
|
-
|
67
|
-
gun
|
68
|
-
.user()
|
69
|
-
.get("chats")
|
70
|
-
.get(roomId)
|
71
|
-
.get("latestMessage")
|
72
|
-
.put(voiceRecording);
|
73
|
-
|
74
|
-
gun
|
75
|
-
.user(publicKey)
|
76
|
-
.get("chats")
|
77
|
-
.get(roomId)
|
78
|
-
.get("latestMessage")
|
79
|
-
.put(voiceRecording, null, { opt: { cert: updateMetaCertificate } });
|
80
|
-
|
81
|
-
gun
|
82
|
-
.user()
|
83
|
-
.get("messages")
|
84
|
-
.get(roomId)
|
85
|
-
.set(voiceRecording, ({ err }) => {
|
86
|
-
if (err)
|
87
|
-
return callback({
|
88
|
-
errMessage: err,
|
89
|
-
errCode: "message-creation-error",
|
90
|
-
success: undefined,
|
91
|
-
});
|
92
|
-
else
|
93
|
-
gun
|
94
|
-
.user(publicKey)
|
95
|
-
.get("messages")
|
96
|
-
.get(roomId)
|
97
|
-
.set(
|
98
|
-
voiceRecording,
|
99
|
-
({ err }) => {
|
100
|
-
if (err)
|
101
|
-
return callback({
|
102
|
-
errMessage: err,
|
103
|
-
errCode: "message-creation-error",
|
104
|
-
success: undefined,
|
105
|
-
});
|
106
|
-
else
|
107
|
-
return callback({
|
108
|
-
errMessage: undefined,
|
109
|
-
errCode: undefined,
|
110
|
-
success: "Created a voice message with friend.",
|
111
|
-
});
|
112
|
-
},
|
113
|
-
{ opt: { cert: createMessagesCertificate } }
|
114
|
-
);
|
115
|
-
});
|
116
|
-
})(callback);
|
117
|
-
};
|
118
|
-
|
119
|
-
export default sendVoiceMessage;
|