diff --git a/lib/Client.js b/lib/Client.js index 1ed7c92..78963ca 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -51,14 +51,15 @@ module.exports = class Client { this.keyEncryptor = defaultKeyEncryptor({ passphrase }); } - send(order) { + async send(order) { const isInObject = ('operation' in order); if (!isInObject) throw new Error('Operation for the order needed'); if (order.operation.toUpperCase() === constants.orderOperations.ini) return this.initialization(order); - if (this.keys() === null) throw new Error('No keys provided. Can not send the order or any other order for that matter.'); + const keys = await this.keys(); + if (keys === null) throw new Error('No keys provided. Can not send the order or any other order for that matter.'); if (order.operation.toUpperCase() === constants.orderOperations.upload) return this.upload(order); if (order.operation.toUpperCase() === constants.orderOperations.download) return this.download(order); @@ -67,7 +68,8 @@ module.exports = class Client { } async initialization(order) { - if (this.keys() === null) this._generateKeys(); + const keys = await this.keys(); + if (keys === null) this._generateKeys(); const res = await this.ebicsRequest(order); const xml = res.orderData(); @@ -110,20 +112,22 @@ module.exports = class Client { } ebicsRequest(order) { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { const { version } = order; - const keys = this.keys(); + const keys = await this.keys(); $request.post({ url: this.url, - body: signer.version(version).sign(serializer.use(order, this).toXML(), keys.x()), + body: signer.version(version).sign((await serializer.use(order, this)).toXML(), keys.x()), headers: { 'content-type': 'text/xml;charset=UTF-8' }, }, (err, res, data) => (err ? reject(err) : resolve(response.version(version)(data, keys)))); }); } - keys() { - return this._readKeys(); + async keys() { + const keysString = await this._readKeys(); + + return keysString ? new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))) : null; } _generateKeys() { @@ -132,20 +136,28 @@ module.exports = class Client { this._writeKeys(keysObject); } - setBankKeys(bankKeys) { - const keysObject = this.keys(); + async setBankKeys(bankKeys) { + const keysObject = await this.keys(); keysObject.setBankKeys(bankKeys); - this._writeKeys(keysObject); + await this._writeKeys(keysObject); } - _readKeys() { - const keysString = this.keyStorage.read(); + async _readKeys() { + try { + const keys = await this.keyStorage.read(); - return keysString ? new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))) : null; + return keys; + } catch (err) { + return null; + } } - _writeKeys(keysObject) { - this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys))); + async _writeKeys(keysObject) { + try { + await this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys))); + } catch (err) { + throw err; + } } }; diff --git a/lib/orders/H004/serializers/download.js b/lib/orders/H004/serializers/download.js index 4788ee0..4ae2b99 100644 --- a/lib/orders/H004/serializers/download.js +++ b/lib/orders/H004/serializers/download.js @@ -7,8 +7,8 @@ const Crypto = require('../../../crypto/Crypto'); const genericSerializer = require('./generic'); module.exports = { - use(order, client) { - const keys = client.keys(); + async use(order, client) { + const keys = await client.keys(); const ebicsAccount = { partnerId: client.partnerId, userId: client.userId, diff --git a/lib/orders/H004/serializers/ini.js b/lib/orders/H004/serializers/ini.js index 2c1f773..195df23 100644 --- a/lib/orders/H004/serializers/ini.js +++ b/lib/orders/H004/serializers/ini.js @@ -118,8 +118,8 @@ const process = { }; module.exports = { - use(order, client) { - const keys = client.keys(); + async use(order, client) { + const keys = await client.keys(); const { orderDetails, transactionId } = order; const { xmlOptions, xmlSchema, productString } = genericSerializer(client.host, transactionId); const orderType = orderDetails.OrderType.toUpperCase(); diff --git a/lib/orders/H004/serializers/upload.js b/lib/orders/H004/serializers/upload.js index 84acdd1..1971088 100644 --- a/lib/orders/H004/serializers/upload.js +++ b/lib/orders/H004/serializers/upload.js @@ -47,19 +47,17 @@ const encryptedOrderData = (document, transactionKey) => { }; module.exports = { - use(order, client) { - const keys = client.keys(); + async use(order, client) { + const keys = await client.keys(); const ebicsAccount = { partnerId: client.partnerId, userId: client.userId, hostId: client.hostId, }; - const { - transactionId, document, - } = order; + const { transactionId, document } = order; const { rootName, xmlOptions, xmlSchema, transfer, - } = downloadSerializer.use(order, client); + } = await downloadSerializer.use(order, client); this.rootName = rootName; this.xmlOptions = xmlOptions; diff --git a/lib/storages/fsKeysStorage.js b/lib/storages/fsKeysStorage.js index 297f8e6..a54835a 100644 --- a/lib/storages/fsKeysStorage.js +++ b/lib/storages/fsKeysStorage.js @@ -6,14 +6,24 @@ module.exports = (pathToFile) => { const path = pathToFile; return { - read() { - if (!fs.existsSync(path)) - return null; - return fs.readFileSync(path, { encoding: 'utf8' }); + write(data) { + return new Promise((resolve, reject) => { + fs.writeFile(path, data, { encoding: 'utf8' }, (error) => { + if (error) throw error; + + return resolve(); + }); + }); }, - write(data) { - fs.writeFileSync(path, data, { encoding: 'utf8' }); + read() { + return new Promise((resolve, reject) => { + fs.readFile(path, { encoding: 'utf8' }, (error, data) => { + if (error) reject(error); + + return resolve(data); + }); + }); }, }; };