-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.js
186 lines (164 loc) · 5.87 KB
/
blockchain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const got = require('got');
const FormData = require('form-data');
const isMock = false
exports.addVote = function add(encryptedVote, byUserPublicKey) {
return isMock ? mockSaveVote(encryptedVote, byUserPublicKey) : blockchainVote(encryptedVote, byUserPublicKey)
}
exports.getAllVotesToNews = function getAllVotesTo(newsURL) {
return isMock ? mockGetAllVotesTo(newsURL) : blockchainGetAllVotesToNews(newsURL)
}
exports.getAllVotesBy = function getAllVotesBy(userPublicKey) {
return isMock ? mockGetAllVotesBy(userPublicKey) : blockchainGetAllVotesBy(userPublicKey)
}
exports.createBlock = function createBlock(userPublicKey) {
return isMock ? mockCreateBlock(userPublicKey) : blockchainCreateBlock(userPublicKey)
}
exports.trendingNews = function trendingNews(quantity) {
return blockchainTrendingNews(quantity)
}
exports.getAllVotes = function getAllVotes() {
return blockchainGetAllVotes()
}
// Blockchain funtions
const votesKey = 'votes'
const encryptedVoteKey = 'encryptedVote'
const voteKey = 'vote'
const newsURLKey = 'newsURL'
const publicKeyKey = 'userPublicKey'
const createBlockKey = 'createBlock'
const blockKey = 'block'
const basePath = 'http://localhost:5000'
const votePath = `${basePath}/vote`
const votesByUserPath = `${basePath}/votesBy?userPublicKey=`
const votesToNewsPath = `${basePath}/votesTo/`
const createBlockPath = `${basePath}/${createBlockKey}`
const allVotesPath = `${basePath}/allVotes`
const trendingNewsPath = `${basePath}/popularNews/`
function blockchainTrendingNews(quantity) {
console.log("Will get trending news")
return new Promise(function(finishPromise, reject) {
got(trendingNewsPath + quantity)
.then(function(response) {
finishPromise(response.body)
})
.catch(function(error) {
console.log(error)
reject(error)
})
})
}
function blockchainVote(encryptedVote, userPublicKey) {
const form = new FormData()
form.append(encryptedVoteKey, encryptedVote)
form.append(publicKeyKey, userPublicKey)
return new Promise(function(finishPromise, reject) {
console.log("Will attempt to vote")
got.post(votePath, {
body: form
}).then(function(response) {
console.log("Success adding vote")
finishPromise(response.body)
})
.catch(function(error) {
console.log("Error adding vote")
reject(error)
})
})
}
function blockchainGetAllVotesToNews(newsURL) {
console.log("Will get all votes to news")
return new Promise(function(finishPromise, reject) {
got(votesToNewsPath + newsURL)
.then(function(response) {
const result = JSON.parse(response.body).map(value => {return new Block(value.vote, value.newsURL, value.userPublicKey, value.date)})
finishPromise(result)
})
.catch(function(error) {
console.log(error)
reject(error)
})
})
}
function blockchainGetAllVotesBy(userPublicKey) {
console.log("Will get all votes by user")
return new Promise(function(finishPromise, reject) {
const encodedPubKey = encodeURIComponent(userPublicKey)
got(votesByUserPath + encodedPubKey)
.then(function(response) {
finishPromise(JSON.parse(response.body))
})
.catch(function(error) {
console.log(error)
reject(error)
})
})
}
function blockchainCreateBlock(userPublicKey) {
console.log("Got to creation of user")
console.log(userPublicKey)
const form = new FormData()
form.append(publicKeyKey, userPublicKey)
return new Promise(function(finishPromise, reject) {
got.post(createBlockPath, {
body: form
}).then(function(response) {
console.log("Got response from block creation")
console.log(JSON.parse(response.body))
finishPromise(JSON.parse(response.body))
}).catch(function(error) {
console.log(error)
reject(error)
})
})
}
function blockchainGetAllVotes() {
console.log("Will get all votes from chain")
return new Promise(function(finishPromise, reject) {
got(allVotesPath)
.then(function(response) {
const result = JSON.parse(response.body).map(value => {return new Block(value.vote, value.newsURL, value.userPublicKey, value.date)})
finishPromise(result)
})
.catch(function(error) {
console.log(error)
reject(error)
})
})
}
// MOCK functions
class Block {
constructor(vote, newsURL, userPublicKey, date) {
this.vote = vote
this.newsURL = newsURL
this.userPublicKey = userPublicKey
this.date = date
}
}
var mockVotesBlock = []
function mockSaveVote(encryptedVote, userPublicKey) {
return new Promise(function(resolve, _reject) {
var newBlock = {"vote": true, "newsURL": "news1", "userPublicKey": userPublicKey, "date": new Date()}
mockVotesBlock.push(newBlock)
console.log('Added vote')
console.log(newBlock)
resolve(newBlock)
})
}
function mockGetAllVotesTo(news) {
return new Promise(function(resolve, _reject) {
const result = mockVotesBlock.filter(value => { return value.news == news }).sort(function(a, b) {
// convert date object into number to resolve issue in typescript
return +new Date(a.date) - +new Date(b.date);
})
resolve(result)
})
}
function mockGetAllVotesBy(userPublicKey) {
return new Promise(function(resolve, _reject) {
resolve(mockVotesBlock.filter(value => { return value.userPublicKey === userPublicKey}).sort(function(a, b) {
// convert date object into number to resolve issue in typescript
return +new Date(a.date) - +new Date(b.date);
}))
})
}
function mockCreateBlock(userPublicKey) {}