Socket
Socket
Sign inDemoInstall

frontblock

Package Overview
Dependencies
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

frontblock - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

14

FrontblockLib.d.ts
import { SubscriptionResponse, ErrorResponse, SuccessResponse } from "frontblock-generic/Service";
import { Coin, AccountMap, TransactionMap } from "frontblock-generic/Types";
import { FrontblockApi } from "frontblock-generic/Api";
import { Payment, PaymentService } from "./PaymentService";
import { DollarValueProvider, FrontblockConf } from "./Types";
export declare const CryptocompareDollarValueProvider: DollarValueProvider;
export declare class FrontblockLib implements PaymentService {
export declare class Frontblock implements PaymentService, FrontblockApi {
private subscriptionUIDs;
private subscriptionTypes;
private payments;

@@ -13,4 +13,6 @@ private paymentStreams;

private dollarValueProvider;
private libfactory;
private libs;
private apikey;
private conf;
private io;
private socket;
constructor(conf: FrontblockConf);

@@ -29,4 +31,4 @@ private updateSubscribers;

streamPayments(callback: (payment: Payment<Coin>) => void): Promise<ErrorResponse | SubscriptionResponse>;
unsubscribe(uid: string | SubscriptionResponse): Promise<ErrorResponse | SuccessResponse>;
subscribe<C extends Coin>(currency: C, account: AccountMap[C], callback: (tx: TransactionMap[C]) => void): Promise<SubscriptionResponse | ErrorResponse>;
subscribe<C extends Coin>(apikey: string, coin: C, account: AccountMap[C], callback: (tx: TransactionMap[C]) => void): Promise<SubscriptionResponse | ErrorResponse>;
unsubscribe(apikey: string, uid: string | SubscriptionResponse): Promise<ErrorResponse | SuccessResponse>;
}

@@ -6,3 +6,3 @@ "use strict";

const Types_1 = require("./Types");
const ServiceLibs_1 = require("./ServiceLibs");
const bsock = require('bsock');
const fetch = require("node-fetch");

@@ -20,6 +20,5 @@ const uuid = require("uuid/v4");

};
class FrontblockLib {
class Frontblock {
constructor(conf) {
this.subscriptionUIDs = new Map();
this.subscriptionTypes = new Map();
this.payments = new Map();

@@ -29,18 +28,10 @@ this.paymentStreams = new Map();

this.dollarValueProvider = exports.CryptocompareDollarValueProvider;
this.libfactory = {
XLM: (conf) => { return new ServiceLibs_1.XLMServiceLib(conf); },
BTC: (conf) => { return new ServiceLibs_1.BTCServiceLib(conf); },
ETH: (conf) => { return new ServiceLibs_1.ETHServiceLib(conf); },
XRP: (conf) => { return new ServiceLibs_1.XRPServiceLib(conf); },
};
this.libs = {};
Object.keys(conf.coinConfigs).forEach((key) => {
if (this.libfactory[key] == null)
return;
let lib = this.libfactory[key](conf.coinConfigs[key]);
if (lib != null)
this.libs[key] = lib;
});
this.apikey = "";
this.io = bsock.createServer();
this.conf = conf;
if (conf.apiKey != null)
this.apikey = conf.apiKey;
if (conf.dollarValueProvider != null)
this.dollarValueProvider = conf.dollarValueProvider;
this.socket = bsock.connect(conf.apiPort, conf.apiHost, conf.tls != null ? conf.tls : false);
}

@@ -84,11 +75,11 @@ updateSubscribers(payment) {

await this.updateDollarValue();
const api = this.libs[currency];
if (api == null)
return new Service_1.ErrorResponse("Unknown coin `" + currency + "`");
const account = api.getNextAccount();
const accountProvider = this.conf[currency];
if (accountProvider == null)
return new Service_1.ErrorResponse("Unable to generate account. Please specifiy an AccountProvider for `" + currency + "`");
const dollarValue = this.dollarValues.get(currency);
if (dollarValue == null)
return new Service_1.ErrorResponse("Unable to get dollar value of `" + currency + "`");
const account = accountProvider();
const payment = new PaymentService_1.Payment(amount, amount / dollarValue, currency, account);
const res = await this.subscribe(currency, account, (tx) => this.handlePayment(payment, tx));
const res = await this.subscribe(this.apikey, currency, account, (tx) => this.handlePayment(payment, tx));
if (res instanceof Service_1.ErrorResponse)

@@ -103,11 +94,11 @@ return res;

await this.updateDollarValue();
const api = this.libs[currency];
if (api == null)
return new Service_1.ErrorResponse("Unknown coin `" + currency + "`");
const account = api.getNextAccount();
const accountProvider = this.conf[currency];
if (accountProvider == null)
return new Service_1.ErrorResponse("Unable to generate account. Please specifiy an AccountProvider for `" + currency + "`");
const dollarValue = this.dollarValues.get(currency);
if (dollarValue == null)
return new Service_1.ErrorResponse("Unable to get dollar value of " + currency);
const account = accountProvider();
const payment = new PaymentService_1.Payment(dollarValue * amount, amount, currency, account);
const res = await this.subscribe(currency, account, (tx) => this.handlePayment(payment, tx));
const res = await this.subscribe(this.apikey, currency, account, (tx) => this.handlePayment(payment, tx));
if (res instanceof Service_1.ErrorResponse)

@@ -178,3 +169,3 @@ return res;

if (sub != null)
this.unsubscribe(sub);
this.unsubscribe(this.apikey, sub);
return new Service_1.SuccessResponse();

@@ -198,27 +189,15 @@ }

}
async unsubscribe(uid) {
if (typeof uid != "string") {
if (uid.uid == null)
return new Service_1.ErrorResponse("Malformed subscription identifier");
uid = uid.uid;
async subscribe(apikey, coin, account, callback) {
const r = await this.socket.call(apikey, coin, account);
const res = Service_1.parseSubResponse(r);
if (res instanceof Service_1.SubscriptionResponse) {
this.socket.hook(res.uid, callback);
}
const currency = this.subscriptionTypes.get(uid);
if (currency == null)
return new Service_1.ErrorResponse("Unknown subscription");
const api = this.libs[currency];
if (api == null)
return new Service_1.ErrorResponse("Unknown coin `" + currency + "`");
return await api.unsubscribe(uid);
}
async subscribe(currency, account, callback) {
const api = this.libs[currency];
if (api == null)
return new Service_1.ErrorResponse("Unknown coin `" + currency + "`");
const res = await api.subscribe(account, callback);
if (res instanceof Service_1.ErrorResponse)
return res;
this.subscriptionTypes.set(res.uid, currency);
return res;
}
async unsubscribe(apikey, uid) {
const res = await this.socket.call('unsubscribe', apikey, uid);
return Service_1.parseResponse(res);
}
}
exports.FrontblockLib = FrontblockLib;
exports.Frontblock = Frontblock;
{
"name": "frontblock",
"version": "0.0.2",
"version": "0.0.3",
"description": "frontblock shop-side library ",

@@ -5,0 +5,0 @@ "scripts": {

@@ -20,2 +20,3 @@ import { SubscriptionResponse, ErrorResponse, SuccessResponse } from "frontblock-generic/Service";

private frontBlock;
private apikey;
constructor(svcConf: ServiceConf, frontblockConf: FrontblockConf);

@@ -22,0 +23,0 @@ private subscriptionHandlerGenerator;

@@ -27,4 +27,5 @@ 'use strict';

this.privateWsServer = http.createServer();
this.apikey = "";
this.subscriptionHandlerGenerator = (socket) => async (currency, account) => {
const res = this.frontBlock.subscribe(currency, account, (tx) => {
const res = this.frontBlock.subscribe(this.apikey, currency, account, (tx) => {
if (res instanceof Service_1.SubscriptionResponse)

@@ -40,3 +41,5 @@ socket.call(res.uid, tx);

};
this.frontBlock = new FrontblockLib_1.FrontblockLib(frontblockConf);
if (frontblockConf.apiKey != null)
this.apikey = frontblockConf.apiKey;
this.frontBlock = new FrontblockLib_1.Frontblock(frontblockConf);
this.publicSock.attach(this.publicWsServer);

@@ -86,6 +89,6 @@ this.publicSock.on('socket', (socket) => {

subscribe(currency, account, callback) {
return this.frontBlock.subscribe(currency, account, callback);
return this.frontBlock.subscribe(this.apikey, currency, account, callback);
}
unsubscribe(uid) {
return this.frontBlock.unsubscribe(uid);
return this.frontBlock.unsubscribe(this.apikey, uid);
}

@@ -92,0 +95,0 @@ }

import { ErrorResponse, SubscriptionResponse, SuccessResponse } from "frontblock-generic/Service";
import { Coin, Transaction, Account, AccountMap, TransactionMap } from "frontblock-generic/Types";
import { Coin, Transaction, Account } from "frontblock-generic/Types";
export declare class Payment<C extends Coin> {

@@ -28,4 +28,2 @@ readonly dollarValue: number;

cancelStream(uid: string | SubscriptionResponse): Promise<ErrorResponse | SuccessResponse>;
subscribe<C extends Coin>(currency: C, account: AccountMap[C], callback: (tx: TransactionMap[C]) => void): Promise<SubscriptionResponse | ErrorResponse>;
unsubscribe(uid: string | SubscriptionResponse): Promise<ErrorResponse | SuccessResponse>;
}

@@ -1,26 +0,15 @@

import { Coin } from "frontblock-generic/Types";
import { Coin, AccountMap } from "frontblock-generic/Types";
import { Payment } from "./PaymentService";
import { ConfMap } from "./ServiceLibs";
export declare const CoinList: Coin[];
export declare type PaymentHandler<C extends Coin> = (payment: Payment<C>) => void;
export declare type LibConf = {
address: string;
export declare type FrontblockConf = {
apiHost: string;
apiPort: number;
};
export declare type XLMLibConf = LibConf & {
memoProvider?: () => string;
};
export declare type BTCLibConf = LibConf & {};
export declare type ETHLibConf = LibConf & {};
export declare type XRPLibConf = LibConf & {
memoProvider?: () => string;
};
export declare type FrontblockConf = {
coinConfigs: {
[coin in keyof ConfMap]?: ConfMap[coin];
};
tls?: boolean;
apiKey?: string;
dollarValueProvider?: DollarValueProvider;
} & {
[coin in Coin]?: AccountProvider<coin>;
};
export declare type AccountProvider<C extends Coin> = () => AccountMap[C];
export declare type DollarValueProvider = (coin: Coin[]) => Promise<Map<Coin, number>>;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc