-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
200 lines (167 loc) · 5.13 KB
/
server.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
'use strict';
const chain = require('./simpleChain');
const bm = require('./blockModel');
const vf = require('./validation');
const boom = require('boom');
const Hapi = require('hapi');
const Joi = require('joi');
const INIT_CHAIN_HEIGHT = parseInt(0);
const blockchain = chain.blockchainFactory();
const validation = vf.validationFactory();
// Create a server with a host and port
const server = Hapi.server({
host: 'localhost',
port: 8000
});
const getHashHandler = async function (request, h) {
const hash = encodeURIComponent(request.params.blockhash);
try {
const block = await blockchain.getBlockByHash(hash)
.catch((err) => {throw err});
return block;
} catch (err) {
throw err;
}
}
const getServerAddressHandler = async function (request, h) {
const address = encodeURIComponent(request.params.address);
try {
const blocks = await blockchain.getBlocksByAddress(address)
.catch((err) => {throw err});
return blocks;
} catch (err) {
throw err;
}
}
const getBlockHandler = async function (request, h) {
const blockHeight = request.params.blockheight ?
encodeURIComponent(request.params.blockheight) : INIT_CHAIN_HEIGHT;
console.log('get BlockHeight ' + blockHeight);
try {
const block = await chain.getBlockObj(parseInt(blockHeight))
.catch((err) => {throw err});
return block;
} catch (err) {
return err;
}
}
const postBlockHandler = async function (request, h) {
console.log("Receive Block POST " + JSON.stringify(request.payload));
try {
let address = request.payload.address;
const addressExist = await validation.existAddress(address);
if (addressExist.hasEntry && addressExist.validationModel.messageVerify) {
let newBlock = bm.buildBlock(address, request.payload.star);
const addBlock = await blockchain.addBlock(newBlock)
.catch((err) => {throw err});
await validation.removeAddress(address)
.catch((err) => {throw err});
return addBlock;
} else {
throw boom.unauthorized("Wallet Address is not registered or verified!");
}
} catch (err) {
return err;
}
}
const postRequestValidationHandler = async function (request, h) {
console.log("Receive RequestValidation POST " + JSON.stringify(request.payload));
try {
const response = await validation.validateRequest(request.payload.address)
.catch((err) => {throw err});
return response;
} catch (err) {
throw err;
}
}
const postMessageSignatureHandler = async function (request, h) {
console.log("Receive MessageSignature POST " + JSON.stringify(request.payload));
try {
const response = await validation.signatureMessage(request.payload.address, request.payload.signature)
.catch((err) => {throw err});
return response;
} catch (err) {
throw err;
}
}
const getServerBlock = {
handler: getBlockHandler
}
const getServerHash = {
handler: getHashHandler
}
const getServerAddress = {
handler: getServerAddressHandler
}
const postServerBlock = {
handler: postBlockHandler,
validate: {
payload: {
address: Joi.string().regex(/^[a-zA-Z0-9]{26,35}$/).required(),
star: {
dec: Joi.string().min(1).required(),
ra: Joi.string().min(1).required(),
story: Joi.string().min(1).required(),
magnitude: Joi.string().min(1).optional(),
constellation: Joi.string().min(1).optional()
}
}
}
}
const postRequestValidation = {
handler: postRequestValidationHandler,
validate: {
payload: {
// Bitcoin addresses between 26 - 35 characters BASE58 check
address: Joi.string().regex(/^[a-zA-Z0-9]{26,35}$/).required()
}
}
}
const postMessageSignature = {
handler: postMessageSignatureHandler,
validate: {
payload: {
// Bitcoin addresses between 26 - 35 characters BASE58 check
address: Joi.string().regex(/^[a-zA-Z0-9]{26,35}$/).required(),
signature: Joi.string().min(1).required()
}
}
}
// Add the route
server.route([{
method: 'GET',
path: '/block/{blockheight?}',
config: getServerBlock
}, {
method: 'GET',
path: '/stars/hash:{blockhash}',
config: getServerHash
}, {
method: 'GET',
path: '/stars/address:{address}',
config: getServerAddress
}, {
method: 'POST',
path: '/block',
config: postServerBlock
}, {
method: 'POST',
path: '/requestValidation',
config: postRequestValidation
}, {
method: 'POST',
path: '/message-signature/validate',
config: postMessageSignature
}]
);
// Start the server
async function start() {
try {
await server.start();
} catch (err) {
console.log(err);
process.exit(1);
}
console.log('Server running at:', server.info.uri);
};
start();