node-ebics-client/lib/BankLetter.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-05-17 15:03:59 +00:00
'use strict';
const fs = require('fs');
2018-05-17 15:03:59 +00:00
const handlebars = require('handlebars');
const Crypto = require('./crypto/Crypto');
2019-11-06 14:52:31 +00:00
const { date } = require('./utils.js');
2018-05-17 15:03:59 +00:00
2018-06-01 13:16:43 +00:00
const registerHelpers = () => {
2019-11-06 14:52:31 +00:00
handlebars.registerHelper('today', () => date.toISODate(Date.now(), false));
2018-05-17 15:03:59 +00:00
2019-11-06 14:52:31 +00:00
handlebars.registerHelper('now', () => date.toISOTime(Date.now(), false));
2018-05-17 15:03:59 +00:00
handlebars.registerHelper('keyExponentBits', k => Buffer.byteLength(k.e()) * 8);
2018-05-17 15:03:59 +00:00
2019-11-01 14:28:04 +00:00
handlebars.registerHelper('keyModulusBits', k => k.size());
2018-05-17 15:03:59 +00:00
handlebars.registerHelper('keyExponent', k => k.e('hex'));
2018-05-17 15:03:59 +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) => {
const digest = Buffer.from(Crypto.digestPublicKey(k), '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
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);
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,
A006: keys.a(),
X002: keys.x(),
E002: keys.e(),
2018-05-17 15:03:59 +00:00
};
return templ(data);
}
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-06-01 13:16:43 +00:00
};