smartlog-receiver/ts/sl.classes.smartlogreceiver.ts

45 lines
1.5 KiB
TypeScript
Raw Normal View History

2018-10-31 16:42:18 +00:00
import * as plugins from './sl.receiver.plugins';
2018-11-11 00:38:19 +00:00
import { ILogPackage, ILogPackageAuthenticated, ILogDestination } from '@pushrocks/smartlog-interfaces';
2018-11-11 16:45:15 +00:00
export type TValidatorFunction = (logPackage: ILogPackage) => boolean;
2018-11-11 00:38:19 +00:00
export interface ISmartlogReceiverOptions {
smartlogInstance: plugins.smartlog.Smartlog;
passphrase: string;
validatorFunction: TValidatorFunction;
}
2018-10-31 16:42:18 +00:00
/**
* a class that receives smartlog packages
*/
export class SmartlogReceiver {
2018-11-11 00:38:19 +00:00
passphrase: string;
validatorFunction: TValidatorFunction;
smartlogInstance: plugins.smartlog.Smartlog;
constructor(smartlogReceiverOptions: ISmartlogReceiverOptions) {
this.passphrase = smartlogReceiverOptions.passphrase;
this.validatorFunction = smartlogReceiverOptions.validatorFunction;
this.smartlogInstance = smartlogReceiverOptions.smartlogInstance;
}
2018-10-31 16:42:18 +00:00
/**
* handles a authenticated log
*/
2018-11-11 00:38:19 +00:00
handleAuthenticatedLog(authenticatedLogPackageArg: ILogPackageAuthenticated) {
const authString = authenticatedLogPackageArg.auth;
const logPackage = authenticatedLogPackageArg.logPackage;
if(authString === plugins.smarthash.sha256FromStringSync(this.passphrase)) {
2018-11-11 00:56:17 +00:00
// this.smartlogInstance.log('ok', 'Message accepted');
2018-11-11 00:56:02 +00:00
this.smartlogInstance.handleLogPackage(logPackage);
2018-11-11 00:38:19 +00:00
} else {
this.smartlogInstance.log('error', 'Message rejected because of bad passphrase');
// console.log(plugins.smarthash.sha256FromStringSync(this.passphrase));
}
}
2018-10-31 16:42:18 +00:00
}