@cosmosapp/pay_sdk 1.0.1 → 1.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.
package/dist/index.cjs CHANGED
@@ -315,6 +315,152 @@ var PaymentIntent = class extends Base {
315
315
  }
316
316
  };
317
317
 
318
+ // src/common/addresses.ts
319
+ var G_ADDRESS = /^G[A-Z2-7]{55}$/;
320
+ var M_ADDRESS = /^M[A-Z2-7]{68}$/;
321
+ var C_ADDRESS = /^C[A-Z2-7]{55}$/;
322
+ function isStellarAddress(value) {
323
+ return typeof value === "string" && G_ADDRESS.test(value);
324
+ }
325
+ function isMuxedAddress(value) {
326
+ return typeof value === "string" && M_ADDRESS.test(value);
327
+ }
328
+ function isContractAddress(value) {
329
+ return typeof value === "string" && C_ADDRESS.test(value);
330
+ }
331
+ function isAddress(value) {
332
+ return isStellarAddress(value) || isMuxedAddress(value) || isContractAddress(value);
333
+ }
334
+ function assertAddress(value, label = "address") {
335
+ if (!isAddress(value)) {
336
+ throw new TypeError(`Invalid Stellar ${label}: ${String(value)}`);
337
+ }
338
+ return value;
339
+ }
340
+ var AddressBook = class {
341
+ constructor(initial = {}) {
342
+ this.entries = /* @__PURE__ */ new Map();
343
+ for (const [name, address] of Object.entries(initial)) {
344
+ this.define(name, address);
345
+ }
346
+ }
347
+ /** Register (or overwrite) a named address. The address is validated. */
348
+ define(name, address) {
349
+ this.entries.set(name, assertAddress(address, `address for "${name}"`));
350
+ return this;
351
+ }
352
+ /** Whether a name is registered. */
353
+ has(name) {
354
+ return this.entries.has(name);
355
+ }
356
+ /** Look up a registered address by name. */
357
+ get(name) {
358
+ return this.entries.get(name);
359
+ }
360
+ /** Remove a named address. */
361
+ delete(name) {
362
+ return this.entries.delete(name);
363
+ }
364
+ /** All registered names. */
365
+ names() {
366
+ return [...this.entries.keys()];
367
+ }
368
+ /**
369
+ * Resolve a reference to a concrete address: a registered name is substituted,
370
+ * any other string is returned unchanged (so raw `G…`, muxed `M…`, federation
371
+ * `name*domain`, etc. all keep working).
372
+ */
373
+ resolve(ref) {
374
+ return this.entries.get(ref) ?? ref;
375
+ }
376
+ };
377
+ var addresses = new AddressBook();
378
+ function resolveAddress(ref, book = addresses) {
379
+ return book.resolve(ref);
380
+ }
381
+
382
+ // src/common/assets.ts
383
+ var NATIVE = { code: "XLM", issuer: null, name: "Stellar Lumens" };
384
+ var Assets = {
385
+ /** Native lumens. */
386
+ XLM: NATIVE,
387
+ /** USD Coin by Circle (mainnet). */
388
+ USDC: {
389
+ code: "USDC",
390
+ issuer: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
391
+ name: "USD Coin",
392
+ network: "public"
393
+ },
394
+ /** Euro Coin by Circle (mainnet). */
395
+ EURC: {
396
+ code: "EURC",
397
+ issuer: "GDHU6WRG4IEQXM5NZ4BMPKOXHW76MZM4Y2IEMFDVXBSDP6SJY4ITNPP2",
398
+ name: "Euro Coin",
399
+ network: "public"
400
+ }
401
+ };
402
+ var TestnetAssets = {
403
+ XLM: NATIVE,
404
+ USDC: {
405
+ code: "USDC",
406
+ issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
407
+ name: "USD Coin",
408
+ network: "testnet"
409
+ },
410
+ EURC: {
411
+ code: "EURC",
412
+ issuer: "GB3Q6QDZYTHWT7E5PVS3W7FUT5GVAFC5KSZFFLPU25GO7VTC3NM2ZTVO",
413
+ name: "Euro Coin",
414
+ network: "testnet"
415
+ }
416
+ };
417
+ function isNativeAsset(ref) {
418
+ if (typeof ref === "string") {
419
+ const code = ref.trim().toLowerCase();
420
+ return code === "" || code === "xlm" || code === "native";
421
+ }
422
+ return !ref.issuer || isNativeAsset(ref.code);
423
+ }
424
+ function defineAsset(def) {
425
+ if (!def.code) throw new TypeError("defineAsset requires a `code`.");
426
+ if (def.issuer != null && !isStellarAddress(def.issuer)) {
427
+ throw new TypeError(`defineAsset: invalid issuer for ${def.code}: ${def.issuer}`);
428
+ }
429
+ if (def.contract != null && !/^C[A-Z2-7]{55}$/.test(def.contract)) {
430
+ throw new TypeError(`defineAsset: invalid contract for ${def.code}: ${def.contract}`);
431
+ }
432
+ return Object.freeze({ ...def });
433
+ }
434
+ function resolveAsset(ref) {
435
+ if (isNativeAsset(ref)) return { code: "XLM", issuer: null };
436
+ if (typeof ref === "string") return { code: ref, issuer: null };
437
+ return {
438
+ code: ref.code,
439
+ issuer: ref.issuer ?? null,
440
+ ...ref.contract ? { contract: ref.contract } : {}
441
+ };
442
+ }
443
+
444
+ // src/common/intent-input.ts
445
+ function resolveIntentBody(options, book = addresses) {
446
+ const out = { ...options };
447
+ if (typeof out["destination"] === "string") {
448
+ out["destination"] = resolveAddress(out["destination"], book);
449
+ }
450
+ if (typeof out["source"] === "string") {
451
+ out["source"] = resolveAddress(out["source"], book);
452
+ }
453
+ if (out["asset"] != null) {
454
+ const { code, issuer } = resolveAsset(out["asset"]);
455
+ delete out["asset"];
456
+ if (issuer) {
457
+ out["assetCode"] ??= code;
458
+ out["assetIssuer"] ??= issuer;
459
+ }
460
+ }
461
+ return out;
462
+ }
463
+
318
464
  // src/managers/PaymentIntentManager.ts
319
465
  var PaymentIntentManager = class extends BaseManager {
320
466
  constructor() {
@@ -324,14 +470,14 @@ var PaymentIntentManager = class extends BaseManager {
324
470
  /** Create a SEP-7 `tx` intent (source known → unsigned XDR + tx URI + QR). */
325
471
  async createTx(options) {
326
472
  const data = await this.rest.post(`${this.route}/tx`, {
327
- body: options
473
+ body: resolveIntentBody(options)
328
474
  });
329
475
  return this._add(new PaymentIntent(this.client, data));
330
476
  }
331
477
  /** Create a SEP-7 `pay` intent (no source → pay URI + QR, no XDR). */
332
478
  async createPay(options) {
333
479
  const data = await this.rest.post(`${this.route}/pay`, {
334
- body: options
480
+ body: resolveIntentBody(options)
335
481
  });
336
482
  return this._add(new PaymentIntent(this.client, data));
337
483
  }
@@ -597,7 +743,7 @@ var WebhookEndpoint = class extends Base {
597
743
  };
598
744
 
599
745
  // src/util/Constants.ts
600
- var version = "1.0.0";
746
+ var version = "1.1.0";
601
747
  var DEFAULT_BASE_URL = "https://api.cosmospay.lat/cosmos-api";
602
748
  var DEFAULT_VERSION = "v1";
603
749
  var DEFAULT_TIMEOUT = 3e4;
@@ -1168,7 +1314,18 @@ var Client = class extends events.EventEmitter {
1168
1314
  }
1169
1315
  };
1170
1316
 
1317
+ // src/common/wallets.ts
1318
+ var Wallets = {
1319
+ FREIGHTER: "freighter",
1320
+ XBULL: "xbull",
1321
+ RABET: "rabet",
1322
+ LOBSTR: "lobstr",
1323
+ ALBEDO: "albedo"
1324
+ };
1325
+
1326
+ exports.AddressBook = AddressBook;
1171
1327
  exports.AnalyticsManager = AnalyticsManager;
1328
+ exports.Assets = Assets;
1172
1329
  exports.Base = Base;
1173
1330
  exports.BaseManager = BaseManager;
1174
1331
  exports.Client = Client;
@@ -1191,6 +1348,8 @@ exports.ProductKind = ProductKind;
1191
1348
  exports.ProductManager = ProductManager;
1192
1349
  exports.REST = REST;
1193
1350
  exports.SIGNATURE_HEADER = SIGNATURE_HEADER;
1351
+ exports.TestnetAssets = TestnetAssets;
1352
+ exports.Wallets = Wallets;
1194
1353
  exports.WebhookDelivery = WebhookDelivery;
1195
1354
  exports.WebhookDeliveryStatus = WebhookDeliveryStatus;
1196
1355
  exports.WebhookEndpoint = WebhookEndpoint;
@@ -1198,6 +1357,17 @@ exports.WebhookEventType = WebhookEventType;
1198
1357
  exports.WebhookManager = WebhookManager;
1199
1358
  exports.WebhookSignatureError = WebhookSignatureError;
1200
1359
  exports.Webhooks = Webhooks;
1360
+ exports.addresses = addresses;
1361
+ exports.assertAddress = assertAddress;
1362
+ exports.defineAsset = defineAsset;
1363
+ exports.isAddress = isAddress;
1364
+ exports.isContractAddress = isContractAddress;
1365
+ exports.isMuxedAddress = isMuxedAddress;
1366
+ exports.isNativeAsset = isNativeAsset;
1367
+ exports.isStellarAddress = isStellarAddress;
1368
+ exports.resolveAddress = resolveAddress;
1369
+ exports.resolveAsset = resolveAsset;
1370
+ exports.resolveIntentBody = resolveIntentBody;
1201
1371
  exports.resolveShared = resolveShared;
1202
1372
  exports.shared = shared;
1203
1373
  exports.version = version;