@dynamic-labs-wallet/stellar 0.0.354 → 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.cjs +449 -39
- package/index.esm.js +450 -42
- package/package.json +3 -3
- package/src/client/client.d.ts +12 -21
- package/src/client/client.d.ts.map +1 -1
- package/src/index.d.ts +1 -0
- package/src/index.d.ts.map +1 -1
- package/src/signingIntent.d.ts +23 -0
- package/src/signingIntent.d.ts.map +1 -0
- package/src/stellarXdr.d.ts +29 -0
- package/src/stellarXdr.d.ts.map +1 -0
- package/src/utils/getNetworkPassphrase/getNetworkPassphrase.d.ts +1 -1
- package/src/utils/getNetworkPassphrase/getNetworkPassphrase.d.ts.map +1 -1
package/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
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,
|
|
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
|
-
|
|
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(
|
|
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(
|
|
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,
|
|
@@ -402,7 +391,8 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
402
391
|
accountAddress: accountAddress,
|
|
403
392
|
clientKeyShares: clientKeyShares,
|
|
404
393
|
password: password,
|
|
405
|
-
signedSessionId: signedSessionId
|
|
394
|
+
signedSessionId: signedSessionId,
|
|
395
|
+
businessAccountId: businessAccountId
|
|
406
396
|
})
|
|
407
397
|
];
|
|
408
398
|
case 4:
|
|
@@ -418,12 +408,14 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
418
408
|
];
|
|
419
409
|
case 5:
|
|
420
410
|
error = _state.sent();
|
|
421
|
-
// Re-throw
|
|
422
|
-
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)) {
|
|
423
413
|
throw error;
|
|
424
414
|
}
|
|
425
415
|
_this.logger.error(ERROR_CREATE_WALLET_ACCOUNT, error);
|
|
426
|
-
throw new Error(ERROR_CREATE_WALLET_ACCOUNT
|
|
416
|
+
throw new Error(ERROR_CREATE_WALLET_ACCOUNT, {
|
|
417
|
+
cause: error
|
|
418
|
+
});
|
|
427
419
|
case 6:
|
|
428
420
|
return [
|
|
429
421
|
2
|
|
@@ -436,7 +428,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
436
428
|
{
|
|
437
429
|
key: "signMessage",
|
|
438
430
|
value: function signMessage(param) {
|
|
439
|
-
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;
|
|
440
432
|
var _this = this;
|
|
441
433
|
return _async_to_generator(function() {
|
|
442
434
|
var resolvedContext, signatureEd25519, formattedSignature, error;
|
|
@@ -478,6 +470,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
478
470
|
mfaToken: mfaToken,
|
|
479
471
|
elevatedAccessToken: elevatedAccessToken,
|
|
480
472
|
context: resolvedContext,
|
|
473
|
+
approvalQuorum: approvalQuorum,
|
|
481
474
|
onError: onError
|
|
482
475
|
})
|
|
483
476
|
];
|
|
@@ -491,8 +484,10 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
491
484
|
];
|
|
492
485
|
case 4:
|
|
493
486
|
error = _state.sent();
|
|
494
|
-
_this.
|
|
495
|
-
throw new Error(ERROR_SIGN_MESSAGE
|
|
487
|
+
_this.logOperationFailure(ERROR_SIGN_MESSAGE, error);
|
|
488
|
+
throw new Error(ERROR_SIGN_MESSAGE, {
|
|
489
|
+
cause: error
|
|
490
|
+
});
|
|
496
491
|
case 5:
|
|
497
492
|
return [
|
|
498
493
|
2
|
|
@@ -505,7 +500,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
505
500
|
{
|
|
506
501
|
key: "signTransaction",
|
|
507
502
|
value: function signTransaction(param) {
|
|
508
|
-
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;
|
|
509
504
|
var _this = this;
|
|
510
505
|
return _async_to_generator(function() {
|
|
511
506
|
var networkPassphrase, stellarTransaction, transactionHash, resolvedContext, signatureEd25519, formattedSignature, error;
|
|
@@ -558,6 +553,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
558
553
|
mfaToken: mfaToken,
|
|
559
554
|
elevatedAccessToken: elevatedAccessToken,
|
|
560
555
|
context: resolvedContext,
|
|
556
|
+
approvalQuorum: approvalQuorum,
|
|
561
557
|
onError: onError
|
|
562
558
|
})
|
|
563
559
|
];
|
|
@@ -571,8 +567,10 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
571
567
|
];
|
|
572
568
|
case 4:
|
|
573
569
|
error = _state.sent();
|
|
574
|
-
_this.
|
|
575
|
-
throw new Error(ERROR_SIGN_TRANSACTION
|
|
570
|
+
_this.logOperationFailure(ERROR_SIGN_TRANSACTION, error);
|
|
571
|
+
throw new Error(ERROR_SIGN_TRANSACTION, {
|
|
572
|
+
cause: error
|
|
573
|
+
});
|
|
576
574
|
case 5:
|
|
577
575
|
return [
|
|
578
576
|
2
|
|
@@ -585,7 +583,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
585
583
|
{
|
|
586
584
|
key: "exportPrivateKey",
|
|
587
585
|
value: function exportPrivateKey(param) {
|
|
588
|
-
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;
|
|
589
587
|
var _this = this;
|
|
590
588
|
return _async_to_generator(function() {
|
|
591
589
|
var derivedPrivateKey, privateScalarHex, privateKeyBytes, stellarSecretKey, error;
|
|
@@ -606,7 +604,9 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
606
604
|
password: password,
|
|
607
605
|
signedSessionId: signedSessionId,
|
|
608
606
|
mfaToken: mfaToken,
|
|
609
|
-
elevatedAccessToken: elevatedAccessToken
|
|
607
|
+
elevatedAccessToken: elevatedAccessToken,
|
|
608
|
+
approvalQuorum: approvalQuorum,
|
|
609
|
+
proposalId: proposalId
|
|
610
610
|
})
|
|
611
611
|
];
|
|
612
612
|
case 1:
|
|
@@ -626,6 +626,9 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
626
626
|
];
|
|
627
627
|
case 2:
|
|
628
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;
|
|
629
632
|
_this.logger.error(ERROR_EXPORT_PRIVATE_KEY, error);
|
|
630
633
|
throw new Error(ERROR_EXPORT_PRIVATE_KEY);
|
|
631
634
|
case 3:
|
|
@@ -680,7 +683,8 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
680
683
|
chainName: chainName,
|
|
681
684
|
thresholdSignatureScheme: thresholdSignatureScheme,
|
|
682
685
|
privateKey: formattedPrivateKey,
|
|
683
|
-
onCeremonyComplete: function(
|
|
686
|
+
onCeremonyComplete: function(param) {
|
|
687
|
+
var accountAddress = param.accountAddress, walletId = param.walletId, shareSetId = param.shareSetId;
|
|
684
688
|
_this.initializeWalletMapEntry({
|
|
685
689
|
accountAddress: accountAddress,
|
|
686
690
|
walletId: walletId,
|
|
@@ -774,7 +778,7 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
774
778
|
return b.toString(16).padStart(2, '0');
|
|
775
779
|
}).join('');
|
|
776
780
|
} catch (error) {
|
|
777
|
-
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');
|
|
778
782
|
throw error;
|
|
779
783
|
}
|
|
780
784
|
}
|
|
@@ -810,4 +814,408 @@ var DynamicStellarWalletClient = /*#__PURE__*/ function(DynamicWalletClient) {
|
|
|
810
814
|
return DynamicStellarWalletClient;
|
|
811
815
|
}(DynamicWalletClient);
|
|
812
816
|
|
|
813
|
-
|
|
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
|
|
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
|
|
7
|
+
"@dynamic-labs-wallet/browser": "0.1.0-request-signing",
|
|
8
8
|
"@stellar/stellar-sdk": "^12.3.0",
|
|
9
|
-
"@dynamic-labs/sdk-api-core": "
|
|
9
|
+
"@dynamic-labs/sdk-api-core": "0.193.0"
|
|
10
10
|
},
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|