Skip to content

fix: change default export to named export #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ic-websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/ic-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -90,7 +90,7 @@ export const createWsConfig = <S extends _WS_CANISTER_SERVICE>(c: IcWebSocketCon

type WsParameters = ConstructorParameters<typeof WebSocket>;

export default class IcWebSocket<
export class IcWebSocket<
S extends _WS_CANISTER_SERVICE,
ApplicationMessageType = GetApplicationMessageType<S>
> {
Expand Down
10 changes: 2 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
// import default member
import IcWebSocket from './ic-websocket';
// re-export non-default members
export * from './ic-websocket';
export * from './identity';

// re-export default member
export default IcWebSocket;
export { IcWebSocket, type IcWebSocketConfig, createWsConfig } from './ic-websocket';
export { generateRandomIdentity } from './identity';
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
7 changes: 7 additions & 0 deletions src/test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
19 changes: 9 additions & 10 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ 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
const canisterId = Principal.fromText("bnz7o-iuaaa-aaaaa-qaaaa-cai");
Expand All @@ -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;
Expand All @@ -30,7 +29,7 @@ describe("Utils of the IcWebSocket", () => {
messageToVerify.cert,
messageToVerify.tree,
agent,
maxCertificateAgeInMinutes,
MAX_CERTIFICATE_AGE_IN_MINUTES,
);

expect(isValid).toBe(true);
Expand All @@ -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.");
});

Expand All @@ -56,7 +55,7 @@ describe("Utils of the IcWebSocket", () => {
messageToVerify.cert,
messageToVerify.tree,
agent,
maxCertificateAgeInMinutes,
MAX_CERTIFICATE_AGE_IN_MINUTES,
);

expect(isValid).toBe(false);
Expand All @@ -70,7 +69,7 @@ describe("Utils of the IcWebSocket", () => {
messageToVerify.cert,
messageToVerify.tree,
agent,
maxCertificateAgeInMinutes,
MAX_CERTIFICATE_AGE_IN_MINUTES,
);

expect(isValid).toBe(false);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down