@witnium/witniumchain 0.28.2 → 0.30.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.js CHANGED
@@ -1,5 +1,21 @@
1
1
  'use strict';
2
2
 
3
+ // src/api-key.ts
4
+ var PREFIXED_FORMAT = /^wcak_(live|sandbox)_[0-9a-f]{64}$/;
5
+ var LEGACY_FORMAT = /^wcak_[0-9a-f]{64}$/;
6
+ function parseApiKey(apiKey) {
7
+ const prefixed = PREFIXED_FORMAT.exec(apiKey);
8
+ if (prefixed) {
9
+ return { environment: prefixed[1], legacyFormat: false };
10
+ }
11
+ if (LEGACY_FORMAT.test(apiKey)) {
12
+ return { environment: "live", legacyFormat: true };
13
+ }
14
+ throw new Error(
15
+ "apiKey must be `wcak_live_` or `wcak_sandbox_` followed by 64 lowercase hex (or a pre-sandbox `wcak_` key)"
16
+ );
17
+ }
18
+
3
19
  // src/errors.ts
4
20
  var WitniumchainApiError = class extends Error {
5
21
  status;
@@ -247,6 +263,13 @@ var WitniumchainClient = class {
247
263
  if (!config.baseUrl) {
248
264
  throw new Error("WitniumchainClient: baseUrl is required");
249
265
  }
266
+ if (config.apiKey !== void 0) {
267
+ try {
268
+ parseApiKey(config.apiKey);
269
+ } catch (err) {
270
+ throw new Error(`WitniumchainClient: ${err.message}`);
271
+ }
272
+ }
250
273
  this.cfg = config;
251
274
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
252
275
  this.chainBaseUrl = config.chainBaseUrl?.replace(/\/$/, "");
@@ -492,19 +515,6 @@ var WitniumchainClient = class {
492
515
  return this.req("POST", "/v1/auth/reset-password", { auth: "Public", body });
493
516
  }
494
517
  // ────────────────────────────────────────────────────────────────────────
495
- // Billing (/v1/billing/*)
496
- // ────────────────────────────────────────────────────────────────────────
497
- createCheckoutSession(body) {
498
- return this.req("POST", "/v1/billing/checkout", {
499
- auth: "SessionOrBearer",
500
- body
501
- });
502
- }
503
- createPortalSession() {
504
- return this.req("GET", "/v1/billing/portal", { auth: "SessionOrBearer" });
505
- }
506
- // Webhook endpoint is intentionally NOT exposed: only Stripe should call it.
507
- // ────────────────────────────────────────────────────────────────────────
508
518
  // Orgs (/v1/orgs/me/*)
509
519
  // ────────────────────────────────────────────────────────────────────────
510
520
  /** The calling API key's own description: its contract and signing key (API key only). */
@@ -759,11 +769,12 @@ var WitniumchainClient = class {
759
769
  // Routing: every method here passes `service: 'chain'` to `req()`. Calls
760
770
  // throw at call time if `chainBaseUrl` wasn't configured.
761
771
  //
762
- // Auth: the contract, witness, transaction and wallet reads are public on
763
- // chain-api (anyone can check a witness), so they send no credential. The
764
- // dashboard reads are BearerJWT: the accounts-issued token already carries
765
- // `aud=https://api.witniumchain.com`, so the same `accessToken` works
766
- // unchanged against both services.
772
+ // Auth: a single witness, its transactions and the contract existence
773
+ // check are public on chain-api (anyone can check a witness), so they send
774
+ // no credential. An org's witness listing and contract info are the
775
+ // registry owner's (BearerJWT): the accounts-issued token already carries
776
+ // the chain-api audience, so the same `accessToken` works unchanged
777
+ // against both services. So do the dashboard reads.
767
778
  //
768
779
  // What's NOT here: chain-api v5 WRITES (propose/sign/finalize/revoke). Those
769
780
  // are proxied by accounts (proposeWitnessV5, etc.) so credits get reserved
@@ -771,11 +782,12 @@ var WitniumchainClient = class {
771
782
  // routing rules; calling chain-api writes directly would burn credits
772
783
  // without billing them.
773
784
  // ════════════════════════════════════════════════════════════════════════
785
+ /** The registry owner's: the token's org must own `contractAddress`. */
774
786
  getContractInfo(contractAddress) {
775
787
  return this.req(
776
788
  "GET",
777
789
  `/v5/contracts/${encodeURIComponent(contractAddress)}/info`,
778
- { auth: "Public", service: "chain" }
790
+ { auth: "BearerJWT", service: "chain" }
779
791
  );
780
792
  }
781
793
  getContractVerification(contractAddress) {
@@ -789,7 +801,7 @@ var WitniumchainClient = class {
789
801
  return this.req(
790
802
  "GET",
791
803
  `/v5/contracts/${encodeURIComponent(contractAddress)}/witnesses`,
792
- { auth: "Public", service: "chain", query: params }
804
+ { auth: "BearerJWT", service: "chain", query: params }
793
805
  );
794
806
  }
795
807
  getWitnessV5(contractAddress, witnessId) {
@@ -813,13 +825,6 @@ var WitniumchainClient = class {
813
825
  { auth: "Public", service: "chain" }
814
826
  );
815
827
  }
816
- getWalletBalance(address) {
817
- return this.req(
818
- "GET",
819
- `/v5/wallets/${encodeURIComponent(address)}/balance`,
820
- { auth: "Public", service: "chain" }
821
- );
822
- }
823
828
  getDashboardContract() {
824
829
  return this.req("GET", "/v5/dashboard/contract", {
825
830
  auth: "BearerJWT",
@@ -1556,22 +1561,6 @@ var Subscriptions = class {
1556
1561
  this.client = client;
1557
1562
  }
1558
1563
  client;
1559
- /**
1560
- * Start a Stripe Checkout session for the supplied price. Returns the
1561
- * hosted Checkout URL; redirect the user to it. Stripe's
1562
- * `checkout.session.completed` webhook grants credits on success.
1563
- */
1564
- subscribe(body) {
1565
- return this.client.createCheckoutSession(body);
1566
- }
1567
- /**
1568
- * Open the Stripe Billing Portal for the calling user's org. Returns the
1569
- * hosted portal URL — redirect the user there for subscription /
1570
- * payment-method management.
1571
- */
1572
- manage() {
1573
- return this.client.createPortalSession();
1574
- }
1575
1564
  /** Recent credit-ledger entries (most recent 200). */
1576
1565
  getLedger() {
1577
1566
  return this.client.getLedger();
@@ -2053,13 +2042,25 @@ function safeParse(text) {
2053
2042
  }
2054
2043
 
2055
2044
  // src/api-key-client.ts
2056
- var API_KEY_FORMAT = /^wcak_[0-9a-f]{64}$/;
2057
2045
  var WitniumchainApiKeyClient = class {
2058
2046
  inner;
2047
+ /**
2048
+ * `live` or `sandbox`, from the key's prefix. Accounts
2049
+ * decides from its own record of the key; this is for the caller's checks
2050
+ * and logs.
2051
+ */
2052
+ environment;
2053
+ /** True for a pre-sandbox `wcak_` + 64 hex key. Rotate it to a `wcak_live_` key before accounts' cutoff. */
2054
+ legacyKeyFormat;
2059
2055
  constructor(config) {
2060
- if (!API_KEY_FORMAT.test(config.apiKey)) {
2061
- throw new Error("WitniumchainApiKeyClient: apiKey must be `wcak_` and 64 lowercase hex");
2056
+ let parsed;
2057
+ try {
2058
+ parsed = parseApiKey(config.apiKey);
2059
+ } catch (err) {
2060
+ throw new Error(`WitniumchainApiKeyClient: ${err.message}`);
2062
2061
  }
2062
+ this.environment = parsed.environment;
2063
+ this.legacyKeyFormat = parsed.legacyFormat;
2063
2064
  this.inner = new WitniumchainClient({
2064
2065
  baseUrl: config.baseUrl,
2065
2066
  apiKey: config.apiKey,
@@ -2251,6 +2252,7 @@ exports.defaultVerifierStorage = defaultVerifierStorage;
2251
2252
  exports.generateOwnerKeyFile = generateOwnerKeyFile;
2252
2253
  exports.ownerKeyFile = ownerKeyFile;
2253
2254
  exports.ownerKeyFileName = ownerKeyFileName;
2255
+ exports.parseApiKey = parseApiKey;
2254
2256
  exports.parseOwnerKeyFile = parseOwnerKeyFile;
2255
2257
  exports.randomWitnessScope = randomWitnessScope;
2256
2258
  exports.serializeOwnerKeyFile = serializeOwnerKeyFile;