@super-protocol/sdk-js 0.12.7-beta.0 → 0.12.7-beta.2

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.
@@ -40,52 +40,62 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  var dto_js_1 = require("@super-protocol/dto-js");
43
- var eccrypto_1 = __importDefault(require("eccrypto"));
43
+ var crypto_1 = __importDefault(require("crypto"));
44
44
  var ECIES = /** @class */ (function () {
45
45
  function ECIES() {
46
46
  }
47
47
  ECIES.encrypt = function (content, encryption) {
48
48
  return __awaiter(this, void 0, void 0, function () {
49
- var result;
49
+ var ecdh, epk, pk, hash, cipherKey, macKey, iv, cipher, ct, dataToMac, mac;
50
50
  return __generator(this, function (_a) {
51
- switch (_a.label) {
52
- case 0:
53
- if (!encryption.key)
54
- throw Error("Encryption key is not provided");
55
- return [4 /*yield*/, eccrypto_1.default.encrypt(Buffer.from(encryption.key, encryption.encoding), Buffer.from(content, "binary"))];
56
- case 1:
57
- result = _a.sent();
58
- return [2 /*return*/, {
59
- iv: result.iv.toString(encryption.encoding),
60
- ephemPublicKey: result.ephemPublicKey.toString(encryption.encoding),
61
- mac: result.mac.toString(encryption.encoding),
62
- encoding: encryption.encoding,
63
- algo: dto_js_1.CryptoAlgorithm.ECIES,
64
- ciphertext: result.ciphertext.toString(encryption.encoding),
65
- }];
66
- }
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
+ }];
67
73
  });
68
74
  });
69
75
  };
70
76
  ECIES.decrypt = function (encryption) {
71
77
  return __awaiter(this, void 0, void 0, function () {
72
- var encryptedObject, result;
78
+ var iv, epk, ct, mac, ecdh, pk, hash, cipherKey, macKey, m, decipher, pt, result;
73
79
  return __generator(this, function (_a) {
74
- switch (_a.label) {
75
- case 0:
76
- if (!encryption.key)
77
- throw Error("Decryption key is not provided");
78
- encryptedObject = {
79
- iv: Buffer.from(encryption.iv, encryption.encoding),
80
- ephemPublicKey: Buffer.from(encryption.ephemPublicKey, encryption.encoding),
81
- ciphertext: Buffer.from(encryption.ciphertext, encryption.encoding),
82
- mac: Buffer.from(encryption.mac, encryption.encoding),
83
- };
84
- return [4 /*yield*/, eccrypto_1.default.decrypt(Buffer.from(encryption.key, encryption.encoding), encryptedObject)];
85
- case 1:
86
- result = _a.sent();
87
- return [2 /*return*/, result.toString("binary")];
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");
88
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")];
89
99
  });
90
100
  });
91
101
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@super-protocol/sdk-js",
3
- "version": "0.12.7-beta.0",
3
+ "version": "0.12.7-beta.2",
4
4
  "main": "build/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -31,7 +31,6 @@
31
31
  "dto:generate": "cd ./dto/src && protoc --plugin=../../node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=esModuleInterop=true --ts_proto_opt=unrecognizedEnum=false --ts_proto_out=../../src/proto ./*"
32
32
  },
33
33
  "devDependencies": {
34
- "@types/eccrypto": "^1.1.3",
35
34
  "@types/jest": "^27.0.3",
36
35
  "@types/lodash": "^4.14.176",
37
36
  "@types/node": "^17.0.0",
@@ -56,7 +55,6 @@
56
55
  "@super-protocol/dto-js": "0.0.24",
57
56
  "@super-protocol/tee-lib": "^0.1.2",
58
57
  "@super-protocol/uplink-nodejs": "1.2.12",
59
- "eccrypto": "^1.1.6",
60
58
  "ethers": "^5.5.3",
61
59
  "hybrid-crypto-js": "^0.2.4",
62
60
  "lodash": "^4.17.21",