@dynamic-labs-wallet/stellar 0.0.353 → 0.1.0-request-signing

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 CHANGED
@@ -1,5 +1,5 @@
1
- import { AuthMode, DynamicWalletClient, ERROR_PASSWORD_MISMATCH, getMPCChainConfig } from '@dynamic-labs-wallet/browser';
2
- import { StrKey, Keypair, TransactionBuilder } from '@stellar/stellar-sdk';
1
+ import { DynamicWalletClient, ERROR_PASSWORD_MISMATCH, ERROR_MULTIPLE_WALLETS_PER_CHAIN, getMPCChainConfig, SigningConsentRequiredError } from '@dynamic-labs-wallet/browser';
2
+ import { StrKey, Keypair, TransactionBuilder, xdr, Account, Memo, Operation, Asset } from '@stellar/stellar-sdk';
3
3
  import * as sdkApiCore from '@dynamic-labs/sdk-api-core';
4
4
  export { sdkApiCore };
5
5
 
@@ -45,9 +45,12 @@ var STELLAR_NETWORK_PASSPHRASES = {
45
45
  '3': STELLAR_NETWORK_PASSPHRASES.FUTURENET
46
46
  };
47
47
  /**
48
- * Gets the network passphrase from a numeric chainId, defaults to mainnet
48
+ * Gets the network passphrase from a numeric chainId, throws for any chainId other than '1', '2' or '3'
49
49
  */ var getNetworkPassphraseFromChainId = function(chainId) {
50
- return STELLAR_CHAIN_ID_TO_PASSPHRASE[chainId] || STELLAR_NETWORK_PASSPHRASES.MAINNET;
50
+ if (!Object.keys(STELLAR_CHAIN_ID_TO_PASSPHRASE).includes(chainId)) {
51
+ throw new Error('Unsupported Stellar chainId');
52
+ }
53
+ return STELLAR_CHAIN_ID_TO_PASSPHRASE[chainId];
51
54
  };
52
55
 
53
56
  function _assert_this_initialized(self) {
@@ -139,7 +142,7 @@ function _inherits(subClass, superClass) {
139
142
  });
140
143
  if (superClass) _set_prototype_of(subClass, superClass);
141
144
  }
142
- function _instanceof(left, right) {
145
+ function _instanceof$1(left, right) {
143
146
  if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
144
147
  return !!right[Symbol.hasInstance](left);
145
148
  } else {
@@ -181,7 +184,7 @@ function _object_spread_props(target, source) {
181
184
  return target;
182
185
  }
183
186
  function _possible_constructor_return(self, call) {
184
- if (call && (_type_of(call) === "object" || typeof call === "function")) {
187
+ if (call && (_type_of$1(call) === "object" || typeof call === "function")) {
185
188
  return call;
186
189
  }
187
190
  return _assert_this_initialized(self);
@@ -193,7 +196,7 @@ function _set_prototype_of(o, p) {
193
196
  };
194
197
  return _set_prototype_of(o, p);
195
198
  }
196
- function _type_of(obj) {
199
+ function _type_of$1(obj) {
197
200
  "@swc/helpers - typeof";
198
201
  return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
199
202
  }
@@ -302,33 +305,17 @@ function _ts_generator(thisArg, body) {
302
305
  }
303
306
  var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
304
307
  _inherits(DynamicStellarWalletClient, DynamicWalletClient);
305
- function DynamicStellarWalletClient(param, internalOptions) {
306
- 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, logger = param.logger;
308
+ function DynamicStellarWalletClient() {
307
309
  _class_call_check(this, DynamicStellarWalletClient);
308
310
  var _this;
309
- _this = _call_super(this, DynamicStellarWalletClient, [
310
- {
311
- environmentId: environmentId,
312
- authToken: authToken,
313
- baseApiUrl: baseApiUrl,
314
- baseMPCRelayApiUrl: baseMPCRelayApiUrl,
315
- storageKey: storageKey,
316
- debug: debug,
317
- featureFlags: featureFlags,
318
- authMode: authMode,
319
- sdkVersion: sdkVersion,
320
- forwardMPCClient: forwardMPCClient,
321
- logger: logger
322
- },
323
- internalOptions
324
- ]), _define_property(_this, "chainName", 'STELLAR');
311
+ _this = _call_super(this, DynamicStellarWalletClient, arguments), _define_property(_this, "chainName", 'STELLAR');
325
312
  return _this;
326
313
  }
327
314
  _create_class(DynamicStellarWalletClient, [
328
315
  {
329
316
  key: "createWalletAccount",
330
317
  value: function createWalletAccount(param) {
331
- var thresholdSignatureScheme = param.thresholdSignatureScheme, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, onError = param.onError, signedSessionId = param.signedSessionId;
318
+ var thresholdSignatureScheme = param.thresholdSignatureScheme, _param_password = param.password, password = _param_password === void 0 ? undefined : _param_password, onError = param.onError, signedSessionId = param.signedSessionId, businessAccountId = param.businessAccountId;
332
319
  var _this = this;
333
320
  return _async_to_generator(function() {
334
321
  var ceremonyCeremonyCompleteResolver, ceremonyCompletePromise, _ref, publicKeyHex, clientKeyShares, accountAddress, pubKeyBytes, error;
@@ -348,9 +335,11 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
348
335
  4,
349
336
  _this.keyGen({
350
337
  chainName: _this.chainName,
338
+ businessAccountId: businessAccountId,
351
339
  thresholdSignatureScheme: thresholdSignatureScheme,
352
340
  onError: onError,
353
- onCeremonyComplete: function(accountAddress, walletId) {
341
+ onCeremonyComplete: function(param) {
342
+ var accountAddress = param.accountAddress, walletId = param.walletId, shareSetId = param.shareSetId;
354
343
  var chainConfig = getMPCChainConfig(_this.chainName);
355
344
  _this.initializeWalletMapEntry({
356
345
  accountAddress: accountAddress,
@@ -362,7 +351,8 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
362
351
  index,
363
352
  value
364
353
  ];
365
- })))
354
+ }))),
355
+ shareSetId: shareSetId
366
356
  });
367
357
  ceremonyCeremonyCompleteResolver(undefined);
368
358
  },
@@ -401,7 +391,8 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
401
391
  accountAddress: accountAddress,
402
392
  clientKeyShares: clientKeyShares,
403
393
  password: password,
404
- signedSessionId: signedSessionId
394
+ signedSessionId: signedSessionId,
395
+ businessAccountId: businessAccountId
405
396
  })
406
397
  ];
407
398
  case 4:
@@ -417,12 +408,14 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
417
408
  ];
418
409
  case 5:
419
410
  error = _state.sent();
420
- // Re-throw password mismatch errors without wrapping
421
- if (_instanceof(error, Error) && error.message === ERROR_PASSWORD_MISMATCH) {
411
+ // Re-throw known business-rule rejections without wrapping
412
+ if (_instanceof$1(error, Error) && (error.message === ERROR_PASSWORD_MISMATCH || error.message === ERROR_MULTIPLE_WALLETS_PER_CHAIN)) {
422
413
  throw error;
423
414
  }
424
415
  _this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
425
- throw new Error(ERROR_CREATE_WALLET_ACCOUNT);
416
+ throw new Error(ERROR_CREATE_WALLET_ACCOUNT, {
417
+ cause: error
418
+ });
426
419
  case 6:
427
420
  return [
428
421
  2
@@ -435,7 +428,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
435
428
  {
436
429
  key: "signMessage",
437
430
  value: function signMessage(param) {
438
- 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, elevatedAccessToken = param.elevatedAccessToken, context = param.context, onError = param.onError;
431
+ 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, elevatedAccessToken = param.elevatedAccessToken, context = param.context, approvalQuorum = param.approvalQuorum, onError = param.onError;
439
432
  var _this = this;
440
433
  return _async_to_generator(function() {
441
434
  var resolvedContext, signatureEd25519, formattedSignature, error;
@@ -477,6 +470,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
477
470
  mfaToken: mfaToken,
478
471
  elevatedAccessToken: elevatedAccessToken,
479
472
  context: resolvedContext,
473
+ approvalQuorum: approvalQuorum,
480
474
  onError: onError
481
475
  })
482
476
  ];
@@ -490,8 +484,10 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
490
484
  ];
491
485
  case 4:
492
486
  error = _state.sent();
493
- _this.logger.error(ERROR_SIGN_MESSAGE, error);
494
- throw new Error(ERROR_SIGN_MESSAGE);
487
+ _this.logOperationFailure(ERROR_SIGN_MESSAGE, error);
488
+ throw new Error(ERROR_SIGN_MESSAGE, {
489
+ cause: error
490
+ });
495
491
  case 5:
496
492
  return [
497
493
  2
@@ -504,7 +500,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
504
500
  {
505
501
  key: "signTransaction",
506
502
  value: function signTransaction(param) {
507
- 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, elevatedAccessToken = param.elevatedAccessToken, chainId = param.chainId, context = param.context, onError = param.onError;
503
+ 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, elevatedAccessToken = param.elevatedAccessToken, chainId = param.chainId, context = param.context, approvalQuorum = param.approvalQuorum, onError = param.onError;
508
504
  var _this = this;
509
505
  return _async_to_generator(function() {
510
506
  var networkPassphrase, stellarTransaction, transactionHash, resolvedContext, signatureEd25519, formattedSignature, error;
@@ -557,6 +553,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
557
553
  mfaToken: mfaToken,
558
554
  elevatedAccessToken: elevatedAccessToken,
559
555
  context: resolvedContext,
556
+ approvalQuorum: approvalQuorum,
560
557
  onError: onError
561
558
  })
562
559
  ];
@@ -570,8 +567,10 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
570
567
  ];
571
568
  case 4:
572
569
  error = _state.sent();
573
- _this.logger.error(ERROR_SIGN_TRANSACTION, error);
574
- throw new Error(ERROR_SIGN_TRANSACTION);
570
+ _this.logOperationFailure(ERROR_SIGN_TRANSACTION, error);
571
+ throw new Error(ERROR_SIGN_TRANSACTION, {
572
+ cause: error
573
+ });
575
574
  case 5:
576
575
  return [
577
576
  2
@@ -584,7 +583,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
584
583
  {
585
584
  key: "exportPrivateKey",
586
585
  value: function exportPrivateKey(param) {
587
- 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;
586
+ 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, approvalQuorum = param.approvalQuorum, proposalId = param.proposalId;
588
587
  var _this = this;
589
588
  return _async_to_generator(function() {
590
589
  var derivedPrivateKey, privateScalarHex, privateKeyBytes, stellarSecretKey, error;
@@ -605,7 +604,9 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
605
604
  password: password,
606
605
  signedSessionId: signedSessionId,
607
606
  mfaToken: mfaToken,
608
- elevatedAccessToken: elevatedAccessToken
607
+ elevatedAccessToken: elevatedAccessToken,
608
+ approvalQuorum: approvalQuorum,
609
+ proposalId: proposalId
609
610
  })
610
611
  ];
611
612
  case 1:
@@ -625,6 +626,9 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
625
626
  ];
626
627
  case 2:
627
628
  error = _state.sent();
629
+ // Not a failure: the export needs approvals, and the caller needs the
630
+ // reason and proposalId to continue.
631
+ if (_instanceof$1(error, SigningConsentRequiredError)) throw error;
628
632
  _this.logger.error(ERROR_EXPORT_PRIVATE_KEY, error);
629
633
  throw new Error(ERROR_EXPORT_PRIVATE_KEY);
630
634
  case 3:
@@ -679,12 +683,14 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
679
683
  chainName: chainName,
680
684
  thresholdSignatureScheme: thresholdSignatureScheme,
681
685
  privateKey: formattedPrivateKey,
682
- onCeremonyComplete: function(accountAddress, walletId) {
686
+ onCeremonyComplete: function(param) {
687
+ var accountAddress = param.accountAddress, walletId = param.walletId, shareSetId = param.shareSetId;
683
688
  _this.initializeWalletMapEntry({
684
689
  accountAddress: accountAddress,
685
690
  walletId: walletId,
686
691
  chainName: _this.chainName,
687
- thresholdSignatureScheme: thresholdSignatureScheme
692
+ thresholdSignatureScheme: thresholdSignatureScheme,
693
+ shareSetId: shareSetId
688
694
  });
689
695
  ceremonyCeremonyCompleteResolver(undefined);
690
696
  },
@@ -772,7 +778,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
772
778
  return b.toString(16).padStart(2, '0');
773
779
  }).join('');
774
780
  } catch (error) {
775
- this.logger.error('Unable to derive public key from private key. Check private key format', _instanceof(error, Error) ? error.message : 'Unknown error');
781
+ this.logger.error('Unable to derive public key from private key. Check private key format', _instanceof$1(error, Error) ? error.message : 'Unknown error');
776
782
  throw error;
777
783
  }
778
784
  }
@@ -808,4 +814,408 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
808
814
  return DynamicStellarWalletClient;
809
815
  }(DynamicWalletClient);
810
816
 
811
- export { DynamicStellarWalletClient, STELLAR_HORIZON_URLS, STELLAR_NETWORK_PASSPHRASES };
817
+ function _instanceof(left, right) {
818
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
819
+ return !!right[Symbol.hasInstance](left);
820
+ } else {
821
+ return left instanceof right;
822
+ }
823
+ }
824
+ var MAX_OPERATIONS = 100;
825
+ var MAX_PATH_ASSETS = 5;
826
+ var MAX_MEMO_TEXT_BYTES = 28;
827
+ var MAX_FEE = 4294967295n;
828
+ var MAX_SEQUENCE = 9223372036854775806n;
829
+ var MAX_TIME = 18446744073709551615n;
830
+ var DECIMAL = /^(0|[1-9]\d*)$/;
831
+ var HEX_32_BYTES = /^[0-9a-f]{64}$/;
832
+ var assertStellarChainId = function(chainId) {
833
+ if (chainId !== '1' && chainId !== '2' && chainId !== '3') {
834
+ throw new Error('Stellar signing intent requires a network chainId of 1, 2 or 3');
835
+ }
836
+ return chainId;
837
+ };
838
+ var isAccount = function(address) {
839
+ return StrKey.isValidEd25519PublicKey(address);
840
+ };
841
+ var assertAccount = function(address, message) {
842
+ if (!isAccount(address)) throw new Error(message);
843
+ return address;
844
+ };
845
+ var describeAsset = function(asset) {
846
+ if (!_instanceof(asset, Asset)) throw new Error('Stellar signing intent does not support liquidity pool assets');
847
+ return asset.isNative() ? 'native' : "".concat(asset.getCode(), ":").concat(asset.getIssuer());
848
+ };
849
+ var toAsset = function(asset) {
850
+ if (asset === 'native') return Asset.native();
851
+ var separator = asset.indexOf(':');
852
+ return new Asset(asset.slice(0, separator), asset.slice(separator + 1));
853
+ };
854
+ var toBytes = function(value) {
855
+ if (value === null) throw new Error('Stellar signing intent does not support this memo type');
856
+ return typeof value === 'string' ? new TextEncoder().encode(value) : Uint8Array.from(value);
857
+ };
858
+ var toHex = function(bytes) {
859
+ return Array.from(bytes, function(byte) {
860
+ return byte.toString(16).padStart(2, '0');
861
+ }).join('');
862
+ };
863
+ var describeMemo = function(memo) {
864
+ switch(memo.type){
865
+ case 'none':
866
+ return {
867
+ type: 'none'
868
+ };
869
+ case 'id':
870
+ return {
871
+ type: 'id',
872
+ value: String(memo.value)
873
+ };
874
+ case 'text':
875
+ {
876
+ var bytes = toBytes(memo.value);
877
+ var value;
878
+ try {
879
+ value = new TextDecoder('utf-8', {
880
+ fatal: true,
881
+ ignoreBOM: true
882
+ }).decode(bytes);
883
+ } catch (e) {
884
+ throw new Error('Stellar signing intent requires a UTF-8 text memo');
885
+ }
886
+ if (bytes.length > MAX_MEMO_TEXT_BYTES) throw new Error('Stellar text memo is too long');
887
+ return {
888
+ type: 'text',
889
+ value: value
890
+ };
891
+ }
892
+ case 'hash':
893
+ case 'return':
894
+ return {
895
+ type: memo.type,
896
+ value: toHex(toBytes(memo.value))
897
+ };
898
+ default:
899
+ throw new Error('Stellar signing intent does not support this memo type');
900
+ }
901
+ };
902
+ var describePath = function(path) {
903
+ if (path.length > MAX_PATH_ASSETS) throw new Error('Stellar path payment path is too long');
904
+ return path.map(describeAsset);
905
+ };
906
+ var describeOperation = function(operation) {
907
+ if (operation.source !== undefined) throw new Error('Stellar signing intent does not support operation sources');
908
+ switch(operation.type){
909
+ case 'payment':
910
+ return {
911
+ type: 'payment',
912
+ destination: assertAccount(operation.destination, 'Stellar signing intent does not support muxed accounts'),
913
+ asset: describeAsset(operation.asset),
914
+ amount: operation.amount
915
+ };
916
+ case 'pathPaymentStrictSend':
917
+ return {
918
+ type: 'pathPaymentStrictSend',
919
+ destination: assertAccount(operation.destination, 'Stellar signing intent does not support muxed accounts'),
920
+ sendAsset: describeAsset(operation.sendAsset),
921
+ sendAmount: operation.sendAmount,
922
+ destAsset: describeAsset(operation.destAsset),
923
+ destMin: operation.destMin,
924
+ path: describePath(operation.path)
925
+ };
926
+ case 'pathPaymentStrictReceive':
927
+ return {
928
+ type: 'pathPaymentStrictReceive',
929
+ destination: assertAccount(operation.destination, 'Stellar signing intent does not support muxed accounts'),
930
+ sendAsset: describeAsset(operation.sendAsset),
931
+ sendMax: operation.sendMax,
932
+ destAsset: describeAsset(operation.destAsset),
933
+ destAmount: operation.destAmount,
934
+ path: describePath(operation.path)
935
+ };
936
+ case 'createAccount':
937
+ return {
938
+ type: 'createAccount',
939
+ destination: assertAccount(operation.destination, 'Stellar signing intent does not support muxed accounts'),
940
+ startingBalance: operation.startingBalance
941
+ };
942
+ case 'changeTrust':
943
+ return {
944
+ type: 'changeTrust',
945
+ asset: describeAsset(operation.line),
946
+ limit: operation.limit
947
+ };
948
+ default:
949
+ throw new Error("Stellar signing intent does not support the ".concat(operation.type, " operation"));
950
+ }
951
+ };
952
+ var assertClassicEnvelope = function(envelope) {
953
+ if (envelope.switch().name !== 'envelopeTypeTx') {
954
+ throw new Error('Stellar signing intent supports only v1 transaction envelopes, not fee-bump envelopes');
955
+ }
956
+ var tx = envelope.v1().tx();
957
+ if (tx.cond().switch().name !== 'precondTime') {
958
+ throw new Error('Stellar signing intent supports only time-bound preconditions');
959
+ }
960
+ if (tx.ext().switch() !== 0) throw new Error('Stellar signing intent does not support Soroban transactions');
961
+ return envelope.v1();
962
+ };
963
+ /**
964
+ * Describes a classic Stellar transaction envelope for a signing-quorum intent.
965
+ * The sequence number and time bounds are not part of the description: they are
966
+ * refreshed when the approved intent is resumed.
967
+ */ var parseStellarTransaction = function(param) {
968
+ var transaction = param.transaction, chainId = param.chainId;
969
+ var envelope;
970
+ try {
971
+ envelope = xdr.TransactionEnvelope.fromXDR(transaction, 'base64');
972
+ } catch (e) {
973
+ throw new Error('Stellar signing intent requires a base64 XDR transaction envelope');
974
+ }
975
+ if (envelope.toXDR('base64') !== transaction) {
976
+ throw new Error('Stellar signing intent requires a canonical XDR transaction envelope');
977
+ }
978
+ assertClassicEnvelope(envelope);
979
+ var parsed = TransactionBuilder.fromXDR(transaction, getNetworkPassphraseFromChainId(chainId));
980
+ if (!isAccount(parsed.source)) throw new Error('Stellar signing intent does not support muxed accounts');
981
+ if (parsed.operations.length === 0 || parsed.operations.length > MAX_OPERATIONS) {
982
+ throw new Error('Stellar signing intent requires between 1 and 100 operations');
983
+ }
984
+ return {
985
+ kind: 'transaction',
986
+ chainName: 'STELLAR',
987
+ chainId: chainId,
988
+ source: parsed.source,
989
+ fee: parsed.fee,
990
+ memo: describeMemo(parsed.memo),
991
+ operations: parsed.operations.map(describeOperation)
992
+ };
993
+ };
994
+ var buildMemo = function(memo) {
995
+ switch(memo.type){
996
+ case 'none':
997
+ return Memo.none();
998
+ case 'text':
999
+ return Memo.text(memo.value);
1000
+ case 'id':
1001
+ return Memo.id(memo.value);
1002
+ case 'hash':
1003
+ case 'return':
1004
+ if (!HEX_32_BYTES.test(memo.value)) throw new Error('Stored Stellar transaction intent is malformed');
1005
+ return memo.type === 'hash' ? Memo.hash(memo.value) : Memo.return(memo.value);
1006
+ default:
1007
+ throw new Error('Stored Stellar transaction intent is malformed');
1008
+ }
1009
+ };
1010
+ var addOperation = function(builder, operation) {
1011
+ switch(operation.type){
1012
+ case 'payment':
1013
+ builder.addOperation(Operation.payment({
1014
+ destination: operation.destination,
1015
+ asset: toAsset(operation.asset),
1016
+ amount: operation.amount
1017
+ }));
1018
+ return;
1019
+ case 'pathPaymentStrictSend':
1020
+ builder.addOperation(Operation.pathPaymentStrictSend({
1021
+ destination: operation.destination,
1022
+ sendAsset: toAsset(operation.sendAsset),
1023
+ sendAmount: operation.sendAmount,
1024
+ destAsset: toAsset(operation.destAsset),
1025
+ destMin: operation.destMin,
1026
+ path: operation.path.map(toAsset)
1027
+ }));
1028
+ return;
1029
+ case 'pathPaymentStrictReceive':
1030
+ builder.addOperation(Operation.pathPaymentStrictReceive({
1031
+ destination: operation.destination,
1032
+ sendAsset: toAsset(operation.sendAsset),
1033
+ sendMax: operation.sendMax,
1034
+ destAsset: toAsset(operation.destAsset),
1035
+ destAmount: operation.destAmount,
1036
+ path: operation.path.map(toAsset)
1037
+ }));
1038
+ return;
1039
+ case 'createAccount':
1040
+ builder.addOperation(Operation.createAccount({
1041
+ destination: operation.destination,
1042
+ startingBalance: operation.startingBalance
1043
+ }));
1044
+ return;
1045
+ case 'changeTrust':
1046
+ builder.addOperation(Operation.changeTrust({
1047
+ asset: toAsset(operation.asset),
1048
+ limit: operation.limit
1049
+ }));
1050
+ return;
1051
+ default:
1052
+ throw new Error('Stored Stellar transaction intent is malformed');
1053
+ }
1054
+ };
1055
+ var decimalWithin = function(value, max) {
1056
+ if (typeof value !== 'string' || !DECIMAL.test(value)) return undefined;
1057
+ var parsed = BigInt(value);
1058
+ return parsed <= max ? parsed : undefined;
1059
+ };
1060
+ var parseStellarTransactionLifetime = function(lifetimeValue) {
1061
+ var parsed;
1062
+ try {
1063
+ parsed = lifetimeValue ? JSON.parse(lifetimeValue) : undefined;
1064
+ } catch (e) {
1065
+ parsed = undefined;
1066
+ }
1067
+ var _ref = parsed !== null && parsed !== void 0 ? parsed : {}, sequence = _ref.sequence, minTime = _ref.minTime, maxTime = _ref.maxTime;
1068
+ var parsedMinTime = decimalWithin(minTime, MAX_TIME);
1069
+ var parsedMaxTime = decimalWithin(maxTime, MAX_TIME);
1070
+ if (decimalWithin(sequence, MAX_SEQUENCE) === undefined || parsedMinTime === undefined || parsedMaxTime === undefined || parsedMaxTime === 0n || parsedMinTime > parsedMaxTime) {
1071
+ throw new Error('Stellar transaction intent requires an account sequence and time bounds');
1072
+ }
1073
+ return {
1074
+ sequence: sequence,
1075
+ minTime: minTime,
1076
+ maxTime: maxTime
1077
+ };
1078
+ };
1079
+ /**
1080
+ * Rebuilds the envelope of an approved Stellar transaction operation. The
1081
+ * operation carries the total fee, but the builder prices every operation at a
1082
+ * base fee, so a total that is not a multiple of the operation count is written
1083
+ * onto the envelope after the build.
1084
+ */ var buildStellarTransaction = function(param) {
1085
+ var operation = param.operation, lifetime = param.lifetime;
1086
+ var totalFee = decimalWithin(operation.fee, MAX_FEE);
1087
+ var count = BigInt(operation.operations.length);
1088
+ if (totalFee === undefined || count === 0n || count > BigInt(MAX_OPERATIONS) || !isAccount(operation.source)) {
1089
+ throw new Error('Stored Stellar transaction intent is malformed');
1090
+ }
1091
+ var builder = new TransactionBuilder(new Account(operation.source, lifetime.sequence), {
1092
+ fee: (totalFee / count).toString(),
1093
+ networkPassphrase: getNetworkPassphraseFromChainId(assertStellarChainId(operation.chainId)),
1094
+ memo: buildMemo(operation.memo),
1095
+ timebounds: {
1096
+ minTime: lifetime.minTime,
1097
+ maxTime: lifetime.maxTime
1098
+ }
1099
+ });
1100
+ operation.operations.forEach(function(stellarOperation) {
1101
+ return addOperation(builder, stellarOperation);
1102
+ });
1103
+ var envelope = builder.build().toEnvelope();
1104
+ envelope.v1().tx().fee(Number(totalFee));
1105
+ return envelope.toXDR('base64');
1106
+ };
1107
+
1108
+ function _array_like_to_array(arr, len) {
1109
+ if (len == null || len > arr.length) len = arr.length;
1110
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
1111
+ return arr2;
1112
+ }
1113
+ function _array_with_holes(arr) {
1114
+ if (Array.isArray(arr)) return arr;
1115
+ }
1116
+ function _iterable_to_array_limit(arr, i) {
1117
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
1118
+ if (_i == null) return;
1119
+ var _arr = [];
1120
+ var _n = true;
1121
+ var _d = false;
1122
+ var _s, _e;
1123
+ try {
1124
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
1125
+ _arr.push(_s.value);
1126
+ if (i && _arr.length === i) break;
1127
+ }
1128
+ } catch (err) {
1129
+ _d = true;
1130
+ _e = err;
1131
+ } finally{
1132
+ try {
1133
+ if (!_n && _i["return"] != null) _i["return"]();
1134
+ } finally{
1135
+ if (_d) throw _e;
1136
+ }
1137
+ }
1138
+ return _arr;
1139
+ }
1140
+ function _non_iterable_rest() {
1141
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1142
+ }
1143
+ function _sliced_to_array(arr, i) {
1144
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
1145
+ }
1146
+ function _type_of(obj) {
1147
+ "@swc/helpers - typeof";
1148
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
1149
+ }
1150
+ function _unsupported_iterable_to_array(o, minLen) {
1151
+ if (!o) return;
1152
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
1153
+ var n = Object.prototype.toString.call(o).slice(8, -1);
1154
+ if (n === "Object" && o.constructor) n = o.constructor.name;
1155
+ if (n === "Map" || n === "Set") return Array.from(n);
1156
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
1157
+ }
1158
+ /**
1159
+ * Describes a Stellar transaction for a signing-quorum intent from its XDR
1160
+ * envelope. The sequence number and time bounds are not part of the
1161
+ * description: they are refreshed when the approved intent is resumed.
1162
+ */ var prepareStellarTransactionSigningOperation = function(param) {
1163
+ var transaction = param.transaction, accountAddress = param.accountAddress, chainId = param.chainId;
1164
+ var operation = parseStellarTransaction({
1165
+ transaction: transaction,
1166
+ chainId: assertStellarChainId(chainId)
1167
+ });
1168
+ if (operation.source !== accountAddress) throw new Error('Stellar transaction source must be the signing account');
1169
+ return {
1170
+ operation: operation,
1171
+ lifetime: {
1172
+ refreshable: 'expiration'
1173
+ }
1174
+ };
1175
+ };
1176
+ var byKey = function(param, param1) {
1177
+ var _param = _sliced_to_array(param, 1), a = _param[0], _param1 = _sliced_to_array(param1, 1), b = _param1[0];
1178
+ if (a < b) return -1;
1179
+ if (a > b) return 1;
1180
+ return 0;
1181
+ };
1182
+ var withSortedKeys = function(_key, nested) {
1183
+ if (!nested || (typeof nested === "undefined" ? "undefined" : _type_of(nested)) !== 'object' || Array.isArray(nested)) return nested;
1184
+ return Object.fromEntries(Object.entries(nested).sort(byKey));
1185
+ };
1186
+ var canonicalJson = function(value) {
1187
+ return JSON.stringify(value, withSortedKeys);
1188
+ };
1189
+ var signedStellarOperation = function(intent) {
1190
+ var _intent_lifetime;
1191
+ var operation = intent.operation;
1192
+ if (operation.kind !== 'transaction' || operation.chainName !== 'STELLAR' || ((_intent_lifetime = intent.lifetime) === null || _intent_lifetime === void 0 ? void 0 : _intent_lifetime.refreshable) !== 'expiration') {
1193
+ throw new Error('Stored signing intent is not a Stellar transaction');
1194
+ }
1195
+ return operation;
1196
+ };
1197
+ /**
1198
+ * Rebuilds the XDR envelope of an approved Stellar transaction intent. The
1199
+ * signed intent carries no sequence number or time bounds, so the caller
1200
+ * supplies them as `lifetimeValue`, a JSON object with the account's current
1201
+ * `sequence` as Horizon returns it (the builder increments it) and `minTime`
1202
+ * and `maxTime` in seconds, all as decimal strings.
1203
+ */ var resumeStellarTransactionSigningIntent = function(param) {
1204
+ var intent = param.intent, lifetimeValue = param.lifetimeValue;
1205
+ var operation = signedStellarOperation(intent);
1206
+ var lifetime = parseStellarTransactionLifetime(lifetimeValue);
1207
+ var transaction = buildStellarTransaction({
1208
+ operation: operation,
1209
+ lifetime: lifetime
1210
+ });
1211
+ var rebuilt = parseStellarTransaction({
1212
+ transaction: transaction,
1213
+ chainId: assertStellarChainId(operation.chainId)
1214
+ });
1215
+ if (canonicalJson(rebuilt) !== canonicalJson(operation)) {
1216
+ throw new Error('Stored Stellar transaction intent is malformed');
1217
+ }
1218
+ return transaction;
1219
+ };
1220
+
1221
+ export { DynamicStellarWalletClient, STELLAR_HORIZON_URLS, STELLAR_NETWORK_PASSPHRASES, prepareStellarTransactionSigningOperation, resumeStellarTransactionSigningIntent };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@dynamic-labs-wallet/stellar",
3
- "version": "0.0.353",
3
+ "version": "0.1.0-request-signing",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "dependencies": {
7
- "@dynamic-labs-wallet/browser": "0.0.353",
7
+ "@dynamic-labs-wallet/browser": "0.1.0-request-signing",
8
8
  "@stellar/stellar-sdk": "^12.3.0",
9
- "@dynamic-labs/sdk-api-core": "^0.0.984"
9
+ "@dynamic-labs/sdk-api-core": "0.193.0"
10
10
  },
11
11
  "publishConfig": {
12
12
  "access": "public"