2018-05-17 15:03:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-08-30 10:53:24 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
const moment = require('moment');
|
2018-05-17 15:03:59 +00:00
|
|
|
const handlebars = require('handlebars');
|
2018-08-30 10:53:24 +00:00
|
|
|
const Crypto = require('./crypto/Crypto');
|
|
|
|
// const BN = require('bn.js');
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
const registerHelpers = () => {
|
|
|
|
handlebars.registerHelper('today', () => moment().format('DD.MM.YYYY'));
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
handlebars.registerHelper('now', () => moment().format('HH:mm:ss'));
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-08-30 10:53:24 +00:00
|
|
|
handlebars.registerHelper('keyExponentBits', k => Buffer.byteLength(k.e()) * 8);
|
|
|
|
// handlebars.registerHelper('keyExponentBits', k => Buffer.byteLength(new BN(k.key.keyPair.e).toBuffer()) * 8);
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
handlebars.registerHelper('keyModulusBits', k => k.key.getKeySize());
|
|
|
|
// return Buffer.byteLength(new BN(k.key.keyPair.e).toBuffer()) * 8;
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
handlebars.registerHelper('keyExponent', k => k.e('hex'));
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
handlebars.registerHelper('keyModulus', k => k.n('hex').toUpperCase().match(/.{1,2}/g).join(' '));
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
handlebars.registerHelper('sha256', (k) => {
|
2018-08-30 10:53:24 +00:00
|
|
|
const digest = Buffer.from(Crypto.digestPublicKey(k), 'base64').toString('HEX');
|
|
|
|
// const digest = Buffer.from(k.publicDigest(), 'base64').toString('HEX');
|
2018-06-01 13:16:43 +00:00
|
|
|
return digest.toUpperCase().match(/.{1,2}/g).join(' ');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
module.exports = class BankLetter {
|
|
|
|
constructor({
|
|
|
|
client,
|
|
|
|
bankName,
|
|
|
|
template,
|
|
|
|
}) {
|
|
|
|
this.client = client;
|
|
|
|
this.bankName = bankName;
|
|
|
|
this.template = template;
|
2018-08-31 07:23:41 +00:00
|
|
|
}
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-08-30 10:53:24 +00:00
|
|
|
async generate() {
|
2018-06-01 13:16:43 +00:00
|
|
|
registerHelpers();
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
const templ = handlebars.compile(this.template);
|
2018-08-30 10:53:24 +00:00
|
|
|
const keys = await this.client.keys();
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
const data = {
|
2018-06-01 13:16:43 +00:00
|
|
|
bankName: this.bankName,
|
|
|
|
userId: this.client.userId,
|
2018-05-17 15:03:59 +00:00
|
|
|
partnerId: this.client.partnerId,
|
2018-08-30 10:53:24 +00:00
|
|
|
A006: keys.a(),
|
|
|
|
X002: keys.x(),
|
|
|
|
E002: keys.e(),
|
2018-05-17 15:03:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return templ(data);
|
|
|
|
}
|
2018-08-30 10:53:24 +00:00
|
|
|
|
|
|
|
async serialize(path) {
|
|
|
|
const letter = await this.generate();
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(path, letter);
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
2019-10-08 13:09:08 +00:00
|
|
|
|
2018-08-31 07:23:41 +00:00
|
|
|
return new Promise(resolve => resolve(true));
|
2018-08-30 10:53:24 +00:00
|
|
|
}
|
2018-06-01 13:16:43 +00:00
|
|
|
};
|