@canton-network/core-rpc-transport 1.15.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 +5 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +5 -10
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let _canton_network_core_types = require("@canton-network/core-types");
|
|
3
3
|
let _canton_network_core_rpc_errors = require("@canton-network/core-rpc-errors");
|
|
4
|
-
|
|
4
|
+
// \0@oxc-project+runtime@0.155.0/helpers/esm/typeof.js
|
|
5
5
|
function _typeof(o) {
|
|
6
6
|
"@babel/helpers - typeof";
|
|
7
7
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -10,8 +10,7 @@ function _typeof(o) {
|
|
|
10
10
|
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
11
11
|
}, _typeof(o);
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPrimitive.js
|
|
13
|
+
// \0@oxc-project+runtime@0.155.0/helpers/esm/toPrimitive.js
|
|
15
14
|
function toPrimitive(t, r) {
|
|
16
15
|
if ("object" != _typeof(t) || !t) return t;
|
|
17
16
|
var e = t[Symbol.toPrimitive];
|
|
@@ -22,14 +21,12 @@ function toPrimitive(t, r) {
|
|
|
22
21
|
}
|
|
23
22
|
return ("string" === r ? String : Number)(t);
|
|
24
23
|
}
|
|
25
|
-
|
|
26
|
-
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPropertyKey.js
|
|
24
|
+
// \0@oxc-project+runtime@0.155.0/helpers/esm/toPropertyKey.js
|
|
27
25
|
function toPropertyKey(t) {
|
|
28
26
|
var i = toPrimitive(t, "string");
|
|
29
27
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
30
28
|
}
|
|
31
|
-
|
|
32
|
-
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/defineProperty.js
|
|
29
|
+
// \0@oxc-project+runtime@0.155.0/helpers/esm/defineProperty.js
|
|
33
30
|
function _defineProperty(e, r, t) {
|
|
34
31
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
35
32
|
value: t,
|
|
@@ -38,8 +35,7 @@ function _defineProperty(e, r, t) {
|
|
|
38
35
|
writable: !0
|
|
39
36
|
}) : e[r] = t, e;
|
|
40
37
|
}
|
|
41
|
-
|
|
42
|
-
//#region src/index.ts
|
|
38
|
+
// src/index.ts
|
|
43
39
|
const jsonRpcRequest = (id, payload) => {
|
|
44
40
|
return {
|
|
45
41
|
jsonrpc: "2.0",
|
|
@@ -173,7 +169,6 @@ var HttpTransport = class {
|
|
|
173
169
|
return _canton_network_core_types.SuccessResponse.parse(json);
|
|
174
170
|
}
|
|
175
171
|
};
|
|
176
|
-
//#endregion
|
|
177
172
|
exports.HttpTransport = HttpTransport;
|
|
178
173
|
exports.WindowTransport = WindowTransport;
|
|
179
174
|
exports.jsonRpcRequest = jsonRpcRequest;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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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IACL,GAAG;IACH,QAAQ;IACR,gBAAgB;GACpB;GACA,MAAM,KAAK,UAAU,OAAO;EAChC,CAAC,CAAC,CAAC,OAAO,UAAmB;GACzB,OAAA,GAAMF,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,CAAC;KAC9D;KACA,KAAK,IAAI,iBAAiB,WAAW,KAAK,oBAAoB;IAClE;IACA,aAAa;KACT,KAAK,q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C,OAAOI,2BAAAA,gBAAgB,MAAM,IAAI;CACrC;AACJ"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ErrorResponse, SuccessResponse, WalletEvent, isSpliceMessageEvent } from "@canton-network/core-types";
|
|
2
2
|
import { providerErrors, rpcErrors, toJsonRpcError } from "@canton-network/core-rpc-errors";
|
|
3
|
-
|
|
3
|
+
// \0@oxc-project+runtime@0.155.0/helpers/esm/typeof.js
|
|
4
4
|
function _typeof(o) {
|
|
5
5
|
"@babel/helpers - typeof";
|
|
6
6
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -9,8 +9,7 @@ function _typeof(o) {
|
|
|
9
9
|
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
10
10
|
}, _typeof(o);
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPrimitive.js
|
|
12
|
+
// \0@oxc-project+runtime@0.155.0/helpers/esm/toPrimitive.js
|
|
14
13
|
function toPrimitive(t, r) {
|
|
15
14
|
if ("object" != _typeof(t) || !t) return t;
|
|
16
15
|
var e = t[Symbol.toPrimitive];
|
|
@@ -21,14 +20,12 @@ function toPrimitive(t, r) {
|
|
|
21
20
|
}
|
|
22
21
|
return ("string" === r ? String : Number)(t);
|
|
23
22
|
}
|
|
24
|
-
|
|
25
|
-
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPropertyKey.js
|
|
23
|
+
// \0@oxc-project+runtime@0.155.0/helpers/esm/toPropertyKey.js
|
|
26
24
|
function toPropertyKey(t) {
|
|
27
25
|
var i = toPrimitive(t, "string");
|
|
28
26
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
29
27
|
}
|
|
30
|
-
|
|
31
|
-
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/defineProperty.js
|
|
28
|
+
// \0@oxc-project+runtime@0.155.0/helpers/esm/defineProperty.js
|
|
32
29
|
function _defineProperty(e, r, t) {
|
|
33
30
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
34
31
|
value: t,
|
|
@@ -37,8 +34,7 @@ function _defineProperty(e, r, t) {
|
|
|
37
34
|
writable: !0
|
|
38
35
|
}) : e[r] = t, e;
|
|
39
36
|
}
|
|
40
|
-
|
|
41
|
-
//#region src/index.ts
|
|
37
|
+
// src/index.ts
|
|
42
38
|
const jsonRpcRequest = (id, payload) => {
|
|
43
39
|
return {
|
|
44
40
|
jsonrpc: "2.0",
|
|
@@ -172,7 +168,6 @@ var HttpTransport = class {
|
|
|
172
168
|
return SuccessResponse.parse(json);
|
|
173
169
|
}
|
|
174
170
|
};
|
|
175
|
-
//#endregion
|
|
176
171
|
export { HttpTransport, WindowTransport, jsonRpcRequest, jsonRpcResponse };
|
|
177
172
|
|
|
178
173
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,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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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AAW,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.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "RPC transport implementations",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@canton-network/core-rpc-errors": "1.
|
|
22
|
-
"@canton-network/core-types": "2.
|
|
21
|
+
"@canton-network/core-rpc-errors": "1.15.0",
|
|
22
|
+
"@canton-network/core-types": "2.2.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@vitest/browser-playwright": "^5.0.5",
|
|
26
26
|
"@vitest/coverage-v8": "^5.0.5",
|
|
27
27
|
"playwright": "^1.64.1",
|
|
28
|
-
"tsdown": "^0.23.
|
|
28
|
+
"tsdown": "^0.23.2",
|
|
29
29
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
30
30
|
"vitest": "^5.0.5"
|
|
31
31
|
},
|