@xyo-network/xl1-rpc 2.2.0 → 2.2.1

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.
@@ -18,7 +18,7 @@ export type RpcContext = MiddlewareContext<RpcContextKeyValues>;
18
18
  /**
19
19
  * Middleware shape used throughout the RPC engine. We only handle
20
20
  * {@link JsonRpcRequest}s (no notifications), so Result is constrained to
21
- * {@link Json}. This matches what `createScaffoldMiddleware` and a sub-engine
21
+ * {@link JSON}. This matches what `createScaffoldMiddleware` and a sub-engine
22
22
  * built purely from request-handling middleware will produce.
23
23
  */
24
24
  export type RpcMiddleware = JsonRpcMiddleware<JsonRpcRequest, Json, RpcContext>;
@@ -1604,14 +1604,15 @@ var HttpRpcTransport = class {
1604
1604
  method
1605
1605
  };
1606
1606
  const schema = schemas[method];
1607
- if (params) {
1607
+ if (params !== void 0) {
1608
1608
  body.params = isDefined(schema) ? schema.params.to.parse(params) : this._passThrough ? z25.json().parse(params) : void 0;
1609
1609
  if (!isDefined(body.params)) {
1610
1610
  throw new Error(`[callRpc] RPC method ${String(method)} missing schema`);
1611
1611
  }
1612
1612
  }
1613
1613
  body.params ??= [];
1614
- const res = await new FetchJsonClient().post(url, body);
1614
+ const client = new FetchJsonClient();
1615
+ const res = await client.post(url, body);
1615
1616
  const json = res.data;
1616
1617
  if (isUndefinedOrNull(json)) {
1617
1618
  throw new Error(`[callRpc] empty response body (status ${res.status})`);
@@ -1718,22 +1719,33 @@ var isMessageWithId = (message) => {
1718
1719
  };
1719
1720
 
1720
1721
  // src/transport/PostMessage/bus/implementations/PostMessage.ts
1722
+ function browserWindow() {
1723
+ return globalThis;
1724
+ }
1725
+ function deliverPostMessage(message, targetOrigin, transfer) {
1726
+ const win = browserWindow();
1727
+ if (typeof win.postMessage !== "function") {
1728
+ throw new TypeError("postMessage is only available in browser environments");
1729
+ }
1730
+ const postMessageFn = win.postMessage;
1731
+ postMessageFn.call(win, message, targetOrigin, transfer);
1732
+ }
1721
1733
  var PostMessageBus = class extends AbstractMessageBus {
1722
1734
  constructor(sessionId, name = "default") {
1723
1735
  super(sessionId, name);
1724
1736
  }
1725
- postMessage(message, origin = globalThis.location.origin) {
1726
- globalThis.postMessage(message, origin);
1737
+ postMessage(message, origin) {
1738
+ deliverPostMessage(message, origin ?? browserWindow().location?.origin ?? "*");
1727
1739
  }
1728
1740
  start() {
1729
- globalThis.addEventListener("message", (event) => this.handleMessage(event));
1741
+ addEventListener("message", (event) => this.handleMessage(event));
1730
1742
  }
1731
1743
  stop() {
1732
- globalThis.removeEventListener("message", this.handleMessage);
1744
+ removeEventListener("message", this.handleMessage);
1733
1745
  this.connections.length = 0;
1734
1746
  }
1735
1747
  handleMessage = (event) => {
1736
- if (event.origin !== globalThis.location.origin) return;
1748
+ if (event.origin !== browserWindow().location?.origin) return;
1737
1749
  if (!this.validateSessionId(event.data?.sessionId)) return;
1738
1750
  for (const connection of this.connections) {
1739
1751
  const { id, listener } = connection;
@@ -1794,20 +1806,21 @@ var PostMessageRpcTransport = class {
1794
1806
  const postMessageBus = new PostMessageBus(this.sessionId);
1795
1807
  const postMessageConnection = {
1796
1808
  listener: (event) => {
1797
- if ("data" in event.data && isJsonRpcResponse(event.data.data)) {
1798
- const response = event.data.data;
1799
- if (isJsonRpcSuccess(response) && Array.isArray(response.result)) {
1800
- const ret = schemas[method].result.from.parse(response.result[0]);
1801
- resolve(ret);
1802
- return;
1803
- }
1804
- if (isJsonRpcFailure(response)) {
1805
- this.logger?.error("Error in PostMessageRpcTransport", response);
1806
- reject(new Error(response.error.message));
1807
- return;
1808
- }
1809
- throw new Error(`Invalid response from wallet extension, ${JSON.stringify(event.data)}`);
1809
+ if (!("data" in event.data) || !isJsonRpcResponse(event.data.data)) {
1810
+ return;
1811
+ }
1812
+ const response = event.data.data;
1813
+ if (isJsonRpcSuccess(response) && Array.isArray(response.result)) {
1814
+ const ret = schemas[method].result.from.parse(response.result[0]);
1815
+ resolve(ret);
1816
+ return;
1810
1817
  }
1818
+ if (isJsonRpcFailure(response)) {
1819
+ this.logger?.error("Error in PostMessageRpcTransport", response);
1820
+ reject(new Error(response.error.message));
1821
+ return;
1822
+ }
1823
+ throw new Error(`Invalid response from wallet extension, ${JSON.stringify(event.data)}`);
1811
1824
  },
1812
1825
  id
1813
1826
  };
@@ -2070,25 +2083,32 @@ var JsonRpcBlockViewer = class extends JsonRpcBlockViewerMethods {
2070
2083
  };
2071
2084
  }
2072
2085
  async blockByHash(hash) {
2073
- return (await this.blocksByHash(hash, 1))[0];
2086
+ const blocks = await this.blocksByHash(hash, 1);
2087
+ return blocks[0];
2074
2088
  }
2075
2089
  async blockByNumber(block) {
2076
- return (await this.blocksByNumber(block, 1))[0];
2090
+ const blocks = await this.blocksByNumber(block, 1);
2091
+ return blocks[0];
2077
2092
  }
2078
2093
  async chainId(blockNumber = "latest") {
2079
2094
  if (blockNumber === "latest") {
2080
- return (await this.currentBlock())[0].chain;
2095
+ const current = await this.currentBlock();
2096
+ return current[0].chain;
2081
2097
  }
2082
- return (await this.blocksByNumber(blockNumber, 1))[0][0].chain;
2098
+ const blocks = await this.blocksByNumber(blockNumber, 1);
2099
+ return blocks[0][0].chain;
2083
2100
  }
2084
2101
  async currentBlockHash() {
2085
- return (await this.currentBlock())[0]._hash;
2102
+ const block = await this.currentBlock();
2103
+ return block[0]._hash;
2086
2104
  }
2087
2105
  async currentBlockNumber() {
2088
- return (await this.currentBlock())[0].block;
2106
+ const block = await this.currentBlock();
2107
+ return block[0].block;
2089
2108
  }
2090
2109
  async payloadByHash(hash) {
2091
- return (await this.payloadsByHash([hash]))[0] ?? null;
2110
+ const payloads = await this.payloadsByHash([hash]);
2111
+ return payloads[0] ?? null;
2092
2112
  }
2093
2113
  async rate(range, timeUnit) {
2094
2114
  return await calculateBlockRate(this, range, timeUnit);
@@ -2140,10 +2160,11 @@ var JsonRpcBlockViewer = class extends JsonRpcBlockViewerMethods {
2140
2160
  }, this.headPollIntervalMs);
2141
2161
  }
2142
2162
  stopHeadPolling() {
2143
- if (this._headPollTimer) {
2144
- clearInterval(this._headPollTimer);
2145
- this._headPollTimer = null;
2163
+ if (!this._headPollTimer) {
2164
+ return;
2146
2165
  }
2166
+ clearInterval(this._headPollTimer);
2167
+ this._headPollTimer = null;
2147
2168
  }
2148
2169
  };
2149
2170
  __publicField(JsonRpcBlockViewer, "defaultMoniker", BlockViewerMoniker2);
@@ -2176,16 +2197,20 @@ var JsonRpcFinalizationViewerMethods = class extends AbstractJsonRpcViewer {
2176
2197
  // src/provider/viewer/JsonRpcFinalizationViewer/JsonRpcFinalizationViewer.ts
2177
2198
  var JsonRpcFinalizationViewer = class extends JsonRpcFinalizationViewerMethods {
2178
2199
  async chainId() {
2179
- return (await this.headBlock()).chain;
2200
+ const headBlock = await this.headBlock();
2201
+ return headBlock.chain;
2180
2202
  }
2181
2203
  async headBlock() {
2182
- return (await this.head())[0];
2204
+ const head = await this.head();
2205
+ return head[0];
2183
2206
  }
2184
2207
  async headHash() {
2185
- return (await this.head())[0]._hash;
2208
+ const head = await this.head();
2209
+ return head[0]._hash;
2186
2210
  }
2187
2211
  async headNumber() {
2188
- return (await this.head())[0].block;
2212
+ const head = await this.head();
2213
+ return head[0].block;
2189
2214
  }
2190
2215
  };
2191
2216
  __publicField(JsonRpcFinalizationViewer, "defaultMoniker", FinalizationViewerMoniker2);
@@ -2602,28 +2627,33 @@ var JsonRpcXyoViewer = class extends AbstractJsonRpcViewer {
2602
2627
  return await this.account.balance.accountBalanceHistory(address, config);
2603
2628
  }
2604
2629
  async blockByHash(hash) {
2605
- return (await this.blocksByHash(hash, 1))[0];
2630
+ const blocks = await this.blocksByHash(hash, 1);
2631
+ return blocks[0];
2606
2632
  }
2607
2633
  async blockByNumber(blockNumber) {
2608
- return (await this.blocksByNumber(blockNumber, 1))[0];
2634
+ const blocks = await this.blocksByNumber(blockNumber, 1);
2635
+ return blocks[0];
2609
2636
  }
2610
2637
  async blocksByHash(hash, limit) {
2611
- return await Promise.all((await this.transport.sendRequest(
2638
+ const blocks = await this.transport.sendRequest(
2612
2639
  "xyoViewer_blocksByHash",
2613
2640
  [hash, limit]
2614
- )).map((b) => fixSignedHydratedBlockWithHashMeta(b)));
2641
+ );
2642
+ return await Promise.all(blocks.map((b) => fixSignedHydratedBlockWithHashMeta(b)));
2615
2643
  }
2616
2644
  async blocksByNumber(blockNumber, limit) {
2617
- return await Promise.all((await this.transport.sendRequest(
2645
+ const blocks = await this.transport.sendRequest(
2618
2646
  "xyoViewer_blocksByNumber",
2619
2647
  [blockNumber, limit]
2620
- )).map((b) => fixSignedHydratedBlockWithHashMeta(b)));
2648
+ );
2649
+ return await Promise.all(blocks.map((b) => fixSignedHydratedBlockWithHashMeta(b)));
2621
2650
  }
2622
2651
  async blocksByStep(stepLevel, stepIndex) {
2623
- return await Promise.all((await this.transport.sendRequest(
2652
+ const blocks = await this.transport.sendRequest(
2624
2653
  "xyoViewer_blocksByStep",
2625
2654
  [stepLevel, stepIndex]
2626
- )).map((b) => fixSignedHydratedBlockWithHashMeta(b)));
2655
+ );
2656
+ return await Promise.all(blocks.map((b) => fixSignedHydratedBlockWithHashMeta(b)));
2627
2657
  }
2628
2658
  async chainId(blockNumber = "latest") {
2629
2659
  const block = blockNumber === "latest" ? await this.currentBlock() : await this.blockByNumber(blockNumber);
@@ -2656,10 +2686,12 @@ var JsonRpcXyoViewer = class extends AbstractJsonRpcViewer {
2656
2686
  return fixSignedHydratedBlockWithHashMeta(result);
2657
2687
  }
2658
2688
  async currentBlockHash() {
2659
- return (await this.currentBlock())[0]._hash;
2689
+ const block = await this.currentBlock();
2690
+ return block[0]._hash;
2660
2691
  }
2661
2692
  async currentBlockNumber() {
2662
- return (await this.currentBlock())[0].block;
2693
+ const block = await this.currentBlock();
2694
+ return block[0].block;
2663
2695
  }
2664
2696
  async forkHistory() {
2665
2697
  return await this.transport.sendRequest("xyoViewer_forkHistory");
@@ -2719,7 +2751,8 @@ var JsonRpcXyoViewer = class extends AbstractJsonRpcViewer {
2719
2751
  return await this.transport.sendRequest("xyoViewer_networkStakeStepRewardsForStepLevel", [stepLevel, range]);
2720
2752
  }
2721
2753
  async payloadByHash(hash) {
2722
- return (await this.payloadsByHash([hash]))[0] ?? null;
2754
+ const payloads = await this.payloadsByHash([hash]);
2755
+ return payloads[0] ?? null;
2723
2756
  }
2724
2757
  async payloadsByHash(hashes) {
2725
2758
  return await PayloadBuilder2.addHashMeta(await this.transport.sendRequest(