@opendatalabs/vana-sdk 3.19.0 → 3.19.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.
@@ -1373,6 +1373,17 @@ var DerivativeQuestionNotFoundError = class extends PersonalServerWriteError {
1373
1373
  super(message, "DERIVATIVE_QUESTION_NOT_FOUND", 404, errorCode, details);
1374
1374
  }
1375
1375
  };
1376
+ var DerivativeDerivedScopeRequiredError = class extends PersonalServerWriteError {
1377
+ constructor(message, errorCode = null, details) {
1378
+ super(
1379
+ message,
1380
+ "DERIVATIVE_DERIVED_SCOPE_REQUIRED",
1381
+ 400,
1382
+ errorCode,
1383
+ details
1384
+ );
1385
+ }
1386
+ };
1376
1387
  var DerivativeSourceNotGrantedError = class extends PersonalServerWriteError {
1377
1388
  constructor(message, errorCode = null, details) {
1378
1389
  super(message, "DERIVATIVE_SOURCE_NOT_GRANTED", 403, errorCode, details);
@@ -29155,6 +29166,9 @@ async function buildWeb3SignedHeader(params) {
29155
29166
  if (params.grantId !== void 0) {
29156
29167
  payload["grantId"] = params.grantId;
29157
29168
  }
29169
+ if (params.nonce !== void 0) {
29170
+ payload["nonce"] = params.nonce;
29171
+ }
29158
29172
  const sortedPayload = Object.keys(payload).sort().reduce((acc, key) => {
29159
29173
  acc[key] = payload[key];
29160
29174
  return acc;
@@ -34498,6 +34512,18 @@ function nextProofIat(proofKey) {
34498
34512
  if (waitSec <= 0) return Promise.resolve(iat);
34499
34513
  return sleep2(waitSec * 1e3).then(() => iat);
34500
34514
  }
34515
+ function freshProofNonce() {
34516
+ const webCrypto = globalThis.crypto;
34517
+ if (typeof webCrypto?.randomUUID === "function") {
34518
+ return webCrypto.randomUUID();
34519
+ }
34520
+ if (typeof webCrypto?.getRandomValues === "function") {
34521
+ return bytesToHex2(webCrypto.getRandomValues(new Uint8Array(16)));
34522
+ }
34523
+ throw new WriteRequestError(
34524
+ "No secure random source available to build a proof nonce; provide a crypto global"
34525
+ );
34526
+ }
34501
34527
  function proofKeyFor(parts) {
34502
34528
  return bytesToHex2(
34503
34529
  sha2565(
@@ -35039,12 +35065,7 @@ var DerivativeQuestionSchema = z5.object({
35039
35065
  var QuestionListSchema = z5.object({
35040
35066
  questions: z5.array(DerivativeQuestionSchema)
35041
35067
  });
35042
- var QuestionRecomputeResultSchema = z5.object({
35043
- questionId: z5.string().min(1),
35044
- derivedScope: z5.string().min(1),
35045
- /** `pending` when the question was never computed, else `stale`. */
35046
- status: QuestionStatusSchema
35047
- });
35068
+ var QuestionRecomputeResultSchema = DerivativeQuestionSchema;
35048
35069
  var QuestionDeleteResultSchema = z5.object({
35049
35070
  questionId: z5.string().min(1),
35050
35071
  deleted: z5.literal(true)
@@ -35112,8 +35133,18 @@ async function resolveSession(params, resolved, force) {
35112
35133
  cache.set(resolved.cacheKey, session);
35113
35134
  return session;
35114
35135
  }
35115
- async function questionErrorFromResponse(response) {
35116
- const { errorCode, message, details } = await readPersonalServerErrorBody(response);
35136
+ var PROOF_FAILURE_CODES = /* @__PURE__ */ new Set([
35137
+ "WRITE_ATTRIBUTION_REQUIRED",
35138
+ "WRITE_ATTRIBUTION_INVALID",
35139
+ "WRITE_ATTRIBUTION_SIGNER_MISMATCH",
35140
+ "WRITE_ATTRIBUTION_GRANT_MISMATCH",
35141
+ "WRITE_ATTRIBUTION_REPLAY"
35142
+ ]);
35143
+ function isStaleSession(errorCode) {
35144
+ return errorCode === null || !PROOF_FAILURE_CODES.has(errorCode);
35145
+ }
35146
+ async function questionErrorFromResponse(response, body) {
35147
+ const { errorCode, message, details } = body ?? await readPersonalServerErrorBody(response);
35117
35148
  const text = message ?? `Derivative question request failed: ${response.status} ${response.statusText}`;
35118
35149
  switch (errorCode) {
35119
35150
  case "DERIVATIVE_SOURCE_NOT_GRANTED":
@@ -35132,6 +35163,8 @@ async function questionErrorFromResponse(response) {
35132
35163
  );
35133
35164
  case "DERIVATIVE_QUESTION_NOT_FOUND":
35134
35165
  return new DerivativeQuestionNotFoundError(text, errorCode, details);
35166
+ case "DERIVATIVE_DERIVED_SCOPE_REQUIRED":
35167
+ return new DerivativeDerivedScopeRequiredError(text, errorCode, details);
35135
35168
  default:
35136
35169
  break;
35137
35170
  }
@@ -35161,7 +35194,7 @@ async function sendOnce(params, resolved, session, spec, bodyBytes) {
35161
35194
  proofKeyFor({
35162
35195
  aud: session.audience,
35163
35196
  method: spec.method,
35164
- uri: spec.path,
35197
+ uri: spec.target,
35165
35198
  grantId: session.grantId,
35166
35199
  signedBytes: bodyBytes
35167
35200
  }),
@@ -35177,17 +35210,22 @@ async function sendOnce(params, resolved, session, spec, bodyBytes) {
35177
35210
  await buildWeb3SignedHeader({
35178
35211
  signMessage: session.signer.signMessage,
35179
35212
  aud: session.audience,
35180
- // The Personal Server verifies the proof against the request's
35181
- // path only, so the signed `uri` must not carry the query string.
35182
- uri: spec.path,
35213
+ // The proof commits to the whole request target, query included:
35214
+ // `?derivedScope=` is what the list route authorizes against, and a
35215
+ // proof that did not cover it would authorize any other scope.
35216
+ uri: spec.target,
35183
35217
  method: spec.method,
35184
35218
  body: bodyBytes,
35185
35219
  grantId: session.grantId,
35220
+ // Fresh per attempt, so a retry after a thrown `fetch` is never the
35221
+ // proof the server may already have consumed, and so two identical
35222
+ // polls inside one second stay distinct.
35223
+ nonce: freshProofNonce(),
35186
35224
  iat
35187
35225
  })
35188
35226
  );
35189
35227
  return {
35190
- url: `${resolved.baseUrl}${spec.path}${spec.query ?? ""}`,
35228
+ url: `${resolved.baseUrl}${spec.target}`,
35191
35229
  init: {
35192
35230
  method: spec.method,
35193
35231
  headers,
@@ -35203,12 +35241,17 @@ async function sendQuestionRequest(params, spec, schema) {
35203
35241
  const bodyBytes = spec.body === void 0 ? void 0 : new TextEncoder().encode(JSON.stringify(spec.body));
35204
35242
  let session = await resolveSession(params, resolved, false);
35205
35243
  let response = await sendOnce(params, resolved, session, spec, bodyBytes);
35244
+ let errorBody;
35206
35245
  if (response.status === 401) {
35207
- session = await resolveSession(params, resolved, true);
35208
- response = await sendOnce(params, resolved, session, spec, bodyBytes);
35246
+ errorBody = await readPersonalServerErrorBody(response);
35247
+ if (isStaleSession(errorBody.errorCode)) {
35248
+ session = await resolveSession(params, resolved, true);
35249
+ response = await sendOnce(params, resolved, session, spec, bodyBytes);
35250
+ errorBody = void 0;
35251
+ }
35209
35252
  }
35210
35253
  if (!response.ok) {
35211
- throw await questionErrorFromResponse(response);
35254
+ throw await questionErrorFromResponse(response, errorBody);
35212
35255
  }
35213
35256
  let body;
35214
35257
  try {
@@ -35302,7 +35345,7 @@ async function registerQuestion(params) {
35302
35345
  params,
35303
35346
  {
35304
35347
  method: "POST",
35305
- path: DERIVATIVE_QUESTIONS_PATH,
35348
+ target: DERIVATIVE_QUESTIONS_PATH,
35306
35349
  body,
35307
35350
  label: "Register derivative question"
35308
35351
  },
@@ -35310,10 +35353,10 @@ async function registerQuestion(params) {
35310
35353
  );
35311
35354
  }
35312
35355
  async function getQuestion(params) {
35313
- const path = assertQuestionId(params.questionId);
35356
+ const target = assertQuestionId(params.questionId);
35314
35357
  return sendQuestionRequest(
35315
35358
  params,
35316
- { method: "GET", path, label: "Read derivative question" },
35359
+ { method: "GET", target, label: "Read derivative question" },
35317
35360
  DerivativeQuestionSchema
35318
35361
  );
35319
35362
  }
@@ -35323,12 +35366,12 @@ async function listQuestions(params) {
35323
35366
  "derivedScope is required; a builder may only list its own questions on a scope it may write"
35324
35367
  );
35325
35368
  }
35369
+ const target = `${DERIVATIVE_QUESTIONS_PATH}?derivedScope=${encodeURIComponent(params.derivedScope)}`;
35326
35370
  const { questions } = await sendQuestionRequest(
35327
35371
  params,
35328
35372
  {
35329
35373
  method: "GET",
35330
- path: DERIVATIVE_QUESTIONS_PATH,
35331
- query: `?derivedScope=${encodeURIComponent(params.derivedScope)}`,
35374
+ target,
35332
35375
  label: "List derivative questions"
35333
35376
  },
35334
35377
  QuestionListSchema
@@ -35336,18 +35379,18 @@ async function listQuestions(params) {
35336
35379
  return questions;
35337
35380
  }
35338
35381
  async function recomputeQuestion(params) {
35339
- const path = `${assertQuestionId(params.questionId)}/recompute`;
35382
+ const target = `${assertQuestionId(params.questionId)}/recompute`;
35340
35383
  return sendQuestionRequest(
35341
35384
  params,
35342
- { method: "POST", path, label: "Recompute derivative question" },
35385
+ { method: "POST", target, label: "Recompute derivative question" },
35343
35386
  QuestionRecomputeResultSchema
35344
35387
  );
35345
35388
  }
35346
35389
  async function deleteQuestion(params) {
35347
- const path = assertQuestionId(params.questionId);
35390
+ const target = assertQuestionId(params.questionId);
35348
35391
  return sendQuestionRequest(
35349
35392
  params,
35350
- { method: "DELETE", path, label: "Delete derivative question" },
35393
+ { method: "DELETE", target, label: "Delete derivative question" },
35351
35394
  QuestionDeleteResultSchema
35352
35395
  );
35353
35396
  }
@@ -36512,6 +36555,7 @@ export {
36512
36555
  DataPointVersionConflictError,
36513
36556
  DerivativeComputeUnavailableError,
36514
36557
  DerivativeCycleError,
36558
+ DerivativeDerivedScopeRequiredError,
36515
36559
  DerivativeQuestionFailedError,
36516
36560
  DerivativeQuestionInvalidError,
36517
36561
  DerivativeQuestionNotFoundError,