@super-protocol/sdk-js 0.15.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/build/TIIGenerator.js +129 -239
  2. package/build/connectors/BaseConnector.js +17 -47
  3. package/build/connectors/BlockchainConnector.js +222 -348
  4. package/build/connectors/BlockchainEventsListener.js +38 -92
  5. package/build/contracts/app.json +466 -35
  6. package/build/crypto/Crypto.js +63 -135
  7. package/build/crypto/index.js +1 -1
  8. package/build/crypto/nodejs/AES.js +62 -119
  9. package/build/crypto/nodejs/ARIA.js +61 -118
  10. package/build/crypto/nodejs/ECIES.js +51 -87
  11. package/build/crypto/nodejs/NativeCrypto.js +64 -118
  12. package/build/crypto/nodejs/RSA-Hybrid.js +54 -113
  13. package/build/index.d.ts +5 -0
  14. package/build/index.js +14 -2
  15. package/build/logger.d.ts +3 -1
  16. package/build/logger.js +2 -2
  17. package/build/models/EtlModel.js +65 -114
  18. package/build/models/Offer.js +227 -462
  19. package/build/models/Order.d.ts +4 -0
  20. package/build/models/Order.js +326 -559
  21. package/build/models/Provider.d.ts +2 -0
  22. package/build/models/Provider.js +74 -153
  23. package/build/models/TCB.js +67 -171
  24. package/build/models/TeeOffer.d.ts +1 -0
  25. package/build/models/TeeOffer.js +389 -731
  26. package/build/proto/Compression.js +20 -21
  27. package/build/proto/TRI.js +46 -50
  28. package/build/proto/TeeProperties.js +60 -66
  29. package/build/providers/storage/ChunksDownloadDecorator.js +125 -235
  30. package/build/providers/storage/S3StorageProvider.js +173 -298
  31. package/build/providers/storage/StorageAdapter.d.ts +60 -0
  32. package/build/providers/storage/StorageAdapter.js +317 -0
  33. package/build/providers/storage/StorageContentWriter.d.ts +39 -0
  34. package/build/providers/storage/StorageContentWriter.js +181 -0
  35. package/build/providers/storage/StorageKeyValueAdapter.d.ts +20 -0
  36. package/build/providers/storage/StorageKeyValueAdapter.js +152 -0
  37. package/build/providers/storage/StorageMetadataReader.d.ts +19 -0
  38. package/build/providers/storage/StorageMetadataReader.js +65 -0
  39. package/build/providers/storage/StorjAdapter.d.ts +19 -0
  40. package/build/providers/storage/StorjAdapter.js +62 -0
  41. package/build/providers/storage/StorjStorageProvider.js +138 -315
  42. package/build/providers/storage/getStorageProvider.js +7 -7
  43. package/build/providers/storage/types.d.ts +7 -0
  44. package/build/providers/storage/types.js +2 -0
  45. package/build/staticModels/ActiveOffers.js +32 -100
  46. package/build/staticModels/ActiveOrders.js +42 -120
  47. package/build/staticModels/BaseStaticModel.js +7 -10
  48. package/build/staticModels/Consensus.d.ts +4 -1
  49. package/build/staticModels/Consensus.js +164 -220
  50. package/build/staticModels/Deposits.js +109 -201
  51. package/build/staticModels/Marks.js +21 -82
  52. package/build/staticModels/ModelPackager.js +34 -84
  53. package/build/staticModels/Offers.js +140 -266
  54. package/build/staticModels/Orders.d.ts +1 -0
  55. package/build/staticModels/Orders.js +286 -429
  56. package/build/staticModels/ProviderRegistry.js +114 -225
  57. package/build/staticModels/Superpro.js +17 -63
  58. package/build/staticModels/SuperproToken.js +90 -169
  59. package/build/staticModels/TeeOffers.js +220 -416
  60. package/build/store.js +2 -2
  61. package/build/types/HardwareInfo.js +2 -2
  62. package/build/types/Order.js +4 -4
  63. package/build/types/Provider.js +1 -1
  64. package/build/types/TeeOfferInfo.js +1 -1
  65. package/build/types/TeeOfferOption.js +2 -2
  66. package/build/types/TeeOfferSlot.js +2 -2
  67. package/build/types/ValueOfferSlot.js +3 -3
  68. package/build/utils/Monitoring.js +24 -28
  69. package/build/utils/NonceTracker.js +56 -121
  70. package/build/utils/PubSub.d.ts +9 -0
  71. package/build/utils/PubSub.js +36 -0
  72. package/build/utils/TxManager.js +121 -215
  73. package/build/utils/compressors/GzipCompressor.js +13 -50
  74. package/build/utils/compressors/UncompressedCompressor.js +10 -13
  75. package/build/utils/helpers/tryWithInterval.js +34 -78
  76. package/build/utils/resourceLoaders/BaseResourceLoader.js +26 -78
  77. package/build/utils/resourceLoaders/StorageProviderLoader.js +33 -97
  78. package/build/utils/resourceLoaders/UrlResourceLoader.js +29 -93
  79. package/build/utils/resourceLoaders/getResourceLoader.js +6 -6
  80. package/build/utils.js +60 -105
  81. package/package.json +4 -1
@@ -8,62 +8,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
13
  };
41
14
  Object.defineProperty(exports, "__esModule", { value: true });
42
- var dto_js_1 = require("@super-protocol/dto-js");
43
- var NativeCrypto_1 = __importDefault(require("./NativeCrypto"));
44
- var ARIA = /** @class */ (function () {
45
- function ARIA() {
46
- }
47
- ARIA.encrypt = function (content, encryption) {
48
- return __awaiter(this, void 0, void 0, function () {
49
- var keyBuffer, encrypted;
50
- return __generator(this, function (_a) {
51
- if (!encryption.key)
52
- throw Error("Encryption key is not provided");
53
- encryption.cipher = encryption.cipher || dto_js_1.Cipher.ARIA_256_GCM;
54
- keyBuffer = Buffer.from(encryption.key, encryption.encoding);
55
- encrypted = NativeCrypto_1.default.encrypt(keyBuffer, content, encryption.cipher);
56
- return [2 /*return*/, {
57
- algo: dto_js_1.CryptoAlgorithm.ARIA,
58
- encoding: encryption.encoding,
59
- cipher: encryption.cipher,
60
- ciphertext: encrypted.ciphertext,
61
- iv: encrypted.iv,
62
- mac: encrypted.mac,
63
- }];
64
- });
15
+ const dto_js_1 = require("@super-protocol/dto-js");
16
+ const NativeCrypto_1 = __importDefault(require("./NativeCrypto"));
17
+ class ARIA {
18
+ static encrypt(content, encryption) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ if (!encryption.key)
21
+ throw Error("Encryption key is not provided");
22
+ encryption.cipher = encryption.cipher || dto_js_1.Cipher.ARIA_256_GCM;
23
+ const keyBuffer = Buffer.from(encryption.key, encryption.encoding);
24
+ const encrypted = NativeCrypto_1.default.encrypt(keyBuffer, content, encryption.cipher);
25
+ return {
26
+ algo: dto_js_1.CryptoAlgorithm.ARIA,
27
+ encoding: encryption.encoding,
28
+ cipher: encryption.cipher,
29
+ ciphertext: encrypted.ciphertext,
30
+ iv: encrypted.iv,
31
+ mac: encrypted.mac,
32
+ };
65
33
  });
66
- };
34
+ }
67
35
  /**
68
36
  * Encrypts data stream
69
37
  * @param inputStream - path to file that will be encrypted
@@ -72,80 +40,55 @@ var ARIA = /** @class */ (function () {
72
40
  * @param key – key that will be used to encrypt data
73
41
  * @returns {Promise<Encryption>} - encryption info
74
42
  */
75
- ARIA.encryptStream = function (inputStream, outputStream, encryption) {
76
- return __awaiter(this, void 0, void 0, function () {
77
- var keyBuffer, encrypted;
78
- return __generator(this, function (_a) {
79
- switch (_a.label) {
80
- case 0:
81
- if (!encryption.key)
82
- throw Error("Encryption key is not provided");
83
- encryption.cipher = encryption.cipher || dto_js_1.Cipher.ARIA_256_GCM;
84
- keyBuffer = Buffer.from(encryption.key, encryption.encoding);
85
- return [4 /*yield*/, NativeCrypto_1.default.encryptStream(keyBuffer, inputStream, outputStream, encryption.cipher)];
86
- case 1:
87
- encrypted = _a.sent();
88
- return [2 /*return*/, {
89
- algo: dto_js_1.CryptoAlgorithm.ARIA,
90
- encoding: encryption.encoding,
91
- cipher: encryption.cipher,
92
- iv: encrypted.iv,
93
- mac: encrypted.mac,
94
- }];
95
- }
96
- });
43
+ static encryptStream(inputStream, outputStream, encryption) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ if (!encryption.key)
46
+ throw Error("Encryption key is not provided");
47
+ encryption.cipher = encryption.cipher || dto_js_1.Cipher.ARIA_256_GCM;
48
+ const keyBuffer = Buffer.from(encryption.key, encryption.encoding);
49
+ const encrypted = yield NativeCrypto_1.default.encryptStream(keyBuffer, inputStream, outputStream, encryption.cipher);
50
+ return {
51
+ algo: dto_js_1.CryptoAlgorithm.ARIA,
52
+ encoding: encryption.encoding,
53
+ cipher: encryption.cipher,
54
+ iv: encrypted.iv,
55
+ mac: encrypted.mac,
56
+ };
97
57
  });
98
- };
99
- ARIA.decrypt = function (encryption) {
100
- return __awaiter(this, void 0, void 0, function () {
101
- var key, params;
102
- return __generator(this, function (_a) {
103
- switch (_a.label) {
104
- case 0:
105
- if (!encryption.key)
106
- throw Error("Decryption key is not provided");
107
- key = Buffer.from(encryption.key, encryption.encoding);
108
- params = {
109
- iv: Buffer.from(encryption.iv, encryption.encoding),
110
- };
111
- if (encryption.mac) {
112
- params.mac = Buffer.from(encryption.mac, encryption.encoding);
113
- }
114
- return [4 /*yield*/, NativeCrypto_1.default.decrypt(key, encryption.ciphertext, encryption.cipher, params, encryption.encoding)];
115
- case 1: return [2 /*return*/, _a.sent()];
116
- }
117
- });
58
+ }
59
+ static decrypt(encryption) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ if (!encryption.key)
62
+ throw Error("Decryption key is not provided");
63
+ const key = Buffer.from(encryption.key, encryption.encoding);
64
+ const params = {
65
+ iv: Buffer.from(encryption.iv, encryption.encoding),
66
+ };
67
+ if (encryption.mac) {
68
+ params.mac = Buffer.from(encryption.mac, encryption.encoding);
69
+ }
70
+ return yield NativeCrypto_1.default.decrypt(key, encryption.ciphertext, encryption.cipher, params, encryption.encoding);
118
71
  });
119
- };
72
+ }
120
73
  /**
121
74
  * Decrypts data stream
122
75
  * @param inputStream - stream with data to decrypt
123
76
  * @param outputStream - stream where the decrypted data will be written
124
77
  * @param encryption – encryption info
125
78
  */
126
- ARIA.decryptStream = function (inputStream, outputStream, encryption) {
127
- return __awaiter(this, void 0, void 0, function () {
128
- var key, params;
129
- return __generator(this, function (_a) {
130
- switch (_a.label) {
131
- case 0:
132
- if (!encryption.key)
133
- throw Error("Decryption key is not provided");
134
- key = Buffer.from(encryption.key, encryption.encoding);
135
- params = {
136
- iv: Buffer.from(encryption.iv, encryption.encoding),
137
- };
138
- if (encryption.mac) {
139
- params.mac = Buffer.from(encryption.mac, encryption.encoding);
140
- }
141
- return [4 /*yield*/, NativeCrypto_1.default.decryptStream(key, inputStream, outputStream, encryption.cipher, params)];
142
- case 1:
143
- _a.sent();
144
- return [2 /*return*/];
145
- }
146
- });
79
+ static decryptStream(inputStream, outputStream, encryption) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ if (!encryption.key)
82
+ throw Error("Decryption key is not provided");
83
+ const key = Buffer.from(encryption.key, encryption.encoding);
84
+ const params = {
85
+ iv: Buffer.from(encryption.iv, encryption.encoding),
86
+ };
87
+ if (encryption.mac) {
88
+ params.mac = Buffer.from(encryption.mac, encryption.encoding);
89
+ }
90
+ yield NativeCrypto_1.default.decryptStream(key, inputStream, outputStream, encryption.cipher, params);
147
91
  });
148
- };
149
- return ARIA;
150
- }());
92
+ }
93
+ }
151
94
  exports.default = ARIA;
@@ -8,97 +8,61 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
13
  };
41
14
  Object.defineProperty(exports, "__esModule", { value: true });
42
- var dto_js_1 = require("@super-protocol/dto-js");
43
- var crypto_1 = __importDefault(require("crypto"));
44
- var ECIES = /** @class */ (function () {
45
- function ECIES() {
46
- }
47
- ECIES.encrypt = function (content, encryption) {
48
- return __awaiter(this, void 0, void 0, function () {
49
- var ecdh, epk, pk, hash, cipherKey, macKey, iv, cipher, ct, dataToMac, mac;
50
- return __generator(this, function (_a) {
51
- if (!encryption.key)
52
- throw Error("Encryption key is not provided");
53
- ecdh = crypto_1.default.createECDH("secp256k1");
54
- ecdh.generateKeys("binary", "uncompressed");
55
- epk = ecdh.getPublicKey();
56
- pk = ecdh.computeSecret(Buffer.from(encryption.key, encryption.encoding));
57
- hash = crypto_1.default.createHash("sha512").update(pk).digest();
58
- cipherKey = hash.slice(0, 32), macKey = hash.slice(32);
59
- iv = crypto_1.default.randomBytes(16);
60
- cipher = crypto_1.default.createCipheriv("aes-256-cbc", cipherKey, iv);
61
- ct = cipher.update(content);
62
- ct = Buffer.concat([ct, cipher.final()]);
63
- dataToMac = Buffer.concat([iv, epk, ct]);
64
- mac = crypto_1.default.createHmac("sha256", macKey).update(dataToMac).digest();
65
- return [2 /*return*/, {
66
- iv: iv.toString(encryption.encoding),
67
- ephemPublicKey: epk.toString(encryption.encoding),
68
- mac: mac.toString(encryption.encoding),
69
- encoding: encryption.encoding,
70
- algo: dto_js_1.CryptoAlgorithm.ECIES,
71
- ciphertext: ct.toString(encryption.encoding),
72
- }];
73
- });
15
+ const dto_js_1 = require("@super-protocol/dto-js");
16
+ const crypto_1 = __importDefault(require("crypto"));
17
+ class ECIES {
18
+ static encrypt(content, encryption) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ if (!encryption.key)
21
+ throw Error("Encryption key is not provided");
22
+ const ecdh = crypto_1.default.createECDH("secp256k1");
23
+ ecdh.generateKeys("binary", "uncompressed");
24
+ const epk = ecdh.getPublicKey();
25
+ const pk = ecdh.computeSecret(Buffer.from(encryption.key, encryption.encoding));
26
+ const hash = crypto_1.default.createHash("sha512").update(pk).digest();
27
+ const cipherKey = hash.slice(0, 32), macKey = hash.slice(32);
28
+ const iv = crypto_1.default.randomBytes(16);
29
+ const cipher = crypto_1.default.createCipheriv("aes-256-cbc", cipherKey, iv);
30
+ let ct = cipher.update(content);
31
+ ct = Buffer.concat([ct, cipher.final()]);
32
+ const dataToMac = Buffer.concat([iv, epk, ct]);
33
+ const mac = crypto_1.default.createHmac("sha256", macKey).update(dataToMac).digest();
34
+ return {
35
+ iv: iv.toString(encryption.encoding),
36
+ ephemPublicKey: epk.toString(encryption.encoding),
37
+ mac: mac.toString(encryption.encoding),
38
+ encoding: encryption.encoding,
39
+ algo: dto_js_1.CryptoAlgorithm.ECIES,
40
+ ciphertext: ct.toString(encryption.encoding),
41
+ };
74
42
  });
75
- };
76
- ECIES.decrypt = function (encryption) {
77
- return __awaiter(this, void 0, void 0, function () {
78
- var iv, epk, ct, mac, ecdh, pk, hash, cipherKey, macKey, m, decipher, pt, result;
79
- return __generator(this, function (_a) {
80
- if (!encryption.key)
81
- throw Error("Decryption key is not provided");
82
- iv = Buffer.from(encryption.iv, encryption.encoding), epk = Buffer.from(encryption.ephemPublicKey, encryption.encoding), ct = Buffer.from(encryption.ciphertext, encryption.encoding), mac = Buffer.from(encryption.mac, encryption.encoding);
83
- ecdh = crypto_1.default.createECDH("secp256k1");
84
- ecdh.setPrivateKey(Buffer.from(encryption.key, encryption.encoding));
85
- pk = ecdh.computeSecret(epk);
86
- hash = crypto_1.default.createHash("sha512").update(pk).digest();
87
- cipherKey = hash.slice(0, 32), macKey = hash.slice(32);
88
- m = crypto_1.default
89
- .createHmac("sha256", macKey)
90
- .update(Buffer.concat([iv, epk, ct]))
91
- .digest();
92
- if (m.compare(mac) !== 0 || mac.compare(m) !== 0) {
93
- throw new Error("Corrupted Ecies body: unmatched authentication code");
94
- }
95
- decipher = crypto_1.default.createDecipheriv("aes-256-cbc", cipherKey, iv);
96
- pt = decipher.update(ct);
97
- result = Buffer.concat([pt, decipher.final()]);
98
- return [2 /*return*/, result.toString("binary")];
99
- });
43
+ }
44
+ static decrypt(encryption) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ if (!encryption.key)
47
+ throw Error("Decryption key is not provided");
48
+ const iv = Buffer.from(encryption.iv, encryption.encoding), epk = Buffer.from(encryption.ephemPublicKey, encryption.encoding), ct = Buffer.from(encryption.ciphertext, encryption.encoding), mac = Buffer.from(encryption.mac, encryption.encoding);
49
+ const ecdh = crypto_1.default.createECDH("secp256k1");
50
+ ecdh.setPrivateKey(Buffer.from(encryption.key, encryption.encoding));
51
+ const pk = ecdh.computeSecret(epk);
52
+ const hash = crypto_1.default.createHash("sha512").update(pk).digest();
53
+ const cipherKey = hash.slice(0, 32), macKey = hash.slice(32);
54
+ const m = crypto_1.default
55
+ .createHmac("sha256", macKey)
56
+ .update(Buffer.concat([iv, epk, ct]))
57
+ .digest();
58
+ if (m.compare(mac) !== 0 || mac.compare(m) !== 0) {
59
+ throw new Error("Corrupted Ecies body: unmatched authentication code");
60
+ }
61
+ const decipher = crypto_1.default.createDecipheriv("aes-256-cbc", cipherKey, iv);
62
+ const pt = decipher.update(ct);
63
+ const result = Buffer.concat([pt, decipher.final()]);
64
+ return result.toString("binary");
100
65
  });
101
- };
102
- return ECIES;
103
- }());
66
+ }
67
+ }
104
68
  exports.default = ECIES;
@@ -8,43 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
11
  Object.defineProperty(exports, "__esModule", { value: true });
39
- var crypto_1 = require("crypto");
40
- var events_1 = require("events");
41
- var dto_js_1 = require("@super-protocol/dto-js");
12
+ const crypto_1 = require("crypto");
13
+ const events_1 = require("events");
14
+ const dto_js_1 = require("@super-protocol/dto-js");
42
15
  /**
43
16
  *
44
17
  */
45
- var NativeCrypto = /** @class */ (function () {
46
- function NativeCrypto() {
47
- }
18
+ class NativeCrypto {
48
19
  /**
49
20
  * Here would be better to check cipher type using
50
21
  * ```
@@ -52,7 +23,7 @@ var NativeCrypto = /** @class */ (function () {
52
23
  * ```
53
24
  * but it doesn't work in browser
54
25
  */
55
- NativeCrypto.getKeyLength = function (cipher) {
26
+ static getKeyLength(cipher) {
56
27
  if (/256\-xts/.test(cipher)) {
57
28
  return 64;
58
29
  }
@@ -72,7 +43,7 @@ var NativeCrypto = /** @class */ (function () {
72
43
  return 5;
73
44
  }
74
45
  return 16;
75
- };
46
+ }
76
47
  /**
77
48
  * Here would be better to check cipher type using
78
49
  * ```
@@ -80,7 +51,7 @@ var NativeCrypto = /** @class */ (function () {
80
51
  * ```
81
52
  * but it doesn't work in browser
82
53
  */
83
- NativeCrypto.getIVLength = function (cipher) {
54
+ static getIVLength(cipher) {
84
55
  if (this.isCCM(cipher) || this.isGCM(cipher) || this.isOCB(cipher)) {
85
56
  return 12;
86
57
  }
@@ -91,34 +62,34 @@ var NativeCrypto = /** @class */ (function () {
91
62
  return 8;
92
63
  }
93
64
  return 16;
94
- };
95
- NativeCrypto.createKey = function (cipher) {
96
- var length = this.getKeyLength(cipher);
65
+ }
66
+ static createKey(cipher) {
67
+ const length = this.getKeyLength(cipher);
97
68
  return (0, crypto_1.randomBytes)(length);
98
- };
99
- NativeCrypto.createIV = function (cipher) {
100
- var length = this.getIVLength(cipher);
69
+ }
70
+ static createIV(cipher) {
71
+ const length = this.getIVLength(cipher);
101
72
  return (0, crypto_1.randomBytes)(length);
102
- };
103
- NativeCrypto.createCipher = function (cipher, key, iv) {
73
+ }
74
+ static createCipher(cipher, key, iv) {
104
75
  if (this.isECB(cipher) || this.isRC4(cipher)) {
105
76
  return (0, crypto_1.createCipher)(cipher, key);
106
77
  }
107
78
  if (this.isCCM(cipher) || this.isOCB(cipher)) {
108
- var options = {
79
+ const options = {
109
80
  authTagLength: 16,
110
81
  };
111
82
  return (0, crypto_1.createCipheriv)(cipher, key, iv, options);
112
83
  }
113
84
  return (0, crypto_1.createCipheriv)(cipher, key, iv);
114
- };
115
- NativeCrypto.createDecipher = function (cipher, key, iv, mac) {
85
+ }
86
+ static createDecipher(cipher, key, iv, mac) {
116
87
  if (iv) {
117
- var options = {};
88
+ const options = {};
118
89
  if (this.isCCM(cipher) || this.isOCB(cipher)) {
119
90
  options.authTagLength = 16;
120
91
  }
121
- var decipher = (0, crypto_1.createDecipheriv)(cipher, key, iv, options);
92
+ const decipher = (0, crypto_1.createDecipheriv)(cipher, key, iv, options);
122
93
  if (mac) {
123
94
  decipher.setAuthTag(mac);
124
95
  }
@@ -127,15 +98,13 @@ var NativeCrypto = /** @class */ (function () {
127
98
  else {
128
99
  return (0, crypto_1.createDecipher)(cipher, key);
129
100
  }
130
- };
131
- NativeCrypto.encrypt = function (key, content, cipherName, outputEncoding,
101
+ }
102
+ static encrypt(key, content, cipherName, outputEncoding = dto_js_1.Encoding.base64,
132
103
  // TODO: replace BufferEncoding with Encoding
133
- inputEncoding) {
134
- if (outputEncoding === void 0) { outputEncoding = dto_js_1.Encoding.base64; }
135
- if (inputEncoding === void 0) { inputEncoding = "binary"; }
136
- var iv = this.createIV(cipherName);
137
- var result = {};
138
- var cipher = this.createCipher(cipherName, key, iv);
104
+ inputEncoding = "binary") {
105
+ const iv = this.createIV(cipherName);
106
+ const result = {};
107
+ const cipher = this.createCipher(cipherName, key, iv);
139
108
  result.ciphertext = cipher.update(content, inputEncoding, outputEncoding);
140
109
  result.ciphertext += cipher.final(outputEncoding);
141
110
  if (!this.isECB(cipherName) && !this.isRC4(cipherName)) {
@@ -145,70 +114,47 @@ var NativeCrypto = /** @class */ (function () {
145
114
  result.mac = cipher.getAuthTag().toString(outputEncoding);
146
115
  }
147
116
  return result;
148
- };
149
- NativeCrypto.encryptStream = function (key, inputStream, outputStream, cipherName, encoding) {
150
- if (encoding === void 0) { encoding = dto_js_1.Encoding.base64; }
151
- return __awaiter(this, void 0, void 0, function () {
152
- var iv, result, cipher;
153
- return __generator(this, function (_a) {
154
- switch (_a.label) {
155
- case 0:
156
- iv = this.createIV(cipherName);
157
- result = {};
158
- cipher = this.createCipher(cipherName, key, iv);
159
- inputStream.pipe(cipher).pipe(outputStream);
160
- return [4 /*yield*/, (0, events_1.once)(outputStream, "finish")];
161
- case 1:
162
- _a.sent();
163
- result.iv = iv.toString(encoding);
164
- if (this.isCCM(cipherName) || this.isGCM(cipherName)) {
165
- result.mac = cipher.getAuthTag().toString(encoding);
166
- }
167
- return [2 /*return*/, result];
168
- }
169
- });
117
+ }
118
+ static encryptStream(key, inputStream, outputStream, cipherName, encoding = dto_js_1.Encoding.base64) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ const iv = this.createIV(cipherName);
121
+ const result = {};
122
+ const cipher = this.createCipher(cipherName, key, iv);
123
+ inputStream.pipe(cipher).pipe(outputStream);
124
+ yield (0, events_1.once)(outputStream, "finish");
125
+ result.iv = iv.toString(encoding);
126
+ if (this.isCCM(cipherName) || this.isGCM(cipherName)) {
127
+ result.mac = cipher.getAuthTag().toString(encoding);
128
+ }
129
+ return result;
170
130
  });
171
- };
172
- NativeCrypto.decrypt = function (key, content, cipherName, params, inputEncoding,
131
+ }
132
+ static decrypt(key, content, cipherName, params, inputEncoding = dto_js_1.Encoding.base64,
173
133
  // TODO: replace BufferEncoding with Encoding
174
- outputEncoding) {
175
- if (inputEncoding === void 0) { inputEncoding = dto_js_1.Encoding.base64; }
176
- if (outputEncoding === void 0) { outputEncoding = "binary"; }
177
- var decipher = this.createDecipher(cipherName, key, params === null || params === void 0 ? void 0 : params.iv, params === null || params === void 0 ? void 0 : params.mac);
178
- var decrypted = decipher.update(content, inputEncoding, outputEncoding);
134
+ outputEncoding = "binary") {
135
+ const decipher = this.createDecipher(cipherName, key, params === null || params === void 0 ? void 0 : params.iv, params === null || params === void 0 ? void 0 : params.mac);
136
+ let decrypted = decipher.update(content, inputEncoding, outputEncoding);
179
137
  decrypted += decipher.final(outputEncoding);
180
138
  return decrypted;
181
- };
182
- NativeCrypto.decryptStream = function (key, inputStream, outputStream, cipherName, params) {
183
- return __awaiter(this, void 0, void 0, function () {
184
- var decipher;
185
- return __generator(this, function (_a) {
186
- switch (_a.label) {
187
- case 0:
188
- decipher = this.createDecipher(cipherName, key, params === null || params === void 0 ? void 0 : params.iv, params === null || params === void 0 ? void 0 : params.mac);
189
- inputStream.pipe(decipher).pipe(outputStream);
190
- return [4 /*yield*/, (0, events_1.once)(outputStream, "finish")];
191
- case 1:
192
- _a.sent();
193
- return [2 /*return*/];
194
- }
195
- });
139
+ }
140
+ static decryptStream(key, inputStream, outputStream, cipherName, params) {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ const decipher = this.createDecipher(cipherName, key, params === null || params === void 0 ? void 0 : params.iv, params === null || params === void 0 ? void 0 : params.mac);
143
+ inputStream.pipe(decipher).pipe(outputStream);
144
+ yield (0, events_1.once)(outputStream, "finish");
196
145
  });
197
- };
198
- /**
199
- * Here would be better to check cipher type using
200
- * ```
201
- * getCipherInfo(cipher)!.mode === 'mode'
202
- * ```
203
- * but it doesn't work in browser
204
- */
205
- NativeCrypto.isCCM = function (cipher) { return /ccm/i.test(cipher) || cipher === "chacha20-poly1305"; };
206
- NativeCrypto.isGCM = function (cipher) { return /gcm/i.test(cipher); };
207
- NativeCrypto.isOCB = function (cipher) { return /ocb/i.test(cipher); };
208
- NativeCrypto.isECB = function (cipher) {
209
- return /ecb/i.test(cipher) || cipher === "des-ede" || cipher === "des-ede3";
210
- };
211
- NativeCrypto.isRC4 = function (cipher) { return /^rc4/i.test(cipher); };
212
- return NativeCrypto;
213
- }());
146
+ }
147
+ }
148
+ /**
149
+ * Here would be better to check cipher type using
150
+ * ```
151
+ * getCipherInfo(cipher)!.mode === 'mode'
152
+ * ```
153
+ * but it doesn't work in browser
154
+ */
155
+ NativeCrypto.isCCM = (cipher) => /ccm/i.test(cipher) || cipher === "chacha20-poly1305";
156
+ NativeCrypto.isGCM = (cipher) => /gcm/i.test(cipher);
157
+ NativeCrypto.isOCB = (cipher) => /ocb/i.test(cipher);
158
+ NativeCrypto.isECB = (cipher) => /ecb/i.test(cipher) || cipher === "des-ede" || cipher === "des-ede3";
159
+ NativeCrypto.isRC4 = (cipher) => /^rc4/i.test(cipher);
214
160
  exports.default = NativeCrypto;