UNPKG

4.15 kBJavaScriptView Raw
1'use strict';
2Object.defineProperty(exports, "__esModule", { value: true });
3const Service_1 = require("frontblock-generic/Service");
4const FrontblockLib_1 = require("./FrontblockLib");
5const express = require('express');
6const http = require('http');
7const bsock = require('bsock');
8class ServiceConf {
9 constructor(publicPort, privatePort) {
10 this.publicPort = publicPort;
11 this.privatePort = privatePort;
12 }
13}
14exports.ServiceConf = ServiceConf;
15class WebConf {
16 constructor(httpPort) {
17 this.httpPort = httpPort;
18 }
19}
20exports.WebConf = WebConf;
21class PaymentServer {
22 constructor(svcConf, frontblockConf) {
23 this.publicSock = bsock.createServer();
24 this.publicWsServer = http.createServer();
25 this.privateSock = bsock.createServer();
26 this.privateWsServer = http.createServer();
27 this.apikey = "";
28 this.subscriptionHandlerGenerator = (socket) => async (currency, account) => {
29 const res = this.frontBlock.subscribe(this.apikey, currency, account, (tx) => {
30 if (res instanceof Service_1.SubscriptionResponse)
31 socket.call(res.uid, tx);
32 });
33 };
34 this.streamHandlerGenerator = (socket) => async () => {
35 const res = this.frontBlock.streamPayments((payment) => {
36 if (res instanceof Service_1.SubscriptionResponse)
37 socket.call(res.uid, payment);
38 });
39 };
40 if (frontblockConf.apiKey != null)
41 this.apikey = frontblockConf.apiKey;
42 this.frontBlock = new FrontblockLib_1.Frontblock(frontblockConf);
43 this.publicSock.attach(this.publicWsServer);
44 this.publicSock.on('socket', (socket) => {
45 socket.hook('createUsdValuePayment', this.createUsdValuePayment);
46 socket.hook('createCryptoPayment', this.createCryptoPayment);
47 socket.hook('cancelPayment', this.cancelPayment);
48 socket.hook('getPayment', this.getPayment);
49 });
50 this.publicWsServer.listen(svcConf.publicPort);
51 this.privateSock.attach(this.privateWsServer);
52 this.privateSock.on('socket', (socket) => {
53 socket.hook('resolePayment', this.resolvePayment);
54 socket.hook('finalizePayment', this.finalizePayment);
55 socket.hook('cancelStream', this.cancelStream);
56 socket.hook('streamPayments', this.streamHandlerGenerator(socket));
57 socket.hook('subscribe', this.subscriptionHandlerGenerator(socket));
58 socket.hook('unsubscribe', this.unsubscribe);
59 });
60 this.privateWsServer.listen(svcConf.privatePort);
61 }
62 createUsdValuePayment(currency, amount) {
63 return this.frontBlock.createUsdValuePayment(currency, amount);
64 }
65 createCryptoPayment(currency, amount) {
66 return this.frontBlock.createCryptoPayment(currency, amount);
67 }
68 getPayment(paymentID) {
69 return this.frontBlock.getPayment(paymentID);
70 }
71 cancelPayment(paymentID) {
72 return this.frontBlock.cancelPayment(paymentID);
73 }
74 resolvePayment(paymentID) {
75 return this.frontBlock.resolvePayment(paymentID);
76 }
77 finalizePayment(paymentID) {
78 return this.frontBlock.finalizePayment(paymentID);
79 }
80 streamPayments(callback) {
81 return this.frontBlock.streamPayments(callback);
82 }
83 cancelStream(uid) {
84 return this.frontBlock.cancelStream(uid);
85 }
86 subscribe(currency, account, callback) {
87 return this.frontBlock.subscribe(this.apikey, currency, account, callback);
88 }
89 unsubscribe(uid) {
90 return this.frontBlock.unsubscribe(this.apikey, uid);
91 }
92}
93exports.PaymentServer = PaymentServer;
94class PaymentServerWithWebsite extends PaymentServer {
95 constructor(svcConf, frontblockConf) {
96 super(svcConf, frontblockConf);
97 this.express = express();
98 this.express.use(express.static('static'));
99 this.httpServer = http.Server(this.express);
100 this.httpServer.listen(svcConf.httpPort, () => {
101 console.log('listening on *' + svcConf.httpPort);
102 });
103 }
104}