35 Commits

Author SHA1 Message Date
Vladislav Hristov
104ec67f8c chore: add ; at the end of Key.js 2019-11-01 16:52:36 +02:00
Vladislav Hristov
e8a80932e1 fit: key size is now calculated instead of hardcoded 2019-11-01 16:50:39 +02:00
Vladislav Hristov
ce6cd02e58 fit: BankLetter.js get key size 2019-11-01 16:28:04 +02:00
Vladislav Hristov
836ec0ebf3 fit(tests): use new Key in keys test 2019-11-01 13:49:59 +02:00
Vladislav Hristov
59a281c895 feat: use new Key in Keys manager 2019-11-01 13:49:33 +02:00
Vladislav Hristov
61ada747f3 clean: remove modulus and exponent property from bankKeys() 2019-11-01 13:48:51 +02:00
Vladislav Hristov
f15e09840a add: add Key class to manage a single key 2019-11-01 13:44:29 +02:00
Vladislav Hristov
4149c01695 wip: migrate to node-forge 2019-10-31 18:57:42 +02:00
Vladislav Hristov
1c1f112ff5 fix: modulus zero byte handling 2019-10-30 10:53:03 +02:00
nanov
1320f6f21d chore: update deps 2019-10-14 14:19:11 +03:00
Vladislav Hristov
0049fffc00 up the version to 0.0.8 2019-10-08 16:10:08 +03:00
Vladislav Hristov
2c9040904a chore: remove console.log statements 2019-10-08 16:09:08 +03:00
Dimitar Nanov
0f6dcf9eb2 Merge pull request #11 from yagop/fix-version
chore: readme maintenance
2019-10-03 09:32:17 +03:00
nanov
ca31edf245 chore: add yagop to contributors 2019-10-03 09:30:49 +03:00
nanov
4464349d0f chore: remove version from readme 2019-10-03 09:29:35 +03:00
Dimitar Nanov
15546df9ea Merge pull request #10 from yagop/examples
Provide examples
2019-10-03 09:20:29 +03:00
Yago Perez
40467a2c5e v0.0.35 -> v0.0.7 2019-10-03 00:20:36 +02:00
Yago Perez
eb9fbf5834 Change HBT order text 2019-10-03 00:10:52 +02:00
Yago Perez
166c61aec4 examples/send-hbt-order.js 2019-10-02 23:58:37 +02:00
Yago Perez
5a63e19aab examples/save-bank-kesy.js 2019-10-02 23:58:35 +02:00
Yago Perez
9e6c318372 keys -> passphrase 2019-10-02 23:58:33 +02:00
Yago Perez
ce6e58b3f3 examples/bankLetter.js 2019-10-02 23:58:29 +02:00
Yago Perez
7dad7c8787 initialize.js 2019-10-02 23:56:51 +02:00
Dimitar Nanov
79f17e1404 bump version 2019-08-02 10:52:48 +03:00
Dimitar Nanov
3ef32c8ce5 Merge pull request #7 from anandsahil/addCAMT53OrderType
* Add Z53 order type
* Create utils container and move dateRange functionality to there
2019-08-01 16:59:25 +03:00
anandsahil
aa761cf7ad move dateRange from constants to utils 2019-08-01 14:14:30 +02:00
anandsahil
5ff3147124 add new order type XZ53 and removed repeated dateRange to consts 2019-08-01 14:00:07 +02:00
anandsahil
01d4634d86 Merge remote-tracking branch 'src/master' 2019-08-01 13:52:51 +02:00
sahil anand
89904afa63 Merge pull request #2 from anandsahil/minorChanges
remove eCollect from constant and minor verbose error handeling
2019-07-25 17:40:29 +02:00
sahil anand
4907524259 Merge pull request #1 from anandsahil/and-XCT-order-type
add iso pain format XCT order type
2019-07-25 17:40:04 +02:00
Dimitar Nanov
c9f52d3bd9 bump 2019-07-24 09:12:06 +03:00
Dimitar Nanov
f5b05ae491 Merge pull request #4 from anandsahil/minorChanges
remove eCollect from constant and minor verbose error handaling
2019-07-24 09:05:14 +03:00
Dimitar Nanov
9f88b048d7 Merge pull request #5 from anandsahil/and-XCT-order-type
add iso pain format XCT order type
2019-07-24 09:04:22 +03:00
anandsahil
d06e92c51c add iso pain format XCT order type 2019-07-22 17:36:38 +02:00
anandsahil
cb2062ae2f remove eCollect from constant and minor verbose error handeling 2019-07-22 17:31:24 +02:00
26 changed files with 795 additions and 112 deletions

View File

@@ -1,4 +1,4 @@
# node-ebics-client v0.0.35 # node-ebics-client
--- ---
Pure node.js ( >=8 ) implementation of [EBICS](https://en.wikipedia.org/wiki/Electronic_Banking_Internet_Communication_Standard) ( Electronic Banking Internet Communication ). Pure node.js ( >=8 ) implementation of [EBICS](https://en.wikipedia.org/wiki/Electronic_Banking_Internet_Communication_Standard) ( Electronic Banking Internet Communication ).
@@ -21,4 +21,4 @@ The basic concept of this library was inspired by the [EPICS](https://github.com
## Copyright ## Copyright
Copyright: eCollect AG, 2018. Copyright: eCollect AG, 2018-9.

31
examples/bankLetter.js Executable file
View File

@@ -0,0 +1,31 @@
#! /usr/bin/env node
'use strict';
const ebics = require('../index');
const path = require('path');
const fs = require('fs');
const os = require('os');
const client = new ebics.Client({
url: 'https://ebics.server',
partnerId: '',
userId: '',
hostId: '',
passphrase: 'test', // keys-test will be decrypted with this passphrase
keyStorage: ebics.fsKeysStorage('./keys-test'),
});
const bankName = 'Bank name';
const template = fs.readFileSync('./templates/ini.hbs').toString();
const letter = new ebics.BankLetter({ client, bankName, template });
const bankLetterFile = path.join(os.homedir(), 'bankLetter.html');
letter.serialize(bankLetterFile)
.then(() => {
console.log('Send your bank the letter (%s)', bankLetterFile);
})
.catch((err) => {
console.error(err);
process.exit(1);
});

32
examples/initialize.js Executable file
View File

@@ -0,0 +1,32 @@
#! /usr/bin/env node
'use strict';
const ebics = require('../index');
const client = new ebics.Client({
url: 'https://ebics.server',
partnerId: 'PARTNER',
userId: 'USER',
hostId: 'HOST',
passphrase: 'test', // keys-test will be encrypted with this passphrase
keyStorage: ebics.fsKeysStorage('./keys-test'),
});
// New keys will be generated and saved in ./keys-test
client.send(ebics.Orders.INI)
.then((resp) => {
console.log('Respose for INI order %j', resp);
return client.send(ebics.Orders.HIA);
})
.then((resp) => {
console.log('Reponse for HIA order %j', resp);
if (resp.technicalCode !== '000000')
throw new Error('Something might went wrong');
console.log('Public keys should be sent to bank now. See examples/bankLetter.js');
})
.catch((err) => {
console.error(err);
process.exit(1);
});

30
examples/save-bank-kesy.js Executable file
View File

@@ -0,0 +1,30 @@
#! /usr/bin/env node
'use strict';
const ebics = require('../index');
const client = new ebics.Client({
url: 'https://ebics.server',
partnerId: '',
userId: '',
hostId: '',
passphrase: 'test', // keys-test will be decrypted with this passphrase
keyStorage: ebics.fsKeysStorage('./keys-test'),
});
// Client keys must be already generated and send by letter.
// The bank should have enabled the user
client.send(ebics.Orders.HPB)
.then((resp) => {
console.log('Respose for HPB order %j', resp);
if (resp.technicalCode !== '000000')
throw new Error('Something went wrong');
console.log('Received bank keys: %j', resp.bankKeys);
return client.setBankKeys(resp.bankKeys);
})
.catch((err) => {
console.error(err);
process.exit(1);
});

29
examples/send-hbt-order.js Executable file
View File

@@ -0,0 +1,29 @@
#! /usr/bin/env node
'use strict';
const ebics = require('../index');
const client = new ebics.Client({
url: 'https://ebics.server',
partnerId: '',
userId: '',
hostId: '',
passphrase: 'test', // keys-test will be decrypted with this passphrase
keyStorage: ebics.fsKeysStorage('./keys-test'),
});
// The bank keys must have been already saved
client.send(ebics.Orders.HTD)
.then((resp) => {
console.log('Respose for HTD order %j', resp);
if (resp.technicalCode !== '000000')
throw new Error('Something went wrong');
const data = Buffer.from(resp.orderData);
console.log(data.toString('utf8'));
})
.catch((err) => {
console.error(err);
process.exit(1);
});

View File

@@ -13,10 +13,8 @@ const registerHelpers = () => {
handlebars.registerHelper('now', () => moment().format('HH:mm:ss')); handlebars.registerHelper('now', () => moment().format('HH:mm:ss'));
handlebars.registerHelper('keyExponentBits', k => Buffer.byteLength(k.e()) * 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()); handlebars.registerHelper('keyModulusBits', k => k.size());
// return Buffer.byteLength(new BN(k.key.keyPair.e).toBuffer()) * 8;
handlebars.registerHelper('keyExponent', k => k.e('hex')); handlebars.registerHelper('keyExponent', k => k.e('hex'));
@@ -24,8 +22,6 @@ const registerHelpers = () => {
handlebars.registerHelper('sha256', (k) => { handlebars.registerHelper('sha256', (k) => {
const digest = Buffer.from(Crypto.digestPublicKey(k), '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(' '); return digest.toUpperCase().match(/.{1,2}/g).join(' ');
}); });
}; };
@@ -63,11 +59,10 @@ module.exports = class BankLetter {
try { try {
fs.writeFileSync(path, letter); fs.writeFileSync(path, letter);
console.log('Data written to file');
} catch (error) { } catch (error) {
console.log(error);
throw error; throw error;
} }
return new Promise(resolve => resolve(true)); return new Promise(resolve => resolve(true));
} }
}; };

View File

@@ -42,7 +42,7 @@ module.exports = class Client {
throw new Error('passphrase is requierd'); throw new Error('passphrase is requierd');
if (!keyStorage || typeof keyStorage.read !== 'function' || typeof keyStorage.write !== 'function') if (!keyStorage || typeof keyStorage.read !== 'function' || typeof keyStorage.write !== 'function')
throw new Error('keyStorage implemntation missing or wrong'); throw new Error('keyStorage implementation missing or wrong');
this.url = url; this.url = url;
this.partnerId = partnerId; this.partnerId = partnerId;

View File

@@ -2,7 +2,7 @@
const packageJson = require('../package.json'); const packageJson = require('../package.json');
const name = 'eCollect Node Ebics Client'; const name = 'Node Ebics Client';
const { version } = packageJson; const { version } = packageJson;
const orderOperations = { const orderOperations = {
ini: 'INI', ini: 'INI',

138
lib/keymanagers/Key.js Normal file
View File

@@ -0,0 +1,138 @@
'use strict';
const {
pki: {
rsa,
publicKeyToPem,
privateKeyToPem,
publicKeyFromPem,
privateKeyFromPem,
},
jsbn: {
BigInteger,
},
} = require('node-forge');
const getKeyType = (str) => {
const matches = str.match(/(PRIVATE|PUBLIC) KEY/);
if (!matches)
return null;
return matches[1].toLowerCase();
};
const keyFromPem = (pem) => {
const type = getKeyType(pem);
const isPublic = type === 'public';
const key = isPublic ? publicKeyFromPem(pem) : privateKeyFromPem(pem);
return {
isPublic,
key,
};
};
/**
* Creates a public key from modulus and exponent
* @param {Buffer} mod - the modulus
* @param {Buffer} exp - the exponent
*/
const keyFromModAndExp = (mod, exp) => {
const bnMod = new BigInteger(mod.toString('hex'), 16);
const bnExp = new BigInteger(exp.toString('hex'), 16);
return {
key: rsa.setPublicKey(bnMod, bnExp),
isPublic: true,
};
};
module.exports = class Key {
constructor({
pem = null, mod = null, exp = null, size = 2048,
} = {}) {
// generate new private key
if (!pem && !mod && !exp) {
const keyPair = rsa.generateKeyPair(size);
this.keyIsPublic = false;
this.privateKey = keyPair.privateKey;
this.publicKey = keyPair.publicKey;
return;
}
// new key from pem string
if (pem) {
const { key, isPublic } = keyFromPem(pem);
this.keyIsPublic = isPublic;
this.privateKey = isPublic ? null : key;
this.publicKey = isPublic ? key : null;
return;
}
// new key from mod and exp
if (mod && exp) {
const { key, isPublic } = keyFromModAndExp(mod, exp);
this.keyIsPublic = isPublic;
this.privateKey = isPublic ? null : key;
this.publicKey = isPublic ? key : null;
return;
}
// not good
throw new Error(`Can not create key without ${!mod ? 'modulus' : 'exponent'}.`);
}
static generate(size = 2048) {
return new Key({ size });
}
static importKey({ mod, exp }) {
return new Key({ mod, exp });
}
n(to = 'buff') {
const key = this.keyIsPublic ? this.publicKey : this.privateKey;
const keyN = Buffer.from(key.n.toByteArray());
return to === 'hex' ? keyN.toString('hex', 1) : keyN;
}
e(to = 'buff') {
const key = this.keyIsPublic ? this.publicKey : this.privateKey;
const eKey = Buffer.from(key.e.toByteArray());
return to === 'hex' ? eKey.toString('hex') : eKey;
}
d() {
if (this.keyIsPublic)
throw new Error('Can not get d component out of public key.');
return Buffer.from(this.privateKey.d.toByteArray());
}
isPrivate() {
return !this.keyIsPublic;
}
isPublic() {
return this.keyIsPublic;
}
// eslint-disable-next-line class-methods-use-this
size() {
const keyN = this.n('hex');
const bn = new BigInteger(keyN, 16);
return bn.bitLength();
}
toPem() {
return this.keyIsPublic ? publicKeyToPem(this.publicKey) : privateKeyToPem(this.privateKey);
}
};

View File

@@ -1,8 +1,14 @@
'use strict'; 'use strict';
const Key = require('./keyRSA'); // const Key = require('./keyRSA');
const Key = require('./Key');
const keyOrNull = key => (key ? Key(key) : null); const keyOrNull = (key) => {
if (key instanceof Key)
return key;
return key ? new Key({ pem: key }) : null;
};
module.exports = class Keys { module.exports = class Keys {
constructor({ constructor({
@@ -25,15 +31,15 @@ module.exports = class Keys {
const keys = {}; const keys = {};
Object.keys({ A006: '', X002: '', E002: '' }).forEach((key) => { Object.keys({ A006: '', X002: '', E002: '' }).forEach((key) => {
keys[key] = Key().generate(); keys[key] = Key.generate(); // Key().generate();
}); });
return new Keys(keys); return new Keys(keys);
} }
setBankKeys(bankKeys) { setBankKeys(bankKeys) {
this.keys.bankX002 = Key().importKey(bankKeys.bankX002); this.keys.bankX002 = new Key(bankKeys.bankX002); // Key().importKey(bankKeys.bankX002);
this.keys.bankE002 = Key().importKey(bankKeys.bankE002); this.keys.bankE002 = new Key(bankKeys.bankE002); // Key().importKey(bankKeys.bankE002);
} }
a() { a() {

59
lib/keymanagers/_Keys.js Normal file
View File

@@ -0,0 +1,59 @@
'use strict';
const Key = require('./keyRSA');
const keyOrNull = key => (key ? Key(key) : null);
module.exports = class Keys {
constructor({
A006,
E002,
X002,
bankX002,
bankE002,
}) {
this.keys = {
A006: keyOrNull(A006),
E002: keyOrNull(E002),
X002: keyOrNull(X002),
bankX002: keyOrNull(bankX002),
bankE002: keyOrNull(bankE002),
};
console.log('debug');
}
static generate() {
const keys = {};
Object.keys({ A006: '', X002: '', E002: '' }).forEach((key) => {
keys[key] = Key().generate();
});
return new Keys(keys);
}
setBankKeys(bankKeys) {
this.keys.bankX002 = Key().importKey(bankKeys.bankX002);
this.keys.bankE002 = Key().importKey(bankKeys.bankE002);
}
a() {
return this.keys.A006;
}
e() {
return this.keys.E002;
}
x() {
return this.keys.X002;
}
bankX() {
return this.keys.bankX002;
}
bankE() {
return this.keys.bankE002;
}
};

View File

@@ -1,30 +1,183 @@
'use strict'; 'use strict';
/* eslint-disable camelcase */
function rsaPublicKeyPem(modulus_b64, exponent_b64) {
function prepadSigned(hexStr) {
const msb = hexStr[0];
if (
(msb >= '8' && msb <= '9') ||
(msb >= 'a' && msb <= 'f') ||
(msb >= 'A' && msb <= 'F'))
return `00${hexStr}`;
return hexStr;
}
function toHex(number) {
const nstr = number.toString(16);
if (nstr.length % 2 === 0) return nstr;
return `0${nstr}`;
}
// encode ASN.1 DER length field
// if <=127, short from
// if >=128, long from
function encodeLengthHex(n) {
if (n <= 127) return toHex(n);
const n_hex = toHex(n);
const length_of_length_byte = 128 + (n_hex.length / 2); // 0x80+numbytes
return toHex(length_of_length_byte) + n_hex;
}
const modulus = Buffer.from(modulus_b64, 'base64');
const exponent = Buffer.from(exponent_b64, 'base64');
let modulus_hex = modulus.toString('hex');
let exponent_hex = exponent.toString('hex');
modulus_hex = prepadSigned(modulus_hex);
exponent_hex = prepadSigned(exponent_hex);
const modlen = modulus_hex.length / 2;
const explen = exponent_hex.length / 2;
const encoded_modlen = encodeLengthHex(modlen);
const encoded_explen = encodeLengthHex(explen);
const encoded_pubkey = `30${
encodeLengthHex(modlen + explen + (encoded_modlen.length / 2) + (encoded_explen.length / 2) + 2)
}02${encoded_modlen}${modulus_hex
}02${encoded_explen}${exponent_hex}`;
let seq2 =
`${'30 0d ' +
'06 09 2a 86 48 86 f7 0d 01 01 01' +
'05 00 ' +
'03'}${encodeLengthHex((encoded_pubkey.length / 2) + 1)
}00${encoded_pubkey}`;
seq2 = seq2.replace(/ /g, '');
let der_hex = `30${encodeLengthHex(seq2.length / 2)}${seq2}`;
der_hex = der_hex.replace(/ /g, '');
const der = Buffer.from(der_hex, 'hex');
const der_b64 = der.toString('base64');
const pem = `-----BEGIN PUBLIC KEY-----\n${
der_b64.match(/.{1,64}/g).join('\n')
}\n-----END PUBLIC KEY-----\n`;
return pem.trim();
}
const BN = require('bn.js'); const BN = require('bn.js');
const NodeRSA = require('node-rsa'); const NodeRSA = require('node-rsa');
const keyOrNull = (encodedKey) => { const {
if (encodedKey === null) return new NodeRSA(); pki: {
rsa,
publicKeyToPem,
privateKeyToPem,
publicKeyFromPem,
privateKeyFromPem,
},
jsbn: {
BigInteger,
},
} = require('node-forge');
return (encodedKey instanceof NodeRSA) ? encodedKey : new NodeRSA(encodedKey); const isKeyInstance = (obj) => {
if (typeof obj !== 'object')
return false;
return ('publicKey' in obj && 'privateKey' in obj);
}; };
module.exports = encodedKey => ({ const getKeyType = (str) => {
const matches = str.match(/(PRIVATE|PUBLIC) KEY/);
if (!matches)
return null;
return matches[1].toLowerCase();
};
/*
class RsaKeyPair {
constructor() {
this._isPublic = null;
this._publicKey = null;
this._privateKey = null;
}
fromString(str) {
}
}
*/
const keyOrNull = (encodedKey) => {
if (encodedKey === null) return {};
if (typeof encodedKey === 'string') {
const type = getKeyType(encodedKey);
const isPublic = type === 'public';
const key = isPublic ? publicKeyFromPem(encodedKey) : privateKeyFromPem(encodedKey);
key.isPublic = isPublic;
return key;
}
return encodedKey;
// return (isKeyInstance(encodedKey)) ? encodedKey;
/* const k = (encodedKey instanceof NodeRSA) ? encodedKey : new NodeRSA(encodedKey);
if (k.keyPair.e === 16777216)
k.keyPair.e = 4294967311;
return k; */
};
module.exports = (encodedKey) => {
if (encodedKey && encodedKey.__RsaKey)
return encodedKey;
return {
__RsaKey: true,
key: keyOrNull(encodedKey), key: keyOrNull(encodedKey),
generate(keySize = 2048) { generate(keySize = 2048) {
return new NodeRSA({ b: keySize }); const keyPair = rsa.generateKeyPair(keySize);
this.key = keyPair.privateKey;
this.key.isPublic = false;
this.publicKey = keyPair.publicKey;
return this;
// return rsa.generateKeyPair(keySize);
// return new NodeRSA({ b: keySize });
}, },
importKey({ mod, exp }) { importKey({
this.key = new NodeRSA(); mod,
this.key.importKey({ n: mod, e: exp }, 'components-public'); exp,
modulus,
exponent,
}) {
this.key = rsa.setPublicKey(new BigInteger(mod.toString('hex'), 16), new BigInteger(exp.toString('hex'), 16));
this.key.isPublic = true;
// const k = rsa.generateKeyPair();
// k.publicKey = rsa.setPublicKey(mod, exp);
// this.key = k;
// this.key.publicKey.
// .this.key.importKey({ n: mod, e: exp }, 'components-public');
/*
this.pempem = modulus && exponent ? {
modulus,
exponent,
} : null;
*/
return this; return this;
}, },
n(to = 'buff') { n(to = 'buff') {
const keyN = Buffer.from(this.key.exportKey('components-public').n); const key = this.publicKey || this.key;
const keyN = Buffer.from(key.n.toByteArray());
return to === 'hex' return to === 'hex'
? keyN.toString('hex', 1) ? keyN.toString('hex', 1)
@@ -32,7 +185,8 @@ module.exports = encodedKey => ({
}, },
e(to = 'buff') { e(to = 'buff') {
const eKey = new BN(this.key.exportKey('components-public').e).toBuffer(); const key = this.publicKey || this.key;
const eKey = Buffer.from(key.e.toByteArray()); // new BN(this.key.exportKey('components-public').e).toBuffer();
return to === 'hex' return to === 'hex'
? eKey.toString('hex') ? eKey.toString('hex')
@@ -40,10 +194,35 @@ module.exports = encodedKey => ({
}, },
d() { d() {
return this.key.keyPair.d.toBuffer(); return Buffer.from(this.key.d.toByteArray());
// return this.key.keyPair.d.toBuffer();
},
isPrivate() {
return !this.key.isPublic;
},
isPublic() {
return this.key.isPublic;
// return this.key.isPublic();
},
size() {
return 2048;
// return this.key.getKeySize();
}, },
toPem() { toPem() {
return this.key.isPrivate() ? this.key.exportKey('pkcs1-private-pem') : this.key.exportKey('pkcs8-public-pem'); if (this.isPublic())
return publicKeyToPem(this.key);
return privateKeyToPem(this.key);
/*
if (this.pempem)
return rsaPublicKeyPem(this.pempem.modulus, this.pempem.exponent);
const isPrivate = this.key.isPrivate();
const pem = isPrivate ? this.key.exportKey('pkcs1-private-pem') : this.key.exportKey('pkcs8-public-pem');
return pem;
*/
}, },
}); };
};

View File

@@ -127,10 +127,12 @@ module.exports = (xml, keys) => ({
const modulus = xpath.select(".//*[local-name(.)='Modulus']", keyNodes[i])[0].textContent; const modulus = xpath.select(".//*[local-name(.)='Modulus']", keyNodes[i])[0].textContent;
const exponent = xpath.select(".//*[local-name(.)='Exponent']", 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 mod = Buffer.from(modulus, 'base64');
const exp = new BN(Buffer.from(exponent, 'base64')).toNumber(); const exp = Buffer.from(exponent, 'base64');
bankKeys[`bank${type}`] = {
bankKeys[`bank${type}`] = { mod, exp }; mod,
exp,
};
} }
return bankKeys; return bankKeys;

View File

@@ -1,23 +1,13 @@
'use strict'; 'use strict';
const dateRange = (start, end) => { const utils = require('../utils');
if (start && end)
return {
DateRange: {
Start: start,
End: end,
},
};
return {};
};
module.exports = (start = null, end = null) => ({ module.exports = (start = null, end = null) => ({
version: 'h004', version: 'h004',
orderDetails: { orderDetails: {
OrderType: 'HAC', OrderType: 'HAC',
OrderAttribute: 'DZHNN', OrderAttribute: 'DZHNN',
StandardOrderParams: dateRange(start, end), StandardOrderParams: utils.dateRange(start, end),
}, },
operation: 'download', operation: 'download',
}); });

View File

@@ -1,23 +1,13 @@
'use strict'; 'use strict';
const dateRange = (start, end) => { const utils = require('../utils');
if (start && end)
return {
DateRange: {
Start: start,
End: end,
},
};
return {};
};
module.exports = (start = null, end = null) => ({ module.exports = (start = null, end = null) => ({
version: 'h004', version: 'h004',
orderDetails: { orderDetails: {
OrderType: 'PTK', OrderType: 'PTK',
OrderAttribute: 'DZHNN', OrderAttribute: 'DZHNN',
StandardOrderParams: dateRange(start, end), StandardOrderParams: utils.dateRange(start, end),
}, },
operation: 'download', operation: 'download',
}); });

View File

@@ -1,23 +1,13 @@
'use strict'; 'use strict';
const dateRange = (start, end) => { const utils = require('../utils');
if (start && end)
return {
DateRange: {
Start: start,
End: end,
},
};
return {};
};
module.exports = (start = null, end = null) => ({ module.exports = (start = null, end = null) => ({
version: 'h004', version: 'h004',
orderDetails: { orderDetails: {
OrderType: 'STA', OrderType: 'STA',
OrderAttribute: 'DZHNN', OrderAttribute: 'DZHNN',
StandardOrderParams: dateRange(start, end), StandardOrderParams: utils.dateRange(start, end),
}, },
operation: 'download', operation: 'download',
}); });

View File

@@ -1,23 +1,13 @@
'use strict'; 'use strict';
const dateRange = (start, end) => { const utils = require('../utils');
if (start && end)
return {
DateRange: {
Start: start,
End: end,
},
};
return {};
};
module.exports = (start = null, end = null) => ({ module.exports = (start = null, end = null) => ({
version: 'h004', version: 'h004',
orderDetails: { orderDetails: {
OrderType: 'VMK', OrderType: 'VMK',
OrderAttribute: 'DZHNN', OrderAttribute: 'DZHNN',
StandardOrderParams: dateRange(start, end), StandardOrderParams: utils.dateRange(start, end),
}, },
operation: 'download', operation: 'download',
}); });

View File

@@ -0,0 +1,8 @@
'use strict';
module.exports = document => ({
version: 'h004',
orderDetails: { OrderType: 'XCT', OrderAttribute: 'OZHNN', StandardOrderParams: {} },
operation: 'upload',
document,
});

View File

@@ -0,0 +1,13 @@
'use strict';
const utils = require('../utils');
module.exports = (start = null, end = null) => ({
version: 'h004',
orderDetails: {
OrderType: 'Z53',
OrderAttribute: 'DZHNN',
StandardOrderParams: utils.dateRange(start, end),
},
operation: 'download',
});

View File

@@ -12,6 +12,7 @@ const CDS = require('./CDS');
const CCT = require('./CCT'); const CCT = require('./CCT');
const CCS = require('./CCS'); const CCS = require('./CCS');
const XE3 = require('./XE3'); const XE3 = require('./XE3');
const XCT = require('./XCT');
const STA = require('./STA'); const STA = require('./STA');
const VMK = require('./VMK'); const VMK = require('./VMK');
@@ -21,11 +22,13 @@ const HPD = require('./HPD');
const HKD = require('./HKD'); const HKD = require('./HKD');
const PTK = require('./PTK'); const PTK = require('./PTK');
const HAC = require('./HAC'); const HAC = require('./HAC');
const Z53 = require('./Z53');
module.exports = { module.exports = {
INI, INI,
HIA, HIA,
HPB, HPB,
Z53,
AZV, AZV,
CD1, CD1,
@@ -35,7 +38,7 @@ module.exports = {
CCT, CCT,
CCS, CCS,
XE3, XE3,
XCT,
STA, STA,
VMK, VMK,
HAA, HAA,

View File

@@ -58,9 +58,7 @@ module.exports = dir => ({
try { try {
fs.writeFileSync(path, this.traceData); fs.writeFileSync(path, this.traceData);
console.log("Data written to file");
} catch (error) { } catch (error) {
console.log(error);
throw error; throw error;
} }
}, },

18
lib/utils.js Normal file
View File

@@ -0,0 +1,18 @@
'use strict';
const dateRange = (start, end) => {
if (start && end)
return {
DateRange: {
Start: start,
End: end,
},
};
return {};
};
module.exports = {
dateRange,
};

View File

@@ -1,6 +1,6 @@
{ {
"name": "ebics-client", "name": "ebics-client",
"version": "0.0.5", "version": "0.0.8",
"description": "Node.js ISO 20022 Compliant EBICS Client", "description": "Node.js ISO 20022 Compliant EBICS Client",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@@ -21,18 +21,23 @@
{ {
"name": "Vasyl Stashuk", "name": "Vasyl Stashuk",
"url": "https://github.com/vasyas" "url": "https://github.com/vasyas"
},
{
"name": "Yago",
"url": "https://github.com/yagop"
} }
], ],
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"bn.js": "^4.11.8", "bn.js": "^5.0.0",
"handlebars": "^4.0.11", "handlebars": "^4.4.3",
"js2xmlparser": "^3.0.0", "js2xmlparser": "^4.0.0",
"moment": "^2.22.1", "moment": "^2.24.0",
"node-rsa": "^0.4.2", "node-forge": "^0.9.1",
"request": "^2.87.0", "node-rsa": "^1.0.6",
"uuid": "^3.3.2", "request": "^2.88.0",
"xml-crypto": "^0.10.1", "uuid": "^3.3.3",
"xml-crypto": "^1.4.0",
"xmldom": "^0.1.27", "xmldom": "^0.1.27",
"xpath": "0.0.27" "xpath": "0.0.27"
}, },

44
test/exponentHandle.js Normal file
View File

@@ -0,0 +1,44 @@
'use strict';
const { assert } = require('chai');
const H004Response = require('../lib/orders/H004/response');
describe('H004 response parsing', () => {
it('parses bank keys exp', () => {
const response = H004Response('<xml/>', {});
const x002mod = 'AJ2/0OoIZydb9sgKbiwqDcwA0NtcUMIYi6GK7PqoRszu1uytGnxJjQhGt62kMslWvLgebUSwdq/T3YyGsa3KQeIGaUUn9iqyu3BoNOMdo2DLN4NdGMY1WR/HbYRKR3JHyURhTBKw27KScRRdKGl6jY+VcXcTKJL8PjXMQH5cD6Gz'; // 'ALr6oSdqbbpRxlJmKtwTjjaCA1zlA61Vd7NfPML1NxY/U3Dt8galrieRjBM0SDn4vD2+AJCQY4zzkdE2m8u/p+3KGtmZtGaaxs11TNRm9GhtwQGw2hW73RduTSwLZJrSilv/GH18vPar8uvlBAXTmtVEy2kfvT3+t1mvqrut/LnUg3t63nx3WVdWylEdVDmf9Ylp+W+2quLyE0TzTVl5OXhvB3tpVs2B4OfsmOHJAaKIdSuXDMJebPiM9uIlraiJVeRMjkW3Xxmrazc7+kuy8RZ4BBgi+Xve6lNaQHGqxPV5q5SeXR5fS0D2sPeewJWbjhaVRBObIV2ZEoEWKx79pnE=';
const e002mod = 'AOzWaiT7aGESXcI3dqLY3RRD36inlZTGmNNprKd/t9uHfoMeLwZHeMwtjCRWjsuZEyBupkNSFWb3vBlxDyhcyTgpbbbcHsDGqF2zCJaK85xUphoH9mKHxbnA8ZlXzmtHwDmwVSns0FAslIqD+Xr+WycQpeCBEK12D8Ii032YS814ZUKHJ1MkS65A5PE0lcvMTyIE7ruG1kFz85F4nX8eWq77mDEiBONkA5RSUb5duGnRohdNYBgO8K6Wn18aDdISGDyPyXHNvC70v8tfWbF9VGE3rXVGcgjpezZZxC8d47vL0x6lOeslgl7s8N456ntAa+oGHRurt5mEhDz1DZg+EJc=';
const exponent = 'AQAB'; // 'AQAAAA8=';
response.orderData = () => `<?xml version="1.0" encoding="UTF-8"?>
<HPBResponseOrderData xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<AuthenticationPubKeyInfo>
<PubKeyValue>
<ds:RSAKeyValue>
<ds:Modulus>${x002mod}</ds:Modulus>
<ds:Exponent>${exponent}</ds:Exponent>
</ds:RSAKeyValue>
<TimeStamp>2015-02-25T08:01:13.061Z</TimeStamp>
</PubKeyValue>
<AuthenticationVersion>X002</AuthenticationVersion>
</AuthenticationPubKeyInfo>
<EncryptionPubKeyInfo>
<PubKeyValue>
<ds:RSAKeyValue>
<ds:Modulus>${e002mod}</ds:Modulus>
<ds:Exponent>${exponent}</ds:Exponent>
</ds:RSAKeyValue>
<TimeStamp>2015-02-25T08:01:12.344Z</TimeStamp>
</PubKeyValue>
<EncryptionVersion>E002</EncryptionVersion>
</EncryptionPubKeyInfo>
<HostID>SBKPR01</HostID>
</HPBResponseOrderData>`;
const bankKeys = response.bankKeys();
assert.equal(bankKeys.bankX002.mod.toString('base64'), x002mod);
assert.equal(bankKeys.bankE002.mod.toString('base64'), e002mod);
});
});

43
test/fixtures/keys.js vendored Normal file
View File

@@ -0,0 +1,43 @@
'use strict';
const publicSmallExpPEM = '';
const publicSmallExpMod = '';
const publicSmallExpExp = '';
const publicBigPEM = `-----BEGIN PUBLIC KEY-----
MIIBJDANBgkqhkiG9w0BAQEFAAOCAREAMIIBDAKCAQEAuvqhJ2ptulHGUmYq3BOO
NoIDXOUDrVV3s188wvU3Fj9TcO3yBqWuJ5GMEzRIOfi8Pb4AkJBjjPOR0Taby7+n
7coa2Zm0ZprGzXVM1Gb0aG3BAbDaFbvdF25NLAtkmtKKW/8YfXy89qvy6+UEBdOa
1UTLaR+9Pf63Wa+qu638udSDe3refHdZV1bKUR1UOZ/1iWn5b7aq4vITRPNNWXk5
eG8He2lWzYHg5+yY4ckBooh1K5cMwl5s+Iz24iWtqIlV5EyORbdfGatrNzv6S7Lx
FngEGCL5e97qU1pAcarE9XmrlJ5dHl9LQPaw957AlZuOFpVEE5shXZkSgRYrHv2m
cQIFAQAAAA8=
-----END PUBLIC KEY-----`;
const publicBigModString = 'ALr6oSdqbbpRxlJmKtwTjjaCA1zlA61Vd7NfPML1NxY/U3Dt8galrieRjBM0SDn4vD2+AJCQY4zzkdE2m8u/p+3KGtmZtGaaxs11TNRm9GhtwQGw2hW73RduTSwLZJrSilv/GH18vPar8uvlBAXTmtVEy2kfvT3+t1mvqrut/LnUg3t63nx3WVdWylEdVDmf9Ylp+W+2quLyE0TzTVl5OXhvB3tpVs2B4OfsmOHJAaKIdSuXDMJebPiM9uIlraiJVeRMjkW3Xxmrazc7+kuy8RZ4BBgi+Xve6lNaQHGqxPV5q5SeXR5fS0D2sPeewJWbjhaVRBObIV2ZEoEWKx79pnE=';
const publicBigModBytes = [0, 186, 250, 161, 39, 106, 109, 186, 81, 198, 82, 102, 42, 220, 19, 142, 54, 130, 3, 92, 229, 3, 173, 85, 119, 179, 95, 60, 194, 245, 55, 22, 63, 83, 112, 237, 242, 6, 165, 174, 39, 145, 140, 19, 52, 72, 57, 248, 188, 61, 190, 0, 144, 144, 99, 140, 243, 145, 209, 54, 155, 203, 191, 167, 237, 202, 26, 217, 153, 180, 102, 154, 198, 205, 117, 76, 212, 102, 244, 104, 109, 193, 1, 176, 218, 21, 187, 221, 23, 110, 77, 44, 11, 100, 154, 210, 138, 91, 255, 24, 125, 124, 188, 246, 171, 242, 235, 229, 4, 5, 211, 154, 213, 68, 203, 105, 31, 189, 61, 254, 183, 89, 175, 170, 187, 173, 252, 185, 212, 131, 123, 122, 222, 124, 119, 89, 87, 86, 202, 81, 29, 84, 57, 159, 245, 137, 105, 249, 111, 182, 170, 226, 242, 19, 68, 243, 77, 89, 121, 57, 120, 111, 7, 123, 105, 86, 205, 129, 224, 231, 236, 152, 225, 201, 1, 162, 136, 117, 43, 151, 12, 194, 94, 108, 248, 140, 246, 226, 37, 173, 168, 137, 85, 228, 76, 142, 69, 183, 95, 25, 171, 107, 55, 59, 250, 75, 178, 241, 22, 120, 4, 24, 34, 249, 123, 222, 234, 83, 90, 64, 113, 170, 196, 245, 121, 171, 148, 158, 93, 30, 95, 75, 64, 246, 176, 247, 158, 192, 149, 155, 142, 22, 149, 68, 19, 155, 33, 93, 153, 18, 129, 22, 43, 30, 253, 166, 113];
const publicBigExpString = 'AQAAAA8=';
const publicBigExpBytes = [1, 0, 0, 0, 15];
const pblc = {
big: {
pem: publicBigPEM,
mod: {
bytes: publicBigModBytes,
string: publicBigModString,
},
exp: {
bytes: publicBigExpBytes,
string: publicBigExpString,
},
},
small: {
pem: publicSmallExpPEM,
mod: publicSmallExpMod,
exp: publicSmallExpExp,
},
};
module.exports = {
pblc,
};

90
test/keys.js Normal file
View File

@@ -0,0 +1,90 @@
'use strict';
const { assert } = require('chai');
const fixtures = require('./fixtures/keys');
// const Key = require('../lib/keymanagers/keyRSA');
const Key = require('../lib/keymanagers/Key');
const stripWhitespace = str => str.replace(/\s+/g, '');
describe('Keys management', () => {
describe('generates brand new', () => {
const keySize = 2048;
// const newKey = Key().generate(keySize);
const newKey = Key.generate();
it('private key', () => {
assert.isTrue(newKey.isPrivate());
});
it('that has the right key size', () => {
// const newKeySize = newKey.size(); // console.log(newKey.size());
assert(newKey.size(), keySize);
});
});
describe('creates public key from mod and exp', () => {
const { pem, mod, exp } = fixtures.pblc.big;
describe('that are strings', () => {
const m = Buffer.from(mod.string, 'base64');
const e = Buffer.from(exp.string, 'base64');
/* const newKey = Key().importKey({
mod: m, exp: e, modulus: mod.string, exponent: exp.string,
}); */
const newKey = new Key({ mod: m, exp: e });
it('and is really public', () => {
assert.isTrue(newKey.isPublic());
});
it('and has a propper mod in bytes', () => {
assert.deepEqual([...newKey.n()], mod.bytes);
});
it('and has a propper pem string', () => {
assert.equal(stripWhitespace(newKey.toPem()), stripWhitespace(pem));
});
});
describe('that are bytes', () => {
const m = Buffer.from(mod.bytes);
const e = Buffer.from(exp.bytes);
/* const newKey = Key().importKey({
mod: m, exp: e, modulus: mod.string, exponent: exp.string,
}); */
const newKey = new Key({ mod: m, exp: e });
it('and is really public', () => {
assert.isTrue(newKey.isPublic());
});
it('and has a propper mod as a string', () => {
assert.equal(newKey.n().toString('base64'), mod.string);
});
it('and has a propper pem string', () => {
assert.equal(stripWhitespace(newKey.toPem()), stripWhitespace(pem));
});
});
});
describe('creates public key from pem string', () => {
const { pem } = fixtures.pblc.big;
// const newKey = Key(pem);
const newKey = new Key({ pem });
it('and is really public', () => {
assert.isTrue(newKey.isPublic());
});
it('and has a propper(the same) pem string', () => {
/* newKey.pempem = {
modulus: mod.string,
exponent: exp.string,
}; */
assert.equal(stripWhitespace(newKey.toPem()), stripWhitespace(pem));
});
});
});