node-ebics-client/lib/BankLetter.js

74 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-05-17 15:03:59 +00:00
'use strict';
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');
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
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
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');
// const digest = Buffer.from(k.publicDigest(), 'base64').toString('HEX');
2018-05-17 15:03:59 +00:00
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);
console.log(`Data written to file on path '${path}'`);
} catch (error) {
console.error(`error while writing bank letter to path '${path}' with error ${error}`);
throw error;
}
2018-08-31 07:23:41 +00:00
return new Promise(resolve => resolve(true));
}
2018-06-01 13:16:43 +00:00
};