@inversealtruism/csd-client 0.1.17 → 0.1.19

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
@@ -21,12 +21,20 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  CsdClient: () => CsdClient,
24
+ parseJsonExact: () => parseJsonExact,
24
25
  rpcHeaderToHeader: () => rpcHeaderToHeader,
25
26
  rpcTxToTx: () => rpcTxToTx,
26
27
  verifyInputValues: () => verifyInputValues
27
28
  });
28
29
  module.exports = __toCommonJS(index_exports);
29
30
  var import_csd_codec = require("@inversealtruism/csd-codec");
31
+ var exactIntReviver = (_key, value, context) => typeof value === "number" && !Number.isSafeInteger(value) && typeof context?.source === "string" && /^-?\d+$/.test(context.source) ? BigInt(context.source) : value;
32
+ function parseJsonExact(text) {
33
+ return JSON.parse(text, exactIntReviver);
34
+ }
35
+ var DEFAULT_MAX_RESPONSE_BYTES = 16 * 1024 * 1024;
36
+ var DEFAULT_RETRIES = 0;
37
+ var finiteOr = (v, dflt) => v !== void 0 && Number.isFinite(v) ? v : dflt;
30
38
  var CsdClient = class {
31
39
  base;
32
40
  f;
@@ -36,12 +44,12 @@ var CsdClient = class {
36
44
  parseJson;
37
45
  constructor(opts) {
38
46
  this.base = opts.baseUrl.replace(/\/+$/, "");
39
- this.maxBytes = Math.max(1, opts.maxResponseBytes ?? 16 * 1024 * 1024);
40
- this.parseJson = opts.parseJson ?? JSON.parse;
47
+ this.maxBytes = Math.max(1, finiteOr(opts.maxResponseBytes, DEFAULT_MAX_RESPONSE_BYTES));
48
+ this.parseJson = opts.parseJson ?? parseJsonExact;
41
49
  const gf = globalThis.fetch;
42
50
  this.f = opts.fetch ?? (gf ? gf.bind(globalThis) : gf);
43
51
  this.timeoutMs = opts.timeoutMs ?? 1e4;
44
- this.retries = Math.max(0, opts.retries ?? 0);
52
+ this.retries = Math.max(0, Math.floor(finiteOr(opts.retries, DEFAULT_RETRIES)));
45
53
  if (!this.f) throw new Error("no fetch available \u2014 pass opts.fetch");
46
54
  }
47
55
  async req(path, init, opts) {
@@ -167,6 +175,8 @@ var CsdClient = class {
167
175
  * rejects on timeout (default 10 min — CSD blocks are ~2 min but the live miner is lumpy).
168
176
  * A tx that drops OUT of the chain mid-wait (reorg) keeps polling until it re-confirms or
169
177
  * times out — never resolves on a stale sighting.
178
+ * The count is `tip.height - tx.height + 1` from the RPC's own /tx and /tip answers, with no PoW
179
+ * or inclusion check, so a lying RPC can over-state it. Gate funds on csd-light, not on this.
170
180
  */
171
181
  async waitForTx(txid, opts = {}) {
172
182
  const want = Math.max(1, opts.confirmations ?? 1);
@@ -253,6 +263,7 @@ function rpcHeaderToHeader(h) {
253
263
  // Annotate the CommonJS export names for ESM import in node:
254
264
  0 && (module.exports = {
255
265
  CsdClient,
266
+ parseJsonExact,
256
267
  rpcHeaderToHeader,
257
268
  rpcTxToTx,
258
269
  verifyInputValues
package/dist/index.d.cts CHANGED
@@ -17,8 +17,9 @@ interface RpcTxJson {
17
17
  script_sig: string;
18
18
  script_sig_text?: string | null;
19
19
  }[];
20
+ /** u64 fields arrive as bigint when above 2^53 (parseJsonExact). */
20
21
  outputs: {
21
- value: number;
22
+ value: number | bigint;
22
23
  script_pubkey: string;
23
24
  }[];
24
25
  locktime: number;
@@ -29,7 +30,7 @@ interface RpcTxJson {
29
30
  domain: string;
30
31
  payload_hash: string;
31
32
  uri: string;
32
- expires_epoch: number;
33
+ expires_epoch: number | bigint;
33
34
  } | {
34
35
  type: "Attest";
35
36
  proposal_id: string;
@@ -77,7 +78,7 @@ interface RpcProposal {
77
78
  domain: string;
78
79
  payload_hash: string;
79
80
  uri: string;
80
- expires_epoch: number;
81
+ expires_epoch: number | bigint;
81
82
  proposer: string;
82
83
  height: number;
83
84
  attestations?: unknown[];
@@ -118,13 +119,11 @@ interface ClientOptions {
118
119
  * path (audit M2). Default 16 MiB ≫ any real block; raise it for an unusually large block source.
119
120
  */
120
121
  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
- */
122
+ /** JSON body parser; default `parseJsonExact`, so a u64 above 2^53 survives to the txid recompute. */
126
123
  parseJson?: (text: string) => unknown;
127
124
  }
125
+ /** JSON.parse, except that an integer literal outside the safe JS range is returned as an exact bigint. */
126
+ declare function parseJsonExact(text: string): unknown;
128
127
  declare class CsdClient {
129
128
  private readonly base;
130
129
  private readonly f;
@@ -164,6 +163,8 @@ declare class CsdClient {
164
163
  * rejects on timeout (default 10 min — CSD blocks are ~2 min but the live miner is lumpy).
165
164
  * A tx that drops OUT of the chain mid-wait (reorg) keeps polling until it re-confirms or
166
165
  * times out — never resolves on a stale sighting.
166
+ * The count is `tip.height - tx.height + 1` from the RPC's own /tx and /tip answers, with no PoW
167
+ * or inclusion check, so a lying RPC can over-state it. Gate funds on csd-light, not on this.
167
168
  */
168
169
  waitForTx(txid: string, opts?: {
169
170
  confirmations?: number;
@@ -191,8 +192,7 @@ declare function rpcTxToTx(j: RpcTxJson): Tx;
191
192
  * a forged body whose recomputed txid still matches the prevout is impossible. Fail-CLOSED: any
192
193
  * unreachable/forged/missing source aborts. Returns the verified input total (use it to compute
193
194
  * change from REAL values, not the reported ones). RPC-facing builders (wallet, cairn-sdk) should
194
- * call this before assembling a spend; csd-tx itself is pure and cannot fetch.
195
- * (Mirrors the proven cairn-wallet `node.ts` implementation; the wallet's TXB-1 cure made canonical.) */
195
+ * call this before assembling a spend; csd-tx itself is pure and cannot fetch. */
196
196
  declare function verifyInputValues(client: {
197
197
  tx(id: string): Promise<RpcTxInfo>;
198
198
  }, inputs: {
@@ -209,4 +209,4 @@ declare function verifyInputValues(client: {
209
209
  /** Convert a node /block header JSON into the codec's BlockHeader. */
210
210
  declare function rpcHeaderToHeader(h: RpcHeaderJson): BlockHeader;
211
211
 
212
- export { type ClientOptions, CsdClient, type RpcBlock, type RpcHeaderJson, type RpcHealth, type RpcProposal, type RpcSubmit, type RpcTip, type RpcTxInfo, type RpcTxJson, type RpcUtxo, type RpcUtxos, type WaitForTxResult, rpcHeaderToHeader, rpcTxToTx, verifyInputValues };
212
+ export { type ClientOptions, CsdClient, type RpcBlock, type RpcHeaderJson, type RpcHealth, type RpcProposal, type RpcSubmit, type RpcTip, type RpcTxInfo, type RpcTxJson, type RpcUtxo, type RpcUtxos, type WaitForTxResult, parseJsonExact, rpcHeaderToHeader, rpcTxToTx, verifyInputValues };
package/dist/index.d.ts CHANGED
@@ -17,8 +17,9 @@ interface RpcTxJson {
17
17
  script_sig: string;
18
18
  script_sig_text?: string | null;
19
19
  }[];
20
+ /** u64 fields arrive as bigint when above 2^53 (parseJsonExact). */
20
21
  outputs: {
21
- value: number;
22
+ value: number | bigint;
22
23
  script_pubkey: string;
23
24
  }[];
24
25
  locktime: number;
@@ -29,7 +30,7 @@ interface RpcTxJson {
29
30
  domain: string;
30
31
  payload_hash: string;
31
32
  uri: string;
32
- expires_epoch: number;
33
+ expires_epoch: number | bigint;
33
34
  } | {
34
35
  type: "Attest";
35
36
  proposal_id: string;
@@ -77,7 +78,7 @@ interface RpcProposal {
77
78
  domain: string;
78
79
  payload_hash: string;
79
80
  uri: string;
80
- expires_epoch: number;
81
+ expires_epoch: number | bigint;
81
82
  proposer: string;
82
83
  height: number;
83
84
  attestations?: unknown[];
@@ -118,13 +119,11 @@ interface ClientOptions {
118
119
  * path (audit M2). Default 16 MiB ≫ any real block; raise it for an unusually large block source.
119
120
  */
120
121
  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
- */
122
+ /** JSON body parser; default `parseJsonExact`, so a u64 above 2^53 survives to the txid recompute. */
126
123
  parseJson?: (text: string) => unknown;
127
124
  }
125
+ /** JSON.parse, except that an integer literal outside the safe JS range is returned as an exact bigint. */
126
+ declare function parseJsonExact(text: string): unknown;
128
127
  declare class CsdClient {
129
128
  private readonly base;
130
129
  private readonly f;
@@ -164,6 +163,8 @@ declare class CsdClient {
164
163
  * rejects on timeout (default 10 min — CSD blocks are ~2 min but the live miner is lumpy).
165
164
  * A tx that drops OUT of the chain mid-wait (reorg) keeps polling until it re-confirms or
166
165
  * times out — never resolves on a stale sighting.
166
+ * The count is `tip.height - tx.height + 1` from the RPC's own /tx and /tip answers, with no PoW
167
+ * or inclusion check, so a lying RPC can over-state it. Gate funds on csd-light, not on this.
167
168
  */
168
169
  waitForTx(txid: string, opts?: {
169
170
  confirmations?: number;
@@ -191,8 +192,7 @@ declare function rpcTxToTx(j: RpcTxJson): Tx;
191
192
  * a forged body whose recomputed txid still matches the prevout is impossible. Fail-CLOSED: any
192
193
  * unreachable/forged/missing source aborts. Returns the verified input total (use it to compute
193
194
  * change from REAL values, not the reported ones). RPC-facing builders (wallet, cairn-sdk) should
194
- * call this before assembling a spend; csd-tx itself is pure and cannot fetch.
195
- * (Mirrors the proven cairn-wallet `node.ts` implementation; the wallet's TXB-1 cure made canonical.) */
195
+ * call this before assembling a spend; csd-tx itself is pure and cannot fetch. */
196
196
  declare function verifyInputValues(client: {
197
197
  tx(id: string): Promise<RpcTxInfo>;
198
198
  }, inputs: {
@@ -209,4 +209,4 @@ declare function verifyInputValues(client: {
209
209
  /** Convert a node /block header JSON into the codec's BlockHeader. */
210
210
  declare function rpcHeaderToHeader(h: RpcHeaderJson): BlockHeader;
211
211
 
212
- export { type ClientOptions, CsdClient, type RpcBlock, type RpcHeaderJson, type RpcHealth, type RpcProposal, type RpcSubmit, type RpcTip, type RpcTxInfo, type RpcTxJson, type RpcUtxo, type RpcUtxos, type WaitForTxResult, rpcHeaderToHeader, rpcTxToTx, verifyInputValues };
212
+ export { type ClientOptions, CsdClient, type RpcBlock, type RpcHeaderJson, type RpcHealth, type RpcProposal, type RpcSubmit, type RpcTip, type RpcTxInfo, type RpcTxJson, type RpcUtxo, type RpcUtxos, type WaitForTxResult, parseJsonExact, rpcHeaderToHeader, rpcTxToTx, verifyInputValues };
package/dist/index.js CHANGED
@@ -1,5 +1,12 @@
1
1
  // src/index.ts
2
2
  import { txid as codecTxid } from "@inversealtruism/csd-codec";
3
+ var exactIntReviver = (_key, value, context) => typeof value === "number" && !Number.isSafeInteger(value) && typeof context?.source === "string" && /^-?\d+$/.test(context.source) ? BigInt(context.source) : value;
4
+ function parseJsonExact(text) {
5
+ return JSON.parse(text, exactIntReviver);
6
+ }
7
+ var DEFAULT_MAX_RESPONSE_BYTES = 16 * 1024 * 1024;
8
+ var DEFAULT_RETRIES = 0;
9
+ var finiteOr = (v, dflt) => v !== void 0 && Number.isFinite(v) ? v : dflt;
3
10
  var CsdClient = class {
4
11
  base;
5
12
  f;
@@ -9,12 +16,12 @@ var CsdClient = class {
9
16
  parseJson;
10
17
  constructor(opts) {
11
18
  this.base = opts.baseUrl.replace(/\/+$/, "");
12
- this.maxBytes = Math.max(1, opts.maxResponseBytes ?? 16 * 1024 * 1024);
13
- this.parseJson = opts.parseJson ?? JSON.parse;
19
+ this.maxBytes = Math.max(1, finiteOr(opts.maxResponseBytes, DEFAULT_MAX_RESPONSE_BYTES));
20
+ this.parseJson = opts.parseJson ?? parseJsonExact;
14
21
  const gf = globalThis.fetch;
15
22
  this.f = opts.fetch ?? (gf ? gf.bind(globalThis) : gf);
16
23
  this.timeoutMs = opts.timeoutMs ?? 1e4;
17
- this.retries = Math.max(0, opts.retries ?? 0);
24
+ this.retries = Math.max(0, Math.floor(finiteOr(opts.retries, DEFAULT_RETRIES)));
18
25
  if (!this.f) throw new Error("no fetch available \u2014 pass opts.fetch");
19
26
  }
20
27
  async req(path, init, opts) {
@@ -140,6 +147,8 @@ var CsdClient = class {
140
147
  * rejects on timeout (default 10 min — CSD blocks are ~2 min but the live miner is lumpy).
141
148
  * A tx that drops OUT of the chain mid-wait (reorg) keeps polling until it re-confirms or
142
149
  * times out — never resolves on a stale sighting.
150
+ * The count is `tip.height - tx.height + 1` from the RPC's own /tx and /tip answers, with no PoW
151
+ * or inclusion check, so a lying RPC can over-state it. Gate funds on csd-light, not on this.
143
152
  */
144
153
  async waitForTx(txid, opts = {}) {
145
154
  const want = Math.max(1, opts.confirmations ?? 1);
@@ -225,6 +234,7 @@ function rpcHeaderToHeader(h) {
225
234
  }
226
235
  export {
227
236
  CsdClient,
237
+ parseJsonExact,
228
238
  rpcHeaderToHeader,
229
239
  rpcTxToTx,
230
240
  verifyInputValues
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@inversealtruism/csd-client",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "engines": {
5
5
  "node": ">=18"
6
6
  },
7
- "description": "Typed HTTP RPC client for a Compute Substrate node (or the Cairn proxy / a discovered gateway) — tip/block/tx/utxos/proposals/top/domains/mempool/submit/templates.",
7
+ "description": "Typed HTTP RPC client for a Compute Substrate node (or the Cairn proxy / a discovered gateway): tip/block/tx/utxos/proposals/top/domains/mempool/submit/templates.",
8
8
  "type": "module",
9
9
  "main": "./dist/index.cjs",
10
10
  "module": "./dist/index.js",
@@ -27,7 +27,7 @@
27
27
  "README.md"
28
28
  ],
29
29
  "dependencies": {
30
- "@inversealtruism/csd-codec": "0.1.17"
30
+ "@inversealtruism/csd-codec": "0.1.18"
31
31
  },
32
32
  "license": "MIT",
33
33
  "sideEffects": false,
@@ -45,6 +45,7 @@
45
45
  "homepage": "https://cairn-substrate.com",
46
46
  "scripts": {
47
47
  "build": "tsup src/index.ts --format esm,cjs --dts --clean",
48
- "test": "tsx test/client.test.ts && tsx test/hostile.test.ts"
48
+ "test": "tsx test/hostile.test.ts && tsx test/input-canon.test.ts",
49
+ "test:live": "tsx test/client.test.ts"
49
50
  }
50
51
  }