Skip to content

Commit cfe141a

Browse files
committed
feat: accept promise of identity
1 parent e2317b5 commit cfe141a

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

src/ic-websocket.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe("IcWebsocket class", () => {
120120
const openMessageBytes = await mockWsServer.nextMessage as ArrayBuffer;
121121

122122
// reconstruct the message that the client should send
123-
const clientKey = icWs["_clientKey"];
123+
const clientKey = icWs["_clientKey"]!;
124124
const { envelope: { content: openMessageContent } }: WsAgentRequestMessage<CallRequest> = Cbor.decode(openMessageBytes);
125125

126126
expect(canisterId.compareTo(
@@ -172,7 +172,7 @@ describe("IcWebsocket class", () => {
172172
icWs.onerror = onError;
173173
await mockWsServer.connected;
174174

175-
const originalClientKey = { ...icWs["_clientKey"] };
175+
const originalClientKey = { ...icWs["_clientKey"]! };
176176
// workaround to simulate the client identity
177177
icWs["_clientKey"] = client1Key;
178178
// send the open confirmation message from the canister
@@ -205,7 +205,7 @@ describe("IcWebsocket class", () => {
205205
icWs.onclose = onClose;
206206
await mockWsServer.connected;
207207

208-
const originalClientKey = { ...icWs["_clientKey"] };
208+
const originalClientKey = { ...icWs["_clientKey"]! };
209209
// workaround to simulate the client identity
210210
icWs["_clientKey"] = client1Key;
211211
// send the open confirmation message from the canister
@@ -240,7 +240,7 @@ describe("IcWebsocket class", () => {
240240
icWs.onclose = onClose;
241241
await mockWsServer.connected;
242242

243-
const originalClientKey = { ...icWs["_clientKey"] };
243+
const originalClientKey = { ...icWs["_clientKey"]! };
244244
// workaround to simulate the client identity
245245
icWs["_clientKey"] = client1Key;
246246
// send the open confirmation message from the canister
@@ -280,7 +280,7 @@ describe("IcWebsocket class", () => {
280280
// wait for the open message from the client
281281
await mockWsServer.nextMessage;
282282

283-
const originalClientKey = { ...icWs["_clientKey"] };
283+
const originalClientKey = { ...icWs["_clientKey"]! };
284284
// workaround to simulate the client identity
285285
icWs["_clientKey"] = client1Key;
286286
// send the open confirmation message from the canister
@@ -347,7 +347,7 @@ describe("Messages acknowledgement", () => {
347347
// wait for the open message from the client
348348
await mockWsServer.nextMessage;
349349

350-
const originalClientKey = { ...icWs["_clientKey"] };
350+
const originalClientKey = { ...icWs["_clientKey"]! };
351351
// workaround to simulate the client identity
352352
icWs["_clientKey"] = client1Key;
353353
// send the open confirmation message from the canister
@@ -389,7 +389,7 @@ describe("Messages acknowledgement", () => {
389389
// wait for the open message from the client
390390
await mockWsServer.nextMessage;
391391

392-
const originalClientKey = { ...icWs["_clientKey"] };
392+
const originalClientKey = { ...icWs["_clientKey"]! };
393393
// workaround to simulate the client identity
394394
icWs["_clientKey"] = client1Key;
395395
// send the open confirmation message from the canister
@@ -435,7 +435,7 @@ describe("Messages acknowledgement", () => {
435435
// wait for the open message from the client
436436
await mockWsServer.nextMessage;
437437

438-
const originalClientKey = { ...icWs["_clientKey"] };
438+
const originalClientKey = { ...icWs["_clientKey"]! };
439439
// workaround to simulate the client identity
440440
icWs["_clientKey"] = client1Key;
441441
// send the open confirmation message from the canister

src/ic-websocket.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface IcWebSocketConfig<S extends _WS_CANISTER_SERVICE> {
5454
/**
5555
* The identity to use for signing messages. If empty, a new random temporary identity will be generated.
5656
*/
57-
identity: SignIdentity,
57+
identity: SignIdentity | Promise<SignIdentity>,
5858
/**
5959
* The IC network url to use for the underlying agent. It can be a local replica URL (e.g. http://localhost:4943) or the IC mainnet URL (https://icp0.io).
6060
*/
@@ -91,14 +91,14 @@ export default class IcWebSocket<
9191
private readonly _httpAgent: HttpAgent;
9292
private _wsAgent: WsAgent | null = null;
9393
private readonly _wsInstance: WebSocket;
94-
private readonly _identity: SignIdentity;
94+
private readonly _identity: Promise<SignIdentity>;
9595
private _incomingSequenceNum = BigInt(1);
9696
private _outgoingSequenceNum = BigInt(0);
9797
private _isConnectionEstablished = false;
9898
private _incomingMessagesQueue: BaseQueue<ArrayBuffer>;
9999
private _outgoingMessagesQueue: BaseQueue<Uint8Array>;
100100
private _ackMessagesQueue: AckMessagesQueue;
101-
private _clientKey: ClientKey;
101+
private _clientKey: ClientKey | null = null;
102102
private _maxCertificateAgeInMinutes = 5;
103103

104104
onclose: ((this: IcWebSocket<S, ApplicationMessageType>, ev: CloseEvent) => any) | null = null;
@@ -140,12 +140,7 @@ export default class IcWebSocket<
140140
if (!(config.identity instanceof SignIdentity)) {
141141
throw new Error("Identity must be a SignIdentity");
142142
}
143-
this._identity = config.identity;
144-
145-
this._clientKey = {
146-
client_principal: this.getPrincipal(),
147-
client_nonce: randomBigInt(),
148-
}
143+
this._identity = Promise.resolve(config.identity);
149144

150145
if (!config.networkUrl) {
151146
throw new Error("Network url is required");
@@ -193,8 +188,8 @@ export default class IcWebSocket<
193188
this._outgoingMessagesQueue.addAndProcess(new Uint8Array(data));
194189
}
195190

196-
public getPrincipal(): Principal {
197-
return this._identity.getPrincipal();
191+
public async getPrincipal(): Promise<Principal> {
192+
return (await this._identity).getPrincipal();
198193
}
199194

200195
public close() {
@@ -213,6 +208,11 @@ export default class IcWebSocket<
213208
}
214209

215210
private async _onWsOpen() {
211+
this._clientKey = {
212+
client_principal: await this.getPrincipal(),
213+
client_nonce: randomBigInt(),
214+
}
215+
216216
this._wsAgent = new WsAgent({
217217
identity: this._identity,
218218
httpAgent: this._httpAgent,
@@ -294,7 +294,7 @@ export default class IcWebSocket<
294294
const serviceMessage = decodeWebsocketServiceMessageContent(content as Uint8Array);
295295
if ("OpenMessage" in serviceMessage) {
296296
logger.debug("[onWsMessage] Received open message from canister");
297-
if (!isClientKeyEq(serviceMessage.OpenMessage.client_key, this._clientKey)) {
297+
if (!isClientKeyEq(serviceMessage.OpenMessage.client_key, this._clientKey!)) {
298298
throw new Error("Client key does not match");
299299
}
300300

@@ -454,7 +454,7 @@ export default class IcWebSocket<
454454
this._outgoingSequenceNum++;
455455

456456
const outgoingMessage: WebsocketMessage = {
457-
client_key: this._clientKey,
457+
client_key: this._clientKey!,
458458
sequence_num: this._outgoingSequenceNum,
459459
timestamp: BigInt(Date.now()) * BigInt(10 ** 6),
460460
content,

0 commit comments

Comments
 (0)