@dynamic-labs-wallet/stellar 0.0.0 → 0.0.274

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,789 @@
1
+ import { AuthMode, DynamicWalletClient, ERROR_PASSWORD_MISMATCH, getMPCChainConfig, WalletOperation } from '@dynamic-labs-wallet/browser';
2
+ import { StrKey, Keypair } from '@stellar/stellar-sdk';
3
+ import * as sdkApiCore from '@dynamic-labs/sdk-api-core';
4
+ export { sdkApiCore };
5
+
6
+ /**
7
+ * Derives a Stellar address (public key) from a raw Ed25519 public key
8
+ * Stellar addresses use StrKey encoding with version byte for public keys
9
+ * They start with 'G' and are 56 characters long
10
+ */ var deriveStellarAddress = function(param) {
11
+ var publicKeyHex = param.publicKeyHex;
12
+ var pubKeyBytes = Buffer.from(publicKeyHex, 'hex');
13
+ if (pubKeyBytes.length !== 32) {
14
+ throw new Error("Invalid public key length: ".concat(pubKeyBytes.length, ", expected 32"));
15
+ }
16
+ // Encode the raw Ed25519 public key to Stellar's StrKey format (G... address)
17
+ return StrKey.encodeEd25519PublicKey(pubKeyBytes);
18
+ };
19
+
20
+ var ERROR_KEYGEN_FAILED = 'Error with keygen';
21
+ var ERROR_CREATE_WALLET_ACCOUNT = 'Error creating stellar wallet account';
22
+ var ERROR_IMPORT_PRIVATE_KEY = 'Error importing private key';
23
+ var ERROR_EXPORT_PRIVATE_KEY = 'Error exporting private key';
24
+ var ERROR_SIGN_MESSAGE = 'Error signing message';
25
+ var ERROR_ACCOUNT_ADDRESS_REQUIRED = 'Account address is required';
26
+ var ERROR_SIGN_TRANSACTION = 'Error signing transaction';
27
+ var STELLAR_NETWORK_PASSPHRASES = {
28
+ MAINNET: 'Public Global Stellar Network ; September 2015',
29
+ TESTNET: 'Test SDF Network ; September 2015',
30
+ FUTURENET: 'Test SDF Future Network ; October 2022'
31
+ };
32
+ /** Horizon API base URLs. Use when fetching account/balance so mainnet UI uses mainnet, testnet uses testnet. */ var STELLAR_HORIZON_URLS = {
33
+ MAINNET: 'https://horizon.stellar.org',
34
+ TESTNET: 'https://horizon-testnet.stellar.org',
35
+ FUTURENET: 'https://horizon-futurenet.stellar.org'
36
+ };
37
+
38
+ // eslint-disable-next-line @nx/enforce-module-boundaries
39
+ function _assert_this_initialized(self) {
40
+ if (self === void 0) {
41
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
42
+ }
43
+ return self;
44
+ }
45
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
46
+ try {
47
+ var info = gen[key](arg);
48
+ var value = info.value;
49
+ } catch (error) {
50
+ reject(error);
51
+ return;
52
+ }
53
+ if (info.done) {
54
+ resolve(value);
55
+ } else {
56
+ Promise.resolve(value).then(_next, _throw);
57
+ }
58
+ }
59
+ function _async_to_generator(fn) {
60
+ return function() {
61
+ var self = this, args = arguments;
62
+ return new Promise(function(resolve, reject) {
63
+ var gen = fn.apply(self, args);
64
+ function _next(value) {
65
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
66
+ }
67
+ function _throw(err) {
68
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
69
+ }
70
+ _next(undefined);
71
+ });
72
+ };
73
+ }
74
+ function _call_super(_this, derived, args) {
75
+ derived = _get_prototype_of(derived);
76
+ return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
77
+ }
78
+ function _class_call_check(instance, Constructor) {
79
+ if (!(instance instanceof Constructor)) {
80
+ throw new TypeError("Cannot call a class as a function");
81
+ }
82
+ }
83
+ function _defineProperties(target, props) {
84
+ for(var i = 0; i < props.length; i++){
85
+ var descriptor = props[i];
86
+ descriptor.enumerable = descriptor.enumerable || false;
87
+ descriptor.configurable = true;
88
+ if ("value" in descriptor) descriptor.writable = true;
89
+ Object.defineProperty(target, descriptor.key, descriptor);
90
+ }
91
+ }
92
+ function _create_class(Constructor, protoProps, staticProps) {
93
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
94
+ return Constructor;
95
+ }
96
+ function _define_property(obj, key, value) {
97
+ if (key in obj) {
98
+ Object.defineProperty(obj, key, {
99
+ value: value,
100
+ enumerable: true,
101
+ configurable: true,
102
+ writable: true
103
+ });
104
+ } else {
105
+ obj[key] = value;
106
+ }
107
+ return obj;
108
+ }
109
+ function _get_prototype_of(o) {
110
+ _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
111
+ return o.__proto__ || Object.getPrototypeOf(o);
112
+ };
113
+ return _get_prototype_of(o);
114
+ }
115
+ function _inherits(subClass, superClass) {
116
+ if (typeof superClass !== "function" && superClass !== null) {
117
+ throw new TypeError("Super expression must either be null or a function");
118
+ }
119
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
120
+ constructor: {
121
+ value: subClass,
122
+ writable: true,
123
+ configurable: true
124
+ }
125
+ });
126
+ if (superClass) _set_prototype_of(subClass, superClass);
127
+ }
128
+ function _instanceof(left, right) {
129
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
130
+ return !!right[Symbol.hasInstance](left);
131
+ } else {
132
+ return left instanceof right;
133
+ }
134
+ }
135
+ function _object_spread(target) {
136
+ for(var i = 1; i < arguments.length; i++){
137
+ var source = arguments[i] != null ? arguments[i] : {};
138
+ var ownKeys = Object.keys(source);
139
+ if (typeof Object.getOwnPropertySymbols === "function") {
140
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
141
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
142
+ }));
143
+ }
144
+ ownKeys.forEach(function(key) {
145
+ _define_property(target, key, source[key]);
146
+ });
147
+ }
148
+ return target;
149
+ }
150
+ function ownKeys(object, enumerableOnly) {
151
+ var keys = Object.keys(object);
152
+ if (Object.getOwnPropertySymbols) {
153
+ var symbols = Object.getOwnPropertySymbols(object);
154
+ keys.push.apply(keys, symbols);
155
+ }
156
+ return keys;
157
+ }
158
+ function _object_spread_props(target, source) {
159
+ source = source != null ? source : {};
160
+ if (Object.getOwnPropertyDescriptors) {
161
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
162
+ } else {
163
+ ownKeys(Object(source)).forEach(function(key) {
164
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
165
+ });
166
+ }
167
+ return target;
168
+ }
169
+ function _possible_constructor_return(self, call) {
170
+ if (call && (_type_of(call) === "object" || typeof call === "function")) {
171
+ return call;
172
+ }
173
+ return _assert_this_initialized(self);
174
+ }
175
+ function _set_prototype_of(o, p) {
176
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
177
+ o.__proto__ = p;
178
+ return o;
179
+ };
180
+ return _set_prototype_of(o, p);
181
+ }
182
+ function _type_of(obj) {
183
+ "@swc/helpers - typeof";
184
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
185
+ }
186
+ function _is_native_reflect_construct() {
187
+ try {
188
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
189
+ } catch (_) {}
190
+ return (_is_native_reflect_construct = function() {
191
+ return !!result;
192
+ })();
193
+ }
194
+ function _ts_generator(thisArg, body) {
195
+ var f, y, t, g, _ = {
196
+ label: 0,
197
+ sent: function() {
198
+ if (t[0] & 1) throw t[1];
199
+ return t[1];
200
+ },
201
+ trys: [],
202
+ ops: []
203
+ };
204
+ return g = {
205
+ next: verb(0),
206
+ "throw": verb(1),
207
+ "return": verb(2)
208
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
209
+ return this;
210
+ }), g;
211
+ function verb(n) {
212
+ return function(v) {
213
+ return step([
214
+ n,
215
+ v
216
+ ]);
217
+ };
218
+ }
219
+ function step(op) {
220
+ if (f) throw new TypeError("Generator is already executing.");
221
+ while(_)try {
222
+ 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;
223
+ if (y = 0, t) op = [
224
+ op[0] & 2,
225
+ t.value
226
+ ];
227
+ switch(op[0]){
228
+ case 0:
229
+ case 1:
230
+ t = op;
231
+ break;
232
+ case 4:
233
+ _.label++;
234
+ return {
235
+ value: op[1],
236
+ done: false
237
+ };
238
+ case 5:
239
+ _.label++;
240
+ y = op[1];
241
+ op = [
242
+ 0
243
+ ];
244
+ continue;
245
+ case 7:
246
+ op = _.ops.pop();
247
+ _.trys.pop();
248
+ continue;
249
+ default:
250
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
251
+ _ = 0;
252
+ continue;
253
+ }
254
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
255
+ _.label = op[1];
256
+ break;
257
+ }
258
+ if (op[0] === 6 && _.label < t[1]) {
259
+ _.label = t[1];
260
+ t = op;
261
+ break;
262
+ }
263
+ if (t && _.label < t[2]) {
264
+ _.label = t[2];
265
+ _.ops.push(op);
266
+ break;
267
+ }
268
+ if (t[2]) _.ops.pop();
269
+ _.trys.pop();
270
+ continue;
271
+ }
272
+ op = body.call(thisArg, _);
273
+ } catch (e) {
274
+ op = [
275
+ 6,
276
+ e
277
+ ];
278
+ y = 0;
279
+ } finally{
280
+ f = t = 0;
281
+ }
282
+ if (op[0] & 5) throw op[1];
283
+ return {
284
+ value: op[0] ? op[1] : void 0,
285
+ done: true
286
+ };
287
+ }
288
+ }
289
+ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
290
+ _inherits(DynamicStellarWalletClient, DynamicWalletClient);
291
+ function DynamicStellarWalletClient(param, internalOptions) {
292
+ 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;
293
+ _class_call_check(this, DynamicStellarWalletClient);
294
+ var _this;
295
+ _this = _call_super(this, DynamicStellarWalletClient, [
296
+ {
297
+ environmentId: environmentId,
298
+ authToken: authToken,
299
+ baseApiUrl: baseApiUrl,
300
+ baseMPCRelayApiUrl: baseMPCRelayApiUrl,
301
+ storageKey: storageKey,
302
+ debug: debug,
303
+ featureFlags: featureFlags,
304
+ authMode: authMode,
305
+ sdkVersion: sdkVersion,
306
+ forwardMPCClient: forwardMPCClient
307
+ },
308
+ internalOptions
309
+ ]), _define_property(_this, "chainName", 'STELLAR');
310
+ return _this;
311
+ }
312
+ _create_class(DynamicStellarWalletClient, [
313
+ {
314
+ key: "createWalletAccount",
315
+ value: function createWalletAccount(param) {
316
+ var thresholdSignatureScheme = param.thresholdSignatureScheme, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, onError = param.onError, signedSessionId = param.signedSessionId;
317
+ var _this = this;
318
+ return _async_to_generator(function() {
319
+ var ceremonyCeremonyCompleteResolver, ceremonyCompletePromise, _ref, publicKeyHex, clientKeyShares, accountAddress, pubKeyBytes, error;
320
+ return _ts_generator(this, function(_state) {
321
+ switch(_state.label){
322
+ case 0:
323
+ _state.trys.push([
324
+ 0,
325
+ 5,
326
+ ,
327
+ 6
328
+ ]);
329
+ ceremonyCompletePromise = new Promise(function(resolve) {
330
+ ceremonyCeremonyCompleteResolver = resolve;
331
+ });
332
+ return [
333
+ 4,
334
+ _this.keyGen({
335
+ chainName: _this.chainName,
336
+ thresholdSignatureScheme: thresholdSignatureScheme,
337
+ onError: onError,
338
+ onCeremonyComplete: function(accountAddress, walletId) {
339
+ var chainConfig = getMPCChainConfig(_this.chainName);
340
+ _this.initializeWalletMapEntry({
341
+ accountAddress: accountAddress,
342
+ walletId: walletId,
343
+ chainName: _this.chainName,
344
+ thresholdSignatureScheme: thresholdSignatureScheme,
345
+ derivationPath: JSON.stringify(Object.fromEntries(chainConfig.derivationPath.map(function(value, index) {
346
+ return [
347
+ index,
348
+ value
349
+ ];
350
+ })))
351
+ });
352
+ ceremonyCeremonyCompleteResolver(undefined);
353
+ },
354
+ password: password,
355
+ signedSessionId: signedSessionId
356
+ })
357
+ ];
358
+ case 1:
359
+ _ref = _state.sent(), publicKeyHex = _ref.rawPublicKey, clientKeyShares = _ref.clientKeyShares;
360
+ // Wait for the ceremony to complete before proceeding
361
+ return [
362
+ 4,
363
+ ceremonyCompletePromise
364
+ ];
365
+ case 2:
366
+ _state.sent();
367
+ if (!publicKeyHex || !clientKeyShares) {
368
+ throw new Error(ERROR_KEYGEN_FAILED);
369
+ }
370
+ accountAddress = deriveStellarAddress({
371
+ publicKeyHex: publicKeyHex
372
+ });
373
+ // Update client key shares in wallet map
374
+ return [
375
+ 4,
376
+ _this.setClientKeySharesToStorage({
377
+ accountAddress: accountAddress,
378
+ clientKeyShares: clientKeyShares,
379
+ overwriteOrMerge: 'overwrite'
380
+ })
381
+ ];
382
+ case 3:
383
+ _state.sent();
384
+ return [
385
+ 4,
386
+ _this.storeEncryptedBackupByWalletWithRetry({
387
+ accountAddress: accountAddress,
388
+ clientKeyShares: clientKeyShares,
389
+ password: password,
390
+ signedSessionId: signedSessionId
391
+ })
392
+ ];
393
+ case 4:
394
+ _state.sent();
395
+ pubKeyBytes = Buffer.from(publicKeyHex, 'hex');
396
+ return [
397
+ 2,
398
+ {
399
+ accountAddress: accountAddress,
400
+ publicKeyHex: publicKeyHex,
401
+ rawPublicKey: new Uint8Array(pubKeyBytes)
402
+ }
403
+ ];
404
+ case 5:
405
+ error = _state.sent();
406
+ // Re-throw password mismatch errors without wrapping
407
+ if (_instanceof(error, Error) && error.message === ERROR_PASSWORD_MISMATCH) {
408
+ throw error;
409
+ }
410
+ _this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
411
+ throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
412
+ case 6:
413
+ return [
414
+ 2
415
+ ];
416
+ }
417
+ });
418
+ })();
419
+ }
420
+ },
421
+ {
422
+ key: "signMessage",
423
+ value: function signMessage(param) {
424
+ 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;
425
+ var _this = this;
426
+ return _async_to_generator(function() {
427
+ var resolvedContext, signatureEd25519, formattedSignature, error;
428
+ return _ts_generator(this, function(_state) {
429
+ switch(_state.label){
430
+ case 0:
431
+ if (!accountAddress) {
432
+ throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
433
+ }
434
+ return [
435
+ 4,
436
+ _this.verifyPassword({
437
+ accountAddress: accountAddress,
438
+ password: password,
439
+ walletOperation: WalletOperation.SIGN_MESSAGE,
440
+ signedSessionId: signedSessionId
441
+ })
442
+ ];
443
+ case 1:
444
+ _state.sent();
445
+ resolvedContext = _object_spread_props(_object_spread({}, context), {
446
+ stellarMessage: message
447
+ });
448
+ _state.label = 2;
449
+ case 2:
450
+ _state.trys.push([
451
+ 2,
452
+ 4,
453
+ ,
454
+ 5
455
+ ]);
456
+ return [
457
+ 4,
458
+ _this.sign({
459
+ message: message,
460
+ accountAddress: accountAddress,
461
+ chainName: _this.chainName,
462
+ password: password,
463
+ signedSessionId: signedSessionId,
464
+ mfaToken: mfaToken,
465
+ context: resolvedContext,
466
+ onError: onError
467
+ })
468
+ ];
469
+ case 3:
470
+ signatureEd25519 = _state.sent();
471
+ // Return signature as base64 encoded string (common format for Stellar)
472
+ formattedSignature = Buffer.from(signatureEd25519).toString('base64');
473
+ return [
474
+ 2,
475
+ formattedSignature
476
+ ];
477
+ case 4:
478
+ error = _state.sent();
479
+ _this.logger.error(ERROR_SIGN_MESSAGE, error);
480
+ throw new Error(ERROR_SIGN_MESSAGE);
481
+ case 5:
482
+ return [
483
+ 2
484
+ ];
485
+ }
486
+ });
487
+ })();
488
+ }
489
+ },
490
+ {
491
+ key: "signTransaction",
492
+ value: function signTransaction(param) {
493
+ 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;
494
+ var _this = this;
495
+ return _async_to_generator(function() {
496
+ var resolvedContext, signatureEd25519, formattedSignature, error;
497
+ return _ts_generator(this, function(_state) {
498
+ switch(_state.label){
499
+ case 0:
500
+ if (!senderAddress) {
501
+ throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
502
+ }
503
+ return [
504
+ 4,
505
+ _this.verifyPassword({
506
+ accountAddress: senderAddress,
507
+ password: password,
508
+ walletOperation: WalletOperation.SIGN_TRANSACTION,
509
+ signedSessionId: signedSessionId
510
+ })
511
+ ];
512
+ case 1:
513
+ _state.sent();
514
+ resolvedContext = _object_spread_props(_object_spread({}, context), {
515
+ stellarTransaction: {
516
+ serializedTransaction: transaction
517
+ }
518
+ });
519
+ _state.label = 2;
520
+ case 2:
521
+ _state.trys.push([
522
+ 2,
523
+ 4,
524
+ ,
525
+ 5
526
+ ]);
527
+ return [
528
+ 4,
529
+ _this.sign({
530
+ message: transaction,
531
+ accountAddress: senderAddress,
532
+ chainName: _this.chainName,
533
+ password: password,
534
+ signedSessionId: signedSessionId,
535
+ mfaToken: mfaToken,
536
+ context: resolvedContext,
537
+ onError: onError
538
+ })
539
+ ];
540
+ case 3:
541
+ signatureEd25519 = _state.sent();
542
+ // Return signature as base64 encoded string
543
+ formattedSignature = Buffer.from(signatureEd25519).toString('base64');
544
+ return [
545
+ 2,
546
+ formattedSignature
547
+ ];
548
+ case 4:
549
+ error = _state.sent();
550
+ _this.logger.error(ERROR_SIGN_TRANSACTION, error);
551
+ throw new Error(ERROR_SIGN_TRANSACTION);
552
+ case 5:
553
+ return [
554
+ 2
555
+ ];
556
+ }
557
+ });
558
+ })();
559
+ }
560
+ },
561
+ {
562
+ key: "exportPrivateKey",
563
+ value: function exportPrivateKey(param) {
564
+ var accountAddress = param.accountAddress, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, signedSessionId = param.signedSessionId, mfaToken = param.mfaToken, elevatedAccessToken = param.elevatedAccessToken;
565
+ var _this = this;
566
+ return _async_to_generator(function() {
567
+ var derivedPrivateKey, privateScalarHex, privateKeyBytes, stellarSecretKey, error;
568
+ return _ts_generator(this, function(_state) {
569
+ switch(_state.label){
570
+ case 0:
571
+ _state.trys.push([
572
+ 0,
573
+ 2,
574
+ ,
575
+ 3
576
+ ]);
577
+ return [
578
+ 4,
579
+ _this.exportKey({
580
+ accountAddress: accountAddress,
581
+ chainName: _this.chainName,
582
+ password: password,
583
+ signedSessionId: signedSessionId,
584
+ mfaToken: mfaToken,
585
+ elevatedAccessToken: elevatedAccessToken
586
+ })
587
+ ];
588
+ case 1:
589
+ derivedPrivateKey = _state.sent().derivedPrivateKey;
590
+ if (!derivedPrivateKey) {
591
+ throw new Error('Derived private key is undefined');
592
+ }
593
+ // Stellar private keys are 64 hex characters (32 bytes)
594
+ // Return the seed portion only (first 32 bytes)
595
+ privateScalarHex = derivedPrivateKey.slice(0, 64);
596
+ // Convert to Stellar secret key format (S...)
597
+ privateKeyBytes = Buffer.from(privateScalarHex, 'hex');
598
+ stellarSecretKey = StrKey.encodeEd25519SecretSeed(privateKeyBytes);
599
+ return [
600
+ 2,
601
+ stellarSecretKey
602
+ ];
603
+ case 2:
604
+ error = _state.sent();
605
+ _this.logger.error(ERROR_EXPORT_PRIVATE_KEY, error);
606
+ throw new Error(ERROR_EXPORT_PRIVATE_KEY);
607
+ case 3:
608
+ return [
609
+ 2
610
+ ];
611
+ }
612
+ });
613
+ })();
614
+ }
615
+ },
616
+ {
617
+ key: "importPrivateKey",
618
+ value: function importPrivateKey(param) {
619
+ var privateKey = param.privateKey, chainName = param.chainName, thresholdSignatureScheme = param.thresholdSignatureScheme, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, signedSessionId = param.signedSessionId, onError = param.onError, publicAddressCheck = param.publicAddressCheck, legacyWalletId = param.legacyWalletId;
620
+ var _this = this;
621
+ return _async_to_generator(function() {
622
+ var ceremonyCeremonyCompleteResolver, ceremonyCompletePromise, formattedPrivateKey, secretKeyBytes, publicKeyHex, derivedAddress, _ref, rawPublicKey, clientKeyShares, accountAddress, pubKeyBytes, error;
623
+ return _ts_generator(this, function(_state) {
624
+ switch(_state.label){
625
+ case 0:
626
+ _state.trys.push([
627
+ 0,
628
+ 5,
629
+ ,
630
+ 6
631
+ ]);
632
+ ceremonyCompletePromise = new Promise(function(resolve) {
633
+ ceremonyCeremonyCompleteResolver = resolve;
634
+ });
635
+ if (privateKey.startsWith('S')) {
636
+ // Stellar secret key format (S...)
637
+ secretKeyBytes = StrKey.decodeEd25519SecretSeed(privateKey);
638
+ formattedPrivateKey = Array.from(secretKeyBytes).map(function(b) {
639
+ return b.toString(16).padStart(2, '0');
640
+ }).join('');
641
+ } else if (privateKey.startsWith('0x')) {
642
+ formattedPrivateKey = privateKey.slice(2);
643
+ } else {
644
+ formattedPrivateKey = privateKey;
645
+ }
646
+ publicKeyHex = _this.getPublicKeyFromPrivateKey(formattedPrivateKey);
647
+ derivedAddress = deriveStellarAddress({
648
+ publicKeyHex: publicKeyHex
649
+ });
650
+ if (publicAddressCheck && derivedAddress !== publicAddressCheck) {
651
+ throw new Error("Public address mismatch: derived address ".concat(derivedAddress, " !== public address ").concat(publicAddressCheck));
652
+ }
653
+ return [
654
+ 4,
655
+ _this.importRawPrivateKey({
656
+ chainName: chainName,
657
+ thresholdSignatureScheme: thresholdSignatureScheme,
658
+ privateKey: formattedPrivateKey,
659
+ onCeremonyComplete: function(accountAddress, walletId) {
660
+ _this.initializeWalletMapEntry({
661
+ accountAddress: accountAddress,
662
+ walletId: walletId,
663
+ chainName: _this.chainName,
664
+ thresholdSignatureScheme: thresholdSignatureScheme
665
+ });
666
+ ceremonyCeremonyCompleteResolver(undefined);
667
+ },
668
+ onError: onError,
669
+ legacyWalletId: legacyWalletId,
670
+ password: password,
671
+ signedSessionId: signedSessionId
672
+ })
673
+ ];
674
+ case 1:
675
+ _ref = _state.sent(), rawPublicKey = _ref.rawPublicKey, clientKeyShares = _ref.clientKeyShares;
676
+ return [
677
+ 4,
678
+ ceremonyCompletePromise
679
+ ];
680
+ case 2:
681
+ _state.sent();
682
+ if (!rawPublicKey || !clientKeyShares) {
683
+ throw new Error(ERROR_IMPORT_PRIVATE_KEY);
684
+ }
685
+ accountAddress = deriveStellarAddress({
686
+ publicKeyHex: rawPublicKey
687
+ });
688
+ if (accountAddress !== derivedAddress) {
689
+ throw new Error("Public key mismatch: derived address ".concat(accountAddress, " !== expected ").concat(derivedAddress));
690
+ }
691
+ // Update client key shares in wallet map
692
+ return [
693
+ 4,
694
+ _this.setClientKeySharesToStorage({
695
+ accountAddress: accountAddress,
696
+ clientKeyShares: clientKeyShares,
697
+ overwriteOrMerge: 'overwrite'
698
+ })
699
+ ];
700
+ case 3:
701
+ _state.sent();
702
+ return [
703
+ 4,
704
+ _this.storeEncryptedBackupByWalletWithRetry({
705
+ accountAddress: accountAddress,
706
+ clientKeyShares: clientKeyShares,
707
+ password: password,
708
+ signedSessionId: signedSessionId
709
+ })
710
+ ];
711
+ case 4:
712
+ _state.sent();
713
+ pubKeyBytes = Buffer.from(rawPublicKey, 'hex');
714
+ return [
715
+ 2,
716
+ {
717
+ accountAddress: accountAddress,
718
+ publicKeyHex: rawPublicKey,
719
+ rawPublicKey: new Uint8Array(pubKeyBytes),
720
+ clientKeyShares: clientKeyShares
721
+ }
722
+ ];
723
+ case 5:
724
+ error = _state.sent();
725
+ _this.logger.error(ERROR_IMPORT_PRIVATE_KEY, error);
726
+ onError === null || onError === void 0 ? void 0 : onError(error);
727
+ throw new Error(ERROR_IMPORT_PRIVATE_KEY);
728
+ case 6:
729
+ return [
730
+ 2
731
+ ];
732
+ }
733
+ });
734
+ })();
735
+ }
736
+ },
737
+ {
738
+ key: "getPublicKeyFromPrivateKey",
739
+ value: function getPublicKeyFromPrivateKey(privateKeyHex) {
740
+ try {
741
+ var privateKeyBytes = Buffer.from(privateKeyHex, 'hex');
742
+ if (privateKeyBytes.length !== 32) {
743
+ throw new Error("Invalid private key length: ".concat(privateKeyBytes.length, ", expected 32"));
744
+ }
745
+ // Create keypair from raw secret seed
746
+ var keypair = Keypair.fromRawEd25519Seed(privateKeyBytes);
747
+ // Return the public key as hex
748
+ var raw = keypair.rawPublicKey();
749
+ return Array.from(raw).map(function(b) {
750
+ return b.toString(16).padStart(2, '0');
751
+ }).join('');
752
+ } catch (error) {
753
+ this.logger.error('Unable to derive public key from private key. Check private key format', _instanceof(error, Error) ? error.message : 'Unknown error');
754
+ throw error;
755
+ }
756
+ }
757
+ },
758
+ {
759
+ key: "getStellarWallets",
760
+ value: function getStellarWallets() {
761
+ var _this = this;
762
+ return _async_to_generator(function() {
763
+ var wallets, stellarWallets;
764
+ return _ts_generator(this, function(_state) {
765
+ switch(_state.label){
766
+ case 0:
767
+ return [
768
+ 4,
769
+ _this.getWallets()
770
+ ];
771
+ case 1:
772
+ wallets = _state.sent();
773
+ stellarWallets = wallets.filter(function(wallet) {
774
+ return wallet.chainName === 'stellar';
775
+ });
776
+ return [
777
+ 2,
778
+ stellarWallets
779
+ ];
780
+ }
781
+ });
782
+ })();
783
+ }
784
+ }
785
+ ]);
786
+ return DynamicStellarWalletClient;
787
+ }(DynamicWalletClient);
788
+
789
+ export { DynamicStellarWalletClient, STELLAR_HORIZON_URLS, STELLAR_NETWORK_PASSPHRASES };