@opendatalabs/personal-server-ts-core 0.0.1-canary.6084f63 → 0.0.1-canary.623e793
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/api/index.d.ts +64 -4
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +321 -43
- package/dist/api/index.js.map +1 -1
- package/dist/client.d.ts +33 -8
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +18 -7
- package/dist/client.js.map +1 -1
- package/dist/contracts/binary.d.ts +54 -0
- package/dist/contracts/binary.d.ts.map +1 -0
- package/dist/contracts/binary.js +92 -0
- package/dist/contracts/binary.js.map +1 -0
- package/dist/contracts/data.d.ts +20 -3
- package/dist/contracts/data.d.ts.map +1 -1
- package/dist/contracts/data.js +99 -4
- package/dist/contracts/data.js.map +1 -1
- package/dist/contracts/grants.d.ts +7 -5
- package/dist/contracts/grants.d.ts.map +1 -1
- package/dist/contracts/grants.js +60 -37
- package/dist/contracts/grants.js.map +1 -1
- package/dist/contracts/index.d.ts +4 -2
- package/dist/contracts/index.d.ts.map +1 -1
- package/dist/contracts/index.js +2 -1
- package/dist/contracts/index.js.map +1 -1
- package/dist/grants/index.d.ts +2 -1
- package/dist/grants/index.d.ts.map +1 -1
- package/dist/grants/index.js +1 -1
- package/dist/grants/index.js.map +1 -1
- package/dist/mcp/activity.d.ts +68 -0
- package/dist/mcp/activity.d.ts.map +1 -0
- package/dist/mcp/activity.js +111 -0
- package/dist/mcp/activity.js.map +1 -0
- package/dist/mcp/builder-registration.d.ts +24 -0
- package/dist/mcp/builder-registration.d.ts.map +1 -0
- package/dist/mcp/builder-registration.js +89 -0
- package/dist/mcp/builder-registration.js.map +1 -0
- package/dist/mcp/connection-api.d.ts +170 -0
- package/dist/mcp/connection-api.d.ts.map +1 -0
- package/dist/mcp/connection-api.js +379 -0
- package/dist/mcp/connection-api.js.map +1 -0
- package/dist/mcp/grantee.d.ts +52 -0
- package/dist/mcp/grantee.d.ts.map +1 -0
- package/dist/mcp/grantee.js +91 -0
- package/dist/mcp/grantee.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +10 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/read-client.d.ts +136 -0
- package/dist/mcp/read-client.d.ts.map +1 -0
- package/dist/mcp/read-client.js +287 -0
- package/dist/mcp/read-client.js.map +1 -0
- package/dist/mcp/resources.d.ts +37 -0
- package/dist/mcp/resources.d.ts.map +1 -0
- package/dist/mcp/resources.js +114 -0
- package/dist/mcp/resources.js.map +1 -0
- package/dist/mcp/search/index.d.ts +34 -0
- package/dist/mcp/search/index.d.ts.map +1 -0
- package/dist/mcp/search/index.js +69 -0
- package/dist/mcp/search/index.js.map +1 -0
- package/dist/mcp/server.d.ts +53 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +308 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/store.d.ts +13 -0
- package/dist/mcp/store.d.ts.map +1 -0
- package/dist/mcp/store.js +98 -0
- package/dist/mcp/store.js.map +1 -0
- package/dist/mcp/tools.d.ts +60 -0
- package/dist/mcp/tools.d.ts.map +1 -0
- package/dist/mcp/tools.js +1058 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/mcp/types.d.ts +97 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +24 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/payment/index.d.ts +2 -0
- package/dist/payment/index.d.ts.map +1 -0
- package/dist/payment/index.js +2 -0
- package/dist/payment/index.js.map +1 -0
- package/dist/payment/x402.d.ts +155 -0
- package/dist/payment/x402.d.ts.map +1 -0
- package/dist/payment/x402.js +402 -0
- package/dist/payment/x402.js.map +1 -0
- package/dist/policy/data-read.d.ts +1 -2
- package/dist/policy/data-read.d.ts.map +1 -1
- package/dist/policy/data-read.js +22 -40
- package/dist/policy/data-read.js.map +1 -1
- package/dist/ports/index.d.ts +32 -23
- package/dist/ports/index.d.ts.map +1 -1
- package/dist/ports/index.js +6 -5
- package/dist/ports/index.js.map +1 -1
- package/dist/schemas/server-config.d.ts +12 -0
- package/dist/schemas/server-config.d.ts.map +1 -1
- package/dist/schemas/server-config.js +33 -3
- package/dist/schemas/server-config.js.map +1 -1
- package/dist/signing/signer.d.ts +20 -2
- package/dist/signing/signer.d.ts.map +1 -1
- package/dist/signing/signer.js +23 -13
- package/dist/signing/signer.js.map +1 -1
- package/dist/storage/adapters/interface.d.ts +10 -0
- package/dist/storage/adapters/interface.d.ts.map +1 -1
- package/dist/storage/adapters/sdk.d.ts +11 -1
- package/dist/storage/adapters/sdk.d.ts.map +1 -1
- package/dist/storage/adapters/sdk.js +3 -1
- package/dist/storage/adapters/sdk.js.map +1 -1
- package/dist/storage/adapters/vana.d.ts.map +1 -1
- package/dist/storage/adapters/vana.js +25 -2
- package/dist/storage/adapters/vana.js.map +1 -1
- package/dist/storage/blocks/build.d.ts +20 -0
- package/dist/storage/blocks/build.d.ts.map +1 -0
- package/dist/storage/blocks/build.js +378 -0
- package/dist/storage/blocks/build.js.map +1 -0
- package/dist/storage/blocks/cursor.d.ts +33 -0
- package/dist/storage/blocks/cursor.d.ts.map +1 -0
- package/dist/storage/blocks/cursor.js +142 -0
- package/dist/storage/blocks/cursor.js.map +1 -0
- package/dist/storage/blocks/errors.d.ts +6 -0
- package/dist/storage/blocks/errors.d.ts.map +1 -0
- package/dist/storage/blocks/errors.js +9 -0
- package/dist/storage/blocks/errors.js.map +1 -0
- package/dist/storage/blocks/index.d.ts +5 -0
- package/dist/storage/blocks/index.d.ts.map +1 -0
- package/dist/storage/blocks/index.js +4 -0
- package/dist/storage/blocks/index.js.map +1 -0
- package/dist/storage/blocks/types.d.ts +49 -0
- package/dist/storage/blocks/types.d.ts.map +1 -0
- package/dist/storage/blocks/types.js +2 -0
- package/dist/storage/blocks/types.js.map +1 -0
- package/dist/storage/index/manager.d.ts +16 -2
- package/dist/storage/index/manager.d.ts.map +1 -1
- package/dist/storage/index/schema.d.ts +1 -1
- package/dist/storage/index/schema.js +1 -1
- package/dist/storage/index/types.d.ts +13 -1
- package/dist/storage/index/types.d.ts.map +1 -1
- package/dist/storage/preview.d.ts +12 -0
- package/dist/storage/preview.d.ts.map +1 -0
- package/dist/storage/preview.js +239 -0
- package/dist/storage/preview.js.map +1 -0
- package/dist/sync/engine/sync-manager.d.ts +6 -0
- package/dist/sync/engine/sync-manager.d.ts.map +1 -1
- package/dist/sync/engine/sync-manager.js +12 -2
- package/dist/sync/engine/sync-manager.js.map +1 -1
- package/dist/sync/index.d.ts +2 -0
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +1 -0
- package/dist/sync/index.js.map +1 -1
- package/dist/sync/issues.d.ts +55 -0
- package/dist/sync/issues.d.ts.map +1 -0
- package/dist/sync/issues.js +150 -0
- package/dist/sync/issues.js.map +1 -0
- package/dist/sync/workers/delete.d.ts +42 -0
- package/dist/sync/workers/delete.d.ts.map +1 -0
- package/dist/sync/workers/delete.js +40 -0
- package/dist/sync/workers/delete.js.map +1 -0
- package/dist/sync/workers/download.d.ts +53 -12
- package/dist/sync/workers/download.d.ts.map +1 -1
- package/dist/sync/workers/download.js +344 -58
- package/dist/sync/workers/download.js.map +1 -1
- package/dist/sync/workers/upload.d.ts +14 -8
- package/dist/sync/workers/upload.d.ts.map +1 -1
- package/dist/sync/workers/upload.js +68 -29
- package/dist/sync/workers/upload.js.map +1 -1
- package/package.json +25 -2
package/dist/api/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AccessLogWriter } from "../logging/access-log.js";
|
|
2
2
|
import type { AccessLogReader } from "../logging/access-reader.js";
|
|
3
|
-
import { type DataStoragePort, type RuntimeAvailabilityPort
|
|
3
|
+
import { type DataStoragePort, type RuntimeAvailabilityPort } from "../ports/index.js";
|
|
4
4
|
import type { DataReadPolicyPorts } from "../policy/index.js";
|
|
5
5
|
import type { SyncManager } from "../sync/index.js";
|
|
6
6
|
import { type OAuthDeviceSessionLookup, type OAuthTokenStorePort } from "../contracts/index.js";
|
|
@@ -23,20 +23,80 @@ export interface PersonalServerApiAuthPort {
|
|
|
23
23
|
}
|
|
24
24
|
export interface PersonalServerApiLogger {
|
|
25
25
|
info?(payload: Record<string, unknown>, message: string): void;
|
|
26
|
+
warn?(payload: Record<string, unknown>, message: string): void;
|
|
26
27
|
error?(payload: Record<string, unknown>, message: string): void;
|
|
27
28
|
}
|
|
28
29
|
export interface PersonalServerIngestSyncManager {
|
|
29
30
|
trigger(): Promise<void>;
|
|
30
31
|
notifyNewData?(): void;
|
|
32
|
+
/** Propagate a scope deletion to R2 + the gateway before the local delete. */
|
|
33
|
+
deleteScopeRemote?(scope: string): Promise<void>;
|
|
31
34
|
}
|
|
32
35
|
export interface PersonalServerDataApiDeps {
|
|
33
36
|
storage: DataStoragePort;
|
|
34
37
|
auth: PersonalServerApiAuthPort;
|
|
35
|
-
schemaResolver?: SchemaResolverPort;
|
|
36
38
|
accessLogWriter: AccessLogWriter;
|
|
37
39
|
syncManager?: PersonalServerIngestSyncManager | null;
|
|
38
40
|
runtimeAvailability?: RuntimeAvailabilityPort;
|
|
39
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Required when payment is on. Powers two things on GET /v1/data/:scope:
|
|
43
|
+
* - the X402 challenge generation (fee lookup, accessRecord binding)
|
|
44
|
+
* - the forward of validated X-PAYMENTs to gateway.payForOperation
|
|
45
|
+
*
|
|
46
|
+
* Two channels because the SDK client's payForOperation throws plain
|
|
47
|
+
* Errors on non-2xx and loses the gateway's structured error body — we
|
|
48
|
+
* need that body to map gateway 402/409/400 into fresh challenges.
|
|
49
|
+
*/
|
|
50
|
+
gateway?: Pick<GatewayClient, "getGrant">;
|
|
51
|
+
/**
|
|
52
|
+
* Gateway base URL. Used for the direct-fetch forwarding of validated
|
|
53
|
+
* X-PAYMENTs to POST /v1/escrow/pay so we can inspect the gateway's
|
|
54
|
+
* structured error body (which the SDK's gateway.payForOperation
|
|
55
|
+
* discards). Required when X402 is enabled.
|
|
56
|
+
*/
|
|
57
|
+
gatewayUrl?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Required to construct escrowPaymentDomain + dataRegistryDomain for
|
|
60
|
+
* EIP-712 signature recovery during X-PAYMENT validation.
|
|
61
|
+
*/
|
|
62
|
+
gatewayConfig?: DataPortabilityGatewayConfig;
|
|
63
|
+
/**
|
|
64
|
+
* Required for the X402 flow. Signs RECORD_DATA_ACCESS attestations
|
|
65
|
+
* embedded in 402 challenges. Without it, challenges still issue but
|
|
66
|
+
* never include an accessRecord — gateway accepts the resulting payment
|
|
67
|
+
* but the on-chain recordDataAccess won't be scheduled.
|
|
68
|
+
*/
|
|
69
|
+
serverSigner?: Pick<ServerSigner, "signRecordDataAccess">;
|
|
70
|
+
serverOwner?: `0x${string}`;
|
|
71
|
+
/**
|
|
72
|
+
* The server's own account address — accessRecord signatures must
|
|
73
|
+
* recover to this for the X-PAYMENT validation to accept them as
|
|
74
|
+
* server-issued.
|
|
75
|
+
*/
|
|
76
|
+
serverAddress?: `0x${string}`;
|
|
77
|
+
/**
|
|
78
|
+
* Identifier echoed in the X402 challenge as `accepts[].network`. Pure
|
|
79
|
+
* convention (e.g. "vana-moksha"); the gateway doesn't read it.
|
|
80
|
+
*/
|
|
81
|
+
network?: string;
|
|
82
|
+
/**
|
|
83
|
+
* When true, GET /v1/data/:scope enforces the X402 dance: missing /
|
|
84
|
+
* invalid X-PAYMENT → 402 challenge; valid → forward to gateway then
|
|
85
|
+
* serve. When false (default), reads bypass payment entirely.
|
|
86
|
+
*/
|
|
87
|
+
paymentEnabled?: boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Test seam for the gateway forwarding fetch. Defaults to the global
|
|
90
|
+
* `fetch`; tests inject a mock that returns specific gateway statuses.
|
|
91
|
+
*/
|
|
92
|
+
paymentFetch?: typeof fetch;
|
|
93
|
+
/**
|
|
94
|
+
* Test seam for the per-event recordId / paymentNonce / clock. Production
|
|
95
|
+
* leaves these unset and the X402 module generates fresh values via
|
|
96
|
+
* crypto.getRandomValues / Date.now.
|
|
97
|
+
*/
|
|
98
|
+
generateRecordId?: () => `0x${string}`;
|
|
99
|
+
generatePaymentNonce?: () => bigint;
|
|
40
100
|
now?: () => Date;
|
|
41
101
|
createLogId?: () => string;
|
|
42
102
|
logger?: PersonalServerApiLogger;
|
|
@@ -52,7 +112,7 @@ export interface PersonalServerSyncApiDeps {
|
|
|
52
112
|
}
|
|
53
113
|
export interface PersonalServerGrantsApiDeps {
|
|
54
114
|
auth: Pick<PersonalServerApiAuthPort, "authorizeOwner">;
|
|
55
|
-
gateway?: Pick<GatewayClient, "getBuilder" | "createGrant" | "listGrantsByUser" | "revokeGrant">;
|
|
115
|
+
gateway?: Pick<GatewayClient, "getBuilder" | "createGrant" | "listGrantsByUser" | "revokeGrant" | "getGrant">;
|
|
56
116
|
gatewayConfig?: DataPortabilityGatewayConfig;
|
|
57
117
|
serverOwner?: `0x${string}`;
|
|
58
118
|
serverSigner?: Pick<ServerSigner, "signGrantRegistration"> & Partial<Pick<ServerSigner, "signGrantRevocation">>;
|
package/dist/api/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,uBAAuB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EACL,KAAK,eAAe,E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AAwB,CAAC;IAC1C,WAAW,IAAI,MAAM,CAAC;IACtB,GAAG,CAAC,EAAE,MAAM,IAAI,CAAC;IACjB,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;CACxD;AAED,MAAM,WAAW,gCAAgC;IAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAyVD,wBAAsB,+BAA+B,CACnD,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,yBAAyB,EAC/B,OAAO,GAAE,gCAAqC,GAC7C,OAAO,CAAC,QAAQ,CAAC,CAiQnB;AAED,wBAAsB,qCAAqC,CACzD,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,+BAA+B,EACrC,OAAO,GAAE,gCAAqC,GAC7C,OAAO,CAAC,QAAQ,CAAC,CAenB;AAED,wBAAsB,+BAA+B,CACnD,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,yBAAyB,EAC/B,OAAO,GAAE,gCAAqC,GAC7C,OAAO,CAAC,QAAQ,CAAC,CAmCnB;AAED,wBAAsB,iCAAiC,CACrD,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,2BAA2B,EACjC,OAAO,GAAE,gCAAqC,GAC7C,OAAO,CAAC,QAAQ,CAAC,CAyEnB;AAED,wBAAsB,iCAAiC,CACrD,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,2BAA2B,GAChC,OAAO,CAAC,QAAQ,CAAC,CA8BnB;AAED,wBAAsB,qCAAqC,CACzD,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,+BAA+B,GACpC,OAAO,CAAC,QAAQ,CAAC,CA6BnB;AAED,YAAY,EAAE,mBAAmB,EAAE,CAAC"}
|
package/dist/api/index.js
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
import { ProtocolError } from "../errors/catalog.js";
|
|
2
|
-
import { deleteDataScopeContract, ingestDataContract, listAccessLogsContract, listDataScopesContract, listDataVersionsContract, parseDataScopeContract, parseJsonObjectBody, readDataContract, syncFileContract, triggerSyncContract, getSyncStatusContract, configReadErrorContract, configWriteErrorContract, createGrantContract, listGrantsContract, oauthTokenContract, revokeGrantContract, validateServerConfigContract, verifyGrantContract, } from "../contracts/index.js";
|
|
2
|
+
import { deleteDataScopeContract, ingestDataContract, ingestBinaryDataContract, listAccessLogsContract, listDataScopesContract, listDataVersionsContract, parseDataScopeContract, parseJsonObjectBody, readDataContract, syncFileContract, triggerSyncContract, getSyncStatusContract, configReadErrorContract, configWriteErrorContract, createGrantContract, listGrantsContract, oauthTokenContract, revokeGrantContract, validateServerConfigContract, verifyGrantContract, decodeBinaryEnvelope, isBinaryEnvelope, parseMetadataHeader, stringifyMetadataHeader, } from "../contracts/index.js";
|
|
3
|
+
import { buildChallenge, parsePaymentHeader, verifyPayment, } from "../payment/index.js";
|
|
3
4
|
function jsonResponse(body, init) {
|
|
4
5
|
const headers = new Headers(init?.headers);
|
|
5
6
|
headers.set("Content-Type", "application/json");
|
|
6
7
|
return new Response(JSON.stringify(body), { ...init, headers });
|
|
7
8
|
}
|
|
9
|
+
// X402 spec convention: payment responses are JSON, base64-encoded into a
|
|
10
|
+
// single header value. Returns undefined on encode failure so callers can
|
|
11
|
+
// just omit the header rather than fail the read. Uses btoa + TextEncoder
|
|
12
|
+
// to stay portable across Node and web runtimes.
|
|
13
|
+
function encodePaymentResponseHeader(body) {
|
|
14
|
+
try {
|
|
15
|
+
const json = JSON.stringify(body ?? null);
|
|
16
|
+
const bytes = new TextEncoder().encode(json);
|
|
17
|
+
let binary = "";
|
|
18
|
+
for (const b of bytes)
|
|
19
|
+
binary += String.fromCharCode(b);
|
|
20
|
+
return btoa(binary);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
8
26
|
function contractResponse(result) {
|
|
9
27
|
return jsonResponse(result.body, { status: result.status });
|
|
10
28
|
}
|
|
@@ -64,45 +82,192 @@ function selectedGrantId(request, url) {
|
|
|
64
82
|
request.headers.get("x-ps-grant-id") ??
|
|
65
83
|
undefined);
|
|
66
84
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Run the X402 dispatch for one read:
|
|
87
|
+
*
|
|
88
|
+
* X-PAYMENT absent / malformed / fails local validation
|
|
89
|
+
* → challenge: fresh 402 with current fee + accessRecord
|
|
90
|
+
* X-PAYMENT valid, forward to gateway succeeds
|
|
91
|
+
* → ok: caller proceeds to read the data
|
|
92
|
+
* X-PAYMENT valid, gateway returns 4xx/5xx
|
|
93
|
+
* → gateway-error: relay the gateway's body verbatim so the builder
|
|
94
|
+
* can dispatch (insufficient balance vs. replay vs. race etc.)
|
|
95
|
+
*
|
|
96
|
+
* The gateway POST goes through `fetch` directly (not `gateway.payForOperation`)
|
|
97
|
+
* because the SDK client throws plain Errors and loses the body — which is
|
|
98
|
+
* exactly what we need to distinguish gateway 402 (insufficient balance) from
|
|
99
|
+
* 409 (replay) from 400 (amount mismatch).
|
|
100
|
+
*/
|
|
101
|
+
async function handleX402Cycle(input) {
|
|
102
|
+
const { deps, gateway, gatewayConfig, gatewayUrl, builder, scope } = input;
|
|
103
|
+
// The gateway lowercases opId before EIP-712 recovery — we must do the
|
|
104
|
+
// same in the challenge so the builder signs over the canonical form.
|
|
105
|
+
const opIdLower = input.grantId.toLowerCase();
|
|
106
|
+
// Live grant — re-fetched every cycle. The fee.totalDue is a snapshot
|
|
107
|
+
// (SDK comment: "clients shouldn't cache"); paymentStatus may have just
|
|
108
|
+
// flipped if a concurrent payer paid the registration fee.
|
|
109
|
+
const grant = await gateway.getGrant(opIdLower);
|
|
110
|
+
if (!grant) {
|
|
74
111
|
return {
|
|
75
|
-
|
|
76
|
-
|
|
112
|
+
kind: "challenge",
|
|
113
|
+
body: {
|
|
114
|
+
x402Version: 1,
|
|
115
|
+
error: "PAYMENT_REQUIRED",
|
|
116
|
+
accepts: [],
|
|
117
|
+
},
|
|
77
118
|
};
|
|
78
119
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
120
|
+
// Bind the accessRecord to the entry being served, if it exists and has
|
|
121
|
+
// been registered with DPv2 yet.
|
|
122
|
+
const entryRow = deps.storage.findEntry({
|
|
123
|
+
scope,
|
|
124
|
+
fileId: input.fileIdParam,
|
|
125
|
+
at: input.atParam,
|
|
126
|
+
});
|
|
127
|
+
const entryForChallenge = entryRow
|
|
128
|
+
? {
|
|
129
|
+
dataPointId: entryRow.dataPointId,
|
|
130
|
+
scope,
|
|
131
|
+
version: entryRow.version,
|
|
89
132
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
133
|
+
: undefined;
|
|
134
|
+
async function buildFreshChallenge() {
|
|
135
|
+
return buildChallenge({
|
|
136
|
+
builder,
|
|
137
|
+
grantId: opIdLower,
|
|
138
|
+
grant,
|
|
139
|
+
network: deps.network ?? `vana:${gatewayConfig.chainId}`,
|
|
140
|
+
gatewayConfig,
|
|
141
|
+
serverSigner: deps.serverSigner,
|
|
142
|
+
serverOwner: deps.serverOwner,
|
|
143
|
+
entry: entryForChallenge,
|
|
144
|
+
generateNonce: deps.generatePaymentNonce,
|
|
145
|
+
generateRecordIdFn: deps.generateRecordId,
|
|
146
|
+
now: deps.now,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const headerValue = input.request.headers.get("x-payment");
|
|
150
|
+
const parsed = parsePaymentHeader(headerValue);
|
|
151
|
+
if (!parsed) {
|
|
152
|
+
return { kind: "challenge", body: await buildFreshChallenge() };
|
|
153
|
+
}
|
|
154
|
+
if (!deps.serverAddress) {
|
|
155
|
+
// Can't validate accessRecord recovery without our own address; fail
|
|
156
|
+
// closed by reissuing a challenge.
|
|
157
|
+
deps.logger?.error?.({ scope, grantId: opIdLower }, "X402 enabled but serverAddress is not configured — cannot verify accessRecord");
|
|
158
|
+
return { kind: "challenge", body: await buildFreshChallenge() };
|
|
159
|
+
}
|
|
160
|
+
const verify = await verifyPayment({
|
|
161
|
+
builder,
|
|
162
|
+
grantId: opIdLower,
|
|
163
|
+
grant,
|
|
164
|
+
entry: entryForChallenge,
|
|
165
|
+
serverAddress: deps.serverAddress,
|
|
166
|
+
gatewayConfig,
|
|
167
|
+
serverOwner: deps.serverOwner,
|
|
168
|
+
payment: parsed,
|
|
169
|
+
});
|
|
170
|
+
if (!verify.ok) {
|
|
171
|
+
deps.logger?.info?.({ scope, grantId: opIdLower, reason: verify.reason }, "X402 payment verification failed; reissuing challenge");
|
|
172
|
+
return { kind: "challenge", body: await buildFreshChallenge() };
|
|
173
|
+
}
|
|
174
|
+
// Forward to gateway via direct fetch (preserves error body). The SDK
|
|
175
|
+
// client wraps the same endpoint but throws away the structured response
|
|
176
|
+
// on non-2xx, which is exactly the info we need to distinguish gateway
|
|
177
|
+
// 402 (insufficient balance) from 409 (replay / race) from 400 (mismatch).
|
|
178
|
+
const doFetch = deps.paymentFetch ?? fetch;
|
|
179
|
+
const body = {
|
|
180
|
+
payerAddress: verify.payment.payload.message.payerAddress,
|
|
181
|
+
opType: verify.payment.payload.message.opType,
|
|
182
|
+
opId: verify.payment.payload.message.opId,
|
|
183
|
+
asset: verify.payment.payload.message.asset,
|
|
184
|
+
amount: verify.payment.payload.message.amount,
|
|
185
|
+
paymentNonce: verify.payment.payload.message.paymentNonce,
|
|
186
|
+
};
|
|
187
|
+
if (verify.payment.payload.accessRecord) {
|
|
188
|
+
body["accessRecord"] = verify.payment.payload.accessRecord;
|
|
189
|
+
}
|
|
190
|
+
let gatewayRes;
|
|
191
|
+
try {
|
|
192
|
+
gatewayRes = await doFetch(`${gatewayUrl.replace(/\/+$/, "")}/v1/escrow/pay`, {
|
|
193
|
+
method: "POST",
|
|
194
|
+
headers: {
|
|
195
|
+
"Content-Type": "application/json",
|
|
196
|
+
Authorization: `Web3Signed ${verify.payment.payload.signature}`,
|
|
197
|
+
},
|
|
198
|
+
body: JSON.stringify(body),
|
|
199
|
+
});
|
|
95
200
|
}
|
|
96
201
|
catch (err) {
|
|
97
|
-
deps.logger?.error?.({
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
202
|
+
deps.logger?.error?.({
|
|
203
|
+
scope,
|
|
204
|
+
grantId: opIdLower,
|
|
205
|
+
error: err.message,
|
|
206
|
+
}, "X402 gateway forward threw; reissuing challenge");
|
|
207
|
+
return { kind: "challenge", body: await buildFreshChallenge() };
|
|
208
|
+
}
|
|
209
|
+
if (gatewayRes.ok) {
|
|
210
|
+
// Preserve the gateway's structured success body so the request
|
|
211
|
+
// handler can echo it back via the X-PAYMENT-RESPONSE header (canonical
|
|
212
|
+
// X402 convention). Empty/malformed body → propagate as null; the
|
|
213
|
+
// header still gets set so builders can tell payment succeeded.
|
|
214
|
+
let payResponseBody = null;
|
|
215
|
+
try {
|
|
216
|
+
payResponseBody = await gatewayRes.json();
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
// Body unreadable but status was 2xx — treat as success with no payload.
|
|
220
|
+
}
|
|
221
|
+
return { kind: "ok", payResponse: payResponseBody };
|
|
222
|
+
}
|
|
223
|
+
// Read the gateway's structured error body if there is one.
|
|
224
|
+
let errorBody;
|
|
225
|
+
try {
|
|
226
|
+
errorBody = await gatewayRes.json();
|
|
105
227
|
}
|
|
228
|
+
catch {
|
|
229
|
+
errorBody = { error: gatewayRes.statusText };
|
|
230
|
+
}
|
|
231
|
+
// 409 (replay or race) and 400 (amount mismatch / shape error) are
|
|
232
|
+
// recoverable with a fresh challenge — the builder re-signs with the
|
|
233
|
+
// new state and retries. 402 (insufficient balance) is relayed verbatim
|
|
234
|
+
// because the builder needs to fix their escrow, not their signature.
|
|
235
|
+
// 5xx is treated like 402-relayed since the personal server can't
|
|
236
|
+
// distinguish "transient" from "permanent" without more info.
|
|
237
|
+
if (gatewayRes.status === 409 || gatewayRes.status === 400) {
|
|
238
|
+
deps.logger?.info?.({ scope, grantId: opIdLower, status: gatewayRes.status, errorBody }, "Gateway rejected X-PAYMENT; reissuing X402 challenge");
|
|
239
|
+
return { kind: "challenge", body: await buildFreshChallenge() };
|
|
240
|
+
}
|
|
241
|
+
return { kind: "gateway-error", status: gatewayRes.status, body: errorBody };
|
|
242
|
+
}
|
|
243
|
+
function collectedAt(now) {
|
|
244
|
+
return now()
|
|
245
|
+
.toISOString()
|
|
246
|
+
.replace(/\.\d{3}Z$/, "Z");
|
|
247
|
+
}
|
|
248
|
+
/** True when the request body should be treated as a JSON object (the legacy
|
|
249
|
+
* path). Missing/blank Content-Type is treated as JSON for backward compat. */
|
|
250
|
+
function isJsonContentType(request) {
|
|
251
|
+
const ct = request.headers.get("content-type");
|
|
252
|
+
if (!ct)
|
|
253
|
+
return true;
|
|
254
|
+
return ct.toLowerCase().includes("application/json");
|
|
255
|
+
}
|
|
256
|
+
function binaryMimeType(request) {
|
|
257
|
+
const ct = request.headers.get("content-type");
|
|
258
|
+
if (!ct)
|
|
259
|
+
return "application/octet-stream";
|
|
260
|
+
// Strip any "; charset=..." / boundary parameters.
|
|
261
|
+
return ct.split(";")[0].trim() || "application/octet-stream";
|
|
262
|
+
}
|
|
263
|
+
/** Extract a filename from X-Filename or a Content-Disposition header. */
|
|
264
|
+
function binaryFilename(request) {
|
|
265
|
+
const explicit = request.headers.get("x-filename");
|
|
266
|
+
if (explicit)
|
|
267
|
+
return explicit;
|
|
268
|
+
const disposition = request.headers.get("content-disposition");
|
|
269
|
+
const match = disposition?.match(/filename\*?=(?:UTF-8'')?"?([^";]+)"?/i);
|
|
270
|
+
return match ? decodeURIComponent(match[1]) : undefined;
|
|
106
271
|
}
|
|
107
272
|
function notifyNewData(syncManager) {
|
|
108
273
|
if (!syncManager)
|
|
@@ -121,7 +286,7 @@ export async function handlePersonalServerDataRequest(request, deps, options = {
|
|
|
121
286
|
if (request.method !== "GET")
|
|
122
287
|
return methodNotAllowed();
|
|
123
288
|
await deps.auth.authorizeBuilderList(request);
|
|
124
|
-
const result = listDataScopesContract({
|
|
289
|
+
const result = await listDataScopesContract({
|
|
125
290
|
storage: deps.storage,
|
|
126
291
|
scopePrefix: url.searchParams.get("scopePrefix") ?? undefined,
|
|
127
292
|
limit: normalizeLimit(url.searchParams.get("limit"), 20),
|
|
@@ -156,6 +321,9 @@ export async function handlePersonalServerDataRequest(request, deps, options = {
|
|
|
156
321
|
fileId: url.searchParams.get("fileId") ?? undefined,
|
|
157
322
|
at: url.searchParams.get("at") ?? undefined,
|
|
158
323
|
});
|
|
324
|
+
// Holds the base64-encoded gateway payForOperation response body when
|
|
325
|
+
// X402 succeeds — emitted on the final read response as X-PAYMENT-RESPONSE.
|
|
326
|
+
let paymentResponseHeader;
|
|
159
327
|
const grantId = selectedGrantId(request, url);
|
|
160
328
|
const authResult = await deps.auth.authorizeBuilderRead({
|
|
161
329
|
request,
|
|
@@ -163,6 +331,51 @@ export async function handlePersonalServerDataRequest(request, deps, options = {
|
|
|
163
331
|
grantId,
|
|
164
332
|
fileId: url.searchParams.get("fileId") ?? selectedEntry?.fileId ?? undefined,
|
|
165
333
|
});
|
|
334
|
+
// X402 payment dance for builder reads. Owner-exempt reads (the
|
|
335
|
+
// grantId sentinels "owner" / "policy-bypass") skip payment entirely
|
|
336
|
+
// since there's no payable op to attach the payment to.
|
|
337
|
+
//
|
|
338
|
+
// The grantId we pay against is the one verifyDataReadPolicy resolved
|
|
339
|
+
// from the Web3Signed payload (authoritative source) — not the URL /
|
|
340
|
+
// header hint, which may be absent when the payload carries it.
|
|
341
|
+
const isOwnerSignal = authResult?.grantId === "owner" ||
|
|
342
|
+
authResult?.grantId === "policy-bypass";
|
|
343
|
+
const builder = authResult?.builder;
|
|
344
|
+
const resolvedGrantId = !isOwnerSignal && authResult?.grantId ? authResult.grantId : undefined;
|
|
345
|
+
if (deps.paymentEnabled &&
|
|
346
|
+
!isOwnerSignal &&
|
|
347
|
+
typeof builder === "string" &&
|
|
348
|
+
builder.startsWith("0x") &&
|
|
349
|
+
resolvedGrantId &&
|
|
350
|
+
deps.gateway &&
|
|
351
|
+
deps.gatewayConfig &&
|
|
352
|
+
deps.gatewayUrl) {
|
|
353
|
+
const x402Result = await handleX402Cycle({
|
|
354
|
+
deps,
|
|
355
|
+
request,
|
|
356
|
+
scope: scopeResult.scope,
|
|
357
|
+
fileIdParam: url.searchParams.get("fileId") ?? undefined,
|
|
358
|
+
atParam: url.searchParams.get("at") ?? undefined,
|
|
359
|
+
grantId: resolvedGrantId,
|
|
360
|
+
builder: builder,
|
|
361
|
+
gateway: deps.gateway,
|
|
362
|
+
gatewayConfig: deps.gatewayConfig,
|
|
363
|
+
gatewayUrl: deps.gatewayUrl,
|
|
364
|
+
});
|
|
365
|
+
if (x402Result.kind === "challenge") {
|
|
366
|
+
return jsonResponse(x402Result.body, { status: 402 });
|
|
367
|
+
}
|
|
368
|
+
if (x402Result.kind === "gateway-error") {
|
|
369
|
+
// Relay the gateway's structured body verbatim so the builder can
|
|
370
|
+
// dispatch on it (insufficient balance vs. replay vs. race etc.).
|
|
371
|
+
return jsonResponse(x402Result.body, { status: x402Result.status });
|
|
372
|
+
}
|
|
373
|
+
// x402Result.kind === "ok" — payment accepted, proceed to read.
|
|
374
|
+
// The gateway's success body is forwarded back to the builder via
|
|
375
|
+
// X-PAYMENT-RESPONSE (canonical X402 convention) so callers can see
|
|
376
|
+
// breakdown / paidAt / paymentNonce without a second gateway round-trip.
|
|
377
|
+
paymentResponseHeader = encodePaymentResponseHeader(x402Result.payResponse);
|
|
378
|
+
}
|
|
166
379
|
const result = await readDataContract({
|
|
167
380
|
storage: deps.storage,
|
|
168
381
|
scopeParam: scopeResult.scope,
|
|
@@ -183,28 +396,78 @@ export async function handlePersonalServerDataRequest(request, deps, options = {
|
|
|
183
396
|
"unknown",
|
|
184
397
|
userAgent: request.headers.get("user-agent") ?? "unknown",
|
|
185
398
|
});
|
|
186
|
-
|
|
399
|
+
const headers = {};
|
|
400
|
+
if (paymentResponseHeader) {
|
|
401
|
+
headers["X-PAYMENT-RESPONSE"] = paymentResponseHeader;
|
|
402
|
+
}
|
|
403
|
+
// `?content=raw` streams the decoded bytes of a binary envelope with its
|
|
404
|
+
// original media type, so a builder can download the file directly. The
|
|
405
|
+
// X-PAYMENT-RESPONSE header (if any) rides along on the raw response too.
|
|
406
|
+
if (url.searchParams.get("content") === "raw" &&
|
|
407
|
+
isBinaryEnvelope(result.envelope)) {
|
|
408
|
+
const decoded = decodeBinaryEnvelope(result.envelope);
|
|
409
|
+
headers["Content-Type"] = decoded.mimeType;
|
|
410
|
+
headers["Content-Length"] = String(decoded.bytes.length);
|
|
411
|
+
if (decoded.filename) {
|
|
412
|
+
headers["Content-Disposition"] =
|
|
413
|
+
`attachment; filename="${decoded.filename}"`;
|
|
414
|
+
}
|
|
415
|
+
if (decoded.metadata !== undefined) {
|
|
416
|
+
headers["X-Vana-Metadata"] = stringifyMetadataHeader(decoded.metadata);
|
|
417
|
+
}
|
|
418
|
+
return new Response(decoded.bytes, {
|
|
419
|
+
status: 200,
|
|
420
|
+
headers,
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
return jsonResponse(result.envelope, { headers });
|
|
187
424
|
}
|
|
188
425
|
if (request.method === "POST") {
|
|
189
426
|
await deps.auth.authorizeOwner(request);
|
|
190
427
|
const scopeResult = parseDataScopeContract(scopeParam);
|
|
191
428
|
if (!scopeResult.ok)
|
|
192
429
|
return contractErrorResponse(scopeResult);
|
|
430
|
+
const collectedAtValue = collectedAt(deps.now ?? (() => new Date()));
|
|
431
|
+
const status = deps.syncManager ? "syncing" : "stored";
|
|
432
|
+
// Binary / unstructured data (e.g. a PDF): the body is raw bytes. DPv2
|
|
433
|
+
// data points are scope-addressed and carry no schemaId, so unstructured
|
|
434
|
+
// data needs no schema at all — we ingest it schemaless. (Structured JSON
|
|
435
|
+
// below still resolves a schema for validation/metadata.)
|
|
436
|
+
if (!isJsonContentType(request)) {
|
|
437
|
+
const bytes = new Uint8Array(await request.arrayBuffer());
|
|
438
|
+
const result = await ingestBinaryDataContract({
|
|
439
|
+
storage: deps.storage,
|
|
440
|
+
scopeParam: scopeResult.scope,
|
|
441
|
+
bytes,
|
|
442
|
+
mimeType: binaryMimeType(request),
|
|
443
|
+
filename: binaryFilename(request),
|
|
444
|
+
metadata: parseMetadataHeader(request.headers.get("x-vana-metadata")),
|
|
445
|
+
collectedAt: collectedAtValue,
|
|
446
|
+
status,
|
|
447
|
+
});
|
|
448
|
+
if (!result.ok)
|
|
449
|
+
return contractErrorResponse(result);
|
|
450
|
+
deps.logger?.info?.({
|
|
451
|
+
scope: scopeResult.scope,
|
|
452
|
+
collectedAt: collectedAtValue,
|
|
453
|
+
path: result.writeResult.relativePath,
|
|
454
|
+
mimeType: binaryMimeType(request),
|
|
455
|
+
sizeBytes: bytes.length,
|
|
456
|
+
}, "Binary data file ingested");
|
|
457
|
+
notifyNewData(deps.syncManager);
|
|
458
|
+
return jsonResponse(result.response, { status: 201 });
|
|
459
|
+
}
|
|
193
460
|
const parsed = await parseJsonObjectBody(request, "Request body must be valid JSON");
|
|
194
461
|
if (!parsed.ok)
|
|
195
462
|
return contractResponse(parsed.result);
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return schema.response;
|
|
199
|
-
const collectedAtValue = collectedAt(deps.now ?? (() => new Date()));
|
|
463
|
+
// DPv2 is scope-addressed and the gateway records no schemas, so JSON
|
|
464
|
+
// ingest is schemaless too — no lookup, no schemaId/$schema stamped.
|
|
200
465
|
const result = await ingestDataContract({
|
|
201
466
|
storage: deps.storage,
|
|
202
467
|
scopeParam: scopeResult.scope,
|
|
203
468
|
body: parsed.body,
|
|
204
469
|
collectedAt: collectedAtValue,
|
|
205
|
-
status
|
|
206
|
-
schemaUrl: schema.schemaUrl,
|
|
207
|
-
schemaId: schema.schemaId,
|
|
470
|
+
status,
|
|
208
471
|
});
|
|
209
472
|
if (!result.ok)
|
|
210
473
|
return contractErrorResponse(result);
|
|
@@ -218,6 +481,20 @@ export async function handlePersonalServerDataRequest(request, deps, options = {
|
|
|
218
481
|
}
|
|
219
482
|
if (request.method === "DELETE") {
|
|
220
483
|
await deps.auth.authorizeOwner(request);
|
|
484
|
+
// Propagate the deletion to the authoritative stores (R2 blobs + gateway records) BEFORE the
|
|
485
|
+
// local delete — it reads the local index to find what to remove. Best-effort: a remote
|
|
486
|
+
// failure must not block the owner's local delete (and sync would otherwise resurrect it; the
|
|
487
|
+
// download-worker reconciliation is the backstop). Parse the scope first so an invalid scope
|
|
488
|
+
// still 400s without a wasted remote call.
|
|
489
|
+
const parsed = parseDataScopeContract(scopeParam);
|
|
490
|
+
if (parsed.ok && deps.syncManager?.deleteScopeRemote) {
|
|
491
|
+
try {
|
|
492
|
+
await deps.syncManager.deleteScopeRemote(parsed.scope);
|
|
493
|
+
}
|
|
494
|
+
catch (err) {
|
|
495
|
+
deps.logger?.info?.({ scope: scopeParam, error: err.message }, "Remote scope deletion failed; proceeding with local delete");
|
|
496
|
+
}
|
|
497
|
+
}
|
|
221
498
|
const result = await deleteDataScopeContract({
|
|
222
499
|
storage: deps.storage,
|
|
223
500
|
scopeParam,
|
|
@@ -295,12 +572,13 @@ export async function handlePersonalServerGrantsRequest(request, deps, options =
|
|
|
295
572
|
const parsed = await parseJsonObjectBody(request);
|
|
296
573
|
if (!parsed.ok)
|
|
297
574
|
return contractResponse(parsed.result);
|
|
575
|
+
// Canary createGrant doesn't need a clock — grantVersion defaults
|
|
576
|
+
// to "1" and callers re-registering pass a strictly higher value.
|
|
298
577
|
return contractResponse(await createGrantContract({
|
|
299
578
|
gateway: deps.gateway,
|
|
300
579
|
serverOwner: deps.serverOwner,
|
|
301
580
|
serverSigner: deps.serverSigner,
|
|
302
581
|
body: parsed.body,
|
|
303
|
-
now: () => deps.now?.().getTime() ?? Date.now(),
|
|
304
582
|
}));
|
|
305
583
|
}
|
|
306
584
|
return methodNotAllowed();
|