-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.js
385 lines (332 loc) · 11.7 KB
/
transaction.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
'use strict'
const stable_stringify = require('json-stable-stringify');
module.exports = function (dependencies) {
const crypto = dependencies['crypto'];
/**
* NOTE THAT UNLIKE THE TRANSACTION.JS IN CHAINGE, THIS TRANSACTION
* OBJECT DOES NOT CONVERT STRINGS INTO BIG INTEGER!
*/
class Transaction {
/**
* Create Transaction Object from JSON string data.
* A transaction object can be one of the following :
* a. DATA ; The identity of the user
* b. REQUEST ; Request to verify the identity
* c. ANSWER ; Either accepts or rejects the request
*
* The structure of general Txn
*
* PUBLIC-KEY
* SIGNATURE (of hash of payload)
* PAYLOAD -- Timestamp
* |- Type
* -- .. data
*
* @param {String or Object} If data is passed as string,
* then data is in JSON format. If it is an object, then we
* we can simply take it as txn object
*/
constructor(data) {
let txn = "";
if (typeof data === 'string' || data instanceof String) {
txn = JSON.parse(data);
} else {
txn = data;
}
this.deserialize_payload(txn.payload);
this.signature = txn.signature; // Signature of Txn
this.public_key = txn.public_key; // Public key of Txn
// The location of the TXN is not specified in TXN.
// However for convenience, we can keep the location data here.
// The location will be pair of (block_num, relative position in Merkle tree)
this.address = null;
this.timestamp = this.payload.timestamp;
this.type = this.payload.type;
}
/**
* serializes this transaction to form a long JSON string
* @returns {String}
*/
serialize() {
const payload_str = this.serialize_payload();
let txn = stable_stringify({
signature: this.signature,
public_key: this.public_key,
payload: payload_str
});
return txn;
}
/**
* serializes the payload into a JSON string
* @returns {String}
*/
serialize_payload() {
let txn_payload = {};
const p = this.payload;
// DATA Txn
if (this.type == 0) {
txn_payload = {
G: p.G,
g: p.g,
g_a: p.g_a,
g_r: p.g_r,
K: p.K,
secret: p.secret,
// Note that g_r_i is initially kepts as
// list of strings until that is actually
// used for Request verification
g_r_i: p.g_r_i,
encrypted: p.encrypted,
encrypted_key: p.encrypted_key,
};
} else if (this.type == 1) {
txn_payload = {
g_b: p.g_b,
g_g_ab_p_r: p.g_g_ab_p_r,
req: p.req,
data_blk_num: p.data_blk_num,
data_txn_sig: p.data_txn_sig,
}
} else if (this.type == 2) {
txn_payload = {
// Like g_r_i's case, response is again
// stored as the list of strings instead
// of list of big integers.
res: p.res,
data_blk_num: p.data_blk_num,
data_txn_sig: p.data_txn_sig,
req_blk_num: p.req_blk_num,
req_txn_sig: p.req_txn_sig
}
}
txn_payload['timestamp'] = this.timestamp;
txn_payload['type'] = this.type;
return stable_stringify(txn_payload);
}
/**
* Deserialize JSON payload data
* @param {JSON} parsed JSON payload data
*/
deserialize_payload(data) {
if (typeof data === 'string' || data instanceof String) {
data = JSON.parse(data)
}
this.payload = {};
if (data.type == 0) {
this.payload = {
G: data.G,
g: data.g,
g_a: data.g_a,
g_r: data.g_r,
K: data.K,
secret: data.secret,
g_r_i: data.g_r_i,
encrypted: data.encrypted,
encrypted_key: data.encrypted_key,
}
} else if (data.type == 1) {
this.payload = {
g_b: data.g_b,
g_g_ab_p_r: data.g_g_ab_p_r,
req: data.req,
data_blk_num: data.data_blk_num,
data_txn_sig: data.data_txn_sig
}
} else if (data.type == 2) {
this.payload = {
res: data.res,
data_blk_num: data.data_blk_num,
data_txn_sig: data.data_txn_sig,
req_blk_num: data.req_blk_num,
req_txn_sig: data.req_txn_sig
}
}
this.payload['timestamp'] = data.timestamp;
this.payload['type'] = data.type;
}
/**
* Check whether txn is identical to this
* @param {Transaction} txn
* @returns {Boolean}
*/
is_equal(txn) {
if (txn.signature == this.signature &&
txn.public_key == this.public_key) {
return true;
}
return false;
}
get_signature() {
return this.signature;
}
get_public_key() {
return this.public_key;
}
/**
* Returns the type of this Transaction in string
*/
get_type() {
return {
0: "DATA",
1: "REQUEST",
2: "ANSWER"
}[this.type];
}
/**
* Check whether the timestamp of current block is not faster than
* the current time
*/
check_timestamp() {
return Date.now() > this.timestamp;
}
/**
* Checks whether the signature of this transaction is correct.
* Note that Full verificatoin of validity of TXN is done in chain.js
* It must check whether the TXNs that this TXN is referring to actually
* matches to existing TXNs.
*/
check_signature() {
const verify = crypto.createVerify('RSA-SHA256');
verify.update(this.serialize_payload());
verify.end();
return verify.verify(this.public_key, this.signature, 'hex');
}
/**
* Return G
* @returns {BigInteger} Returns G of Data Txn in a big integer object
*/
get_G() {
if (this.type == 0) {
return this.payload.G;
}
throw "Invalid field 'G' is requested from block with type " + this.type;
}
/**
* @returns {BigInteger} Returns g of Data Txn in a big integer object
*/
get_g() {
if (this.type == 0) {
return this.payload.g;
}
throw "Invalid field 'g' is requested from block with type " + this.type;
}
/**
* @returns {BigInteger} Returns g^r of Data Txn in a big integer object
*/
get_g_r() {
if (this.type == 0) {
return this.payload.g_r;
}
throw "Invalid field 'g^r' is requested from block with type " + this.type;
}
/**
* @returns {Array} Returns list of g^{r_i}s in string array
*/
get_g_r_i() {
if (this.type == 0) {
return this.payload.g_r_i;
}
throw "Invalid field 'g_r_i' is requested from block with type " + this.type;
}
/**
* @returns {BigInteger} Returns g^a of Data Txn in a big integer object
*/
get_g_a() {
if (this.type == 0) {
return this.payload.g_a;
}
throw "Invalid field 'g^a' is requested from block with type" + this.type;
}
/**
* @returns {BigInteger} Returns g^b of Request Txn in a big integer object
*/
get_g_b() {
if (this.type == 1) {
return this.payload.g_b;
}
throw "Invalid field 'g^b' is requested from block with type " + this.type;
}
/**
* @returns {BigInteger} Returns g^(g^(ab) + r) of Reqeust Txn in a big integer object
*/
get_g_g_ab_p_r() {
if (this.type == 1) {
return this.payload.g_g_ab_p_r;
}
throw "Invalid field 'g^(g^(ab) + r)' is requested from block with type " + this.type;
}
get_K() {
if (this.type == 0) {
return this.payload.K;
}
throw "Invalid field 'K' is requested from block with type " + this.type;
}
get_secret() {
if (this.type == 0) {
return this.payload.secret;
}
throw "Invalid field 'secret' is requested from block with type " + this.type;
}
get_req() {
if (this.type == 1) {
return this.payload.req;
}
throw "Invalid field 'req' is requested from block with type " + this.type;
}
get_res() {
if (this.type == 2) {
return this.payload.res;
}
throw "Invalid field 'req' is requested from block with type " + this.type;
}
/**
* @returns {Number} Returns Block number of Data Txn that this Txn is referring to.
*/
get_data_blk_num() {
if (this.type == 1 || this.type == 2) {
return this.payload.data_blk_num;
}
throw "Invalid field 'data_blk_num' is requested from block with type" + this.type;
}
get_data_txn_sig() {
if (this.type == 1 || this.type == 2) {
return this.payload.data_txn_sig;
}
throw "Invalid field 'data_txn_sig' is requested from block with type" + this.type;
}
get_req_blk_num() {
if (this.type == 2) {
return this.payload.req_blk_num;
}
throw "Invalid field 'req_blk_num' is requested from block with type" + this.type;
}
get_req_txn_sig() {
if (this.type == 2) {
return this.payload.req_txn_sig;
}
throw "Invalid field 'req_txn_sig' is requested from block with type" + this.type;
}
get_encrypted_data() {
if (this.type == 0) {
return this.payload.encrypted;
}
throw "Invalid field 'encrypted' is requested for txn with type " + this.type;
}
get_encrypted_key() {
if (this.type == 0) {
return this.payload.encrypted_key;
}
throw "Invalid field 'encrypted_key' is requested for txn with type " + this.type;
}
}
/**
* Create a Transaction object from JSON string representing TXN.
* @param {String} data; MUST BE JSON string representing the TXN
*/
const create_transaction = function (data) {
return new Transaction(data);
}
return {
create_transaction,
}
}