@canton-network/core-rpc-transport 1.14.0 → 1.16.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.
package/dist/index.cjs CHANGED
@@ -1,8 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- let uuid = require("uuid");
3
2
  let _canton_network_core_types = require("@canton-network/core-types");
4
3
  let _canton_network_core_rpc_errors = require("@canton-network/core-rpc-errors");
5
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/typeof.js
4
+ // \0@oxc-project+runtime@0.155.0/helpers/esm/typeof.js
6
5
  function _typeof(o) {
7
6
  "@babel/helpers - typeof";
8
7
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -11,8 +10,7 @@ function _typeof(o) {
11
10
  return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
12
11
  }, _typeof(o);
13
12
  }
14
- //#endregion
15
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPrimitive.js
13
+ // \0@oxc-project+runtime@0.155.0/helpers/esm/toPrimitive.js
16
14
  function toPrimitive(t, r) {
17
15
  if ("object" != _typeof(t) || !t) return t;
18
16
  var e = t[Symbol.toPrimitive];
@@ -23,14 +21,12 @@ function toPrimitive(t, r) {
23
21
  }
24
22
  return ("string" === r ? String : Number)(t);
25
23
  }
26
- //#endregion
27
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPropertyKey.js
24
+ // \0@oxc-project+runtime@0.155.0/helpers/esm/toPropertyKey.js
28
25
  function toPropertyKey(t) {
29
26
  var i = toPrimitive(t, "string");
30
27
  return "symbol" == _typeof(i) ? i : i + "";
31
28
  }
32
- //#endregion
33
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/defineProperty.js
29
+ // \0@oxc-project+runtime@0.155.0/helpers/esm/defineProperty.js
34
30
  function _defineProperty(e, r, t) {
35
31
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
36
32
  value: t,
@@ -39,8 +35,7 @@ function _defineProperty(e, r, t) {
39
35
  writable: !0
40
36
  }) : e[r] = t, e;
41
37
  }
42
- //#endregion
43
- //#region src/index.ts
38
+ // src/index.ts
44
39
  const jsonRpcRequest = (id, payload) => {
45
40
  return {
46
41
  jsonrpc: "2.0",
@@ -93,7 +88,7 @@ var WindowTransport = class {
93
88
  );
94
89
  _defineProperty(this, "submit", async (payload) => {
95
90
  const message = {
96
- request: jsonRpcRequest((0, uuid.v4)(), payload),
91
+ request: jsonRpcRequest(crypto.randomUUID(), payload),
97
92
  type: _canton_network_core_types.WalletEvent.SPLICE_WALLET_REQUEST,
98
93
  target: this.options.target
99
94
  };
@@ -153,7 +148,7 @@ var HttpTransport = class {
153
148
  jsonrpc: "2.0",
154
149
  method: payload.method,
155
150
  params: payload.params,
156
- id: (0, uuid.v4)()
151
+ id: crypto.randomUUID()
157
152
  };
158
153
  const header = this.accessToken ? { Authorization: `Bearer ${this.accessToken}` } : void 0;
159
154
  const response = await fetch(this.url.href, {
@@ -174,7 +169,6 @@ var HttpTransport = class {
174
169
  return _canton_network_core_types.SuccessResponse.parse(json);
175
170
  }
176
171
  };
177
- //#endregion
178
172
  exports.HttpTransport = HttpTransport;
179
173
  exports.WindowTransport = WindowTransport;
180
174
  exports.jsonRpcRequest = jsonRpcRequest;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["isSpliceMessageEvent","WalletEvent","uuidv4","toJsonRpcError","ErrorResponse","rpcErrors","providerErrors","SuccessResponse"],"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { v4 as uuidv4 } from 'uuid'\nimport {\n type RequestPayload,\n type ResponsePayload,\n type JsonRpcRequest,\n type SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n type JsonRpcResponse,\n} from '@canton-network/core-types'\nimport {\n providerErrors,\n rpcErrors,\n toJsonRpcError,\n} from '@canton-network/core-rpc-errors'\n\nexport const jsonRpcRequest = (\n id: string | number | null,\n payload: RequestPayload\n): JsonRpcRequest => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport const jsonRpcResponse = (\n id: string | number | null,\n payload: ResponsePayload\n): JsonRpcResponse => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport interface RpcTransport {\n /** Rejects with a `JsonRpcError` when the wallet answers with an error. */\n submit: (payload: RequestPayload) => Promise<SuccessResponse>\n}\n\n/**\n * Handler for wallet-pushed JSON-RPC notifications (CIP-103 events such as\n * txChanged / accountsChanged / statusChanged / connected).\n */\nexport type NotificationHandler = (method: string, params?: unknown) => void\n\nexport type WindowTransportOptions = {\n /**\n * Optional routing key for browser-extension messaging. When set, extensions\n * should ignore messages that do not match their own identifier.\n */\n target?: string | undefined\n}\n\nexport class WindowTransport implements RpcTransport {\n private notificationHandlers = new Set<NotificationHandler>()\n private notificationListener: ((event: MessageEvent) => void) | undefined\n\n constructor(\n private win: Window,\n private options: WindowTransportOptions = {}\n ) {}\n\n /**\n * Subscribe to wallet-pushed JSON-RPC notifications: SPLICE_WALLET_REQUEST\n * frames with no `id` (JSON-RPC 2.0 §4.1). The window listener must be\n * always-on for the transport's lifetime — CIP-103 events arrive between\n * requests, where a per-request listener can never observe them.\n */\n onNotification = (handler: NotificationHandler): (() => void) => {\n this.notificationHandlers.add(handler)\n if (!this.notificationListener) {\n this.notificationListener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_REQUEST\n ) {\n return\n }\n // Requests submitted by this transport always carry a uuid id,\n // so an id-less request can only be a wallet-originated\n // notification — this also keeps the dApp's own outgoing\n // frames (posted to the same window) out of the event path.\n if (event.data.request.id != null) {\n return\n }\n // Same gating as extension detection: a transport constructed\n // from an announced provider's routing key only accepts frames\n // stamped with that key, so multi-wallet pages do not\n // cross-deliver events between providers.\n if (\n this.options.target &&\n event.data.target !== this.options.target\n ) {\n return\n }\n const { method, params } = event.data.request\n this.notificationHandlers.forEach((h) => h(method, params))\n }\n this.win.addEventListener('message', this.notificationListener)\n }\n return () => {\n this.notificationHandlers.delete(handler)\n // Drop the DOM listener with the last subscriber so an abandoned\n // transport leaves nothing attached to the window; a later\n // subscribe re-installs it.\n if (\n this.notificationHandlers.size === 0 &&\n this.notificationListener\n ) {\n this.win.removeEventListener(\n 'message',\n this.notificationListener\n )\n this.notificationListener = undefined\n }\n }\n }\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(uuidv4(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\n target: this.options.target,\n }\n\n this.win.postMessage(message, '*')\n\n return new Promise<SuccessResponse>((resolve, reject) => {\n const listener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_RESPONSE ||\n event.data.response.id !== message.request.id\n ) {\n return\n }\n\n window.removeEventListener('message', listener)\n if ('error' in event.data.response) {\n reject(toJsonRpcError(event.data.response.error))\n } else {\n resolve(event.data.response)\n }\n }\n\n window.addEventListener('message', listener)\n })\n }\n\n submitResponse = (id: string | number | null, payload: ResponsePayload) => {\n const message: SpliceMessage = {\n response: jsonRpcResponse(id, payload),\n type: WalletEvent.SPLICE_WALLET_RESPONSE,\n }\n this.win.postMessage(message, '*')\n }\n}\n\nexport class HttpTransport implements RpcTransport {\n constructor(\n private url: URL,\n private accessToken?: string\n ) {}\n\n protected async handleErrorResponse(response: Response): Promise<never> {\n const body = await response.text()\n\n let parsedBody: unknown\n try {\n parsedBody = JSON.parse(body)\n } catch {\n // not JSON, fall back to the HTTP status below\n }\n const rpcError = ErrorResponse.safeParse(parsedBody)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n const details = {\n message: response.statusText || `HTTP ${response.status}`,\n data: body,\n }\n switch (response.status) {\n case 400:\n throw rpcErrors.invalidRequest(details)\n case 401:\n case 403:\n throw providerErrors.unauthorized(details)\n case 404:\n throw rpcErrors.resourceNotFound(details)\n case 413:\n case 429:\n throw rpcErrors.limitExceeded(details)\n case 408:\n case 502:\n case 503:\n case 504:\n throw rpcErrors.resourceUnavailable(details)\n default:\n throw rpcErrors.internal(details)\n }\n }\n\n async submit(payload: RequestPayload): Promise<SuccessResponse> {\n const request: JsonRpcRequest = {\n jsonrpc: '2.0',\n method: payload.method,\n params: payload.params,\n id: uuidv4(),\n }\n\n const header = this.accessToken\n ? { Authorization: `Bearer ${this.accessToken}` }\n : undefined\n\n const response = await fetch(this.url.href, {\n method: 'POST',\n headers: {\n ...header,\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n }).catch((error: unknown) => {\n throw toJsonRpcError(error)\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const rpcError = ErrorResponse.safeParse(json)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n return SuccessResponse.parse(json)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBA,MAAa,kBACT,IACA,YACiB;CACjB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAEA,MAAa,mBACT,IACA,YACkB;CAClB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAqBA,IAAa,kBAAb,MAAqD;CAIjD,YACI,KACA,UAA0C,CAAC,GAC7C;EAFU,KAAA,MAAA;EACA,KAAA,UAAA;EALJ,gBAAA,MAAA,wCAAuB,IAAI,IAAyB,CAAA;EACpD,gBAAA,MAAA,wBAAA,KAAA,CAAA;;;;;;;;;GAaR;IAAkB,YAA+C;IAC7D,KAAK,qBAAqB,IAAI,OAAO;IACrC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AAK,cAAc,IAC9C,KAAA;EAEN,MAAM,WAAW,MAAM,MAAM,KAAK,IAAI,MAAM;GACxC,QAAQ;GACR,SAAS;IACL,GAAG;IACH,QAAQ;IACR,gBAAgB;GACpB;GACA,MAAM,KAAK,UAAU,OAAO;EAChC,CAAC,CAAC,CAAC,OAAO,UAAmB;GACzB,OAAA,GAAMC,gCAAAA,eAAAA,CAAe,KAAK;EAC9B,CAAC;EAED,IAAI,CAAC,SAAS,IACV,OAAO,KAAK,oBAAoB,QAAQ;EAG5C,MAAM,OAAO,MAAM,SAAS,KAAK;EACjC,MAAM,WAAWC,2BAAAA,cAAc,UAAU,IAAI;EAC7C,IAAI,SAAS,SACT,OAAA,GAAMD,gCAAAA,eAAAA,CAAe,SAAS,KAAK,KAAK;EAG5C,OAAOI,2BAAAA,gBAAgB,MAAM,IAAI;CACrC;AACJ"}
1
+ {"version":3,"file":"index.cjs","names":["isSpliceMessageEvent","WalletEvent","toJsonRpcError","ErrorResponse","rpcErrors","providerErrors","SuccessResponse"],"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport {\n type RequestPayload,\n type ResponsePayload,\n type JsonRpcRequest,\n type SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n type JsonRpcResponse,\n} from '@canton-network/core-types'\nimport {\n providerErrors,\n rpcErrors,\n toJsonRpcError,\n} from '@canton-network/core-rpc-errors'\n\nexport const jsonRpcRequest = (\n id: string | number | null,\n payload: RequestPayload\n): JsonRpcRequest => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport const jsonRpcResponse = (\n id: string | number | null,\n payload: ResponsePayload\n): JsonRpcResponse => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport interface RpcTransport {\n /** Rejects with a `JsonRpcError` when the wallet answers with an error. */\n submit: (payload: RequestPayload) => Promise<SuccessResponse>\n}\n\n/**\n * Handler for wallet-pushed JSON-RPC notifications (CIP-103 events such as\n * txChanged / accountsChanged / statusChanged / connected).\n */\nexport type NotificationHandler = (method: string, params?: unknown) => void\n\nexport type WindowTransportOptions = {\n /**\n * Optional routing key for browser-extension messaging. When set, extensions\n * should ignore messages that do not match their own identifier.\n */\n target?: string | undefined\n}\n\nexport class WindowTransport implements RpcTransport {\n private notificationHandlers = new Set<NotificationHandler>()\n private notificationListener: ((event: MessageEvent) => void) | undefined\n\n constructor(\n private win: Window,\n private options: WindowTransportOptions = {}\n ) {}\n\n /**\n * Subscribe to wallet-pushed JSON-RPC notifications: SPLICE_WALLET_REQUEST\n * frames with no `id` (JSON-RPC 2.0 §4.1). The window listener must be\n * always-on for the transport's lifetime — CIP-103 events arrive between\n * requests, where a per-request listener can never observe them.\n */\n onNotification = (handler: NotificationHandler): (() => void) => {\n this.notificationHandlers.add(handler)\n if (!this.notificationListener) {\n this.notificationListener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_REQUEST\n ) {\n return\n }\n // Requests submitted by this transport always carry a uuid id,\n // so an id-less request can only be a wallet-originated\n // notification — this also keeps the dApp's own outgoing\n // frames (posted to the same window) out of the event path.\n if (event.data.request.id != null) {\n return\n }\n // Same gating as extension detection: a transport constructed\n // from an announced provider's routing key only accepts frames\n // stamped with that key, so multi-wallet pages do not\n // cross-deliver events between providers.\n if (\n this.options.target &&\n event.data.target !== this.options.target\n ) {\n return\n }\n const { method, params } = event.data.request\n this.notificationHandlers.forEach((h) => h(method, params))\n }\n this.win.addEventListener('message', this.notificationListener)\n }\n return () => {\n this.notificationHandlers.delete(handler)\n // Drop the DOM listener with the last subscriber so an abandoned\n // transport leaves nothing attached to the window; a later\n // subscribe re-installs it.\n if (\n this.notificationHandlers.size === 0 &&\n this.notificationListener\n ) {\n this.win.removeEventListener(\n 'message',\n this.notificationListener\n )\n this.notificationListener = undefined\n }\n }\n }\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(crypto.randomUUID(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\n target: this.options.target,\n }\n\n this.win.postMessage(message, '*')\n\n return new Promise<SuccessResponse>((resolve, reject) => {\n const listener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_RESPONSE ||\n event.data.response.id !== message.request.id\n ) {\n return\n }\n\n window.removeEventListener('message', listener)\n if ('error' in event.data.response) {\n reject(toJsonRpcError(event.data.response.error))\n } else {\n resolve(event.data.response)\n }\n }\n\n window.addEventListener('message', listener)\n })\n }\n\n submitResponse = (id: string | number | null, payload: ResponsePayload) => {\n const message: SpliceMessage = {\n response: jsonRpcResponse(id, payload),\n type: WalletEvent.SPLICE_WALLET_RESPONSE,\n }\n this.win.postMessage(message, '*')\n }\n}\n\nexport class HttpTransport implements RpcTransport {\n constructor(\n private url: URL,\n private accessToken?: string\n ) {}\n\n protected async handleErrorResponse(response: Response): Promise<never> {\n const body = await response.text()\n\n let parsedBody: unknown\n try {\n parsedBody = JSON.parse(body)\n } catch {\n // not JSON, fall back to the HTTP status below\n }\n const rpcError = ErrorResponse.safeParse(parsedBody)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n const details = {\n message: response.statusText || `HTTP ${response.status}`,\n data: body,\n }\n switch (response.status) {\n case 400:\n throw rpcErrors.invalidRequest(details)\n case 401:\n case 403:\n throw providerErrors.unauthorized(details)\n case 404:\n throw rpcErrors.resourceNotFound(details)\n case 413:\n case 429:\n throw rpcErrors.limitExceeded(details)\n case 408:\n case 502:\n case 503:\n case 504:\n throw rpcErrors.resourceUnavailable(details)\n default:\n throw rpcErrors.internal(details)\n }\n }\n\n async submit(payload: RequestPayload): Promise<SuccessResponse> {\n const request: JsonRpcRequest = {\n jsonrpc: '2.0',\n method: payload.method,\n params: payload.params,\n id: crypto.randomUUID(),\n }\n\n const header = this.accessToken\n ? { Authorization: `Bearer ${this.accessToken}` }\n : undefined\n\n const response = await fetch(this.url.href, {\n method: 'POST',\n headers: {\n ...header,\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n }).catch((error: unknown) => {\n throw toJsonRpcError(error)\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const rpcError = ErrorResponse.safeParse(json)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n return SuccessResponse.parse(json)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,MAAa,kBACT,IACA,YACiB;CACjB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAEA,MAAa,mBACT,IACA,YACkB;CAClB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAqBA,IAAa,kBAAb,MAAqD;CAIjD,YACI,KACA,UAA0C,CAAC,GAC7C;EAFU,KAAA,MAAA;EACA,KAAA,UAAA;EALJ,gBAAA,MAAA,wCAAuB,IAAI,IAAyB,CAAA;EACpD,gBAAA,MAAA,wBAAA,KAAA,CAAA;;;;;;;;;GAaR;IAAkB,YAA+C;IAC7D,KAAK,qBAAqB,IAAI,OAAO;IACrC,IAAI,CAAC,KAAK,sBAAsB;KAC5B,KAAK,wBAAwB,UAAwB;MACjD,IACI,EAAA,GAACA,2BAAAA,qBAAAA,CAAqB,KAAK,KAC3B,MAAM,KAAK,SAASC,2BAAAA,YAAY,uBAEhC;MAMJ,IAAI,MAAM,KAAK,QAAQ,MAAM,MACzB;MAMJ,IACI,KAAK,QAAQ,UACb,MAAM,KAAK,WAAW,KAAK,QAAQ,QAEnC;MAEJ,MAAM,EAAE,QAAQ,WAAW,MAAM,KAAK;MACtC,KAAK,qBAAqB,SAAS,MAAM,EAAE,QAAQ,MAAM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IAEzE,YACY,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,sBAA2B,EAC5C;IAEJ;;;;;OAKG;IACH,cAAc,YAAa,mBAAmB,KAAG,CAAC,MAAM,IAAI,CAAC,CAgD5D;IAED,MAAM,YAAmB,cAAc;;OA6BtC;IAED,cAAc,OAAQ,MAAM,GAAG,MAAM,GAAG,IAAI,WAAW,eAAe,UAMrE;CACJ;AAED,qBAAa,aAAc,YAAW,YAAY;IAE1C,OAAO,CAAC,GAAG;IACX,OAAO,CAAC,WAAW,CAAC;IAFxB,YACY,GAAG,EAAE,GAAG,EACR,WAAW,CAAC,EAAE,MAAM,YAAA,EAC5B;IAEJ,UAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,CAqCtE;IAEK,MAAM,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CAmC9D;CACJ"}
package/dist/index.js CHANGED
@@ -1,7 +1,6 @@
1
- import { v4 } from "uuid";
2
1
  import { ErrorResponse, SuccessResponse, WalletEvent, isSpliceMessageEvent } from "@canton-network/core-types";
3
2
  import { providerErrors, rpcErrors, toJsonRpcError } from "@canton-network/core-rpc-errors";
4
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/typeof.js
3
+ // \0@oxc-project+runtime@0.155.0/helpers/esm/typeof.js
5
4
  function _typeof(o) {
6
5
  "@babel/helpers - typeof";
7
6
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -10,8 +9,7 @@ function _typeof(o) {
10
9
  return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
11
10
  }, _typeof(o);
12
11
  }
13
- //#endregion
14
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPrimitive.js
12
+ // \0@oxc-project+runtime@0.155.0/helpers/esm/toPrimitive.js
15
13
  function toPrimitive(t, r) {
16
14
  if ("object" != _typeof(t) || !t) return t;
17
15
  var e = t[Symbol.toPrimitive];
@@ -22,14 +20,12 @@ function toPrimitive(t, r) {
22
20
  }
23
21
  return ("string" === r ? String : Number)(t);
24
22
  }
25
- //#endregion
26
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPropertyKey.js
23
+ // \0@oxc-project+runtime@0.155.0/helpers/esm/toPropertyKey.js
27
24
  function toPropertyKey(t) {
28
25
  var i = toPrimitive(t, "string");
29
26
  return "symbol" == _typeof(i) ? i : i + "";
30
27
  }
31
- //#endregion
32
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/defineProperty.js
28
+ // \0@oxc-project+runtime@0.155.0/helpers/esm/defineProperty.js
33
29
  function _defineProperty(e, r, t) {
34
30
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
35
31
  value: t,
@@ -38,8 +34,7 @@ function _defineProperty(e, r, t) {
38
34
  writable: !0
39
35
  }) : e[r] = t, e;
40
36
  }
41
- //#endregion
42
- //#region src/index.ts
37
+ // src/index.ts
43
38
  const jsonRpcRequest = (id, payload) => {
44
39
  return {
45
40
  jsonrpc: "2.0",
@@ -92,7 +87,7 @@ var WindowTransport = class {
92
87
  );
93
88
  _defineProperty(this, "submit", async (payload) => {
94
89
  const message = {
95
- request: jsonRpcRequest(v4(), payload),
90
+ request: jsonRpcRequest(crypto.randomUUID(), payload),
96
91
  type: WalletEvent.SPLICE_WALLET_REQUEST,
97
92
  target: this.options.target
98
93
  };
@@ -152,7 +147,7 @@ var HttpTransport = class {
152
147
  jsonrpc: "2.0",
153
148
  method: payload.method,
154
149
  params: payload.params,
155
- id: v4()
150
+ id: crypto.randomUUID()
156
151
  };
157
152
  const header = this.accessToken ? { Authorization: `Bearer ${this.accessToken}` } : void 0;
158
153
  const response = await fetch(this.url.href, {
@@ -173,7 +168,6 @@ var HttpTransport = class {
173
168
  return SuccessResponse.parse(json);
174
169
  }
175
170
  };
176
- //#endregion
177
171
  export { HttpTransport, WindowTransport, jsonRpcRequest, jsonRpcResponse };
178
172
 
179
173
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["uuidv4"],"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { v4 as uuidv4 } from 'uuid'\nimport {\n type RequestPayload,\n type ResponsePayload,\n type JsonRpcRequest,\n type SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n type JsonRpcResponse,\n} from '@canton-network/core-types'\nimport {\n providerErrors,\n rpcErrors,\n toJsonRpcError,\n} from '@canton-network/core-rpc-errors'\n\nexport const jsonRpcRequest = (\n id: string | number | null,\n payload: RequestPayload\n): JsonRpcRequest => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport const jsonRpcResponse = (\n id: string | number | null,\n payload: ResponsePayload\n): JsonRpcResponse => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport interface RpcTransport {\n /** Rejects with a `JsonRpcError` when the wallet answers with an error. */\n submit: (payload: RequestPayload) => Promise<SuccessResponse>\n}\n\n/**\n * Handler for wallet-pushed JSON-RPC notifications (CIP-103 events such as\n * txChanged / accountsChanged / statusChanged / connected).\n */\nexport type NotificationHandler = (method: string, params?: unknown) => void\n\nexport type WindowTransportOptions = {\n /**\n * Optional routing key for browser-extension messaging. When set, extensions\n * should ignore messages that do not match their own identifier.\n */\n target?: string | undefined\n}\n\nexport class WindowTransport implements RpcTransport {\n private notificationHandlers = new Set<NotificationHandler>()\n private notificationListener: ((event: MessageEvent) => void) | undefined\n\n constructor(\n private win: Window,\n private options: WindowTransportOptions = {}\n ) {}\n\n /**\n * Subscribe to wallet-pushed JSON-RPC notifications: SPLICE_WALLET_REQUEST\n * frames with no `id` (JSON-RPC 2.0 §4.1). The window listener must be\n * always-on for the transport's lifetime — CIP-103 events arrive between\n * requests, where a per-request listener can never observe them.\n */\n onNotification = (handler: NotificationHandler): (() => void) => {\n this.notificationHandlers.add(handler)\n if (!this.notificationListener) {\n this.notificationListener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_REQUEST\n ) {\n return\n }\n // Requests submitted by this transport always carry a uuid id,\n // so an id-less request can only be a wallet-originated\n // notification — this also keeps the dApp's own outgoing\n // frames (posted to the same window) out of the event path.\n if (event.data.request.id != null) {\n return\n }\n // Same gating as extension detection: a transport constructed\n // from an announced provider's routing key only accepts frames\n // stamped with that key, so multi-wallet pages do not\n // cross-deliver events between providers.\n if (\n this.options.target &&\n event.data.target !== this.options.target\n ) {\n return\n }\n const { method, params } = event.data.request\n this.notificationHandlers.forEach((h) => h(method, params))\n }\n this.win.addEventListener('message', this.notificationListener)\n }\n return () => {\n this.notificationHandlers.delete(handler)\n // Drop the DOM listener with the last subscriber so an abandoned\n // transport leaves nothing attached to the window; a later\n // subscribe re-installs it.\n if (\n this.notificationHandlers.size === 0 &&\n this.notificationListener\n ) {\n this.win.removeEventListener(\n 'message',\n this.notificationListener\n )\n this.notificationListener = undefined\n }\n }\n }\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(uuidv4(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\n target: this.options.target,\n }\n\n this.win.postMessage(message, '*')\n\n return new Promise<SuccessResponse>((resolve, reject) => {\n const listener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_RESPONSE ||\n event.data.response.id !== message.request.id\n ) {\n return\n }\n\n window.removeEventListener('message', listener)\n if ('error' in event.data.response) {\n reject(toJsonRpcError(event.data.response.error))\n } else {\n resolve(event.data.response)\n }\n }\n\n window.addEventListener('message', listener)\n })\n }\n\n submitResponse = (id: string | number | null, payload: ResponsePayload) => {\n const message: SpliceMessage = {\n response: jsonRpcResponse(id, payload),\n type: WalletEvent.SPLICE_WALLET_RESPONSE,\n }\n this.win.postMessage(message, '*')\n }\n}\n\nexport class HttpTransport implements RpcTransport {\n constructor(\n private url: URL,\n private accessToken?: string\n ) {}\n\n protected async handleErrorResponse(response: Response): Promise<never> {\n const body = await response.text()\n\n let parsedBody: unknown\n try {\n parsedBody = JSON.parse(body)\n } catch {\n // not JSON, fall back to the HTTP status below\n }\n const rpcError = ErrorResponse.safeParse(parsedBody)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n const details = {\n message: response.statusText || `HTTP ${response.status}`,\n data: body,\n }\n switch (response.status) {\n case 400:\n throw rpcErrors.invalidRequest(details)\n case 401:\n case 403:\n throw providerErrors.unauthorized(details)\n case 404:\n throw rpcErrors.resourceNotFound(details)\n case 413:\n case 429:\n throw rpcErrors.limitExceeded(details)\n case 408:\n case 502:\n case 503:\n case 504:\n throw rpcErrors.resourceUnavailable(details)\n default:\n throw rpcErrors.internal(details)\n }\n }\n\n async submit(payload: RequestPayload): Promise<SuccessResponse> {\n const request: JsonRpcRequest = {\n jsonrpc: '2.0',\n method: payload.method,\n params: payload.params,\n id: uuidv4(),\n }\n\n const header = this.accessToken\n ? { Authorization: `Bearer ${this.accessToken}` }\n : undefined\n\n const response = await fetch(this.url.href, {\n method: 'POST',\n headers: {\n ...header,\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n }).catch((error: unknown) => {\n throw toJsonRpcError(error)\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const rpcError = ErrorResponse.safeParse(json)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n return SuccessResponse.parse(json)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBA,MAAa,kBACT,IACA,YACiB;CACjB,OAAO;EACH,SAAS;EACT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5B,SAAS;GACT,QAAQ,QAAQ;GAChB,QAAQ,QAAQ;GAChB,IAAIA,GAAO;EACf;EAEA,MAAM,SAAS,KAAK,cACd,EAAE,eAAe,UAAU,KAAK,cAAc,IAC9C,KAAA;EAEN,MAAM,WAAW,MAAM,MAAM,KAAK,IAAI,MAAM;GACxC,QAAQ;GACR,SAAS;IACL,GAAG;IACH,QAAQ;IACR,gBAAgB;GACpB;GACA,MAAM,KAAK,UAAU,OAAO;EAChC,CAAC,CAAC,CAAC,OAAO,UAAmB;GACzB,MAAM,eAAe,KAAK;EAC9B,CAAC;EAED,IAAI,CAAC,SAAS,IACV,OAAO,KAAK,oBAAoB,QAAQ;EAG5C,MAAM,OAAO,MAAM,SAAS,KAAK;EACjC,MAAM,WAAW,cAAc,UAAU,IAAI;EAC7C,IAAI,SAAS,SACT,MAAM,eAAe,SAAS,KAAK,KAAK;EAG5C,OAAO,gBAAgB,MAAM,IAAI;CACrC;AACJ"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport {\n type RequestPayload,\n type ResponsePayload,\n type JsonRpcRequest,\n type SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n type JsonRpcResponse,\n} from '@canton-network/core-types'\nimport {\n providerErrors,\n rpcErrors,\n toJsonRpcError,\n} from '@canton-network/core-rpc-errors'\n\nexport const jsonRpcRequest = (\n id: string | number | null,\n payload: RequestPayload\n): JsonRpcRequest => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport const jsonRpcResponse = (\n id: string | number | null,\n payload: ResponsePayload\n): JsonRpcResponse => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport interface RpcTransport {\n /** Rejects with a `JsonRpcError` when the wallet answers with an error. */\n submit: (payload: RequestPayload) => Promise<SuccessResponse>\n}\n\n/**\n * Handler for wallet-pushed JSON-RPC notifications (CIP-103 events such as\n * txChanged / accountsChanged / statusChanged / connected).\n */\nexport type NotificationHandler = (method: string, params?: unknown) => void\n\nexport type WindowTransportOptions = {\n /**\n * Optional routing key for browser-extension messaging. When set, extensions\n * should ignore messages that do not match their own identifier.\n */\n target?: string | undefined\n}\n\nexport class WindowTransport implements RpcTransport {\n private notificationHandlers = new Set<NotificationHandler>()\n private notificationListener: ((event: MessageEvent) => void) | undefined\n\n constructor(\n private win: Window,\n private options: WindowTransportOptions = {}\n ) {}\n\n /**\n * Subscribe to wallet-pushed JSON-RPC notifications: SPLICE_WALLET_REQUEST\n * frames with no `id` (JSON-RPC 2.0 §4.1). The window listener must be\n * always-on for the transport's lifetime — CIP-103 events arrive between\n * requests, where a per-request listener can never observe them.\n */\n onNotification = (handler: NotificationHandler): (() => void) => {\n this.notificationHandlers.add(handler)\n if (!this.notificationListener) {\n this.notificationListener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_REQUEST\n ) {\n return\n }\n // Requests submitted by this transport always carry a uuid id,\n // so an id-less request can only be a wallet-originated\n // notification — this also keeps the dApp's own outgoing\n // frames (posted to the same window) out of the event path.\n if (event.data.request.id != null) {\n return\n }\n // Same gating as extension detection: a transport constructed\n // from an announced provider's routing key only accepts frames\n // stamped with that key, so multi-wallet pages do not\n // cross-deliver events between providers.\n if (\n this.options.target &&\n event.data.target !== this.options.target\n ) {\n return\n }\n const { method, params } = event.data.request\n this.notificationHandlers.forEach((h) => h(method, params))\n }\n this.win.addEventListener('message', this.notificationListener)\n }\n return () => {\n this.notificationHandlers.delete(handler)\n // Drop the DOM listener with the last subscriber so an abandoned\n // transport leaves nothing attached to the window; a later\n // subscribe re-installs it.\n if (\n this.notificationHandlers.size === 0 &&\n this.notificationListener\n ) {\n this.win.removeEventListener(\n 'message',\n this.notificationListener\n )\n this.notificationListener = undefined\n }\n }\n }\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(crypto.randomUUID(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\n target: this.options.target,\n }\n\n this.win.postMessage(message, '*')\n\n return new Promise<SuccessResponse>((resolve, reject) => {\n const listener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_RESPONSE ||\n event.data.response.id !== message.request.id\n ) {\n return\n }\n\n window.removeEventListener('message', listener)\n if ('error' in event.data.response) {\n reject(toJsonRpcError(event.data.response.error))\n } else {\n resolve(event.data.response)\n }\n }\n\n window.addEventListener('message', listener)\n })\n }\n\n submitResponse = (id: string | number | null, payload: ResponsePayload) => {\n const message: SpliceMessage = {\n response: jsonRpcResponse(id, payload),\n type: WalletEvent.SPLICE_WALLET_RESPONSE,\n }\n this.win.postMessage(message, '*')\n }\n}\n\nexport class HttpTransport implements RpcTransport {\n constructor(\n private url: URL,\n private accessToken?: string\n ) {}\n\n protected async handleErrorResponse(response: Response): Promise<never> {\n const body = await response.text()\n\n let parsedBody: unknown\n try {\n parsedBody = JSON.parse(body)\n } catch {\n // not JSON, fall back to the HTTP status below\n }\n const rpcError = ErrorResponse.safeParse(parsedBody)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n const details = {\n message: response.statusText || `HTTP ${response.status}`,\n data: body,\n }\n switch (response.status) {\n case 400:\n throw rpcErrors.invalidRequest(details)\n case 401:\n case 403:\n throw providerErrors.unauthorized(details)\n case 404:\n throw rpcErrors.resourceNotFound(details)\n case 413:\n case 429:\n throw rpcErrors.limitExceeded(details)\n case 408:\n case 502:\n case 503:\n case 504:\n throw rpcErrors.resourceUnavailable(details)\n default:\n throw rpcErrors.internal(details)\n }\n }\n\n async submit(payload: RequestPayload): Promise<SuccessResponse> {\n const request: JsonRpcRequest = {\n jsonrpc: '2.0',\n method: payload.method,\n params: payload.params,\n id: crypto.randomUUID(),\n }\n\n const header = this.accessToken\n ? { Authorization: `Bearer ${this.accessToken}` }\n : undefined\n\n const response = await fetch(this.url.href, {\n method: 'POST',\n headers: {\n ...header,\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n }).catch((error: unknown) => {\n throw toJsonRpcError(error)\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const rpcError = ErrorResponse.safeParse(json)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n return SuccessResponse.parse(json)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,MAAa,kBACT,IACA,YACiB;CACjB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAEA,MAAa,mBACT,IACA,YACkB;CAClB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAqBA,IAAa,kBAAb,MAAqD;CAIjD,YACI,KACA,UAA0C,CAAC,GAC7C;EAFU,KAAA,MAAA;EACA,KAAA,UAAA;EALJ,gBAAA,MAAA,wCAAuB,IAAI,IAAyB,CAAA;EACpD,gBAAA,MAAA,wBAAA,KAAA,CAAA;;;;;;;;;GAaR;IAAkB,YAA+C;IAC7D,KAAK,qBAAqB,IAAI,OAAO;IACrC,IAAI,CAAC,KAAK,sBAAsB;KAC5B,KAAK,wBAAwB,UAAwB;MACjD,IACI,CAAC,qBAAqB,KAAK,KAC3B,MAAM,KAAK,SAAS,YAAY,uBAEhC;MAMJ,IAAI,MAAM,KAAK,QAAQ,MAAM,MACzB;MAMJ,IACI,KAAK,QAAQ,UACb,MAAM,KAAK,WAAW,KAAK,QAAQ,QAEnC;MAEJ,MAAM,EAAE,QAAQ,WAAW,MAAM,KAAK;MACtC,KAAK,qBAAqB,SAAS,MAAM,EAAE,QAAQ,MAAM,CAAC;KAC9D;KACA,KAAK,IAAI,iBAAiB,WAAW,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AS,SACT,MAAM,eAAe,SAAS,KAAK,KAAK;EAG5C,MAAM,UAAU;GACZ,SAAS,SAAS,cAAc,QAAQ,SAAS;GACjD,MAAM;EACV;EACA,QAAQ,SAAS,QAAjB;GACI,KAAK,KACD,MAAM,UAAU,eAAe,OAAO;GAC1C,KAAK;GACL,KAAK,KACD,MAAM,eAAe,aAAa,OAAO;GAC7C,KAAK,KACD,MAAM,UAAU,iBAAiB,OAAO;GAC5C,KAAK;GACL,KAAK,KACD,MAAM,UAAU,cAAc,OAAO;GACzC,KAAK;GACL,KAAK;GACL,KAAK;GACL,KAAK,KACD,MAAM,UAAU,oBAAoB,OAAO;GAC/C,SACI,MAAM,UAAU,SAAS,OAAO;EACxC;CACJ;CAEA,MAAM,OAAO,SAAmD;EAC5D,MAAM,UAA0B;GAC5B,SAAS;GACT,QAAQ,QAAQ;GAChB,QAAQ,QAAQ;GAChB,IAAI,OAAO,WAAW;EAC1B;EAEA,MAAM,SAAS,KAAK,cACd,EAAE,eAAe,UAAU,KAAK,cAAc,IAC9C,KAAA;EAEN,MAAM,WAAW,MAAM,MAAM,KAAK,IAAI,MAAM;GACxC,QAAQ;GACR,SAAS;IACL,GAAG;IACH,QAAQ;IACR,gBAAgB;GACpB;GACA,MAAM,KAAK,UAAU,OAAO;EAChC,CAAC,CAAC,CAAC,OAAO,UAAmB;GACzB,MAAM,eAAe,KAAK;EAC9B,CAAC;EAED,IAAI,CAAC,SAAS,IACV,OAAO,KAAK,oBAAoB,QAAQ;EAG5C,MAAM,OAAO,MAAM,SAAS,KAAK;EACjC,MAAM,WAAW,cAAc,UAAU,IAAI;EAC7C,IAAI,SAAS,SACT,MAAM,eAAe,SAAS,KAAK,KAAK;EAG5C,OAAO,gBAAgB,MAAM,IAAI;CACrC;AACJ"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canton-network/core-rpc-transport",
3
- "version": "1.14.0",
3
+ "version": "1.16.0",
4
4
  "type": "module",
5
5
  "description": "RPC transport implementations",
6
6
  "license": "Apache-2.0",
@@ -18,17 +18,16 @@
18
18
  }
19
19
  },
20
20
  "dependencies": {
21
- "@canton-network/core-rpc-errors": "1.13.0",
22
- "@canton-network/core-types": "1.14.0",
23
- "uuid": "^14.0.1"
21
+ "@canton-network/core-rpc-errors": "1.15.0",
22
+ "@canton-network/core-types": "2.2.0"
24
23
  },
25
24
  "devDependencies": {
26
- "@vitest/browser-playwright": "^5.0.1",
27
- "@vitest/coverage-v8": "^5.0.1",
28
- "playwright": "^1.63.0",
29
- "tsdown": "^0.22.9",
30
- "typescript": "^6.0.3",
31
- "vitest": "^5.0.1"
25
+ "@vitest/browser-playwright": "^5.0.5",
26
+ "@vitest/coverage-v8": "^5.0.5",
27
+ "playwright": "^1.64.1",
28
+ "tsdown": "^0.23.2",
29
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
30
+ "vitest": "^5.0.5"
32
31
  },
33
32
  "files": [
34
33
  "dist/**"
@@ -46,9 +45,9 @@
46
45
  "url": "https://github.com/canton-network/wallet/issues"
47
46
  },
48
47
  "scripts": {
49
- "build": "tsdown --onSuccess \"tsc\"",
50
- "dev": "tsdown --watch --onSuccess \"tsc\"",
48
+ "build": "tsdown --onSuccess \"tsc -p tsconfig.lib.json\"",
49
+ "dev": "tsdown --watch --onSuccess \"tsc -p tsconfig.lib.json\"",
51
50
  "flatpack": "pnpm pack --pack-destination \"$FLATPACK_OUTDIR\"",
52
- "clean": "tsc -b --clean; rm -rf dist"
51
+ "clean": "rm -rf dist"
53
52
  }
54
53
  }