-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mempool.js
94 lines (84 loc) · 3.25 KB
/
Mempool.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
/**
* ******MEMPOOL COMPONENT******
*/
const bitcoinMessage = require('bitcoinjs-message');
class MemPool {
constructor() {
this.memPool = []; // temporary storage for validation requests:
this.timeoutRequests = [], // temporary storage for time out requests:
this.mempoolValid = []; // temporary storage valid requests:
}
// get request object details from mempool
getDetailsFromMemPool(cWalletAddress){
return this.memPool[cWalletAddress]
}
// Add reuest to validate in mempool
addRequestValidation(newRequestObject, requestTimeOut){
let self = this;
this.memPool[newRequestObject.walletAddress] = newRequestObject;
this.timeoutRequests[newRequestObject.walletAddress]=setTimeout(function(){ self.removeValidationRequest(newRequestObject.walletAddress); console.log('SET-TIME-OUT')}, requestTimeOut );
}
// Delete the request from mempool and timeout-request
removeValidationRequest(cWalletAddress){
this.memPool[cWalletAddress] = null;
this.timeoutRequests[cWalletAddress] = null;
clearTimeout(this.timeoutRequests[cWalletAddress])
}
// Once the request in validated, add the validated mempool in Valid Mempool array
storeToValidMemPool(cWalletAddress){
try{
this.mempoolValid[cWalletAddress] = cWalletAddress
this.memPool[cWalletAddress] = null;
this.timeoutRequests[cWalletAddress] = null;
clearTimeout(this.timeoutRequests[cWalletAddress])
return true;
}catch(e){
return false;
}
}
// Check if the window time has expired or not
verifyTimeLeft(cWalletAddress) {
if(this.memPool[cWalletAddress]){
return true;
}
else return false;
}
// Verify if the request validation exists and if it is valid.
verifyAddressRequest(cWalletAddress){
if(this.mempoolValid[cWalletAddress]){
return true;
}
else return false;
}
// Validate the request by wallet address and store it to an array of validated mempool
validateRequestByWallet(requestData){
let address = requestData.address;
let signature = requestData.signature;
let isWindowTimeLeft = this.verifyTimeLeft(address);
if (!isWindowTimeLeft) {
return "Request expired"
}
let requestObject = this.getDetailsFromMemPool(address);
const { message, requestTimeStamp, validationWindow } = requestObject;
const isValid = bitcoinMessage.verify(message, address, signature);;
if(!isValid){
const isStored = this.storeToValidMemPool(address)
if ( isStored ) {
let obj = {
registerStar:true,
status: {
address,
requestTimeStamp,
message,
validationWindow,
messageSignature: true
}
}
return obj;
}
}
return 'Signature Invalid'
}
}
module.exports = MemPool;
// timeouts[address] = setTimeout(function(){ removeWhiteList(address) }, whiteListTimeWall );