Add serialize function to BankLetter class

This commit is contained in:
Vladislav Hristov 2018-08-30 13:53:24 +03:00
parent 5d4e9f8309
commit 548c17a868
2 changed files with 27 additions and 7 deletions

View File

@ -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,
};

View File

@ -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;
}
}
};