Skip to content

Commit da4f8a2

Browse files
authored
94 save your own encrypted posts with keypair (#97)
* Adding new model PostEncrypted * Refactoring huginSyncer.js * Updating huginSyncer.js
1 parent 72fd30a commit da4f8a2

File tree

4 files changed

+234
-104
lines changed

4 files changed

+234
-104
lines changed

configs/huginSyncer.js

+171-102
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ require('dotenv').config()
99
let log = require('loglevel')
1010
const { extraDataToMessage } = require('hugin-crypto')
1111
const { performance } = require('perf_hooks')
12+
const { WebSocket } = require('ws')
13+
let ws = new WebSocket(`ws://localhost:8080`)
1214

1315
const { getTimestamp } = require('../utils/time')
1416
const { messageCriteria } = require('../utils/messageCriteria')
@@ -21,9 +23,6 @@ let models = require('../database/models')
2123

2224
let known_pool_txs = [];
2325

24-
const { WebSocket } = require('ws');
25-
let ws = new WebSocket(`ws://localhost:8080`);
26-
2726
/**
2827
* Background sync to fetch data
2928
*
@@ -39,14 +38,14 @@ module.exports.backgroundSyncMessages = async () => {
3938
body: JSON.stringify({ knownTxsIds: known_pool_txs })
4039
})
4140

42-
let json = await resp.json();
41+
let json = await resp.json()
4342
json = JSON.stringify(json)
4443
.replaceAll('.txPrefix', '')
4544
.replaceAll('transactionPrefixInfo.txHash', 'transactionPrefixInfotxHash')
46-
json = JSON.parse(json);
45+
json = JSON.parse(json)
4746

48-
let transactions = json.addedTxs;
49-
let transaction;
47+
let transactions = json.addedTxs
48+
let transaction
5049

5150
if (transactions.length === 0) {
5251
log.info(getTimestamp() + ' INFO: Got empty transaction array.')
@@ -55,15 +54,24 @@ module.exports.backgroundSyncMessages = async () => {
5554

5655
for (transaction in transactions) {
5756
try {
57+
console.log(transactions[transaction])
58+
5859
let thisExtra = transactions[transaction].transactionPrefixInfo.extra
59-
let thisHash = transactions[transaction].transactionPrefixInfotxHash
60+
let txHash = transactions[transaction].transactionPrefixInfotxHash
6061

61-
if (known_pool_txs.indexOf(thisHash) === -1) {
62-
known_pool_txs.push(thisHash)
62+
// checking if encrypted post with txHash already exists in db - if not create a new record
63+
encryptedPostExists(txHash).then(result => {
64+
if (result === null) {
65+
saveEncryptedPost(transactions[transaction])
66+
}
67+
})
68+
69+
if (known_pool_txs.indexOf(txHash) === -1) {
70+
known_pool_txs.push(txHash)
6371
message_was_unknown = true
6472
} else {
6573
message_was_unknown = false
66-
console.log("This transaction is already known", thisHash)
74+
console.log("This transaction is already known", txHash)
6775
continue
6876
}
6977

@@ -79,8 +87,6 @@ module.exports.backgroundSyncMessages = async () => {
7987
message = await extraDataToMessage(thisExtra, knownk, keypair)
8088
}
8189

82-
// let msg = JSON.parse(message)
83-
8490
if (!message || message === undefined) {
8591
log.info(getTimestamp() + ' INFO: Caught undefined null message, continue.')
8692
continue
@@ -96,7 +102,7 @@ module.exports.backgroundSyncMessages = async () => {
96102
board: message.brd || null,
97103
time: message.t || null,
98104
nickname: message.n || null,
99-
tx_hash: thisHash || null,
105+
tx_hash: txHash || null,
100106
reply: message.r ||null
101107
}
102108

@@ -108,99 +114,17 @@ module.exports.backgroundSyncMessages = async () => {
108114
log.info(getTimestamp() + ' INFO: Message does not meet criteria based on configuration: ' + JSON.stringify(message))
109115
continue
110116
}
111-
112117
log.info(getTimestamp() + ' INFO: Criteria fulfilled.')
113118

114119
// broadcast message object to websocket server
115120
ws.send(JSON.stringify(messageObj))
116121

117-
let startTime = performance.now()
118-
119-
try {
120-
// database lookup if row exists with a certain tx_hash
121-
const postTxHashLookup = models.Post.findOne({
122-
where: {
123-
tx_hash: thisHash
124-
},
125-
order: [[ 'id', 'DESC' ]],
126-
raw: true,
127-
})
128-
129-
// checking if post with tx_hash already exists in db - if not create a new record
130-
postTxHashLookup.then(async result => {
131-
if (result === null) {
132-
await sequelize.transaction(async (t) => {
133-
return models.Post.create(messageObj).then(postObj => {
134-
log.info(getTimestamp() + ` INFO: Post transaction was successful - Post with ID ${postObj.id} was created.`)
135-
136-
// extract hashtags from message and save it do db and add relationship in post_hashtag table
137-
let messageStr = message.m
138-
let hashtags
139-
140-
try {
141-
hashtags = messageStr.match(/#[^\s#\.\;!*%&()?^$@`¨]*/gmi)
142-
143-
if (hashtags) {
144-
// going through all hashtags and do separate lookups
145-
hashtags.forEach(hashtag => {
146-
// removing the # and making it lowercase, so we have proper input for query
147-
let hashtagName = hashtag.replace('#', '').toLowerCase()
148-
149-
const hashtagLookup = models.Hashtag.findOne({
150-
where: {
151-
name: hashtagName
152-
},
153-
order: [[ 'id', 'DESC' ]],
154-
raw: true,
155-
})
156-
157-
// create hashtag if it does not exist otherwise get the hashtag ID
158-
hashtagLookup.then(async result => {
159-
if (result === null) {
160-
await sequelize.transaction(async (t1) => {
161-
return models.Hashtag.create({
162-
name: hashtagName
163-
}).then(async hashtagObj => {
164-
await sequelize.transaction(async (t2) => {
165-
return models.PostHashtag.create({
166-
post_id: postObj.id,
167-
hashtag_id: hashtagObj.id
168-
})
169-
})
170-
})
171-
})
172-
} else {
173-
// hashtag exists, so we add a new row in post_hashtag with its ID
174-
await sequelize.transaction(async (t3) => {
175-
return models.PostHashtag.create({
176-
post_id: postObj.id,
177-
hashtag_id: result.id
178-
})
179-
})
180-
}
181-
})
182-
183-
})
184-
}
185-
} catch(TypeError) {
186-
log.error(getTimestamp() + ' ERROR: Could not parse hashtags')
187-
}
188-
189-
})
190-
})
191-
} else {
192-
log.info(getTimestamp() + ' INFO: Found record in database - Skipping.')
193-
}
194-
})
195-
196-
// calculating queries for debug reasons
197-
let endTime = performance.now()
198-
log.info(getTimestamp() + ` INFO: Queries to took ${endTime - startTime} seconds`)
199-
200-
} catch (err) {
201-
log.info(getTimestamp() + ' ERROR: An error adding a Post transaction - Rolling back. ' + err)
202-
}
203-
122+
// checking if post with txHash already exists in db - if not create a new record
123+
postExists(txHash).then(result => {
124+
if (result === null) {
125+
savePost(messageObj, txHash)
126+
}
127+
})
204128
} else {
205129
log.info(getTimestamp() + ' INFO: No message.')
206130
}
@@ -212,4 +136,149 @@ module.exports.backgroundSyncMessages = async () => {
212136
} catch (err) {
213137
log.error(getTimestamp() + ' ERROR: Sync error. ' + err)
214138
}
139+
}
140+
141+
/**
142+
* Check if encrypted post exists in database.
143+
*
144+
* @param {String} txHash - Hash value.
145+
* @returns {Boolean} Resolves to true if found.
146+
*/
147+
async function encryptedPostExists(txHash) {
148+
try {
149+
const postEncryptedTxHashLookup = models.PostEncrypted.findOne({
150+
where: {
151+
tx_hash: txHash
152+
},
153+
order: [[ 'id', 'DESC' ]],
154+
raw: true,
155+
})
156+
157+
return postEncryptedTxHashLookup
158+
} catch (err) {
159+
log.error(getTimestamp() + ' ERROR: Sync error. ' + err)
160+
}
161+
}
162+
163+
/**
164+
* Check if post exists in database.
165+
*
166+
* @param {String} txHash - Hash value.
167+
* @returns {Boolean} Resolves to true if found.
168+
*/
169+
async function postExists(txHash) {
170+
try {
171+
const postTxHashLookup = models.Post.findOne({
172+
where: {
173+
tx_hash: txHash
174+
},
175+
order: [[ 'id', 'DESC' ]],
176+
raw: true,
177+
})
178+
179+
return postTxHashLookup
180+
} catch (err) {
181+
log.error(getTimestamp() + ' ERROR: Sync error. ' + err)
182+
}
183+
}
184+
185+
/**
186+
* Save an encrypted post to database.
187+
*
188+
* @param {Object} transaction - Transaction object.
189+
* @returns {Promise} Resolves to this if transaction to database succeeded.
190+
*/
191+
async function saveEncryptedPost(transaction) {
192+
try {
193+
await sequelize.transaction(async (t) => {
194+
return models.PostEncrypted.create({
195+
tx_hash: transaction.transactionPrefixInfotxHash,
196+
tx_extra: transaction.transactionPrefixInfo.extra,
197+
tx_unlock_time: transaction.transactionPrefixInfo.unlock_time,
198+
tx_version: transaction.transactionPrefixInfo.version
199+
})
200+
})
201+
} catch(err) {
202+
log.error(getTimestamp() + ' ERROR: ' + err)
203+
}
204+
}
205+
206+
/**
207+
* Save a post to database.
208+
*
209+
* @param {Object} messageObj - Message object.
210+
* @param {String} txHash - Hash value.
211+
* @returns {Promise} Resolves to this if transaction to database succeeded.
212+
*/
213+
async function savePost(messageObj, txHash) {
214+
let startTime = performance.now()
215+
216+
try {
217+
await sequelize.transaction(async (t) => {
218+
return models.Post.create(messageObj).then(postObj => {
219+
log.info(getTimestamp() + ` INFO: Post transaction was successful - Post with ID ${postObj.id} was created.`)
220+
221+
// extract hashtags from message and save it do db and add relationship in post_hashtag table
222+
let messageStr = messageObj.message
223+
224+
try {
225+
let hashtags = messageStr.match(/#[^\s#\.\;!*%&()?^$@`¨]*/gmi)
226+
227+
228+
if (hashtags) {
229+
// going through all hashtags and do separate lookups
230+
hashtags.forEach(hashtag => {
231+
// removing the # and making it lowercase, so we have proper input for query
232+
let hashtagName = hashtag.replace('#', '').toLowerCase()
233+
234+
const hashtagLookup = models.Hashtag.findOne({
235+
where: {
236+
name: hashtagName
237+
},
238+
order: [[ 'id', 'DESC' ]],
239+
raw: true,
240+
})
241+
242+
// create hashtag if it does not exist otherwise get the hashtag ID
243+
hashtagLookup.then(async result => {
244+
if (result === null) {
245+
await sequelize.transaction(async (t1) => {
246+
return models.Hashtag.create({
247+
name: hashtagName
248+
}).then(async hashtagObj => {
249+
await sequelize.transaction(async (t2) => {
250+
return models.PostHashtag.create({
251+
post_id: postObj.id,
252+
hashtag_id: hashtagObj.id
253+
})
254+
})
255+
})
256+
})
257+
} else {
258+
// hashtag exists, so we add a new row in post_hashtag with its ID
259+
await sequelize.transaction(async (t3) => {
260+
return models.PostHashtag.create({
261+
post_id: postObj.id,
262+
hashtag_id: result.id
263+
})
264+
})
265+
}
266+
})
267+
268+
})
269+
}
270+
} catch(TypeError) {
271+
log.error(getTimestamp() + ' ERROR: Could not parse hashtags')
272+
}
273+
274+
})
275+
})
276+
277+
// calculating queries for debug reasons
278+
let endTime = performance.now()
279+
log.info(getTimestamp() + ` INFO: Queries to took ${endTime - startTime} seconds`)
280+
281+
} catch (err) {
282+
log.info(getTimestamp() + ' ERROR: An error adding a Post transaction - Rolling back. ' + err)
283+
}
215284
}

configs/postgresql.js

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ if (process.env.NODE_ENV === 'development') {
2323
try {
2424
sequelize.authenticate()
2525
.then(r =>
26-
//TODO: we need to do a initial migrate on start (only if it hasn't before)
27-
// correction: we need to do this in the package.json script section...
2826
log.info(getTimestamp() + ' INFO: Connection to database has been established successfully.')
2927
);
3028
} catch (err) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
module.exports = {
3+
async up(queryInterface, Sequelize) {
4+
await queryInterface.createTable('post_encrypted', {
5+
id: {
6+
allowNull: false,
7+
autoIncrement: true,
8+
primaryKey: true,
9+
type: Sequelize.INTEGER
10+
},
11+
tx_hash: {
12+
type: Sequelize.STRING
13+
},
14+
tx_extra: {
15+
type: Sequelize.TEXT
16+
},
17+
tx_unlock_time: {
18+
type: Sequelize.INTEGER
19+
},
20+
tx_version: {
21+
type: Sequelize.INTEGER
22+
},
23+
createdAt: {
24+
allowNull: false,
25+
type: Sequelize.DATE
26+
},
27+
updatedAt: {
28+
allowNull: false,
29+
type: Sequelize.DATE
30+
}
31+
});
32+
},
33+
async down(queryInterface, Sequelize) {
34+
await queryInterface.dropTable('post_encrypted');
35+
}
36+
};

0 commit comments

Comments
 (0)