@opendatalabs/vana-sdk 3.15.0 → 3.16.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.
@@ -47,6 +47,7 @@ export { escrowContractAddress, encodeDepositNativeData, encodeDepositTokenData,
47
47
  export { DATA_REGISTRY_STATUS_ABI, DataPointStatus, dataRegistryContractAddress, encodeSetDataPointStatusData, buildSetDataPointStatusRequest, buildMarkDataPointUnavailableRequest, type SetDataPointStatusInput, type DataPointStatusTransactionRequest, } from "./protocol/data-point-status.js";
48
48
  export { personalServerDataReadPath, buildPersonalServerDataReadRequest, readPersonalServerData, type BuildPersonalServerDataReadRequestParams, type ReadPersonalServerDataParams, } from "./protocol/personal-server-data.js";
49
49
  export { ScopeSchema, parseScope, scopeToPathSegments, scopeMatchesPattern, scopeCoveredByGrant, type Scope, type ParsedScope, } from "./protocol/scopes.js";
50
+ export { SCOPE_ACTIONS, InvalidScopeEntryError, parseScopeEntry, formatScopeEntry, grantPermissions, permissionsToScopes, tryGrantPermissions, hasAction, type ScopeAction, type ParsedScopeEntry, type GrantPermission, } from "./protocol/scope-actions.js";
50
51
  export { DataFileEnvelopeSchema, createDataFileEnvelope, IngestResponseSchema, type DataFileEnvelope, type IngestResponse, } from "./protocol/data-file.js";
51
52
  export { createGatewayClient, type GatewayEnvelope, type GatewayProof, type Builder, type Schema, type ServerInfo, type OwnerServerRecord, type OwnerServersResult, type GatewayGrantFee, type GatewayGrantStatus, type GatewayGrantResponse, type GrantListItem, type DataPointRecord, type DataPointListResult, type ListDataPointsOptions, type RegisterServerParams, type RegisterServerResult, type RegisterBuilderParams, type RegisterBuilderResult, type RegisterDataPointParams, type RegisterDataPointResult, type CreateGrantParams, type RevokeGrantParams, type AccessRecord, type PayForOperationParams, type PayForOperationResult, type SettleOpType, type SettleItem, type SettlePromoteResult, type SettleReconcileItem, type SettleParams, type SettleResult, type GatewayClient, } from "./protocol/gateway.js";
52
53
  export { createEscrowGatewayClient, genericPaymentDomain, GENERIC_PAYMENT_TYPES, ESCROW_DEPOSIT_ABI, NATIVE_ASSET_ADDRESS, type GenericPaymentMessage, type EscrowBalanceEntry, type EscrowBalanceResult, type EscrowBalanceSyncResult, type DepositSubmissionResult, type PaymentBreakdown, type EscrowPayResult, type SubmitDepositParams, type PayForOpParams, type EscrowGatewayClient, type SubmittedDepositEntry, type FinalizedDepositEntry, type FailedDepositEntry, } from "./protocol/escrow.js";
@@ -32947,7 +32947,161 @@ function scopeCoveredByGrant(requestedScope, grantedScopes) {
32947
32947
  );
32948
32948
  }
32949
32949
 
32950
+ // src/protocol/scope-actions.ts
32951
+ var SCOPE_ACTIONS = ["read", "write"];
32952
+ var InvalidScopeEntryError = class extends Error {
32953
+ /** The offending entry, verbatim (unknown because it may not be a string). */
32954
+ entry;
32955
+ constructor(entry, reason) {
32956
+ super(`Invalid scope entry ${describeValue(entry)}: ${reason}`);
32957
+ this.name = "InvalidScopeEntryError";
32958
+ this.entry = entry;
32959
+ }
32960
+ };
32961
+ var OPERATION_SEPARATOR = ":";
32962
+ function describeValue(value) {
32963
+ if (typeof value === "string") return JSON.stringify(value);
32964
+ if (value === null) return "null";
32965
+ return `[${typeof value}]`;
32966
+ }
32967
+ var OPERATION_BY_PREFIX = {
32968
+ write: "write"
32969
+ };
32970
+ function assertScopePart(entry, scope) {
32971
+ if (scope.length === 0) {
32972
+ throw new InvalidScopeEntryError(entry, "scope part is empty");
32973
+ }
32974
+ if (scope.includes(OPERATION_SEPARATOR)) {
32975
+ throw new InvalidScopeEntryError(
32976
+ entry,
32977
+ `scope part must not contain "${OPERATION_SEPARATOR}"`
32978
+ );
32979
+ }
32980
+ }
32981
+ function parseScopeEntry(entry) {
32982
+ const raw = entry;
32983
+ if (typeof raw !== "string") {
32984
+ throw new InvalidScopeEntryError(raw, "entry must be a string");
32985
+ }
32986
+ const separatorIndex = entry.indexOf(OPERATION_SEPARATOR);
32987
+ if (separatorIndex === -1) {
32988
+ assertScopePart(entry, entry);
32989
+ return { scope: entry, action: "read" };
32990
+ }
32991
+ const prefix = entry.slice(0, separatorIndex);
32992
+ const scope = entry.slice(separatorIndex + 1);
32993
+ const action = Object.hasOwn(OPERATION_BY_PREFIX, prefix) ? OPERATION_BY_PREFIX[prefix] : void 0;
32994
+ if (action === void 0) {
32995
+ throw new InvalidScopeEntryError(
32996
+ entry,
32997
+ `unknown operation "${prefix}" (known: ${Object.keys(OPERATION_BY_PREFIX).join(", ")}; read has no prefix)`
32998
+ );
32999
+ }
33000
+ assertScopePart(entry, scope);
33001
+ return { scope, action };
33002
+ }
33003
+ function formatScopeEntry(parsed) {
33004
+ const { scope, action } = parsed;
33005
+ assertScopePart(scope, scope);
33006
+ if (action === "read") return scope;
33007
+ const prefix = Object.entries(OPERATION_BY_PREFIX).find(
33008
+ ([, candidate]) => candidate === action
33009
+ )?.[0];
33010
+ if (prefix === void 0) {
33011
+ throw new InvalidScopeEntryError(
33012
+ scope,
33013
+ `unknown action ${describeValue(action)} (known: ${SCOPE_ACTIONS.join(", ")})`
33014
+ );
33015
+ }
33016
+ return `${prefix}${OPERATION_SEPARATOR}${scope}`;
33017
+ }
33018
+ function compareScopes(a, b) {
33019
+ if (a < b) return -1;
33020
+ if (a > b) return 1;
33021
+ return 0;
33022
+ }
33023
+ function sortActions(actions) {
33024
+ const present = new Set(actions);
33025
+ return SCOPE_ACTIONS.filter((action) => present.has(action));
33026
+ }
33027
+ function grantPermissions(scopes) {
33028
+ const byScope = /* @__PURE__ */ new Map();
33029
+ for (const entry of scopes) {
33030
+ const { scope, action } = parseScopeEntry(entry);
33031
+ let actions = byScope.get(scope);
33032
+ if (actions === void 0) {
33033
+ actions = /* @__PURE__ */ new Set();
33034
+ byScope.set(scope, actions);
33035
+ }
33036
+ actions.add(action);
33037
+ }
33038
+ return [...byScope.keys()].sort(compareScopes).map((scope) => ({
33039
+ scope,
33040
+ actions: sortActions(byScope.get(scope) ?? [])
33041
+ }));
33042
+ }
33043
+ function permissionsToScopes(permissions) {
33044
+ const byScope = /* @__PURE__ */ new Map();
33045
+ for (const { scope, actions } of permissions) {
33046
+ let merged = byScope.get(scope);
33047
+ if (merged === void 0) {
33048
+ merged = /* @__PURE__ */ new Set();
33049
+ byScope.set(scope, merged);
33050
+ }
33051
+ for (const action of actions) {
33052
+ if (!SCOPE_ACTIONS.includes(action)) {
33053
+ throw new InvalidScopeEntryError(
33054
+ scope,
33055
+ `unknown action ${describeValue(action)} (known: ${SCOPE_ACTIONS.join(", ")})`
33056
+ );
33057
+ }
33058
+ merged.add(action);
33059
+ }
33060
+ }
33061
+ const entries = [];
33062
+ for (const scope of [...byScope.keys()].sort(compareScopes)) {
33063
+ for (const action of sortActions(byScope.get(scope) ?? [])) {
33064
+ entries.push(formatScopeEntry({ scope, action }));
33065
+ }
33066
+ }
33067
+ return entries;
33068
+ }
33069
+ function hasAction(scopes, scope, action) {
33070
+ if (scope.includes(OPERATION_SEPARATOR)) return false;
33071
+ for (const entry of scopes) {
33072
+ let parsed;
33073
+ try {
33074
+ parsed = parseScopeEntry(entry);
33075
+ } catch (error) {
33076
+ if (error instanceof InvalidScopeEntryError) continue;
33077
+ throw error;
33078
+ }
33079
+ if (parsed.action === action && scopeMatchesPattern(scope, parsed.scope)) {
33080
+ return true;
33081
+ }
33082
+ }
33083
+ return false;
33084
+ }
33085
+ function tryGrantPermissions(scopes) {
33086
+ try {
33087
+ return grantPermissions(scopes);
33088
+ } catch (error) {
33089
+ if (error instanceof InvalidScopeEntryError) return void 0;
33090
+ throw error;
33091
+ }
33092
+ }
33093
+
32950
33094
  // src/protocol/gateway.ts
33095
+ function withGrantPermissions(grant) {
33096
+ const stripped = { ...grant };
33097
+ delete stripped.permissions;
33098
+ const scopes = stripped.scopes;
33099
+ if (!Array.isArray(scopes)) {
33100
+ return stripped;
33101
+ }
33102
+ const permissions = tryGrantPermissions(scopes);
33103
+ return permissions === void 0 ? stripped : { ...stripped, permissions };
33104
+ }
32951
33105
  function createGatewayClient(baseUrl) {
32952
33106
  const base = baseUrl.replace(/\/+$/, "");
32953
33107
  async function unwrapEnvelope(res) {
@@ -32977,7 +33131,9 @@ function createGatewayClient(baseUrl) {
32977
33131
  if (!res.ok) {
32978
33132
  throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
32979
33133
  }
32980
- return unwrapEnvelope(res);
33134
+ return withGrantPermissions(
33135
+ await unwrapEnvelope(res)
33136
+ );
32981
33137
  },
32982
33138
  async listGrantsByUser(userAddress) {
32983
33139
  const res = await fetch(`${base}/v1/grants?user=${userAddress}`);
@@ -32985,7 +33141,8 @@ function createGatewayClient(baseUrl) {
32985
33141
  if (!res.ok) {
32986
33142
  throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
32987
33143
  }
32988
- return unwrapEnvelope(res);
33144
+ const grants = await unwrapEnvelope(res);
33145
+ return grants.map(withGrantPermissions);
32989
33146
  },
32990
33147
  async getSchemaForScope(scope) {
32991
33148
  const res = await fetch(`${base}/v1/schemas?scope=${scope}`);
@@ -33451,6 +33608,7 @@ export {
33451
33608
  InMemoryTokenStore,
33452
33609
  IngestResponseSchema,
33453
33610
  InvalidConfigurationError,
33611
+ InvalidScopeEntryError,
33454
33612
  InvalidSignatureError,
33455
33613
  IpfsStorage,
33456
33614
  MASTER_KEY_MESSAGE,
@@ -33476,6 +33634,7 @@ export {
33476
33634
  REGISTRATION_KIND_FOR_OP,
33477
33635
  ReadOnlyError,
33478
33636
  RelayerError,
33637
+ SCOPE_ACTIONS,
33479
33638
  SERVER_REGISTRATION_TYPES,
33480
33639
  ScopeSchema,
33481
33640
  SerializationError,
@@ -33525,6 +33684,7 @@ export {
33525
33684
  encryptWithPassword,
33526
33685
  escrowContractAddress,
33527
33686
  escrowPaymentDomain,
33687
+ formatScopeEntry,
33528
33688
  generatePkceVerifier,
33529
33689
  genericPaymentDomain,
33530
33690
  getAbi,
@@ -33537,8 +33697,10 @@ export {
33537
33697
  getOpFee,
33538
33698
  getPlatformCapabilities,
33539
33699
  getServiceEndpoints,
33700
+ grantPermissions,
33540
33701
  grantRegistrationDomain,
33541
33702
  grantRevocationDomain,
33703
+ hasAction,
33542
33704
  isDataPortabilityGatewayConfig,
33543
33705
  isECIESEncrypted,
33544
33706
  isPlatformSupported,
@@ -33548,7 +33710,9 @@ export {
33548
33710
  mokshaTestnet2 as mokshaTestnet,
33549
33711
  parsePSError,
33550
33712
  parseScope,
33713
+ parseScopeEntry,
33551
33714
  parseWeb3SignedHeader,
33715
+ permissionsToScopes,
33552
33716
  personalServerDataReadPath,
33553
33717
  personalServerRegistrationDomain,
33554
33718
  readPersonalServerData,
@@ -33562,6 +33726,7 @@ export {
33562
33726
  signPersonalServerLiteOwnerBinding,
33563
33727
  signPersonalServerLiteOwnerBindingWithAccountClient,
33564
33728
  signPersonalServerRegistrationWithAccount,
33729
+ tryGrantPermissions,
33565
33730
  vanaMainnet2 as vanaMainnet,
33566
33731
  verifyGrantRegistration,
33567
33732
  verifyPkceChallenge,