@opendatalabs/personal-server-ts-core 0.0.1-canary.01ca694 → 0.0.1-canary.052d47e
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 +282 -9
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +867 -163
- package/dist/api/index.js.map +1 -1
- package/dist/auth/request.d.ts.map +1 -1
- package/dist/auth/request.js.map +1 -1
- package/dist/client.d.ts +8 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +21 -3
- package/dist/client.js.map +1 -1
- package/dist/contracts/binary.d.ts +12 -0
- package/dist/contracts/binary.d.ts.map +1 -1
- package/dist/contracts/binary.js +31 -0
- package/dist/contracts/binary.js.map +1 -1
- package/dist/contracts/data.d.ts +51 -5
- package/dist/contracts/data.d.ts.map +1 -1
- package/dist/contracts/data.js +166 -30
- 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 +1 -1
- package/dist/contracts/index.d.ts.map +1 -1
- package/dist/contracts/index.js +1 -1
- package/dist/contracts/index.js.map +1 -1
- package/dist/derivatives/api.d.ts +48 -0
- package/dist/derivatives/api.d.ts.map +1 -0
- package/dist/derivatives/api.js +366 -0
- package/dist/derivatives/api.js.map +1 -0
- package/dist/derivatives/compute.d.ts +106 -0
- package/dist/derivatives/compute.d.ts.map +1 -0
- package/dist/derivatives/compute.js +414 -0
- package/dist/derivatives/compute.js.map +1 -0
- package/dist/derivatives/e2ee/aad.d.ts +22 -0
- package/dist/derivatives/e2ee/aad.d.ts.map +1 -0
- package/dist/derivatives/e2ee/aad.js +32 -0
- package/dist/derivatives/e2ee/aad.js.map +1 -0
- package/dist/derivatives/e2ee/attestation.d.ts +134 -0
- package/dist/derivatives/e2ee/attestation.d.ts.map +1 -0
- package/dist/derivatives/e2ee/attestation.js +248 -0
- package/dist/derivatives/e2ee/attestation.js.map +1 -0
- package/dist/derivatives/e2ee/index.d.ts +6 -0
- package/dist/derivatives/e2ee/index.d.ts.map +1 -0
- package/dist/derivatives/e2ee/index.js +6 -0
- package/dist/derivatives/e2ee/index.js.map +1 -0
- package/dist/derivatives/e2ee/jcs.d.ts +17 -0
- package/dist/derivatives/e2ee/jcs.d.ts.map +1 -0
- package/dist/derivatives/e2ee/jcs.js +55 -0
- package/dist/derivatives/e2ee/jcs.js.map +1 -0
- package/dist/derivatives/e2ee/phala.d.ts +62 -0
- package/dist/derivatives/e2ee/phala.d.ts.map +1 -0
- package/dist/derivatives/e2ee/phala.js +168 -0
- package/dist/derivatives/e2ee/phala.js.map +1 -0
- package/dist/derivatives/e2ee/suite.d.ts +56 -0
- package/dist/derivatives/e2ee/suite.d.ts.map +1 -0
- package/dist/derivatives/e2ee/suite.js +164 -0
- package/dist/derivatives/e2ee/suite.js.map +1 -0
- package/dist/derivatives/index.d.ts +10 -0
- package/dist/derivatives/index.d.ts.map +1 -0
- package/dist/derivatives/index.js +10 -0
- package/dist/derivatives/index.js.map +1 -0
- package/dist/derivatives/inference.d.ts +151 -0
- package/dist/derivatives/inference.d.ts.map +1 -0
- package/dist/derivatives/inference.js +262 -0
- package/dist/derivatives/inference.js.map +1 -0
- package/dist/derivatives/prompt.d.ts +64 -0
- package/dist/derivatives/prompt.d.ts.map +1 -0
- package/dist/derivatives/prompt.js +193 -0
- package/dist/derivatives/prompt.js.map +1 -0
- package/dist/derivatives/registration.d.ts +54 -0
- package/dist/derivatives/registration.d.ts.map +1 -0
- package/dist/derivatives/registration.js +180 -0
- package/dist/derivatives/registration.js.map +1 -0
- package/dist/derivatives/scheduler.d.ts +84 -0
- package/dist/derivatives/scheduler.d.ts.map +1 -0
- package/dist/derivatives/scheduler.js +257 -0
- package/dist/derivatives/scheduler.js.map +1 -0
- package/dist/derivatives/store.d.ts +14 -0
- package/dist/derivatives/store.d.ts.map +1 -0
- package/dist/derivatives/store.js +77 -0
- package/dist/derivatives/store.js.map +1 -0
- package/dist/derivatives/types.d.ts +92 -0
- package/dist/derivatives/types.d.ts.map +1 -0
- package/dist/derivatives/types.js +27 -0
- package/dist/derivatives/types.js.map +1 -0
- package/dist/errors/catalog.d.ts +75 -0
- package/dist/errors/catalog.d.ts.map +1 -1
- package/dist/errors/catalog.js +104 -0
- package/dist/errors/catalog.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/keys/index.d.ts +1 -0
- package/dist/keys/index.d.ts.map +1 -1
- package/dist/keys/index.js +1 -1
- package/dist/keys/index.js.map +1 -1
- package/dist/keys/public-only-account.d.ts +8 -0
- package/dist/keys/public-only-account.d.ts.map +1 -0
- package/dist/keys/public-only-account.js +21 -0
- package/dist/keys/public-only-account.js.map +1 -0
- package/dist/keys/server-account.d.ts +2 -2
- package/dist/keys/server-account.d.ts.map +1 -1
- package/dist/lineage/attestation.d.ts +42 -0
- package/dist/lineage/attestation.d.ts.map +1 -0
- package/dist/lineage/attestation.js +25 -0
- package/dist/lineage/attestation.js.map +1 -0
- package/dist/lineage/gateway.d.ts +105 -0
- package/dist/lineage/gateway.d.ts.map +1 -0
- package/dist/lineage/gateway.js +242 -0
- package/dist/lineage/gateway.js.map +1 -0
- package/dist/lineage/index.d.ts +5 -0
- package/dist/lineage/index.d.ts.map +1 -0
- package/dist/lineage/index.js +5 -0
- package/dist/lineage/index.js.map +1 -0
- package/dist/lineage/lineage.d.ts +141 -0
- package/dist/lineage/lineage.d.ts.map +1 -0
- package/dist/lineage/lineage.js +255 -0
- package/dist/lineage/lineage.js.map +1 -0
- package/dist/logging/access-log.d.ts +10 -1
- package/dist/logging/access-log.d.ts.map +1 -1
- package/dist/mcp/activity.d.ts.map +1 -1
- package/dist/mcp/activity.js.map +1 -1
- package/dist/mcp/connection-api.d.ts.map +1 -1
- package/dist/mcp/connection-api.js +0 -1
- package/dist/mcp/connection-api.js.map +1 -1
- package/dist/mcp/index.d.ts +1 -0
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +1 -0
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/read-client.d.ts +61 -1
- package/dist/mcp/read-client.d.ts.map +1 -1
- package/dist/mcp/read-client.js +397 -42
- package/dist/mcp/read-client.js.map +1 -1
- 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.map +1 -1
- package/dist/mcp/search/index.js +108 -21
- package/dist/mcp/search/index.js.map +1 -1
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +27 -9
- package/dist/mcp/server.js.map +1 -1
- package/dist/mcp/session.d.ts +160 -0
- package/dist/mcp/session.d.ts.map +1 -0
- package/dist/mcp/session.js +243 -0
- package/dist/mcp/session.js.map +1 -0
- package/dist/mcp/tools.d.ts +29 -3
- package/dist/mcp/tools.d.ts.map +1 -1
- package/dist/mcp/tools.js +515 -48
- package/dist/mcp/tools.js.map +1 -1
- package/dist/mcp/types.d.ts.map +1 -1
- 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 +18 -2
- package/dist/policy/data-read.d.ts.map +1 -1
- package/dist/policy/data-read.js +80 -49
- package/dist/policy/data-read.js.map +1 -1
- package/dist/policy/data-write.d.ts +69 -0
- package/dist/policy/data-write.d.ts.map +1 -0
- package/dist/policy/data-write.js +131 -0
- package/dist/policy/data-write.js.map +1 -0
- package/dist/policy/index.d.ts +3 -1
- package/dist/policy/index.d.ts.map +1 -1
- package/dist/policy/index.js +3 -1
- package/dist/policy/index.js.map +1 -1
- package/dist/policy/signed-artifacts.d.ts +17 -0
- package/dist/policy/signed-artifacts.d.ts.map +1 -0
- package/dist/policy/signed-artifacts.js +110 -0
- package/dist/policy/signed-artifacts.js.map +1 -0
- package/dist/ports/index.d.ts +159 -32
- 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 +49 -4
- package/dist/schemas/server-config.d.ts.map +1 -1
- package/dist/schemas/server-config.js +95 -13
- package/dist/schemas/server-config.js.map +1 -1
- package/dist/signing/index.d.ts +2 -2
- package/dist/signing/index.d.ts.map +1 -1
- package/dist/signing/index.js +1 -1
- package/dist/signing/index.js.map +1 -1
- package/dist/signing/request-signer.d.ts +2 -0
- package/dist/signing/request-signer.d.ts.map +1 -1
- package/dist/signing/request-signer.js +1 -0
- package/dist/signing/request-signer.js.map +1 -1
- package/dist/signing/signer.d.ts +27 -25
- package/dist/signing/signer.d.ts.map +1 -1
- package/dist/signing/signer.js +27 -51
- package/dist/signing/signer.js.map +1 -1
- package/dist/storage/adapters/index.d.ts +1 -1
- package/dist/storage/adapters/index.d.ts.map +1 -1
- package/dist/storage/adapters/index.js +1 -1
- package/dist/storage/adapters/index.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 +38 -3
- package/dist/storage/adapters/sdk.js.map +1 -1
- package/dist/storage/adapters/vana.d.ts +3 -0
- package/dist/storage/adapters/vana.d.ts.map +1 -1
- package/dist/storage/adapters/vana.js +95 -2
- package/dist/storage/adapters/vana.js.map +1 -1
- package/dist/storage/blocks/errors.d.ts.map +1 -1
- package/dist/storage/blocks/errors.js.map +1 -1
- package/dist/storage/blocks/index.d.ts +2 -0
- package/dist/storage/blocks/index.d.ts.map +1 -1
- package/dist/storage/blocks/index.js +2 -0
- package/dist/storage/blocks/index.js.map +1 -1
- package/dist/storage/blocks/select.d.ts +29 -0
- package/dist/storage/blocks/select.d.ts.map +1 -0
- package/dist/storage/blocks/select.js +120 -0
- package/dist/storage/blocks/select.js.map +1 -0
- package/dist/storage/blocks/types.d.ts +7 -0
- package/dist/storage/blocks/types.d.ts.map +1 -1
- package/dist/storage/index/manager.d.ts +30 -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 +6 -1
- package/dist/storage/index/types.d.ts.map +1 -1
- package/dist/sync/data-point-feed.d.ts +35 -0
- package/dist/sync/data-point-feed.d.ts.map +1 -0
- package/dist/sync/data-point-feed.js +122 -0
- package/dist/sync/data-point-feed.js.map +1 -0
- package/dist/sync/data-point-id.d.ts +7 -0
- package/dist/sync/data-point-id.d.ts.map +1 -0
- package/dist/sync/data-point-id.js +16 -0
- package/dist/sync/data-point-id.js.map +1 -0
- package/dist/sync/delete-data-port.d.ts +50 -0
- package/dist/sync/delete-data-port.d.ts.map +1 -0
- package/dist/sync/delete-data-port.js +244 -0
- package/dist/sync/delete-data-port.js.map +1 -0
- package/dist/sync/engine/sync-manager.d.ts +16 -4
- package/dist/sync/engine/sync-manager.d.ts.map +1 -1
- package/dist/sync/engine/sync-manager.js +93 -32
- package/dist/sync/engine/sync-manager.js.map +1 -1
- package/dist/sync/index.d.ts +7 -0
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +7 -0
- package/dist/sync/index.js.map +1 -1
- package/dist/sync/issues.d.ts.map +1 -1
- package/dist/sync/issues.js +21 -3
- package/dist/sync/issues.js.map +1 -1
- package/dist/sync/pending-blob-deletions.d.ts +29 -0
- package/dist/sync/pending-blob-deletions.d.ts.map +1 -0
- package/dist/sync/pending-blob-deletions.js +116 -0
- package/dist/sync/pending-blob-deletions.js.map +1 -0
- package/dist/sync/retry-memory.d.ts +33 -0
- package/dist/sync/retry-memory.d.ts.map +1 -0
- package/dist/sync/retry-memory.js +68 -0
- package/dist/sync/retry-memory.js.map +1 -0
- package/dist/sync/scope-deletions.d.ts +155 -0
- package/dist/sync/scope-deletions.d.ts.map +1 -0
- package/dist/sync/scope-deletions.js +225 -0
- package/dist/sync/scope-deletions.js.map +1 -0
- package/dist/sync/tombstone.d.ts +27 -0
- package/dist/sync/tombstone.d.ts.map +1 -0
- package/dist/sync/tombstone.js +28 -0
- package/dist/sync/tombstone.js.map +1 -0
- package/dist/sync/workers/delete.d.ts +152 -31
- package/dist/sync/workers/delete.d.ts.map +1 -1
- package/dist/sync/workers/delete.js +457 -55
- package/dist/sync/workers/delete.js.map +1 -1
- package/dist/sync/workers/download.d.ts +108 -27
- package/dist/sync/workers/download.d.ts.map +1 -1
- package/dist/sync/workers/download.js +321 -126
- package/dist/sync/workers/download.js.map +1 -1
- package/dist/sync/workers/upload.d.ts +65 -10
- package/dist/sync/workers/upload.d.ts.map +1 -1
- package/dist/sync/workers/upload.js +371 -42
- package/dist/sync/workers/upload.js.map +1 -1
- package/dist/test-utils/e2ee-fixtures.d.ts +94 -0
- package/dist/test-utils/e2ee-fixtures.d.ts.map +1 -0
- package/dist/test-utils/e2ee-fixtures.js +116 -0
- package/dist/test-utils/e2ee-fixtures.js.map +1 -0
- package/dist/test-utils/e2ee-gateway.d.ts +61 -0
- package/dist/test-utils/e2ee-gateway.d.ts.map +1 -0
- package/dist/test-utils/e2ee-gateway.js +248 -0
- package/dist/test-utils/e2ee-gateway.js.map +1 -0
- package/dist/test-utils/index.d.ts +3 -0
- package/dist/test-utils/index.d.ts.map +1 -1
- package/dist/test-utils/index.js +3 -0
- package/dist/test-utils/index.js.map +1 -1
- package/dist/test-utils/memory-storage.d.ts +12 -0
- package/dist/test-utils/memory-storage.d.ts.map +1 -0
- package/dist/test-utils/memory-storage.js +146 -0
- package/dist/test-utils/memory-storage.js.map +1 -0
- package/dist/test-utils/wallet.d.ts +6 -0
- package/dist/test-utils/wallet.d.ts.map +1 -1
- package/dist/test-utils/wallet.js +46 -0
- package/dist/test-utils/wallet.js.map +1 -1
- package/dist/write/attribution.d.ts +185 -0
- package/dist/write/attribution.d.ts.map +1 -0
- package/dist/write/attribution.js +450 -0
- package/dist/write/attribution.js.map +1 -0
- package/dist/write/authorize.d.ts +77 -0
- package/dist/write/authorize.d.ts.map +1 -0
- package/dist/write/authorize.js +109 -0
- package/dist/write/authorize.js.map +1 -0
- package/dist/write/index.d.ts +5 -0
- package/dist/write/index.d.ts.map +1 -0
- package/dist/write/index.js +5 -0
- package/dist/write/index.js.map +1 -0
- package/dist/write/route.d.ts +61 -0
- package/dist/write/route.d.ts.map +1 -0
- package/dist/write/route.js +132 -0
- package/dist/write/route.js.map +1 -0
- package/dist/write/session.d.ts +96 -0
- package/dist/write/session.d.ts.map +1 -0
- package/dist/write/session.js +136 -0
- package/dist/write/session.js.map +1 -0
- package/package.json +19 -3
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builder attribution for delegated writes.
|
|
3
|
+
*
|
|
4
|
+
* The write-session token authorizes a write; it does not PROVE who authored
|
|
5
|
+
* the payload (a bearer token is not a signature). For cryptographic
|
|
6
|
+
* attribution the builder also signs the payload: a Web3Signed proof over the
|
|
7
|
+
* exact write request (aud / method / uri / bodyHash / iat / exp), carried in
|
|
8
|
+
* the `X-Vana-Write-Signature` header and signed by the builder key proven at
|
|
9
|
+
* handshake. The PS verifies the proof recovers to the session's builder and
|
|
10
|
+
* that its bodyHash commits to the received bytes, then stores the proof and
|
|
11
|
+
* the builder identity WITH the record — inside the envelope's `data` under
|
|
12
|
+
* the reserved `$writtenBy` key (same in-`data` marker idiom as `$binary`),
|
|
13
|
+
* so attribution travels through the unchanged encrypt / upload / register
|
|
14
|
+
* path and back out on read. The on-chain shape is untouched.
|
|
15
|
+
*
|
|
16
|
+
* The proof binds the write to its context, not just its bytes: the signed
|
|
17
|
+
* claims carry the request uri (path AND query, see canonicalSignedUri; the
|
|
18
|
+
* path's last segment is the scope on the ingest route), the method, the
|
|
19
|
+
* audience (this server's origin) and the session's grantId, which the PS
|
|
20
|
+
* requires to match the session at ingest. A stored `$writtenBy` therefore
|
|
21
|
+
* cannot be lifted onto another scope's record or relabelled with a
|
|
22
|
+
* different grant and still verify. A proof may also carry an optional
|
|
23
|
+
* `nonce` claim, which is what makes it unique: without one, two identical
|
|
24
|
+
* requests signed in the same second are byte-identical and the replay guard
|
|
25
|
+
* refuses the second.
|
|
26
|
+
*
|
|
27
|
+
* What the builder signs is the STORED representation, not the wire body:
|
|
28
|
+
* the compact JSON of the envelope `data` record the PS will write (minus
|
|
29
|
+
* `$writtenBy`). A third party holding the envelope can therefore verify
|
|
30
|
+
* authorship from the envelope ALONE (verifyStoredWriterAttribution): recover
|
|
31
|
+
* the signer over the stored proof's base64url payload (EIP-191), check the
|
|
32
|
+
* signed claims against the envelope's scope and stored grantId, and hash
|
|
33
|
+
* JSON.stringify(data minus the server-stamped keys `$writtenBy` and
|
|
34
|
+
* `$lineage`) against the signed bodyHash.
|
|
35
|
+
* - JSON writes: the body IS the stored record, so the builder signs the
|
|
36
|
+
* body bytes, which must be compact JSON (what JSON.stringify emits: no
|
|
37
|
+
* insignificant whitespace, keys in the order given). JSON.parse /
|
|
38
|
+
* JSON.stringify round-trips that byte-for-byte; a body that does not
|
|
39
|
+
* round-trip is rejected at write time (WRITE_BODY_NOT_CANONICAL) instead
|
|
40
|
+
* of being stored with an attribution nobody can check.
|
|
41
|
+
* - Binary writes: the stored record is the `$binary` envelope data (media
|
|
42
|
+
* type, filename, caller metadata, size, content hash, base64 content),
|
|
43
|
+
* so the builder signs its compact JSON (binaryWriteSignedBytes). That
|
|
44
|
+
* binds the caller-controlled representation headers (Content-Type,
|
|
45
|
+
* X-Filename / Content-Disposition, X-Vana-Metadata) to the signature and
|
|
46
|
+
* makes a JSON-vs-binary switch of the same bytes fail verification.
|
|
47
|
+
*/
|
|
48
|
+
import { computeBodyHash, parseWeb3SignedHeader, verifyWeb3Signed, } from "@opendatalabs/vana-sdk/browser";
|
|
49
|
+
import { recoverMessageAddress } from "viem";
|
|
50
|
+
import { ProtocolError } from "../errors/catalog.js";
|
|
51
|
+
import { hashConnectionToken as sha256HexOf } from "../mcp/connection-api.js";
|
|
52
|
+
import { binaryFilename, binaryMimeType, buildBinaryEnvelopeData, isJsonContentType, normalizeBinaryMimeType, parseMetadataHeader, sha256Hex, } from "../contracts/binary.js";
|
|
53
|
+
import { LINEAGE_KEY, StoredLineageMalformedError, extractLineageField, readStoredLineage, } from "../lineage/lineage.js";
|
|
54
|
+
import { isBinaryEnvelope } from "../contracts/binary.js";
|
|
55
|
+
/** Header carrying the builder's signed-payload proof on a session write. */
|
|
56
|
+
export const WRITE_SIGNATURE_HEADER = "x-vana-write-signature";
|
|
57
|
+
/**
|
|
58
|
+
* Upper bound on the optional `nonce` claim (a uuid or a short random
|
|
59
|
+
* string is the intended shape). Bounded so a proof cannot smuggle bulk
|
|
60
|
+
* data into the replay store's keys.
|
|
61
|
+
*/
|
|
62
|
+
export const MAX_PROOF_NONCE_LENGTH = 128;
|
|
63
|
+
/** HTTP methods that never carry a body on this API. */
|
|
64
|
+
function isBodylessMethod(method) {
|
|
65
|
+
const upper = method.toUpperCase();
|
|
66
|
+
return upper === "GET" || upper === "HEAD" || upper === "DELETE";
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Canonical query string: parameters sorted by name and then value. The
|
|
70
|
+
* signed uri must commit to the query because a query parameter can decide
|
|
71
|
+
* authorization (`GET /v1/derivatives/questions?derivedScope=X` authorizes
|
|
72
|
+
* the caller against that scope), and a proof that does not cover it can be
|
|
73
|
+
* replayed against a different scope. Sorting makes the rule insensitive to
|
|
74
|
+
* the order a client happens to serialize its parameters in.
|
|
75
|
+
*/
|
|
76
|
+
function canonicalQuery(search) {
|
|
77
|
+
const entries = [...new URLSearchParams(search).entries()].sort((a, b) => a[0] === b[0]
|
|
78
|
+
? a[1] < b[1]
|
|
79
|
+
? -1
|
|
80
|
+
: a[1] > b[1]
|
|
81
|
+
? 1
|
|
82
|
+
: 0
|
|
83
|
+
: a[0] < b[0]
|
|
84
|
+
? -1
|
|
85
|
+
: 1);
|
|
86
|
+
const canonical = new URLSearchParams();
|
|
87
|
+
for (const [name, value] of entries)
|
|
88
|
+
canonical.append(name, value);
|
|
89
|
+
return canonical.toString();
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The uri a proof must commit to for a request: the path, plus the canonical
|
|
93
|
+
* query when the request carries one. A request with no query string signs
|
|
94
|
+
* the bare path exactly as before.
|
|
95
|
+
*/
|
|
96
|
+
export function canonicalSignedUri(pathname, search) {
|
|
97
|
+
const query = canonicalQuery(search);
|
|
98
|
+
return query ? `${pathname}?${query}` : pathname;
|
|
99
|
+
}
|
|
100
|
+
/** The same canonical form applied to a signed `uri` claim. */
|
|
101
|
+
export function canonicalizeSignedUri(uri) {
|
|
102
|
+
const mark = uri.indexOf("?");
|
|
103
|
+
if (mark === -1)
|
|
104
|
+
return uri;
|
|
105
|
+
return canonicalSignedUri(uri.slice(0, mark), uri.slice(mark + 1));
|
|
106
|
+
}
|
|
107
|
+
function base64UrlDecode(value) {
|
|
108
|
+
const base64 = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
109
|
+
const padded = base64.padEnd(base64.length + ((4 - (base64.length % 4)) % 4), "=");
|
|
110
|
+
const binary = atob(padded);
|
|
111
|
+
return new TextDecoder().decode(Uint8Array.from(binary, (char) => char.charCodeAt(0)));
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The proof's optional `nonce` claim. Read from the raw signed payload, not
|
|
115
|
+
* from the verified claims: the SDK's parser keeps only the claims it knows,
|
|
116
|
+
* and the nonce is covered by the signature all the same (the signature is
|
|
117
|
+
* over the base64url payload string).
|
|
118
|
+
*/
|
|
119
|
+
function signedNonce(payloadBase64) {
|
|
120
|
+
let raw;
|
|
121
|
+
try {
|
|
122
|
+
raw = JSON.parse(base64UrlDecode(payloadBase64));
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// A payload that does not decode is rejected by verifyWeb3Signed.
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
if (typeof raw !== "object" || raw === null)
|
|
129
|
+
return undefined;
|
|
130
|
+
const nonce = raw.nonce;
|
|
131
|
+
if (nonce === undefined || nonce === null)
|
|
132
|
+
return undefined;
|
|
133
|
+
if (typeof nonce !== "string" ||
|
|
134
|
+
nonce.length === 0 ||
|
|
135
|
+
nonce.length > MAX_PROOF_NONCE_LENGTH) {
|
|
136
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_INVALID", `Payload proof nonce must be a string of 1 to ${MAX_PROOF_NONCE_LENGTH} characters`);
|
|
137
|
+
}
|
|
138
|
+
return nonce;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Reserved key inside the envelope's `data` record for builder attribution.
|
|
142
|
+
* Mirrors the `$binary` marker convention (contracts/binary.ts).
|
|
143
|
+
*/
|
|
144
|
+
export const WRITER_ATTRIBUTION_KEY = "$writtenBy";
|
|
145
|
+
function resolveOrigin(origin) {
|
|
146
|
+
return typeof origin === "function" ? origin() : origin;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The bytes a builder signs (as the Web3Signed body) for a BINARY session
|
|
150
|
+
* write: the compact JSON of the `$binary` record the PS stores for these
|
|
151
|
+
* headers and bytes. Mirrors ingest exactly (same media-type normalization,
|
|
152
|
+
* metadata parsing and envelope builder), so the signature covers the stored
|
|
153
|
+
* representation and verifyStoredWriterAttribution can re-derive it.
|
|
154
|
+
*/
|
|
155
|
+
export async function binaryWriteSignedBytes(input) {
|
|
156
|
+
const data = buildBinaryEnvelopeData({
|
|
157
|
+
bytes: input.bytes,
|
|
158
|
+
mimeType: normalizeBinaryMimeType(input.contentType),
|
|
159
|
+
filename: input.filename,
|
|
160
|
+
contentHash: await sha256Hex(input.bytes),
|
|
161
|
+
metadata: parseMetadataHeader(input.metadataHeader ?? null),
|
|
162
|
+
});
|
|
163
|
+
return new TextEncoder().encode(JSON.stringify(data));
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The bytes the proof must commit to for this request: the body itself for
|
|
167
|
+
* a JSON write, the stored `$binary` representation for anything else.
|
|
168
|
+
*/
|
|
169
|
+
async function signedBytesFor(request, bodyBytes) {
|
|
170
|
+
// A bodyless method has no stored representation to sign: the proof
|
|
171
|
+
// commits to the empty body, whatever Content-Type the caller sent.
|
|
172
|
+
if (isBodylessMethod(request.method))
|
|
173
|
+
return bodyBytes;
|
|
174
|
+
if (isJsonContentType(request))
|
|
175
|
+
return bodyBytes;
|
|
176
|
+
return binaryWriteSignedBytes({
|
|
177
|
+
bytes: bodyBytes,
|
|
178
|
+
contentType: binaryMimeType(request),
|
|
179
|
+
filename: binaryFilename(request),
|
|
180
|
+
metadataHeader: request.headers.get("x-vana-metadata") ?? undefined,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Verify the `X-Vana-Write-Signature` proof on a session write and produce
|
|
185
|
+
* the attribution record to store with the data. Throws ProtocolError(401)
|
|
186
|
+
* when the proof is missing, malformed, expired, fails EIP-191 recovery /
|
|
187
|
+
* bodyHash binding (the hash covers the stored representation, see
|
|
188
|
+
* signedBytesFor), recovers to a different key than the session builder, or
|
|
189
|
+
* does not carry the session's grantId as a signed claim.
|
|
190
|
+
*/
|
|
191
|
+
export async function verifyWriterAttribution(input) {
|
|
192
|
+
const headerValue = input.request.headers.get(WRITE_SIGNATURE_HEADER) ?? undefined;
|
|
193
|
+
if (!headerValue) {
|
|
194
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_REQUIRED", `Session writes must carry a builder-signed payload proof in ${WRITE_SIGNATURE_HEADER}`);
|
|
195
|
+
}
|
|
196
|
+
const url = new URL(input.request.url);
|
|
197
|
+
const bodyBytes = new Uint8Array(await input.request.clone().arrayBuffer());
|
|
198
|
+
// The proof must commit to the whole request uri, query included. Any
|
|
199
|
+
// signed uri whose canonical form equals the request's canonical form is
|
|
200
|
+
// accepted, so parameter order is the client's business; anything else is
|
|
201
|
+
// handed to verifyWeb3Signed as a URI mismatch against the canonical form.
|
|
202
|
+
const expectedUri = canonicalSignedUri(url.pathname, url.search);
|
|
203
|
+
let expectedPath = expectedUri;
|
|
204
|
+
try {
|
|
205
|
+
const claimedUri = parseWeb3SignedHeader(headerValue).payload.uri;
|
|
206
|
+
if (canonicalizeSignedUri(claimedUri) === expectedUri) {
|
|
207
|
+
expectedPath = claimedUri;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
// Unparseable header: verifyWeb3Signed reports it with its own error.
|
|
212
|
+
}
|
|
213
|
+
let verified;
|
|
214
|
+
try {
|
|
215
|
+
verified = await verifyWeb3Signed({
|
|
216
|
+
headerValue,
|
|
217
|
+
expectedOrigin: resolveOrigin(input.serverOrigin),
|
|
218
|
+
expectedMethod: input.request.method,
|
|
219
|
+
expectedPath,
|
|
220
|
+
bodyBytes: await signedBytesFor(input.request, bodyBytes),
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
catch (err) {
|
|
224
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_INVALID", err instanceof Error ? err.message : String(err));
|
|
225
|
+
}
|
|
226
|
+
if (verified.signer.toLowerCase() !== input.builderAddress.toLowerCase()) {
|
|
227
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_SIGNER_MISMATCH", "Payload proof is not signed by the session builder", { expected: input.builderAddress, actual: verified.signer });
|
|
228
|
+
}
|
|
229
|
+
// The grant is a signed claim, so the stored attribution's grantId is
|
|
230
|
+
// covered by the builder's signature rather than being a bare label.
|
|
231
|
+
if (verified.payload.grantId !== input.grantId) {
|
|
232
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_GRANT_MISMATCH", "Payload proof must carry the write session's grantId as a signed claim", { expected: input.grantId, actual: verified.payload.grantId ?? null });
|
|
233
|
+
}
|
|
234
|
+
// The compact `{payload}.{signature}` form (scheme prefix stripped) is what
|
|
235
|
+
// gets stored, so verifiers don't need to know the header framing; the raw
|
|
236
|
+
// payload is also where the optional `nonce` claim is read from.
|
|
237
|
+
const { payloadBase64, signature } = parseWeb3SignedHeader(headerValue);
|
|
238
|
+
const nonce = signedNonce(payloadBase64);
|
|
239
|
+
// Bodyless methods (the derivative read / delete routes) never carry a
|
|
240
|
+
// body: skip the canonical-JSON rule explicitly instead of running it over
|
|
241
|
+
// empty bytes, where isJsonContentType's missing-header default would send
|
|
242
|
+
// it into a swallowed JSON.parse throw.
|
|
243
|
+
if (!isBodylessMethod(input.request.method) &&
|
|
244
|
+
bodyBytes.byteLength > 0 &&
|
|
245
|
+
isJsonContentType(input.request)) {
|
|
246
|
+
assertCanonicalJsonBody(bodyBytes);
|
|
247
|
+
}
|
|
248
|
+
// Replay guard, last: only a proof that passed every check is consumed,
|
|
249
|
+
// so a rejected request never burns a proof.
|
|
250
|
+
//
|
|
251
|
+
// Without a `nonce` claim the key is the proof itself, so two identical
|
|
252
|
+
// requests signed in the same second (a naive poll of GET /questions/:id)
|
|
253
|
+
// produce byte-identical proofs and the second is refused. A proof that
|
|
254
|
+
// carries a nonce is keyed by (builder, nonce) instead: the builder makes
|
|
255
|
+
// each call distinct, and re-using a nonce is a replay even if the rest of
|
|
256
|
+
// the payload changed.
|
|
257
|
+
let releaseProof;
|
|
258
|
+
if (input.replayStore) {
|
|
259
|
+
const store = input.replayStore;
|
|
260
|
+
const proofId = await sha256HexOf(nonce === undefined
|
|
261
|
+
? headerValue
|
|
262
|
+
: `nonce:${input.builderAddress.toLowerCase()}:${nonce}`);
|
|
263
|
+
const replayed = await store.consume(proofId, verified.payload.exp * 1000);
|
|
264
|
+
if (replayed) {
|
|
265
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_REPLAY", nonce === undefined
|
|
266
|
+
? "Payload proof already used; sign a fresh proof for each call (or add a unique nonce claim so repeated reads stay distinct)"
|
|
267
|
+
: "Payload proof nonce already used; each nonce is single use");
|
|
268
|
+
}
|
|
269
|
+
releaseProof = async () => {
|
|
270
|
+
await store.release?.(proofId);
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
builder: input.builderAddress,
|
|
275
|
+
grantId: input.grantId,
|
|
276
|
+
signature: `${payloadBase64}.${signature}`,
|
|
277
|
+
bodyHash: verified.payload.bodyHash,
|
|
278
|
+
writtenAt: (input.now?.() ?? new Date()).toISOString(),
|
|
279
|
+
...(releaseProof ? { releaseProof } : {}),
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Stamp attribution into an envelope `data` record. The reserved key must not
|
|
284
|
+
* appear in the caller's payload — a builder must not be able to forge (or
|
|
285
|
+
* shadow) its own attribution.
|
|
286
|
+
*/
|
|
287
|
+
export function stampWriterAttribution(data, attribution) {
|
|
288
|
+
return { ...data, [WRITER_ATTRIBUTION_KEY]: attribution };
|
|
289
|
+
}
|
|
290
|
+
export function hasReservedWriterKey(data) {
|
|
291
|
+
return Object.prototype.hasOwnProperty.call(data, WRITER_ATTRIBUTION_KEY);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* A JSON session write must be the compact serialization of its own parsed
|
|
295
|
+
* value, so the record stored after JSON.parse re-serializes to the signed
|
|
296
|
+
* bytes. Bodies that do not parse are left to the ingest path, which reports
|
|
297
|
+
* the parse error itself.
|
|
298
|
+
*/
|
|
299
|
+
function assertCanonicalJsonBody(bodyBytes) {
|
|
300
|
+
const text = new TextDecoder().decode(bodyBytes);
|
|
301
|
+
let parsed;
|
|
302
|
+
try {
|
|
303
|
+
parsed = JSON.parse(text);
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (JSON.stringify(parsed) !== text) {
|
|
309
|
+
throw new ProtocolError(400, "WRITE_BODY_NOT_CANONICAL", "Session writes must send compact JSON (the JSON.stringify form) so the stored record re-serializes to the signed bytes and the attribution bodyHash stays verifiable");
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/** Proof scheme prefix the compact stored signature was stripped of. */
|
|
313
|
+
const WEB3_SIGNED_SCHEME = "Web3Signed";
|
|
314
|
+
export class WriterAttributionVerificationError extends Error {
|
|
315
|
+
reason;
|
|
316
|
+
constructor(reason, message) {
|
|
317
|
+
super(message);
|
|
318
|
+
this.reason = reason;
|
|
319
|
+
this.name = "WriterAttributionVerificationError";
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function isWriterAttribution(value) {
|
|
323
|
+
if (typeof value !== "object" || value === null)
|
|
324
|
+
return false;
|
|
325
|
+
const v = value;
|
|
326
|
+
return (typeof v.builder === "string" &&
|
|
327
|
+
v.builder.startsWith("0x") &&
|
|
328
|
+
typeof v.grantId === "string" &&
|
|
329
|
+
typeof v.signature === "string" &&
|
|
330
|
+
typeof v.bodyHash === "string" &&
|
|
331
|
+
typeof v.writtenAt === "string");
|
|
332
|
+
}
|
|
333
|
+
/** Last path segment of the signed request uri, i.e. the scope written to. */
|
|
334
|
+
function signedScopeOf(uri) {
|
|
335
|
+
const path = uri.split("?")[0] ?? "";
|
|
336
|
+
const segment = path.slice(path.lastIndexOf("/") + 1);
|
|
337
|
+
if (!segment)
|
|
338
|
+
return null;
|
|
339
|
+
try {
|
|
340
|
+
return decodeURIComponent(segment);
|
|
341
|
+
}
|
|
342
|
+
catch {
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Verify builder attribution from a stored envelope alone (no request, no
|
|
348
|
+
* server state, no expiry check: the proof is evidence of who wrote the
|
|
349
|
+
* record at the time, not a live credential). Checks, in order: the envelope
|
|
350
|
+
* carries an attribution; the stored representation (compact JSON of the
|
|
351
|
+
* data minus the server-stamped `$writtenBy` and `$lineage` keys, for JSON
|
|
352
|
+
* and `$binary` records alike) re-hashes
|
|
353
|
+
* to the signed bodyHash; the proof parses and recovers to the attributed
|
|
354
|
+
* builder; and the signed claims bind the proof to THIS record: a POST whose
|
|
355
|
+
* path names the envelope's scope, the stored grantId, and (when given) the
|
|
356
|
+
* server origin. Throws WriterAttributionVerificationError with the reason.
|
|
357
|
+
*/
|
|
358
|
+
export async function verifyStoredWriterAttribution(envelope, options = {}) {
|
|
359
|
+
const data = envelope.data;
|
|
360
|
+
const attribution = data[WRITER_ATTRIBUTION_KEY];
|
|
361
|
+
if (!isWriterAttribution(attribution)) {
|
|
362
|
+
throw new WriterAttributionVerificationError("ATTRIBUTION_MISSING", `Record carries no ${WRITER_ATTRIBUTION_KEY} attribution`);
|
|
363
|
+
}
|
|
364
|
+
// Every server-stamped reserved key is stripped, not just the attribution:
|
|
365
|
+
// `$lineage` is the server's validated mirror of the caller's `lineage`
|
|
366
|
+
// field (the field itself stays inside the signed bytes), so a derivative
|
|
367
|
+
// record re-hashes to the same signed bodyHash as a root record.
|
|
368
|
+
const { [WRITER_ATTRIBUTION_KEY]: _attribution, [LINEAGE_KEY]: storedLineage, ...payloadData } = data;
|
|
369
|
+
const bodyHash = computeBodyHash(new TextEncoder().encode(JSON.stringify(payloadData)));
|
|
370
|
+
if (bodyHash !== attribution.bodyHash) {
|
|
371
|
+
throw new WriterAttributionVerificationError("BODY_HASH_MISMATCH", "Stored record does not hash to the attribution bodyHash");
|
|
372
|
+
}
|
|
373
|
+
let proof;
|
|
374
|
+
let signer;
|
|
375
|
+
try {
|
|
376
|
+
proof = parseWeb3SignedHeader(`${WEB3_SIGNED_SCHEME} ${attribution.signature}`);
|
|
377
|
+
signer = await recoverMessageAddress({
|
|
378
|
+
message: proof.payloadBase64,
|
|
379
|
+
signature: proof.signature,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
catch (err) {
|
|
383
|
+
throw new WriterAttributionVerificationError("PROOF_INVALID", err instanceof Error ? err.message : String(err));
|
|
384
|
+
}
|
|
385
|
+
if (proof.payload.bodyHash !== bodyHash) {
|
|
386
|
+
throw new WriterAttributionVerificationError("BODY_HASH_MISMATCH", "Stored proof does not commit to the attribution bodyHash");
|
|
387
|
+
}
|
|
388
|
+
if (signer.toLowerCase() !== attribution.builder.toLowerCase()) {
|
|
389
|
+
throw new WriterAttributionVerificationError("SIGNER_MISMATCH", "Stored proof is not signed by the attributed builder");
|
|
390
|
+
}
|
|
391
|
+
// Context binding: the signed claims must describe a write of THIS record.
|
|
392
|
+
if (proof.payload.method !== "POST" ||
|
|
393
|
+
signedScopeOf(proof.payload.uri) !== envelope.scope) {
|
|
394
|
+
throw new WriterAttributionVerificationError("SCOPE_MISMATCH", `Stored proof is not a write to scope ${envelope.scope}`);
|
|
395
|
+
}
|
|
396
|
+
if (proof.payload.grantId !== attribution.grantId) {
|
|
397
|
+
throw new WriterAttributionVerificationError("GRANT_MISMATCH", "Stored grantId is not the grant the builder signed");
|
|
398
|
+
}
|
|
399
|
+
if (options.expectedOrigin !== undefined &&
|
|
400
|
+
proof.payload.aud !== options.expectedOrigin) {
|
|
401
|
+
throw new WriterAttributionVerificationError("AUDIENCE_MISMATCH", "Stored proof was not addressed to this server");
|
|
402
|
+
}
|
|
403
|
+
// The stripped mirror is not unauthenticated for that: it must restate
|
|
404
|
+
// the builder-signed `lineage` field (body top level, or the binary
|
|
405
|
+
// record's metadata object) in both directions, so a `$lineage` edited
|
|
406
|
+
// after the write, or one removed to hide a derivative's sources, is
|
|
407
|
+
// caught even though it is outside the hashed bytes. A signed list
|
|
408
|
+
// (empty included: an explicit root statement) must have its mirror; an
|
|
409
|
+
// absent signed field must have none.
|
|
410
|
+
const signedField = extractLineageField(isBinaryEnvelope({ data: payloadData })
|
|
411
|
+
? payloadData.metadata
|
|
412
|
+
: payloadData);
|
|
413
|
+
// Absent or null means "no statement". Anything else that is not a list
|
|
414
|
+
// of strings is a malformed signed field: an independently constructed
|
|
415
|
+
// record must not pass verification just because its lineage claim is
|
|
416
|
+
// unreadable, so it is a mismatch, never "absent".
|
|
417
|
+
const signedAbsent = signedField === undefined || signedField === null;
|
|
418
|
+
const signedWellFormed = Array.isArray(signedField) &&
|
|
419
|
+
signedField.every((id) => typeof id === "string");
|
|
420
|
+
const signed = signedAbsent || !signedWellFormed
|
|
421
|
+
? null
|
|
422
|
+
: signedField.map((id) => id.toLowerCase());
|
|
423
|
+
let mirror;
|
|
424
|
+
try {
|
|
425
|
+
mirror = storedLineage === undefined ? null : readStoredLineage(data);
|
|
426
|
+
}
|
|
427
|
+
catch (error) {
|
|
428
|
+
if (error instanceof StoredLineageMalformedError) {
|
|
429
|
+
throw new WriterAttributionVerificationError("LINEAGE_MISMATCH", `stored $lineage mirror is malformed: ${error.message}`);
|
|
430
|
+
}
|
|
431
|
+
throw error;
|
|
432
|
+
}
|
|
433
|
+
const consistent = !signedAbsent && !signedWellFormed
|
|
434
|
+
? false
|
|
435
|
+
: signed === null
|
|
436
|
+
? storedLineage === undefined
|
|
437
|
+
: mirror !== null &&
|
|
438
|
+
signed.length === mirror.sources.length &&
|
|
439
|
+
signed.every((id, i) => id === mirror.sources[i]);
|
|
440
|
+
if (!consistent) {
|
|
441
|
+
throw new WriterAttributionVerificationError("LINEAGE_MISMATCH", "Stored $lineage does not restate the builder-signed lineage field");
|
|
442
|
+
}
|
|
443
|
+
return {
|
|
444
|
+
builder: attribution.builder,
|
|
445
|
+
grantId: attribution.grantId,
|
|
446
|
+
bodyHash,
|
|
447
|
+
payload: proof.payload,
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
//# sourceMappingURL=attribution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attribution.js","sourceRoot":"","sources":["../../src/write/attribution.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAEH,OAAO,EACL,eAAe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QACf,IAAI,KAAK,YAAY,2BAA2B,EAAE,CAAC;YACjD,MAAM,IAAI,kCAAkC,CAC1C,kBAAkB,EAClB,wCAAwC,KAAK,CAAC,OAAO,EAAE,CACxD,CAAC;QACJ,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;IACD,MAAM,UAAU,GACd,CAAC,YAAY,IAAI,CAAC,gBAAgB;QAChC,CAAC,CAAC,KAAK;QACP,CAAC,CAAC,MAAM,KAAK,IAAI;YACf,CAAC,CAAC,aAAa,KAAK,SAAS;YAC7B,CAAC,CAAC,MAAM,KAAK,IAAI;gBACf,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC,OAAO,CAAC,MAAM;gBACvC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1D,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,kCAAkC,CAC1C,kBAAkB,EAClB,mEAAmE,CACpE,CAAC;IACJ,CAAC;IACD,OAAO;QACL,OAAO,EAAE,WAAW,CAAC,OAAO;QAC5B,OAAO,EAAE,WAAW,CAAC,OAAO;QAC5B,QAAQ;QACR,OAAO,EAAE,KAAK,CAAC,OAAO;KACvB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delegated-write authorization, shared by every build.
|
|
3
|
+
*
|
|
4
|
+
* `authorizeWrite` / `authorizeWriteSession` are the two write-session halves
|
|
5
|
+
* of `PersonalServerApiAuthPort`. They were originally written inside the Node
|
|
6
|
+
* server's auth adapter (packages/server/src/api-auth.ts); the checks they run
|
|
7
|
+
* are pure protocol — a token hash, a live grant re-check, an EIP-191 payload
|
|
8
|
+
* proof and a replay reservation — with nothing Node-specific in them, so they
|
|
9
|
+
* live here and BOTH the Node server adapter and the browser (PS-Lite)
|
|
10
|
+
* adapters call the same code. There is exactly one implementation of the
|
|
11
|
+
* write policy; a browser build cannot drift into a weaker one.
|
|
12
|
+
*
|
|
13
|
+
* The two entry points differ in what they are allowed to decide:
|
|
14
|
+
*
|
|
15
|
+
* - `authorizeSessionWrite` authorizes a write to a NAMED scope. It
|
|
16
|
+
* re-runs `verifyDataWritePolicy` against the LIVE grant on every call,
|
|
17
|
+
* so revocation, expiry, grantee binding, owner binding and scope
|
|
18
|
+
* coverage stay authoritative per write rather than being frozen at
|
|
19
|
+
* handshake, and it verifies the builder's per-write payload proof.
|
|
20
|
+
* - `recognizeWriteSession` is identity only: the bearer must resolve to a
|
|
21
|
+
* live session and the request must carry that builder's valid proof, but
|
|
22
|
+
* NO grant policy runs (there is no scope to run one against). Callers use
|
|
23
|
+
* it only to choose between error shapes, never to release data.
|
|
24
|
+
*
|
|
25
|
+
* Both fail closed: an unknown bearer, an unknown session, a missing server
|
|
26
|
+
* owner, or any failing proof check returns/raises rather than falling back to
|
|
27
|
+
* a weaker credential. A request that is not a write-session request at all
|
|
28
|
+
* returns `undefined`, and the caller applies its own owner gate.
|
|
29
|
+
*/
|
|
30
|
+
import { ProtocolError } from "../errors/catalog.js";
|
|
31
|
+
import { type DataWritePolicyPorts } from "../policy/data-write.js";
|
|
32
|
+
import type { PersonalServerWriteAuthInput, PersonalServerWriteAuthResult, PersonalServerWriteSessionResult } from "../api/index.js";
|
|
33
|
+
import { type WriteProofReplayStore, type WriteSessionStore } from "./session.js";
|
|
34
|
+
export interface WriteSessionAuthorizationDeps {
|
|
35
|
+
/**
|
|
36
|
+
* This server's origin. Every payload proof must name it as its audience,
|
|
37
|
+
* so a proof signed for another Personal Server cannot be relayed here.
|
|
38
|
+
*/
|
|
39
|
+
serverOrigin: string | (() => string);
|
|
40
|
+
/**
|
|
41
|
+
* The owner every grant must have been issued by. Absent = fail closed:
|
|
42
|
+
* a delegated write cannot be bound to an owner we cannot name.
|
|
43
|
+
*/
|
|
44
|
+
serverOwner?: `0x${string}`;
|
|
45
|
+
/**
|
|
46
|
+
* Live write sessions (minted by POST /v1/write/session). Absent = the host
|
|
47
|
+
* does not support delegated writes and every request falls through to the
|
|
48
|
+
* caller's owner gate.
|
|
49
|
+
*/
|
|
50
|
+
sessionStore?: WriteSessionStore;
|
|
51
|
+
/** Replay guard for the per-write `X-Vana-Write-Signature` proofs. */
|
|
52
|
+
replayStore: WriteProofReplayStore;
|
|
53
|
+
/** Live builder/grant verification, re-run on every delegated write. */
|
|
54
|
+
policyPorts: DataWritePolicyPorts;
|
|
55
|
+
now?: () => Date;
|
|
56
|
+
}
|
|
57
|
+
export interface WriteSessionAuthorization {
|
|
58
|
+
/**
|
|
59
|
+
* Authorize a delegated write to `input.scope`, or `undefined` when the
|
|
60
|
+
* request carries no live write-session credential (the caller then applies
|
|
61
|
+
* its owner gate, exactly as before write sessions existed).
|
|
62
|
+
*/
|
|
63
|
+
authorizeSessionWrite(input: PersonalServerWriteAuthInput): Promise<PersonalServerWriteAuthResult | undefined>;
|
|
64
|
+
/**
|
|
65
|
+
* Identity-only recognition of a write-session caller. Never releases data.
|
|
66
|
+
*/
|
|
67
|
+
recognizeWriteSession(request: Request): Promise<PersonalServerWriteSessionResult | undefined>;
|
|
68
|
+
}
|
|
69
|
+
/** The bearer credential a write session is presented under. */
|
|
70
|
+
export declare function writeSessionBearerToken(request: Request): string | null;
|
|
71
|
+
/**
|
|
72
|
+
* Same shape and message as the auth layer's own missing-owner failure, so a
|
|
73
|
+
* host that never configured an owner reports one thing, not two.
|
|
74
|
+
*/
|
|
75
|
+
export declare function writeServerNotConfigured(): ProtocolError;
|
|
76
|
+
export declare function createWriteSessionAuthorization(deps: WriteSessionAuthorizationDeps): WriteSessionAuthorization;
|
|
77
|
+
//# sourceMappingURL=authorize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authorize.d.ts","sourceRoot":"","sources":["../../src/write/authorize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;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Session` are the two write-session halves
|
|
5
|
+
* of `PersonalServerApiAuthPort`. They were originally written inside the Node
|
|
6
|
+
* server's auth adapter (packages/server/src/api-auth.ts); the checks they run
|
|
7
|
+
* are pure protocol — a token hash, a live grant re-check, an EIP-191 payload
|
|
8
|
+
* proof and a replay reservation — with nothing Node-specific in them, so they
|
|
9
|
+
* live here and BOTH the Node server adapter and the browser (PS-Lite)
|
|
10
|
+
* adapters call the same code. There is exactly one implementation of the
|
|
11
|
+
* write policy; a browser build cannot drift into a weaker one.
|
|
12
|
+
*
|
|
13
|
+
* The two entry points differ in what they are allowed to decide:
|
|
14
|
+
*
|
|
15
|
+
* - `authorizeSessionWrite` authorizes a write to a NAMED scope. It
|
|
16
|
+
* re-runs `verifyDataWritePolicy` against the LIVE grant on every call,
|
|
17
|
+
* so revocation, expiry, grantee binding, owner binding and scope
|
|
18
|
+
* coverage stay authoritative per write rather than being frozen at
|
|
19
|
+
* handshake, and it verifies the builder's per-write payload proof.
|
|
20
|
+
* - `recognizeWriteSession` is identity only: the bearer must resolve to a
|
|
21
|
+
* live session and the request must carry that builder's valid proof, but
|
|
22
|
+
* NO grant policy runs (there is no scope to run one against). Callers use
|
|
23
|
+
* it only to choose between error shapes, never to release data.
|
|
24
|
+
*
|
|
25
|
+
* Both fail closed: an unknown bearer, an unknown session, a missing server
|
|
26
|
+
* owner, or any failing proof check returns/raises rather than falling back to
|
|
27
|
+
* a weaker credential. A request that is not a write-session request at all
|
|
28
|
+
* returns `undefined`, and the caller applies its own owner gate.
|
|
29
|
+
*/
|
|
30
|
+
import { ProtocolError } from "../errors/catalog.js";
|
|
31
|
+
import { verifyDataWritePolicy, } from "../policy/data-write.js";
|
|
32
|
+
import { verifyWriterAttribution } from "./attribution.js";
|
|
33
|
+
import { hashWriteSessionToken, } from "./session.js";
|
|
34
|
+
/** The bearer credential a write session is presented under. */
|
|
35
|
+
export function writeSessionBearerToken(request) {
|
|
36
|
+
const header = request.headers.get("authorization");
|
|
37
|
+
if (!header?.startsWith("Bearer "))
|
|
38
|
+
return null;
|
|
39
|
+
return header.slice(7);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Same shape and message as the auth layer's own missing-owner failure, so a
|
|
43
|
+
* host that never configured an owner reports one thing, not two.
|
|
44
|
+
*/
|
|
45
|
+
export function writeServerNotConfigured() {
|
|
46
|
+
return new ProtocolError(500, "SERVER_NOT_CONFIGURED", "Server owner address not configured. Set VANA_MASTER_KEY_SIGNATURE environment variable.");
|
|
47
|
+
}
|
|
48
|
+
export function createWriteSessionAuthorization(deps) {
|
|
49
|
+
async function liveSession(request) {
|
|
50
|
+
const token = writeSessionBearerToken(request);
|
|
51
|
+
if (!token || !deps.sessionStore)
|
|
52
|
+
return null;
|
|
53
|
+
return deps.sessionStore.getByTokenHash(await hashWriteSessionToken(token));
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
async authorizeSessionWrite(input) {
|
|
57
|
+
const session = await liveSession(input.request);
|
|
58
|
+
if (!session)
|
|
59
|
+
return undefined;
|
|
60
|
+
if (!deps.serverOwner)
|
|
61
|
+
throw writeServerNotConfigured();
|
|
62
|
+
// The grant is re-checked live on EVERY write: a session token minted
|
|
63
|
+
// an hour ago against a grant that has since been revoked, narrowed or
|
|
64
|
+
// expired authorizes nothing.
|
|
65
|
+
const grant = await verifyDataWritePolicy({
|
|
66
|
+
signer: session.builderAddress,
|
|
67
|
+
grantId: session.grantId,
|
|
68
|
+
requestedScope: input.scope,
|
|
69
|
+
serverOwner: deps.serverOwner,
|
|
70
|
+
}, deps.policyPorts);
|
|
71
|
+
// releaseProof is a rollback hook for the handler, never part of the
|
|
72
|
+
// stored attribution record.
|
|
73
|
+
const { releaseProof, ...attribution } = await verifyWriterAttribution({
|
|
74
|
+
request: input.request,
|
|
75
|
+
builderAddress: session.builderAddress,
|
|
76
|
+
grantId: grant.id,
|
|
77
|
+
serverOrigin: deps.serverOrigin,
|
|
78
|
+
replayStore: deps.replayStore,
|
|
79
|
+
now: deps.now,
|
|
80
|
+
});
|
|
81
|
+
return {
|
|
82
|
+
builder: session.builderAddress,
|
|
83
|
+
grantId: grant.id,
|
|
84
|
+
grantScopes: grant.scopes ?? [],
|
|
85
|
+
attribution,
|
|
86
|
+
releaseProof,
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
async recognizeWriteSession(request) {
|
|
90
|
+
const session = await liveSession(request);
|
|
91
|
+
if (!session)
|
|
92
|
+
return undefined;
|
|
93
|
+
const { releaseProof } = await verifyWriterAttribution({
|
|
94
|
+
request,
|
|
95
|
+
builderAddress: session.builderAddress,
|
|
96
|
+
grantId: session.grantId,
|
|
97
|
+
serverOrigin: deps.serverOrigin,
|
|
98
|
+
replayStore: deps.replayStore,
|
|
99
|
+
now: deps.now,
|
|
100
|
+
});
|
|
101
|
+
return {
|
|
102
|
+
builder: session.builderAddress,
|
|
103
|
+
grantId: session.grantId,
|
|
104
|
+
releaseProof,
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=authorize.js.map
|