@canton-network/core-rpc-transport 0.2.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 +106 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +101 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var uuid = require('uuid');
|
|
4
|
+
var coreTypes = require('@canton-network/core-types');
|
|
5
|
+
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
9
|
+
var jsonRpcRequest = (id, payload) => {
|
|
10
|
+
return {
|
|
11
|
+
jsonrpc: "2.0",
|
|
12
|
+
id,
|
|
13
|
+
// id should be set based on the request context
|
|
14
|
+
...payload
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
var jsonRpcResponse = (id, payload) => {
|
|
18
|
+
return {
|
|
19
|
+
jsonrpc: "2.0",
|
|
20
|
+
id,
|
|
21
|
+
// id should be set based on the request context
|
|
22
|
+
...payload
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
var WindowTransport = class {
|
|
26
|
+
constructor(win) {
|
|
27
|
+
this.win = win;
|
|
28
|
+
__publicField(this, "submit", async (payload) => {
|
|
29
|
+
const message = {
|
|
30
|
+
request: jsonRpcRequest(uuid.v4(), payload),
|
|
31
|
+
type: coreTypes.WalletEvent.SPLICE_WALLET_REQUEST
|
|
32
|
+
};
|
|
33
|
+
this.win.postMessage(message, "*");
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const listener = (event) => {
|
|
36
|
+
if (!coreTypes.isSpliceMessageEvent(event) || event.data.type !== coreTypes.WalletEvent.SPLICE_WALLET_RESPONSE || event.data.response.id !== message.request.id) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
window.removeEventListener("message", listener);
|
|
40
|
+
if ("error" in event.data.response) {
|
|
41
|
+
reject(event.data.response.error);
|
|
42
|
+
} else {
|
|
43
|
+
resolve(event.data.response);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
window.addEventListener("message", listener);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
__publicField(this, "submitResponse", (id, payload) => {
|
|
50
|
+
const message = {
|
|
51
|
+
response: jsonRpcResponse(id, payload),
|
|
52
|
+
type: coreTypes.WalletEvent.SPLICE_WALLET_RESPONSE
|
|
53
|
+
};
|
|
54
|
+
this.win.postMessage(message, "*");
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var HttpTransport = class {
|
|
59
|
+
constructor(url, accessToken) {
|
|
60
|
+
this.url = url;
|
|
61
|
+
this.accessToken = accessToken;
|
|
62
|
+
}
|
|
63
|
+
async handleErrorResponse(response) {
|
|
64
|
+
const body = await response.text();
|
|
65
|
+
if (coreTypes.ErrorResponse.safeParse(JSON.parse(body)).success) {
|
|
66
|
+
throw JSON.parse(body);
|
|
67
|
+
} else {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`HTTP request failed: ${response.status}, text: ${await response.text()} `
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async submit(payload) {
|
|
74
|
+
const request = {
|
|
75
|
+
jsonrpc: "2.0",
|
|
76
|
+
method: payload.method,
|
|
77
|
+
params: payload.params,
|
|
78
|
+
id: uuid.v4()
|
|
79
|
+
};
|
|
80
|
+
const header = this.accessToken ? { Authorization: `Bearer ${this.accessToken}` } : void 0;
|
|
81
|
+
const response = await fetch(this.url.href, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
...header,
|
|
85
|
+
"Content-Type": "application/json"
|
|
86
|
+
},
|
|
87
|
+
body: JSON.stringify(request)
|
|
88
|
+
});
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
return this.handleErrorResponse(response);
|
|
91
|
+
}
|
|
92
|
+
const json = await response.json();
|
|
93
|
+
const parsed = coreTypes.ResponsePayload.parse(json);
|
|
94
|
+
if ("error" in parsed) {
|
|
95
|
+
throw parsed;
|
|
96
|
+
}
|
|
97
|
+
return parsed;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
exports.HttpTransport = HttpTransport;
|
|
102
|
+
exports.WindowTransport = WindowTransport;
|
|
103
|
+
exports.jsonRpcRequest = jsonRpcRequest;
|
|
104
|
+
exports.jsonRpcResponse = jsonRpcResponse;
|
|
105
|
+
//# sourceMappingURL=index.cjs.map
|
|
106
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["uuidv4","WalletEvent","isSpliceMessageEvent","ErrorResponse","ResponsePayload"],"mappings":";;;;;;;;AAgBO,IAAM,cAAA,GAAiB,CAC1B,EAAA,EACA,OAAA,KACiB;AACjB,EAAA,OAAO;AAAA,IACH,OAAA,EAAS,KAAA;AAAA,IACT,EAAA;AAAA;AAAA,IACA,GAAG;AAAA,GACP;AACJ;AAEO,IAAM,eAAA,GAAkB,CAC3B,EAAA,EACA,OAAA,KACkB;AAClB,EAAA,OAAO;AAAA,IACH,OAAA,EAAS,KAAA;AAAA,IACT,EAAA;AAAA;AAAA,IACA,GAAG;AAAA,GACP;AACJ;AAMO,IAAM,kBAAN,MAA8C;AAAA,EACjD,YAAoB,GAAA,EAAa;AAAb,IAAA,IAAA,CAAA,GAAA,GAAA,GAAA;AAEpB,IAAA,aAAA,CAAA,IAAA,EAAA,QAAA,EAAS,OAAO,OAAA,KAA4B;AACxC,MAAA,MAAM,OAAA,GAAyB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AA,EAAI;AACd,MAAA,OAAO,IAAA,CAAK,oBAAoB,QAAQ,CAAA;AAAA,IAC5C;AAEA,IAAA,MAAM,IAAA,GAAO,MAAM,QAAA,CAAS,IAAA,EAAK;AACjC,IAAA,MAAM,MAAA,GAASI,yBAAA,CAAgB,KAAA,CAAM,IAAI,CAAA;AAEzC,IAAA,IAAI,WAAW,MAAA,EAAQ;AACnB,MAAA,MAAM,MAAA;AAAA,IACV;AAEA,IAAA,OAAO,MAAA;AAAA,EACX;AACJ","file":"index.cjs","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 RequestPayload,\n ResponsePayload,\n JsonRpcRequest,\n SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n JsonRpcResponse,\n} from '@canton-network/core-types'\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 submit: (payload: RequestPayload) => Promise<ResponsePayload>\n}\n\nexport class WindowTransport implements RpcTransport {\n constructor(private win: Window) {}\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(uuidv4(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\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(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 // if the response uses the RPC error format, throw it as is\n if (ErrorResponse.safeParse(JSON.parse(body)).success) {\n throw JSON.parse(body)\n } else {\n throw new Error(\n `HTTP request failed: ${response.status}, text: ${await response.text()} `\n )\n }\n }\n\n async submit(payload: RequestPayload): Promise<ResponsePayload> {\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 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const parsed = ResponsePayload.parse(json)\n\n if ('error' in parsed) {\n throw parsed\n }\n\n return parsed\n }\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { RequestPayload, ResponsePayload, JsonRpcRequest, JsonRpcResponse } from '@canton-network/core-types';
|
|
2
|
+
export declare const jsonRpcRequest: (id: string | number | null, payload: RequestPayload) => JsonRpcRequest;
|
|
3
|
+
export declare const jsonRpcResponse: (id: string | number | null, payload: ResponsePayload) => JsonRpcResponse;
|
|
4
|
+
export interface RpcTransport {
|
|
5
|
+
submit: (payload: RequestPayload) => Promise<ResponsePayload>;
|
|
6
|
+
}
|
|
7
|
+
export declare class WindowTransport implements RpcTransport {
|
|
8
|
+
private win;
|
|
9
|
+
constructor(win: Window);
|
|
10
|
+
submit: (payload: RequestPayload) => Promise<{
|
|
11
|
+
result?: unknown;
|
|
12
|
+
}>;
|
|
13
|
+
submitResponse: (id: string | number | null, payload: ResponsePayload) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare class HttpTransport implements RpcTransport {
|
|
16
|
+
private url;
|
|
17
|
+
private accessToken?;
|
|
18
|
+
constructor(url: URL, accessToken?: string | undefined);
|
|
19
|
+
protected handleErrorResponse(response: Response): Promise<never>;
|
|
20
|
+
submit(payload: RequestPayload): Promise<ResponsePayload>;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EACH,cAAc,EACd,eAAe,EACf,cAAc,EAMd,eAAe,EAClB,MAAM,4BAA4B,CAAA;AAEnC,eAAO,MAAM,cAAc,GACvB,IAAI,MAAM,GAAG,MAAM,GAAG,IAAI,EAC1B,SAAS,cAAc,KACxB,cAMF,CAAA;AAED,eAAO,MAAM,eAAe,GACxB,IAAI,MAAM,GAAG,MAAM,GAAG,IAAI,EAC1B,SAAS,eAAe,KACzB,eAMF,CAAA;AAED,MAAM,WAAW,YAAY;IACzB,MAAM,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;CAChE;AAED,qBAAa,eAAgB,YAAW,YAAY;IACpC,OAAO,CAAC,GAAG;gBAAH,GAAG,EAAE,MAAM;IAE/B,MAAM,GAAU,SAAS,cAAc;;OA4BtC;IAED,cAAc,GAAI,IAAI,MAAM,GAAG,MAAM,GAAG,IAAI,EAAE,SAAS,eAAe,UAMrE;CACJ;AAED,qBAAa,aAAc,YAAW,YAAY;IAE1C,OAAO,CAAC,GAAG;IACX,OAAO,CAAC,WAAW,CAAC;gBADZ,GAAG,EAAE,GAAG,EACR,WAAW,CAAC,EAAE,MAAM,YAAA;cAGhB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC;IAajE,MAAM,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC;CAkClE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { v4 } from 'uuid';
|
|
2
|
+
import { ErrorResponse, ResponsePayload, WalletEvent, isSpliceMessageEvent } from '@canton-network/core-types';
|
|
3
|
+
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
var jsonRpcRequest = (id, payload) => {
|
|
8
|
+
return {
|
|
9
|
+
jsonrpc: "2.0",
|
|
10
|
+
id,
|
|
11
|
+
// id should be set based on the request context
|
|
12
|
+
...payload
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
var jsonRpcResponse = (id, payload) => {
|
|
16
|
+
return {
|
|
17
|
+
jsonrpc: "2.0",
|
|
18
|
+
id,
|
|
19
|
+
// id should be set based on the request context
|
|
20
|
+
...payload
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
var WindowTransport = class {
|
|
24
|
+
constructor(win) {
|
|
25
|
+
this.win = win;
|
|
26
|
+
__publicField(this, "submit", async (payload) => {
|
|
27
|
+
const message = {
|
|
28
|
+
request: jsonRpcRequest(v4(), payload),
|
|
29
|
+
type: WalletEvent.SPLICE_WALLET_REQUEST
|
|
30
|
+
};
|
|
31
|
+
this.win.postMessage(message, "*");
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const listener = (event) => {
|
|
34
|
+
if (!isSpliceMessageEvent(event) || event.data.type !== WalletEvent.SPLICE_WALLET_RESPONSE || event.data.response.id !== message.request.id) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
window.removeEventListener("message", listener);
|
|
38
|
+
if ("error" in event.data.response) {
|
|
39
|
+
reject(event.data.response.error);
|
|
40
|
+
} else {
|
|
41
|
+
resolve(event.data.response);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
window.addEventListener("message", listener);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
__publicField(this, "submitResponse", (id, payload) => {
|
|
48
|
+
const message = {
|
|
49
|
+
response: jsonRpcResponse(id, payload),
|
|
50
|
+
type: WalletEvent.SPLICE_WALLET_RESPONSE
|
|
51
|
+
};
|
|
52
|
+
this.win.postMessage(message, "*");
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var HttpTransport = class {
|
|
57
|
+
constructor(url, accessToken) {
|
|
58
|
+
this.url = url;
|
|
59
|
+
this.accessToken = accessToken;
|
|
60
|
+
}
|
|
61
|
+
async handleErrorResponse(response) {
|
|
62
|
+
const body = await response.text();
|
|
63
|
+
if (ErrorResponse.safeParse(JSON.parse(body)).success) {
|
|
64
|
+
throw JSON.parse(body);
|
|
65
|
+
} else {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`HTTP request failed: ${response.status}, text: ${await response.text()} `
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async submit(payload) {
|
|
72
|
+
const request = {
|
|
73
|
+
jsonrpc: "2.0",
|
|
74
|
+
method: payload.method,
|
|
75
|
+
params: payload.params,
|
|
76
|
+
id: v4()
|
|
77
|
+
};
|
|
78
|
+
const header = this.accessToken ? { Authorization: `Bearer ${this.accessToken}` } : void 0;
|
|
79
|
+
const response = await fetch(this.url.href, {
|
|
80
|
+
method: "POST",
|
|
81
|
+
headers: {
|
|
82
|
+
...header,
|
|
83
|
+
"Content-Type": "application/json"
|
|
84
|
+
},
|
|
85
|
+
body: JSON.stringify(request)
|
|
86
|
+
});
|
|
87
|
+
if (!response.ok) {
|
|
88
|
+
return this.handleErrorResponse(response);
|
|
89
|
+
}
|
|
90
|
+
const json = await response.json();
|
|
91
|
+
const parsed = ResponsePayload.parse(json);
|
|
92
|
+
if ("error" in parsed) {
|
|
93
|
+
throw parsed;
|
|
94
|
+
}
|
|
95
|
+
return parsed;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export { HttpTransport, WindowTransport, jsonRpcRequest, jsonRpcResponse };
|
|
100
|
+
//# sourceMappingURL=index.js.map
|
|
101
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["uuidv4"],"mappings":";;;;;;AAgBO,IAAM,cAAA,GAAiB,CAC1B,EAAA,EACA,OAAA,KACiB;AACjB,EAAA,OAAO;AAAA,IACH,OAAA,EAAS,KAAA;AAAA,IACT,EAAA;AAAA;AAAA,IACA,GAAG;AAAA,GACP;AACJ;AAEO,IAAM,eAAA,GAAkB,CAC3B,EAAA,EACA,OAAA,KACkB;AAClB,EAAA,OAAO;AAAA,IACH,OAAA,EAAS,KAAA;AAAA,IACT,EAAA;AAAA;AAAA,IACA,GAAG;AAAA,GACP;AACJ;AAMO,IAAM,kBAAN,MAA8C;AAAA,EACjD,YAAoB,GAAA,EAAa;AAAb,IAAA,IAAA,CAAA,GAAA,GAAA,GAAA;AAEpB,IAAA,aAAA,CAAA,IAAA,EAAA,QAAA,EAAS,OAAO,OAAA,KAA4B;AACxC,MAAA,MAAM,OAAA,GAAyB;AAAA,QAC3B,OAAA,EAAS,cAAA,CAAeA,EAAA,EAAO,EAAG,OAAO,CAAA;AAAA,QACzC,MAAM,WAAA,CAAY;AAAA,OACtB;AAEA,MAAA,IAAA,CAAK,GAAA,CAAI,WAAA,CAAY,OAAA,EAAS,GAAG,CAAA;AAEjC,MAAA,OAAO,IAAI,OAAA,CAAyB,CAAC,OAAA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nB,MAAA,MAAM,MAAA;AAAA,IACV;AAEA,IAAA,OAAO,MAAA;AAAA,EACX;AACJ","file":"index.js","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 RequestPayload,\n ResponsePayload,\n JsonRpcRequest,\n SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n JsonRpcResponse,\n} from '@canton-network/core-types'\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 submit: (payload: RequestPayload) => Promise<ResponsePayload>\n}\n\nexport class WindowTransport implements RpcTransport {\n constructor(private win: Window) {}\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(uuidv4(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\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(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 // if the response uses the RPC error format, throw it as is\n if (ErrorResponse.safeParse(JSON.parse(body)).success) {\n throw JSON.parse(body)\n } else {\n throw new Error(\n `HTTP request failed: ${response.status}, text: ${await response.text()} `\n )\n }\n }\n\n async submit(payload: RequestPayload): Promise<ResponsePayload> {\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 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const parsed = ResponsePayload.parse(json)\n\n if ('error' in parsed) {\n throw parsed\n }\n\n return parsed\n }\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@canton-network/core-rpc-transport",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "RPC transport implementations",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"author": "Digital Asset (Switzerland) GmbH and/or its affiliates",
|
|
8
|
+
"packageManager": "yarn@4.9.4",
|
|
9
|
+
"main": "./dist/index.cjs",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup && tsc -p tsconfig.types.json",
|
|
22
|
+
"dev": "tsup --watch --onSuccess \"tsc -p tsconfig.types.json\"",
|
|
23
|
+
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
|
|
24
|
+
"clean": "tsc -b --clean; rm -rf dist"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@canton-network/core-types": "^0.16.0",
|
|
28
|
+
"uuid": "^11.1.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"tsup": "^8.5.1",
|
|
32
|
+
"typescript": "^5.9.3"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist/**"
|
|
36
|
+
],
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/hyperledger-labs/splice-wallet-kernel.git",
|
|
43
|
+
"directory": "core/rpc-transport"
|
|
44
|
+
}
|
|
45
|
+
}
|