jcc_wallet 3.0.2 → 4.0.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 (96) hide show
  1. package/dist/jcc-wallet.min.js +24 -45
  2. package/lib/{constant/index.js → constant.js} +8 -8
  3. package/lib/constant.js.map +1 -0
  4. package/lib/hd/constant.js +4 -4
  5. package/lib/hd/constant.js.map +1 -1
  6. package/lib/hd/eos.plugin.js +36 -39
  7. package/lib/hd/eos.plugin.js.map +1 -1
  8. package/lib/hd/ethereum.plugin.d.ts +1 -0
  9. package/lib/hd/ethereum.plugin.js +86 -67
  10. package/lib/hd/ethereum.plugin.js.map +1 -1
  11. package/lib/hd/index.d.ts +1 -1
  12. package/lib/hd/index.js +167 -179
  13. package/lib/hd/index.js.map +1 -1
  14. package/lib/hd/plugins.d.ts +9 -0
  15. package/lib/hd/plugins.js +26 -22
  16. package/lib/hd/plugins.js.map +1 -1
  17. package/lib/hd/swtc.plugin.d.ts +1 -5
  18. package/lib/hd/swtc.plugin.js +74 -60
  19. package/lib/hd/swtc.plugin.js.map +1 -1
  20. package/lib/hd/tron.plugin.js +28 -33
  21. package/lib/hd/tron.plugin.js.map +1 -1
  22. package/lib/index.d.ts +3 -4
  23. package/lib/index.js +5 -8
  24. package/lib/index.js.map +1 -1
  25. package/lib/{jingchangWallet/index.d.ts → jingchangWallet.d.ts} +1 -1
  26. package/lib/jingchangWallet.js +531 -0
  27. package/lib/jingchangWallet.js.map +1 -0
  28. package/lib/minify-ethereumjs-util/account.d.ts +1 -0
  29. package/lib/minify-ethereumjs-util/account.js +22 -0
  30. package/lib/minify-ethereumjs-util/account.js.map +1 -0
  31. package/lib/minify-ethereumjs-util/bytes.d.ts +7 -0
  32. package/lib/minify-ethereumjs-util/bytes.js +100 -0
  33. package/lib/minify-ethereumjs-util/bytes.js.map +1 -0
  34. package/lib/minify-ethereumjs-util/constants.d.ts +4 -0
  35. package/lib/minify-ethereumjs-util/constants.js +9 -0
  36. package/lib/minify-ethereumjs-util/constants.js.map +1 -0
  37. package/lib/minify-ethereumjs-util/helper.d.ts +5 -0
  38. package/lib/minify-ethereumjs-util/helper.js +16 -0
  39. package/lib/minify-ethereumjs-util/helper.js.map +1 -0
  40. package/lib/minify-ethereumjs-util/internal.d.ts +8 -0
  41. package/lib/minify-ethereumjs-util/internal.js +33 -0
  42. package/lib/minify-ethereumjs-util/internal.js.map +1 -0
  43. package/lib/minify-ethereumjs-util/signature.d.ts +13 -0
  44. package/lib/minify-ethereumjs-util/signature.js +45 -0
  45. package/lib/minify-ethereumjs-util/signature.js.map +1 -0
  46. package/lib/minify-swtc-keypair/address-codec.d.ts +16 -0
  47. package/lib/minify-swtc-keypair/address-codec.js +166 -0
  48. package/lib/minify-swtc-keypair/address-codec.js.map +1 -0
  49. package/lib/minify-swtc-keypair/index.d.ts +25 -0
  50. package/lib/minify-swtc-keypair/index.js +178 -0
  51. package/lib/minify-swtc-keypair/index.js.map +1 -0
  52. package/lib/minify-swtc-keypair/sha512.d.ts +9 -0
  53. package/lib/minify-swtc-keypair/sha512.js +33 -0
  54. package/lib/minify-swtc-keypair/sha512.js.map +1 -0
  55. package/lib/minify-swtc-keypair/utils.d.ts +13 -0
  56. package/lib/minify-swtc-keypair/utils.js +63 -0
  57. package/lib/minify-swtc-keypair/utils.js.map +1 -0
  58. package/lib/minify-tron/address.d.ts +6 -0
  59. package/lib/minify-tron/address.js +11 -0
  60. package/lib/minify-tron/address.js.map +1 -0
  61. package/lib/minify-tron/base58.d.ts +2 -0
  62. package/lib/minify-tron/base58.js +67 -0
  63. package/lib/minify-tron/base58.js.map +1 -0
  64. package/lib/minify-tron/bytes.d.ts +2 -0
  65. package/lib/minify-tron/bytes.js +23 -0
  66. package/lib/minify-tron/bytes.js.map +1 -0
  67. package/lib/minify-tron/code.d.ts +3 -0
  68. package/lib/minify-tron/code.js +56 -0
  69. package/lib/minify-tron/code.js.map +1 -0
  70. package/lib/minify-tron/crypto.d.ts +7 -0
  71. package/lib/minify-tron/crypto.js +83 -0
  72. package/lib/minify-tron/crypto.js.map +1 -0
  73. package/lib/minify-tron/message.d.ts +4 -0
  74. package/lib/minify-tron/message.js +42 -0
  75. package/lib/minify-tron/message.js.map +1 -0
  76. package/lib/{types/index.d.ts → types.d.ts} +16 -3
  77. package/lib/types.js +16 -0
  78. package/lib/types.js.map +1 -0
  79. package/lib/util/index.js +40 -30
  80. package/lib/util/index.js.map +1 -1
  81. package/package.json +21 -16
  82. package/lib/constant/index.js.map +0 -1
  83. package/lib/eth/index.d.ts +0 -37
  84. package/lib/eth/index.js +0 -98
  85. package/lib/eth/index.js.map +0 -1
  86. package/lib/jingchangWallet/index.js +0 -713
  87. package/lib/jingchangWallet/index.js.map +0 -1
  88. package/lib/moac/index.d.ts +0 -29
  89. package/lib/moac/index.js +0 -58
  90. package/lib/moac/index.js.map +0 -1
  91. package/lib/types/index.js +0 -3
  92. package/lib/types/index.js.map +0 -1
  93. package/lib/x-wallet/index.d.ts +0 -22
  94. package/lib/x-wallet/index.js +0 -86
  95. package/lib/x-wallet/index.js.map +0 -1
  96. /package/lib/{constant/index.d.ts → constant.d.ts} +0 -0
@@ -1,713 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- var assert_1 = __importDefault(require("assert"));
43
- var clone_deep_1 = __importDefault(require("clone-deep"));
44
- var crypto_1 = __importDefault(require("crypto"));
45
- var eccrypto_1 = __importDefault(require("eccrypto"));
46
- var jcc_common_1 = require("jcc_common");
47
- var lockr_1 = __importDefault(require("lockr"));
48
- var keypairs_1 = require("@swtc/keypairs");
49
- var constant_1 = require("../constant");
50
- var x_wallet_1 = require("../x-wallet");
51
- var util_1 = require("../util");
52
- lockr_1.default.prefix = "jingchang_";
53
- /**
54
- * api of jingchang wallet
55
- *
56
- * @export
57
- * @class JingchangWallet
58
- */
59
- var JingchangWallet = /** @class */ (function () {
60
- /**
61
- * Creates an instance of JingchangWallet.
62
- * @param {IJingchangWalletModel} wallet
63
- * @param {boolean} [multiple=false] if the value is true, support save multiple wallet keystore for each type, otherwise only support one.
64
- * @param {boolean} [samePassword=true] if the value is true, use the default swt keystore's password which be generated
65
- * in the beginning as password for other type.
66
- * @memberof JingchangWallet
67
- */
68
- function JingchangWallet(wallet, multiple, samePassword) {
69
- if (multiple === void 0) { multiple = false; }
70
- if (samePassword === void 0) { samePassword = true; }
71
- this._multiple = multiple;
72
- this._jingchangWallet = wallet;
73
- this._samePassword = samePassword;
74
- }
75
- /**
76
- * check jingchang wallet is valid or not
77
- *
78
- * @static
79
- * @param {*} wallet
80
- * @returns {boolean} return true if valid.
81
- * @memberof JingchangWallet
82
- */
83
- JingchangWallet.isValid = function (wallet) {
84
- try {
85
- if (typeof wallet === "string") {
86
- wallet = JSON.parse(wallet);
87
- }
88
- var walletsNotEmpty = !(0, jcc_common_1.isEmptyObject)(wallet) && Array.isArray(wallet.wallets) && wallet.wallets.length > 0;
89
- return Boolean(walletsNotEmpty && wallet.contact && wallet.id && wallet.version);
90
- }
91
- catch (error) {
92
- return false;
93
- }
94
- };
95
- /**
96
- * create a jingchang wallet
97
- *
98
- * @static
99
- * @param {string} password password for keystore
100
- * @param {string} [secret] swtc chain's secret
101
- * @param {string} [alias] wallet name
102
- * @returns {Promise<IJingchangWalletModel>} resolve jingchang wallet if success.
103
- * @memberof JingchangWallet
104
- */
105
- JingchangWallet.generate = function (password, secret, alias) {
106
- return new Promise(function (resolve, reject) {
107
- var keypairs = {};
108
- if (secret === undefined) {
109
- var wallet = x_wallet_1.jtWallet.createWallet();
110
- secret = wallet.secret;
111
- keypairs.address = wallet.address;
112
- }
113
- else {
114
- if (!x_wallet_1.jtWallet.isValidSecret(secret)) {
115
- return reject(new Error(constant_1.SECRET_IS_INVALID));
116
- }
117
- keypairs.address = x_wallet_1.jtWallet.getAddress(secret);
118
- }
119
- keypairs.secret = secret;
120
- keypairs.type = "swt";
121
- keypairs.default = true;
122
- keypairs.alias = alias || "swt wallet";
123
- var jcWallet = {};
124
- var walletObj = (0, util_1.encryptWallet)(password, keypairs);
125
- jcWallet.version = JingchangWallet.version;
126
- jcWallet.id = JingchangWallet._walletID;
127
- jcWallet.contact = {};
128
- jcWallet.wallets = [];
129
- jcWallet.wallets.push(walletObj);
130
- return resolve(jcWallet);
131
- });
132
- };
133
- /**
134
- * get jingchang wallet from local storage
135
- *
136
- * @static
137
- * @returns {(IJingchangWalletModel | null)} return jingchang wallet or null
138
- * @memberof JingchangWallet
139
- */
140
- JingchangWallet.get = function () {
141
- var jcWallet = lockr_1.default.get(JingchangWallet._walletID);
142
- if (!JingchangWallet.isValid(jcWallet)) {
143
- return null;
144
- }
145
- return jcWallet;
146
- };
147
- /**
148
- * clear jingchang wallet from local storage
149
- *
150
- * @static
151
- * @memberof JingchangWallet
152
- */
153
- JingchangWallet.clear = function () {
154
- lockr_1.default.set(JingchangWallet._walletID, {});
155
- };
156
- /**
157
- * save jingchang wallet to local storage.
158
- *
159
- * @static
160
- * @param {IJingchangWalletModel} wallet
161
- * @memberof JingchangWallet
162
- */
163
- JingchangWallet.save = function (wallet) {
164
- lockr_1.default.set(JingchangWallet._walletID, wallet);
165
- };
166
- /**
167
- * derive key pair with secret
168
- *
169
- * @static
170
- * @param {string} secret
171
- * @param {string} [chain="swt"]
172
- * @returns {IKeyPair} for privateKey, it's length should be 64 when call `decryptWithPrivateKey`, but the origin derived
173
- * privateKey's length is 66 that contains prefix `00` for `secp256k1` or `ED` for `ed25519`, so removed it.
174
- * @memberof JingchangWallet
175
- */
176
- JingchangWallet.deriveKeyPair = function (secret, chain) {
177
- if (chain === void 0) { chain = "swt"; }
178
- var keyPair = (0, keypairs_1.Factory)(chain);
179
- var pair = keyPair.deriveKeyPair(secret);
180
- return {
181
- privateKey: pair.privateKey.substring(2),
182
- publicKey: pair.publicKey
183
- };
184
- };
185
- /**
186
- * encrypt data with public key
187
- *
188
- * @static
189
- * @param {string} message
190
- * @param {string} publicKey
191
- * @returns {Promise<IEncrypt>}
192
- * @memberof JingchangWallet
193
- */
194
- JingchangWallet.encryptWithPublicKey = function (message, publicKey) {
195
- return __awaiter(this, void 0, void 0, function () {
196
- var encode;
197
- return __generator(this, function (_a) {
198
- switch (_a.label) {
199
- case 0: return [4 /*yield*/, eccrypto_1.default.encrypt(Buffer.from(publicKey, "hex"), Buffer.from(message))];
200
- case 1:
201
- encode = _a.sent();
202
- return [2 /*return*/, {
203
- ciphertext: encode.ciphertext.toString("hex"),
204
- ephemPublicKey: encode.ephemPublicKey.toString("hex"),
205
- iv: encode.iv.toString("hex"),
206
- mac: encode.mac.toString("hex")
207
- }];
208
- }
209
- });
210
- });
211
- };
212
- /**
213
- * decrypt data with private key
214
- *
215
- * @static
216
- * @param {IEncrypt} message
217
- * @param {string} privateKey the privateKey's length should be 64
218
- * @returns {Promise<string>}
219
- * @memberof JingchangWallet
220
- */
221
- JingchangWallet.decryptWithPrivateKey = function (message, privateKey) {
222
- return __awaiter(this, void 0, void 0, function () {
223
- var encode, decode;
224
- return __generator(this, function (_a) {
225
- switch (_a.label) {
226
- case 0:
227
- encode = {
228
- ciphertext: Buffer.from(message.ciphertext, "hex"),
229
- ephemPublicKey: Buffer.from(message.ephemPublicKey, "hex"),
230
- iv: Buffer.from(message.iv, "hex"),
231
- mac: Buffer.from(message.mac, "hex")
232
- };
233
- return [4 /*yield*/, eccrypto_1.default.decrypt(Buffer.from(privateKey, "hex"), encode)];
234
- case 1:
235
- decode = _a.sent();
236
- return [2 /*return*/, decode.toString()];
237
- }
238
- });
239
- });
240
- };
241
- /**
242
- * get wallets from jingchang wallet
243
- *
244
- * @static
245
- * @param {IJingchangWalletModel} jcWallet
246
- * @returns {Array<IKeystoreModel>} return wallets if valid, otherwise return empty array.
247
- * @memberof JingchangWallet
248
- */
249
- JingchangWallet.getWallets = function (keystore) {
250
- var wallets;
251
- if (JingchangWallet.isValid(keystore)) {
252
- wallets = keystore.wallets;
253
- }
254
- else {
255
- wallets = [];
256
- }
257
- return wallets;
258
- };
259
- /**
260
- * set property of _jingchangWallet
261
- *
262
- * @param {IJingchangWalletModel} wallet
263
- * @memberof JingchangWallet
264
- */
265
- JingchangWallet.prototype.setJingchangWallet = function (wallet) {
266
- this._jingchangWallet = wallet;
267
- };
268
- /**
269
- * get default wallet's keystore address for each type
270
- *
271
- * @param {string} [type="swt"]
272
- * @returns {Promise<string>} resolve address if success
273
- * @memberof JingchangWallet
274
- */
275
- JingchangWallet.prototype.getAddress = function (type) {
276
- if (type === void 0) { type = "swt"; }
277
- return __awaiter(this, void 0, void 0, function () {
278
- var wallet;
279
- return __generator(this, function (_a) {
280
- switch (_a.label) {
281
- case 0: return [4 /*yield*/, this.getWalletWithType(type)];
282
- case 1:
283
- wallet = _a.sent();
284
- return [2 /*return*/, wallet.address];
285
- }
286
- });
287
- });
288
- };
289
- /**
290
- * get default wallet keystore with type
291
- *
292
- * @param {string} [type="swt"]
293
- * @returns {Promise<IKeystoreModel>} resolve default wallet keystore if success.
294
- * @memberof JingchangWallet
295
- */
296
- JingchangWallet.prototype.getWalletWithType = function (type) {
297
- if (type === void 0) { type = "swt"; }
298
- return __awaiter(this, void 0, void 0, function () {
299
- var wallet;
300
- return __generator(this, function (_a) {
301
- wallet = this.findWallet(function (w) { return w.type.toLowerCase() === type.toLowerCase() && w.default; });
302
- return [2 /*return*/, wallet];
303
- });
304
- });
305
- };
306
- /**
307
- * get wallet keystore with address
308
- *
309
- * @param {string} address
310
- * @returns {Promise<IKeystoreModel>} resolve wallet keystore if success.
311
- * @memberof JingchangWallet
312
- */
313
- JingchangWallet.prototype.getWalletWithAddress = function (address) {
314
- return __awaiter(this, void 0, void 0, function () {
315
- var wallet;
316
- return __generator(this, function (_a) {
317
- switch (_a.label) {
318
- case 0: return [4 /*yield*/, this.findWallet(function (w) { return w.address === address; })];
319
- case 1:
320
- wallet = _a.sent();
321
- return [2 /*return*/, wallet];
322
- }
323
- });
324
- });
325
- };
326
- /**
327
- * check if has default wallet for each type
328
- *
329
- * @param {string} [type="swt"]
330
- * @returns {boolean} return true if has default
331
- * @memberof JingchangWallet
332
- */
333
- JingchangWallet.prototype.hasDefault = function (type) {
334
- if (type === void 0) { type = "swt"; }
335
- try {
336
- var wallet = this.findWallet(function (w) { return w.type === type && w.default; });
337
- return !(0, jcc_common_1.isEmptyObject)(wallet);
338
- }
339
- catch (error) {
340
- return false;
341
- }
342
- };
343
- /**
344
- * get the default wallet keystore's secret with type.
345
- *
346
- * @param {string} password
347
- * @param {string} [type="swt"]
348
- * @returns {Promise<string>}
349
- * @memberof JingchangWallet
350
- */
351
- JingchangWallet.prototype.getSecretWithType = function (password, type) {
352
- if (type === void 0) { type = "swt"; }
353
- return __awaiter(this, void 0, void 0, function () {
354
- var wallet, secret;
355
- return __generator(this, function (_a) {
356
- switch (_a.label) {
357
- case 0: return [4 /*yield*/, this.findWallet(function (w) { return w.type === type && w.default; })];
358
- case 1:
359
- wallet = _a.sent();
360
- secret = (0, util_1.decrypt)(password, wallet);
361
- return [2 /*return*/, secret];
362
- }
363
- });
364
- });
365
- };
366
- /**
367
- * get the wallet keystore's secret with address.
368
- *
369
- * @param {string} password
370
- * @param {string} address
371
- * @returns {Promise<string>}
372
- * @memberof JingchangWallet resolve secret if success.
373
- */
374
- JingchangWallet.prototype.getSecretWithAddress = function (password, address) {
375
- return __awaiter(this, void 0, void 0, function () {
376
- var wallet, secret;
377
- return __generator(this, function (_a) {
378
- switch (_a.label) {
379
- case 0: return [4 /*yield*/, this.findWallet(function (w) { return w.address === address; })];
380
- case 1:
381
- wallet = _a.sent();
382
- secret = (0, util_1.decrypt)(password, wallet);
383
- return [2 /*return*/, secret];
384
- }
385
- });
386
- });
387
- };
388
- /**
389
- * change the whole jingchang wallet password, if you set property of _samePassword is false, will throw an error
390
- *
391
- * @param {string} oldPassword
392
- * @param {string} newPassword
393
- * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
394
- * @memberof JingchangWallet
395
- */
396
- JingchangWallet.prototype.changeWholePassword = function (oldPassword, newPassword) {
397
- return __awaiter(this, void 0, void 0, function () {
398
- var jcWallet, wallets, arr, _i, wallets_1, wallet, address, secret, keypairs, newWallet;
399
- return __generator(this, function (_a) {
400
- switch (_a.label) {
401
- case 0:
402
- if (!this._samePassword) {
403
- throw new Error("the property of _samePassword is false, so please don't call this function!");
404
- }
405
- jcWallet = (0, clone_deep_1.default)(this._jingchangWallet);
406
- assert_1.default.notEqual(jcWallet, this._jingchangWallet);
407
- wallets = JingchangWallet.getWallets(jcWallet);
408
- arr = [];
409
- _i = 0, wallets_1 = wallets;
410
- _a.label = 1;
411
- case 1:
412
- if (!(_i < wallets_1.length)) return [3 /*break*/, 4];
413
- wallet = wallets_1[_i];
414
- address = wallet.address;
415
- return [4 /*yield*/, this.getSecretWithAddress(oldPassword, address)];
416
- case 2:
417
- secret = _a.sent();
418
- keypairs = {
419
- address: wallet.address,
420
- alias: wallet.alias,
421
- default: wallet.default,
422
- secret: secret,
423
- type: wallet.type
424
- };
425
- newWallet = this.getEncryptData(newPassword, keypairs);
426
- arr.push(newWallet);
427
- _a.label = 3;
428
- case 3:
429
- _i++;
430
- return [3 /*break*/, 1];
431
- case 4:
432
- jcWallet.wallets = arr;
433
- this.setJingchangWallet(jcWallet);
434
- return [2 /*return*/, jcWallet];
435
- }
436
- });
437
- });
438
- };
439
- /**
440
- * change the keystore password with address, if you set the property of _samePassword is true, will throw an error
441
- *
442
- * @param {string} address
443
- * @param {string} oldPassword
444
- * @param {string} newPassword
445
- * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
446
- * @memberof JingchangWallet
447
- */
448
- JingchangWallet.prototype.changePasswordWithAddress = function (address, oldPassword, newPassword) {
449
- return __awaiter(this, void 0, void 0, function () {
450
- var wallet, secret, keypairs, newWallet;
451
- return __generator(this, function (_a) {
452
- switch (_a.label) {
453
- case 0:
454
- if (this._samePassword) {
455
- throw new Error("the property of _samePassword is true, so please don't call this function!");
456
- }
457
- wallet = this.findWallet(function (w) { return w.address === address; });
458
- return [4 /*yield*/, this.getSecretWithAddress(oldPassword, address)];
459
- case 1:
460
- secret = _a.sent();
461
- keypairs = {
462
- address: wallet.address,
463
- alias: wallet.alias,
464
- default: wallet.default,
465
- secret: secret,
466
- type: wallet.type
467
- };
468
- newWallet = this.getEncryptData(newPassword, keypairs);
469
- // shadow copy
470
- wallet.ciphertext = newWallet.ciphertext;
471
- wallet.crypto = newWallet.crypto;
472
- wallet.mac = newWallet.mac;
473
- return [2 /*return*/, this._jingchangWallet];
474
- }
475
- });
476
- });
477
- };
478
- /**
479
- * replace keystore, if forget password
480
- *
481
- * @param {string} secret
482
- * @param {string} password
483
- * @param {(secret: string) => string} retriveSecret
484
- * @returns {Promise<IJingchangWalletModel>}
485
- * @memberof JingchangWallet
486
- */
487
- JingchangWallet.prototype.replaceKeystore = function (secret, password, retriveSecret) {
488
- return __awaiter(this, void 0, void 0, function () {
489
- var address, wallet, keypairs, newWallet;
490
- return __generator(this, function (_a) {
491
- address = retriveSecret(secret);
492
- wallet = this.findWallet(function (w) { return w.address === address; });
493
- keypairs = {
494
- address: wallet.address,
495
- alias: wallet.alias,
496
- default: wallet.default,
497
- secret: secret,
498
- type: wallet.type
499
- };
500
- newWallet = this.getEncryptData(password, keypairs);
501
- // shadow copy
502
- wallet.ciphertext = newWallet.ciphertext;
503
- wallet.crypto = newWallet.crypto;
504
- wallet.mac = newWallet.mac;
505
- return [2 /*return*/, this._jingchangWallet];
506
- });
507
- });
508
- };
509
- /**
510
- * remove default wallet keystore of the given type
511
- *
512
- * @param {string} [type="swt"]
513
- * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
514
- * @memberof JingchangWallet
515
- */
516
- JingchangWallet.prototype.removeWalletWithType = function (type) {
517
- if (type === void 0) { type = "swt"; }
518
- return __awaiter(this, void 0, void 0, function () {
519
- var address, wallet;
520
- return __generator(this, function (_a) {
521
- switch (_a.label) {
522
- case 0: return [4 /*yield*/, this.getAddress(type)];
523
- case 1:
524
- address = _a.sent();
525
- return [4 /*yield*/, this.removeWalletWithAddress(address)];
526
- case 2:
527
- wallet = _a.sent();
528
- return [2 /*return*/, wallet];
529
- }
530
- });
531
- });
532
- };
533
- /**
534
- * remove wallet keystore of the given address
535
- *
536
- * @param {string} address
537
- * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
538
- * @memberof JingchangWallet
539
- */
540
- JingchangWallet.prototype.removeWalletWithAddress = function (address) {
541
- return __awaiter(this, void 0, void 0, function () {
542
- var jcWallet, wallets, wallet, index, next;
543
- return __generator(this, function (_a) {
544
- jcWallet = (0, clone_deep_1.default)(this._jingchangWallet);
545
- assert_1.default.notEqual(this._jingchangWallet, jcWallet);
546
- wallets = JingchangWallet.getWallets(jcWallet);
547
- wallet = this.findWallet(function (w) { return w.address === address; });
548
- index = wallets.findIndex(function (w) { return w.address === wallet.address; });
549
- wallets.splice(index, 1);
550
- next = wallets.find(function (w) { return w.type === wallet.type; });
551
- if (next) {
552
- next.default = true;
553
- }
554
- this.setJingchangWallet(jcWallet);
555
- return [2 /*return*/, jcWallet];
556
- });
557
- });
558
- };
559
- /**
560
- * set defalut wallet keystore for each type
561
- *
562
- * @param {string} address
563
- * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
564
- * @memberof JingchangWallet
565
- */
566
- JingchangWallet.prototype.setDefaultWallet = function (address) {
567
- return __awaiter(this, void 0, void 0, function () {
568
- var wallet, defaultWallet;
569
- return __generator(this, function (_a) {
570
- switch (_a.label) {
571
- case 0: return [4 /*yield*/, this.getWalletWithAddress(address)];
572
- case 1:
573
- wallet = _a.sent();
574
- if (wallet.default) {
575
- return [2 /*return*/, this._jingchangWallet];
576
- }
577
- return [4 /*yield*/, this.getWalletWithType(wallet.type)];
578
- case 2:
579
- defaultWallet = _a.sent();
580
- defaultWallet.default = false;
581
- wallet.default = true;
582
- return [2 /*return*/, this._jingchangWallet];
583
- }
584
- });
585
- });
586
- };
587
- /**
588
- * import secret
589
- *
590
- * @param {string} secret
591
- * @param {string} password
592
- * @param {string} type
593
- * @param {(secret: string) => string} retriveSecret
594
- * @param {string} [alias] wallet name
595
- * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
596
- * @memberof JingchangWallet
597
- */
598
- JingchangWallet.prototype.importSecret = function (secret, password, type, retriveSecret, alias) {
599
- return __awaiter(this, void 0, void 0, function () {
600
- var address, wallets, wallet, keypairs, w;
601
- return __generator(this, function (_a) {
602
- switch (_a.label) {
603
- case 0:
604
- if (!this._samePassword) return [3 /*break*/, 2];
605
- // validate default password of swt keystore is right or not
606
- return [4 /*yield*/, this.getSecretWithType(password)];
607
- case 1:
608
- // validate default password of swt keystore is right or not
609
- _a.sent();
610
- _a.label = 2;
611
- case 2:
612
- address = retriveSecret(secret);
613
- if (!address) {
614
- throw new Error(constant_1.SECRET_IS_INVALID);
615
- }
616
- wallets = JingchangWallet.getWallets(this._jingchangWallet);
617
- wallet = wallets.find(function (w) { return w.address === address && w.type === type; });
618
- if (wallet) {
619
- throw new Error(constant_1.ADDRESS_IS_EXISTENT);
620
- }
621
- keypairs = {
622
- address: address,
623
- alias: alias || "".concat(type, " wallet"),
624
- secret: secret,
625
- type: type
626
- };
627
- return [4 /*yield*/, this.saveWallet(password, keypairs)];
628
- case 3:
629
- w = _a.sent();
630
- return [2 /*return*/, w];
631
- }
632
- });
633
- });
634
- };
635
- /**
636
- * find wallet keystore according to filter function
637
- *
638
- * @protected
639
- * @param {(wallet: IKeystoreModel) => boolean} filter
640
- * @returns {IKeystoreModel} return wallet keystore if existent, otherwise throw `keystore is invalid` if the jingchang wallet is invalid
641
- * or throw `wallet is empty` if the wallet isn't existent
642
- * @memberof JingchangWallet
643
- */
644
- JingchangWallet.prototype.findWallet = function (filter) {
645
- var jingchangWallet = this._jingchangWallet;
646
- if (!JingchangWallet.isValid(jingchangWallet)) {
647
- throw new Error(constant_1.KEYSTORE_IS_INVALID);
648
- }
649
- var wallets = jingchangWallet.wallets;
650
- var wallet = wallets.find(filter);
651
- if ((0, jcc_common_1.isEmptyObject)(wallet)) {
652
- throw new Error(constant_1.WALLET_IS_EMPTY);
653
- }
654
- return wallet;
655
- };
656
- /**
657
- * encrypt data
658
- *
659
- * @protected
660
- * @param {string} password
661
- * @param {IKeypairsModel} keypairs
662
- * @returns {IKeystoreModel}
663
- * @memberof JingchangWallet
664
- */
665
- JingchangWallet.prototype.getEncryptData = function (password, keypairs) {
666
- var encryptData = (0, util_1.encryptWallet)(password, keypairs, {});
667
- return encryptData;
668
- };
669
- /**
670
- * save wallet keystore to jingchang wallet
671
- *
672
- * @private
673
- * @param {string} password
674
- * @param {IKeypairsModel} keypairs
675
- * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
676
- * @memberof JingchangWallet
677
- */
678
- JingchangWallet.prototype.saveWallet = function (password, keypairs) {
679
- return __awaiter(this, void 0, void 0, function () {
680
- var encryptData, jcWallet, wallets, pre;
681
- return __generator(this, function (_a) {
682
- // support type: ethereum, stream, jingtum, call and moac
683
- keypairs.default = this._multiple ? !this.hasDefault(keypairs.type) : true;
684
- encryptData = this.getEncryptData(password, keypairs);
685
- jcWallet = (0, clone_deep_1.default)(this._jingchangWallet);
686
- assert_1.default.notEqual(this._jingchangWallet, jcWallet);
687
- wallets = jcWallet.wallets;
688
- pre = wallets.findIndex(function (w) { return w.type.toLowerCase() === keypairs.type.toLowerCase(); });
689
- if (this._multiple) {
690
- wallets.push(encryptData);
691
- }
692
- else {
693
- /* istanbul ignore else */
694
- if (pre >= 0) {
695
- wallets.splice(pre, 1);
696
- }
697
- wallets.push(encryptData);
698
- }
699
- this.setJingchangWallet(jcWallet);
700
- return [2 /*return*/, jcWallet];
701
- });
702
- });
703
- };
704
- JingchangWallet.version = "1.0";
705
- JingchangWallet._name = "wallets";
706
- JingchangWallet._walletID = crypto_1.default
707
- .createHash("sha256")
708
- .update(JingchangWallet._name.toLowerCase())
709
- .digest("hex");
710
- return JingchangWallet;
711
- }());
712
- exports.default = JingchangWallet;
713
- //# sourceMappingURL=index.js.map