@dynamic-labs-wallet/ton 0.0.230

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/index.esm.js ADDED
@@ -0,0 +1,623 @@
1
+ import { AuthMode, DynamicWalletClient, getClientKeyShareBackupInfo, WalletOperation } from '@dynamic-labs-wallet/browser';
2
+ import { WalletContractV5R1 } from '@ton/ton';
3
+ import * as sdkApiCore from '@dynamic-labs/sdk-api-core';
4
+ export { sdkApiCore };
5
+
6
+ /**
7
+ * Derives a TON address from a public key
8
+ * TON addresses are derived from the wallet contract state init
9
+ */ var deriveTonAddress = function(param) {
10
+ var publicKeyHex = param.publicKeyHex, _param_workchain = param.workchain, workchain = _param_workchain === void 0 ? 0 : _param_workchain;
11
+ var pubKeyBytes = Buffer.from(publicKeyHex, 'hex');
12
+ if (pubKeyBytes.length !== 32) {
13
+ throw new Error("Invalid public key length: ".concat(pubKeyBytes.length, ", expected 32"));
14
+ }
15
+ var walletContract = WalletContractV5R1.create({
16
+ workchain: workchain,
17
+ publicKey: pubKeyBytes
18
+ });
19
+ return walletContract.address.toString({
20
+ urlSafe: true,
21
+ bounceable: false,
22
+ testOnly: false
23
+ });
24
+ };
25
+
26
+ var ERROR_KEYGEN_FAILED = 'Error with keygen';
27
+ var ERROR_CREATE_WALLET_ACCOUNT = 'Error creating ton wallet account';
28
+ var ERROR_EXPORT_PRIVATE_KEY = 'Error exporting private key';
29
+ var ERROR_SIGN_MESSAGE = 'Error signing message';
30
+ var ERROR_ACCOUNT_ADDRESS_REQUIRED = 'Account address is required';
31
+ var ERROR_SIGN_TRANSACTION = 'Error signing transaction';
32
+
33
+ function _assert_this_initialized(self) {
34
+ if (self === void 0) {
35
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
36
+ }
37
+ return self;
38
+ }
39
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
40
+ try {
41
+ var info = gen[key](arg);
42
+ var value = info.value;
43
+ } catch (error) {
44
+ reject(error);
45
+ return;
46
+ }
47
+ if (info.done) {
48
+ resolve(value);
49
+ } else {
50
+ Promise.resolve(value).then(_next, _throw);
51
+ }
52
+ }
53
+ function _async_to_generator(fn) {
54
+ return function() {
55
+ var self = this, args = arguments;
56
+ return new Promise(function(resolve, reject) {
57
+ var gen = fn.apply(self, args);
58
+ function _next(value) {
59
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
60
+ }
61
+ function _throw(err) {
62
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
63
+ }
64
+ _next(undefined);
65
+ });
66
+ };
67
+ }
68
+ function _call_super(_this, derived, args) {
69
+ derived = _get_prototype_of(derived);
70
+ return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
71
+ }
72
+ function _class_call_check(instance, Constructor) {
73
+ if (!(instance instanceof Constructor)) {
74
+ throw new TypeError("Cannot call a class as a function");
75
+ }
76
+ }
77
+ function _defineProperties(target, props) {
78
+ for(var i = 0; i < props.length; i++){
79
+ var descriptor = props[i];
80
+ descriptor.enumerable = descriptor.enumerable || false;
81
+ descriptor.configurable = true;
82
+ if ("value" in descriptor) descriptor.writable = true;
83
+ Object.defineProperty(target, descriptor.key, descriptor);
84
+ }
85
+ }
86
+ function _create_class(Constructor, protoProps, staticProps) {
87
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
88
+ return Constructor;
89
+ }
90
+ function _define_property(obj, key, value) {
91
+ if (key in obj) {
92
+ Object.defineProperty(obj, key, {
93
+ value: value,
94
+ enumerable: true,
95
+ configurable: true,
96
+ writable: true
97
+ });
98
+ } else {
99
+ obj[key] = value;
100
+ }
101
+ return obj;
102
+ }
103
+ function _get_prototype_of(o) {
104
+ _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
105
+ return o.__proto__ || Object.getPrototypeOf(o);
106
+ };
107
+ return _get_prototype_of(o);
108
+ }
109
+ function _inherits(subClass, superClass) {
110
+ if (typeof superClass !== "function" && superClass !== null) {
111
+ throw new TypeError("Super expression must either be null or a function");
112
+ }
113
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
114
+ constructor: {
115
+ value: subClass,
116
+ writable: true,
117
+ configurable: true
118
+ }
119
+ });
120
+ if (superClass) _set_prototype_of(subClass, superClass);
121
+ }
122
+ function _object_spread(target) {
123
+ for(var i = 1; i < arguments.length; i++){
124
+ var source = arguments[i] != null ? arguments[i] : {};
125
+ var ownKeys = Object.keys(source);
126
+ if (typeof Object.getOwnPropertySymbols === "function") {
127
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
128
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
129
+ }));
130
+ }
131
+ ownKeys.forEach(function(key) {
132
+ _define_property(target, key, source[key]);
133
+ });
134
+ }
135
+ return target;
136
+ }
137
+ function ownKeys(object, enumerableOnly) {
138
+ var keys = Object.keys(object);
139
+ if (Object.getOwnPropertySymbols) {
140
+ var symbols = Object.getOwnPropertySymbols(object);
141
+ keys.push.apply(keys, symbols);
142
+ }
143
+ return keys;
144
+ }
145
+ function _object_spread_props(target, source) {
146
+ source = source != null ? source : {};
147
+ if (Object.getOwnPropertyDescriptors) {
148
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
149
+ } else {
150
+ ownKeys(Object(source)).forEach(function(key) {
151
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
152
+ });
153
+ }
154
+ return target;
155
+ }
156
+ function _possible_constructor_return(self, call) {
157
+ if (call && (_type_of(call) === "object" || typeof call === "function")) {
158
+ return call;
159
+ }
160
+ return _assert_this_initialized(self);
161
+ }
162
+ function _set_prototype_of(o, p) {
163
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
164
+ o.__proto__ = p;
165
+ return o;
166
+ };
167
+ return _set_prototype_of(o, p);
168
+ }
169
+ function _type_of(obj) {
170
+ "@swc/helpers - typeof";
171
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
172
+ }
173
+ function _is_native_reflect_construct() {
174
+ try {
175
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
176
+ } catch (_) {}
177
+ return (_is_native_reflect_construct = function() {
178
+ return !!result;
179
+ })();
180
+ }
181
+ function _ts_generator(thisArg, body) {
182
+ var f, y, t, g, _ = {
183
+ label: 0,
184
+ sent: function() {
185
+ if (t[0] & 1) throw t[1];
186
+ return t[1];
187
+ },
188
+ trys: [],
189
+ ops: []
190
+ };
191
+ return g = {
192
+ next: verb(0),
193
+ "throw": verb(1),
194
+ "return": verb(2)
195
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
196
+ return this;
197
+ }), g;
198
+ function verb(n) {
199
+ return function(v) {
200
+ return step([
201
+ n,
202
+ v
203
+ ]);
204
+ };
205
+ }
206
+ function step(op) {
207
+ if (f) throw new TypeError("Generator is already executing.");
208
+ while(_)try {
209
+ 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;
210
+ if (y = 0, t) op = [
211
+ op[0] & 2,
212
+ t.value
213
+ ];
214
+ switch(op[0]){
215
+ case 0:
216
+ case 1:
217
+ t = op;
218
+ break;
219
+ case 4:
220
+ _.label++;
221
+ return {
222
+ value: op[1],
223
+ done: false
224
+ };
225
+ case 5:
226
+ _.label++;
227
+ y = op[1];
228
+ op = [
229
+ 0
230
+ ];
231
+ continue;
232
+ case 7:
233
+ op = _.ops.pop();
234
+ _.trys.pop();
235
+ continue;
236
+ default:
237
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
238
+ _ = 0;
239
+ continue;
240
+ }
241
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
242
+ _.label = op[1];
243
+ break;
244
+ }
245
+ if (op[0] === 6 && _.label < t[1]) {
246
+ _.label = t[1];
247
+ t = op;
248
+ break;
249
+ }
250
+ if (t && _.label < t[2]) {
251
+ _.label = t[2];
252
+ _.ops.push(op);
253
+ break;
254
+ }
255
+ if (t[2]) _.ops.pop();
256
+ _.trys.pop();
257
+ continue;
258
+ }
259
+ op = body.call(thisArg, _);
260
+ } catch (e) {
261
+ op = [
262
+ 6,
263
+ e
264
+ ];
265
+ y = 0;
266
+ } finally{
267
+ f = t = 0;
268
+ }
269
+ if (op[0] & 5) throw op[1];
270
+ return {
271
+ value: op[0] ? op[1] : void 0,
272
+ done: true
273
+ };
274
+ }
275
+ }
276
+ var DynamicTonWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
277
+ _inherits(DynamicTonWalletClient, DynamicWalletClient);
278
+ function DynamicTonWalletClient(param) {
279
+ var environmentId = param.environmentId, authToken = param.authToken, baseApiUrl = param.baseApiUrl, baseMPCRelayApiUrl = param.baseMPCRelayApiUrl, storageKey = param.storageKey, debug = param.debug, featureFlags = param.featureFlags, _param_authMode = param.authMode, authMode = _param_authMode === void 0 ? AuthMode.HEADER : _param_authMode, sdkVersion = param.sdkVersion, forwardMPCClient = param.forwardMPCClient;
280
+ _class_call_check(this, DynamicTonWalletClient);
281
+ var _this;
282
+ _this = _call_super(this, DynamicTonWalletClient, [
283
+ {
284
+ environmentId: environmentId,
285
+ authToken: authToken,
286
+ baseApiUrl: baseApiUrl,
287
+ baseMPCRelayApiUrl: baseMPCRelayApiUrl,
288
+ storageKey: storageKey,
289
+ debug: debug,
290
+ featureFlags: featureFlags,
291
+ authMode: authMode,
292
+ sdkVersion: sdkVersion,
293
+ forwardMPCClient: forwardMPCClient
294
+ }
295
+ ]), _define_property(_this, "chainName", 'TON');
296
+ return _this;
297
+ }
298
+ _create_class(DynamicTonWalletClient, [
299
+ {
300
+ key: "createWalletAccount",
301
+ value: function createWalletAccount(param) {
302
+ var thresholdSignatureScheme = param.thresholdSignatureScheme, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, onError = param.onError, signedSessionId = param.signedSessionId;
303
+ var _this = this;
304
+ return _async_to_generator(function() {
305
+ var ceremonyCeremonyCompleteResolver, ceremonyCompletePromise, _ref, publicKeyHex, clientKeyShares, accountAddress, pubKeyBytes, error;
306
+ return _ts_generator(this, function(_state) {
307
+ switch(_state.label){
308
+ case 0:
309
+ _state.trys.push([
310
+ 0,
311
+ 5,
312
+ ,
313
+ 6
314
+ ]);
315
+ ceremonyCompletePromise = new Promise(function(resolve) {
316
+ ceremonyCeremonyCompleteResolver = resolve;
317
+ });
318
+ return [
319
+ 4,
320
+ _this.keyGen({
321
+ chainName: _this.chainName,
322
+ thresholdSignatureScheme: thresholdSignatureScheme,
323
+ onError: onError,
324
+ onCeremonyComplete: function(accountAddress, walletId) {
325
+ // update wallet map
326
+ _this.walletMap[accountAddress] = _object_spread_props(_object_spread({}, _this.walletMap[accountAddress]), {
327
+ accountAddress: accountAddress,
328
+ walletId: walletId,
329
+ chainName: _this.chainName,
330
+ thresholdSignatureScheme: thresholdSignatureScheme,
331
+ clientKeySharesBackupInfo: getClientKeyShareBackupInfo()
332
+ });
333
+ _this.logger.debug('walletMap updated for wallet', {
334
+ context: {
335
+ accountAddress: accountAddress,
336
+ walletId: walletId,
337
+ walletMap: _this.walletMap
338
+ }
339
+ });
340
+ ceremonyCeremonyCompleteResolver(undefined);
341
+ }
342
+ })
343
+ ];
344
+ case 1:
345
+ _ref = _state.sent(), publicKeyHex = _ref.rawPublicKey, clientKeyShares = _ref.clientKeyShares;
346
+ // Wait for the ceremony to complete before proceeding
347
+ return [
348
+ 4,
349
+ ceremonyCompletePromise
350
+ ];
351
+ case 2:
352
+ _state.sent();
353
+ if (!publicKeyHex || !clientKeyShares) {
354
+ throw new Error(ERROR_KEYGEN_FAILED);
355
+ }
356
+ accountAddress = deriveTonAddress({
357
+ publicKeyHex: publicKeyHex,
358
+ workchain: 0
359
+ });
360
+ // Update client key shares in wallet map
361
+ return [
362
+ 4,
363
+ _this.setClientKeySharesToLocalStorage({
364
+ accountAddress: accountAddress,
365
+ clientKeyShares: clientKeyShares,
366
+ overwriteOrMerge: 'overwrite'
367
+ })
368
+ ];
369
+ case 3:
370
+ _state.sent();
371
+ return [
372
+ 4,
373
+ _this.storeEncryptedBackupByWalletWithRetry({
374
+ accountAddress: accountAddress,
375
+ clientKeyShares: clientKeyShares,
376
+ password: password,
377
+ signedSessionId: signedSessionId
378
+ })
379
+ ];
380
+ case 4:
381
+ _state.sent();
382
+ pubKeyBytes = Buffer.from(publicKeyHex, 'hex');
383
+ return [
384
+ 2,
385
+ {
386
+ accountAddress: accountAddress,
387
+ publicKeyHex: publicKeyHex,
388
+ rawPublicKey: new Uint8Array(pubKeyBytes)
389
+ }
390
+ ];
391
+ case 5:
392
+ error = _state.sent();
393
+ _this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
394
+ throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
395
+ case 6:
396
+ return [
397
+ 2
398
+ ];
399
+ }
400
+ });
401
+ })();
402
+ }
403
+ },
404
+ {
405
+ key: "signMessage",
406
+ value: function signMessage(param) {
407
+ var message = param.message, accountAddress = param.accountAddress, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, signedSessionId = param.signedSessionId, mfaToken = param.mfaToken, context = param.context, onError = param.onError;
408
+ var _this = this;
409
+ return _async_to_generator(function() {
410
+ var resolvedContext, signatureEd25519, formattedSignature, error;
411
+ return _ts_generator(this, function(_state) {
412
+ switch(_state.label){
413
+ case 0:
414
+ if (!accountAddress) {
415
+ throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
416
+ }
417
+ return [
418
+ 4,
419
+ _this.verifyPassword({
420
+ accountAddress: accountAddress,
421
+ password: password,
422
+ walletOperation: WalletOperation.SIGN_MESSAGE,
423
+ signedSessionId: signedSessionId
424
+ })
425
+ ];
426
+ case 1:
427
+ _state.sent();
428
+ resolvedContext = _object_spread_props(_object_spread({}, context), {
429
+ tonMessage: message
430
+ });
431
+ _state.label = 2;
432
+ case 2:
433
+ _state.trys.push([
434
+ 2,
435
+ 4,
436
+ ,
437
+ 5
438
+ ]);
439
+ return [
440
+ 4,
441
+ _this.sign({
442
+ message: message,
443
+ accountAddress: accountAddress,
444
+ chainName: _this.chainName,
445
+ password: password,
446
+ signedSessionId: signedSessionId,
447
+ mfaToken: mfaToken,
448
+ context: resolvedContext,
449
+ onError: onError
450
+ })
451
+ ];
452
+ case 3:
453
+ signatureEd25519 = _state.sent();
454
+ formattedSignature = Buffer.from(signatureEd25519).toString('base64');
455
+ return [
456
+ 2,
457
+ formattedSignature
458
+ ];
459
+ case 4:
460
+ error = _state.sent();
461
+ _this.logger.error(ERROR_SIGN_MESSAGE, error);
462
+ throw new Error(ERROR_SIGN_MESSAGE);
463
+ case 5:
464
+ return [
465
+ 2
466
+ ];
467
+ }
468
+ });
469
+ })();
470
+ }
471
+ },
472
+ {
473
+ key: "signTransaction",
474
+ value: function signTransaction(param) {
475
+ var senderAddress = param.senderAddress, transaction = param.transaction, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, signedSessionId = param.signedSessionId, mfaToken = param.mfaToken, context = param.context, onError = param.onError;
476
+ var _this = this;
477
+ return _async_to_generator(function() {
478
+ var resolvedContext, signatureEd25519, formattedSignature, error;
479
+ return _ts_generator(this, function(_state) {
480
+ switch(_state.label){
481
+ case 0:
482
+ if (!senderAddress) {
483
+ throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
484
+ }
485
+ return [
486
+ 4,
487
+ _this.verifyPassword({
488
+ accountAddress: senderAddress,
489
+ password: password,
490
+ walletOperation: WalletOperation.SIGN_TRANSACTION,
491
+ signedSessionId: signedSessionId
492
+ })
493
+ ];
494
+ case 1:
495
+ _state.sent();
496
+ resolvedContext = _object_spread_props(_object_spread({}, context), {
497
+ tonTransaction: {
498
+ serializedTransaction: transaction
499
+ }
500
+ });
501
+ _state.label = 2;
502
+ case 2:
503
+ _state.trys.push([
504
+ 2,
505
+ 4,
506
+ ,
507
+ 5
508
+ ]);
509
+ return [
510
+ 4,
511
+ _this.sign({
512
+ message: transaction,
513
+ accountAddress: senderAddress,
514
+ chainName: _this.chainName,
515
+ password: password,
516
+ signedSessionId: signedSessionId,
517
+ mfaToken: mfaToken,
518
+ context: resolvedContext,
519
+ onError: onError
520
+ })
521
+ ];
522
+ case 3:
523
+ signatureEd25519 = _state.sent();
524
+ formattedSignature = Buffer.from(signatureEd25519).toString('base64');
525
+ return [
526
+ 2,
527
+ formattedSignature
528
+ ];
529
+ case 4:
530
+ error = _state.sent();
531
+ _this.logger.error(ERROR_SIGN_TRANSACTION, error);
532
+ throw new Error(ERROR_SIGN_TRANSACTION);
533
+ case 5:
534
+ return [
535
+ 2
536
+ ];
537
+ }
538
+ });
539
+ })();
540
+ }
541
+ },
542
+ {
543
+ key: "exportPrivateKey",
544
+ value: function exportPrivateKey(param) {
545
+ var accountAddress = param.accountAddress, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, signedSessionId = param.signedSessionId, mfaToken = param.mfaToken;
546
+ var _this = this;
547
+ return _async_to_generator(function() {
548
+ var derivedPrivateKey, privateScalarHex, error;
549
+ return _ts_generator(this, function(_state) {
550
+ switch(_state.label){
551
+ case 0:
552
+ _state.trys.push([
553
+ 0,
554
+ 2,
555
+ ,
556
+ 3
557
+ ]);
558
+ return [
559
+ 4,
560
+ _this.exportKey({
561
+ accountAddress: accountAddress,
562
+ chainName: _this.chainName,
563
+ password: password,
564
+ signedSessionId: signedSessionId,
565
+ mfaToken: mfaToken
566
+ })
567
+ ];
568
+ case 1:
569
+ derivedPrivateKey = _state.sent().derivedPrivateKey;
570
+ if (!derivedPrivateKey) {
571
+ throw new Error('Derived private key is undefined');
572
+ }
573
+ // TON private keys are 64 hex characters (32 bytes)
574
+ privateScalarHex = derivedPrivateKey.slice(0, 64);
575
+ return [
576
+ 2,
577
+ privateScalarHex
578
+ ];
579
+ case 2:
580
+ error = _state.sent();
581
+ _this.logger.error(ERROR_EXPORT_PRIVATE_KEY, error);
582
+ throw new Error(ERROR_EXPORT_PRIVATE_KEY);
583
+ case 3:
584
+ return [
585
+ 2
586
+ ];
587
+ }
588
+ });
589
+ })();
590
+ }
591
+ },
592
+ {
593
+ key: "getTonWallets",
594
+ value: function getTonWallets() {
595
+ var _this = this;
596
+ return _async_to_generator(function() {
597
+ var wallets, tonWallets;
598
+ return _ts_generator(this, function(_state) {
599
+ switch(_state.label){
600
+ case 0:
601
+ return [
602
+ 4,
603
+ _this.getWallets()
604
+ ];
605
+ case 1:
606
+ wallets = _state.sent();
607
+ tonWallets = wallets.filter(function(wallet) {
608
+ return wallet.chainName === 'ton';
609
+ });
610
+ return [
611
+ 2,
612
+ tonWallets
613
+ ];
614
+ }
615
+ });
616
+ })();
617
+ }
618
+ }
619
+ ]);
620
+ return DynamicTonWalletClient;
621
+ }(DynamicWalletClient);
622
+
623
+ export { DynamicTonWalletClient };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@dynamic-labs-wallet/ton",
3
+ "version": "0.0.230",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "dependencies": {
7
+ "@dynamic-labs-wallet/browser": "0.0.230",
8
+ "@ton/ton": "^16.0.0",
9
+ "@dynamic-labs/sdk-api-core": "^0.0.828"
10
+ },
11
+ "nx": {
12
+ "sourceRoot": "packages/ton/src",
13
+ "projectType": "library",
14
+ "name": "ton",
15
+ "targets": {
16
+ "build": {}
17
+ }
18
+ },
19
+ "main": "./index.cjs.js",
20
+ "module": "./index.esm.js",
21
+ "types": "./index.esm.d.ts",
22
+ "exports": {
23
+ "./package.json": "./package.json",
24
+ ".": {
25
+ "types": "./index.esm.d.ts",
26
+ "import": "./index.esm.js",
27
+ "require": "./index.cjs.js",
28
+ "default": "./index.esm.js"
29
+ }
30
+ }
31
+ }