@oxy.so/protocol 1.0.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.
Files changed (122) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +16 -0
  3. package/dist/cjs/.tsbuildinfo +1 -0
  4. package/dist/cjs/chain/continuity.js +54 -0
  5. package/dist/cjs/chain/engine.js +34 -0
  6. package/dist/cjs/chain/recordStore.js +25 -0
  7. package/dist/cjs/chain/types.js +22 -0
  8. package/dist/cjs/chain/verify.js +82 -0
  9. package/dist/cjs/envelope/canonicalJson.js +107 -0
  10. package/dist/cjs/envelope/recordId.js +60 -0
  11. package/dist/cjs/envelope/sign.js +75 -0
  12. package/dist/cjs/envelope/signingInput.js +32 -0
  13. package/dist/cjs/identity/resolver.js +50 -0
  14. package/dist/cjs/index.js +71 -0
  15. package/dist/cjs/node/constants.js +85 -0
  16. package/dist/cjs/node/didWebResolver.js +126 -0
  17. package/dist/cjs/node/httpFetch.js +61 -0
  18. package/dist/cjs/node/index.js +71 -0
  19. package/dist/cjs/node/nodeApp.js +344 -0
  20. package/dist/cjs/node/nodeClient.js +204 -0
  21. package/dist/cjs/node/rateLimit.js +187 -0
  22. package/dist/cjs/node/verifyRecord.js +51 -0
  23. package/dist/cjs/platform/crypto.js +186 -0
  24. package/dist/cjs/platform/crypto.native.js +204 -0
  25. package/dist/cjs/platform/expoTypes.js +24 -0
  26. package/dist/cjs/platform/platform.js +33 -0
  27. package/dist/cjs/secp256k1.js +148 -0
  28. package/dist/cjs/transparency/checkpoint.js +79 -0
  29. package/dist/cjs/transparency/tree.js +197 -0
  30. package/dist/esm/.tsbuildinfo +1 -0
  31. package/dist/esm/chain/continuity.js +51 -0
  32. package/dist/esm/chain/engine.js +31 -0
  33. package/dist/esm/chain/recordStore.js +24 -0
  34. package/dist/esm/chain/types.js +19 -0
  35. package/dist/esm/chain/verify.js +78 -0
  36. package/dist/esm/envelope/canonicalJson.js +104 -0
  37. package/dist/esm/envelope/recordId.js +56 -0
  38. package/dist/esm/envelope/sign.js +69 -0
  39. package/dist/esm/envelope/signingInput.js +29 -0
  40. package/dist/esm/identity/resolver.js +47 -0
  41. package/dist/esm/index.js +36 -0
  42. package/dist/esm/node/constants.js +82 -0
  43. package/dist/esm/node/didWebResolver.js +122 -0
  44. package/dist/esm/node/httpFetch.js +55 -0
  45. package/dist/esm/node/index.js +28 -0
  46. package/dist/esm/node/nodeApp.js +336 -0
  47. package/dist/esm/node/nodeClient.js +198 -0
  48. package/dist/esm/node/rateLimit.js +182 -0
  49. package/dist/esm/node/verifyRecord.js +48 -0
  50. package/dist/esm/platform/crypto.js +145 -0
  51. package/dist/esm/platform/crypto.native.js +196 -0
  52. package/dist/esm/platform/expoTypes.js +23 -0
  53. package/dist/esm/platform/platform.js +29 -0
  54. package/dist/esm/secp256k1.js +137 -0
  55. package/dist/esm/transparency/checkpoint.js +73 -0
  56. package/dist/esm/transparency/tree.js +189 -0
  57. package/dist/types/.tsbuildinfo +1 -0
  58. package/dist/types/chain/continuity.d.ts +28 -0
  59. package/dist/types/chain/engine.d.ts +27 -0
  60. package/dist/types/chain/recordStore.d.ts +85 -0
  61. package/dist/types/chain/types.d.ts +79 -0
  62. package/dist/types/chain/verify.d.ts +45 -0
  63. package/dist/types/envelope/canonicalJson.d.ts +44 -0
  64. package/dist/types/envelope/recordId.d.ts +30 -0
  65. package/dist/types/envelope/sign.d.ts +47 -0
  66. package/dist/types/envelope/signingInput.d.ts +33 -0
  67. package/dist/types/identity/resolver.d.ts +67 -0
  68. package/dist/types/index.d.ts +32 -0
  69. package/dist/types/node/constants.d.ts +80 -0
  70. package/dist/types/node/didWebResolver.d.ts +47 -0
  71. package/dist/types/node/httpFetch.d.ts +60 -0
  72. package/dist/types/node/index.d.ts +28 -0
  73. package/dist/types/node/nodeApp.d.ts +120 -0
  74. package/dist/types/node/nodeClient.d.ts +135 -0
  75. package/dist/types/node/rateLimit.d.ts +95 -0
  76. package/dist/types/node/verifyRecord.d.ts +41 -0
  77. package/dist/types/platform/crypto.d.ts +93 -0
  78. package/dist/types/platform/crypto.native.d.ts +77 -0
  79. package/dist/types/platform/expoTypes.d.ts +99 -0
  80. package/dist/types/platform/platform.d.ts +25 -0
  81. package/dist/types/secp256k1.d.ts +45 -0
  82. package/dist/types/transparency/checkpoint.d.ts +71 -0
  83. package/dist/types/transparency/tree.d.ts +135 -0
  84. package/package.json +157 -0
  85. package/src/__tests__/canonicalJson.test.ts +116 -0
  86. package/src/__tests__/chain.test.ts +279 -0
  87. package/src/__tests__/didWebResolver.test.ts +132 -0
  88. package/src/__tests__/envelope.test.ts +267 -0
  89. package/src/__tests__/nodeApp.test.ts +410 -0
  90. package/src/__tests__/nodeClient.test.ts +177 -0
  91. package/src/__tests__/nodeHarness.ts +151 -0
  92. package/src/__tests__/optionalNativePeers.test.ts +233 -0
  93. package/src/__tests__/rateLimit.test.ts +268 -0
  94. package/src/__tests__/runnerGuard.test.ts +85 -0
  95. package/src/__tests__/secp256k1.test.ts +118 -0
  96. package/src/__tests__/transparency.test.ts +353 -0
  97. package/src/chain/continuity.ts +59 -0
  98. package/src/chain/engine.ts +43 -0
  99. package/src/chain/recordStore.ts +98 -0
  100. package/src/chain/types.ts +85 -0
  101. package/src/chain/verify.ts +102 -0
  102. package/src/envelope/canonicalJson.ts +120 -0
  103. package/src/envelope/recordId.ts +63 -0
  104. package/src/envelope/sign.ts +86 -0
  105. package/src/envelope/signingInput.ts +48 -0
  106. package/src/identity/resolver.ts +90 -0
  107. package/src/index.ts +101 -0
  108. package/src/node/constants.ts +105 -0
  109. package/src/node/didWebResolver.ts +162 -0
  110. package/src/node/httpFetch.ts +88 -0
  111. package/src/node/index.ts +87 -0
  112. package/src/node/nodeApp.ts +471 -0
  113. package/src/node/nodeClient.ts +322 -0
  114. package/src/node/rateLimit.ts +233 -0
  115. package/src/node/verifyRecord.ts +60 -0
  116. package/src/platform/crypto.native.ts +251 -0
  117. package/src/platform/crypto.ts +172 -0
  118. package/src/platform/expoTypes.ts +99 -0
  119. package/src/platform/platform.ts +31 -0
  120. package/src/secp256k1.ts +207 -0
  121. package/src/transparency/checkpoint.ts +109 -0
  122. package/src/transparency/tree.ts +258 -0
@@ -0,0 +1,198 @@
1
+ /**
2
+ * `NodeClient` — the HTTP client that drives an Oxy-protocol data node's
3
+ * routes (head / log / records / blobs). It is the OUTBOUND half of the node
4
+ * protocol: oxy-api uses it to PULL a user's chain back from their node; a
5
+ * future Mention backend (B3) uses it to drive a node + push records/blobs.
6
+ *
7
+ * The client is transport-agnostic — it takes an injected {@link NodeFetch} so
8
+ * the protocol package never depends on `@oxy.so/core`. Oxy supplies an adapter
9
+ * over `@oxy.so/core/server`'s `safeFetch` (HTTPS-only, DNS-pinned, private-IP
10
+ * denylist, bounded redirects); a test supplies an in-process stub. Every
11
+ * response body is read with a hard byte ceiling, so a node cannot stream an
12
+ * unbounded body into the caller.
13
+ */
14
+ import { readBoundedBytes, readBoundedJson, } from './httpFetch.js';
15
+ import { DEFAULT_CLIENT_MAX_REDIRECTS, DEFAULT_CLIENT_TIMEOUT_MS, DEFAULT_HEAD_MAX_BYTES, DEFAULT_LOG_MAX_BYTES, DEFAULT_MAX_BLOB_BYTES, DEFAULT_WRITE_RESPONSE_MAX_BYTES, NODE_BLOBS_PATH, NODE_HEAD_PATH, NODE_LOG_PATH, NODE_RECORDS_PATH, NODE_SYNC_PUSH_PATH, OWNER_AUTH_HEADERS, } from './constants.js';
16
+ /** A non-2xx node response (or a node that returned a malformed body). */
17
+ export class NodeClientError extends Error {
18
+ constructor(message, status, reason) {
19
+ super(message);
20
+ this.status = status;
21
+ this.reason = reason;
22
+ this.name = 'NodeClientError';
23
+ }
24
+ }
25
+ function readError(body) {
26
+ if (typeof body === 'object' && body !== null && typeof body.error === 'string') {
27
+ return body.error;
28
+ }
29
+ return undefined;
30
+ }
31
+ /**
32
+ * Trim every trailing slash from a base URL in LINEAR time.
33
+ *
34
+ * Replaces an anchored-quantifier regex (`/\/+$/`) whose backtracking is a
35
+ * polynomial-ReDoS sink on a long all-slash input; a single-pass scan is O(n)
36
+ * with no ReDoS surface.
37
+ */
38
+ export function trimTrailingSlashes(value) {
39
+ let end = value.length;
40
+ while (end > 0 && value.charCodeAt(end - 1) === 47 /* '/' */) {
41
+ end -= 1;
42
+ }
43
+ return end === value.length ? value : value.slice(0, end);
44
+ }
45
+ export class NodeClient {
46
+ constructor(options) {
47
+ this.baseUrl = trimTrailingSlashes(options.baseUrl);
48
+ this.fetch = options.fetch;
49
+ this.headersTimeoutMs = options.headersTimeoutMs ?? DEFAULT_CLIENT_TIMEOUT_MS;
50
+ this.maxRedirects = options.maxRedirects ?? DEFAULT_CLIENT_MAX_REDIRECTS;
51
+ this.headMaxBytes = options.headMaxBytes ?? DEFAULT_HEAD_MAX_BYTES;
52
+ this.logMaxBytes = options.logMaxBytes ?? DEFAULT_LOG_MAX_BYTES;
53
+ this.writeResponseMaxBytes = options.writeResponseMaxBytes ?? DEFAULT_WRITE_RESPONSE_MAX_BYTES;
54
+ this.blobMaxBytes = options.blobMaxBytes ?? DEFAULT_MAX_BLOB_BYTES;
55
+ }
56
+ /** Base request options shared by every call (timeout + redirect budget). */
57
+ init(extra) {
58
+ return {
59
+ headersTimeoutMs: this.headersTimeoutMs,
60
+ maxRedirects: this.maxRedirects,
61
+ ...extra,
62
+ };
63
+ }
64
+ /** The node's current chain head. Throws {@link NodeClientError} on a non-2xx. */
65
+ async head() {
66
+ const res = await this.fetch(`${this.baseUrl}${NODE_HEAD_PATH}`, this.init({ method: 'GET' }));
67
+ if (res.status < 200 || res.status >= 300) {
68
+ res.destroy();
69
+ throw new NodeClientError(`node ${NODE_HEAD_PATH} responded HTTP ${res.status}`, res.status);
70
+ }
71
+ const body = await readBoundedJson(res, this.headMaxBytes);
72
+ const obj = (typeof body === 'object' && body !== null ? body : {});
73
+ return {
74
+ seq: typeof obj.seq === 'number' ? obj.seq : null,
75
+ headRecordId: typeof obj.headRecordId === 'string' ? obj.headRecordId : null,
76
+ recordCount: typeof obj.recordCount === 'number' ? obj.recordCount : 0,
77
+ };
78
+ }
79
+ /**
80
+ * One ordered page of the node's log strictly after `sinceSeq` (pass `-1` from
81
+ * genesis), capped at `limit`. Throws {@link NodeClientError} on a non-2xx or a
82
+ * response missing the `records` array.
83
+ */
84
+ async log(sinceSeq, limit) {
85
+ // A genesis cursor (`sinceSeq < 0`) is expressed by OMITTING `since` — the
86
+ // node reads an absent cursor as "from genesis". A negative numeric `since`
87
+ // is not a valid cursor on the wire (only an absent one, a non-negative seq,
88
+ // or a recordId), so omitting it is the correct way to request the whole log.
89
+ const sinceParam = sinceSeq >= 0 ? `since=${encodeURIComponent(String(sinceSeq))}&` : '';
90
+ const url = `${this.baseUrl}${NODE_LOG_PATH}?${sinceParam}limit=${encodeURIComponent(String(limit))}`;
91
+ const res = await this.fetch(url, this.init({ method: 'GET' }));
92
+ if (res.status < 200 || res.status >= 300) {
93
+ res.destroy();
94
+ throw new NodeClientError(`node ${NODE_LOG_PATH} responded HTTP ${res.status}`, res.status);
95
+ }
96
+ const body = await readBoundedJson(res, this.logMaxBytes);
97
+ const records = body.records;
98
+ if (!Array.isArray(records)) {
99
+ throw new NodeClientError(`node ${NODE_LOG_PATH} returned no records array`, res.status);
100
+ }
101
+ const headRaw = body.head;
102
+ const head = typeof headRaw === 'object' &&
103
+ headRaw !== null &&
104
+ typeof headRaw.seq === 'number' &&
105
+ typeof headRaw.headRecordId === 'string'
106
+ ? { seq: headRaw.seq, headRecordId: headRaw.headRecordId }
107
+ : null;
108
+ return { records, count: records.length, head };
109
+ }
110
+ /**
111
+ * Write a single owner-signed envelope (`POST /records`). Throws
112
+ * {@link NodeClientError} (carrying the node's `reason`) on any non-2xx — a
113
+ * chain rejection (`chain_gap`/`chain_fork`/`bad_seq`/`chain_conflict`) or an
114
+ * authorization failure.
115
+ */
116
+ async writeRecord(envelope) {
117
+ const res = await this.fetch(`${this.baseUrl}${NODE_RECORDS_PATH}`, this.init({
118
+ method: 'POST',
119
+ headers: { 'Content-Type': 'application/json' },
120
+ body: Buffer.from(JSON.stringify(envelope), 'utf8'),
121
+ }));
122
+ const body = await readBoundedJson(res, this.writeResponseMaxBytes);
123
+ if (res.status < 200 || res.status >= 300) {
124
+ const reason = readError(body);
125
+ throw new NodeClientError(`node ${NODE_RECORDS_PATH} responded HTTP ${res.status}${reason ? ` (${reason})` : ''}`, res.status, reason);
126
+ }
127
+ const obj = body;
128
+ if (typeof obj.recordId !== 'string' || typeof obj.seq !== 'number') {
129
+ throw new NodeClientError(`node ${NODE_RECORDS_PATH} returned a malformed write result`, res.status);
130
+ }
131
+ return { recordId: obj.recordId, seq: obj.seq };
132
+ }
133
+ /**
134
+ * Push a batch of owner-signed envelopes (`POST /sync/push`). Returns the
135
+ * node's per-item results. Throws {@link NodeClientError} only on a non-2xx
136
+ * batch-level failure (`invalid_batch` / `batch_too_large`).
137
+ */
138
+ async pushRecords(envelopes) {
139
+ const res = await this.fetch(`${this.baseUrl}${NODE_SYNC_PUSH_PATH}`, this.init({
140
+ method: 'POST',
141
+ headers: { 'Content-Type': 'application/json' },
142
+ body: Buffer.from(JSON.stringify({ records: envelopes }), 'utf8'),
143
+ }));
144
+ const body = await readBoundedJson(res, this.writeResponseMaxBytes);
145
+ if (res.status < 200 || res.status >= 300) {
146
+ const reason = readError(body);
147
+ throw new NodeClientError(`node ${NODE_SYNC_PUSH_PATH} responded HTTP ${res.status}${reason ? ` (${reason})` : ''}`, res.status, reason);
148
+ }
149
+ const obj = body;
150
+ return {
151
+ accepted: typeof obj.accepted === 'number' ? obj.accepted : 0,
152
+ results: Array.isArray(obj.results)
153
+ ? obj.results
154
+ : [],
155
+ };
156
+ }
157
+ /** Fetch a content-addressed blob. Returns `null` on a 404; throws on other non-2xx. */
158
+ async getBlob(hash) {
159
+ const res = await this.fetch(`${this.baseUrl}${NODE_BLOBS_PATH}/${encodeURIComponent(hash)}`, this.init({ method: 'GET' }));
160
+ if (res.status === 404) {
161
+ res.destroy();
162
+ return null;
163
+ }
164
+ if (res.status < 200 || res.status >= 300) {
165
+ res.destroy();
166
+ throw new NodeClientError(`node ${NODE_BLOBS_PATH}/:hash responded HTTP ${res.status}`, res.status);
167
+ }
168
+ return readBoundedBytes(res, this.blobMaxBytes);
169
+ }
170
+ /**
171
+ * Pin a content-addressed blob with an owner-signed authorization
172
+ * (`PUT /blobs/:hash`). The caller signs the pin (it holds the owner key) and
173
+ * passes the resulting `{ publicKey, signature, timestamp }`; the client sets
174
+ * the owner-auth headers. Throws {@link NodeClientError} on a non-2xx.
175
+ */
176
+ async putBlob(hash, bytes, auth) {
177
+ const res = await this.fetch(`${this.baseUrl}${NODE_BLOBS_PATH}/${encodeURIComponent(hash)}`, this.init({
178
+ method: 'PUT',
179
+ headers: {
180
+ 'Content-Type': 'application/octet-stream',
181
+ [OWNER_AUTH_HEADERS.publicKey]: auth.publicKey,
182
+ [OWNER_AUTH_HEADERS.signature]: auth.signature,
183
+ [OWNER_AUTH_HEADERS.timestamp]: String(auth.timestamp),
184
+ },
185
+ body: bytes,
186
+ }));
187
+ const body = await readBoundedJson(res, this.writeResponseMaxBytes);
188
+ if (res.status < 200 || res.status >= 300) {
189
+ const reason = readError(body);
190
+ throw new NodeClientError(`node ${NODE_BLOBS_PATH}/:hash responded HTTP ${res.status}${reason ? ` (${reason})` : ''}`, res.status, reason);
191
+ }
192
+ const obj = body;
193
+ if (typeof obj.hash !== 'string' || typeof obj.size !== 'number') {
194
+ throw new NodeClientError(`node ${NODE_BLOBS_PATH}/:hash returned a malformed pin result`, res.status);
195
+ }
196
+ return { hash: obj.hash, size: obj.size };
197
+ }
198
+ }
@@ -0,0 +1,182 @@
1
+ /**
2
+ * A small, dependency-free fixed-window per-IP rate limiter for the node app's
3
+ * owner-authorized write routes.
4
+ *
5
+ * The node is a single-writer model (only the owner key may write), so the
6
+ * limiter is a defence-in-depth budget on the unauthenticated edge — it caps the
7
+ * request rate BEFORE signature verification so a flood of bogus envelopes can't
8
+ * pin CPU on crypto. It is intentionally process-local (a single node serves one
9
+ * owner's repo); there is no shared store to coordinate.
10
+ *
11
+ * Fixed-window counting, one budget per client: each key gets `max` requests per
12
+ * `windowMs`, and the window resets lazily on the first request after it elapses.
13
+ * The key is a SALTED HASH of the client address, never the address itself — see
14
+ * {@link clientRateLimitKey}.
15
+ *
16
+ * Bounded memory (defence against a key-rotation DoS — spoofed IPs / many DIDs
17
+ * growing the map without limit → memory exhaustion):
18
+ * - An ACTIVE periodic sweep on an `unref()`'d interval deletes every entry
19
+ * whose window has fully elapsed, so keys that are never touched again do not
20
+ * leak forever (lazy expiry-on-access alone cannot reclaim them). The
21
+ * interval is `unref()`'d so it never keeps the node process alive, and
22
+ * {@link RateLimiter.stop} clears it for a clean lifecycle teardown.
23
+ * - A hard cap on the number of tracked keys ({@link RateLimitConfig.maxEntries})
24
+ * evicts the OLDEST window (insertion-order LRU) when exceeded — a synchronous
25
+ * backstop against a burst that arrives between sweeps.
26
+ */
27
+ import { createHmac, randomBytes } from 'node:crypto';
28
+ // The package's `lib` includes `DOM` (the isomorphic root code uses Web Crypto),
29
+ // so the ambient `setInterval` overload TypeScript picks for a bare call is the
30
+ // browser one returning `number` — which has no `.unref()`. This `node/` subpath
31
+ // is Node-only; reach the Node timer globals through their `@types/node`
32
+ // signatures so the handle is correctly `NodeJS.Timeout` (no cast, no shadowing).
33
+ // Resolved at call time (not captured at module load) so test fake-timers that
34
+ // swap the globals still drive the sweep.
35
+ function nodeSetInterval(handler, ms) {
36
+ const set = globalThis.setInterval;
37
+ return set(handler, ms);
38
+ }
39
+ function nodeClearInterval(timer) {
40
+ const clear = globalThis.clearInterval;
41
+ clear(timer);
42
+ }
43
+ /**
44
+ * The HMAC key under which client addresses are hashed into rate-limit keys.
45
+ * 256 CSPRNG bits, minted once when this module is first loaded and held only in
46
+ * memory — never read from a config, never written anywhere, never sent.
47
+ *
48
+ * ## Why the salt is deliberately EPHEMERAL, and what a stable one would cost
49
+ *
50
+ * A rate-limit window is short-lived (`windowMs`, seconds to a minute) and this
51
+ * limiter is process-local by design — a node serves one owner's repo and there
52
+ * is no shared store to coordinate. So nothing here needs a key to mean the same
53
+ * thing after a restart, or to mean the same thing on another node. That makes a
54
+ * per-process salt not merely sufficient but BETTER than a configured one:
55
+ *
56
+ * - there is no value to distribute, so there is nothing for a node operator to
57
+ * get wrong, nothing to rotate, and nothing to leak from an env file, a
58
+ * process listing or a container image;
59
+ * - the mapping dies with the process, so the same address hashes to a
60
+ * different key after every restart and the keys correlate to nothing once
61
+ * the process exits.
62
+ *
63
+ * A stable salt (an env var, a file) would buy exactly one thing this limiter
64
+ * does not want — a client identifier that survives a restart and can be compared
65
+ * across nodes — in exchange for a config burden and a secret at rest. That is
66
+ * the trade, and it is why this is not configurable.
67
+ *
68
+ * ## What the hash does and does not buy, stated honestly
69
+ *
70
+ * It removes the raw address from the process's data structures: the limiter's
71
+ * Map holds digests, so an address is no longer sitting in memory as a key for
72
+ * the lifetime of a window, and nothing downstream can casually read one back
73
+ * out. What it does NOT claim is secrecy against an attacker who already has the
74
+ * live process — the salt is in the same heap, and with it the IPv4 space is
75
+ * enumerable. That is the general reason hashing is not an acceptable AT-REST
76
+ * form for an address anywhere in Oxy; this value is never at rest.
77
+ */
78
+ const CLIENT_KEY_SALT = randomBytes(32);
79
+ /**
80
+ * The rate-limit key for a request: a salted hash of the client address, or the
81
+ * `'unknown'` sentinel when Express resolved no address at all (a request whose
82
+ * address is unknown cannot be budgeted individually, so all of them share one
83
+ * bucket — the same behaviour this limiter has always had).
84
+ *
85
+ * Truncated to 96 bits, which keeps a tracked entry to a short string beside its
86
+ * two numbers (the memory-bounding rationale on {@link RateLimitConfig.maxEntries}
87
+ * assumes exactly that). At the 10 000-entry cap a collision — two clients
88
+ * sharing one budget — has probability around 10⁸/2⁹⁷, i.e. never.
89
+ *
90
+ * Residue, named rather than left implicit: the address is hashed VERBATIM, so an
91
+ * IPv6 client that rotates through its /64 still mints a fresh key per address,
92
+ * exactly as it did before this was hashed. oxy-api's `hashedIpKey` buckets IPv6
93
+ * to /56 first to close that; doing the same here is a rate-limiting change with
94
+ * its own reasoning (it makes a whole prefix share one budget) and is deliberately
95
+ * not folded into a privacy fix.
96
+ *
97
+ * Exported for {@link createRateLimiter}'s own tests, not part of
98
+ * `@oxy.so/protocol/node`'s public surface — it is not re-exported by the barrel.
99
+ */
100
+ export function clientRateLimitKey(req) {
101
+ const ip = req.ip;
102
+ if (!ip) {
103
+ return 'unknown';
104
+ }
105
+ // Single-purpose salt: it derives this key and nothing else, so there is no
106
+ // second derivation to namespace against (oxy-api's `hashedIpKey` prefixes
107
+ // `rl|` because its salt is shared with deviceId derivation). A future second
108
+ // use of this salt would need a namespace, or its own salt.
109
+ return createHmac('sha256', CLIENT_KEY_SALT).update(ip).digest('hex').slice(0, 24);
110
+ }
111
+ /** Default budget for owner write routes (generous — single-writer model). */
112
+ export const DEFAULT_WRITE_RATE_LIMIT = { windowMs: 60000, max: 60 };
113
+ /**
114
+ * Default hard cap on tracked keys. Sized so the map's worst-case footprint
115
+ * stays small (each entry is a short string key + two numbers) while never
116
+ * evicting a legitimately active key for the single-writer node — the owner
117
+ * drives traffic from a handful of IPs, far below this ceiling.
118
+ */
119
+ export const DEFAULT_MAX_RATE_LIMIT_ENTRIES = 10000;
120
+ /**
121
+ * Build an Express middleware enforcing a fixed-window per-client rate limit.
122
+ * Exceeding the budget responds `429 { error: 'rate_limited' }` and does not call
123
+ * `next`. The key is {@link clientRateLimitKey} — a salted hash of Express's
124
+ * resolved client IP, so no address is held in the tracked map.
125
+ *
126
+ * The returned middleware owns a background sweep timer; call {@link RateLimiter.stop}
127
+ * to release it (e.g. on app shutdown).
128
+ */
129
+ export function createRateLimiter(config) {
130
+ const windows = new Map();
131
+ const maxEntries = config.maxEntries ?? DEFAULT_MAX_RATE_LIMIT_ENTRIES;
132
+ // Active sweep: delete every entry whose window has fully elapsed. Running on
133
+ // a timer (rather than only on request arrival) reclaims keys that are never
134
+ // touched again, so a churn of distinct IPs cannot leak memory once traffic
135
+ // for those keys stops. One pass per window is sufficient: an entry lives at
136
+ // most `2 * windowMs` before a sweep removes it.
137
+ function sweepExpired() {
138
+ const now = Date.now();
139
+ for (const [key, counter] of windows) {
140
+ if (now - counter.start >= config.windowMs) {
141
+ windows.delete(key);
142
+ }
143
+ }
144
+ }
145
+ const sweepTimer = nodeSetInterval(sweepExpired, config.windowMs);
146
+ // Never let the sweep keep the node process alive on its own.
147
+ sweepTimer.unref();
148
+ function rateLimit(req, res, next) {
149
+ const now = Date.now();
150
+ const key = clientRateLimitKey(req);
151
+ const counter = windows.get(key);
152
+ if (!counter || now - counter.start >= config.windowMs) {
153
+ // Hard cap backstop: if a burst of distinct keys outran the sweep, evict
154
+ // the oldest-inserted window before admitting a new key. A `Map` preserves
155
+ // insertion order, so its first key is the oldest tracked entry.
156
+ if (!counter && windows.size >= maxEntries) {
157
+ const oldest = windows.keys().next().value;
158
+ if (oldest !== undefined) {
159
+ windows.delete(oldest);
160
+ }
161
+ }
162
+ windows.set(key, { start: now, count: 1 });
163
+ next();
164
+ return;
165
+ }
166
+ if (counter.count >= config.max) {
167
+ const retryAfterSec = Math.ceil((counter.start + config.windowMs - now) / 1000);
168
+ res.setHeader('Retry-After', String(Math.max(retryAfterSec, 1)));
169
+ res.status(429).json({ error: 'rate_limited' });
170
+ return;
171
+ }
172
+ counter.count += 1;
173
+ next();
174
+ }
175
+ // Attach the lifecycle hook to the middleware, yielding the `RateLimiter`
176
+ // callable-with-`stop` without a cast.
177
+ return Object.assign(rateLimit, {
178
+ stop() {
179
+ nodeClearInterval(sweepTimer);
180
+ },
181
+ });
182
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Record verification for a data node — reuses the protocol envelope primitives
3
+ * so a record verifies on the node with the EXACT code Oxy uses. No crypto is
4
+ * re-implemented:
5
+ *
6
+ * - {@link verifyEnvelopeSignature} recomputes the canonical signing input (the
7
+ * bytes the signature covers) from the envelope's own fields and checks the
8
+ * secp256k1 DER signature against the envelope's embedded `publicKey`.
9
+ * - {@link computeRecordId} recomputes `recordId = sha256(signingInput)` — the
10
+ * content address used as the chain's `prev` pointer.
11
+ *
12
+ * The envelope shape is validated with the shared `signedRecordEnvelopeSchema`.
13
+ * A node is a v2 hash chain, so only v2 envelopes (carrying
14
+ * `seq`/`prev`/`collection`/`rkey`) are accepted; v1 singletons have no chain
15
+ * coordinates.
16
+ *
17
+ * Verification here proves the signature is internally consistent with the
18
+ * embedded `publicKey`. Whether that key is authorized for the node is the OWNER
19
+ * check (the injected owner-key authority) — on a node the authority is the
20
+ * configured owner public key, not a DID lookup.
21
+ */
22
+ import { signedRecordEnvelopeSchema } from '@oxy.so/contracts';
23
+ import { computeRecordId } from '../envelope/recordId.js';
24
+ import { verifyEnvelopeSignature } from '../envelope/sign.js';
25
+ /**
26
+ * Validate, signature-check, and content-address a candidate signed record for a
27
+ * v2 hash-chain node.
28
+ *
29
+ * On success the parsed envelope and its `recordId` are returned; the caller
30
+ * (the node app) still enforces owner authority and chain continuity before the
31
+ * record is appended.
32
+ */
33
+ export async function verifyNodeRecordEnvelope(input) {
34
+ const parsed = signedRecordEnvelopeSchema.safeParse(input);
35
+ if (!parsed.success) {
36
+ return { ok: false, reason: 'invalid_envelope' };
37
+ }
38
+ const envelope = parsed.data;
39
+ if (envelope.version !== 2) {
40
+ return { ok: false, reason: 'not_v2' };
41
+ }
42
+ const signatureValid = await verifyEnvelopeSignature(envelope);
43
+ if (!signatureValid) {
44
+ return { ok: false, reason: 'bad_signature' };
45
+ }
46
+ const recordId = await computeRecordId(envelope);
47
+ return { ok: true, envelope, recordId };
48
+ }
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Platform Crypto / Storage — Default Variant (Node.js, Browser, generic bundlers)
3
+ *
4
+ * Provides lazy access to platform-specific crypto and storage modules.
5
+ *
6
+ * # Variants
7
+ *
8
+ * This module ships in two physical variants on disk, selected per consumer
9
+ * by the bundler / runtime:
10
+ *
11
+ * - `crypto.js` — this file. Used by Node.js, Vite, webpack,
12
+ * Rollup, esbuild, and anything that does not match
13
+ * Metro's `*.native.js` source-extension preference.
14
+ * - `crypto.native.js` — sibling file. Picked up automatically by Metro's
15
+ * resolver (which prefers `*.<platform>.js` and
16
+ * `*.native.js` over plain `*.js` when
17
+ * `preferNativePlatform` is true — Expo sets this for
18
+ * all non-web builds).
19
+ *
20
+ * The `package.json#exports` map also declares a `"react-native"` condition
21
+ * pointing at the same `dist/esm/index.js` entry — that entry transitively
22
+ * imports `./platform/crypto`, and Metro's per-file source-extension lookup
23
+ * substitutes the `.native.js` sibling automatically inside `dist/`. The
24
+ * package's top-level `"react-native"` map additionally pins the built
25
+ * `platform/crypto.js` (under both `dist/cjs` and `dist/esm`) to its
26
+ * `crypto.native.js` sibling belt-and-braces. This means consumers never have
27
+ * to add resolver shims; Metro Just Works.
28
+ *
29
+ * Both variants expose the EXACT same public API; importers don't need to know
30
+ * which one they got. The variant difference is purely about which underlying
31
+ * native modules each one references:
32
+ *
33
+ * ┌──────────────────┬───────────────────────┬───────────────────────────────┐
34
+ * │ Function │ Default variant │ React Native variant │
35
+ * ├──────────────────┼───────────────────────┼───────────────────────────────┤
36
+ * │ loadNodeCrypto │ `await import('crypto')` (Node built-in) │
37
+ * │ │ │ throws — Node crypto is not │
38
+ * │ │ │ available on Hermes/RN │
39
+ * ├──────────────────┼───────────────────────┼───────────────────────────────┤
40
+ * │ loadExpoCrypto │ throws — expo-crypto │ optional `require('expo- │
41
+ * │ │ is not part of a │ crypto')` │
42
+ * │ │ Node/Vite bundle │ │
43
+ * ├──────────────────┼───────────────────────┼───────────────────────────────┤
44
+ * │ loadSecureStore │ throws (web/Node have │ optional `require('expo- │
45
+ * │ │ their own storage) │ secure-store')` │
46
+ * ├──────────────────┼───────────────────────┼───────────────────────────────┤
47
+ * │ loadAsyncStorage │ throws (web/Node have │ optional `require('@react- │
48
+ * │ │ their own storage) │ native-async-storage/...')` │
49
+ * ├──────────────────┼───────────────────────┼───────────────────────────────┤
50
+ * │ getRandomBytesRN │ throws (RN-only) │ direct call into expo-crypto │
51
+ * └──────────────────┴───────────────────────┴───────────────────────────────┘
52
+ *
53
+ * Crucially, the default variant references ONLY Node's `'crypto'`. It never
54
+ * mentions `expo-*` or `@react-native-async-storage/*` — so Vite, webpack,
55
+ * esbuild, Rollup, and Node itself can bundle / require it without ever
56
+ * attempting to resolve those RN-only packages.
57
+ *
58
+ * The React Native variant references ONLY the RN packages, each behind
59
+ * Metro's optional-dependency mechanism (a literal `require()` inside a `try`)
60
+ * because they are declared OPTIONAL peer dependencies. It never mentions
61
+ * `'crypto'` — so Metro and Hermes have nothing to choke on.
62
+ *
63
+ * # Why not a single file with dynamic import?
64
+ *
65
+ * A previous iteration used a "bundler-opaque" `new Function('s', 'return
66
+ * import(s)')` trick so a single file could service every platform. It
67
+ * bundled cleanly on Metro but Hermes refused to PARSE the resulting
68
+ * `import()` expression inside a Function-constructor body
69
+ * (`SyntaxError: Invalid expression encountered` at the `(` of `import(`).
70
+ * The platform-extension split is the only approach that lets each runtime
71
+ * see a file containing only specifiers it can understand — no tricks, no
72
+ * runtime parsing risks.
73
+ */
74
+ import { isReactNative } from './platform.js';
75
+ // ---------------------------------------------------------------------------
76
+ // Node `crypto` — Node built-in
77
+ //
78
+ // `await import('crypto')` here is a real, static-from-tsc's-perspective
79
+ // dynamic import. Node ESM, Vite, webpack, and esbuild all resolve it fine.
80
+ // Metro never sees this file because the `.native.js` sibling shadows
81
+ // it, so Metro never tries to resolve `'crypto'`.
82
+ // ---------------------------------------------------------------------------
83
+ let cachedNodeCrypto = null;
84
+ export async function loadNodeCrypto() {
85
+ if (cachedNodeCrypto) {
86
+ return cachedNodeCrypto;
87
+ }
88
+ cachedNodeCrypto = await import('node:crypto');
89
+ return cachedNodeCrypto;
90
+ }
91
+ // ---------------------------------------------------------------------------
92
+ // RN-only modules — never called from this variant.
93
+ //
94
+ // These throw a clear error if anything ever reaches them outside RN. In
95
+ // practice every caller gates with `isReactNative()` before calling, so
96
+ // these are belt-and-braces.
97
+ //
98
+ // Return types use the structural interfaces from expoTypes.ts rather than
99
+ // `typeof import('expo-crypto')` / `typeof import('expo-secure-store')`.
100
+ // This prevents TypeScript from traversing into expo-modules-core under
101
+ // NodeNext module resolution, which would otherwise pollute the global type
102
+ // environment in server/Node consumers (TS2322 on NodeJS.Timeout vs number).
103
+ // ---------------------------------------------------------------------------
104
+ function notReactNativeError(module) {
105
+ return new Error(`[oxy.protocol.crypto] Tried to load '${module}' outside React Native. This module is only available in a React Native runtime; bundling routed this consumer to the default (Node/web) variant. This indicates a missing platform gate (\`isReactNative()\`) in the calling code.`);
106
+ }
107
+ export async function loadExpoCrypto() {
108
+ if (isReactNative()) {
109
+ // Should be unreachable: when running on RN, Metro / the `react-native`
110
+ // exports condition serves the sibling variant. If we got here, the
111
+ // package-exports map is misconfigured for this host. Throw with a
112
+ // helpful diagnostic rather than fall back to a broken dynamic import.
113
+ throw new Error('[oxy.protocol.crypto] React Native runtime resolved the default ' +
114
+ '(non-RN) variant of @oxy.so/protocol/platform/crypto. Check the ' +
115
+ "consumer's bundler resolution — Metro should pick the sibling " +
116
+ '.native.js file via package exports.');
117
+ }
118
+ throw notReactNativeError('expo-crypto');
119
+ }
120
+ export async function loadSecureStore() {
121
+ throw notReactNativeError('expo-secure-store');
122
+ }
123
+ export async function loadAsyncStorage() {
124
+ throw notReactNativeError('@react-native-async-storage/async-storage');
125
+ }
126
+ /**
127
+ * Synchronous random-bytes via `expo-crypto.getRandomBytes`. Only available
128
+ * in the React Native variant. The default variant throws because Node and
129
+ * browsers have their own native CSPRNGs (`crypto.randomBytes` and
130
+ * `crypto.getRandomValues` respectively) — callers should use those.
131
+ */
132
+ export function getRandomBytesRN(_byteCount) {
133
+ throw notReactNativeError('expo-crypto.getRandomBytes (sync)');
134
+ }
135
+ // ---------------------------------------------------------------------------
136
+ // Shared identity bridge — `@oxy.so/expo-oxy-identity` (native-only).
137
+ //
138
+ // The default (web / Node) variant has no cross-app identity channel, so this
139
+ // always resolves to `null`. `@oxy.so/core`'s `KeyManager` treats `null` as "no
140
+ // bridge" and falls back to its package-private store — which is correct on web
141
+ // (there is no shared identity there).
142
+ // ---------------------------------------------------------------------------
143
+ export function loadSharedIdentityBridge() {
144
+ return Promise.resolve(null);
145
+ }