@caatinga/client 0.2.1 → 0.2.3

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/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/artifacts/resolve-contract-id.ts
2
- import { CaatingaError, CaatingaErrorCode } from "@caatinga/core";
2
+ import { CaatingaError, CaatingaErrorCode } from "@caatinga/core/browser";
3
3
  function resolveContractId(input) {
4
4
  if (input.explicitContractId) {
5
5
  return input.explicitContractId;
@@ -16,7 +16,7 @@ function resolveContractId(input) {
16
16
  }
17
17
 
18
18
  // src/bindings/default-binding-adapter.ts
19
- import { CaatingaError as CaatingaError2, CaatingaErrorCode as CaatingaErrorCode2 } from "@caatinga/core";
19
+ import { CaatingaError as CaatingaError2, CaatingaErrorCode as CaatingaErrorCode2 } from "@caatinga/core/browser";
20
20
  function createDefaultBindingAdapter(binding) {
21
21
  return {
22
22
  createClient({ contractId, publicKey, rpcUrl, networkPassphrase }) {
@@ -50,13 +50,13 @@ function createDefaultBindingAdapter(binding) {
50
50
  }
51
51
 
52
52
  // src/client/create-caatinga-client.ts
53
- import { CaatingaError as CaatingaError5, CaatingaErrorCode as CaatingaErrorCode5 } from "@caatinga/core";
53
+ import { CaatingaError as CaatingaError5, CaatingaErrorCode as CaatingaErrorCode5 } from "@caatinga/core/browser";
54
54
 
55
55
  // src/client/caatinga-contract-client.ts
56
- import { CaatingaError as CaatingaError4, CaatingaErrorCode as CaatingaErrorCode4 } from "@caatinga/core";
56
+ import { CaatingaError as CaatingaError4, CaatingaErrorCode as CaatingaErrorCode4 } from "@caatinga/core/browser";
57
57
 
58
58
  // src/xdr/build-xdr.ts
59
- import { CaatingaError as CaatingaError3, CaatingaErrorCode as CaatingaErrorCode3 } from "@caatinga/core";
59
+ import { CaatingaError as CaatingaError3, CaatingaErrorCode as CaatingaErrorCode3 } from "@caatinga/core/browser";
60
60
  async function buildXdr(input) {
61
61
  try {
62
62
  const transaction = input.transaction;
@@ -72,7 +72,7 @@ async function buildXdr(input) {
72
72
  throw new CaatingaError3(
73
73
  `Failed to prepare XDR for "${input.contractName}.${input.method}".`,
74
74
  CaatingaErrorCode3.XDR_PREPARE_FAILED,
75
- "Check RPC connectivity, simulation errors, and binding compatibility.",
75
+ `RPC: ${input.rpcUrl}. Check connectivity, simulation errors, and binding compatibility.`,
76
76
  error
77
77
  );
78
78
  }
@@ -134,6 +134,7 @@ var CaatingaContractClient = class {
134
134
  method,
135
135
  contractId,
136
136
  transaction,
137
+ rpcUrl: this.config.network.rpcUrl,
137
138
  debug: debugRaw
138
139
  });
139
140
  }
@@ -145,23 +146,54 @@ var CaatingaContractClient = class {
145
146
  method,
146
147
  contractId,
147
148
  transaction,
149
+ rpcUrl: this.config.network.rpcUrl,
148
150
  debug: debugRaw
149
151
  });
150
152
  let signedXdr;
151
- try {
152
- signedXdr = await this.config.wallet.signTransaction({
153
- xdr: xdr.preparedXdr,
154
- networkPassphrase: this.config.network.networkPassphrase
155
- });
156
- } catch (error) {
153
+ const signTransaction = async (xdr2) => {
154
+ try {
155
+ signedXdr = await this.withWalletTimeout(
156
+ "signTransaction",
157
+ () => this.config.wallet.signTransaction({
158
+ xdr: xdr2,
159
+ networkPassphrase: this.config.network.networkPassphrase
160
+ })
161
+ );
162
+ } catch (error) {
163
+ if (error instanceof CaatingaError4) {
164
+ throw error;
165
+ }
166
+ throw new CaatingaError4(
167
+ `Failed to sign XDR for "${this.contractName}.${method}".`,
168
+ CaatingaErrorCode4.XDR_SIGN_FAILED,
169
+ "Connect a wallet and approve the transaction.",
170
+ error
171
+ );
172
+ }
173
+ if (typeof signedXdr !== "string" || signedXdr.trim().length === 0) {
174
+ throw new CaatingaError4(
175
+ `Failed to sign XDR for "${this.contractName}.${method}".`,
176
+ CaatingaErrorCode4.XDR_SIGN_FAILED,
177
+ "Wallet returned an empty or invalid signed XDR. The user may have dismissed the signing prompt.",
178
+ signedXdr
179
+ );
180
+ }
181
+ return { signedTxXdr: signedXdr };
182
+ };
183
+ const raw = await submitTransaction(
184
+ transaction,
185
+ signTransaction,
186
+ this.contractName,
187
+ method,
188
+ this.config.network.rpcUrl
189
+ );
190
+ if (typeof transaction.signAndSend === "function" && signedXdr === void 0) {
157
191
  throw new CaatingaError4(
158
192
  `Failed to sign XDR for "${this.contractName}.${method}".`,
159
193
  CaatingaErrorCode4.XDR_SIGN_FAILED,
160
- "Connect a wallet and approve the transaction.",
161
- error
194
+ "Wallet returned an empty or invalid signed XDR. The generated transaction did not request a wallet signature."
162
195
  );
163
196
  }
164
- const raw = await submitTransaction(transaction, signedXdr, this.contractName, method);
165
197
  const normalized = normalizeSubmitResult(raw);
166
198
  return {
167
199
  status: "confirmed",
@@ -174,12 +206,35 @@ var CaatingaContractClient = class {
174
206
  xdr: {
175
207
  unsigned: xdr.unsignedXdr,
176
208
  prepared: xdr.preparedXdr,
177
- signed: signedXdr
209
+ ...signedXdr ? { signed: signedXdr } : {}
178
210
  }
179
211
  } : {},
180
212
  ...debugRaw ? { raw } : {}
181
213
  };
182
214
  }
215
+ async simulate(method, argsOrOptions, maybeOptions) {
216
+ const { args, debugRaw } = splitReadArgsAndOptions(argsOrOptions, maybeOptions);
217
+ const { contractId, transaction } = await this.createTransaction(method, args);
218
+ const raw = await prepareReadTransaction(
219
+ transaction,
220
+ this.contractName,
221
+ method,
222
+ this.config.network.rpcUrl
223
+ );
224
+ const result = readSimulationResult(raw, this.contractName, method);
225
+ return {
226
+ status: "simulated",
227
+ contract: this.contractName,
228
+ method,
229
+ contractId,
230
+ result,
231
+ ...debugRaw ? { raw } : {}
232
+ };
233
+ }
234
+ async read(method, argsOrOptions, maybeOptions) {
235
+ const result = await this.simulate(method, argsOrOptions, maybeOptions);
236
+ return result.result;
237
+ }
183
238
  async createTransaction(method, args) {
184
239
  const contractId = resolveContractId({
185
240
  artifacts: this.config.artifacts,
@@ -189,7 +244,10 @@ var CaatingaContractClient = class {
189
244
  });
190
245
  let publicKey;
191
246
  try {
192
- publicKey = await this.config.wallet.getPublicKey();
247
+ publicKey = await this.withWalletTimeout(
248
+ "getPublicKey",
249
+ () => this.config.wallet.getPublicKey()
250
+ );
193
251
  } catch (error) {
194
252
  if (error instanceof CaatingaError4) {
195
253
  throw error;
@@ -210,6 +268,41 @@ var CaatingaContractClient = class {
210
268
  const transaction = await this.bindingAdapter.callMethod({ client, method, args });
211
269
  return { contractId, transaction };
212
270
  }
271
+ withWalletTimeout(label, fn) {
272
+ const timeoutMs = this.config.walletTimeout;
273
+ if (timeoutMs === void 0 || timeoutMs <= 0) {
274
+ return fn();
275
+ }
276
+ let timedOut = false;
277
+ return new Promise((resolve, reject) => {
278
+ const timer = setTimeout(() => {
279
+ timedOut = true;
280
+ reject(
281
+ new CaatingaError4(
282
+ `Wallet "${label}" timed out after ${timeoutMs}ms.`,
283
+ CaatingaErrorCode4.WALLET_TIMEOUT,
284
+ "Ensure the wallet adapter rejects on user dismissal, or increase walletTimeout."
285
+ )
286
+ );
287
+ }, timeoutMs);
288
+ fn().then(
289
+ (value) => {
290
+ if (timedOut) {
291
+ return;
292
+ }
293
+ clearTimeout(timer);
294
+ resolve(value);
295
+ },
296
+ (error) => {
297
+ if (timedOut) {
298
+ return;
299
+ }
300
+ clearTimeout(timer);
301
+ reject(error);
302
+ }
303
+ );
304
+ });
305
+ }
213
306
  };
214
307
  function splitArgsAndOptions(argsOrOptions, maybeOptions) {
215
308
  return {
@@ -233,38 +326,100 @@ function splitInvokeArgsAndOptions(argsOrOptions, maybeOptions) {
233
326
  debugRaw: maybeOptions?.debugRaw ?? false
234
327
  };
235
328
  }
236
- async function submitTransaction(transaction, signedXdr, contractName, method) {
329
+ function splitReadArgsAndOptions(argsOrOptions, maybeOptions) {
330
+ const looksLikeOptions = argsOrOptions !== void 0 && "debugRaw" in argsOrOptions && maybeOptions === void 0;
331
+ if (looksLikeOptions) {
332
+ const options = argsOrOptions;
333
+ return {
334
+ args: void 0,
335
+ debugRaw: options.debugRaw ?? false
336
+ };
337
+ }
338
+ return {
339
+ args: argsOrOptions,
340
+ debugRaw: maybeOptions?.debugRaw ?? false
341
+ };
342
+ }
343
+ async function submitTransaction(transaction, signTransaction, contractName, method, rpcUrl) {
237
344
  const candidate = transaction;
238
- const submit = candidate.signAndSend ?? candidate.send;
239
- if (typeof submit !== "function") {
240
- throw new CaatingaError4(
241
- `Binding transaction for "${contractName}.${method}" cannot be submitted.`,
242
- CaatingaErrorCode4.XDR_SUBMIT_FAILED,
243
- "Regenerate bindings or provide a compatible binding adapter."
244
- );
345
+ if (typeof candidate.signAndSend === "function") {
346
+ try {
347
+ const raw = await candidate.signAndSend.call(transaction, { signTransaction });
348
+ assertSubmitResultRecognized(raw, contractName, method);
349
+ return raw;
350
+ } catch (error) {
351
+ if (error instanceof CaatingaError4) {
352
+ throw error;
353
+ }
354
+ throw new CaatingaError4(
355
+ `Failed to submit XDR for "${contractName}.${method}".`,
356
+ CaatingaErrorCode4.XDR_SUBMIT_FAILED,
357
+ `RPC: ${rpcUrl}. Check wallet signature and RPC connectivity.`,
358
+ error
359
+ );
360
+ }
361
+ }
362
+ if (typeof candidate.send === "function") {
363
+ try {
364
+ const raw = await candidate.send.call(transaction);
365
+ assertSubmitResultRecognized(raw, contractName, method);
366
+ return raw;
367
+ } catch (error) {
368
+ if (error instanceof CaatingaError4) {
369
+ throw error;
370
+ }
371
+ throw new CaatingaError4(
372
+ `Failed to submit XDR for "${contractName}.${method}".`,
373
+ CaatingaErrorCode4.XDR_SUBMIT_FAILED,
374
+ `RPC: ${rpcUrl}. Check wallet signature and RPC connectivity.`,
375
+ error
376
+ );
377
+ }
378
+ }
379
+ throw new CaatingaError4(
380
+ `Binding transaction for "${contractName}.${method}" cannot be submitted.`,
381
+ CaatingaErrorCode4.XDR_SUBMIT_FAILED,
382
+ "Regenerate bindings or provide a compatible binding adapter."
383
+ );
384
+ }
385
+ async function prepareReadTransaction(transaction, contractName, method, rpcUrl) {
386
+ const candidate = transaction;
387
+ if (typeof candidate.prepare !== "function") {
388
+ return transaction;
245
389
  }
246
390
  try {
247
- const raw = await submit.call(transaction, { signedXdr });
248
- assertSubmitResultRecognized(raw, contractName, method);
249
- return raw;
391
+ return await candidate.prepare.call(transaction);
250
392
  } catch (error) {
251
393
  if (error instanceof CaatingaError4) {
252
394
  throw error;
253
395
  }
254
396
  throw new CaatingaError4(
255
- `Failed to submit XDR for "${contractName}.${method}".`,
256
- CaatingaErrorCode4.XDR_SUBMIT_FAILED,
257
- "Check wallet signature and RPC connectivity.",
397
+ `Failed to prepare XDR for "${contractName}.${method}".`,
398
+ CaatingaErrorCode4.XDR_PREPARE_FAILED,
399
+ `RPC: ${rpcUrl}. Check connectivity, simulation errors, and binding compatibility.`,
258
400
  error
259
401
  );
260
402
  }
261
403
  }
404
+ function readSimulationResult(raw, contractName, method) {
405
+ if (raw !== null && typeof raw === "object" && "result" in raw) {
406
+ const result = raw.result;
407
+ if (result !== void 0) {
408
+ return result;
409
+ }
410
+ }
411
+ throw new CaatingaError4(
412
+ `Simulation for "${contractName}.${method}" did not return a result.`,
413
+ CaatingaErrorCode4.READ_RESULT_MISSING,
414
+ `Expected "${contractName}.${method}" to expose a simulation result. Use debugRaw to inspect the generated binding output.`
415
+ );
416
+ }
262
417
  function assertSubmitResultRecognized(raw, contractName, method) {
263
418
  if (raw === null || typeof raw !== "object") {
264
419
  return;
265
420
  }
266
421
  const record = raw;
267
- const hasTransactionId = "txHash" in record || "transactionHash" in record || "hash" in record;
422
+ const hasTransactionId = "txHash" in record || "transactionHash" in record || "hash" in record || hasNestedSendTransactionResponseHash(record);
268
423
  const hasResult = "result" in record;
269
424
  if (hasTransactionId || hasResult) {
270
425
  return;
@@ -272,13 +427,17 @@ function assertSubmitResultRecognized(raw, contractName, method) {
272
427
  throw new CaatingaError4(
273
428
  `Submit returned an unrecognized payload for "${contractName}.${method}".`,
274
429
  CaatingaErrorCode4.XDR_RESULT_FAILED,
275
- "Expected txHash, transactionHash, hash, or result on the submit response. Use debugRaw to inspect the binding output."
430
+ "Expected txHash, transactionHash, hash, sendTransactionResponse.hash, or result on the submit response. Use debugRaw to inspect the binding output."
276
431
  );
277
432
  }
433
+ function hasNestedSendTransactionResponseHash(record) {
434
+ const response = record.sendTransactionResponse;
435
+ return response !== null && typeof response === "object" && "hash" in response;
436
+ }
278
437
  function normalizeSubmitResult(raw) {
279
438
  const candidate = raw;
280
439
  return {
281
- transactionHash: candidate.txHash ?? candidate.transactionHash ?? candidate.hash,
440
+ transactionHash: candidate.txHash ?? candidate.transactionHash ?? candidate.hash ?? candidate.sendTransactionResponse?.hash,
282
441
  result: candidate.result
283
442
  };
284
443
  }
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/stellar-wallets-kit.ts
21
+ var stellar_wallets_kit_exports = {};
22
+ __export(stellar_wallets_kit_exports, {
23
+ createStellarWalletsKitAdapter: () => createStellarWalletsKitAdapter
24
+ });
25
+ module.exports = __toCommonJS(stellar_wallets_kit_exports);
26
+
27
+ // src/adapters/stellar-wallets-kit.ts
28
+ var import_stellar_wallets_kit = require("stellar-wallets-kit");
29
+ function createStellarWalletsKitAdapter(options = {}) {
30
+ const kit = options.kit ?? new import_stellar_wallets_kit.StellarWalletsKit({
31
+ network: options.network ?? import_stellar_wallets_kit.WalletNetwork.TESTNET,
32
+ selectedWallet: options.selectedWallet ?? import_stellar_wallets_kit.WalletType.XBULL
33
+ });
34
+ let publicKey;
35
+ let isWalletConnectStarted = false;
36
+ const sessionDeletedCallbacks = /* @__PURE__ */ new Set();
37
+ return {
38
+ kit,
39
+ async setWallet(wallet) {
40
+ await kit.setWallet(wallet);
41
+ publicKey = void 0;
42
+ },
43
+ async setNetwork(network) {
44
+ await kit.setNetwork(network);
45
+ publicKey = void 0;
46
+ },
47
+ async getPublicKey() {
48
+ publicKey = await kit.getPublicKey();
49
+ return publicKey;
50
+ },
51
+ async signTransaction({ xdr, networkPassphrase }) {
52
+ const walletNetwork = resolveWalletNetwork(networkPassphrase);
53
+ if (walletNetwork) {
54
+ await kit.setNetwork(walletNetwork);
55
+ }
56
+ const signer = publicKey ?? await kit.getPublicKey();
57
+ publicKey = signer;
58
+ const response = await kit.sign({ xdr, publicKey: signer });
59
+ return response.signedXDR;
60
+ },
61
+ async startWalletConnect(metadata = options.walletConnectMetadata) {
62
+ if (!metadata) {
63
+ throw new Error("WalletConnect metadata is required before starting WalletConnect.");
64
+ }
65
+ await kit.startWalletConnect(metadata);
66
+ isWalletConnectStarted = true;
67
+ for (const callback of sessionDeletedCallbacks) {
68
+ kit.onSessionDeleted((sessionId) => {
69
+ publicKey = void 0;
70
+ callback(sessionId);
71
+ });
72
+ }
73
+ },
74
+ async connectWalletConnect(connectOptions) {
75
+ await kit.connectWalletConnect(connectOptions);
76
+ publicKey = void 0;
77
+ },
78
+ async getWalletConnectSessions() {
79
+ return kit.getSessions();
80
+ },
81
+ setWalletConnectSession(sessionId) {
82
+ kit.setSession(sessionId);
83
+ publicKey = void 0;
84
+ },
85
+ onWalletConnectSessionDeleted(callback) {
86
+ sessionDeletedCallbacks.add(callback);
87
+ if (!isWalletConnectStarted) {
88
+ return;
89
+ }
90
+ kit.onSessionDeleted((sessionId) => {
91
+ publicKey = void 0;
92
+ callback(sessionId);
93
+ });
94
+ }
95
+ };
96
+ }
97
+ function resolveWalletNetwork(networkPassphrase) {
98
+ if (networkPassphrase === "Test SDF Network ; September 2015") {
99
+ return import_stellar_wallets_kit.WalletNetwork.TESTNET;
100
+ }
101
+ if (networkPassphrase === "Public Global Stellar Network ; September 2015") {
102
+ return import_stellar_wallets_kit.WalletNetwork.PUBLIC;
103
+ }
104
+ return void 0;
105
+ }
106
+ // Annotate the CommonJS export names for ESM import in node:
107
+ 0 && (module.exports = {
108
+ createStellarWalletsKitAdapter
109
+ });
@@ -0,0 +1,46 @@
1
+ import { StellarWalletsKit, WalletType, WalletNetwork, IConnectWalletConnectParams } from 'stellar-wallets-kit';
2
+ import { C as CaatingaWalletAdapter } from './types-D4XEyX4J.cjs';
3
+ import '@caatinga/core/browser';
4
+
5
+ interface StellarWalletsKitMetadata {
6
+ name: string;
7
+ description: string;
8
+ url: string;
9
+ icons: string[];
10
+ projectId: string;
11
+ }
12
+ interface StellarWalletsKitAdapterOptions {
13
+ kit?: StellarWalletsKit;
14
+ network?: WalletNetwork;
15
+ selectedWallet?: WalletType;
16
+ walletConnectMetadata?: StellarWalletsKitMetadata;
17
+ }
18
+ interface StellarWalletsKitConnectOptions {
19
+ chains?: IConnectWalletConnectParams["chains"];
20
+ methods?: IConnectWalletConnectParams["methods"];
21
+ pairingTopic?: IConnectWalletConnectParams["pairingTopic"];
22
+ }
23
+ interface StellarWalletsKitSession {
24
+ id: string;
25
+ name: string;
26
+ description: string;
27
+ url: string;
28
+ icons: string;
29
+ accounts: Array<{
30
+ network: "pubnet" | "testnet";
31
+ publicKey: string;
32
+ }>;
33
+ }
34
+ interface StellarWalletsKitAdapter extends CaatingaWalletAdapter {
35
+ kit: StellarWalletsKit;
36
+ setWallet(wallet: WalletType): Promise<void>;
37
+ setNetwork(network: WalletNetwork): Promise<void>;
38
+ startWalletConnect(metadata?: StellarWalletsKitMetadata): Promise<void>;
39
+ connectWalletConnect(options?: StellarWalletsKitConnectOptions): Promise<void>;
40
+ getWalletConnectSessions(): Promise<StellarWalletsKitSession[]>;
41
+ setWalletConnectSession(sessionId: string): void;
42
+ onWalletConnectSessionDeleted(callback: (sessionId: string) => void): void;
43
+ }
44
+ declare function createStellarWalletsKitAdapter(options?: StellarWalletsKitAdapterOptions): StellarWalletsKitAdapter;
45
+
46
+ export { type StellarWalletsKitAdapter, type StellarWalletsKitAdapterOptions, type StellarWalletsKitConnectOptions, type StellarWalletsKitMetadata, type StellarWalletsKitSession, createStellarWalletsKitAdapter };
@@ -0,0 +1,46 @@
1
+ import { StellarWalletsKit, WalletType, WalletNetwork, IConnectWalletConnectParams } from 'stellar-wallets-kit';
2
+ import { C as CaatingaWalletAdapter } from './types-D4XEyX4J.js';
3
+ import '@caatinga/core/browser';
4
+
5
+ interface StellarWalletsKitMetadata {
6
+ name: string;
7
+ description: string;
8
+ url: string;
9
+ icons: string[];
10
+ projectId: string;
11
+ }
12
+ interface StellarWalletsKitAdapterOptions {
13
+ kit?: StellarWalletsKit;
14
+ network?: WalletNetwork;
15
+ selectedWallet?: WalletType;
16
+ walletConnectMetadata?: StellarWalletsKitMetadata;
17
+ }
18
+ interface StellarWalletsKitConnectOptions {
19
+ chains?: IConnectWalletConnectParams["chains"];
20
+ methods?: IConnectWalletConnectParams["methods"];
21
+ pairingTopic?: IConnectWalletConnectParams["pairingTopic"];
22
+ }
23
+ interface StellarWalletsKitSession {
24
+ id: string;
25
+ name: string;
26
+ description: string;
27
+ url: string;
28
+ icons: string;
29
+ accounts: Array<{
30
+ network: "pubnet" | "testnet";
31
+ publicKey: string;
32
+ }>;
33
+ }
34
+ interface StellarWalletsKitAdapter extends CaatingaWalletAdapter {
35
+ kit: StellarWalletsKit;
36
+ setWallet(wallet: WalletType): Promise<void>;
37
+ setNetwork(network: WalletNetwork): Promise<void>;
38
+ startWalletConnect(metadata?: StellarWalletsKitMetadata): Promise<void>;
39
+ connectWalletConnect(options?: StellarWalletsKitConnectOptions): Promise<void>;
40
+ getWalletConnectSessions(): Promise<StellarWalletsKitSession[]>;
41
+ setWalletConnectSession(sessionId: string): void;
42
+ onWalletConnectSessionDeleted(callback: (sessionId: string) => void): void;
43
+ }
44
+ declare function createStellarWalletsKitAdapter(options?: StellarWalletsKitAdapterOptions): StellarWalletsKitAdapter;
45
+
46
+ export { type StellarWalletsKitAdapter, type StellarWalletsKitAdapterOptions, type StellarWalletsKitConnectOptions, type StellarWalletsKitMetadata, type StellarWalletsKitSession, createStellarWalletsKitAdapter };
@@ -0,0 +1,86 @@
1
+ // src/adapters/stellar-wallets-kit.ts
2
+ import {
3
+ StellarWalletsKit,
4
+ WalletNetwork,
5
+ WalletType
6
+ } from "stellar-wallets-kit";
7
+ function createStellarWalletsKitAdapter(options = {}) {
8
+ const kit = options.kit ?? new StellarWalletsKit({
9
+ network: options.network ?? WalletNetwork.TESTNET,
10
+ selectedWallet: options.selectedWallet ?? WalletType.XBULL
11
+ });
12
+ let publicKey;
13
+ let isWalletConnectStarted = false;
14
+ const sessionDeletedCallbacks = /* @__PURE__ */ new Set();
15
+ return {
16
+ kit,
17
+ async setWallet(wallet) {
18
+ await kit.setWallet(wallet);
19
+ publicKey = void 0;
20
+ },
21
+ async setNetwork(network) {
22
+ await kit.setNetwork(network);
23
+ publicKey = void 0;
24
+ },
25
+ async getPublicKey() {
26
+ publicKey = await kit.getPublicKey();
27
+ return publicKey;
28
+ },
29
+ async signTransaction({ xdr, networkPassphrase }) {
30
+ const walletNetwork = resolveWalletNetwork(networkPassphrase);
31
+ if (walletNetwork) {
32
+ await kit.setNetwork(walletNetwork);
33
+ }
34
+ const signer = publicKey ?? await kit.getPublicKey();
35
+ publicKey = signer;
36
+ const response = await kit.sign({ xdr, publicKey: signer });
37
+ return response.signedXDR;
38
+ },
39
+ async startWalletConnect(metadata = options.walletConnectMetadata) {
40
+ if (!metadata) {
41
+ throw new Error("WalletConnect metadata is required before starting WalletConnect.");
42
+ }
43
+ await kit.startWalletConnect(metadata);
44
+ isWalletConnectStarted = true;
45
+ for (const callback of sessionDeletedCallbacks) {
46
+ kit.onSessionDeleted((sessionId) => {
47
+ publicKey = void 0;
48
+ callback(sessionId);
49
+ });
50
+ }
51
+ },
52
+ async connectWalletConnect(connectOptions) {
53
+ await kit.connectWalletConnect(connectOptions);
54
+ publicKey = void 0;
55
+ },
56
+ async getWalletConnectSessions() {
57
+ return kit.getSessions();
58
+ },
59
+ setWalletConnectSession(sessionId) {
60
+ kit.setSession(sessionId);
61
+ publicKey = void 0;
62
+ },
63
+ onWalletConnectSessionDeleted(callback) {
64
+ sessionDeletedCallbacks.add(callback);
65
+ if (!isWalletConnectStarted) {
66
+ return;
67
+ }
68
+ kit.onSessionDeleted((sessionId) => {
69
+ publicKey = void 0;
70
+ callback(sessionId);
71
+ });
72
+ }
73
+ };
74
+ }
75
+ function resolveWalletNetwork(networkPassphrase) {
76
+ if (networkPassphrase === "Test SDF Network ; September 2015") {
77
+ return WalletNetwork.TESTNET;
78
+ }
79
+ if (networkPassphrase === "Public Global Stellar Network ; September 2015") {
80
+ return WalletNetwork.PUBLIC;
81
+ }
82
+ return void 0;
83
+ }
84
+ export {
85
+ createStellarWalletsKitAdapter
86
+ };