client and order optimization

This commit is contained in:
Vladislav Hristov
2018-06-20 12:20:03 +03:00
parent 187636019c
commit 945499290a
15 changed files with 259 additions and 194 deletions

View File

@@ -3,26 +3,22 @@
const BN = require('bn.js');
const NodeRSA = require('node-rsa');
module.exports = class Key {
constructor(encodedKey/* , passphrase = null */) {
this._key = (encodedKey instanceof NodeRSA) ? encodedKey : new NodeRSA(encodedKey);
}
const keyOrNull = (encodedKey) => {
if (encodedKey === null) return new NodeRSA();
static generate(keysize = 2048) {
return new NodeRSA({ b: keysize });
}
return (encodedKey instanceof NodeRSA) ? encodedKey : new NodeRSA(encodedKey);
};
static importKey({ mod, exp }) {
const key = new NodeRSA();
module.exports = encodedKey => ({
key: keyOrNull(encodedKey),
key.importKey({ n: mod, e: exp }, 'components-public');
generate(keySize = 2048) {
return new NodeRSA({ b: keySize });
},
return new Key(key);
}
get key() {
return this._key;
}
importKey({ mod, exp }) {
this.key.importKey({ n: mod, e: exp }, 'components-public');
},
n(to = 'buff') {
const keyN = Buffer.from(this.key.exportKey('components-public').n);
@@ -30,7 +26,7 @@ module.exports = class Key {
return to === 'hex'
? keyN.toString('hex', 1)
: keyN;
}
},
e(to = 'buff') {
const eKey = new BN(this.key.exportKey('components-public').e).toBuffer();
@@ -38,13 +34,13 @@ module.exports = class Key {
return to === 'hex'
? eKey.toString('hex')
: eKey;
}
},
d() {
return this.key.keyPair.d.toBuffer();
}
},
toPem() {
return this.key.isPrivate() ? this.key.exportKey('pkcs1-private-pem') : this.key.exportKey('pkcs8-public-pem');
}
};
},
});

View File

@@ -1,8 +1,8 @@
'use strict';
const Key = require('./Key');
const Key = require('./key');
const keyOrNull = key => (key ? new Key(key) : null);
const keyOrNull = key => (key ? Key(key) : null);
module.exports = class Keys {
constructor({
@@ -32,8 +32,8 @@ module.exports = class Keys {
}
setBankKeys(bankKeys) {
this.keys.bankX002 = Key.importKey(bankKeys.bankX002);
this.keys.bankE002 = Key.importKey(bankKeys.bankE002);
this.keys.bankX002.importKey(bankKeys.bankX002);
this.keys.bankE002.importKey(bankKeys.bankE002);
}
a() {

View File

@@ -0,0 +1,24 @@
'use strict';
const crypto = require('crypto');
const encrypt = (data, algorithm, passphrase) => {
const cipher = crypto.createCipher(algorithm, passphrase);
const encrypted = cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
return Buffer.from(encrypted).toString('base64');
};
const decrypt = (data, algorithm, passphrase) => {
data = (Buffer.from(data, 'base64')).toString();
const decipher = crypto.createDecipher(algorithm, passphrase);
const decrypted = decipher.update(data, 'hex', 'utf8') + decipher.final('utf8');
return decrypted;
};
module.exports = ({
passphrase,
algorithm = 'aes-256-cbc',
}) => ({
encrypt: data => encrypt(data, algorithm, passphrase),
decrypt: data => decrypt(data, algorithm, passphrase),
});

View File

@@ -1,26 +0,0 @@
'use strict';
const fs = require('fs');
module.exports = (pathToFile) => {
const path = pathToFile;
return {
read() {
return fs.readFileSync(path, { encoding: 'utf8' });
},
save(data) {
fs.writeFileSync(path, data, { encoding: 'utf8' });
return this;
},
hasData() {
if (fs.existsSync(path))
return this.read() !== '';
return false;
},
};
};