jcc_wallet 4.0.1 → 4.0.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.
Files changed (78) hide show
  1. package/dist/jcc-wallet.min.js +21 -26
  2. package/lib/constant.js +0 -1
  3. package/lib/eccrypto.d.ts +12 -0
  4. package/lib/eccrypto.js +116 -0
  5. package/lib/hd/constant.js +0 -1
  6. package/lib/hd/eos.plugin.d.ts +1 -0
  7. package/lib/hd/eos.plugin.js +36 -25
  8. package/lib/hd/ethereum.plugin.d.ts +1 -1
  9. package/lib/hd/ethereum.plugin.js +21 -26
  10. package/lib/hd/index.js +0 -1
  11. package/lib/hd/plugins.js +0 -1
  12. package/lib/hd/swtc.plugin.js +0 -1
  13. package/lib/hd/tron.plugin.js +0 -1
  14. package/lib/index.js +0 -1
  15. package/lib/jingchangWallet.d.ts +1 -1
  16. package/lib/jingchangWallet.js +70 -54
  17. package/lib/minify-eosjs/PrivateKey.d.ts +27 -0
  18. package/lib/minify-eosjs/PrivateKey.js +77 -0
  19. package/lib/minify-eosjs/PublicKey.d.ts +25 -0
  20. package/lib/minify-eosjs/PublicKey.js +61 -0
  21. package/lib/minify-eosjs/Signature.d.ts +34 -0
  22. package/lib/minify-eosjs/Signature.js +100 -0
  23. package/lib/minify-eosjs/bn-utils.d.ts +2 -0
  24. package/lib/minify-eosjs/bn-utils.js +17 -0
  25. package/lib/minify-eosjs/eosjs-key-conversions.d.ts +12 -0
  26. package/lib/minify-eosjs/eosjs-key-conversions.js +25 -0
  27. package/lib/minify-eosjs/eosjs-numeric.d.ts +48 -0
  28. package/lib/minify-eosjs/eosjs-numeric.js +303 -0
  29. package/lib/minify-ethereumjs-util/account.js +0 -1
  30. package/lib/minify-ethereumjs-util/bytes.js +0 -1
  31. package/lib/minify-ethereumjs-util/constants.js +0 -1
  32. package/lib/minify-ethereumjs-util/helper.js +0 -1
  33. package/lib/minify-ethereumjs-util/internal.js +0 -1
  34. package/lib/minify-ethereumjs-util/signature.js +0 -1
  35. package/lib/minify-swtc-keypair/address-codec.js +0 -1
  36. package/lib/minify-swtc-keypair/index.js +7 -7
  37. package/lib/minify-swtc-keypair/sha512.js +0 -1
  38. package/lib/minify-swtc-keypair/utils.js +0 -1
  39. package/lib/minify-tron/address.js +0 -1
  40. package/lib/minify-tron/base58.js +0 -1
  41. package/lib/minify-tron/bytes.js +0 -1
  42. package/lib/minify-tron/code.d.ts +1 -1
  43. package/lib/minify-tron/code.js +0 -1
  44. package/lib/minify-tron/crypto.js +3 -4
  45. package/lib/minify-tron/message.d.ts +2 -2
  46. package/lib/minify-tron/message.js +8 -9
  47. package/lib/types.js +0 -1
  48. package/lib/util/index.d.ts +5 -4
  49. package/lib/util/index.js +28 -21
  50. package/package.json +23 -41
  51. package/lib/constant.js.map +0 -1
  52. package/lib/hd/constant.js.map +0 -1
  53. package/lib/hd/eos.plugin.js.map +0 -1
  54. package/lib/hd/ethereum.plugin.js.map +0 -1
  55. package/lib/hd/index.js.map +0 -1
  56. package/lib/hd/plugins.js.map +0 -1
  57. package/lib/hd/swtc.plugin.js.map +0 -1
  58. package/lib/hd/tron.plugin.js.map +0 -1
  59. package/lib/index.js.map +0 -1
  60. package/lib/jingchangWallet.js.map +0 -1
  61. package/lib/minify-ethereumjs-util/account.js.map +0 -1
  62. package/lib/minify-ethereumjs-util/bytes.js.map +0 -1
  63. package/lib/minify-ethereumjs-util/constants.js.map +0 -1
  64. package/lib/minify-ethereumjs-util/helper.js.map +0 -1
  65. package/lib/minify-ethereumjs-util/internal.js.map +0 -1
  66. package/lib/minify-ethereumjs-util/signature.js.map +0 -1
  67. package/lib/minify-swtc-keypair/address-codec.js.map +0 -1
  68. package/lib/minify-swtc-keypair/index.js.map +0 -1
  69. package/lib/minify-swtc-keypair/sha512.js.map +0 -1
  70. package/lib/minify-swtc-keypair/utils.js.map +0 -1
  71. package/lib/minify-tron/address.js.map +0 -1
  72. package/lib/minify-tron/base58.js.map +0 -1
  73. package/lib/minify-tron/bytes.js.map +0 -1
  74. package/lib/minify-tron/code.js.map +0 -1
  75. package/lib/minify-tron/crypto.js.map +0 -1
  76. package/lib/minify-tron/message.js.map +0 -1
  77. package/lib/types.js.map +0 -1
  78. package/lib/util/index.js.map +0 -1
@@ -0,0 +1,303 @@
1
+ "use strict";
2
+ /**
3
+ * forked from https://github.com/EOSIO/eosjs/blob/master/src/eosjs-key-conversions.ts
4
+ *
5
+ * rewrite ripemd160
6
+ *
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.privateKeyToString = exports.privateKeyToLegacyString = exports.signatureToString = exports.stringToSignature = exports.stringToPrivateKey = exports.publicKeyToLegacyString = exports.stringToPublicKey = exports.signatureDataSize = exports.privateKeyDataSize = exports.publicKeyDataSize = exports.KeyType = exports.binaryToBase58 = exports.base58ToBinary = void 0;
10
+ const ripemd160_1 = require("@noble/hashes/ripemd160");
11
+ const sha256_1 = require("@noble/hashes/sha256");
12
+ const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
13
+ const create_base58_map = () => {
14
+ const base58M = Array(256).fill(-1);
15
+ for (let i = 0; i < base58Chars.length; ++i) {
16
+ base58M[base58Chars.charCodeAt(i)] = i;
17
+ }
18
+ return base58M;
19
+ };
20
+ const base58Map = create_base58_map();
21
+ const base58ToBinaryVarSize = (s) => {
22
+ const result = [];
23
+ for (let i = 0; i < s.length; ++i) {
24
+ let carry = base58Map[s.charCodeAt(i)];
25
+ if (carry < 0) {
26
+ throw new Error("invalid base-58 value");
27
+ }
28
+ for (let j = 0; j < result.length; ++j) {
29
+ const x = result[j] * 58 + carry;
30
+ result[j] = x & 0xff;
31
+ carry = x >> 8;
32
+ }
33
+ if (carry) {
34
+ result.push(carry);
35
+ }
36
+ }
37
+ for (const ch of s) {
38
+ if (ch === "1") {
39
+ result.push(0);
40
+ }
41
+ else {
42
+ break;
43
+ }
44
+ }
45
+ result.reverse();
46
+ return new Uint8Array(result);
47
+ };
48
+ /**
49
+ * Convert an unsigned base-58 number in `s` to a bignum
50
+ *
51
+ * @param size bignum size (bytes)
52
+ */
53
+ const base58ToBinary = (size, s) => {
54
+ if (!size) {
55
+ return base58ToBinaryVarSize(s);
56
+ }
57
+ const result = new Uint8Array(size);
58
+ for (let i = 0; i < s.length; ++i) {
59
+ let carry = base58Map[s.charCodeAt(i)];
60
+ if (carry < 0) {
61
+ throw new Error("invalid base-58 value");
62
+ }
63
+ for (let j = 0; j < size; ++j) {
64
+ const x = result[j] * 58 + carry;
65
+ result[j] = x;
66
+ carry = x >> 8;
67
+ }
68
+ if (carry) {
69
+ throw new Error("base-58 value is out of range");
70
+ }
71
+ }
72
+ result.reverse();
73
+ return result;
74
+ };
75
+ exports.base58ToBinary = base58ToBinary;
76
+ /**
77
+ * Convert `bignum` to a base-58 number
78
+ *
79
+ * @param minDigits 0-pad result to this many digits
80
+ */
81
+ const binaryToBase58 = (bignum) => {
82
+ const result = [];
83
+ for (const byte of bignum) {
84
+ let carry = byte;
85
+ for (let j = 0; j < result.length; ++j) {
86
+ const x = (base58Map[result[j]] << 8) + carry;
87
+ result[j] = base58Chars.charCodeAt(x % 58);
88
+ carry = (x / 58) | 0;
89
+ }
90
+ while (carry) {
91
+ result.push(base58Chars.charCodeAt(carry % 58));
92
+ carry = (carry / 58) | 0;
93
+ }
94
+ }
95
+ for (const byte of bignum) {
96
+ if (byte) {
97
+ break;
98
+ }
99
+ else {
100
+ result.push("1".charCodeAt(0));
101
+ }
102
+ }
103
+ result.reverse();
104
+ return String.fromCharCode(...result);
105
+ };
106
+ exports.binaryToBase58 = binaryToBase58;
107
+ /** Key types this library supports */
108
+ var KeyType;
109
+ (function (KeyType) {
110
+ KeyType[KeyType["k1"] = 0] = "k1";
111
+ KeyType[KeyType["r1"] = 1] = "r1";
112
+ KeyType[KeyType["wa"] = 2] = "wa";
113
+ })(KeyType || (exports.KeyType = KeyType = {}));
114
+ /** Public key data size, excluding type field */
115
+ exports.publicKeyDataSize = 33;
116
+ /** Private key data size, excluding type field */
117
+ exports.privateKeyDataSize = 32;
118
+ /** Signature data size, excluding type field */
119
+ exports.signatureDataSize = 65;
120
+ const digestSuffixRipemd160 = (data, suffix) => {
121
+ const d = new Uint8Array(data.length + suffix.length);
122
+ for (let i = 0; i < data.length; ++i) {
123
+ d[i] = data[i];
124
+ }
125
+ for (let i = 0; i < suffix.length; ++i) {
126
+ d[data.length + i] = suffix.charCodeAt(i);
127
+ }
128
+ return (0, ripemd160_1.ripemd160)(d);
129
+ };
130
+ const stringToKey = (s, type, size, suffix) => {
131
+ const whole = (0, exports.base58ToBinary)(size ? size + 4 : 0, s);
132
+ const result = { type, data: new Uint8Array(whole.buffer, 0, whole.length - 4) };
133
+ const digest = new Uint8Array(digestSuffixRipemd160(result.data, suffix));
134
+ if (digest[0] !== whole[whole.length - 4] ||
135
+ digest[1] !== whole[whole.length - 3] ||
136
+ digest[2] !== whole[whole.length - 2] ||
137
+ digest[3] !== whole[whole.length - 1]) {
138
+ throw new Error("checksum doesn't match");
139
+ }
140
+ return result;
141
+ };
142
+ const keyToString = (key, suffix, prefix) => {
143
+ const digest = new Uint8Array(digestSuffixRipemd160(key.data, suffix));
144
+ const whole = new Uint8Array(key.data.length + 4);
145
+ for (let i = 0; i < key.data.length; ++i) {
146
+ whole[i] = key.data[i];
147
+ }
148
+ for (let i = 0; i < 4; ++i) {
149
+ whole[i + key.data.length] = digest[i];
150
+ }
151
+ return prefix + (0, exports.binaryToBase58)(whole);
152
+ };
153
+ /** Convert key in `s` to binary form */
154
+ const stringToPublicKey = (s) => {
155
+ if (typeof s !== "string") {
156
+ throw new Error("expected string containing public key");
157
+ }
158
+ if (s.substring(0, 3) === "EOS") {
159
+ const whole = (0, exports.base58ToBinary)(exports.publicKeyDataSize + 4, s.substring(3));
160
+ const key = { type: KeyType.k1, data: new Uint8Array(exports.publicKeyDataSize) };
161
+ for (let i = 0; i < exports.publicKeyDataSize; ++i) {
162
+ key.data[i] = whole[i];
163
+ }
164
+ const digest = new Uint8Array((0, ripemd160_1.ripemd160)(key.data));
165
+ if (digest[0] !== whole[exports.publicKeyDataSize] ||
166
+ digest[1] !== whole[34] ||
167
+ digest[2] !== whole[35] ||
168
+ digest[3] !== whole[36]) {
169
+ throw new Error("checksum doesn't match");
170
+ }
171
+ return key;
172
+ }
173
+ else if (s.substring(0, 7) === "PUB_K1_") {
174
+ return stringToKey(s.substring(7), KeyType.k1, exports.publicKeyDataSize, "K1");
175
+ }
176
+ else if (s.substring(0, 7) === "PUB_R1_") {
177
+ return stringToKey(s.substring(7), KeyType.r1, exports.publicKeyDataSize, "R1");
178
+ }
179
+ else if (s.substring(0, 7) === "PUB_WA_") {
180
+ return stringToKey(s.substring(7), KeyType.wa, 0, "WA");
181
+ }
182
+ else {
183
+ throw new Error("unrecognized public key format");
184
+ }
185
+ };
186
+ exports.stringToPublicKey = stringToPublicKey;
187
+ /** Convert public `key` to legacy string (base-58) form */
188
+ const publicKeyToLegacyString = (key) => {
189
+ if (key.type === KeyType.k1 && key.data.length === exports.publicKeyDataSize) {
190
+ return keyToString(key, "", "EOS");
191
+ }
192
+ else if (key.type === KeyType.r1 || key.type === KeyType.wa) {
193
+ throw new Error("Key format not supported in legacy conversion");
194
+ }
195
+ else {
196
+ throw new Error("unrecognized public key format");
197
+ }
198
+ };
199
+ exports.publicKeyToLegacyString = publicKeyToLegacyString;
200
+ /** Convert key in `s` to binary form */
201
+ const stringToPrivateKey = (s) => {
202
+ if (typeof s !== "string") {
203
+ throw new Error("expected string containing private key");
204
+ }
205
+ if (s.substring(0, 7) === "PVT_R1_") {
206
+ return stringToKey(s.substring(7), KeyType.r1, exports.privateKeyDataSize, "R1");
207
+ }
208
+ else if (s.substring(0, 7) === "PVT_K1_") {
209
+ return stringToKey(s.substring(7), KeyType.k1, exports.privateKeyDataSize, "K1");
210
+ }
211
+ else {
212
+ // todo: Verify checksum: sha256(sha256(key.data)).
213
+ // Not critical since a bad key will fail to produce a
214
+ // valid signature anyway.
215
+ const whole = (0, exports.base58ToBinary)(exports.privateKeyDataSize + 5, s);
216
+ const key = { type: KeyType.k1, data: new Uint8Array(exports.privateKeyDataSize) };
217
+ if (whole[0] !== 0x80) {
218
+ throw new Error("unrecognized private key type");
219
+ }
220
+ for (let i = 0; i < exports.privateKeyDataSize; ++i) {
221
+ key.data[i] = whole[i + 1];
222
+ }
223
+ return key;
224
+ }
225
+ };
226
+ exports.stringToPrivateKey = stringToPrivateKey;
227
+ /** Convert key in `s` to binary form */
228
+ const stringToSignature = (s) => {
229
+ if (typeof s !== "string") {
230
+ throw new Error("expected string containing signature");
231
+ }
232
+ if (s.substring(0, 7) === "SIG_K1_") {
233
+ return stringToKey(s.substring(7), KeyType.k1, exports.signatureDataSize, "K1");
234
+ }
235
+ else if (s.substring(0, 7) === "SIG_R1_") {
236
+ return stringToKey(s.substring(7), KeyType.r1, exports.signatureDataSize, "R1");
237
+ }
238
+ else if (s.substring(0, 7) === "SIG_WA_") {
239
+ return stringToKey(s.substring(7), KeyType.wa, 0, "WA");
240
+ }
241
+ else {
242
+ throw new Error("unrecognized signature format");
243
+ }
244
+ };
245
+ exports.stringToSignature = stringToSignature;
246
+ /** Convert `signature` to string (base-58) form */
247
+ const signatureToString = (signature) => {
248
+ if (signature.type === KeyType.k1) {
249
+ return keyToString(signature, "K1", "SIG_K1_");
250
+ }
251
+ else if (signature.type === KeyType.r1) {
252
+ return keyToString(signature, "R1", "SIG_R1_");
253
+ }
254
+ else if (signature.type === KeyType.wa) {
255
+ return keyToString(signature, "WA", "SIG_WA_");
256
+ }
257
+ else {
258
+ throw new Error("unrecognized signature format");
259
+ }
260
+ };
261
+ exports.signatureToString = signatureToString;
262
+ const privateKeyToLegacyString = (key) => {
263
+ if (key.type === KeyType.k1 && key.data.length === exports.privateKeyDataSize) {
264
+ const whole = [];
265
+ whole.push(128);
266
+ key.data.forEach((byte) => whole.push(byte));
267
+ const digest = new Uint8Array(sha256_1.sha256
268
+ .create()
269
+ .update(sha256_1.sha256
270
+ .create()
271
+ .update(Buffer.from(whole))
272
+ .digest())
273
+ .digest());
274
+ const result = new Uint8Array(exports.privateKeyDataSize + 5);
275
+ for (let i = 0; i < whole.length; i++) {
276
+ result[i] = whole[i];
277
+ }
278
+ for (let i = 0; i < 4; i++) {
279
+ result[i + whole.length] = digest[i];
280
+ }
281
+ return (0, exports.binaryToBase58)(result);
282
+ }
283
+ else if (key.type === KeyType.r1 || key.type === KeyType.wa) {
284
+ throw new Error("Key format not supported in legacy conversion");
285
+ }
286
+ else {
287
+ throw new Error("unrecognized public key format");
288
+ }
289
+ };
290
+ exports.privateKeyToLegacyString = privateKeyToLegacyString;
291
+ /** Convert `key` to string (base-58) form */
292
+ const privateKeyToString = (key) => {
293
+ if (key.type === KeyType.r1) {
294
+ return keyToString(key, "R1", "PVT_R1_");
295
+ }
296
+ else if (key.type === KeyType.k1) {
297
+ return keyToString(key, "K1", "PVT_K1_");
298
+ }
299
+ else {
300
+ throw new Error("unrecognized private key format");
301
+ }
302
+ };
303
+ exports.privateKeyToString = privateKeyToString;
@@ -19,4 +19,3 @@ const pubToAddress = function (pubKey, sanitize = false) {
19
19
  return (0, keccak_js_1.keccak256)(pubKey).subarray(-20);
20
20
  };
21
21
  exports.pubToAddress = pubToAddress;
22
- //# sourceMappingURL=account.js.map
@@ -97,4 +97,3 @@ const bytesToBigInt = (bytes) => {
97
97
  return BigInt(hex);
98
98
  };
99
99
  exports.bytesToBigInt = bytesToBigInt;
100
- //# sourceMappingURL=bytes.js.map
@@ -6,4 +6,3 @@ exports.BIGINT_0 = BigInt(0);
6
6
  exports.BIGINT_1 = BigInt(1);
7
7
  exports.BIGINT_2 = BigInt(2);
8
8
  exports.BIGINT_27 = BigInt(27);
9
- //# sourceMappingURL=constants.js.map
@@ -13,4 +13,3 @@ const assertIsBytes = function (input) {
13
13
  }
14
14
  };
15
15
  exports.assertIsBytes = assertIsBytes;
16
- //# sourceMappingURL=helper.js.map
@@ -30,4 +30,3 @@ const stripHexPrefix = (str) => {
30
30
  return isHexPrefixed(str) ? str.slice(2) : str;
31
31
  };
32
32
  exports.stripHexPrefix = stripHexPrefix;
33
- //# sourceMappingURL=internal.js.map
@@ -42,4 +42,3 @@ const ecrecover = function (msgHash, v, r, s, chainId) {
42
42
  return senderPubKey.toRawBytes(false).slice(1);
43
43
  };
44
44
  exports.ecrecover = ecrecover;
45
- //# sourceMappingURL=signature.js.map
@@ -163,4 +163,3 @@ const Factory = (alphabet) => {
163
163
  };
164
164
  };
165
165
  exports.Factory = Factory;
166
- //# sourceMappingURL=address-codec.js.map
@@ -1,7 +1,4 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.Factory = void 0;
7
4
  const address_codec_1 = require("./address-codec");
@@ -12,8 +9,7 @@ const sha256_1 = require("@noble/hashes/sha256");
12
9
  const ripemd160_1 = require("@noble/hashes/ripemd160");
13
10
  const ed25519_1 = require("@noble/curves/ed25519");
14
11
  const secp256k1_1 = require("@noble/curves/secp256k1");
15
- const brorand_1 = __importDefault(require("brorand"));
16
- const assert = require("assert");
12
+ const utils_3 = require("@noble/hashes/utils");
17
13
  const hash = (message) => {
18
14
  return sha512_1.sha512
19
15
  .create()
@@ -28,6 +24,11 @@ const funcHexToBytes = (hex) => {
28
24
  }
29
25
  return bytes;
30
26
  };
27
+ function assert(condition, message) {
28
+ if (!condition) {
29
+ throw new Error(message || "Assertion failed");
30
+ }
31
+ }
31
32
  const SECP256K1_PREFIX = "00";
32
33
  const ED25519_PREFIX = "ED";
33
34
  const computePublicKeyHash = (publicKeyBytes) => {
@@ -140,7 +141,7 @@ const Factory = (alphabet) => {
140
141
  };
141
142
  const generate = (options) => {
142
143
  assert(!options.entropy || options.entropy.length >= 16, "entropy too short");
143
- const entropy = options.entropy ? options.entropy.slice(0, 16) : (0, brorand_1.default)(16);
144
+ const entropy = options.entropy ? options.entropy.slice(0, 16) : (0, utils_3.randomBytes)(16);
144
145
  const type = options.algorithm === "ed25519" ? "ed25519" : "secp256k1";
145
146
  const secret = addressCodec.encodeSeed(entropy, type);
146
147
  const keypair = deriveKeyPair(secret);
@@ -175,4 +176,3 @@ const Factory = (alphabet) => {
175
176
  };
176
177
  };
177
178
  exports.Factory = Factory;
178
- //# sourceMappingURL=index.js.map
@@ -30,4 +30,3 @@ class Sha512 {
30
30
  }
31
31
  }
32
32
  exports.default = Sha512;
33
- //# sourceMappingURL=sha512.js.map
@@ -60,4 +60,3 @@ function derivePrivateKey(seed, opts = {}) {
60
60
  const accountIndex = opts.accountIndex || 0;
61
61
  return (deriveScalar(publicGen, accountIndex) + privateGen) % order;
62
62
  }
63
- //# sourceMappingURL=utils.js.map
@@ -8,4 +8,3 @@ exports.ADDRESS_PREFIX_BYTE = 0x41;
8
8
  exports.ADDRESS_PREFIX_REGEX = /^(41)/;
9
9
  exports.TRON_BIP39_PATH_PREFIX = "m/44'/195'";
10
10
  exports.TRON_BIP39_PATH_INDEX_0 = exports.TRON_BIP39_PATH_PREFIX + "/0'/0/0";
11
- //# sourceMappingURL=address.js.map
@@ -64,4 +64,3 @@ function decode58(string) {
64
64
  bytes.push(0);
65
65
  return bytes.reverse();
66
66
  }
67
- //# sourceMappingURL=base58.js.map
@@ -20,4 +20,3 @@ function byte2hexStr(byte) {
20
20
  str += hexByteMap.charAt(byte & 0x0f);
21
21
  return str;
22
22
  }
23
- //# sourceMappingURL=bytes.js.map
@@ -1,3 +1,3 @@
1
- export declare function isHexChar(c: any): 1 | 0;
1
+ export declare function isHexChar(c: any): 0 | 1;
2
2
  export declare function hexChar2byte(c: any): number;
3
3
  export declare function hexStr2byteArray(str: any, strict?: boolean): any[];
@@ -53,4 +53,3 @@ function hexStr2byteArray(str, strict = false) {
53
53
  }
54
54
  return byteArray;
55
55
  }
56
- //# sourceMappingURL=code.js.map
@@ -13,7 +13,7 @@ const code_1 = require("./code");
13
13
  const base58_1 = require("./base58");
14
14
  const bytes_1 = require("./bytes");
15
15
  const secp256k1_1 = require("@noble/curves/secp256k1");
16
- const { keccak256, sha256 } = require("ethers/crypto");
16
+ const crypto_1 = require("ethers/crypto");
17
17
  function normalizePrivateKeyBytes(priKeyBytes) {
18
18
  return (0, code_1.hexStr2byteArray)((0, bytes_1.byteArray2hexStr)(priKeyBytes).padStart(64, "0"));
19
19
  }
@@ -50,7 +50,7 @@ function isAddressValid(base58Str) {
50
50
  function computeAddress(pubBytes) {
51
51
  if (pubBytes.length === 65)
52
52
  pubBytes = pubBytes.slice(1);
53
- const hash = keccak256(new Uint8Array(pubBytes))
53
+ const hash = (0, crypto_1.keccak256)(new Uint8Array(pubBytes))
54
54
  .toString()
55
55
  .substring(2);
56
56
  const addressHex = address_1.ADDRESS_PREFIX + hash.substring(24);
@@ -72,7 +72,7 @@ function getPubKeyFromPriKey(priKeyBytes) {
72
72
  }
73
73
  function SHA256(msgBytes) {
74
74
  const msgHex = (0, bytes_1.byteArray2hexStr)(msgBytes);
75
- const hashHex = sha256("0x" + msgHex).replace(/^0x/, "");
75
+ const hashHex = (0, crypto_1.sha256)("0x" + msgHex).replace(/^0x/, "");
76
76
  return (0, code_1.hexStr2byteArray)(hashHex);
77
77
  }
78
78
  function pkToAddress(privateKey, strict = false) {
@@ -80,4 +80,3 @@ function pkToAddress(privateKey, strict = false) {
80
80
  const com_addressBytes = getAddressFromPriKey(com_priKeyBytes);
81
81
  return getBase58CheckAddress(com_addressBytes);
82
82
  }
83
- //# sourceMappingURL=crypto.js.map
@@ -1,4 +1,4 @@
1
1
  export declare const TRON_MESSAGE_PREFIX = "\u0019TRON Signed Message:\n";
2
- export declare function hashMessage(message: any): any;
3
- export declare function signMessage(message: any, privateKey: any): any;
2
+ export declare function hashMessage(message: any): string;
3
+ export declare function signMessage(message: any, privateKey: any): string;
4
4
  export declare function verifyMessage(message: any, signature: any): string;
@@ -8,25 +8,25 @@ exports.verifyMessage = verifyMessage;
8
8
  const address_1 = require("./address");
9
9
  const crypto_1 = require("./crypto");
10
10
  const code_1 = require("./code");
11
- const { keccak256, SigningKey, Signature } = require("ethers/crypto");
12
- const { recoverAddress } = require("ethers/transaction");
13
- const { toUtf8Bytes, concat } = require("ethers/utils");
14
- const joinSignature = (splitSig) => Signature.from(splitSig).serialized;
11
+ const crypto_2 = require("ethers/crypto");
12
+ const transaction_1 = require("ethers/transaction");
13
+ const utils_1 = require("ethers/utils");
14
+ const joinSignature = (splitSig) => crypto_2.Signature.from(splitSig).serialized;
15
15
  exports.TRON_MESSAGE_PREFIX = "\x19TRON Signed Message:\n";
16
16
  function hashMessage(message) {
17
17
  if (typeof message === "string") {
18
- message = toUtf8Bytes(message);
18
+ message = (0, utils_1.toUtf8Bytes)(message);
19
19
  }
20
20
  if (Array.isArray(message)) {
21
21
  message = new Uint8Array(message);
22
22
  }
23
- return keccak256(concat([toUtf8Bytes(exports.TRON_MESSAGE_PREFIX), toUtf8Bytes(String(message.length)), message]));
23
+ return (0, crypto_2.keccak256)((0, utils_1.concat)([(0, utils_1.toUtf8Bytes)(exports.TRON_MESSAGE_PREFIX), (0, utils_1.toUtf8Bytes)(String(message.length)), message]));
24
24
  }
25
25
  function signMessage(message, privateKey) {
26
26
  if (!privateKey.match(/^0x/)) {
27
27
  privateKey = "0x" + privateKey;
28
28
  }
29
- const signingKey = new SigningKey(privateKey);
29
+ const signingKey = new crypto_2.SigningKey(privateKey);
30
30
  const messageDigest = hashMessage(message);
31
31
  const signature = signingKey.sign(messageDigest);
32
32
  return joinSignature(signature);
@@ -35,8 +35,7 @@ function verifyMessage(message, signature) {
35
35
  if (!signature.match(/^0x/)) {
36
36
  signature = "0x" + signature;
37
37
  }
38
- const recovered = recoverAddress(hashMessage(message), signature);
38
+ const recovered = (0, transaction_1.recoverAddress)(hashMessage(message), signature);
39
39
  const base58Address = (0, crypto_1.getBase58CheckAddress)((0, code_1.hexStr2byteArray)(recovered.replace(/^0x/, address_1.ADDRESS_PREFIX)));
40
40
  return base58Address;
41
41
  }
42
- //# sourceMappingURL=message.js.map
package/lib/types.js CHANGED
@@ -13,4 +13,3 @@ var Alphabet;
13
13
  Alphabet["XRP"] = "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz";
14
14
  Alphabet["SWTC"] = "jpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65rkm8oFqi1tuvAxyz";
15
15
  })(Alphabet || (exports.Alphabet = Alphabet = {}));
16
- //# sourceMappingURL=types.js.map
@@ -1,4 +1,5 @@
1
1
  import { IEncryptModel, IKeystoreModel, IKeypairsModel } from "../types";
2
+ export declare const isEmptyPlainObject: (obj: any) => boolean;
2
3
  /**
3
4
  * decrypt wallet with password
4
5
  *
@@ -7,7 +8,7 @@ import { IEncryptModel, IKeystoreModel, IKeypairsModel } from "../types";
7
8
  * @returns {(string)} return secret if success, otherwise throws `keystore is invalid` if the keystore is invalid or
8
9
  * throws `password is wrong` if the password is wrong
9
10
  */
10
- declare const decrypt: (password: string, encryptData: IKeystoreModel) => string;
11
+ declare const decrypt: (password: string, encryptData: IKeystoreModel) => Promise<Buffer>;
11
12
  /**
12
13
  * encrypt data with password
13
14
  *
@@ -16,7 +17,7 @@ declare const decrypt: (password: string, encryptData: IKeystoreModel) => string
16
17
  * @param {IEncryptModel} [opts={}]
17
18
  * @returns {IKeystoreModel}
18
19
  */
19
- declare const encrypt: (password: string, data: string, opts: IEncryptModel) => IKeystoreModel;
20
- declare const encryptContact: (password: string, contacts: any, opts?: IEncryptModel) => IKeystoreModel;
21
- declare const encryptWallet: (password: string, keypairs: IKeypairsModel, opts?: IEncryptModel) => IKeystoreModel;
20
+ declare const encrypt: (password: string, data: string, opts: IEncryptModel) => Promise<IKeystoreModel>;
21
+ declare const encryptContact: (password: string, contacts: any, opts?: IEncryptModel) => Promise<IKeystoreModel>;
22
+ declare const encryptWallet: (password: string, keypairs: IKeypairsModel, opts?: IEncryptModel) => Promise<IKeystoreModel>;
22
23
  export { decrypt, encrypt, encryptContact, encryptWallet };
package/lib/util/index.js CHANGED
@@ -3,13 +3,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.encryptWallet = exports.encryptContact = exports.encrypt = exports.decrypt = void 0;
7
- const crypto_1 = __importDefault(require("crypto"));
8
- const jcc_common_1 = require("jcc_common");
6
+ exports.encryptWallet = exports.encryptContact = exports.encrypt = exports.decrypt = exports.isEmptyPlainObject = void 0;
9
7
  const keccak_js_1 = require("ethereum-cryptography/keccak.js");
10
- const randombytes_1 = __importDefault(require("randombytes"));
8
+ const aes_1 = require("ethereum-cryptography/aes");
9
+ const utils_1 = require("@noble/hashes/utils");
11
10
  const scrypt_1 = require("@noble/hashes/scrypt");
12
11
  const constant_1 = require("../constant");
12
+ const is_plain_object_1 = __importDefault(require("is-plain-object"));
13
+ const isEmptyPlainObject = (obj) => {
14
+ const isPlain = (0, is_plain_object_1.default)(obj);
15
+ if (!isPlain) {
16
+ return true;
17
+ }
18
+ return Object.keys(obj).length === 0;
19
+ };
20
+ exports.isEmptyPlainObject = isEmptyPlainObject;
13
21
  /**
14
22
  * decrypt wallet with password
15
23
  *
@@ -18,8 +26,10 @@ const constant_1 = require("../constant");
18
26
  * @returns {(string)} return secret if success, otherwise throws `keystore is invalid` if the keystore is invalid or
19
27
  * throws `password is wrong` if the password is wrong
20
28
  */
21
- const decrypt = (password, encryptData) => {
22
- if ((0, jcc_common_1.isEmptyObject)(encryptData) || (0, jcc_common_1.isEmptyObject)(encryptData.crypto) || (0, jcc_common_1.isEmptyObject)(encryptData.crypto.kdfparams)) {
29
+ const decrypt = async (password, encryptData) => {
30
+ if ((0, exports.isEmptyPlainObject)(encryptData) ||
31
+ (0, exports.isEmptyPlainObject)(encryptData.crypto) ||
32
+ (0, exports.isEmptyPlainObject)(encryptData.crypto.kdfparams)) {
23
33
  throw new Error(constant_1.KEYSTORE_IS_INVALID);
24
34
  }
25
35
  const iv = Buffer.from(encryptData.crypto.iv, "hex");
@@ -38,9 +48,8 @@ const decrypt = (password, encryptData) => {
38
48
  if (Buffer.from(mac).toString("hex") !== encryptData.mac) {
39
49
  throw new Error(constant_1.PASSWORD_IS_WRONG);
40
50
  }
41
- const decipher = crypto_1.default.createDecipheriv("aes-128-ctr", derivedKey.slice(0, 16), iv);
42
- const seed = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
43
- return seed.toString();
51
+ const buf = await (0, aes_1.decrypt)(ciphertext, derivedKey.slice(0, 16), iv, "aes-128-ctr");
52
+ return Buffer.from(buf);
44
53
  };
45
54
  exports.decrypt = decrypt;
46
55
  /**
@@ -51,14 +60,14 @@ exports.decrypt = decrypt;
51
60
  * @param {IEncryptModel} [opts={}]
52
61
  * @returns {IKeystoreModel}
53
62
  */
54
- const encrypt = (password, data, opts) => {
55
- const iv = opts.iv || (0, randombytes_1.default)(16).toString("hex");
63
+ const encrypt = async (password, data, opts) => {
64
+ const iv = opts.iv || Buffer.from((0, utils_1.randomBytes)(16)).toString("hex");
56
65
  const kdfparams = {
57
66
  dklen: opts.dklen || 32,
58
67
  n: opts.n || 4096,
59
68
  p: opts.p || 1,
60
69
  r: opts.r || 8,
61
- salt: opts.salt || (0, randombytes_1.default)(32).toString("hex")
70
+ salt: opts.salt || Buffer.from((0, utils_1.randomBytes)(32)).toString("hex")
62
71
  };
63
72
  const derivedKey = (0, scrypt_1.scrypt)(Buffer.from(password), Buffer.from(kdfparams.salt, "hex"), {
64
73
  N: kdfparams.n,
@@ -66,14 +75,13 @@ const encrypt = (password, data, opts) => {
66
75
  p: kdfparams.p,
67
76
  dkLen: kdfparams.dklen
68
77
  });
69
- const cipher = crypto_1.default.createCipheriv(opts.cipher || "aes-128-ctr", derivedKey.slice(0, 16), Buffer.from(iv, "hex"));
70
- const ciphertext = Buffer.concat([cipher.update(Buffer.from(data)), cipher.final()]);
78
+ const buf = await (0, aes_1.encrypt)(Buffer.from(data), derivedKey.slice(0, 16), Buffer.from(iv, "hex"), "aes-128-ctr");
71
79
  const mac = keccak_js_1.keccak256
72
80
  .create()
73
- .update(Buffer.concat([derivedKey.slice(16, 32), ciphertext]))
81
+ .update(Buffer.concat([derivedKey.slice(16, 32), buf]))
74
82
  .digest();
75
83
  return {
76
- ciphertext: ciphertext.toString("hex"),
84
+ ciphertext: Buffer.from(buf).toString("hex"),
77
85
  crypto: {
78
86
  cipher: opts.cipher || "aes-128-ctr",
79
87
  iv,
@@ -84,12 +92,12 @@ const encrypt = (password, data, opts) => {
84
92
  };
85
93
  };
86
94
  exports.encrypt = encrypt;
87
- const encryptContact = (password, contacts, opts = {}) => {
88
- return encrypt(password, JSON.stringify(contacts), opts);
95
+ const encryptContact = async (password, contacts, opts = {}) => {
96
+ return await encrypt(password, JSON.stringify(contacts), opts);
89
97
  };
90
98
  exports.encryptContact = encryptContact;
91
- const encryptWallet = (password, keypairs, opts = {}) => {
92
- const data = encrypt(password, keypairs.secret, opts);
99
+ const encryptWallet = async (password, keypairs, opts = {}) => {
100
+ const data = await encrypt(password, keypairs.secret, opts);
93
101
  data.type = keypairs.type || "swt";
94
102
  data.address = keypairs.address;
95
103
  data.default = typeof keypairs.default === "boolean" ? keypairs.default : true;
@@ -97,4 +105,3 @@ const encryptWallet = (password, keypairs, opts = {}) => {
97
105
  return data;
98
106
  };
99
107
  exports.encryptWallet = encryptWallet;
100
- //# sourceMappingURL=index.js.map