@opendatalabs/personal-server-ts-core 1.4.0 → 1.6.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/dist/api/index.d.ts +80 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +126 -68
- package/dist/api/index.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 +21 -0
- package/dist/contracts/data.d.ts.map +1 -1
- package/dist/contracts/data.js +45 -4
- package/dist/contracts/data.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/logging/access-log.d.ts +1 -1
- package/dist/logging/access-log.d.ts.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 +23 -0
- package/dist/mcp/read-client.d.ts.map +1 -1
- package/dist/mcp/read-client.js +16 -4
- package/dist/mcp/read-client.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 +234 -0
- package/dist/mcp/session.js.map +1 -0
- package/dist/mcp/tools.d.ts.map +1 -1
- package/dist/mcp/tools.js +135 -15
- package/dist/mcp/tools.js.map +1 -1
- package/dist/policy/data-read.d.ts +8 -0
- package/dist/policy/data-read.d.ts.map +1 -1
- package/dist/policy/data-read.js +9 -2
- 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 +2 -1
- package/dist/policy/index.d.ts.map +1 -1
- package/dist/policy/index.js +2 -1
- package/dist/policy/index.js.map +1 -1
- package/dist/write/attribution.d.ts +165 -0
- package/dist/write/attribution.d.ts.map +1 -0
- package/dist/write/attribution.js +277 -0
- package/dist/write/attribution.js.map +1 -0
- package/dist/write/index.d.ts +3 -0
- package/dist/write/index.d.ts.map +1 -0
- package/dist/write/index.js +3 -0
- package/dist/write/index.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 +5 -1
|
@@ -0,0 +1,165 @@
|
|
|
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 path (whose last segment is the scope), the
|
|
18
|
+
* method, the audience (this server's origin) and the session's grantId,
|
|
19
|
+
* which the PS requires to match the session at ingest. A stored `$writtenBy`
|
|
20
|
+
* therefore cannot be lifted onto another scope's record or relabelled with a
|
|
21
|
+
* different grant and still verify.
|
|
22
|
+
*
|
|
23
|
+
* What the builder signs is the STORED representation, not the wire body:
|
|
24
|
+
* the compact JSON of the envelope `data` record the PS will write (minus
|
|
25
|
+
* `$writtenBy`). A third party holding the envelope can therefore verify
|
|
26
|
+
* authorship from the envelope ALONE (verifyStoredWriterAttribution): recover
|
|
27
|
+
* the signer over the stored proof's base64url payload (EIP-191), check the
|
|
28
|
+
* signed claims against the envelope's scope and stored grantId, and hash
|
|
29
|
+
* JSON.stringify(data minus $writtenBy) against the signed bodyHash.
|
|
30
|
+
* - JSON writes: the body IS the stored record, so the builder signs the
|
|
31
|
+
* body bytes, which must be compact JSON (what JSON.stringify emits: no
|
|
32
|
+
* insignificant whitespace, keys in the order given). JSON.parse /
|
|
33
|
+
* JSON.stringify round-trips that byte-for-byte; a body that does not
|
|
34
|
+
* round-trip is rejected at write time (WRITE_BODY_NOT_CANONICAL) instead
|
|
35
|
+
* of being stored with an attribution nobody can check.
|
|
36
|
+
* - Binary writes: the stored record is the `$binary` envelope data (media
|
|
37
|
+
* type, filename, caller metadata, size, content hash, base64 content),
|
|
38
|
+
* so the builder signs its compact JSON (binaryWriteSignedBytes). That
|
|
39
|
+
* binds the caller-controlled representation headers (Content-Type,
|
|
40
|
+
* X-Filename / Content-Disposition, X-Vana-Metadata) to the signature and
|
|
41
|
+
* makes a JSON-vs-binary switch of the same bytes fail verification.
|
|
42
|
+
*/
|
|
43
|
+
import { type Web3SignedPayload } from "@opendatalabs/vana-sdk/browser";
|
|
44
|
+
import type { WriteProofReplayStore } from "./session.js";
|
|
45
|
+
/** Header carrying the builder's signed-payload proof on a session write. */
|
|
46
|
+
export declare const WRITE_SIGNATURE_HEADER = "x-vana-write-signature";
|
|
47
|
+
/**
|
|
48
|
+
* Reserved key inside the envelope's `data` record for builder attribution.
|
|
49
|
+
* Mirrors the `$binary` marker convention (contracts/binary.ts).
|
|
50
|
+
*/
|
|
51
|
+
export declare const WRITER_ATTRIBUTION_KEY: "$writtenBy";
|
|
52
|
+
export interface WriterAttribution {
|
|
53
|
+
/** The builder address the proof recovered to. */
|
|
54
|
+
builder: `0x${string}`;
|
|
55
|
+
/** The write-grant the record was written under. */
|
|
56
|
+
grantId: string;
|
|
57
|
+
/**
|
|
58
|
+
* The builder's compact Web3Signed proof (`{base64url(payload)}.{sig}`,
|
|
59
|
+
* scheme prefix stripped). Verifiable offline: recover the EIP-191 signer
|
|
60
|
+
* over the base64url payload string; the decoded payload's `bodyHash`
|
|
61
|
+
* commits to the written bytes.
|
|
62
|
+
*/
|
|
63
|
+
signature: string;
|
|
64
|
+
/** `bodyHash` claim from the proof (sha-256 of the request body bytes). */
|
|
65
|
+
bodyHash: string;
|
|
66
|
+
/** ISO timestamp the PS accepted the write. */
|
|
67
|
+
writtenAt: string;
|
|
68
|
+
}
|
|
69
|
+
export interface VerifyWriterAttributionInput {
|
|
70
|
+
request: Request;
|
|
71
|
+
/** The builder address bound to the write session at handshake. */
|
|
72
|
+
builderAddress: `0x${string}`;
|
|
73
|
+
/** The grant the session (and therefore this write) is bound to. */
|
|
74
|
+
grantId: string;
|
|
75
|
+
serverOrigin: string | (() => string);
|
|
76
|
+
now?: () => Date;
|
|
77
|
+
/**
|
|
78
|
+
* Replay guard for the per-write proof. When supplied, a proof is consumed
|
|
79
|
+
* on successful verification (keyed by a digest of the exact header,
|
|
80
|
+
* remembered until the proof's own expiry) and a second write carrying the
|
|
81
|
+
* same proof is rejected: holding the session bearer plus one captured
|
|
82
|
+
* signed request must not allow re-storing it until the proof expires.
|
|
83
|
+
*/
|
|
84
|
+
replayStore?: WriteProofReplayStore;
|
|
85
|
+
}
|
|
86
|
+
/** verifyWriterAttribution's result: the record to store plus a rollback. */
|
|
87
|
+
export interface VerifiedWriterAttribution extends WriterAttribution {
|
|
88
|
+
/**
|
|
89
|
+
* Present when a replay store consumed the proof. Callers whose write then
|
|
90
|
+
* FAILS before the record is committed should call it so the builder can
|
|
91
|
+
* retry with the same still-valid proof (a retry after success is a replay).
|
|
92
|
+
*/
|
|
93
|
+
releaseProof?: () => Promise<void>;
|
|
94
|
+
}
|
|
95
|
+
export interface BinaryWriteSignedBytesInput {
|
|
96
|
+
/** The raw body bytes the write will send. */
|
|
97
|
+
bytes: Uint8Array;
|
|
98
|
+
/** The Content-Type header the write will send (parameters are ignored). */
|
|
99
|
+
contentType: string;
|
|
100
|
+
/** The X-Filename header value the write will send, if any. */
|
|
101
|
+
filename?: string;
|
|
102
|
+
/** The exact X-Vana-Metadata header value the write will send, if any. */
|
|
103
|
+
metadataHeader?: string;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The bytes a builder signs (as the Web3Signed body) for a BINARY session
|
|
107
|
+
* write: the compact JSON of the `$binary` record the PS stores for these
|
|
108
|
+
* headers and bytes. Mirrors ingest exactly (same media-type normalization,
|
|
109
|
+
* metadata parsing and envelope builder), so the signature covers the stored
|
|
110
|
+
* representation and verifyStoredWriterAttribution can re-derive it.
|
|
111
|
+
*/
|
|
112
|
+
export declare function binaryWriteSignedBytes(input: BinaryWriteSignedBytesInput): Promise<Uint8Array>;
|
|
113
|
+
/**
|
|
114
|
+
* Verify the `X-Vana-Write-Signature` proof on a session write and produce
|
|
115
|
+
* the attribution record to store with the data. Throws ProtocolError(401)
|
|
116
|
+
* when the proof is missing, malformed, expired, fails EIP-191 recovery /
|
|
117
|
+
* bodyHash binding (the hash covers the stored representation, see
|
|
118
|
+
* signedBytesFor), recovers to a different key than the session builder, or
|
|
119
|
+
* does not carry the session's grantId as a signed claim.
|
|
120
|
+
*/
|
|
121
|
+
export declare function verifyWriterAttribution(input: VerifyWriterAttributionInput): Promise<VerifiedWriterAttribution>;
|
|
122
|
+
/**
|
|
123
|
+
* Stamp attribution into an envelope `data` record. The reserved key must not
|
|
124
|
+
* appear in the caller's payload — a builder must not be able to forge (or
|
|
125
|
+
* shadow) its own attribution.
|
|
126
|
+
*/
|
|
127
|
+
export declare function stampWriterAttribution(data: Record<string, unknown>, attribution: WriterAttribution): Record<string, unknown>;
|
|
128
|
+
export declare function hasReservedWriterKey(data: Record<string, unknown>): boolean;
|
|
129
|
+
export declare class WriterAttributionVerificationError extends Error {
|
|
130
|
+
readonly reason: "ATTRIBUTION_MISSING" | "BODY_HASH_MISMATCH" | "PROOF_INVALID" | "SIGNER_MISMATCH" | "SCOPE_MISMATCH" | "GRANT_MISMATCH" | "AUDIENCE_MISMATCH";
|
|
131
|
+
constructor(reason: "ATTRIBUTION_MISSING" | "BODY_HASH_MISMATCH" | "PROOF_INVALID" | "SIGNER_MISMATCH" | "SCOPE_MISMATCH" | "GRANT_MISMATCH" | "AUDIENCE_MISMATCH", message: string);
|
|
132
|
+
}
|
|
133
|
+
export interface StoredWriterAttributionVerification {
|
|
134
|
+
/** Signer recovered from the stored proof (equals the stored builder). */
|
|
135
|
+
builder: `0x${string}`;
|
|
136
|
+
grantId: string;
|
|
137
|
+
/** Hash re-derived from the stored data; equals the proof's bodyHash. */
|
|
138
|
+
bodyHash: string;
|
|
139
|
+
/** The signed claims (aud / method / uri / bodyHash / iat / exp). */
|
|
140
|
+
payload: Web3SignedPayload;
|
|
141
|
+
}
|
|
142
|
+
export interface VerifyStoredWriterAttributionOptions {
|
|
143
|
+
/**
|
|
144
|
+
* When known, the origin of the Personal Server the record was written to;
|
|
145
|
+
* the proof's signed audience must match it. Omit for records whose server
|
|
146
|
+
* is not known to the verifier.
|
|
147
|
+
*/
|
|
148
|
+
expectedOrigin?: string;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Verify builder attribution from a stored envelope alone (no request, no
|
|
152
|
+
* server state, no expiry check: the proof is evidence of who wrote the
|
|
153
|
+
* record at the time, not a live credential). Checks, in order: the envelope
|
|
154
|
+
* carries an attribution; the stored representation (compact JSON of the
|
|
155
|
+
* data minus `$writtenBy`, for JSON and `$binary` records alike) re-hashes
|
|
156
|
+
* to the signed bodyHash; the proof parses and recovers to the attributed
|
|
157
|
+
* builder; and the signed claims bind the proof to THIS record: a POST whose
|
|
158
|
+
* path names the envelope's scope, the stored grantId, and (when given) the
|
|
159
|
+
* server origin. Throws WriterAttributionVerificationError with the reason.
|
|
160
|
+
*/
|
|
161
|
+
export declare function verifyStoredWriterAttribution(envelope: {
|
|
162
|
+
scope: string;
|
|
163
|
+
data: Record<string, unknown>;
|
|
164
|
+
}, options?: VerifyStoredWriterAttributionOptions): Promise<StoredWriterAttributionVerification>;
|
|
165
|
+
//# sourceMappingURL=attribution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attribution.d.ts","sourceRoot":"","sources":["../../src/write/attribution.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAEH,OAAO,EAIL,KAAK,iBAAiB,EACvB,MAAM,gCAAgC,CAAC;AAIxC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAW1D,6EAA6E;AAC7E,eAAO,MAAM,sBAAsB,2BAA2B,CAAC;AAE/D;;;GAGG;AACH,eAAO,MAAM,sBAAsB,EAAG,YAAqB,CAAC;AAE5D,MAAM,WAAW,iBAAiB;IAChC,kDAAkD;IAClD,OAAO,EAAE,KAAK,MAAM,EAAE,CAAC;IACvB,oDAAoD;IACpD,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB,2EAA2E;IAC3E,QAAQ,EAAE,MAAM,CAAC;IACjB,+CAA+C;IAC/C,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,4BAA4B;IAC3C,OAAO,EAAE,OAAO,CAAC;IACjB,mEAAmE;IACnE,cAAc,EAAE,KAAK,MAAM,EAAE,CAAC;IAC9B,oEAAoE;IACpE,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,CAAC;IACtC,GAAG,CAAC,EAAE,MAAM,IAAI,CAAC;IACjB;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,qBAAqB,CAAC;CACrC;AAED,6EAA6E;AA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ChB,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC;IACjB,qEAAqE;IACrE,OAAO,EAAE,iBAAiB,CAAC;CAC5B;AAeD,MAAM,WAAW,oCAAoC;IACnD;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAcD;;;;;;;;;;GAUG;AACH,wBAAsB,6BAA6B,CACjD,QAAQ,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,EAC1D,OAAO,GAAE,oCAAyC,GACjD,OAAO,CAAC,mCAAmC,CAAC,CAgF9C"}
|
|
@@ -0,0 +1,277 @@
|
|
|
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 path (whose last segment is the scope), the
|
|
18
|
+
* method, the audience (this server's origin) and the session's grantId,
|
|
19
|
+
* which the PS requires to match the session at ingest. A stored `$writtenBy`
|
|
20
|
+
* therefore cannot be lifted onto another scope's record or relabelled with a
|
|
21
|
+
* different grant and still verify.
|
|
22
|
+
*
|
|
23
|
+
* What the builder signs is the STORED representation, not the wire body:
|
|
24
|
+
* the compact JSON of the envelope `data` record the PS will write (minus
|
|
25
|
+
* `$writtenBy`). A third party holding the envelope can therefore verify
|
|
26
|
+
* authorship from the envelope ALONE (verifyStoredWriterAttribution): recover
|
|
27
|
+
* the signer over the stored proof's base64url payload (EIP-191), check the
|
|
28
|
+
* signed claims against the envelope's scope and stored grantId, and hash
|
|
29
|
+
* JSON.stringify(data minus $writtenBy) against the signed bodyHash.
|
|
30
|
+
* - JSON writes: the body IS the stored record, so the builder signs the
|
|
31
|
+
* body bytes, which must be compact JSON (what JSON.stringify emits: no
|
|
32
|
+
* insignificant whitespace, keys in the order given). JSON.parse /
|
|
33
|
+
* JSON.stringify round-trips that byte-for-byte; a body that does not
|
|
34
|
+
* round-trip is rejected at write time (WRITE_BODY_NOT_CANONICAL) instead
|
|
35
|
+
* of being stored with an attribution nobody can check.
|
|
36
|
+
* - Binary writes: the stored record is the `$binary` envelope data (media
|
|
37
|
+
* type, filename, caller metadata, size, content hash, base64 content),
|
|
38
|
+
* so the builder signs its compact JSON (binaryWriteSignedBytes). That
|
|
39
|
+
* binds the caller-controlled representation headers (Content-Type,
|
|
40
|
+
* X-Filename / Content-Disposition, X-Vana-Metadata) to the signature and
|
|
41
|
+
* makes a JSON-vs-binary switch of the same bytes fail verification.
|
|
42
|
+
*/
|
|
43
|
+
import { computeBodyHash, parseWeb3SignedHeader, verifyWeb3Signed, } from "@opendatalabs/vana-sdk/browser";
|
|
44
|
+
import { recoverMessageAddress } from "viem";
|
|
45
|
+
import { ProtocolError } from "../errors/catalog.js";
|
|
46
|
+
import { hashConnectionToken as sha256HexOf } from "../mcp/connection-api.js";
|
|
47
|
+
import { binaryFilename, binaryMimeType, buildBinaryEnvelopeData, isJsonContentType, normalizeBinaryMimeType, parseMetadataHeader, sha256Hex, } from "../contracts/binary.js";
|
|
48
|
+
/** Header carrying the builder's signed-payload proof on a session write. */
|
|
49
|
+
export const WRITE_SIGNATURE_HEADER = "x-vana-write-signature";
|
|
50
|
+
/**
|
|
51
|
+
* Reserved key inside the envelope's `data` record for builder attribution.
|
|
52
|
+
* Mirrors the `$binary` marker convention (contracts/binary.ts).
|
|
53
|
+
*/
|
|
54
|
+
export const WRITER_ATTRIBUTION_KEY = "$writtenBy";
|
|
55
|
+
function resolveOrigin(origin) {
|
|
56
|
+
return typeof origin === "function" ? origin() : origin;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The bytes a builder signs (as the Web3Signed body) for a BINARY session
|
|
60
|
+
* write: the compact JSON of the `$binary` record the PS stores for these
|
|
61
|
+
* headers and bytes. Mirrors ingest exactly (same media-type normalization,
|
|
62
|
+
* metadata parsing and envelope builder), so the signature covers the stored
|
|
63
|
+
* representation and verifyStoredWriterAttribution can re-derive it.
|
|
64
|
+
*/
|
|
65
|
+
export async function binaryWriteSignedBytes(input) {
|
|
66
|
+
const data = buildBinaryEnvelopeData({
|
|
67
|
+
bytes: input.bytes,
|
|
68
|
+
mimeType: normalizeBinaryMimeType(input.contentType),
|
|
69
|
+
filename: input.filename,
|
|
70
|
+
contentHash: await sha256Hex(input.bytes),
|
|
71
|
+
metadata: parseMetadataHeader(input.metadataHeader ?? null),
|
|
72
|
+
});
|
|
73
|
+
return new TextEncoder().encode(JSON.stringify(data));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The bytes the proof must commit to for this request: the body itself for
|
|
77
|
+
* a JSON write, the stored `$binary` representation for anything else.
|
|
78
|
+
*/
|
|
79
|
+
async function signedBytesFor(request, bodyBytes) {
|
|
80
|
+
if (isJsonContentType(request))
|
|
81
|
+
return bodyBytes;
|
|
82
|
+
return binaryWriteSignedBytes({
|
|
83
|
+
bytes: bodyBytes,
|
|
84
|
+
contentType: binaryMimeType(request),
|
|
85
|
+
filename: binaryFilename(request),
|
|
86
|
+
metadataHeader: request.headers.get("x-vana-metadata") ?? undefined,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Verify the `X-Vana-Write-Signature` proof on a session write and produce
|
|
91
|
+
* the attribution record to store with the data. Throws ProtocolError(401)
|
|
92
|
+
* when the proof is missing, malformed, expired, fails EIP-191 recovery /
|
|
93
|
+
* bodyHash binding (the hash covers the stored representation, see
|
|
94
|
+
* signedBytesFor), recovers to a different key than the session builder, or
|
|
95
|
+
* does not carry the session's grantId as a signed claim.
|
|
96
|
+
*/
|
|
97
|
+
export async function verifyWriterAttribution(input) {
|
|
98
|
+
const headerValue = input.request.headers.get(WRITE_SIGNATURE_HEADER) ?? undefined;
|
|
99
|
+
if (!headerValue) {
|
|
100
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_REQUIRED", `Session writes must carry a builder-signed payload proof in ${WRITE_SIGNATURE_HEADER}`);
|
|
101
|
+
}
|
|
102
|
+
const url = new URL(input.request.url);
|
|
103
|
+
const bodyBytes = new Uint8Array(await input.request.clone().arrayBuffer());
|
|
104
|
+
let verified;
|
|
105
|
+
try {
|
|
106
|
+
verified = await verifyWeb3Signed({
|
|
107
|
+
headerValue,
|
|
108
|
+
expectedOrigin: resolveOrigin(input.serverOrigin),
|
|
109
|
+
expectedMethod: input.request.method,
|
|
110
|
+
expectedPath: url.pathname,
|
|
111
|
+
bodyBytes: await signedBytesFor(input.request, bodyBytes),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_INVALID", err instanceof Error ? err.message : String(err));
|
|
116
|
+
}
|
|
117
|
+
if (verified.signer.toLowerCase() !== input.builderAddress.toLowerCase()) {
|
|
118
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_SIGNER_MISMATCH", "Payload proof is not signed by the session builder", { expected: input.builderAddress, actual: verified.signer });
|
|
119
|
+
}
|
|
120
|
+
// The grant is a signed claim, so the stored attribution's grantId is
|
|
121
|
+
// covered by the builder's signature rather than being a bare label.
|
|
122
|
+
if (verified.payload.grantId !== input.grantId) {
|
|
123
|
+
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 });
|
|
124
|
+
}
|
|
125
|
+
if (isJsonContentType(input.request)) {
|
|
126
|
+
assertCanonicalJsonBody(bodyBytes);
|
|
127
|
+
}
|
|
128
|
+
// Replay guard, last: only a proof that passed every check is consumed,
|
|
129
|
+
// so a rejected request never burns a proof.
|
|
130
|
+
let releaseProof;
|
|
131
|
+
if (input.replayStore) {
|
|
132
|
+
const store = input.replayStore;
|
|
133
|
+
const proofId = await sha256HexOf(headerValue);
|
|
134
|
+
const replayed = await store.consume(proofId, verified.payload.exp * 1000);
|
|
135
|
+
if (replayed) {
|
|
136
|
+
throw new ProtocolError(401, "WRITE_ATTRIBUTION_REPLAY", "Payload proof already used; sign a fresh proof for each write");
|
|
137
|
+
}
|
|
138
|
+
releaseProof = async () => {
|
|
139
|
+
await store.release?.(proofId);
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
// Store the compact `{payload}.{signature}` form (scheme prefix stripped)
|
|
143
|
+
// so verifiers don't need to know the header framing.
|
|
144
|
+
const { payloadBase64, signature } = parseWeb3SignedHeader(headerValue);
|
|
145
|
+
return {
|
|
146
|
+
builder: input.builderAddress,
|
|
147
|
+
grantId: input.grantId,
|
|
148
|
+
signature: `${payloadBase64}.${signature}`,
|
|
149
|
+
bodyHash: verified.payload.bodyHash,
|
|
150
|
+
writtenAt: (input.now?.() ?? new Date()).toISOString(),
|
|
151
|
+
...(releaseProof ? { releaseProof } : {}),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Stamp attribution into an envelope `data` record. The reserved key must not
|
|
156
|
+
* appear in the caller's payload — a builder must not be able to forge (or
|
|
157
|
+
* shadow) its own attribution.
|
|
158
|
+
*/
|
|
159
|
+
export function stampWriterAttribution(data, attribution) {
|
|
160
|
+
return { ...data, [WRITER_ATTRIBUTION_KEY]: attribution };
|
|
161
|
+
}
|
|
162
|
+
export function hasReservedWriterKey(data) {
|
|
163
|
+
return Object.prototype.hasOwnProperty.call(data, WRITER_ATTRIBUTION_KEY);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* A JSON session write must be the compact serialization of its own parsed
|
|
167
|
+
* value, so the record stored after JSON.parse re-serializes to the signed
|
|
168
|
+
* bytes. Bodies that do not parse are left to the ingest path, which reports
|
|
169
|
+
* the parse error itself.
|
|
170
|
+
*/
|
|
171
|
+
function assertCanonicalJsonBody(bodyBytes) {
|
|
172
|
+
const text = new TextDecoder().decode(bodyBytes);
|
|
173
|
+
let parsed;
|
|
174
|
+
try {
|
|
175
|
+
parsed = JSON.parse(text);
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (JSON.stringify(parsed) !== text) {
|
|
181
|
+
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");
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** Proof scheme prefix the compact stored signature was stripped of. */
|
|
185
|
+
const WEB3_SIGNED_SCHEME = "Web3Signed";
|
|
186
|
+
export class WriterAttributionVerificationError extends Error {
|
|
187
|
+
reason;
|
|
188
|
+
constructor(reason, message) {
|
|
189
|
+
super(message);
|
|
190
|
+
this.reason = reason;
|
|
191
|
+
this.name = "WriterAttributionVerificationError";
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function isWriterAttribution(value) {
|
|
195
|
+
if (typeof value !== "object" || value === null)
|
|
196
|
+
return false;
|
|
197
|
+
const v = value;
|
|
198
|
+
return (typeof v.builder === "string" &&
|
|
199
|
+
v.builder.startsWith("0x") &&
|
|
200
|
+
typeof v.grantId === "string" &&
|
|
201
|
+
typeof v.signature === "string" &&
|
|
202
|
+
typeof v.bodyHash === "string" &&
|
|
203
|
+
typeof v.writtenAt === "string");
|
|
204
|
+
}
|
|
205
|
+
/** Last path segment of the signed request uri, i.e. the scope written to. */
|
|
206
|
+
function signedScopeOf(uri) {
|
|
207
|
+
const path = uri.split("?")[0] ?? "";
|
|
208
|
+
const segment = path.slice(path.lastIndexOf("/") + 1);
|
|
209
|
+
if (!segment)
|
|
210
|
+
return null;
|
|
211
|
+
try {
|
|
212
|
+
return decodeURIComponent(segment);
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Verify builder attribution from a stored envelope alone (no request, no
|
|
220
|
+
* server state, no expiry check: the proof is evidence of who wrote the
|
|
221
|
+
* record at the time, not a live credential). Checks, in order: the envelope
|
|
222
|
+
* carries an attribution; the stored representation (compact JSON of the
|
|
223
|
+
* data minus `$writtenBy`, for JSON and `$binary` records alike) re-hashes
|
|
224
|
+
* to the signed bodyHash; the proof parses and recovers to the attributed
|
|
225
|
+
* builder; and the signed claims bind the proof to THIS record: a POST whose
|
|
226
|
+
* path names the envelope's scope, the stored grantId, and (when given) the
|
|
227
|
+
* server origin. Throws WriterAttributionVerificationError with the reason.
|
|
228
|
+
*/
|
|
229
|
+
export async function verifyStoredWriterAttribution(envelope, options = {}) {
|
|
230
|
+
const data = envelope.data;
|
|
231
|
+
const attribution = data[WRITER_ATTRIBUTION_KEY];
|
|
232
|
+
if (!isWriterAttribution(attribution)) {
|
|
233
|
+
throw new WriterAttributionVerificationError("ATTRIBUTION_MISSING", `Record carries no ${WRITER_ATTRIBUTION_KEY} attribution`);
|
|
234
|
+
}
|
|
235
|
+
const { [WRITER_ATTRIBUTION_KEY]: _attribution, ...payloadData } = data;
|
|
236
|
+
const bodyHash = computeBodyHash(new TextEncoder().encode(JSON.stringify(payloadData)));
|
|
237
|
+
if (bodyHash !== attribution.bodyHash) {
|
|
238
|
+
throw new WriterAttributionVerificationError("BODY_HASH_MISMATCH", "Stored record does not hash to the attribution bodyHash");
|
|
239
|
+
}
|
|
240
|
+
let proof;
|
|
241
|
+
let signer;
|
|
242
|
+
try {
|
|
243
|
+
proof = parseWeb3SignedHeader(`${WEB3_SIGNED_SCHEME} ${attribution.signature}`);
|
|
244
|
+
signer = await recoverMessageAddress({
|
|
245
|
+
message: proof.payloadBase64,
|
|
246
|
+
signature: proof.signature,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
catch (err) {
|
|
250
|
+
throw new WriterAttributionVerificationError("PROOF_INVALID", err instanceof Error ? err.message : String(err));
|
|
251
|
+
}
|
|
252
|
+
if (proof.payload.bodyHash !== bodyHash) {
|
|
253
|
+
throw new WriterAttributionVerificationError("BODY_HASH_MISMATCH", "Stored proof does not commit to the attribution bodyHash");
|
|
254
|
+
}
|
|
255
|
+
if (signer.toLowerCase() !== attribution.builder.toLowerCase()) {
|
|
256
|
+
throw new WriterAttributionVerificationError("SIGNER_MISMATCH", "Stored proof is not signed by the attributed builder");
|
|
257
|
+
}
|
|
258
|
+
// Context binding: the signed claims must describe a write of THIS record.
|
|
259
|
+
if (proof.payload.method !== "POST" ||
|
|
260
|
+
signedScopeOf(proof.payload.uri) !== envelope.scope) {
|
|
261
|
+
throw new WriterAttributionVerificationError("SCOPE_MISMATCH", `Stored proof is not a write to scope ${envelope.scope}`);
|
|
262
|
+
}
|
|
263
|
+
if (proof.payload.grantId !== attribution.grantId) {
|
|
264
|
+
throw new WriterAttributionVerificationError("GRANT_MISMATCH", "Stored grantId is not the grant the builder signed");
|
|
265
|
+
}
|
|
266
|
+
if (options.expectedOrigin !== undefined &&
|
|
267
|
+
proof.payload.aud !== options.expectedOrigin) {
|
|
268
|
+
throw new WriterAttributionVerificationError("AUDIENCE_MISMATCH", "Stored proof was not addressed to this server");
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
builder: attribution.builder,
|
|
272
|
+
grantId: attribution.grantId,
|
|
273
|
+
bodyHash,
|
|
274
|
+
payload: proof.payload,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
//# sourceMappingURL=attribution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attribution.js","sourceRoot":"","sources":["../../src/write/attribution.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAEH,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,gBAAgB,GAEjB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,qBAAqB,EAAE,MAAM,MAAM,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,mBAAmB,IAAI,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE9E,OAAO,EACL,cAAc,EACd,cAAc,EACd,uBAAuB,EACvB,iBAAiB,EACjB,uBAAuB,EACvB,mBAAmB,EACnB,SAAS,GACV,MAAM,wBAAwB,CAAC;AAEhC,6EAA6E;AAC7E,MAAM,CAAC,MAAM,sBAAsB,GAAG,wBAAwB,CAAC;AAE/D;;;GAGG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,YAAqB,CAAC;AAgD5D,SAAS,aAAa,CAAC,MAA+B;IACpD,OAAO,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;AAC1D,CAAC;AAaD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,KAAkC;IAElC,MAAM,IAAI,GAAG,uBAAuB,CAAC;QACnC,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,QAAQ,EAAE,uBAAuB,CAAC,KAAK,CAAC,WAAW,CAAC;QACpD,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,WAAW,EAAE,MAAM,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC;QACzC,QAAQ,EAAE,mBAAmB,CAA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,EACnB,+CAA+C,CAChD,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,3 @@
|
|
|
1
|
+
export { createInMemoryWriteProofReplayStore, createInMemoryWriteSessionStore, createWriteSession, hashWriteSessionToken, type CreateWriteSessionInput, type CreateWriteSessionOptions, type CreateWriteSessionResult, type WriteProofReplayStore, type WriteSessionRecord, type WriteSessionStore, } from "./session.js";
|
|
2
|
+
export { WRITE_SIGNATURE_HEADER, WRITER_ATTRIBUTION_KEY, binaryWriteSignedBytes, hasReservedWriterKey, stampWriterAttribution, verifyStoredWriterAttribution, verifyWriterAttribution, WriterAttributionVerificationError, type BinaryWriteSignedBytesInput, type StoredWriterAttributionVerification, type VerifiedWriterAttribution, type VerifyStoredWriterAttributionOptions, type VerifyWriterAttributionInput, type WriterAttribution, } from "./attribution.js";
|
|
3
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/write/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mCAAmC,EACnC,+BAA+B,EAC/B,kBAAkB,EAClB,qBAAqB,EACrB,KAAK,uBAAuB,EAC5B,KAAK,yBAAyB,EAC9B,KAAK,wBAAwB,EAC7B,KAAK,qBAAqB,EAC1B,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,GACvB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,sBAAsB,EACtB,oBAAoB,EACpB,sBAAsB,EACtB,6BAA6B,EAC7B,uBAAuB,EACvB,kCAAkC,EAClC,KAAK,2BAA2B,EAChC,KAAK,mCAAmC,EACxC,KAAK,yBAAyB,EAC9B,KAAK,oCAAoC,EACzC,KAAK,4BAA4B,EACjC,KAAK,iBAAiB,GACvB,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { createInMemoryWriteProofReplayStore, createInMemoryWriteSessionStore, createWriteSession, hashWriteSessionToken, } from "./session.js";
|
|
2
|
+
export { WRITE_SIGNATURE_HEADER, WRITER_ATTRIBUTION_KEY, binaryWriteSignedBytes, hasReservedWriterKey, stampWriterAttribution, verifyStoredWriterAttribution, verifyWriterAttribution, WriterAttributionVerificationError, } from "./attribution.js";
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/write/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mCAAmC,EACnC,+BAA+B,EAC/B,kBAAkB,EAClB,qBAAqB,GAOtB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,sBAAsB,EACtB,oBAAoB,EACpB,sBAAsB,EACtB,6BAA6B,EAC7B,uBAAuB,EACvB,kCAAkC,GAOnC,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write API session (server-signed delegated writes, v1).
|
|
3
|
+
*
|
|
4
|
+
* A registered builder that holds its OWN key and a WRITE-grant proves control
|
|
5
|
+
* of that key ONCE — a Web3Signed handshake to `POST /v1/write/session` — and
|
|
6
|
+
* the PS mints a short-lived bearer token bound to `{ builderAddress, grantId }`
|
|
7
|
+
* (the grant's `write:`-prefixed scope entries define what it may write).
|
|
8
|
+
* Writes then present that token on the EXISTING ingest endpoint
|
|
9
|
+
* (`POST /v1/data/:scope`); each write authorizes as the builder via
|
|
10
|
+
* `verifyDataWritePolicy` and the PS ingests / encrypts / uploads / registers
|
|
11
|
+
* through the normal owner path — the PS signs AddData as the owner exactly as
|
|
12
|
+
* it does today, and the builder never holds the owner key.
|
|
13
|
+
*
|
|
14
|
+
* Deliberately the same handshake shape as the self-signing MCP session
|
|
15
|
+
* (mcp/session.ts): prove key control once, short-lived bearer after. The
|
|
16
|
+
* session token is authorization only; per-write builder ATTRIBUTION is a
|
|
17
|
+
* separate signed proof (see ./attribution.ts).
|
|
18
|
+
*/
|
|
19
|
+
import { hashConnectionToken } from "../mcp/connection-api.js";
|
|
20
|
+
import { createInMemoryMcpProofReplayStore, type McpProofReplayStore } from "../mcp/session.js";
|
|
21
|
+
import type { AuthSessionVerifierPort, GrantVerifierPort } from "../ports/index.js";
|
|
22
|
+
export type WriteProofReplayStore = McpProofReplayStore;
|
|
23
|
+
export declare const createInMemoryWriteProofReplayStore: typeof createInMemoryMcpProofReplayStore;
|
|
24
|
+
export declare const hashWriteSessionToken: typeof hashConnectionToken;
|
|
25
|
+
export interface WriteSessionRecord {
|
|
26
|
+
/** SHA-256 hex of the raw session token. Only the hash is stored. */
|
|
27
|
+
tokenHash: string;
|
|
28
|
+
/** The builder's address (recovered from the handshake proof). */
|
|
29
|
+
builderAddress: `0x${string}`;
|
|
30
|
+
/** The write-grant id the builder was issued (grantee == builder). */
|
|
31
|
+
grantId: string;
|
|
32
|
+
/** The grant's write patterns at handshake time (prefix stripped). */
|
|
33
|
+
writeScopes: string[];
|
|
34
|
+
createdAt: string;
|
|
35
|
+
expiresAtMs: number;
|
|
36
|
+
}
|
|
37
|
+
export interface WriteSessionStore {
|
|
38
|
+
create(record: WriteSessionRecord): Promise<void>;
|
|
39
|
+
/** Returns a live (non-expired) session, or null. */
|
|
40
|
+
getByTokenHash(tokenHash: string): Promise<WriteSessionRecord | null>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* KNOWN LIMITATION: in-memory only, same trade-off as the MCP session store —
|
|
44
|
+
* a process restart drops live tokens and the builder must re-handshake.
|
|
45
|
+
* Acceptable for today's single-instance Personal Server; persist via the
|
|
46
|
+
* host's state store before multi-instance.
|
|
47
|
+
*/
|
|
48
|
+
export declare function createInMemoryWriteSessionStore(): WriteSessionStore;
|
|
49
|
+
export interface CreateWriteSessionInput {
|
|
50
|
+
builderAddress: `0x${string}`;
|
|
51
|
+
grantId: string;
|
|
52
|
+
/**
|
|
53
|
+
* Handshake-proof replay guard. When supplied together with a `replayStore`,
|
|
54
|
+
* a proof id already seen (and still live) is rejected instead of minting a
|
|
55
|
+
* fresh token. `expiresAtMs` bounds how long the id is remembered (the
|
|
56
|
+
* proof's own expiry).
|
|
57
|
+
*/
|
|
58
|
+
proof?: {
|
|
59
|
+
id: string;
|
|
60
|
+
expiresAtMs: number;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export interface CreateWriteSessionOptions {
|
|
64
|
+
store: WriteSessionStore;
|
|
65
|
+
authSessionVerifier: AuthSessionVerifierPort;
|
|
66
|
+
grantVerifier: GrantVerifierPort;
|
|
67
|
+
/**
|
|
68
|
+
* This server's owner address. The grant's grantor MUST equal it — mirrors
|
|
69
|
+
* `verifyDataWritePolicy`'s ownership binding, applied at handshake so a
|
|
70
|
+
* wrong-owner grant fails with a clear error instead of minting a token
|
|
71
|
+
* whose every write would 403.
|
|
72
|
+
*/
|
|
73
|
+
serverOwner: `0x${string}`;
|
|
74
|
+
randomToken: () => string;
|
|
75
|
+
ttlMs?: number;
|
|
76
|
+
now?: () => number;
|
|
77
|
+
/** Optional replay guard for the handshake proof (see `input.proof`). */
|
|
78
|
+
replayStore?: WriteProofReplayStore;
|
|
79
|
+
}
|
|
80
|
+
export interface CreateWriteSessionResult {
|
|
81
|
+
accessToken: string;
|
|
82
|
+
expiresInSeconds: number;
|
|
83
|
+
grantId: string;
|
|
84
|
+
/** Write patterns (prefix stripped) the session may write into. */
|
|
85
|
+
writeScopes: string[];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Validate the handshake identity and mint a write-session token. Same
|
|
89
|
+
* division of labor as createMcpSession: the handshake does the minimal
|
|
90
|
+
* checks that give a clear error (builder registered, grant exists + not
|
|
91
|
+
* revoked + carries write scopes, grantee == builder, grantor == owner);
|
|
92
|
+
* per-scope / expiry enforcement stays authoritative at write time, where
|
|
93
|
+
* `verifyDataWritePolicy` runs against the live grant on every POST.
|
|
94
|
+
*/
|
|
95
|
+
export declare function createWriteSession(input: CreateWriteSessionInput, options: CreateWriteSessionOptions): Promise<CreateWriteSessionResult>;
|
|
96
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/write/session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EACL,iCAAiC,EACjC,KAAK,mBAAmB,EACzB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EACV,uBAAuB,EACvB,iBAAiB,EAClB,MAAM,mBAAmB,CAAC;AAc3B,MAAM,MAAM,qBAAqB,GAAG,mBAAmB,CAAC;AACxD,eAAO,MAAM,mCAAmC,0CACb,CAAC;AACpC,eAAO,MAAM,qBAAqB,4BAAsB,CAAC;AAEzD,MAAM,WAAW,kBAAkB;IACjC,qEAAqE;IACrE,SAAS,EAAE,MAAM,CAAC;IAClB,kEAAkE;IAClE,cAAc,EAAE,KAAK,MAAM,EAAE,CAAC;IAC9B,sEAAsE;IACtE,OAAO,EAAE,MAAM,CAAC;IAChB,sEAAsE;IACtE,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAClD,qDAAqD;IACrD,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAAC;CACvE;AAED;;;;;GAKG;AACH,wBAAg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