@inversealtruism/csd-client 0.1.11 → 0.1.13

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
@@ -42,17 +42,18 @@ var CsdClient = class {
42
42
  this.retries = Math.max(0, opts.retries ?? 0);
43
43
  if (!this.f) throw new Error("no fetch available \u2014 pass opts.fetch");
44
44
  }
45
- async req(path, init) {
45
+ async req(path, init, opts) {
46
+ const maxRetries = opts?.noRetry ? 0 : this.retries;
46
47
  let lastErr;
47
48
  for (let attempt = 0; ; attempt++) {
48
49
  try {
49
50
  const r = await this.f(`${this.base}${path}`, { ...init, signal: AbortSignal.timeout(this.timeoutMs) });
50
- if (r.status >= 500 && attempt < this.retries) {
51
+ if (r.status >= 500 && attempt < maxRetries) {
51
52
  lastErr = new Error(`HTTP ${r.status}`);
52
53
  } else if (!r.ok) throw new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`);
53
54
  else return await this.readCapped(r, path);
54
55
  } catch (e) {
55
- if (attempt >= this.retries) throw e;
56
+ if (attempt >= maxRetries) throw e;
56
57
  lastErr = e;
57
58
  }
58
59
  const cap = Math.min(5e3, 250 * 2 ** attempt);
@@ -74,7 +75,8 @@ var CsdClient = class {
74
75
  const body = r.body;
75
76
  if (!body || typeof body.getReader !== "function") {
76
77
  const t = await r.text();
77
- if (t.length > max) throw new Error(`GET ${path} \u2192 response too large (${t.length} > ${max} bytes)`);
78
+ const byteLen = new TextEncoder().encode(t).length;
79
+ if (byteLen > max) throw new Error(`GET ${path} \u2192 response too large (${byteLen} > ${max} bytes)`);
78
80
  return JSON.parse(t);
79
81
  }
80
82
  const reader = body.getReader();
@@ -106,8 +108,8 @@ var CsdClient = class {
106
108
  get(path) {
107
109
  return this.req(path);
108
110
  }
109
- post(path, body) {
110
- return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
111
+ post(path, body, opts) {
112
+ return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }, opts);
111
113
  }
112
114
  // The node returns application errors as `{ok:false, err}` with HTTP **200**, so a bare `get()`
113
115
  // can't see them. For endpoints whose `{ok:false}` result is useless to the caller (a missing
@@ -133,11 +135,17 @@ var CsdClient = class {
133
135
  tx(id) {
134
136
  return this.get(`/tx/${id}`);
135
137
  }
136
- utxos(addr) {
137
- return this.get(`/utxos/${addr}`);
138
+ // Default available=true: excludes immature/locked coinbase UTXOs so callers don't build txs spending
139
+ // un-spendable outputs (the node would silently reject them) — audit TXB-1-SDK. Pass {available:false}
140
+ // for the full set (e.g. balance display that wants to show locked coinbases).
141
+ utxos(addr, opts = {}) {
142
+ return this.get(`/utxos/${addr}${opts.available === false ? "" : "?available=true"}`);
138
143
  }
144
+ // getOk: a not-found proposal returns {ok:false}@200; without this the caller reads .domain/.uri off a
145
+ // malformed object instead of seeing the error (audit M6). (tx() deliberately keeps its bare get — its
146
+ // {ok:false} is a documented VALID "not yet in a block" state that waitForTx/verifyInputValues handle.)
139
147
  proposal(id) {
140
- return this.get(`/proposal/${id}`);
148
+ return this.getOk(`/proposal/${id}`);
141
149
  }
142
150
  proposals(domain, limit = 40) {
143
151
  return this.get(`/proposals/${encodeURIComponent(domain)}/${limit}`);
@@ -183,7 +191,7 @@ var CsdClient = class {
183
191
  * alone mistakes a rejected tx for a broadcast one. Use `submitOrThrow` if you want a hard failure.
184
192
  */
185
193
  submit(nodeJsonTx) {
186
- return this.post("/tx/submit", { tx: nodeJsonTx });
194
+ return this.post("/tx/submit", { tx: nodeJsonTx }, { noRetry: true });
187
195
  }
188
196
  /** As `submit`, but throws on node rejection (`ok:false`) instead of returning a misleading txid. */
189
197
  async submitOrThrow(nodeJsonTx) {
@@ -220,13 +228,14 @@ async function verifyInputValues(client, inputs) {
220
228
  }
221
229
  const body = info?.tx ?? info;
222
230
  if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0 };
223
- let tx;
231
+ let tx, idHex;
224
232
  try {
225
233
  tx = rpcTxToTx(body);
234
+ idHex = (0, import_csd_codec.txid)(tx);
226
235
  } catch {
227
236
  return { ok: false, total: 0 };
228
237
  }
229
- if (norm((0, import_csd_codec.txid)(tx)) !== norm(i.txid)) return { ok: false, total: 0 };
238
+ if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0 };
230
239
  const out = tx.outputs[i.vout];
231
240
  if (!out) return { ok: false, total: 0 };
232
241
  const v = Number(out.value);
package/dist/index.d.cts CHANGED
@@ -143,7 +143,9 @@ declare class CsdClient {
143
143
  blockByHeight(h: number): Promise<RpcBlock>;
144
144
  blockByHash(hash: string): Promise<RpcBlock>;
145
145
  tx(id: string): Promise<RpcTxInfo>;
146
- utxos(addr: string): Promise<RpcUtxos>;
146
+ utxos(addr: string, opts?: {
147
+ available?: boolean;
148
+ }): Promise<RpcUtxos>;
147
149
  proposal(id: string): Promise<RpcProposal>;
148
150
  proposals(domain: string, limit?: number): Promise<RpcProposal[]>;
149
151
  topDomain(domain: string, epoch?: number): Promise<unknown>;
package/dist/index.d.ts CHANGED
@@ -143,7 +143,9 @@ declare class CsdClient {
143
143
  blockByHeight(h: number): Promise<RpcBlock>;
144
144
  blockByHash(hash: string): Promise<RpcBlock>;
145
145
  tx(id: string): Promise<RpcTxInfo>;
146
- utxos(addr: string): Promise<RpcUtxos>;
146
+ utxos(addr: string, opts?: {
147
+ available?: boolean;
148
+ }): Promise<RpcUtxos>;
147
149
  proposal(id: string): Promise<RpcProposal>;
148
150
  proposals(domain: string, limit?: number): Promise<RpcProposal[]>;
149
151
  topDomain(domain: string, epoch?: number): Promise<unknown>;
package/dist/index.js CHANGED
@@ -15,17 +15,18 @@ var CsdClient = class {
15
15
  this.retries = Math.max(0, opts.retries ?? 0);
16
16
  if (!this.f) throw new Error("no fetch available \u2014 pass opts.fetch");
17
17
  }
18
- async req(path, init) {
18
+ async req(path, init, opts) {
19
+ const maxRetries = opts?.noRetry ? 0 : this.retries;
19
20
  let lastErr;
20
21
  for (let attempt = 0; ; attempt++) {
21
22
  try {
22
23
  const r = await this.f(`${this.base}${path}`, { ...init, signal: AbortSignal.timeout(this.timeoutMs) });
23
- if (r.status >= 500 && attempt < this.retries) {
24
+ if (r.status >= 500 && attempt < maxRetries) {
24
25
  lastErr = new Error(`HTTP ${r.status}`);
25
26
  } else if (!r.ok) throw new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`);
26
27
  else return await this.readCapped(r, path);
27
28
  } catch (e) {
28
- if (attempt >= this.retries) throw e;
29
+ if (attempt >= maxRetries) throw e;
29
30
  lastErr = e;
30
31
  }
31
32
  const cap = Math.min(5e3, 250 * 2 ** attempt);
@@ -47,7 +48,8 @@ var CsdClient = class {
47
48
  const body = r.body;
48
49
  if (!body || typeof body.getReader !== "function") {
49
50
  const t = await r.text();
50
- if (t.length > max) throw new Error(`GET ${path} \u2192 response too large (${t.length} > ${max} bytes)`);
51
+ const byteLen = new TextEncoder().encode(t).length;
52
+ if (byteLen > max) throw new Error(`GET ${path} \u2192 response too large (${byteLen} > ${max} bytes)`);
51
53
  return JSON.parse(t);
52
54
  }
53
55
  const reader = body.getReader();
@@ -79,8 +81,8 @@ var CsdClient = class {
79
81
  get(path) {
80
82
  return this.req(path);
81
83
  }
82
- post(path, body) {
83
- return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
84
+ post(path, body, opts) {
85
+ return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }, opts);
84
86
  }
85
87
  // The node returns application errors as `{ok:false, err}` with HTTP **200**, so a bare `get()`
86
88
  // can't see them. For endpoints whose `{ok:false}` result is useless to the caller (a missing
@@ -106,11 +108,17 @@ var CsdClient = class {
106
108
  tx(id) {
107
109
  return this.get(`/tx/${id}`);
108
110
  }
109
- utxos(addr) {
110
- return this.get(`/utxos/${addr}`);
111
+ // Default available=true: excludes immature/locked coinbase UTXOs so callers don't build txs spending
112
+ // un-spendable outputs (the node would silently reject them) — audit TXB-1-SDK. Pass {available:false}
113
+ // for the full set (e.g. balance display that wants to show locked coinbases).
114
+ utxos(addr, opts = {}) {
115
+ return this.get(`/utxos/${addr}${opts.available === false ? "" : "?available=true"}`);
111
116
  }
117
+ // getOk: a not-found proposal returns {ok:false}@200; without this the caller reads .domain/.uri off a
118
+ // malformed object instead of seeing the error (audit M6). (tx() deliberately keeps its bare get — its
119
+ // {ok:false} is a documented VALID "not yet in a block" state that waitForTx/verifyInputValues handle.)
112
120
  proposal(id) {
113
- return this.get(`/proposal/${id}`);
121
+ return this.getOk(`/proposal/${id}`);
114
122
  }
115
123
  proposals(domain, limit = 40) {
116
124
  return this.get(`/proposals/${encodeURIComponent(domain)}/${limit}`);
@@ -156,7 +164,7 @@ var CsdClient = class {
156
164
  * alone mistakes a rejected tx for a broadcast one. Use `submitOrThrow` if you want a hard failure.
157
165
  */
158
166
  submit(nodeJsonTx) {
159
- return this.post("/tx/submit", { tx: nodeJsonTx });
167
+ return this.post("/tx/submit", { tx: nodeJsonTx }, { noRetry: true });
160
168
  }
161
169
  /** As `submit`, but throws on node rejection (`ok:false`) instead of returning a misleading txid. */
162
170
  async submitOrThrow(nodeJsonTx) {
@@ -193,13 +201,14 @@ async function verifyInputValues(client, inputs) {
193
201
  }
194
202
  const body = info?.tx ?? info;
195
203
  if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0 };
196
- let tx;
204
+ let tx, idHex;
197
205
  try {
198
206
  tx = rpcTxToTx(body);
207
+ idHex = codecTxid(tx);
199
208
  } catch {
200
209
  return { ok: false, total: 0 };
201
210
  }
202
- if (norm(codecTxid(tx)) !== norm(i.txid)) return { ok: false, total: 0 };
211
+ if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0 };
203
212
  const out = tx.outputs[i.vout];
204
213
  if (!out) return { ok: false, total: 0 };
205
214
  const v = Number(out.value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inversealtruism/csd-client",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "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.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -19,12 +19,12 @@
19
19
  "README.md"
20
20
  ],
21
21
  "dependencies": {
22
- "@inversealtruism/csd-codec": "0.1.11"
22
+ "@inversealtruism/csd-codec": "0.1.13"
23
23
  },
24
24
  "license": "MIT",
25
25
  "sideEffects": false,
26
26
  "devDependencies": {
27
- "@inversealtruism/csd-vectors": "0.1.11"
27
+ "@inversealtruism/csd-vectors": "0.1.13"
28
28
  },
29
29
  "publishConfig": {
30
30
  "access": "public"