@inversealtruism/csd-client 0.1.15 → 0.1.16

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/index.cjs CHANGED
@@ -33,9 +33,11 @@ var CsdClient = class {
33
33
  timeoutMs;
34
34
  retries;
35
35
  maxBytes;
36
+ parseJson;
36
37
  constructor(opts) {
37
38
  this.base = opts.baseUrl.replace(/\/+$/, "");
38
39
  this.maxBytes = Math.max(1, opts.maxResponseBytes ?? 16 * 1024 * 1024);
40
+ this.parseJson = opts.parseJson ?? JSON.parse;
39
41
  const gf = globalThis.fetch;
40
42
  this.f = opts.fetch ?? (gf ? gf.bind(globalThis) : gf);
41
43
  this.timeoutMs = opts.timeoutMs ?? 1e4;
@@ -77,7 +79,7 @@ var CsdClient = class {
77
79
  const t = await r.text();
78
80
  const byteLen = new TextEncoder().encode(t).length;
79
81
  if (byteLen > max) throw new Error(`GET ${path} \u2192 response too large (${byteLen} > ${max} bytes)`);
80
- return JSON.parse(t);
82
+ return this.parseJson(t);
81
83
  }
82
84
  const reader = body.getReader();
83
85
  const chunks = [];
@@ -103,7 +105,7 @@ var CsdClient = class {
103
105
  buf.set(c, off);
104
106
  off += c.length;
105
107
  }
106
- return JSON.parse(new TextDecoder().decode(buf));
108
+ return this.parseJson(new TextDecoder().decode(buf));
107
109
  }
108
110
  get(path) {
109
111
  return this.req(path);
@@ -224,24 +226,24 @@ async function verifyInputValues(client, inputs) {
224
226
  try {
225
227
  info = await client.tx(i.txid);
226
228
  } catch {
227
- return { ok: false, total: 0 };
229
+ return { ok: false, total: 0, badInput: i };
228
230
  }
229
231
  const body = info?.tx ?? info;
230
- if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0 };
232
+ if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0, badInput: i };
231
233
  let tx, idHex;
232
234
  try {
233
235
  tx = rpcTxToTx(body);
234
236
  idHex = (0, import_csd_codec.txid)(tx);
235
237
  } catch {
236
- return { ok: false, total: 0 };
238
+ return { ok: false, total: 0, badInput: i };
237
239
  }
238
- if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0 };
240
+ if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0, badInput: i };
239
241
  const out = tx.outputs[i.vout];
240
- if (!out) return { ok: false, total: 0 };
242
+ if (!out) return { ok: false, total: 0, badInput: i };
241
243
  const v = Number(out.value);
242
- if (!Number.isSafeInteger(v) || v <= 0) return { ok: false, total: 0 };
244
+ if (!Number.isSafeInteger(v) || v <= 0) return { ok: false, total: 0, badInput: i };
243
245
  total += v;
244
- if (!Number.isSafeInteger(total)) return { ok: false, total: 0 };
246
+ if (!Number.isSafeInteger(total)) return { ok: false, total: 0, badInput: i };
245
247
  }
246
248
  return { ok: true, total };
247
249
  }
package/dist/index.d.cts CHANGED
@@ -118,6 +118,12 @@ interface ClientOptions {
118
118
  * path (audit M2). Default 16 MiB ≫ any real block; raise it for an unusually large block source.
119
119
  */
120
120
  maxResponseBytes?: number;
121
+ /**
122
+ * M1: optional JSON body parser (default `JSON.parse`). A verifier that must preserve u64 fields
123
+ * (e.g. a Propose's `expires_epoch`, unbounded on-chain) past 2^53 injects a source-preserving
124
+ * reviver here so the exact value survives to the txid recompute. The default is unchanged.
125
+ */
126
+ parseJson?: (text: string) => unknown;
121
127
  }
122
128
  declare class CsdClient {
123
129
  private readonly base;
@@ -125,6 +131,7 @@ declare class CsdClient {
125
131
  private readonly timeoutMs;
126
132
  private readonly retries;
127
133
  private readonly maxBytes;
134
+ private readonly parseJson;
128
135
  constructor(opts: ClientOptions);
129
136
  private req;
130
137
  /**
@@ -194,6 +201,10 @@ declare function verifyInputValues(client: {
194
201
  }[]): Promise<{
195
202
  ok: boolean;
196
203
  total: number;
204
+ badInput?: {
205
+ txid: string;
206
+ vout: number;
207
+ };
197
208
  }>;
198
209
  /** Convert a node /block header JSON into the codec's BlockHeader. */
199
210
  declare function rpcHeaderToHeader(h: RpcHeaderJson): BlockHeader;
package/dist/index.d.ts CHANGED
@@ -118,6 +118,12 @@ interface ClientOptions {
118
118
  * path (audit M2). Default 16 MiB ≫ any real block; raise it for an unusually large block source.
119
119
  */
120
120
  maxResponseBytes?: number;
121
+ /**
122
+ * M1: optional JSON body parser (default `JSON.parse`). A verifier that must preserve u64 fields
123
+ * (e.g. a Propose's `expires_epoch`, unbounded on-chain) past 2^53 injects a source-preserving
124
+ * reviver here so the exact value survives to the txid recompute. The default is unchanged.
125
+ */
126
+ parseJson?: (text: string) => unknown;
121
127
  }
122
128
  declare class CsdClient {
123
129
  private readonly base;
@@ -125,6 +131,7 @@ declare class CsdClient {
125
131
  private readonly timeoutMs;
126
132
  private readonly retries;
127
133
  private readonly maxBytes;
134
+ private readonly parseJson;
128
135
  constructor(opts: ClientOptions);
129
136
  private req;
130
137
  /**
@@ -194,6 +201,10 @@ declare function verifyInputValues(client: {
194
201
  }[]): Promise<{
195
202
  ok: boolean;
196
203
  total: number;
204
+ badInput?: {
205
+ txid: string;
206
+ vout: number;
207
+ };
197
208
  }>;
198
209
  /** Convert a node /block header JSON into the codec's BlockHeader. */
199
210
  declare function rpcHeaderToHeader(h: RpcHeaderJson): BlockHeader;
package/dist/index.js CHANGED
@@ -6,9 +6,11 @@ var CsdClient = class {
6
6
  timeoutMs;
7
7
  retries;
8
8
  maxBytes;
9
+ parseJson;
9
10
  constructor(opts) {
10
11
  this.base = opts.baseUrl.replace(/\/+$/, "");
11
12
  this.maxBytes = Math.max(1, opts.maxResponseBytes ?? 16 * 1024 * 1024);
13
+ this.parseJson = opts.parseJson ?? JSON.parse;
12
14
  const gf = globalThis.fetch;
13
15
  this.f = opts.fetch ?? (gf ? gf.bind(globalThis) : gf);
14
16
  this.timeoutMs = opts.timeoutMs ?? 1e4;
@@ -50,7 +52,7 @@ var CsdClient = class {
50
52
  const t = await r.text();
51
53
  const byteLen = new TextEncoder().encode(t).length;
52
54
  if (byteLen > max) throw new Error(`GET ${path} \u2192 response too large (${byteLen} > ${max} bytes)`);
53
- return JSON.parse(t);
55
+ return this.parseJson(t);
54
56
  }
55
57
  const reader = body.getReader();
56
58
  const chunks = [];
@@ -76,7 +78,7 @@ var CsdClient = class {
76
78
  buf.set(c, off);
77
79
  off += c.length;
78
80
  }
79
- return JSON.parse(new TextDecoder().decode(buf));
81
+ return this.parseJson(new TextDecoder().decode(buf));
80
82
  }
81
83
  get(path) {
82
84
  return this.req(path);
@@ -197,24 +199,24 @@ async function verifyInputValues(client, inputs) {
197
199
  try {
198
200
  info = await client.tx(i.txid);
199
201
  } catch {
200
- return { ok: false, total: 0 };
202
+ return { ok: false, total: 0, badInput: i };
201
203
  }
202
204
  const body = info?.tx ?? info;
203
- if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0 };
205
+ if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0, badInput: i };
204
206
  let tx, idHex;
205
207
  try {
206
208
  tx = rpcTxToTx(body);
207
209
  idHex = codecTxid(tx);
208
210
  } catch {
209
- return { ok: false, total: 0 };
211
+ return { ok: false, total: 0, badInput: i };
210
212
  }
211
- if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0 };
213
+ if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0, badInput: i };
212
214
  const out = tx.outputs[i.vout];
213
- if (!out) return { ok: false, total: 0 };
215
+ if (!out) return { ok: false, total: 0, badInput: i };
214
216
  const v = Number(out.value);
215
- if (!Number.isSafeInteger(v) || v <= 0) return { ok: false, total: 0 };
217
+ if (!Number.isSafeInteger(v) || v <= 0) return { ok: false, total: 0, badInput: i };
216
218
  total += v;
217
- if (!Number.isSafeInteger(total)) return { ok: false, total: 0 };
219
+ if (!Number.isSafeInteger(total)) return { ok: false, total: 0, badInput: i };
218
220
  }
219
221
  return { ok: true, total };
220
222
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inversealtruism/csd-client",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "engines": {
5
5
  "node": ">=18"
6
6
  },
@@ -27,7 +27,7 @@
27
27
  "README.md"
28
28
  ],
29
29
  "dependencies": {
30
- "@inversealtruism/csd-codec": "0.1.15"
30
+ "@inversealtruism/csd-codec": "0.1.16"
31
31
  },
32
32
  "license": "MIT",
33
33
  "sideEffects": false,