@zkp2p/sdk 0.5.7 → 0.6.1

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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- export { TAKER_TIER_CAPS, TAKER_TIER_ORDER, TAKER_TIER_SCHEDULE, ZERO_RATE_MANAGER_ID, classifyDelegationState, getDelegationRoute, getNextTakerTier, isZeroRateManagerId, normalizeRateManagerId, normalizeRegistry } from './chunk-TGMRXUP2.mjs';
1
+ export { TAKER_TIER_CAPS, TAKER_TIER_FEE_DISCOUNT_BPS, TAKER_TIER_ORDER, TAKER_TIER_SCHEDULE, ZERO_RATE_MANAGER_ID, classifyDelegationState, getDelegationRoute, getNextTakerTier, getTakerTierFeeDiscountBps, isZeroRateManagerId, normalizeRateManagerId, normalizeRegistry } from './chunk-6WAMIVRB.mjs';
2
2
  import { APIError, NetworkError, ValidationError } from './chunk-GHQK65J2.mjs';
3
3
  export { APIError, ContractError, ErrorCode, NetworkError, ValidationError, ZKP2PError } from './chunk-GHQK65J2.mjs';
4
4
  import { getContracts, getRateManagerContracts, getPaymentMethodsCatalog, resolvePaymentMethodHashFromCatalog, getGatingServiceAddress, parseBigIntLike, resolveFiatCurrencyBytes32, resolvePaymentMethodNameFromHash } from './chunk-NMIFJSZ3.mjs';
@@ -371,9 +371,11 @@ async function apiSignIntentV3(request, opts) {
371
371
  async () => {
372
372
  let res;
373
373
  try {
374
+ const headers2 = { "Content-Type": "application/json" };
375
+ if (opts.apiKey) headers2["x-api-key"] = opts.apiKey;
374
376
  res = await fetch(url, {
375
377
  method: "POST",
376
- headers: { "Content-Type": "application/json" },
378
+ headers: headers2,
377
379
  body: JSON.stringify(request)
378
380
  });
379
381
  } catch (error) {
@@ -404,9 +406,9 @@ async function apiSignIntentV3(request, opts) {
404
406
  referralFees
405
407
  };
406
408
  }
407
- function headers() {
408
- return { "Content-Type": "application/json" };
409
- }
409
+
410
+ // src/adapters/attestationTransport.ts
411
+ var DEFAULT_ATTESTATION_PROXY_URL = "https://attestation-service-proxy.up.railway.app";
410
412
  function normalizeOptionalString(value) {
411
413
  if (typeof value !== "string") {
412
414
  return null;
@@ -414,13 +416,92 @@ function normalizeOptionalString(value) {
414
416
  const normalized = value.trim();
415
417
  return normalized.length > 0 ? normalized : null;
416
418
  }
417
- function normalizeBaseUrl(value, label) {
419
+ function normalizeAttestationBaseUrl(value, label) {
418
420
  const normalized = normalizeOptionalString(value)?.replace(/\/+$/u, "");
419
421
  if (!normalized) {
420
422
  throw new Error(`${label} is required.`);
421
423
  }
422
424
  return normalized;
423
425
  }
426
+ function resolveFallbackUrls(primaryBaseUrl, fallbackUrls) {
427
+ const rawFallbackUrls = fallbackUrls == null ? [DEFAULT_ATTESTATION_PROXY_URL] : fallbackUrls;
428
+ const normalized = rawFallbackUrls.map((url) => normalizeOptionalString(url)?.replace(/\/+$/u, "") ?? null).filter((url) => Boolean(url));
429
+ return Array.from(new Set(normalized)).filter((url) => url !== primaryBaseUrl);
430
+ }
431
+ function resolveFetch(requestFetch) {
432
+ if (requestFetch) {
433
+ return requestFetch;
434
+ }
435
+ if (typeof globalThis.fetch !== "function") {
436
+ throw new Error("fetch is not available in this runtime.");
437
+ }
438
+ return globalThis.fetch.bind(globalThis);
439
+ }
440
+ function inputUrl(input) {
441
+ try {
442
+ if (typeof input === "string" || input instanceof URL) {
443
+ return new URL(input);
444
+ }
445
+ if (typeof Request !== "undefined" && input instanceof Request) {
446
+ return new URL(input.url);
447
+ }
448
+ } catch {
449
+ return null;
450
+ }
451
+ return null;
452
+ }
453
+ function rewriteInputBaseUrl(input, primaryBaseUrl, targetBaseUrl) {
454
+ const url = inputUrl(input);
455
+ if (!url) {
456
+ return input;
457
+ }
458
+ const primaryUrl = new URL(primaryBaseUrl);
459
+ const targetUrl = new URL(targetBaseUrl);
460
+ if (url.origin === primaryUrl.origin) {
461
+ url.protocol = targetUrl.protocol;
462
+ url.host = targetUrl.host;
463
+ }
464
+ if (typeof Request !== "undefined" && input instanceof Request) {
465
+ return new Request(url.toString(), input.clone());
466
+ }
467
+ return url.toString();
468
+ }
469
+ function createAttestationTransport(primaryBaseUrl, options = {}) {
470
+ const requestFetch = resolveFetch(options.fetch);
471
+ const fallbackUrls = resolveFallbackUrls(primaryBaseUrl, options.fallbackUrls);
472
+ let activeBaseUrl = primaryBaseUrl;
473
+ const transportFetch = async (input, init) => {
474
+ const attemptInput = activeBaseUrl === primaryBaseUrl ? input : rewriteInputBaseUrl(input, primaryBaseUrl, activeBaseUrl);
475
+ try {
476
+ return await requestFetch(attemptInput, init);
477
+ } catch (primaryError) {
478
+ if (activeBaseUrl !== primaryBaseUrl || fallbackUrls.length === 0) {
479
+ throw primaryError;
480
+ }
481
+ let fallbackError = primaryError;
482
+ for (const fallbackUrl of fallbackUrls) {
483
+ try {
484
+ const fallbackInput = rewriteInputBaseUrl(input, primaryBaseUrl, fallbackUrl);
485
+ const response = await requestFetch(fallbackInput, init);
486
+ activeBaseUrl = fallbackUrl;
487
+ return response;
488
+ } catch (error) {
489
+ fallbackError = error;
490
+ }
491
+ }
492
+ throw fallbackError;
493
+ }
494
+ };
495
+ return {
496
+ fetch: transportFetch,
497
+ getActiveBaseUrl: () => activeBaseUrl
498
+ };
499
+ }
500
+
501
+ // src/adapters/attestation.ts
502
+ function headers() {
503
+ return { "Content-Type": "application/json" };
504
+ }
424
505
  function isPayeeBoundSellerCredentialUploadInput(payload) {
425
506
  return typeof payload.payeeId === "string";
426
507
  }
@@ -429,7 +510,8 @@ function isWiseCredentialUploadInput(payload) {
429
510
  }
430
511
  async function createEncryptedSellerCredentialUploadForPlatform({
431
512
  attestationServiceUrl,
432
- attestationRuntime,
513
+ attestationTransport,
514
+ cryptoRuntime,
433
515
  payload,
434
516
  platform,
435
517
  timeoutMs
@@ -443,9 +525,9 @@ async function createEncryptedSellerCredentialUploadForPlatform({
443
525
  platform: "wise",
444
526
  sessionMaterial: payload.sessionMaterial,
445
527
  ...typeof timeoutMs === "number" ? { timeoutMs } : {},
446
- ...attestationRuntime?.fetch ? { fetch: attestationRuntime.fetch } : {},
447
- ...attestationRuntime?.subtle ? { subtle: attestationRuntime.subtle } : {},
448
- ...attestationRuntime?.getRandomValues ? { getRandomValues: attestationRuntime.getRandomValues } : {}
528
+ fetch: attestationTransport.fetch,
529
+ ...cryptoRuntime?.subtle ? { subtle: cryptoRuntime.subtle } : {},
530
+ ...cryptoRuntime?.getRandomValues ? { getRandomValues: cryptoRuntime.getRandomValues } : {}
449
531
  });
450
532
  }
451
533
  if (!isPayeeBoundSellerCredentialUploadInput(payload)) {
@@ -457,19 +539,23 @@ async function createEncryptedSellerCredentialUploadForPlatform({
457
539
  payeeId: payload.payeeId,
458
540
  sessionMaterial: payload.sessionMaterial,
459
541
  ...typeof timeoutMs === "number" ? { timeoutMs } : {},
460
- ...attestationRuntime?.fetch ? { fetch: attestationRuntime.fetch } : {},
461
- ...attestationRuntime?.subtle ? { subtle: attestationRuntime.subtle } : {},
462
- ...attestationRuntime?.getRandomValues ? { getRandomValues: attestationRuntime.getRandomValues } : {}
542
+ fetch: attestationTransport.fetch,
543
+ ...cryptoRuntime?.subtle ? { subtle: cryptoRuntime.subtle } : {},
544
+ ...cryptoRuntime?.getRandomValues ? { getRandomValues: cryptoRuntime.getRandomValues } : {}
463
545
  });
464
546
  }
465
- async function apiVerifyBuyerTeePayment(payload, attestationServiceUrl, platform, actionType) {
547
+ async function apiVerifyBuyerTeePayment(payload, attestationServiceUrl, platform, actionType, options = {}) {
466
548
  return withRetry(async () => {
467
549
  let res;
468
550
  try {
469
551
  const endpoint = `/buyer/verify/${encodeURIComponent(platform)}/${encodeURIComponent(
470
552
  actionType
471
553
  )}`;
472
- res = await fetch(`${attestationServiceUrl}${endpoint}`, {
554
+ const baseUrl = normalizeAttestationBaseUrl(attestationServiceUrl, "Attestation Service URL");
555
+ const attestationTransport = createAttestationTransport(baseUrl, {
556
+ fallbackUrls: options.fallbackUrls
557
+ });
558
+ res = await attestationTransport.fetch(`${baseUrl}${endpoint}`, {
473
559
  method: "POST",
474
560
  headers: headers(),
475
561
  body: JSON.stringify(payload)
@@ -487,13 +573,16 @@ async function apiVerifyBuyerTeePayment(payload, attestationServiceUrl, platform
487
573
  return res.json();
488
574
  });
489
575
  }
490
- async function apiRequestIdentityAttestation(payload, attestationServiceUrl, platform, actionType) {
576
+ async function apiRequestIdentityAttestation(payload, attestationServiceUrl, platform, actionType, options = {}) {
491
577
  return withRetry(async () => {
492
578
  let res;
493
579
  try {
494
580
  const endpoint = "/identity";
495
- const baseUrl = normalizeBaseUrl(attestationServiceUrl, "Attestation Service URL");
496
- res = await fetch(`${baseUrl}${endpoint}`, {
581
+ const baseUrl = normalizeAttestationBaseUrl(attestationServiceUrl, "Attestation Service URL");
582
+ const attestationTransport = createAttestationTransport(baseUrl, {
583
+ fallbackUrls: options.fallbackUrls
584
+ });
585
+ res = await attestationTransport.fetch(`${baseUrl}${endpoint}`, {
497
586
  method: "POST",
498
587
  headers: headers(),
499
588
  body: JSON.stringify({
@@ -519,31 +608,51 @@ async function apiRequestIdentityAttestation(payload, attestationServiceUrl, pla
519
608
  }
520
609
  async function createEncryptedBuyerTeeSessionMaterial({
521
610
  actionType,
611
+ attestationServiceFallbackUrls,
522
612
  attestationRuntime,
523
613
  attestationServiceUrl,
524
614
  platform,
525
615
  sessionMaterial,
526
616
  timeoutMs
527
617
  }) {
618
+ const normalizedAttestationServiceUrl = normalizeAttestationBaseUrl(
619
+ attestationServiceUrl,
620
+ "Attestation Service URL"
621
+ );
622
+ const attestationTransport = createAttestationTransport(normalizedAttestationServiceUrl, {
623
+ fallbackUrls: attestationServiceFallbackUrls,
624
+ ...attestationRuntime?.fetch ? { fetch: attestationRuntime.fetch } : {}
625
+ });
528
626
  const params = {
529
627
  actionType,
530
- attestationServiceUrl: normalizeBaseUrl(attestationServiceUrl, "Attestation Service URL"),
628
+ attestationServiceUrl: normalizedAttestationServiceUrl,
531
629
  platform,
532
630
  sessionMaterial,
533
631
  ...timeoutMs == null ? {} : { timeoutMs },
534
- ...attestationRuntime?.fetch ? { fetch: attestationRuntime.fetch } : {},
632
+ fetch: attestationTransport.fetch,
535
633
  ...attestationRuntime?.subtle ? { subtle: attestationRuntime.subtle } : {},
536
634
  ...attestationRuntime?.getRandomValues ? { getRandomValues: attestationRuntime.getRandomValues } : {}
537
635
  };
538
636
  return createEncryptedBuyerTeeSessionMaterial$1(params);
539
637
  }
540
- async function apiCreateSellerCredentialBundle(payload, attestationServiceUrl, platform, timeoutMs, attestationRuntime) {
638
+ async function apiCreateSellerCredentialBundle(payload, attestationServiceUrl, platform, timeoutMs, attestationRuntime, options = {}) {
541
639
  return withRetry(
542
640
  async () => {
543
- const requestFetch = attestationRuntime?.fetch ?? fetch;
544
- const encryptedUpload = await createEncryptedSellerCredentialUploadForPlatform({
641
+ const normalizedAttestationServiceUrl = normalizeAttestationBaseUrl(
545
642
  attestationServiceUrl,
546
- attestationRuntime,
643
+ "Attestation Service URL"
644
+ );
645
+ const attestationTransport = createAttestationTransport(normalizedAttestationServiceUrl, {
646
+ fallbackUrls: options.fallbackUrls,
647
+ ...attestationRuntime?.fetch ? { fetch: attestationRuntime.fetch } : {}
648
+ });
649
+ const encryptedUpload = await createEncryptedSellerCredentialUploadForPlatform({
650
+ attestationServiceUrl: normalizedAttestationServiceUrl,
651
+ attestationTransport,
652
+ cryptoRuntime: {
653
+ subtle: attestationRuntime?.subtle,
654
+ getRandomValues: attestationRuntime?.getRandomValues
655
+ },
547
656
  payload,
548
657
  platform,
549
658
  timeoutMs
@@ -551,7 +660,8 @@ async function apiCreateSellerCredentialBundle(payload, attestationServiceUrl, p
551
660
  let res;
552
661
  try {
553
662
  const endpoint = `/seller/credentials/${encodeURIComponent(platform)}`;
554
- res = await requestFetch(`${attestationServiceUrl}${endpoint}`, {
663
+ const activeBaseUrl = attestationTransport.getActiveBaseUrl();
664
+ res = await attestationTransport.fetch(`${activeBaseUrl}${endpoint}`, {
555
665
  method: "POST",
556
666
  headers: headers(),
557
667
  body: JSON.stringify({ encryptedUpload })
@@ -1105,6 +1215,7 @@ var IntentOperations = class {
1105
1215
  if ((!gatingServiceSignature || !signatureExpiration) && baseApiUrl) {
1106
1216
  const apiOpts = {
1107
1217
  baseApiUrl,
1218
+ apiKey: this.config.getApiKey(),
1108
1219
  timeoutMs: this.config.getApiTimeoutMs()
1109
1220
  };
1110
1221
  const response = await apiSignIntentV3(
@@ -1374,7 +1485,8 @@ var IntentOperations = class {
1374
1485
  },
1375
1486
  attestationServiceUrl,
1376
1487
  attestationRoute.actionPlatform,
1377
- attestationRoute.actionType
1488
+ attestationRoute.actionType,
1489
+ { fallbackUrls: params.attestationServiceFallbackUrls }
1378
1490
  );
1379
1491
  assertReleaseAmountMeetsMinimum(attestation, inputs.minimumReleaseAmount);
1380
1492
  paymentProof = encodePaymentAttestation(attestation);
@@ -6729,6 +6841,7 @@ var Zkp2pClient = class {
6729
6841
  getChainId: () => this.chainId,
6730
6842
  getRuntimeEnv: () => this.runtimeEnv,
6731
6843
  getBaseApiUrl: () => this.baseApiUrl,
6844
+ getApiKey: () => this.apiKey,
6732
6845
  getApiTimeoutMs: () => this.apiTimeoutMs,
6733
6846
  getProtocolViewerAddress: () => this.protocolViewerAddress,
6734
6847
  getProtocolViewerAbi: () => this.protocolViewerAbi,
@@ -8086,18 +8199,44 @@ var Zkp2pClient = class {
8086
8199
  const attestationServiceUrl = this.stripTrailingSlash(
8087
8200
  opts?.attestationServiceUrl ?? this.defaultAttestationServiceForBaseApiUrl(baseApiUrl)
8088
8201
  );
8089
- const createBundle = (uploadPayload) => opts?.attestationRuntime ? apiCreateSellerCredentialBundle(
8090
- uploadPayload,
8091
- attestationServiceUrl,
8092
- params.platform,
8093
- timeoutMs,
8094
- opts.attestationRuntime
8095
- ) : apiCreateSellerCredentialBundle(
8096
- uploadPayload,
8097
- attestationServiceUrl,
8098
- params.platform,
8099
- timeoutMs
8100
- );
8202
+ const createBundle = (uploadPayload) => {
8203
+ const requestOptions = opts?.attestationServiceFallbackUrls ? { fallbackUrls: opts.attestationServiceFallbackUrls } : void 0;
8204
+ if (opts?.attestationRuntime && requestOptions) {
8205
+ return apiCreateSellerCredentialBundle(
8206
+ uploadPayload,
8207
+ attestationServiceUrl,
8208
+ params.platform,
8209
+ timeoutMs,
8210
+ opts.attestationRuntime,
8211
+ requestOptions
8212
+ );
8213
+ }
8214
+ if (opts?.attestationRuntime) {
8215
+ return apiCreateSellerCredentialBundle(
8216
+ uploadPayload,
8217
+ attestationServiceUrl,
8218
+ params.platform,
8219
+ timeoutMs,
8220
+ opts.attestationRuntime
8221
+ );
8222
+ }
8223
+ if (requestOptions) {
8224
+ return apiCreateSellerCredentialBundle(
8225
+ uploadPayload,
8226
+ attestationServiceUrl,
8227
+ params.platform,
8228
+ timeoutMs,
8229
+ void 0,
8230
+ requestOptions
8231
+ );
8232
+ }
8233
+ return apiCreateSellerCredentialBundle(
8234
+ uploadPayload,
8235
+ attestationServiceUrl,
8236
+ params.platform,
8237
+ timeoutMs
8238
+ );
8239
+ };
8101
8240
  if (params.platform === "wise") {
8102
8241
  const bundleResponse2 = await createBundle({
8103
8242
  sessionMaterial: params.sessionMaterial
@@ -8168,6 +8307,21 @@ var Zkp2pClient = class {
8168
8307
  }
8169
8308
  );
8170
8309
  }
8310
+ if (params.platform === "zelle") {
8311
+ return apiUploadGoogleOAuthSellerCredential(
8312
+ "zelle",
8313
+ params.payeeDetails,
8314
+ {
8315
+ payeeId: params.payeeId,
8316
+ authorizationCode: params.authorizationCode,
8317
+ redirectUri: params.redirectUri
8318
+ },
8319
+ baseApiUrl,
8320
+ {
8321
+ timeoutMs
8322
+ }
8323
+ );
8324
+ }
8171
8325
  return apiUploadGoogleOAuthSellerCredential(
8172
8326
  "venmo",
8173
8327
  params.payeeDetails,