@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.cjs
CHANGED
|
@@ -1196,6 +1196,7 @@ __export(index_node_exports, {
|
|
|
1196
1196
|
DataPointVersionConflictError: () => DataPointVersionConflictError,
|
|
1197
1197
|
DerivativeComputeUnavailableError: () => DerivativeComputeUnavailableError,
|
|
1198
1198
|
DerivativeCycleError: () => DerivativeCycleError,
|
|
1199
|
+
DerivativeDerivedScopeRequiredError: () => DerivativeDerivedScopeRequiredError,
|
|
1199
1200
|
DerivativeQuestionFailedError: () => DerivativeQuestionFailedError,
|
|
1200
1201
|
DerivativeQuestionInvalidError: () => DerivativeQuestionInvalidError,
|
|
1201
1202
|
DerivativeQuestionNotFoundError: () => DerivativeQuestionNotFoundError,
|
|
@@ -1655,6 +1656,17 @@ var DerivativeQuestionNotFoundError = class extends PersonalServerWriteError {
|
|
|
1655
1656
|
super(message, "DERIVATIVE_QUESTION_NOT_FOUND", 404, errorCode, details);
|
|
1656
1657
|
}
|
|
1657
1658
|
};
|
|
1659
|
+
var DerivativeDerivedScopeRequiredError = class extends PersonalServerWriteError {
|
|
1660
|
+
constructor(message, errorCode = null, details) {
|
|
1661
|
+
super(
|
|
1662
|
+
message,
|
|
1663
|
+
"DERIVATIVE_DERIVED_SCOPE_REQUIRED",
|
|
1664
|
+
400,
|
|
1665
|
+
errorCode,
|
|
1666
|
+
details
|
|
1667
|
+
);
|
|
1668
|
+
}
|
|
1669
|
+
};
|
|
1658
1670
|
var DerivativeSourceNotGrantedError = class extends PersonalServerWriteError {
|
|
1659
1671
|
constructor(message, errorCode = null, details) {
|
|
1660
1672
|
super(message, "DERIVATIVE_SOURCE_NOT_GRANTED", 403, errorCode, details);
|
|
@@ -29431,6 +29443,9 @@ async function buildWeb3SignedHeader(params) {
|
|
|
29431
29443
|
if (params.grantId !== void 0) {
|
|
29432
29444
|
payload["grantId"] = params.grantId;
|
|
29433
29445
|
}
|
|
29446
|
+
if (params.nonce !== void 0) {
|
|
29447
|
+
payload["nonce"] = params.nonce;
|
|
29448
|
+
}
|
|
29434
29449
|
const sortedPayload = Object.keys(payload).sort().reduce((acc, key) => {
|
|
29435
29450
|
acc[key] = payload[key];
|
|
29436
29451
|
return acc;
|
|
@@ -34755,6 +34770,18 @@ function nextProofIat(proofKey) {
|
|
|
34755
34770
|
if (waitSec <= 0) return Promise.resolve(iat);
|
|
34756
34771
|
return sleep2(waitSec * 1e3).then(() => iat);
|
|
34757
34772
|
}
|
|
34773
|
+
function freshProofNonce() {
|
|
34774
|
+
const webCrypto = globalThis.crypto;
|
|
34775
|
+
if (typeof webCrypto?.randomUUID === "function") {
|
|
34776
|
+
return webCrypto.randomUUID();
|
|
34777
|
+
}
|
|
34778
|
+
if (typeof webCrypto?.getRandomValues === "function") {
|
|
34779
|
+
return (0, import_viem23.bytesToHex)(webCrypto.getRandomValues(new Uint8Array(16)));
|
|
34780
|
+
}
|
|
34781
|
+
throw new WriteRequestError(
|
|
34782
|
+
"No secure random source available to build a proof nonce; provide a crypto global"
|
|
34783
|
+
);
|
|
34784
|
+
}
|
|
34758
34785
|
function proofKeyFor(parts) {
|
|
34759
34786
|
return (0, import_viem23.bytesToHex)(
|
|
34760
34787
|
(0, import_sha25.sha256)(
|
|
@@ -35296,12 +35323,7 @@ var DerivativeQuestionSchema = import_zod5.z.object({
|
|
|
35296
35323
|
var QuestionListSchema = import_zod5.z.object({
|
|
35297
35324
|
questions: import_zod5.z.array(DerivativeQuestionSchema)
|
|
35298
35325
|
});
|
|
35299
|
-
var QuestionRecomputeResultSchema =
|
|
35300
|
-
questionId: import_zod5.z.string().min(1),
|
|
35301
|
-
derivedScope: import_zod5.z.string().min(1),
|
|
35302
|
-
/** `pending` when the question was never computed, else `stale`. */
|
|
35303
|
-
status: QuestionStatusSchema
|
|
35304
|
-
});
|
|
35326
|
+
var QuestionRecomputeResultSchema = DerivativeQuestionSchema;
|
|
35305
35327
|
var QuestionDeleteResultSchema = import_zod5.z.object({
|
|
35306
35328
|
questionId: import_zod5.z.string().min(1),
|
|
35307
35329
|
deleted: import_zod5.z.literal(true)
|
|
@@ -35369,8 +35391,18 @@ async function resolveSession(params, resolved, force) {
|
|
|
35369
35391
|
cache.set(resolved.cacheKey, session);
|
|
35370
35392
|
return session;
|
|
35371
35393
|
}
|
|
35372
|
-
|
|
35373
|
-
|
|
35394
|
+
var PROOF_FAILURE_CODES = /* @__PURE__ */ new Set([
|
|
35395
|
+
"WRITE_ATTRIBUTION_REQUIRED",
|
|
35396
|
+
"WRITE_ATTRIBUTION_INVALID",
|
|
35397
|
+
"WRITE_ATTRIBUTION_SIGNER_MISMATCH",
|
|
35398
|
+
"WRITE_ATTRIBUTION_GRANT_MISMATCH",
|
|
35399
|
+
"WRITE_ATTRIBUTION_REPLAY"
|
|
35400
|
+
]);
|
|
35401
|
+
function isStaleSession(errorCode) {
|
|
35402
|
+
return errorCode === null || !PROOF_FAILURE_CODES.has(errorCode);
|
|
35403
|
+
}
|
|
35404
|
+
async function questionErrorFromResponse(response, body) {
|
|
35405
|
+
const { errorCode, message, details } = body ?? await readPersonalServerErrorBody(response);
|
|
35374
35406
|
const text = message ?? `Derivative question request failed: ${response.status} ${response.statusText}`;
|
|
35375
35407
|
switch (errorCode) {
|
|
35376
35408
|
case "DERIVATIVE_SOURCE_NOT_GRANTED":
|
|
@@ -35389,6 +35421,8 @@ async function questionErrorFromResponse(response) {
|
|
|
35389
35421
|
);
|
|
35390
35422
|
case "DERIVATIVE_QUESTION_NOT_FOUND":
|
|
35391
35423
|
return new DerivativeQuestionNotFoundError(text, errorCode, details);
|
|
35424
|
+
case "DERIVATIVE_DERIVED_SCOPE_REQUIRED":
|
|
35425
|
+
return new DerivativeDerivedScopeRequiredError(text, errorCode, details);
|
|
35392
35426
|
default:
|
|
35393
35427
|
break;
|
|
35394
35428
|
}
|
|
@@ -35418,7 +35452,7 @@ async function sendOnce(params, resolved, session, spec, bodyBytes) {
|
|
|
35418
35452
|
proofKeyFor({
|
|
35419
35453
|
aud: session.audience,
|
|
35420
35454
|
method: spec.method,
|
|
35421
|
-
uri: spec.
|
|
35455
|
+
uri: spec.target,
|
|
35422
35456
|
grantId: session.grantId,
|
|
35423
35457
|
signedBytes: bodyBytes
|
|
35424
35458
|
}),
|
|
@@ -35434,17 +35468,22 @@ async function sendOnce(params, resolved, session, spec, bodyBytes) {
|
|
|
35434
35468
|
await buildWeb3SignedHeader({
|
|
35435
35469
|
signMessage: session.signer.signMessage,
|
|
35436
35470
|
aud: session.audience,
|
|
35437
|
-
// The
|
|
35438
|
-
//
|
|
35439
|
-
|
|
35471
|
+
// The proof commits to the whole request target, query included:
|
|
35472
|
+
// `?derivedScope=` is what the list route authorizes against, and a
|
|
35473
|
+
// proof that did not cover it would authorize any other scope.
|
|
35474
|
+
uri: spec.target,
|
|
35440
35475
|
method: spec.method,
|
|
35441
35476
|
body: bodyBytes,
|
|
35442
35477
|
grantId: session.grantId,
|
|
35478
|
+
// Fresh per attempt, so a retry after a thrown `fetch` is never the
|
|
35479
|
+
// proof the server may already have consumed, and so two identical
|
|
35480
|
+
// polls inside one second stay distinct.
|
|
35481
|
+
nonce: freshProofNonce(),
|
|
35443
35482
|
iat
|
|
35444
35483
|
})
|
|
35445
35484
|
);
|
|
35446
35485
|
return {
|
|
35447
|
-
url: `${resolved.baseUrl}${spec.
|
|
35486
|
+
url: `${resolved.baseUrl}${spec.target}`,
|
|
35448
35487
|
init: {
|
|
35449
35488
|
method: spec.method,
|
|
35450
35489
|
headers,
|
|
@@ -35460,12 +35499,17 @@ async function sendQuestionRequest(params, spec, schema) {
|
|
|
35460
35499
|
const bodyBytes = spec.body === void 0 ? void 0 : new TextEncoder().encode(JSON.stringify(spec.body));
|
|
35461
35500
|
let session = await resolveSession(params, resolved, false);
|
|
35462
35501
|
let response = await sendOnce(params, resolved, session, spec, bodyBytes);
|
|
35502
|
+
let errorBody;
|
|
35463
35503
|
if (response.status === 401) {
|
|
35464
|
-
|
|
35465
|
-
|
|
35504
|
+
errorBody = await readPersonalServerErrorBody(response);
|
|
35505
|
+
if (isStaleSession(errorBody.errorCode)) {
|
|
35506
|
+
session = await resolveSession(params, resolved, true);
|
|
35507
|
+
response = await sendOnce(params, resolved, session, spec, bodyBytes);
|
|
35508
|
+
errorBody = void 0;
|
|
35509
|
+
}
|
|
35466
35510
|
}
|
|
35467
35511
|
if (!response.ok) {
|
|
35468
|
-
throw await questionErrorFromResponse(response);
|
|
35512
|
+
throw await questionErrorFromResponse(response, errorBody);
|
|
35469
35513
|
}
|
|
35470
35514
|
let body;
|
|
35471
35515
|
try {
|
|
@@ -35559,7 +35603,7 @@ async function registerQuestion(params) {
|
|
|
35559
35603
|
params,
|
|
35560
35604
|
{
|
|
35561
35605
|
method: "POST",
|
|
35562
|
-
|
|
35606
|
+
target: DERIVATIVE_QUESTIONS_PATH,
|
|
35563
35607
|
body,
|
|
35564
35608
|
label: "Register derivative question"
|
|
35565
35609
|
},
|
|
@@ -35567,10 +35611,10 @@ async function registerQuestion(params) {
|
|
|
35567
35611
|
);
|
|
35568
35612
|
}
|
|
35569
35613
|
async function getQuestion(params) {
|
|
35570
|
-
const
|
|
35614
|
+
const target = assertQuestionId(params.questionId);
|
|
35571
35615
|
return sendQuestionRequest(
|
|
35572
35616
|
params,
|
|
35573
|
-
{ method: "GET",
|
|
35617
|
+
{ method: "GET", target, label: "Read derivative question" },
|
|
35574
35618
|
DerivativeQuestionSchema
|
|
35575
35619
|
);
|
|
35576
35620
|
}
|
|
@@ -35580,12 +35624,12 @@ async function listQuestions(params) {
|
|
|
35580
35624
|
"derivedScope is required; a builder may only list its own questions on a scope it may write"
|
|
35581
35625
|
);
|
|
35582
35626
|
}
|
|
35627
|
+
const target = `${DERIVATIVE_QUESTIONS_PATH}?derivedScope=${encodeURIComponent(params.derivedScope)}`;
|
|
35583
35628
|
const { questions } = await sendQuestionRequest(
|
|
35584
35629
|
params,
|
|
35585
35630
|
{
|
|
35586
35631
|
method: "GET",
|
|
35587
|
-
|
|
35588
|
-
query: `?derivedScope=${encodeURIComponent(params.derivedScope)}`,
|
|
35632
|
+
target,
|
|
35589
35633
|
label: "List derivative questions"
|
|
35590
35634
|
},
|
|
35591
35635
|
QuestionListSchema
|
|
@@ -35593,18 +35637,18 @@ async function listQuestions(params) {
|
|
|
35593
35637
|
return questions;
|
|
35594
35638
|
}
|
|
35595
35639
|
async function recomputeQuestion(params) {
|
|
35596
|
-
const
|
|
35640
|
+
const target = `${assertQuestionId(params.questionId)}/recompute`;
|
|
35597
35641
|
return sendQuestionRequest(
|
|
35598
35642
|
params,
|
|
35599
|
-
{ method: "POST",
|
|
35643
|
+
{ method: "POST", target, label: "Recompute derivative question" },
|
|
35600
35644
|
QuestionRecomputeResultSchema
|
|
35601
35645
|
);
|
|
35602
35646
|
}
|
|
35603
35647
|
async function deleteQuestion(params) {
|
|
35604
|
-
const
|
|
35648
|
+
const target = assertQuestionId(params.questionId);
|
|
35605
35649
|
return sendQuestionRequest(
|
|
35606
35650
|
params,
|
|
35607
|
-
{ method: "DELETE",
|
|
35651
|
+
{ method: "DELETE", target, label: "Delete derivative question" },
|
|
35608
35652
|
QuestionDeleteResultSchema
|
|
35609
35653
|
);
|
|
35610
35654
|
}
|
|
@@ -36770,6 +36814,7 @@ async function parsePSError(response) {
|
|
|
36770
36814
|
DataPointVersionConflictError,
|
|
36771
36815
|
DerivativeComputeUnavailableError,
|
|
36772
36816
|
DerivativeCycleError,
|
|
36817
|
+
DerivativeDerivedScopeRequiredError,
|
|
36773
36818
|
DerivativeQuestionFailedError,
|
|
36774
36819
|
DerivativeQuestionInvalidError,
|
|
36775
36820
|
DerivativeQuestionNotFoundError,
|