-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: http endpoint for raiden hash resolver
This commit adds a new http server and endpoint to listen for hash resolver requests from Raiden. It replaces the previous method of a grpc interface for listening to resolve requests. Closes #931.
- Loading branch information
Showing
11 changed files
with
270 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import http from 'http'; | ||
import Service from '../service/Service'; | ||
import HttpService from './HttpService'; | ||
import Logger from '../Logger'; | ||
import errors from '../service/errors'; | ||
|
||
const reqToJson = (req: http.IncomingMessage) => { | ||
return new Promise<object>((resolve, reject) => { | ||
const body: any[] = []; | ||
req.on('data', (chunk) => { | ||
body.push(chunk); | ||
}).on('end', () => { | ||
const bodyStr = Buffer.concat(body).toString(); | ||
const reqContentLength = parseInt(req.headers['content-length'] || '', 10); | ||
|
||
if (bodyStr.length !== reqContentLength) { | ||
reject('invalid content-length header'); | ||
return; | ||
} | ||
|
||
try { | ||
resolve(JSON.parse(bodyStr)); | ||
} catch (err) { | ||
reject('cannot parse request json'); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
class HttpServer { | ||
private server: http.Server; | ||
private httpService: HttpService; | ||
|
||
constructor(private logger: Logger, service: Service) { | ||
this.server = http.createServer(this.requestListener); | ||
this.httpService = new HttpService(service); | ||
} | ||
|
||
private requestListener: http.RequestListener = async (req, res) => { | ||
if (!req.headers['content-length']) { | ||
res.writeHead(411); | ||
return; | ||
} | ||
|
||
let reqJson: any; | ||
try { | ||
reqJson = await reqToJson(req); | ||
} catch (err) { | ||
res.writeHead(400); | ||
res.end(err); | ||
return; | ||
} | ||
|
||
let resJson: any; | ||
try { | ||
switch (req.url) { | ||
case '/resolveraiden': | ||
resJson = await this.httpService.resolveHashRaiden(reqJson); | ||
break; | ||
default: | ||
res.writeHead(404); | ||
res.end(); | ||
} | ||
res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
res.end(JSON.stringify(resJson)); | ||
} catch (err) { | ||
if (err.code === errors.INVALID_ARGUMENT) { | ||
res.writeHead(400); | ||
res.end(err.message); | ||
} else { | ||
res.writeHead(500); | ||
res.end(JSON.stringify(err)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Starts the server and begins listening on the provided port. | ||
* @returns true if the server started listening successfully, false otherwise | ||
*/ | ||
public listen = async (port: number) => { | ||
return new Promise<void>((resolve, reject) => { | ||
const listenErrHandler = (err: Error) => { | ||
reject(err); | ||
}; | ||
|
||
this.server.listen(port, '127.0.0.1').once('listening', () => { | ||
this.logger.info(`http server listening on 127.0.0.1:${port}`); | ||
this.server.removeListener('error', listenErrHandler); | ||
resolve(); | ||
}).on('error', listenErrHandler); | ||
}); | ||
} | ||
|
||
/** | ||
* Stops listening for requests. | ||
*/ | ||
public close = () => { | ||
return new Promise<void>((resolve) => { | ||
this.server.close(() => { | ||
this.logger.info('http server has closed'); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default HttpServer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Service from '../service/Service'; | ||
import { RaidenResolveRequest, RaidenResolveResponse } from '../raidenclient/types'; | ||
|
||
class HttpService { | ||
constructor(private service: Service) {} | ||
|
||
public resolveHashRaiden = async (resolveRequest: RaidenResolveRequest): Promise<RaidenResolveResponse> => { | ||
const secret = await this.service.resolveHash({ | ||
amount: resolveRequest.amount, | ||
rHash: resolveRequest.secret_hash, | ||
}); | ||
return { secret }; | ||
} | ||
} | ||
|
||
export default HttpService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.