From 21d64c91e67fb5590256ffa56aa1c42d645db577 Mon Sep 17 00:00:00 2001 From: Gancho Radkov Date: Thu, 14 Sep 2023 10:11:47 +0300 Subject: [PATCH 1/3] feat: POST auth requests to verify & throw on failed publish --- .../auth-client/src/controllers/engine.ts | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/auth-client/src/controllers/engine.ts b/packages/auth-client/src/controllers/engine.ts index 2feac7d..e58b46b 100644 --- a/packages/auth-client/src/controllers/engine.ts +++ b/packages/auth-client/src/controllers/engine.ts @@ -9,7 +9,7 @@ import { isJsonRpcResult, } from "@walletconnect/jsonrpc-utils"; import { PairingTypes, RelayerTypes, Verify } from "@walletconnect/types"; -import { getInternalError, hashKey, TYPE_1 } from "@walletconnect/utils"; +import { getInternalError, hashKey, isBrowser, TYPE_1 } from "@walletconnect/utils"; import { AUTH_CLIENT_PUBLIC_KEY_NAME, ENGINE_RPC_OPTS } from "../constants"; import { AuthClientTypes, AuthEngineTypes, IAuthEngine, JsonRpcTypes } from "../types"; import { getDidAddress, getDidChainId, getNamespacedDidChainId } from "../utils/address"; @@ -203,8 +203,16 @@ export class AuthEngine extends IAuthEngine { const rpcOpts = ENGINE_RPC_OPTS[method].req; if (expiry) rpcOpts.ttl = expiry; this.client.core.history.set(topic, payload); - await this.client.core.relayer.publish(topic, message, rpcOpts); - + if (isBrowser()) { + const hash = hashMessage(JSON.stringify(payload)); + this.client.core.verify.register({ attestationId: hash }); + } + await this.client.core.relayer.publish(topic, message, { + ...rpcOpts, + internal: { + throwOnFailedPublish: true, + }, + }); return payload.id; }; @@ -214,9 +222,13 @@ export class AuthEngine extends IAuthEngine { const record = await this.client.core.history.get(topic, id); const rpcOpts = ENGINE_RPC_OPTS[record.request.method].res; - await this.client.core.relayer.publish(topic, message, rpcOpts); + await this.client.core.relayer.publish(topic, message, { + ...rpcOpts, + internal: { + throwOnFailedPublish: true, + }, + }); await this.client.core.history.resolve(payload); - return payload.id; }; From c6f010cdd31873bae25a168aa3d1589d74bf2a25 Mon Sep 17 00:00:00 2001 From: Gancho Radkov Date: Thu, 14 Sep 2023 10:18:58 +0300 Subject: [PATCH 2/3] chore: wait for ack requests before shutting transport --- packages/auth-client/test/helpers/ws.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/auth-client/test/helpers/ws.ts b/packages/auth-client/test/helpers/ws.ts index 55c7262..d6d64a3 100644 --- a/packages/auth-client/test/helpers/ws.ts +++ b/packages/auth-client/test/helpers/ws.ts @@ -3,6 +3,8 @@ import { ICore } from "@walletconnect/types"; import EventEmitter from "events"; export async function disconnectSocket(core: ICore) { + // wait a bit for all ACK requests to be processed + await new Promise((resolve) => setTimeout(resolve, 1000)); if (core.relayer.connected) { core.relayer.provider.events = new EventEmitter(); core.relayer.core.heartbeat.events = new EventEmitter(); From 72c7f571c89d01dbc94aba548552baf7bf578103 Mon Sep 17 00:00:00 2001 From: Gancho Radkov Date: Thu, 14 Sep 2023 10:25:51 +0300 Subject: [PATCH 3/3] refactor: uses `relayer.transportClose` to close sockets --- packages/auth-client/test/helpers/ws.ts | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/packages/auth-client/test/helpers/ws.ts b/packages/auth-client/test/helpers/ws.ts index d6d64a3..25a4a30 100644 --- a/packages/auth-client/test/helpers/ws.ts +++ b/packages/auth-client/test/helpers/ws.ts @@ -1,20 +1,7 @@ -import { IJsonRpcConnection } from "@walletconnect/jsonrpc-utils"; import { ICore } from "@walletconnect/types"; -import EventEmitter from "events"; export async function disconnectSocket(core: ICore) { // wait a bit for all ACK requests to be processed await new Promise((resolve) => setTimeout(resolve, 1000)); - if (core.relayer.connected) { - core.relayer.provider.events = new EventEmitter(); - core.relayer.core.heartbeat.events = new EventEmitter(); - core.relayer.provider.connection.on("open", async () => { - await disconnect(core.relayer.provider.connection); - }); - await disconnect(core.relayer.provider.connection); - } -} - -function disconnect(socket: IJsonRpcConnection) { - return socket.close(); + await core.relayer.transportClose(); }