From 59a281c8951bf81482da2318c42a6c1f308e613c Mon Sep 17 00:00:00 2001 From: Vladislav Hristov Date: Fri, 1 Nov 2019 13:49:33 +0200 Subject: [PATCH] feat: use new Key in Keys manager --- lib/keymanagers/Keys.js | 16 +++++++---- lib/keymanagers/_Keys.js | 59 +++++++++++++++++++++++++++++++++++++++ lib/keymanagers/keyRSA.js | 15 ++++++++-- 3 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 lib/keymanagers/_Keys.js diff --git a/lib/keymanagers/Keys.js b/lib/keymanagers/Keys.js index fc110c7..1762642 100644 --- a/lib/keymanagers/Keys.js +++ b/lib/keymanagers/Keys.js @@ -1,8 +1,14 @@ '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 { constructor({ @@ -25,15 +31,15 @@ module.exports = class Keys { const keys = {}; Object.keys({ A006: '', X002: '', E002: '' }).forEach((key) => { - keys[key] = Key().generate(); + keys[key] = Key.generate(); // Key().generate(); }); return new Keys(keys); } setBankKeys(bankKeys) { - this.keys.bankX002 = Key().importKey(bankKeys.bankX002); - this.keys.bankE002 = Key().importKey(bankKeys.bankE002); + this.keys.bankX002 = new Key(bankKeys.bankX002); // Key().importKey(bankKeys.bankX002); + this.keys.bankE002 = new Key(bankKeys.bankE002); // Key().importKey(bankKeys.bankE002); } a() { diff --git a/lib/keymanagers/_Keys.js b/lib/keymanagers/_Keys.js new file mode 100644 index 0000000..94fb1ce --- /dev/null +++ b/lib/keymanagers/_Keys.js @@ -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; + } +}; diff --git a/lib/keymanagers/keyRSA.js b/lib/keymanagers/keyRSA.js index d48d3c1..e359329 100644 --- a/lib/keymanagers/keyRSA.js +++ b/lib/keymanagers/keyRSA.js @@ -76,9 +76,18 @@ function rsaPublicKeyPem(modulus_b64, exponent_b64) { const BN = require('bn.js'); const NodeRSA = require('node-rsa'); -const { pki: { - rsa, publicKeyToPem, privateKeyToPem, publicKeyFromPem, privateKeyFromPem -}, jsbn: { BigInteger } } = require('node-forge'); +const { + pki: { + rsa, + publicKeyToPem, + privateKeyToPem, + publicKeyFromPem, + privateKeyFromPem, + }, + jsbn: { + BigInteger, + }, +} = require('node-forge'); const isKeyInstance = (obj) => { if (typeof obj !== 'object')