@opendatalabs/vana-sdk 3.19.0 → 3.20.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/README.md +82 -15
- package/dist/auth/web3-signed-builder.cjs +3 -0
- package/dist/auth/web3-signed-builder.cjs.map +1 -1
- package/dist/auth/web3-signed-builder.d.ts +14 -0
- package/dist/auth/web3-signed-builder.js +3 -0
- package/dist/auth/web3-signed-builder.js.map +1 -1
- package/dist/direct/controller.cjs +3 -2
- package/dist/direct/controller.cjs.map +1 -1
- package/dist/direct/controller.d.ts +19 -2
- package/dist/direct/controller.js +3 -2
- package/dist/direct/controller.js.map +1 -1
- package/dist/errors.cjs +13 -0
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +21 -2
- package/dist/errors.js +12 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.browser.js +69 -25
- package/dist/index.browser.js.map +2 -2
- package/dist/index.node.cjs +70 -25
- package/dist/index.node.cjs.map +2 -2
- package/dist/index.node.js +69 -25
- package/dist/index.node.js.map +2 -2
- package/dist/protocol/derivative-questions.cjs +42 -25
- package/dist/protocol/derivative-questions.cjs.map +1 -1
- package/dist/protocol/derivative-questions.d.ts +51 -10
- package/dist/protocol/derivative-questions.js +47 -26
- package/dist/protocol/derivative-questions.js.map +1 -1
- package/dist/protocol/write-request.cjs +14 -0
- package/dist/protocol/write-request.cjs.map +1 -1
- package/dist/protocol/write-request.d.ts +12 -0
- package/dist/protocol/write-request.js +13 -0
- package/dist/protocol/write-request.js.map +1 -1
- package/dist/server.cjs +28 -2
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +2 -0
- package/dist/server.js +29 -1
- package/dist/server.js.map +1 -1
- package/dist/tests/mock-personal-server.d.ts +9 -6
- package/package.json +1 -1
package/dist/index.node.js
CHANGED
|
@@ -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 =
|
|
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
|
-
|
|
35116
|
-
|
|
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.
|
|
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
|
|
35181
|
-
//
|
|
35182
|
-
|
|
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.
|
|
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
|
-
|
|
35208
|
-
|
|
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
|
-
|
|
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
|
|
35356
|
+
const target = assertQuestionId(params.questionId);
|
|
35314
35357
|
return sendQuestionRequest(
|
|
35315
35358
|
params,
|
|
35316
|
-
{ method: "GET",
|
|
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
|
-
|
|
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
|
|
35382
|
+
const target = `${assertQuestionId(params.questionId)}/recompute`;
|
|
35340
35383
|
return sendQuestionRequest(
|
|
35341
35384
|
params,
|
|
35342
|
-
{ method: "POST",
|
|
35385
|
+
{ method: "POST", target, label: "Recompute derivative question" },
|
|
35343
35386
|
QuestionRecomputeResultSchema
|
|
35344
35387
|
);
|
|
35345
35388
|
}
|
|
35346
35389
|
async function deleteQuestion(params) {
|
|
35347
|
-
const
|
|
35390
|
+
const target = assertQuestionId(params.questionId);
|
|
35348
35391
|
return sendQuestionRequest(
|
|
35349
35392
|
params,
|
|
35350
|
-
{ method: "DELETE",
|
|
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,
|