@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.
- package/build/TIIGenerator.js +129 -239
- package/build/connectors/BaseConnector.js +17 -47
- package/build/connectors/BlockchainConnector.js +222 -348
- package/build/connectors/BlockchainEventsListener.js +38 -92
- package/build/contracts/app.json +466 -35
- package/build/crypto/Crypto.js +63 -135
- package/build/crypto/index.js +1 -1
- package/build/crypto/nodejs/AES.js +62 -119
- package/build/crypto/nodejs/ARIA.js +61 -118
- package/build/crypto/nodejs/ECIES.js +51 -87
- package/build/crypto/nodejs/NativeCrypto.js +64 -118
- package/build/crypto/nodejs/RSA-Hybrid.js +54 -113
- package/build/index.d.ts +5 -0
- package/build/index.js +14 -2
- package/build/logger.d.ts +3 -1
- package/build/logger.js +2 -2
- package/build/models/EtlModel.js +65 -114
- package/build/models/Offer.js +227 -462
- package/build/models/Order.d.ts +4 -0
- package/build/models/Order.js +326 -559
- package/build/models/Provider.d.ts +2 -0
- package/build/models/Provider.js +74 -153
- package/build/models/TCB.js +67 -171
- package/build/models/TeeOffer.d.ts +1 -0
- package/build/models/TeeOffer.js +389 -731
- package/build/proto/Compression.js +20 -21
- package/build/proto/TRI.js +46 -50
- package/build/proto/TeeProperties.js +60 -66
- package/build/providers/storage/ChunksDownloadDecorator.js +125 -235
- package/build/providers/storage/S3StorageProvider.js +173 -298
- package/build/providers/storage/StorageAdapter.d.ts +60 -0
- package/build/providers/storage/StorageAdapter.js +317 -0
- package/build/providers/storage/StorageContentWriter.d.ts +39 -0
- package/build/providers/storage/StorageContentWriter.js +181 -0
- package/build/providers/storage/StorageKeyValueAdapter.d.ts +20 -0
- package/build/providers/storage/StorageKeyValueAdapter.js +152 -0
- package/build/providers/storage/StorageMetadataReader.d.ts +19 -0
- package/build/providers/storage/StorageMetadataReader.js +65 -0
- package/build/providers/storage/StorjAdapter.d.ts +19 -0
- package/build/providers/storage/StorjAdapter.js +62 -0
- package/build/providers/storage/StorjStorageProvider.js +138 -315
- package/build/providers/storage/getStorageProvider.js +7 -7
- package/build/providers/storage/types.d.ts +7 -0
- package/build/providers/storage/types.js +2 -0
- package/build/staticModels/ActiveOffers.js +32 -100
- package/build/staticModels/ActiveOrders.js +42 -120
- package/build/staticModels/BaseStaticModel.js +7 -10
- package/build/staticModels/Consensus.d.ts +4 -1
- package/build/staticModels/Consensus.js +164 -220
- package/build/staticModels/Deposits.js +109 -201
- package/build/staticModels/Marks.js +21 -82
- package/build/staticModels/ModelPackager.js +34 -84
- package/build/staticModels/Offers.js +140 -266
- package/build/staticModels/Orders.d.ts +1 -0
- package/build/staticModels/Orders.js +286 -429
- package/build/staticModels/ProviderRegistry.js +114 -225
- package/build/staticModels/Superpro.js +17 -63
- package/build/staticModels/SuperproToken.js +90 -169
- package/build/staticModels/TeeOffers.js +220 -416
- package/build/store.js +2 -2
- package/build/types/HardwareInfo.js +2 -2
- package/build/types/Order.js +4 -4
- package/build/types/Provider.js +1 -1
- package/build/types/TeeOfferInfo.js +1 -1
- package/build/types/TeeOfferOption.js +2 -2
- package/build/types/TeeOfferSlot.js +2 -2
- package/build/types/ValueOfferSlot.js +3 -3
- package/build/utils/Monitoring.js +24 -28
- package/build/utils/NonceTracker.js +56 -121
- package/build/utils/PubSub.d.ts +9 -0
- package/build/utils/PubSub.js +36 -0
- package/build/utils/TxManager.js +121 -215
- package/build/utils/compressors/GzipCompressor.js +13 -50
- package/build/utils/compressors/UncompressedCompressor.js +10 -13
- package/build/utils/helpers/tryWithInterval.js +34 -78
- package/build/utils/resourceLoaders/BaseResourceLoader.js +26 -78
- package/build/utils/resourceLoaders/StorageProviderLoader.js +33 -97
- package/build/utils/resourceLoaders/UrlResourceLoader.js +29 -93
- package/build/utils/resourceLoaders/getResourceLoader.js +6 -6
- package/build/utils.js +60 -105
- 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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
76
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
100
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
127
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
mac
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
77
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
96
|
-
|
|
65
|
+
}
|
|
66
|
+
static createKey(cipher) {
|
|
67
|
+
const length = this.getKeyLength(cipher);
|
|
97
68
|
return (0, crypto_1.randomBytes)(length);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
69
|
+
}
|
|
70
|
+
static createIV(cipher) {
|
|
71
|
+
const length = this.getIVLength(cipher);
|
|
101
72
|
return (0, crypto_1.randomBytes)(length);
|
|
102
|
-
}
|
|
103
|
-
|
|
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
|
-
|
|
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
|
-
|
|
85
|
+
}
|
|
86
|
+
static createDecipher(cipher, key, iv, mac) {
|
|
116
87
|
if (iv) {
|
|
117
|
-
|
|
88
|
+
const options = {};
|
|
118
89
|
if (this.isCCM(cipher) || this.isOCB(cipher)) {
|
|
119
90
|
options.authTagLength = 16;
|
|
120
91
|
}
|
|
121
|
-
|
|
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
|
-
|
|
101
|
+
}
|
|
102
|
+
static encrypt(key, content, cipherName, outputEncoding = dto_js_1.Encoding.base64,
|
|
132
103
|
// TODO: replace BufferEncoding with Encoding
|
|
133
|
-
inputEncoding) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
131
|
+
}
|
|
132
|
+
static decrypt(key, content, cipherName, params, inputEncoding = dto_js_1.Encoding.base64,
|
|
173
133
|
// TODO: replace BufferEncoding with Encoding
|
|
174
|
-
outputEncoding) {
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
183
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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;
|