From 548c17a868b4edcd894ce53f20435e52d45cdda3 Mon Sep 17 00:00:00 2001 From: Vladislav Hristov Date: Thu, 30 Aug 2018 13:53:24 +0300 Subject: [PATCH] Add serialize function to BankLetter class --- index.js | 2 ++ lib/BankLetter.js | 32 +++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index f90a594..3b26213 100644 --- a/index.js +++ b/index.js @@ -3,9 +3,11 @@ const Client = require('./lib/Client'); const Orders = require('./lib/predefinedOrders'); const fsKeysStorage = require('./lib/storages/fsKeysStorage'); +const BankLetter = require('./lib/BankLetter'); module.exports = { Client, Orders, + BankLetter, fsKeysStorage, }; diff --git a/lib/BankLetter.js b/lib/BankLetter.js index a31e404..d32c132 100644 --- a/lib/BankLetter.js +++ b/lib/BankLetter.js @@ -1,15 +1,19 @@ 'use strict'; +const fs = require('fs'); + const moment = require('moment'); const handlebars = require('handlebars'); -const BN = require('bn.js'); +const Crypto = require('./crypto/Crypto'); +// const BN = require('bn.js'); const registerHelpers = () => { handlebars.registerHelper('today', () => moment().format('DD.MM.YYYY')); handlebars.registerHelper('now', () => moment().format('HH:mm:ss')); - handlebars.registerHelper('keyExponentBits', k => Buffer.byteLength(new BN(k.key.keyPair.e).toBuffer()) * 8); + handlebars.registerHelper('keyExponentBits', k => Buffer.byteLength(k.e()) * 8); + // handlebars.registerHelper('keyExponentBits', k => Buffer.byteLength(new BN(k.key.keyPair.e).toBuffer()) * 8); handlebars.registerHelper('keyModulusBits', k => k.key.getKeySize()); // return Buffer.byteLength(new BN(k.key.keyPair.e).toBuffer()) * 8; @@ -19,7 +23,8 @@ const registerHelpers = () => { handlebars.registerHelper('keyModulus', k => k.n('hex').toUpperCase().match(/.{1,2}/g).join(' ')); handlebars.registerHelper('sha256', (k) => { - const digest = Buffer.from(k.publicDigest(), 'base64').toString('HEX'); + const digest = Buffer.from(Crypto.digestPublicKey(k), 'base64').toString('HEX'); + // const digest = Buffer.from(k.publicDigest(), 'base64').toString('HEX'); return digest.toUpperCase().match(/.{1,2}/g).join(' '); }); @@ -36,20 +41,33 @@ module.exports = class BankLetter { } - generate() { + async generate() { registerHelpers(); const templ = handlebars.compile(this.template); + const keys = await this.client.keys(); const data = { bankName: this.bankName, userId: this.client.userId, partnerId: this.client.partnerId, - A006: this.client.a(), - X002: this.client.x(), - E002: this.client.e(), + A006: keys.a(), + X002: keys.x(), + E002: keys.e(), }; return templ(data); } + + async serialize(path) { + const letter = await this.generate(); + + try { + fs.writeFileSync(path, letter); + console.log("Data written to file"); + } catch (error) { + console.log(error); + throw error; + } + } };