@cosmosapp/pay_sdk 1.2.1 → 2.1.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.
@@ -0,0 +1,106 @@
1
+ import { injectedProvider, resolveProvider } from '../chunk-AYVWYEOK.js';
2
+ export { injectedProvider, loadHostedProvider, resetProviderCache, resolveProvider } from '../chunk-AYVWYEOK.js';
3
+
4
+ // src/swk/types.ts
5
+ var SwkModuleType = {
6
+ HW_WALLET: "HW_WALLET",
7
+ HOT_WALLET: "HOT_WALLET",
8
+ BRIDGE_WALLET: "BRIDGE_WALLET",
9
+ AIR_GAPED_WALLET: "AIR_GAPED_WALLET"
10
+ };
11
+
12
+ // src/swk/CosmosWalletModule.ts
13
+ var COSMOS_WALLET_ID = "cosmos-wallet";
14
+ var PRODUCT_NAME = "Cosmos Wallet";
15
+ var PRODUCT_URL = "https://github.com/CosmosPay/CosmosPay-Wallet";
16
+ var PRODUCT_ICON = "https://raw.githubusercontent.com/CosmosPay/CosmosPay-Wallet/main/public/logo-512.png";
17
+ var NOT_SUPPORTED = -3;
18
+ var CosmosWalletModule = class {
19
+ constructor(options = {}) {
20
+ this.productId = COSMOS_WALLET_ID;
21
+ this.moduleType = options.moduleType ?? SwkModuleType.HOT_WALLET;
22
+ this.productName = options.productName ?? PRODUCT_NAME;
23
+ this.productUrl = options.productUrl ?? PRODUCT_URL;
24
+ this.productIcon = options.productIcon ?? PRODUCT_ICON;
25
+ this.options = {
26
+ provider: options.provider,
27
+ walletUrl: options.walletUrl,
28
+ loadTimeoutMs: options.loadTimeoutMs
29
+ };
30
+ }
31
+ /**
32
+ * The kit allows 1000ms for this and shows the wallet as unavailable otherwise,
33
+ * so it answers from what is already in the page: an injected provider, or a
34
+ * configured hosted wallet — which needs no install and is therefore always
35
+ * usable. The hosted script itself is fetched on the first real call.
36
+ */
37
+ async isAvailable() {
38
+ if (this.options.provider) return true;
39
+ if (injectedProvider()) return true;
40
+ return !!this.options.walletUrl;
41
+ }
42
+ async provider() {
43
+ const provider = await resolveProvider(this.options);
44
+ if (!provider) throw kitError(`${this.productName} is not available.`, -1);
45
+ return provider;
46
+ }
47
+ /**
48
+ * `skipRequestAccess` is accepted and ignored on purpose: the wallet decides
49
+ * whether an origin it already knows needs to be asked again, and a dapp asking
50
+ * to skip the prompt is not the party that gets to decide that.
51
+ */
52
+ async getAddress(_params) {
53
+ const provider = await this.provider();
54
+ const { address } = await provider.getAddress();
55
+ if (!address) throw kitError("The wallet returned no address.", -1);
56
+ return { address };
57
+ }
58
+ async signTransaction(xdr, opts) {
59
+ const provider = await this.provider();
60
+ const result = await provider.signTransaction(xdr, {
61
+ networkPassphrase: opts?.networkPassphrase,
62
+ address: opts?.address
63
+ });
64
+ if (!result?.signedTxXdr) throw kitError("The wallet returned no signed transaction.", -1);
65
+ return { signedTxXdr: result.signedTxXdr, signerAddress: result.signerAddress };
66
+ }
67
+ async signMessage(message, opts) {
68
+ const provider = await this.provider();
69
+ const result = await provider.signMessage(message, {
70
+ networkPassphrase: opts?.networkPassphrase,
71
+ address: opts?.address
72
+ });
73
+ if (!result?.signedMessage) throw kitError("The wallet returned no signature.", -1);
74
+ return { signedMessage: result.signedMessage, signerAddress: result.signerAddress };
75
+ }
76
+ /**
77
+ * Not supported, and reported as such in the kit's own vocabulary.
78
+ *
79
+ * Cosmos Wallet has no Soroban authorization-entry signing. Anything this could
80
+ * return instead of an error would be a signature the network rejects, discovered
81
+ * by the user rather than by the dapp.
82
+ */
83
+ signAuthEntry() {
84
+ return Promise.reject(
85
+ kitError(`${this.productName} does not support the "signAuthEntry" function`, NOT_SUPPORTED)
86
+ );
87
+ }
88
+ /**
89
+ * The network the WALLET is on, which is the only one it will sign for. A dapp
90
+ * that wants another network has to ask the user to switch, not ask the wallet to
91
+ * pretend.
92
+ */
93
+ async getNetwork() {
94
+ const provider = await this.provider();
95
+ const { network, networkPassphrase } = await provider.getNetwork();
96
+ if (!networkPassphrase) throw kitError("The wallet returned no network.", -1);
97
+ return { network, networkPassphrase };
98
+ }
99
+ };
100
+ function kitError(message, code) {
101
+ return Object.assign(new Error(message), { code, message });
102
+ }
103
+
104
+ export { COSMOS_WALLET_ID, CosmosWalletModule, SwkModuleType };
105
+ //# sourceMappingURL=index.js.map
106
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/swk/types.ts","../../src/swk/CosmosWalletModule.ts"],"names":[],"mappings":";;;;AAuBO,IAAM,aAAA,GAAgB;AAAA,EAC3B,SAAA,EAAW,WAAA;AAAA,EACX,UAAA,EAAY,YAAA;AAAA,EACZ,aAAA,EAAe,eAAA;AAAA,EACf,gBAAA,EAAkB;AACpB;;;ACoBO,IAAM,gBAAA,GAAmB;AAEhC,IAAM,YAAA,GAAe,eAAA;AACrB,IAAM,WAAA,GAAc,+CAAA;AAMpB,IAAM,YAAA,GACJ,uFAAA;AAGF,IAAM,aAAA,GAAgB,EAAA;AAiBf,IAAM,qBAAN,MAEP;AAAA,EASE,WAAA,CAAY,OAAA,GAAkD,EAAC,EAAG;AAPlE,IAAA,IAAA,CAAgB,SAAA,GAAoB,gBAAA;AAQlC,IAAA,IAAA,CAAK,UAAA,GAAc,OAAA,CAAQ,UAAA,IAAc,aAAA,CAAc,UAAA;AACvD,IAAA,IAAA,CAAK,WAAA,GAAc,QAAQ,WAAA,IAAe,YAAA;AAC1C,IAAA,IAAA,CAAK,UAAA,GAAa,QAAQ,UAAA,IAAc,WAAA;AACxC,IAAA,IAAA,CAAK,WAAA,GAAc,QAAQ,WAAA,IAAe,YAAA;AAC1C,IAAA,IAAA,CAAK,OAAA,GAAU;AAAA,MACb,UAAU,OAAA,CAAQ,QAAA;AAAA,MAClB,WAAW,OAAA,CAAQ,SAAA;AAAA,MACnB,eAAe,OAAA,CAAQ;AAAA,KACzB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAa,WAAA,GAAgC;AAC3C,IAAA,IAAI,IAAA,CAAK,OAAA,CAAQ,QAAA,EAAU,OAAO,IAAA;AAClC,IAAA,IAAI,gBAAA,IAAoB,OAAO,IAAA;AAC/B,IAAA,OAAO,CAAC,CAAC,IAAA,CAAK,OAAA,CAAQ,SAAA;AAAA,EACxB;AAAA,EAEA,MAAc,QAAA,GAA0C;AACtD,IAAA,MAAM,QAAA,GAAW,MAAM,eAAA,CAAgB,IAAA,CAAK,OAAO,CAAA;AACnD,IAAA,IAAI,CAAC,UAAU,MAAM,QAAA,CAAS,GAAG,IAAA,CAAK,WAAW,sBAAsB,EAAE,CAAA;AACzE,IAAA,OAAO,QAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAa,WAAW,OAAA,EAErB;AACD,IAAA,MAAM,QAAA,GAAW,MAAM,IAAA,CAAK,QAAA,EAAS;AACrC,IAAA,MAAM,EAAE,OAAA,EAAQ,GAAI,MAAM,SAAS,UAAA,EAAW;AAC9C,IAAA,IAAI,CAAC,OAAA,EAAS,MAAM,QAAA,CAAS,mCAAmC,EAAE,CAAA;AAClE,IAAA,OAAO,EAAE,OAAA,EAAQ;AAAA,EACnB;AAAA,EAEA,MAAa,eAAA,CACX,GAAA,EACA,IAAA,EAC0D;AAC1D,IAAA,MAAM,QAAA,GAAW,MAAM,IAAA,CAAK,QAAA,EAAS;AAKrC,IAAA,MAAM,MAAA,GAAS,MAAM,QAAA,CAAS,eAAA,CAAgB,GAAA,EAAK;AAAA,MACjD,mBAAmB,IAAA,EAAM,iBAAA;AAAA,MACzB,SAAS,IAAA,EAAM;AAAA,KAChB,CAAA;AACD,IAAA,IAAI,CAAC,MAAA,EAAQ,WAAA,EAAa,MAAM,QAAA,CAAS,8CAA8C,EAAE,CAAA;AACzF,IAAA,OAAO,EAAE,WAAA,EAAa,MAAA,CAAO,WAAA,EAAa,aAAA,EAAe,OAAO,aAAA,EAAc;AAAA,EAChF;AAAA,EAEA,MAAa,WAAA,CACX,OAAA,EACA,IAAA,EAC4D;AAC5D,IAAA,MAAM,QAAA,GAAW,MAAM,IAAA,CAAK,QAAA,EAAS;AACrC,IAAA,MAAM,MAAA,GAAS,MAAM,QAAA,CAAS,WAAA,CAAY,OAAA,EAAS;AAAA,MACjD,mBAAmB,IAAA,EAAM,iBAAA;AAAA,MACzB,SAAS,IAAA,EAAM;AAAA,KAChB,CAAA;AACD,IAAA,IAAI,CAAC,MAAA,EAAQ,aAAA,EAAe,MAAM,QAAA,CAAS,qCAAqC,EAAE,CAAA;AAClF,IAAA,OAAO,EAAE,aAAA,EAAe,MAAA,CAAO,aAAA,EAAe,aAAA,EAAe,OAAO,aAAA,EAAc;AAAA,EACpF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,aAAA,GAA8E;AACnF,IAAA,OAAO,OAAA,CAAQ,MAAA;AAAA,MACb,QAAA,CAAS,CAAA,EAAG,IAAA,CAAK,WAAW,kDAAkD,aAAa;AAAA,KAC7F;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAa,UAAA,GAAsE;AACjF,IAAA,MAAM,QAAA,GAAW,MAAM,IAAA,CAAK,QAAA,EAAS;AACrC,IAAA,MAAM,EAAE,OAAA,EAAS,iBAAA,EAAkB,GAAI,MAAM,SAAS,UAAA,EAAW;AACjE,IAAA,IAAI,CAAC,iBAAA,EAAmB,MAAM,QAAA,CAAS,mCAAmC,EAAE,CAAA;AAC5E,IAAA,OAAO,EAAE,SAAS,iBAAA,EAAkB;AAAA,EACtC;AACF;AAQA,SAAS,QAAA,CAAS,SAAiB,IAAA,EAAiC;AAClE,EAAA,OAAO,MAAA,CAAO,OAAO,IAAI,KAAA,CAAM,OAAO,CAAA,EAAG,EAAE,IAAA,EAAM,OAAA,EAAS,CAAA;AAC5D","file":"index.js","sourcesContent":["/**\n * The Stellar Wallets Kit module contract, transcribed.\n *\n * WHY A COPY INSTEAD OF AN IMPORT. A module is one small class; depending on the\n * whole kit to describe it would put a UI library (and, when this was measured, 22\n * dev advisories) into an SDK whose own dependency list is empty. The kit is what a\n * CONSUMER installs — this package only has to produce something that fits it.\n *\n * The shapes below are transcribed from `@creit.tech/stellar-wallets-kit@2.6`\n * (`esm/types/mod.d.ts`) and are structural, so a module written against them is\n * assignable to the kit's `ModuleInterface` without either side knowing about the\n * other. The one place structural typing does not reach is `moduleType`: the kit\n * declares it as a string ENUM, and TypeScript does not accept a plain string\n * literal for one. `CosmosWalletModule` is therefore generic in that property —\n * pass the kit's own `ModuleType.HOT_WALLET` and it type-checks exactly; pass\n * nothing and you get the same string at runtime.\n *\n * Keep this file in step with the kit when it moves. `signAndSubmitTransaction` and\n * `isPlatformWrapper` are deliberately absent below: they are optional in the kit\n * and Cosmos Wallet implements neither.\n */\n\n/** `moduleType` values the kit filters and groups by. */\nexport const SwkModuleType = {\n HW_WALLET: 'HW_WALLET',\n HOT_WALLET: 'HOT_WALLET',\n BRIDGE_WALLET: 'BRIDGE_WALLET',\n AIR_GAPED_WALLET: 'AIR_GAPED_WALLET',\n} as const;\n\nexport type SwkModuleTypeValue = (typeof SwkModuleType)[keyof typeof SwkModuleType];\n\n/**\n * What a module rejects with. The kit's `parseError` reads `code`, `message` and\n * `ext` off whatever it catches, so a plain object is the native shape — and `-3`\n * is the code its own modules use for \"this wallet cannot do that\".\n */\nexport interface IKitError {\n code: number;\n message: string;\n ext?: string;\n}\n\n/** Payload of the optional `onChange` subscription. */\nexport interface IOnChangeEvent {\n address: string;\n /** Human-readable network name, as the wallet calls it. */\n network: string;\n networkPassphrase: string;\n error?: IKitError;\n}\n\n/** Options every signing method accepts. `path` is for hardware wallets. */\nexport interface SwkSignOptions {\n networkPassphrase?: string;\n address?: string;\n path?: string;\n}\n\n/**\n * The kit's `ModuleInterface`, minus the optional members Cosmos Wallet does not\n * implement. `TModuleType` exists only so the kit's enum can flow through — see\n * the header.\n */\nexport interface SwkModule<TModuleType extends string = SwkModuleTypeValue> {\n moduleType: TModuleType;\n productId: string;\n productName: string;\n productUrl: string;\n productIcon: string;\n /** Must answer in under 1000ms or the kit renders the wallet as unavailable. */\n isAvailable(): Promise<boolean>;\n getAddress(params?: { path?: string; skipRequestAccess?: boolean }): Promise<{ address: string }>;\n signTransaction(\n xdr: string,\n opts?: SwkSignOptions,\n ): Promise<{ signedTxXdr: string; signerAddress?: string }>;\n signAuthEntry(\n authEntry: string,\n opts?: SwkSignOptions,\n ): Promise<{ signedAuthEntry: string; signerAddress?: string }>;\n signMessage(\n message: string,\n opts?: SwkSignOptions,\n ): Promise<{ signedMessage: string; signerAddress?: string }>;\n getNetwork(): Promise<{ network: string; networkPassphrase: string }>;\n onChange?(callback: (event: IOnChangeEvent) => void): void;\n disconnect?(): Promise<void>;\n}\n","/**\n * Cosmos Wallet as a Stellar Wallets Kit module.\n *\n * The kit is how most Stellar dapps ask for a signature: they register a list of\n * modules and it renders one button per wallet. A wallet that is not a module is a\n * wallet those dapps cannot see, however good its provider is — which is the whole\n * reason this file exists. Registering it:\n *\n * ```ts\n * import { StellarWalletsKit, WalletNetwork, ModuleType } from '@creit.tech/stellar-wallets-kit';\n * import { CosmosWalletModule, COSMOS_WALLET_ID } from '@cosmosapp/pay_sdk/swk';\n *\n * const kit = new StellarWalletsKit({\n * network: WalletNetwork.PUBLIC,\n * selectedWalletId: COSMOS_WALLET_ID,\n * modules: [\n * // `moduleType` is the kit's own enum — passing it here keeps the module\n * // exactly assignable to `ModuleInterface` with no cast anywhere.\n * new CosmosWalletModule({ moduleType: ModuleType.HOT_WALLET }),\n * ],\n * });\n * ```\n *\n * TWO TRANSPORTS, ONE MODULE. With the extension installed the provider is already\n * on the page. Without it, pass `walletUrl` and the hosted wallet's provider script\n * is fetched from that origin on first use, so a dapp can offer Cosmos Wallet to\n * users who have installed nothing:\n *\n * ```ts\n * new CosmosWalletModule({ walletUrl: 'https://wallet.cosmospay.lat' })\n * ```\n *\n * WHAT IT REFUSES, AND WHY THAT IS THE POINT. `signAuthEntry` rejects with the\n * kit's own `-3` \"not supported\" shape rather than returning something. Cosmos\n * Wallet signs transactions and domain-separated messages; it does not sign Soroban\n * authorization entries, and a module that faked one would hand a dapp a signature\n * that fails on chain instead of an error it can render.\n */\n\nimport {\n injectedProvider,\n resolveProvider,\n type CosmosProviderOptions,\n type CosmosWalletProvider,\n} from '@/web/cosmosProvider';\nimport { SwkModuleType, type IKitError, type SwkModule, type SwkSignOptions } from '@/swk/types';\n\n/** The id a dapp filters or preselects this wallet by. */\nexport const COSMOS_WALLET_ID = 'cosmos-wallet';\n\nconst PRODUCT_NAME = 'Cosmos Wallet';\nconst PRODUCT_URL = 'https://github.com/CosmosPay/CosmosPay-Wallet';\n/**\n * Served from the wallet's repository so the module has nothing to host. When this\n * module is upstreamed the kit serves its own copy from `stellar.creit.tech`, which\n * is why the URL is an option rather than a constant baked into every call site.\n */\nconst PRODUCT_ICON =\n 'https://raw.githubusercontent.com/CosmosPay/CosmosPay-Wallet/main/public/logo-512.png';\n\n/** The kit's code for \"this wallet cannot do that\" (see its own modules). */\nconst NOT_SUPPORTED = -3;\n\nexport interface CosmosWalletModuleOptions<TModuleType extends string = string>\n extends CosmosProviderOptions {\n /**\n * Pass the kit's `ModuleType.HOT_WALLET` for exact typing. Defaults to the same\n * string, which is what the enum holds at runtime.\n */\n moduleType?: TModuleType;\n /** Override the name shown in the kit's modal. */\n productName?: string;\n /** Override the \"get this wallet\" link. */\n productUrl?: string;\n /** Override the icon shown in the modal. */\n productIcon?: string;\n}\n\nexport class CosmosWalletModule<TModuleType extends string = string>\n implements SwkModule<TModuleType>\n{\n public readonly moduleType: TModuleType;\n public readonly productId: string = COSMOS_WALLET_ID;\n public readonly productName: string;\n public readonly productUrl: string;\n public readonly productIcon: string;\n\n private readonly options: CosmosProviderOptions;\n\n constructor(options: CosmosWalletModuleOptions<TModuleType> = {}) {\n this.moduleType = (options.moduleType ?? SwkModuleType.HOT_WALLET) as TModuleType;\n this.productName = options.productName ?? PRODUCT_NAME;\n this.productUrl = options.productUrl ?? PRODUCT_URL;\n this.productIcon = options.productIcon ?? PRODUCT_ICON;\n this.options = {\n provider: options.provider,\n walletUrl: options.walletUrl,\n loadTimeoutMs: options.loadTimeoutMs,\n };\n }\n\n /**\n * The kit allows 1000ms for this and shows the wallet as unavailable otherwise,\n * so it answers from what is already in the page: an injected provider, or a\n * configured hosted wallet — which needs no install and is therefore always\n * usable. The hosted script itself is fetched on the first real call.\n */\n public async isAvailable(): Promise<boolean> {\n if (this.options.provider) return true;\n if (injectedProvider()) return true;\n return !!this.options.walletUrl;\n }\n\n private async provider(): Promise<CosmosWalletProvider> {\n const provider = await resolveProvider(this.options);\n if (!provider) throw kitError(`${this.productName} is not available.`, -1);\n return provider;\n }\n\n /**\n * `skipRequestAccess` is accepted and ignored on purpose: the wallet decides\n * whether an origin it already knows needs to be asked again, and a dapp asking\n * to skip the prompt is not the party that gets to decide that.\n */\n public async getAddress(_params?: { path?: string; skipRequestAccess?: boolean }): Promise<{\n address: string;\n }> {\n const provider = await this.provider();\n const { address } = await provider.getAddress();\n if (!address) throw kitError('The wallet returned no address.', -1);\n return { address };\n }\n\n public async signTransaction(\n xdr: string,\n opts?: SwkSignOptions,\n ): Promise<{ signedTxXdr: string; signerAddress?: string }> {\n const provider = await this.provider();\n // The passphrase is forwarded, not authoritative: Cosmos Wallet parses with its\n // own network configuration and REFUSES a mismatch, so a caller cannot redirect\n // a signature to another network by naming one. Forwarding it is what turns a\n // wrong-network request into an error the dapp can show.\n const result = await provider.signTransaction(xdr, {\n networkPassphrase: opts?.networkPassphrase,\n address: opts?.address,\n });\n if (!result?.signedTxXdr) throw kitError('The wallet returned no signed transaction.', -1);\n return { signedTxXdr: result.signedTxXdr, signerAddress: result.signerAddress };\n }\n\n public async signMessage(\n message: string,\n opts?: SwkSignOptions,\n ): Promise<{ signedMessage: string; signerAddress?: string }> {\n const provider = await this.provider();\n const result = await provider.signMessage(message, {\n networkPassphrase: opts?.networkPassphrase,\n address: opts?.address,\n });\n if (!result?.signedMessage) throw kitError('The wallet returned no signature.', -1);\n return { signedMessage: result.signedMessage, signerAddress: result.signerAddress };\n }\n\n /**\n * Not supported, and reported as such in the kit's own vocabulary.\n *\n * Cosmos Wallet has no Soroban authorization-entry signing. Anything this could\n * return instead of an error would be a signature the network rejects, discovered\n * by the user rather than by the dapp.\n */\n public signAuthEntry(): Promise<{ signedAuthEntry: string; signerAddress?: string }> {\n return Promise.reject(\n kitError(`${this.productName} does not support the \"signAuthEntry\" function`, NOT_SUPPORTED),\n );\n }\n\n /**\n * The network the WALLET is on, which is the only one it will sign for. A dapp\n * that wants another network has to ask the user to switch, not ask the wallet to\n * pretend.\n */\n public async getNetwork(): Promise<{ network: string; networkPassphrase: string }> {\n const provider = await this.provider();\n const { network, networkPassphrase } = await provider.getNetwork();\n if (!networkPassphrase) throw kitError('The wallet returned no network.', -1);\n return { network, networkPassphrase };\n }\n}\n\n/**\n * The kit reads `code`, `message` and `ext` off whatever a module rejects with, and\n * its own modules reject with a plain object. This keeps the `Error` prototype (so\n * a stack survives and `instanceof Error` holds for anyone else who catches it)\n * while carrying the fields the kit looks for.\n */\nfunction kitError(message: string, code: number): Error & IKitError {\n return Object.assign(new Error(message), { code, message });\n}\n"]}
@@ -461,6 +461,120 @@ var AlbedoAdapter = class {
461
461
  }
462
462
  };
463
463
 
464
+ // src/web/cosmosProvider.ts
465
+ var PROVIDER_FILE = "cosmos-wallet.js";
466
+ var DEFAULT_LOAD_TIMEOUT_MS = 15e3;
467
+ function browserWindow() {
468
+ return typeof window !== "undefined" ? window : void 0;
469
+ }
470
+ function injectedProvider() {
471
+ const win = browserWindow();
472
+ const provider = win?.["cosmosWallet"];
473
+ return provider && typeof provider === "object" ? provider : void 0;
474
+ }
475
+ var loads = /* @__PURE__ */ new Map();
476
+ function loadHostedProvider(walletUrl, timeoutMs = DEFAULT_LOAD_TIMEOUT_MS) {
477
+ const existing = injectedProvider();
478
+ if (existing) return Promise.resolve(existing);
479
+ const win = browserWindow();
480
+ if (!win || typeof document === "undefined") return Promise.resolve(void 0);
481
+ const origin = walletUrl.replace(/\/+$/, "");
482
+ const cached = loads.get(origin);
483
+ if (cached) return cached;
484
+ const pending = new Promise((resolve) => {
485
+ const src = `${origin}/${PROVIDER_FILE}`;
486
+ const done = (value) => {
487
+ clearTimeout(timer);
488
+ resolve(value);
489
+ };
490
+ const timer = setTimeout(() => done(injectedProvider()), timeoutMs);
491
+ const existingTag = document.querySelector(`script[src="${src}"]`);
492
+ if (existingTag) {
493
+ const found = injectedProvider();
494
+ if (found) return done(found);
495
+ existingTag.addEventListener("load", () => done(injectedProvider()), { once: true });
496
+ existingTag.addEventListener("error", () => done(void 0), { once: true });
497
+ return;
498
+ }
499
+ const tag = document.createElement("script");
500
+ tag.src = src;
501
+ tag.async = true;
502
+ tag.addEventListener("load", () => done(injectedProvider()), { once: true });
503
+ tag.addEventListener("error", () => done(void 0), { once: true });
504
+ (document.head ?? document.documentElement).appendChild(tag);
505
+ });
506
+ loads.set(origin, pending);
507
+ void pending.then((provider) => {
508
+ if (!provider) loads.delete(origin);
509
+ });
510
+ return pending;
511
+ }
512
+ async function resolveProvider(options = {}) {
513
+ if (options.provider) return options.provider;
514
+ const injected = injectedProvider();
515
+ if (injected) return injected;
516
+ if (!options.walletUrl) return void 0;
517
+ return loadHostedProvider(options.walletUrl, options.loadTimeoutMs);
518
+ }
519
+ function resetProviderCache() {
520
+ loads.clear();
521
+ }
522
+
523
+ // src/web/adapters/CosmosWalletAdapter.ts
524
+ var CosmosWalletAdapter = class {
525
+ /**
526
+ * @param options - A provider to use directly, or the origin of a hosted Cosmos
527
+ * Wallet to fall back to. Both optional: with the extension installed there is
528
+ * nothing to configure.
529
+ */
530
+ constructor(options = {}) {
531
+ this.id = "cosmos";
532
+ this.name = "Cosmos Wallet";
533
+ this.options = isProvider(options) ? { provider: options } : options;
534
+ }
535
+ /**
536
+ * Available when a provider is already here, or when a hosted wallet is
537
+ * configured — the hosted build needs no install, so it is always usable.
538
+ *
539
+ * Deliberately does NOT wait for the hosted script: this answer gates a modal,
540
+ * and a wallet that takes a network round trip to say "yes" reads as broken. The
541
+ * script is fetched on the first real call instead.
542
+ */
543
+ async isAvailable() {
544
+ if (this.options.provider) return true;
545
+ if (injectedProvider()) return true;
546
+ return !!this.options.walletUrl;
547
+ }
548
+ async provider() {
549
+ const provider = await resolveProvider(this.options);
550
+ if (!provider) throw new WalletError(this.id, "Cosmos Wallet is not available.");
551
+ return provider;
552
+ }
553
+ async getPublicKey(_network) {
554
+ try {
555
+ const provider = await this.provider();
556
+ return unwrap(await provider.getAddress(), "address");
557
+ } catch (error) {
558
+ throw new WalletError(this.id, errorMessage(error), error);
559
+ }
560
+ }
561
+ async signTransaction(xdr, params) {
562
+ try {
563
+ const provider = await this.provider();
564
+ const result = await provider.signTransaction(xdr, {
565
+ networkPassphrase: params.networkPassphrase,
566
+ address: params.address
567
+ });
568
+ return unwrap(result, "signedTxXdr");
569
+ } catch (error) {
570
+ throw new WalletError(this.id, errorMessage(error), error);
571
+ }
572
+ }
573
+ };
574
+ function isProvider(value) {
575
+ return typeof value.signTransaction === "function";
576
+ }
577
+
464
578
  // src/web/adapters/index.ts
465
579
  function defaultAdapters(injected = {}) {
466
580
  return [
@@ -468,7 +582,13 @@ function defaultAdapters(injected = {}) {
468
582
  new XBullAdapter(injected.xBull),
469
583
  new RabetAdapter(injected.rabet),
470
584
  new LobstrAdapter(injected.lobstr),
471
- new AlbedoAdapter(injected.albedo)
585
+ new AlbedoAdapter(injected.albedo),
586
+ // Appended LAST on purpose. Auto-detection takes the first available adapter,
587
+ // so putting the first-party wallet at the front would silently move an
588
+ // existing integration off Freighter the day a user installs Cosmos Wallet.
589
+ // A dapp that wants it preferred says so — `pay({ wallet: Wallets.COSMOS })`,
590
+ // or `registry.register(adapter, true)`.
591
+ new CosmosWalletAdapter(injected.cosmos ?? {})
472
592
  ];
473
593
  }
474
594
 
@@ -829,7 +949,7 @@ var CosmosPayRequestError = class _CosmosPayRequestError extends Error {
829
949
  };
830
950
 
831
951
  // src/util/Constants.ts
832
- var version = "1.2.1";
952
+ var version = "2.1.0";
833
953
  var DEFAULT_BASE_URL = "https://api.cosmospay.lat/cosmos-api";
834
954
 
835
955
  // src/web/rest.ts
@@ -1205,6 +1325,8 @@ function extractHash(response) {
1205
1325
 
1206
1326
  // src/common/wallets.ts
1207
1327
  var Wallets = {
1328
+ /** Cosmos Pay's own wallet — extension, or the hosted build (see CosmosWalletAdapter). */
1329
+ COSMOS: "cosmos",
1208
1330
  FREIGHTER: "freighter",
1209
1331
  XBULL: "xbull",
1210
1332
  RABET: "rabet",
@@ -1218,6 +1340,7 @@ exports.Assets = Assets;
1218
1340
  exports.CosmosPayAPIError = CosmosPayAPIError;
1219
1341
  exports.CosmosPayRequestError = CosmosPayRequestError;
1220
1342
  exports.CosmosPayWebError = CosmosPayWebError;
1343
+ exports.CosmosWalletAdapter = CosmosWalletAdapter;
1221
1344
  exports.FreighterAdapter = FreighterAdapter;
1222
1345
  exports.HORIZON_URLS = HORIZON_URLS;
1223
1346
  exports.IntentError = IntentError;
@@ -1239,6 +1362,7 @@ exports.buildPaymentXdr = buildPaymentXdr;
1239
1362
  exports.classifyNetwork = classifyNetwork;
1240
1363
  exports.defaultAdapters = defaultAdapters;
1241
1364
  exports.defineAsset = defineAsset;
1365
+ exports.injectedProvider = injectedProvider;
1242
1366
  exports.isAddress = isAddress;
1243
1367
  exports.isContractAddress = isContractAddress;
1244
1368
  exports.isMuxedAddress = isMuxedAddress;
@@ -1246,14 +1370,17 @@ exports.isNativeAsset = isNativeAsset;
1246
1370
  exports.isPassphrase = isPassphrase;
1247
1371
  exports.isSep7Uri = isSep7Uri;
1248
1372
  exports.isStellarAddress = isStellarAddress;
1373
+ exports.loadHostedProvider = loadHostedProvider;
1249
1374
  exports.loadStellarSdk = loadStellarSdk;
1250
1375
  exports.normalizeIntent = normalizeIntent;
1251
1376
  exports.parseSep7 = parseSep7;
1252
1377
  exports.postToCallback = postToCallback;
1378
+ exports.resetProviderCache = resetProviderCache;
1253
1379
  exports.resolveAddress = resolveAddress;
1254
1380
  exports.resolveAsset = resolveAsset;
1255
1381
  exports.resolveIntentBody = resolveIntentBody;
1256
1382
  exports.resolveNetwork = resolveNetwork;
1383
+ exports.resolveProvider = resolveProvider;
1257
1384
  exports.submitToHorizon = submitToHorizon;
1258
1385
  exports.transactionHash = transactionHash;
1259
1386
  //# sourceMappingURL=index.cjs.map