@tangle-network/agent-app 0.60.143 → 0.60.144

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.
@@ -705,8 +705,81 @@ function createPlatformAgentSpend(options) {
705
705
  };
706
706
  }
707
707
 
708
- // src/platform/delegated-keys.ts
708
+ // src/platform/billing-consent.ts
709
709
  var REQUEST_TIMEOUT_MS2 = 1e4;
710
+ var notChargeable = {
711
+ status: 402,
712
+ code: "billing_consent.payer_not_chargeable",
713
+ message: "Your Tangle account has no credit for sandbox use yet. Add credit in your Tangle account, then open the terminal again.",
714
+ platformCode: "PAYER_NOT_CHARGEABLE"
715
+ };
716
+ function refusal2(status, platformCode) {
717
+ if (platformCode === "PAYER_NOT_CHARGEABLE") return notChargeable;
718
+ if (status === null || status >= 500 || status === 429) {
719
+ return {
720
+ status: 503,
721
+ code: "billing_consent.unavailable",
722
+ message: "Tangle could not record your sandbox billing consent right now. Try again in a moment.",
723
+ platformCode
724
+ };
725
+ }
726
+ return {
727
+ status: 403,
728
+ code: "billing_consent.refused",
729
+ message: "Tangle did not accept your sign-in for sandbox billing. Sign out and sign in again.",
730
+ platformCode
731
+ };
732
+ }
733
+ function createPlatformBillingConsent(options) {
734
+ const fetchImpl = options.fetch ?? fetch;
735
+ const base = options.platformUrl.replace(/\/+$/, "");
736
+ let delegate = null;
737
+ async function request(path, init) {
738
+ try {
739
+ const response = await fetchImpl(`${base}${path}`, {
740
+ ...init,
741
+ redirect: "manual",
742
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS2)
743
+ });
744
+ return { status: response.status, body: await response.json().catch(() => ({})) };
745
+ } catch {
746
+ return null;
747
+ }
748
+ }
749
+ const codeOf = (body2) => typeof body2.error?.code === "string" ? body2.error.code : null;
750
+ return {
751
+ async consent({ payerKey, serviceKey }) {
752
+ if (delegate?.serviceKey !== serviceKey) {
753
+ const self = await request("/v1/keys/self", { headers: { authorization: `Bearer ${serviceKey}` } });
754
+ const keyId = self?.body.data?.id;
755
+ if (!self || self.status !== 200 || typeof keyId !== "string" || !keyId) {
756
+ return { succeeded: false, error: refusal2(self ? Math.max(self.status, 500) : null, self ? codeOf(self.body) : null) };
757
+ }
758
+ delegate = { serviceKey, keyId };
759
+ }
760
+ const token = typeof options.serviceToken === "function" ? options.serviceToken() : options.serviceToken;
761
+ const result = await request("/v1/keys/billing-delegation-grants", {
762
+ method: "POST",
763
+ headers: {
764
+ authorization: `Bearer ${token}`,
765
+ "x-service-name": options.serviceName,
766
+ "content-type": "application/json"
767
+ },
768
+ body: JSON.stringify({ sourceKey: payerKey, delegateKeyId: delegate.keyId })
769
+ });
770
+ if (!result) return { succeeded: false, error: refusal2(null, null) };
771
+ if (result.status === 200 || result.status === 201) {
772
+ return { succeeded: true, value: { created: result.status === 201 } };
773
+ }
774
+ const platformCode = codeOf(result.body);
775
+ if (platformCode === "PAYER_IS_DELEGATE_OWNER") return { succeeded: true, value: { created: false } };
776
+ return { succeeded: false, error: refusal2(result.status, platformCode) };
777
+ }
778
+ };
779
+ }
780
+
781
+ // src/platform/delegated-keys.ts
782
+ var REQUEST_TIMEOUT_MS3 = 1e4;
710
783
  var KEY_PREFIX = "sk-tan-";
711
784
  var AUTO_PREFIX = "auto:";
712
785
  var PRODUCT_QUALIFIED = /* @__PURE__ */ new Set(["router", "sandbox"]);
@@ -717,7 +790,7 @@ function canonicalDelegatedKeyName(input) {
717
790
  const withoutService = logical.startsWith(`${input.serviceName}:`) ? logical.slice(input.serviceName.length + 1) : logical;
718
791
  return `${AUTO_PREFIX}${input.serviceName}:${input.product}:${withoutService}`;
719
792
  }
720
- function refusal2(status, code) {
793
+ function refusal3(status, code) {
721
794
  if (code === "INSUFFICIENT_BALANCE") {
722
795
  return { status: 402, code: "delegated_key.payment_required", message: "The payer's Tangle account has no credit." };
723
796
  }
@@ -791,7 +864,7 @@ function createPlatformDelegatedKeyClient(options) {
791
864
  },
792
865
  ...init.body === void 0 ? {} : { body: JSON.stringify(init.body) },
793
866
  redirect: "manual",
794
- signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS2)
867
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS3)
795
868
  });
796
869
  return { status: response.status, body: record2(await response.json().catch(() => null)) };
797
870
  } catch {
@@ -823,10 +896,10 @@ function createPlatformDelegatedKeyClient(options) {
823
896
  expiresAt: request.expiresAt.toISOString()
824
897
  }
825
898
  });
826
- if (!result) return { succeeded: false, error: refusal2(503, null) };
899
+ if (!result) return { succeeded: false, error: refusal3(503, null) };
827
900
  const data = record2(result.body?.data);
828
901
  if (result.status !== 201 || result.body?.success !== true || !data) {
829
- return { succeeded: false, error: refusal2(result.status, record2(result.body?.error)?.code) };
902
+ return { succeeded: false, error: refusal3(result.status, record2(result.body?.error)?.code) };
830
903
  }
831
904
  const problem = childProblem(data, request, name, options.serviceName, now());
832
905
  if (problem) {
@@ -863,7 +936,7 @@ function createPlatformDelegatedKeyClient(options) {
863
936
  service: true,
864
937
  body: { keyId, expectedProduct: product }
865
938
  });
866
- if (!result || result.status >= 500 || result.status === 429) return { succeeded: false, error: refusal2(503, null) };
939
+ if (!result || result.status >= 500 || result.status === 429) return { succeeded: false, error: refusal3(503, null) };
867
940
  const body2 = result.body;
868
941
  if (result.status !== 200 || typeof body2?.valid !== "boolean" || body2.valid && body2.keyId !== keyId) {
869
942
  return failure(502, "delegated_key.response_invalid", "Platform returned no verification verdict for this key.");
@@ -879,7 +952,7 @@ function createPlatformDelegatedKeyClient(options) {
879
952
  },
880
953
  async revoke({ sourceKey, keyId, slot }) {
881
954
  const result = await call(`/v1/keys/${encodeURIComponent(keyId)}`, { method: "DELETE", bearer: sourceKey, service: false });
882
- if (!result) return { succeeded: false, error: refusal2(503, null) };
955
+ if (!result) return { succeeded: false, error: refusal3(503, null) };
883
956
  if (result.status === 404 || result.status >= 200 && result.status < 300 && result.body?.success === true) {
884
957
  return { succeeded: true, value: null };
885
958
  }
@@ -891,7 +964,7 @@ function createPlatformDelegatedKeyClient(options) {
891
964
  const data = record2(row?.body?.data);
892
965
  if (row?.status === 404) return { succeeded: true, value: null };
893
966
  if (!row || row.status !== 200 || typeof data?.name !== "string" || typeof data.product !== "string") {
894
- return { succeeded: false, error: refusal2(row?.status ?? 503, null) };
967
+ return { succeeded: false, error: refusal3(row?.status ?? 503, null) };
895
968
  }
896
969
  if (data.revokedAt) return { succeeded: true, value: null };
897
970
  name = data.name;
@@ -909,19 +982,19 @@ function createPlatformDelegatedKeyClient(options) {
909
982
  ...slot.sourceKeyId ? { sourceKeyId: slot.sourceKeyId } : {}
910
983
  }
911
984
  });
912
- if (!retired) return { succeeded: false, error: refusal2(503, null) };
985
+ if (!retired) return { succeeded: false, error: refusal3(503, null) };
913
986
  if (retired.status >= 200 && retired.status < 300 && retired.body?.success === true) return { succeeded: true, value: null };
914
- return { succeeded: false, error: refusal2(retired.status, record2(retired.body?.error)?.code) };
987
+ return { succeeded: false, error: refusal3(retired.status, record2(retired.body?.error)?.code) };
915
988
  }
916
- return { succeeded: false, error: refusal2(result.status, record2(result.body?.error)?.code) };
989
+ return { succeeded: false, error: refusal3(result.status, record2(result.body?.error)?.code) };
917
990
  },
918
991
  async listChildren({ sourceKey, name, product }) {
919
992
  const canonical = canonicalDelegatedKeyName({ serviceName: options.serviceName, product, name });
920
993
  const result = await call("/v1/keys", { method: "GET", bearer: sourceKey, service: false });
921
- if (!result) return { succeeded: false, error: refusal2(503, null) };
994
+ if (!result) return { succeeded: false, error: refusal3(503, null) };
922
995
  const rows = result.body?.data;
923
996
  if (result.status !== 200 || result.body?.success !== true || !Array.isArray(rows)) {
924
- return { succeeded: false, error: refusal2(result.status, record2(result.body?.error)?.code) };
997
+ return { succeeded: false, error: refusal3(result.status, record2(result.body?.error)?.code) };
925
998
  }
926
999
  return { succeeded: true, value: rows.flatMap((row) => {
927
1000
  const value = record2(row);
@@ -940,9 +1013,9 @@ var DelegatedKeyMissingError = class extends Error {
940
1013
  }
941
1014
  };
942
1015
  var DelegatedKeyRefusalError = class extends Error {
943
- constructor(refusal3) {
944
- super(refusal3.message);
945
- this.refusal = refusal3;
1016
+ constructor(refusal4) {
1017
+ super(refusal4.message);
1018
+ this.refusal = refusal4;
946
1019
  this.name = "DelegatedKeyRefusalError";
947
1020
  }
948
1021
  refusal;
@@ -1039,6 +1112,7 @@ export {
1039
1112
  createHubSettingsRoutes,
1040
1113
  createPlatformAgentKeyVerifier,
1041
1114
  createPlatformAgentSpend,
1115
+ createPlatformBillingConsent,
1042
1116
  createPlatformBillingHttp,
1043
1117
  createPlatformDelegatedKeyClient,
1044
1118
  createSignedSsoState,