From c214d5085592aecc9ac22f7fe3a57f81687d7b30 Mon Sep 17 00:00:00 2001 From: ilbertt Date: Thu, 13 Mar 2025 21:48:13 +0100 Subject: [PATCH 1/6] fix: change default export to named export to avoid double default export issue --- src/ic-websocket.test.ts | 2 +- src/ic-websocket.ts | 2 +- src/index.ts | 7 +------ 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/ic-websocket.test.ts b/src/ic-websocket.test.ts index 28a16c0..982043b 100644 --- a/src/ic-websocket.test.ts +++ b/src/ic-websocket.test.ts @@ -5,7 +5,7 @@ import { CallRequest, Cbor } from "@dfinity/agent"; import { IDL } from "@dfinity/candid"; import { Principal } from "@dfinity/principal"; -import IcWebSocket, { COMMUNICATION_LATENCY_BOUND_MS, createWsConfig } from "./ic-websocket"; +import { IcWebSocket, COMMUNICATION_LATENCY_BOUND_MS, createWsConfig } from "./ic-websocket"; import { generateRandomIdentity } from "./identity"; import { CanisterWsMessageArguments, diff --git a/src/ic-websocket.ts b/src/ic-websocket.ts index 53c83b7..71fa690 100644 --- a/src/ic-websocket.ts +++ b/src/ic-websocket.ts @@ -90,7 +90,7 @@ export const createWsConfig = (c: IcWebSocketCon type WsParameters = ConstructorParameters; -export default class IcWebSocket< +export class IcWebSocket< S extends _WS_CANISTER_SERVICE, ApplicationMessageType = GetApplicationMessageType > { diff --git a/src/index.ts b/src/index.ts index 93c47bf..6b99f6d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,3 @@ -// import default member -import IcWebSocket from './ic-websocket'; -// re-export non-default members +// Export all named exports from ic-websocket export * from './ic-websocket'; export * from './identity'; - -// re-export default member -export default IcWebSocket; From 441b6dca75e08af7e48145ee11cbb702f2353454 Mon Sep 17 00:00:00 2001 From: ilbertt Date: Thu, 13 Mar 2025 22:13:53 +0100 Subject: [PATCH 2/6] chore: remove comment --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 6b99f6d..6f95a0b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,2 @@ -// Export all named exports from ic-websocket export * from './ic-websocket'; export * from './identity'; From 8244ce76f3e90de63b89fd74b929913b7faf79b2 Mon Sep 17 00:00:00 2001 From: ilbertt Date: Thu, 13 Mar 2025 22:24:10 +0100 Subject: [PATCH 3/6] fix: certificate validity in tests --- src/ic-websocket.test.ts | 4 ++-- src/test/constants.ts | 7 +++++++ src/utils.test.ts | 17 ++++++++--------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/ic-websocket.test.ts b/src/ic-websocket.test.ts index 982043b..c10c7fb 100644 --- a/src/ic-websocket.test.ts +++ b/src/ic-websocket.test.ts @@ -38,7 +38,7 @@ import { getTestCanisterActorWrongArgs, getTestCanisterActorWrongOpt, } from "./test/actor"; -import { GATEWAY_PRINCIPAL, LOCAL_REPLICA_ROOT_KEY } from "./test/constants"; +import { GATEWAY_PRINCIPAL, LOCAL_REPLICA_ROOT_KEY, MAX_CERTIFICATE_AGE_IN_MINUTES } from "./test/constants"; const wsGatewayAddress = "ws://127.0.0.1:8080"; // the canister from which the application message was sent (needed to verify the message certificate) @@ -51,7 +51,7 @@ const icWebsocketConfig = createWsConfig({ canisterActor: testCanisterActor, networkUrl: icNetworkUrl, identity: generateRandomIdentity(), - maxCertificateAgeInMinutes: 60 * 24 * 365, // 1 year. Since we're using pre-generated certificates, we need to set it really far in the future + maxCertificateAgeInMinutes: MAX_CERTIFICATE_AGE_IN_MINUTES, }); //// Mock Servers diff --git a/src/test/constants.ts b/src/test/constants.ts index 7630c4e..617a3b6 100644 --- a/src/test/constants.ts +++ b/src/test/constants.ts @@ -4,3 +4,10 @@ import { Principal } from "@dfinity/principal"; export const GATEWAY_PRINCIPAL = Principal.fromText("sqdfl-mr4km-2hfjy-gajqo-xqvh7-hf4mf-nra4i-3it6l-neaw4-soolw-tae"); export const LOCAL_REPLICA_ROOT_KEY = fromHex("d9d9f7a66e69635f6170695f76657273696f6e66302e31382e3068726f6f745f6b65795885308182301d060d2b0601040182dc7c0503010201060c2b0601040182dc7c050302010361008005229d89a17c6f9ec403a4b1a8aa103fc48055046c95f1e60ee2fbfb0bb23ab21617a93f48b99b1199ac89008cf3cf0a83e9da35f5cf27d0d51535ceff89c43ee236c31c3a7865cc6b333194ad3f7155b2931a7ffec2066777dffb20f277ca6c696d706c5f76657273696f6e65302e382e3069696d706c5f68617368784064613931633732316637386462393433346561336630303437383939383836346439313731346538626561363862333963633736326662306263383937313662757265706c6963615f6865616c74685f737461747573676865616c746879706365727469666965645f68656967687418d4"); + +const YEAR_IN_MINUTES = 365 * 24 * 60; +/* + * The max age of the certificate (5 years). + * Since we're using pre-generated certificates, we need to set it really far in the future. + */ +export const MAX_CERTIFICATE_AGE_IN_MINUTES = 5 * YEAR_IN_MINUTES; diff --git a/src/utils.test.ts b/src/utils.test.ts index 912b1ca..16184db 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -3,6 +3,7 @@ import { Principal } from "@dfinity/principal"; import { isMessageBodyValid, randomBigInt, safeExecute } from "./utils"; import { ClientIncomingMessage } from "./types"; import logger from "./logger"; +import { MAX_CERTIFICATE_AGE_IN_MINUTES } from "./test/constants"; // the canister from which the correct data were generated const canisterId = Principal.fromText("bnz7o-iuaaa-aaaaa-qaaaa-cai"); @@ -15,8 +16,6 @@ const messageToVerify: ClientIncomingMessage = { cert: new Uint8Array(fromHex("d9d9f7a2647472656583018301830183024863616e6973746572830183024a8000000000100000010183018301830183024e6365727469666965645f646174618203582087be17d7ba688bc4fb13d61f3d8d5642df92536e40da98facba7ba0450ea59e082045820f8d20e36feb79f8495eb4c632b7a04171599957c8c267f55b2156d89b5c1e424820458200d3dc76c69e69f12678a2e9a5a6859579ceb28350608e69f1902055650edaf7f820458209e5663705fa61a6ca53ee4a61daa4621e8ece0febd99b334d0ae625aad9f3f6e8204582077d28a3053cd3845a065a879ce36849add41cae9e6a56d452e328194b38e15ec82045820932e7cd3d24b95ff6c0fea6086fc624ee43b0875101777e089ba915ef7ded93d82045820fbb733f900879885afed4ace8eb90b19245f8386e7537769c072e9fded13c6ad830182045820ae29f371a7ae2a4af8bb125ef23486745500f8cb31a02c35cc7155053b67cce683024474696d6582034992da96e7ae90a2ba17697369676e61747572655830932828f169fdce98969c417666f38002e2826081deb756e94c73091a1b7c0455f6c2f3ccc509ab576c8e6f8753b7d85b")), tree: new Uint8Array(fromHex("d9d9f7830249776562736f636b657483025854737164666c2d6d72346b6d2d3268666a792d67616a716f2d78717668372d6866346d662d6e726134692d336974366c2d6e656177342d736f6f6c772d7461655f303030303030303030303030303030303030303082035820215b2aae42ccf90c6fd928fec029d0b9308e97d0c40f8772100a30097b004bb5")), }; -// the max age of the certificate. Since we're using pre-generated certificates, we need to set it really far in the future -const maxCertificateAgeInMinutes = 5 * 60 * 24 * 365; // 5 years const agent = HttpAgent.createSync(); agent.rootKey = localReplicaRootKey; @@ -30,7 +29,7 @@ describe("Utils of the IcWebSocket", () => { messageToVerify.cert, messageToVerify.tree, agent, - maxCertificateAgeInMinutes, + MAX_CERTIFICATE_AGE_IN_MINUTES, ); expect(isValid).toBe(true); @@ -44,7 +43,7 @@ describe("Utils of the IcWebSocket", () => { messageToVerify.cert, messageToVerify.tree, agent, - maxCertificateAgeInMinutes, + MAX_CERTIFICATE_AGE_IN_MINUTES, )).rejects.toThrow("Could not find certified data for this canister in the certificate."); }); @@ -56,7 +55,7 @@ describe("Utils of the IcWebSocket", () => { messageToVerify.cert, messageToVerify.tree, agent, - maxCertificateAgeInMinutes, + MAX_CERTIFICATE_AGE_IN_MINUTES, ); expect(isValid).toBe(false); @@ -70,7 +69,7 @@ describe("Utils of the IcWebSocket", () => { messageToVerify.cert, messageToVerify.tree, agent, - maxCertificateAgeInMinutes, + MAX_CERTIFICATE_AGE_IN_MINUTES, ); expect(isValid).toBe(false); @@ -84,7 +83,7 @@ describe("Utils of the IcWebSocket", () => { new Uint8Array([0, 1, 2, 3]), messageToVerify.tree, agent, - maxCertificateAgeInMinutes, + MAX_CERTIFICATE_AGE_IN_MINUTES, ); expect(isValid).toBe(false); @@ -99,7 +98,7 @@ describe("Utils of the IcWebSocket", () => { // another tree, valid but not the same as the one used to generate the data fromHex("d9d9f7830249776562736f636b657483025854737164666c2d6d72346b6d2d3268666a792d67616a716f2d78717668372d6866346d662d6e726134692d336974366c2d6e656177342d736f6f6c772d7461655f3030303030303030303030303030303030303030820358200a621494d244cd4426e1f3c2ac6942bb21a5312f613f534e6da5182571757403"), agent, - maxCertificateAgeInMinutes, + MAX_CERTIFICATE_AGE_IN_MINUTES, ); expect(isValid).toBe(false); @@ -114,7 +113,7 @@ describe("Utils of the IcWebSocket", () => { messageToVerify.tree, // the default agent uses the mainnet replica root key new HttpAgent(), - maxCertificateAgeInMinutes, + MAX_CERTIFICATE_AGE_IN_MINUTES, ); expect(isValid).toBe(false); From 202ab252b29cffde4854d80e8695352db9d0c9e9 Mon Sep 17 00:00:00 2001 From: ilbertt Date: Thu, 13 Mar 2025 22:25:53 +0100 Subject: [PATCH 4/6] docs: fix jsdoc --- src/test/constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/constants.ts b/src/test/constants.ts index 617a3b6..df424f2 100644 --- a/src/test/constants.ts +++ b/src/test/constants.ts @@ -6,7 +6,7 @@ export const GATEWAY_PRINCIPAL = Principal.fromText("sqdfl-mr4km-2hfjy-gajqo-xqv export const LOCAL_REPLICA_ROOT_KEY = fromHex("d9d9f7a66e69635f6170695f76657273696f6e66302e31382e3068726f6f745f6b65795885308182301d060d2b0601040182dc7c0503010201060c2b0601040182dc7c050302010361008005229d89a17c6f9ec403a4b1a8aa103fc48055046c95f1e60ee2fbfb0bb23ab21617a93f48b99b1199ac89008cf3cf0a83e9da35f5cf27d0d51535ceff89c43ee236c31c3a7865cc6b333194ad3f7155b2931a7ffec2066777dffb20f277ca6c696d706c5f76657273696f6e65302e382e3069696d706c5f68617368784064613931633732316637386462393433346561336630303437383939383836346439313731346538626561363862333963633736326662306263383937313662757265706c6963615f6865616c74685f737461747573676865616c746879706365727469666965645f68656967687418d4"); const YEAR_IN_MINUTES = 365 * 24 * 60; -/* +/** * The max age of the certificate (5 years). * Since we're using pre-generated certificates, we need to set it really far in the future. */ From 8f7680c1e5895e80114041b1d1a61be12697d496 Mon Sep 17 00:00:00 2001 From: ilbertt Date: Thu, 13 Mar 2025 22:42:03 +0100 Subject: [PATCH 5/6] refactor: do not use default exports --- src/ic-websocket.ts | 2 +- src/logger.ts | 2 +- src/utils.test.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ic-websocket.ts b/src/ic-websocket.ts index 71fa690..afc253f 100644 --- a/src/ic-websocket.ts +++ b/src/ic-websocket.ts @@ -20,7 +20,7 @@ import { extractApplicationMessageIdlFromActor, isClientKeyEq, } from "./idl"; -import logger from "./logger"; +import { logger } from "./logger"; import { isMessageBodyValid, randomBigInt, safeExecute } from "./utils"; import { isClientIncomingMessage, diff --git a/src/logger.ts b/src/logger.ts index 002b708..4715239 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -5,4 +5,4 @@ const logLevel = process.env.LOG_LEVEL || 'info'; const logger = log.getLogger('ic-websocket-js'); logger.setDefaultLevel(logLevel as LogLevelDesc); -export default logger; +export { logger }; diff --git a/src/utils.test.ts b/src/utils.test.ts index 16184db..b0dc093 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -2,7 +2,7 @@ import { HttpAgent, fromHex } from "@dfinity/agent"; import { Principal } from "@dfinity/principal"; import { isMessageBodyValid, randomBigInt, safeExecute } from "./utils"; import { ClientIncomingMessage } from "./types"; -import logger from "./logger"; +import { logger } from "./logger"; import { MAX_CERTIFICATE_AGE_IN_MINUTES } from "./test/constants"; // the canister from which the correct data were generated From a0c9a495ffa5e68b197e4fbbff4f088d4d6fff2d Mon Sep 17 00:00:00 2001 From: ilbertt Date: Thu, 13 Mar 2025 22:45:48 +0100 Subject: [PATCH 6/6] fix: exports --- src/index.ts | 4 ++-- src/utils.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6f95a0b..87bb459 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export * from './ic-websocket'; -export * from './identity'; +export { IcWebSocket, type IcWebSocketConfig, createWsConfig } from './ic-websocket'; +export { generateRandomIdentity } from './identity'; diff --git a/src/utils.ts b/src/utils.ts index 4eccf7b..3b143bb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,7 +9,7 @@ import { reconstruct, } from "@dfinity/agent"; import { Principal } from "@dfinity/principal"; -import logger from "./logger"; +import { logger } from "./logger"; const areBuffersEqual = (buf1: ArrayBuffer, buf2: ArrayBuffer): boolean => { return compare(buf1, buf2) === 0;