@@ -12,24 +12,18 @@ const {
12
12
removeMessages,
13
13
removeContact,
14
14
addGroup,
15
- removeGroup,
16
15
unBlockContact,
17
16
loadBlockList,
18
17
blockContact,
19
18
getGroupReply,
20
- printGroup,
21
- getGroups,
22
19
loadGroups,
23
20
deleteMessage,
24
21
addRoom,
25
- loadRooms,
26
22
addRoomKeys,
27
- getRooms,
28
23
removeRoom} = require ( "./database.cjs" )
29
24
const {
30
25
trimExtra,
31
- sanitize_pm_message,
32
- parse_call,
26
+ sanitize_pm_message,
33
27
sleep,
34
28
hexToUint,
35
29
randomKey,
@@ -159,6 +153,8 @@ ipcMain.on('send-msg', (e, msg, receiver, off_chain, grp, beam) => {
159
153
//Listens for event from frontend and saves contact and nickname.
160
154
ipcMain . on ( 'add-chat' , async ( e , hugin_address , nickname , first ) => {
161
155
save_contact ( hugin_address , nickname , first )
156
+ const key = await key_derivation_hash ( hugin_address . substring ( 0 , 99 ) )
157
+ await new_beam ( key , hugin_address , false ) ;
162
158
} )
163
159
164
160
@@ -190,11 +186,12 @@ ipcMain.on('expand-sdp', (e, data, address) => {
190
186
191
187
//BEAM
192
188
193
- ipcMain . on ( "beam" , async ( e , link , chat , send = false , offchain = false ) => {
194
- let beamMessage = await new_beam ( link , chat , send ) ;
189
+ ipcMain . on ( "beam" , async ( e , chat , send = false , beam = false ) => {
190
+ const key = await key_derivation_hash ( chat . substring ( 0 , 99 ) )
191
+ let beamMessage = await new_beam ( key , chat , send ) ;
195
192
if ( beamMessage === "Error" ) return
196
193
if ( ! beamMessage ) return
197
- send_message ( beamMessage . msg , beamMessage . chat , offchain )
194
+ if ( beam ) send_message ( beamMessage . msg , beamMessage . chat , offchain )
198
195
} ) ;
199
196
200
197
//SWARM
@@ -207,62 +204,28 @@ ipcMain.on('end-swarm', async (e, key) => {
207
204
end_swarm ( key )
208
205
} )
209
206
210
-
211
- //TORRENT
212
-
213
- ipcMain . on ( 'download-torrent' , ( e , file ) => {
214
- download_torrent ( file )
215
- } )
216
-
217
- const download_torrent = ( file ) => {
218
- return
219
- // const client = new WebTorrent({utp: true, dht: false})
220
- // const path = Hugin.downloadDir
221
- // Hugin.send('downloading', file)
222
- // client.add(file.message, { path: path, utp: true }, torrent => {
223
- // console.log("torrent added!")
224
- // torrent.on('download', function (bytes) {
225
- // console.log("Downloading!!!! --------->")
226
- // Hugin.send('download-file-progress', {
227
- // fileName: file.fileName,
228
- // progress: torrent.progress,
229
- // group: file.group,
230
- // chat: file.group,
231
- // path
232
- // })
233
- // } )
234
- // torrent.on('done', () => {
235
- // console.log("Downloaded torrent!")
236
- // Hugin.send('downloading-torrent')
237
- // console.log('torrent download finished')
238
- // })
239
- // })
207
+ const peer_dms = async ( ) => {
208
+ const contacts = await getConversations ( )
209
+ for ( const c of contacts ) {
210
+ const hashDerivation = await key_derivation_hash ( c . chat )
211
+ const beam = await new_beam ( hashDerivation , c . chat + c . key , false )
212
+ if ( beam === "Error" ) continue
213
+ if ( ! beam ) continue
214
+ }
215
+
240
216
}
241
217
242
- ipcMain . on ( 'upload-torrent' , ( e , [ fileName , path , size , time , group , hash ] ) => {
243
- return
244
- // console.log("Upload this!", path, fileName, size, time)
245
- // const client = new WebTorrent()
246
- // Hugin.send('uploading', {fileName, progress: 0, size, chat: group, time, hash})
247
- // client.seed(path, {}, (torrent) => {
248
- // console.log('Client is seeding ' + torrent.magnetURI)
249
- // torrent.on('wire', (wire, addr) => {
250
- // Hugin.send('torrent-connection')
251
- // Hugin.send('uploading-torrent')
252
- // })
253
- // torrent.on('upload', function (uploaded) {
254
- // console.log("Uploaded", uploaded)
255
- // Hugin.send('upload-file-progress', {fileName, progress: (uploaded / size) * 100, chat: group, time})
256
- // })
257
- // const message = {m: 'TORRENT://' + torrent.magnetURI, g: group, t: time}
258
-
259
- // send_group_message(message, false, false)
260
- // })
261
-
262
- } )
218
+ async function key_derivation_hash ( chat ) {
219
+ const [ privateSpendKey , privateViewKey ] = keychain . getPrivKeys ( )
220
+ const recvAddr = await Address . fromAddress ( chat )
221
+ const recvPubKey = recvAddr . m_keys . m_viewKeys . m_publicKey
222
+ const derivation = await crypto . generateKeyDerivation ( recvPubKey , privateViewKey ) ;
223
+ return await crypto . cn_fast_hash ( derivation )
224
+ }
263
225
264
226
const start_message_syncer = async ( ) => {
265
227
//Load knownTxsIds to backgroundSyncMessages on startup
228
+ peer_dms ( )
266
229
known_keys = Hugin . known_keys
267
230
block_list = Hugin . block_list
268
231
await background_sync_messages ( await load_checked_txs ( ) )
@@ -598,6 +561,9 @@ async function fetch_hugin_messages() {
598
561
599
562
async function send_message ( message , receiver , off_chain = false , group = false , beam_this = false ) {
600
563
//Assert address length
564
+ console . log ( "Send message!" , message )
565
+ console . log ( "offchain" , off_chain )
566
+ console . log ( "beam_this" , beam_this )
601
567
if ( receiver . length !== 163 ) {
602
568
return
603
569
}
@@ -1170,17 +1136,6 @@ async function save_message(msg, offchain = false) {
1170
1136
if ( ! message ) return
1171
1137
1172
1138
if ( await messageExists ( timestamp ) ) return
1173
-
1174
- //Checking if private msg is a call
1175
- let [ text , data , is_call , if_sent ] = parse_call ( msg . msg , addr , sent , offchain , timestamp )
1176
-
1177
- if ( text === "Audio call started" || text === "Video call started" && is_call && ! if_sent ) {
1178
- //Incoming calll
1179
- Hugin . send ( 'call-incoming' , data )
1180
- } else if ( text === "Call answered" && is_call && ! if_sent ) {
1181
- //Callback
1182
- Hugin . send ( 'got-callback' , data )
1183
- }
1184
1139
1185
1140
//If sent set addr to chat instead of from
1186
1141
if ( msg . chat && sent ) {
@@ -1193,7 +1148,6 @@ async function save_message(msg, offchain = false) {
1193
1148
await save_contact ( hugin )
1194
1149
}
1195
1150
1196
- message = sanitizeHtml ( text )
1197
1151
let newMsg = await saveMsg ( message , addr , sent , timestamp , offchain )
1198
1152
if ( sent ) {
1199
1153
//If sent, update conversation list
@@ -1234,6 +1188,9 @@ async function save_contact(hugin_address, nickname = false, first = false) {
1234
1188
t : Date . now ( ) ,
1235
1189
} )
1236
1190
known_keys . pop ( key )
1191
+ } else {
1192
+ const key = await key_derivation_hash ( addr )
1193
+ await new_beam ( key , hugin_address , false ) ;
1237
1194
}
1238
1195
}
1239
1196
@@ -1256,12 +1213,12 @@ function get_sdp(data)
1256
1213
if ( data . type == 'offer' )
1257
1214
{
1258
1215
let parsed_data = `${ data . video ? 'Δ' : 'Λ' } ` + parse_sdp ( data . data , false )
1259
- send_message ( parsed_data , data . contact , data . offchain , data . group )
1216
+ send_message ( parsed_data , data . contact , true , data . group , true )
1260
1217
}
1261
1218
else if ( data . type == 'answer' )
1262
1219
{
1263
1220
let parsed_data = `${ data . video ? 'δ' : 'λ' } ` + parse_sdp ( data . data , true )
1264
- send_message ( parsed_data , data . contact , data . offchain , data . group )
1221
+ send_message ( parsed_data , data . contact , true , data . group , true )
1265
1222
}
1266
1223
}
1267
1224
0 commit comments