@dynamic-labs/stellar 4.59.1 → 4.60.0
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/CHANGELOG.md +23 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +10 -4
- package/src/StellarLocalStorageCache.cjs +65 -0
- package/src/StellarLocalStorageCache.d.ts +29 -0
- package/src/StellarLocalStorageCache.js +61 -0
- package/src/connectors/FreighterWalletConnector/FreighterProvider.cjs +84 -0
- package/src/connectors/FreighterWalletConnector/FreighterProvider.d.ts +6 -0
- package/src/connectors/FreighterWalletConnector/FreighterProvider.js +80 -0
- package/src/connectors/FreighterWalletConnector/FreighterWalletConnector.cjs +45 -0
- package/src/connectors/FreighterWalletConnector/FreighterWalletConnector.d.ts +16 -0
- package/src/connectors/FreighterWalletConnector/FreighterWalletConnector.js +41 -0
- package/src/connectors/FreighterWalletConnector/index.d.ts +2 -0
- package/src/connectors/LobstrWalletConnector/LobstrWalletConnector.cjs +95 -0
- package/src/connectors/LobstrWalletConnector/LobstrWalletConnector.d.ts +22 -0
- package/src/connectors/LobstrWalletConnector/LobstrWalletConnector.js +91 -0
- package/src/connectors/LobstrWalletConnector/index.d.ts +1 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyProvider.cjs +124 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyProvider.d.ts +6 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyProvider.js +120 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyWalletConnector.cjs +43 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyWalletConnector.d.ts +14 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyWalletConnector.js +39 -0
- package/src/connectors/StellarWalletConnector/StellarWalletConnector.cjs +427 -0
- package/src/connectors/StellarWalletConnector/StellarWalletConnector.d.ts +112 -0
- package/src/connectors/StellarWalletConnector/StellarWalletConnector.js +423 -0
- package/src/connectors/StellarWalletConnector/index.d.ts +1 -0
- package/src/index.cjs +10 -1
- package/src/index.d.ts +5 -2
- package/src/index.js +7 -1
- package/src/injected/fetchInjectedWalletConnectors.cjs +56 -0
- package/src/injected/fetchInjectedWalletConnectors.d.ts +16 -0
- package/src/injected/fetchInjectedWalletConnectors.js +52 -0
- package/src/types/IStellarProvider.d.ts +73 -0
- package/src/types/LobstrProvider.d.ts +33 -0
- package/src/types.d.ts +13 -26
- package/src/utils/buildPaymentTransaction.cjs +37 -0
- package/src/utils/buildPaymentTransaction.d.ts +21 -0
- package/src/utils/buildPaymentTransaction.js +33 -0
- package/src/utils/checkTrustline.cjs +33 -0
- package/src/utils/checkTrustline.d.ts +9 -0
- package/src/utils/checkTrustline.js +29 -0
- package/src/utils/createPaymentAsset.cjs +23 -0
- package/src/utils/createPaymentAsset.d.ts +13 -0
- package/src/utils/createPaymentAsset.js +19 -0
- package/src/utils/createTransactionMemo.d.ts +13 -0
- package/src/utils/getAccountBalance.cjs +33 -0
- package/src/utils/getAccountBalance.d.ts +11 -0
- package/src/utils/getAccountBalance.js +29 -0
- package/src/utils/getNetworkPassphrase.d.ts +7 -0
- package/src/utils/index.d.ts +7 -0
- package/src/utils/normalizeNetworkName.d.ts +14 -0
- package/src/wallet/StellarWallet.cjs +65 -10
- package/src/wallet/StellarWallet.d.ts +17 -21
- package/src/wallet/StellarWallet.js +65 -10
- package/src/wallet/index.d.ts +1 -0
- package/src/wallet/isStellarWallet/index.d.ts +1 -0
- package/src/wallet/isStellarWallet/isStellarWallet.cjs +8 -0
- package/src/wallet/isStellarWallet/isStellarWallet.js +4 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
import { Horizon, Networks } from '@stellar/stellar-sdk';
|
|
4
|
+
import { Logger } from '@dynamic-labs/logger';
|
|
5
|
+
import { DynamicError } from '@dynamic-labs/utils';
|
|
6
|
+
import { WalletConnectorBase } from '@dynamic-labs/wallet-connector-core';
|
|
7
|
+
import { StellarWallet } from '../../wallet/StellarWallet.js';
|
|
8
|
+
import { getNetworkFromAddress } from '../../utils/getNetworkFromAddress.js';
|
|
9
|
+
import { StellarLocalStorageCache } from '../../StellarLocalStorageCache.js';
|
|
10
|
+
|
|
11
|
+
const HORIZON_MAINNET_URL = 'https://horizon.stellar.org';
|
|
12
|
+
const HORIZON_TESTNET_URL = 'https://horizon-testnet.stellar.org';
|
|
13
|
+
/**
|
|
14
|
+
* Abstract base class for all Stellar wallet connectors.
|
|
15
|
+
*
|
|
16
|
+
* This class provides the common functionality needed by all
|
|
17
|
+
* Stellar wallet implementations. It handles connection management,
|
|
18
|
+
* network operations, and transaction signing.
|
|
19
|
+
*/
|
|
20
|
+
class StellarWalletConnector extends WalletConnectorBase {
|
|
21
|
+
constructor(name, opts) {
|
|
22
|
+
var _a;
|
|
23
|
+
super({
|
|
24
|
+
metadata: opts.metadata,
|
|
25
|
+
walletBook: opts.walletBook,
|
|
26
|
+
});
|
|
27
|
+
this.name = 'Stellar';
|
|
28
|
+
this.ChainWallet = StellarWallet;
|
|
29
|
+
this.connectedChain = 'STELLAR';
|
|
30
|
+
this.supportedChains = ['STELLAR'];
|
|
31
|
+
/** Connection state */
|
|
32
|
+
this.isConnecting = false;
|
|
33
|
+
/** Flag to track if connection was cancelled by disconnect */
|
|
34
|
+
this.connectionCancelled = false;
|
|
35
|
+
this.name = name;
|
|
36
|
+
this.logger = new Logger(this.name);
|
|
37
|
+
this.stellarNetworks = (_a = opts.stellarNetworks) !== null && _a !== void 0 ? _a : [];
|
|
38
|
+
this.overrideKey = this.key;
|
|
39
|
+
this.cache = new StellarLocalStorageCache(this.overrideKey);
|
|
40
|
+
}
|
|
41
|
+
getEnabledNetworks() {
|
|
42
|
+
return this.stellarNetworks;
|
|
43
|
+
}
|
|
44
|
+
getSelectedNetwork() {
|
|
45
|
+
var _a;
|
|
46
|
+
return (_a = this.stellarNetworks) === null || _a === void 0 ? void 0 : _a[0];
|
|
47
|
+
}
|
|
48
|
+
getNetwork() {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
const address = yield this.getAddress();
|
|
51
|
+
if (!address) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
if (!this.stellarNetworks.length) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const network = yield getNetworkFromAddress(address, this.stellarNetworks[0]);
|
|
58
|
+
if (!network) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
this.selectedNetwork = this.stellarNetworks.find((n) => n.chainId.toString() === network);
|
|
62
|
+
return network;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks if the provider is already connected.
|
|
67
|
+
*/
|
|
68
|
+
isProviderConnected() {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
const provider = this.getProvider();
|
|
71
|
+
if (!provider) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
// Check if provider has an isConnected property (not part of IStellarProvider but some wallets have it)
|
|
75
|
+
const providerWithConnection = provider;
|
|
76
|
+
if (typeof providerWithConnection.isConnected === 'boolean') {
|
|
77
|
+
return providerWithConnection.isConnected;
|
|
78
|
+
}
|
|
79
|
+
// Some providers have isConnected as a method
|
|
80
|
+
if (typeof providerWithConnection.isConnected === 'function') {
|
|
81
|
+
return providerWithConnection.isConnected();
|
|
82
|
+
}
|
|
83
|
+
// If we have a cached public key, assume connected
|
|
84
|
+
return Boolean(this.connectedPublicKey);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Connects to the Stellar wallet.
|
|
89
|
+
*/
|
|
90
|
+
connect() {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
// Avoid unnecessary connection attempts
|
|
93
|
+
if (this.isConnecting) {
|
|
94
|
+
this.logger.debug('[connect] Connection already in progress');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
// Check if already connected
|
|
98
|
+
if (yield this.isProviderConnected()) {
|
|
99
|
+
this.logger.debug('[connect] Provider already connected');
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.isConnecting = true;
|
|
103
|
+
this.connectionCancelled = false;
|
|
104
|
+
this.logger.debug('[connect] Attempting to connect');
|
|
105
|
+
try {
|
|
106
|
+
const provider = this.getProvider();
|
|
107
|
+
if (!provider) {
|
|
108
|
+
throw new DynamicError('No Stellar provider found');
|
|
109
|
+
}
|
|
110
|
+
const publicKey = yield provider.connect();
|
|
111
|
+
// Check if disconnect was called while we were waiting for provider.connect()
|
|
112
|
+
// This prevents a race condition where disconnect() clears state but connect()
|
|
113
|
+
// subsequently sets connectedPublicKey
|
|
114
|
+
if (this.connectionCancelled) {
|
|
115
|
+
this.logger.debug('[connect] Connection cancelled by disconnect');
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
this.connectedPublicKey = publicKey;
|
|
119
|
+
if (publicKey) {
|
|
120
|
+
yield this.cache.setConnectedAccount(publicKey, {
|
|
121
|
+
active: true,
|
|
122
|
+
additionalAddresses: [],
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
this.logger.debug('[connect] Connected successfully');
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
this.logger.error('[connect] Connection failed:', error);
|
|
129
|
+
throw new DynamicError('Failed to connect to Stellar wallet');
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
this.isConnecting = false;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Disconnects from the wallet.
|
|
138
|
+
*/
|
|
139
|
+
disconnect() {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
this.logger.debug('[disconnect] Disconnecting');
|
|
142
|
+
// Cancel any ongoing connection attempt to prevent race condition
|
|
143
|
+
// where connect() might set connectedPublicKey after we clear it
|
|
144
|
+
if (this.isConnecting) {
|
|
145
|
+
this.logger.debug('[disconnect] Cancelling ongoing connection attempt');
|
|
146
|
+
this.connectionCancelled = true;
|
|
147
|
+
this.isConnecting = false;
|
|
148
|
+
}
|
|
149
|
+
const provider = this.getProvider();
|
|
150
|
+
if (!provider) {
|
|
151
|
+
// Still clear state even if no provider
|
|
152
|
+
this.connectedPublicKey = undefined;
|
|
153
|
+
this.getAddressPromise = undefined;
|
|
154
|
+
yield this.cache.clearConnectedAcccounts();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
yield provider.disconnect();
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
this.logger.debug('[disconnect] Disconnect not supported or failed:', error);
|
|
162
|
+
}
|
|
163
|
+
// Clear all cached state
|
|
164
|
+
this.connectedPublicKey = undefined;
|
|
165
|
+
this.getAddressPromise = undefined;
|
|
166
|
+
yield this.cache.clearConnectedAcccounts();
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Signs a Stellar transaction XDR.
|
|
171
|
+
*/
|
|
172
|
+
signTransaction(transactionXdr) {
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
const provider = this.getProvider();
|
|
175
|
+
if (!provider) {
|
|
176
|
+
throw new DynamicError('No Stellar provider found');
|
|
177
|
+
}
|
|
178
|
+
const network = yield this.getNetwork();
|
|
179
|
+
const networkPassphrase = yield this.getNetworkPassphrase();
|
|
180
|
+
return provider.sign(transactionXdr, {
|
|
181
|
+
network,
|
|
182
|
+
networkPassphrase,
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Signs a message.
|
|
188
|
+
*/
|
|
189
|
+
signMessage(messageToSign) {
|
|
190
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
191
|
+
const provider = this.getProvider();
|
|
192
|
+
if (!provider) {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
return provider.signMessage(messageToSign);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
getHorizonServer() {
|
|
199
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
200
|
+
var _a, _b;
|
|
201
|
+
const url = ((_b = (_a = this.stellarNetworks[0]) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.toLowerCase().includes('testnet'))
|
|
202
|
+
? HORIZON_TESTNET_URL
|
|
203
|
+
: HORIZON_MAINNET_URL;
|
|
204
|
+
return new Horizon.Server(url);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Gets the network passphrase for the current network.
|
|
209
|
+
* @returns The network passphrase string
|
|
210
|
+
*/
|
|
211
|
+
getNetworkPassphrase() {
|
|
212
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
+
var _a, _b, _c, _d, _e, _f;
|
|
214
|
+
// First, try to get network from provider
|
|
215
|
+
const provider = this.getProvider();
|
|
216
|
+
if (provider) {
|
|
217
|
+
try {
|
|
218
|
+
const providerNetwork = yield provider.getNetwork();
|
|
219
|
+
if (!providerNetwork) {
|
|
220
|
+
throw new Error('Provider returned null or undefined network');
|
|
221
|
+
}
|
|
222
|
+
const networkUpper = providerNetwork.toUpperCase();
|
|
223
|
+
if (networkUpper === 'TESTNET' || networkUpper.includes('TESTNET')) {
|
|
224
|
+
return Networks.TESTNET;
|
|
225
|
+
}
|
|
226
|
+
if (networkUpper === 'FUTURENET' ||
|
|
227
|
+
networkUpper.includes('FUTURENET')) {
|
|
228
|
+
return Networks.FUTURENET;
|
|
229
|
+
}
|
|
230
|
+
if (networkUpper === 'PUBLIC' || networkUpper.includes('PUBLIC')) {
|
|
231
|
+
return Networks.PUBLIC;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
// Provider getNetwork failed, fall through to other checks
|
|
236
|
+
this.logger.debug('[getNetworkPassphrase] Provider getNetwork failed:', error);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
// Fall back to checking selectedNetwork name
|
|
240
|
+
const networkName = (_c = (_b = (_a = this.selectedNetwork) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : '';
|
|
241
|
+
if (networkName.includes('testnet')) {
|
|
242
|
+
return Networks.TESTNET;
|
|
243
|
+
}
|
|
244
|
+
if (networkName.includes('futurenet')) {
|
|
245
|
+
return Networks.FUTURENET;
|
|
246
|
+
}
|
|
247
|
+
// Check Horizon server URL as last resort
|
|
248
|
+
const horizonUrl = (_f = (_e = (_d = this.selectedNetwork) === null || _d === void 0 ? void 0 : _d.rpcUrls) === null || _e === void 0 ? void 0 : _e[0]) !== null && _f !== void 0 ? _f : '';
|
|
249
|
+
if (horizonUrl.includes('testnet')) {
|
|
250
|
+
return Networks.TESTNET;
|
|
251
|
+
}
|
|
252
|
+
if (horizonUrl.includes('futurenet')) {
|
|
253
|
+
return Networks.FUTURENET;
|
|
254
|
+
}
|
|
255
|
+
// Default to mainnet/public
|
|
256
|
+
return Networks.PUBLIC;
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
isTestnet() {
|
|
260
|
+
var _a, _b;
|
|
261
|
+
const network = this.getSelectedNetwork();
|
|
262
|
+
if (!network) {
|
|
263
|
+
return Promise.resolve(false);
|
|
264
|
+
}
|
|
265
|
+
return Promise.resolve((_b = (_a = network.name) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes('testnet')) !== null && _b !== void 0 ? _b : false);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Gets the connected public key.
|
|
269
|
+
*/
|
|
270
|
+
getConnectedPublicKey() {
|
|
271
|
+
return this.connectedPublicKey;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Fetches the public address from the wallet provider.
|
|
275
|
+
* This method ensures the wallet is connected before fetching the address.
|
|
276
|
+
* Override this method in child classes if the wallet has a custom way to fetch addresses.
|
|
277
|
+
* @returns The public address from the wallet
|
|
278
|
+
*/
|
|
279
|
+
fetchPublicAddress() {
|
|
280
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
281
|
+
var _a;
|
|
282
|
+
const provider = this.getProvider();
|
|
283
|
+
if (!provider) {
|
|
284
|
+
return '';
|
|
285
|
+
}
|
|
286
|
+
// Ensure provider is connected
|
|
287
|
+
yield this.connect();
|
|
288
|
+
// After connection, try to get the public key from the provider
|
|
289
|
+
// Different wallets may store it in different ways (not part of IStellarProvider but some wallets have these)
|
|
290
|
+
const providerWithExtras = provider;
|
|
291
|
+
if (providerWithExtras.publicKey) {
|
|
292
|
+
return providerWithExtras.publicKey;
|
|
293
|
+
}
|
|
294
|
+
// Some wallets require calling getPublicKey()
|
|
295
|
+
if (typeof providerWithExtras.getPublicKey === 'function') {
|
|
296
|
+
return providerWithExtras.getPublicKey();
|
|
297
|
+
}
|
|
298
|
+
// If we still don't have it, the connectedPublicKey should have been set by connect()
|
|
299
|
+
return (_a = this.connectedPublicKey) !== null && _a !== void 0 ? _a : '';
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Gets the address from cache or fetches it from the wallet.
|
|
304
|
+
* This implementation avoids prompting the user unnecessarily by checking cache first.
|
|
305
|
+
* Uses promise caching to prevent multiple simultaneous calls.
|
|
306
|
+
*/
|
|
307
|
+
getAddress() {
|
|
308
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
309
|
+
// If there's already a pending getAddress call, return the same promise
|
|
310
|
+
// This prevents multiple simultaneous wallet prompts
|
|
311
|
+
if (this.getAddressPromise) {
|
|
312
|
+
try {
|
|
313
|
+
return yield this.getAddressPromise;
|
|
314
|
+
}
|
|
315
|
+
catch (error) {
|
|
316
|
+
this.logger.error('[getAddress] Error fetching address from promise cache:', error);
|
|
317
|
+
this.getAddressPromise = undefined;
|
|
318
|
+
// Fall through to try again
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
// Return cached public key if already connected
|
|
322
|
+
if (this.connectedPublicKey) {
|
|
323
|
+
return this.connectedPublicKey;
|
|
324
|
+
}
|
|
325
|
+
// Create a new promise and cache it
|
|
326
|
+
this.getAddressPromise = (() => __awaiter(this, void 0, void 0, function* () {
|
|
327
|
+
try {
|
|
328
|
+
// Check cache first to avoid prompting the user
|
|
329
|
+
const cachedAccount = yield this.cache.getActiveAccount();
|
|
330
|
+
if (cachedAccount === null || cachedAccount === void 0 ? void 0 : cachedAccount.address) {
|
|
331
|
+
this.connectedPublicKey = cachedAccount.address;
|
|
332
|
+
return cachedAccount.address;
|
|
333
|
+
}
|
|
334
|
+
// Fetch from wallet provider
|
|
335
|
+
const address = yield this.fetchPublicAddress();
|
|
336
|
+
this.connectedPublicKey = address;
|
|
337
|
+
// Cache the address
|
|
338
|
+
if (address) {
|
|
339
|
+
yield this.cache.setConnectedAccount(address, {
|
|
340
|
+
active: true,
|
|
341
|
+
additionalAddresses: [],
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
return address !== null && address !== void 0 ? address : '';
|
|
345
|
+
}
|
|
346
|
+
finally {
|
|
347
|
+
// Clear the promise cache after completion
|
|
348
|
+
this.getAddressPromise = undefined;
|
|
349
|
+
}
|
|
350
|
+
}))();
|
|
351
|
+
return this.getAddressPromise;
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Clears all connected accounts from cache.
|
|
356
|
+
*/
|
|
357
|
+
clearConnectedAccounts() {
|
|
358
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
359
|
+
yield this.cache.clearConnectedAcccounts();
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Gets connected accounts from cache.
|
|
364
|
+
*/
|
|
365
|
+
getConnectedAccountsFromCache() {
|
|
366
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
367
|
+
const currentAccount = yield this.cache.getActiveAccount();
|
|
368
|
+
const allAccounts = yield this.cache.getConnectedAccounts();
|
|
369
|
+
const allConnectedAddresses = Object.keys(allAccounts || {});
|
|
370
|
+
if (!(currentAccount === null || currentAccount === void 0 ? void 0 : currentAccount.address)) {
|
|
371
|
+
return allConnectedAddresses;
|
|
372
|
+
}
|
|
373
|
+
return [
|
|
374
|
+
currentAccount.address,
|
|
375
|
+
...allConnectedAddresses.filter((address) => address !== currentAccount.address),
|
|
376
|
+
];
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Gets connected accounts.
|
|
381
|
+
* Returns accounts from cache to avoid prompting the user.
|
|
382
|
+
*/
|
|
383
|
+
getConnectedAccounts() {
|
|
384
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
385
|
+
return this.getConnectedAccountsFromCache();
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Gets additional addresses for a main address.
|
|
390
|
+
*/
|
|
391
|
+
getAdditionalAddresses(mainAddress) {
|
|
392
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
393
|
+
if (!mainAddress) {
|
|
394
|
+
return [];
|
|
395
|
+
}
|
|
396
|
+
const currentAccount = yield this.cache.getConnectedAccount(mainAddress);
|
|
397
|
+
return (currentAccount === null || currentAccount === void 0 ? void 0 : currentAccount.additionalAddresses) || [];
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Sets additional addresses for a main address.
|
|
402
|
+
*/
|
|
403
|
+
setAdditionalAddresses(mainAddress, additionalAddresses) {
|
|
404
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
405
|
+
return this.cache.setConnectedAccount(mainAddress, {
|
|
406
|
+
additionalAddresses,
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Ends the session and clears connected accounts.
|
|
412
|
+
*/
|
|
413
|
+
endSession() {
|
|
414
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
415
|
+
this.connectedPublicKey = undefined;
|
|
416
|
+
this.getAddressPromise = undefined;
|
|
417
|
+
this.connectionCancelled = false;
|
|
418
|
+
yield this.cache.clearConnectedAcccounts();
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export { StellarWalletConnector };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { StellarWalletConnector } from './StellarWalletConnector';
|
package/src/index.cjs
CHANGED
|
@@ -5,16 +5,25 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
|
|
6
6
|
var assertPackageVersion = require('@dynamic-labs/assert-package-version');
|
|
7
7
|
var _package = require('../package.cjs');
|
|
8
|
+
var fetchInjectedWalletConnectors = require('./injected/fetchInjectedWalletConnectors.cjs');
|
|
8
9
|
var StellarWallet = require('./wallet/StellarWallet.cjs');
|
|
10
|
+
var isStellarWallet = require('./wallet/isStellarWallet/isStellarWallet.cjs');
|
|
11
|
+
var StellarWalletConnector = require('./connectors/StellarWalletConnector/StellarWalletConnector.cjs');
|
|
12
|
+
var StellarLocalStorageCache = require('./StellarLocalStorageCache.cjs');
|
|
13
|
+
require('@stellar/stellar-sdk');
|
|
9
14
|
var getNetworkFromAddress = require('./utils/getNetworkFromAddress.cjs');
|
|
10
15
|
|
|
11
16
|
assertPackageVersion.assertPackageVersion('@dynamic-labs/stellar', _package.version);
|
|
12
17
|
// Wallet connector factory (to be implemented by specific wallet connectors)
|
|
13
18
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
14
19
|
const StellarWalletConnectors = (_props) => [
|
|
15
|
-
|
|
20
|
+
...fetchInjectedWalletConnectors.fetchInjectedWalletConnectors(),
|
|
21
|
+
// Specific wallet connectors will be added here by implementers
|
|
16
22
|
];
|
|
17
23
|
|
|
18
24
|
exports.StellarWallet = StellarWallet.StellarWallet;
|
|
25
|
+
exports.isStellarWallet = isStellarWallet.isStellarWallet;
|
|
26
|
+
exports.StellarWalletConnector = StellarWalletConnector.StellarWalletConnector;
|
|
27
|
+
exports.StellarLocalStorageCache = StellarLocalStorageCache.StellarLocalStorageCache;
|
|
19
28
|
exports.getNetworkFromAddress = getNetworkFromAddress.getNetworkFromAddress;
|
|
20
29
|
exports.StellarWalletConnectors = StellarWalletConnectors;
|
package/src/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
export { StellarWallet } from './wallet';
|
|
1
|
+
export { StellarWallet, isStellarWallet } from './wallet';
|
|
2
|
+
export { StellarWalletConnector } from './connectors/StellarWalletConnector';
|
|
3
|
+
export { StellarLocalStorageCache, type IStellarSessionCache, type StellarConnectedAccount, } from './StellarLocalStorageCache';
|
|
4
|
+
export type { IStellarProvider, StellarConnectionResult, StellarEventCallback, StellarMethodName, StellarNetworkName, StellarProviderEvent, StellarSendBalanceProps, StellarSignTransactionOptions, StellarWalletConnectorProps, } from './types';
|
|
2
5
|
export { getNetworkFromAddress } from './utils';
|
|
3
|
-
export declare const StellarWalletConnectors: (_props?: unknown) =>
|
|
6
|
+
export declare const StellarWalletConnectors: (_props?: unknown) => import("dist/packages/wallet-connector-core/src").WalletConnectorConstructor[];
|
package/src/index.js
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
import { assertPackageVersion } from '@dynamic-labs/assert-package-version';
|
|
3
3
|
import { version } from '../package.js';
|
|
4
|
+
import { fetchInjectedWalletConnectors } from './injected/fetchInjectedWalletConnectors.js';
|
|
4
5
|
export { StellarWallet } from './wallet/StellarWallet.js';
|
|
6
|
+
export { isStellarWallet } from './wallet/isStellarWallet/isStellarWallet.js';
|
|
7
|
+
export { StellarWalletConnector } from './connectors/StellarWalletConnector/StellarWalletConnector.js';
|
|
8
|
+
export { StellarLocalStorageCache } from './StellarLocalStorageCache.js';
|
|
9
|
+
import '@stellar/stellar-sdk';
|
|
5
10
|
export { getNetworkFromAddress } from './utils/getNetworkFromAddress.js';
|
|
6
11
|
|
|
7
12
|
assertPackageVersion('@dynamic-labs/stellar', version);
|
|
8
13
|
// Wallet connector factory (to be implemented by specific wallet connectors)
|
|
9
14
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
10
15
|
const StellarWalletConnectors = (_props) => [
|
|
11
|
-
|
|
16
|
+
...fetchInjectedWalletConnectors(),
|
|
17
|
+
// Specific wallet connectors will be added here by implementers
|
|
12
18
|
];
|
|
13
19
|
|
|
14
20
|
export { StellarWalletConnectors };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
7
|
+
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
8
|
+
var logger$1 = require('@dynamic-labs/logger');
|
|
9
|
+
var FreighterWalletConnector = require('../connectors/FreighterWalletConnector/FreighterWalletConnector.cjs');
|
|
10
|
+
var LobstrWalletConnector = require('../connectors/LobstrWalletConnector/LobstrWalletConnector.cjs');
|
|
11
|
+
var OneKeyWalletConnector = require('../connectors/OneKeyWalletConnector/OneKeyWalletConnector.cjs');
|
|
12
|
+
|
|
13
|
+
const logger = new logger$1.Logger('fetchInjectedWalletConnectors');
|
|
14
|
+
const initialStellarWalletConnectors = [LobstrWalletConnector.LobstrWalletConnector, FreighterWalletConnector.FreighterWalletConnector, OneKeyWalletConnector.OneKeyWalletConnector];
|
|
15
|
+
const stellarWalletConnectors = [
|
|
16
|
+
...initialStellarWalletConnectors,
|
|
17
|
+
];
|
|
18
|
+
const checkAndEmitWalletConnector = (walletConnector) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
var _a;
|
|
20
|
+
try {
|
|
21
|
+
yield ((_a = walletConnector.isConnected) === null || _a === void 0 ? void 0 : _a.call(walletConnector));
|
|
22
|
+
return walletConnector;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
logger.logVerboseTroubleshootingMessage(`[STELLAR fetchInjectedWalletConnectors] Error checking ${walletConnector.name}:`, error);
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
});
|
|
29
|
+
const setupStellarWalletListener = () => {
|
|
30
|
+
if (typeof window === 'undefined') {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const discoveredWallets = new Set();
|
|
34
|
+
const checkForNewWallets = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
for (const walletConnector of stellarWalletConnectors) {
|
|
36
|
+
if (discoveredWallets.has(walletConnector.name)) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const connector = yield checkAndEmitWalletConnector(walletConnector);
|
|
40
|
+
if (connector) {
|
|
41
|
+
discoveredWallets.add(walletConnector.name);
|
|
42
|
+
logger.logVerboseTroubleshootingMessage(`[STELLAR fetchInjectedWalletConnectors] Emitting providerInjected for ${walletConnector.name}`);
|
|
43
|
+
walletConnectorCore.walletConnectorEvents.emit('providerInjected', {
|
|
44
|
+
injectedConnectorConstructor: connector,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
void checkForNewWallets();
|
|
50
|
+
};
|
|
51
|
+
const fetchInjectedWalletConnectors = () => {
|
|
52
|
+
setupStellarWalletListener();
|
|
53
|
+
return [...stellarWalletConnectors];
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
exports.fetchInjectedWalletConnectors = fetchInjectedWalletConnectors;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { WalletConnectorConstructor } from '@dynamic-labs/wallet-connector-core';
|
|
2
|
+
interface StellarWalletConnectorWithIsConnected extends WalletConnectorConstructor {
|
|
3
|
+
isConnected?: () => Promise<boolean>;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Registers a Stellar wallet connector for injection discovery.
|
|
7
|
+
* @internal - Used for testing and internal registration
|
|
8
|
+
*/
|
|
9
|
+
export declare const registerStellarWalletConnector: (connector: StellarWalletConnectorWithIsConnected) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Clears dynamically registered Stellar wallet connectors and restores initial connectors.
|
|
12
|
+
* @internal - Used for testing cleanup
|
|
13
|
+
*/
|
|
14
|
+
export declare const clearStellarWalletConnectors: () => void;
|
|
15
|
+
export declare const fetchInjectedWalletConnectors: () => WalletConnectorConstructor[];
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
3
|
+
import { walletConnectorEvents } from '@dynamic-labs/wallet-connector-core';
|
|
4
|
+
import { Logger } from '@dynamic-labs/logger';
|
|
5
|
+
import { FreighterWalletConnector } from '../connectors/FreighterWalletConnector/FreighterWalletConnector.js';
|
|
6
|
+
import { LobstrWalletConnector } from '../connectors/LobstrWalletConnector/LobstrWalletConnector.js';
|
|
7
|
+
import { OneKeyWalletConnector } from '../connectors/OneKeyWalletConnector/OneKeyWalletConnector.js';
|
|
8
|
+
|
|
9
|
+
const logger = new Logger('fetchInjectedWalletConnectors');
|
|
10
|
+
const initialStellarWalletConnectors = [LobstrWalletConnector, FreighterWalletConnector, OneKeyWalletConnector];
|
|
11
|
+
const stellarWalletConnectors = [
|
|
12
|
+
...initialStellarWalletConnectors,
|
|
13
|
+
];
|
|
14
|
+
const checkAndEmitWalletConnector = (walletConnector) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
var _a;
|
|
16
|
+
try {
|
|
17
|
+
yield ((_a = walletConnector.isConnected) === null || _a === void 0 ? void 0 : _a.call(walletConnector));
|
|
18
|
+
return walletConnector;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
logger.logVerboseTroubleshootingMessage(`[STELLAR fetchInjectedWalletConnectors] Error checking ${walletConnector.name}:`, error);
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
});
|
|
25
|
+
const setupStellarWalletListener = () => {
|
|
26
|
+
if (typeof window === 'undefined') {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const discoveredWallets = new Set();
|
|
30
|
+
const checkForNewWallets = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
+
for (const walletConnector of stellarWalletConnectors) {
|
|
32
|
+
if (discoveredWallets.has(walletConnector.name)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const connector = yield checkAndEmitWalletConnector(walletConnector);
|
|
36
|
+
if (connector) {
|
|
37
|
+
discoveredWallets.add(walletConnector.name);
|
|
38
|
+
logger.logVerboseTroubleshootingMessage(`[STELLAR fetchInjectedWalletConnectors] Emitting providerInjected for ${walletConnector.name}`);
|
|
39
|
+
walletConnectorEvents.emit('providerInjected', {
|
|
40
|
+
injectedConnectorConstructor: connector,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
void checkForNewWallets();
|
|
46
|
+
};
|
|
47
|
+
const fetchInjectedWalletConnectors = () => {
|
|
48
|
+
setupStellarWalletListener();
|
|
49
|
+
return [...stellarWalletConnectors];
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export { fetchInjectedWalletConnectors };
|