2018-05-17 15:03:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
const zlib = require('zlib');
|
|
|
|
const crypto = require('crypto');
|
2018-06-11 08:38:32 +00:00
|
|
|
const BN = require('bn.js');
|
|
|
|
|
2018-06-11 12:25:07 +00:00
|
|
|
const Crypto = require('../../crypto/Crypto');
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
const { DOMParser, XMLSerializer } = require('xmldom');
|
|
|
|
const xpath = require('xpath');
|
2018-07-03 09:48:00 +00:00
|
|
|
const errors = require('./errors');
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
const DEFAULT_IV = Buffer.from(Array(16).fill(0, 0, 15));
|
2018-06-11 08:38:32 +00:00
|
|
|
|
|
|
|
const lastChild = (node) => {
|
|
|
|
let y = node.lastChild;
|
|
|
|
|
|
|
|
while (y.nodeType !== 1) y = y.previousSibling;
|
|
|
|
|
|
|
|
return y;
|
|
|
|
};
|
|
|
|
|
2018-06-15 06:33:41 +00:00
|
|
|
module.exports = (xml, keys) => ({
|
|
|
|
keys,
|
|
|
|
doc: new DOMParser().parseFromString(xml, 'text/xml'),
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
isSegmented() {
|
2018-06-01 13:16:43 +00:00
|
|
|
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
|
|
|
|
const node = select('//xmlns:header/xmlns:mutable/xmlns:SegmentNumber', this.doc);
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
return !!node.length;
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
isLastSegment() {
|
2018-06-01 13:16:43 +00:00
|
|
|
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
|
2018-05-17 15:03:59 +00:00
|
|
|
const node = select("//xmlns:header/xmlns:mutable/*[@lastSegment='true']", this.doc);
|
|
|
|
|
2018-06-01 13:16:43 +00:00
|
|
|
return !!node.length;
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
orderData() {
|
2018-06-11 08:38:32 +00:00
|
|
|
const orderDataNode = this.doc.getElementsByTagNameNS('urn:org:ebics:H004', 'OrderData');
|
|
|
|
|
|
|
|
if (!orderDataNode.length) return {};
|
|
|
|
|
|
|
|
const orderData = orderDataNode[0].textContent;
|
2018-06-01 13:16:43 +00:00
|
|
|
const decipher = crypto.createDecipheriv('aes-128-cbc', this.transactionKey(), DEFAULT_IV).setAutoPadding(false);
|
|
|
|
const data = Buffer.from(decipher.update(orderData, 'base64', 'binary') + decipher.final('binary'), 'binary');
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
return zlib.inflateSync(data).toString();
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
transactionKey() {
|
2018-06-01 13:16:43 +00:00
|
|
|
const keyNodeText = this.doc.getElementsByTagNameNS('urn:org:ebics:H004', 'TransactionKey')[0].textContent;
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
return Crypto.privateDecrypt(this.keys.e(), Buffer.from(keyNodeText, 'base64'));
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
transactionId() {
|
2018-06-01 13:16:43 +00:00
|
|
|
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
|
|
|
|
const node = select('//xmlns:header/xmlns:static/xmlns:TransactionID', this.doc);
|
2018-05-17 15:03:59 +00:00
|
|
|
|
|
|
|
return node.length ? node[0].textContent : '';
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
2018-05-17 15:03:59 +00:00
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
orderId() {
|
|
|
|
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
|
|
|
|
const node = select('//xmlns:header/xmlns:mutable/xmlns:OrderID', this.doc);
|
|
|
|
|
|
|
|
return node.length ? node[0].textContent : '';
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
2018-06-11 08:38:32 +00:00
|
|
|
|
2018-07-03 09:48:00 +00:00
|
|
|
businessCode() {
|
2018-06-11 08:38:32 +00:00
|
|
|
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
|
2018-07-03 09:48:00 +00:00
|
|
|
const node = select('//xmlns:body/xmlns:ReturnCode', this.doc)
|
2018-06-11 08:38:32 +00:00
|
|
|
|
|
|
|
return node.length ? node[0].textContent : '';
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
2018-06-11 08:38:32 +00:00
|
|
|
|
2018-07-03 09:48:00 +00:00
|
|
|
businessSymbol(code) {
|
|
|
|
return errors.business[code].symbol;
|
|
|
|
},
|
|
|
|
|
|
|
|
businessShortText(code) {
|
|
|
|
return errors.business[code].short_text;
|
|
|
|
},
|
|
|
|
|
|
|
|
businessMeaning(code) {
|
|
|
|
return errors.business[code].meaning;
|
|
|
|
},
|
|
|
|
|
|
|
|
technicalCode() {
|
|
|
|
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
|
|
|
|
const node = select('//xmlns:header/xmlns:mutable/xmlns:ReturnCode', this.doc);
|
|
|
|
|
|
|
|
return node.length ? node[0].textContent : '';
|
|
|
|
},
|
|
|
|
|
|
|
|
technicalSymbol() {
|
2018-06-11 08:38:32 +00:00
|
|
|
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
|
|
|
|
const node = select('//xmlns:header/xmlns:mutable/xmlns:ReportText', this.doc);
|
|
|
|
|
|
|
|
return node.length ? node[0].textContent : '';
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
2018-06-11 08:38:32 +00:00
|
|
|
|
2018-07-03 09:48:00 +00:00
|
|
|
technicalShortText(code) {
|
|
|
|
return errors.technical[code].short_text;
|
|
|
|
},
|
|
|
|
|
|
|
|
technicalMeaning(code) {
|
|
|
|
return errors.technical[code].meaning;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* returnCode(where = 'header') {
|
|
|
|
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
|
|
|
|
const node = where === 'header'
|
|
|
|
? select('//xmlns:header/xmlns:mutable/xmlns:ReturnCode', this.doc)
|
|
|
|
: select('//xmlns:body/xmlns:ReturnCode', this.doc);
|
|
|
|
|
|
|
|
return node.length ? node[0].textContent : '';
|
|
|
|
}, */
|
|
|
|
|
2018-06-11 08:38:32 +00:00
|
|
|
bankKeys() {
|
|
|
|
const orderData = this.orderData();
|
|
|
|
if (!Object.keys(orderData).length) return {};
|
|
|
|
|
|
|
|
const doc = new DOMParser().parseFromString(orderData, 'text/xml');
|
|
|
|
const select = xpath.useNamespaces({ xmlns: 'urn:org:ebics:H004' });
|
|
|
|
const keyNodes = select('//xmlns:PubKeyValue', doc);
|
|
|
|
const bankKeys = {};
|
|
|
|
|
|
|
|
if (!keyNodes.length) return {};
|
|
|
|
|
|
|
|
for (let i = 0; i < keyNodes.length; i++) {
|
|
|
|
const type = lastChild(keyNodes[i].parentNode).textContent;
|
|
|
|
const modulus = xpath.select("//*[local-name(.)='Modulus']", keyNodes[i])[0].textContent;
|
|
|
|
const exponent = xpath.select("//*[local-name(.)='Exponent']", keyNodes[i])[0].textContent;
|
|
|
|
|
|
|
|
const mod = new BN(Buffer.from(modulus, 'base64'), 2).toBuffer();
|
|
|
|
const exp = new BN(Buffer.from(exponent, 'base64')).toNumber();
|
|
|
|
|
|
|
|
bankKeys[`bank${type}`] = { mod, exp };
|
|
|
|
}
|
|
|
|
|
|
|
|
return bankKeys;
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
2018-06-11 08:38:32 +00:00
|
|
|
|
2018-05-17 15:03:59 +00:00
|
|
|
toXML() {
|
|
|
|
return new XMLSerializer().serializeToString(this.doc);
|
2018-06-15 06:33:41 +00:00
|
|
|
},
|
|
|
|
});
|