From df45590899d209dbdc706a394a76ad1ace7c7665 Mon Sep 17 00:00:00 2001 From: Reza Rahemtola Date: Mon, 29 Jul 2024 15:40:05 +0200 Subject: [PATCH 1/3] feat: Check connection method --- packages/adena-extension/src/inject.ts | 5 +++++ packages/adena-extension/src/inject/executor/executor.ts | 9 ++++++++- .../src/inject/message/message-handler.ts | 3 +++ packages/adena-extension/src/inject/message/message.ts | 5 +++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/adena-extension/src/inject.ts b/packages/adena-extension/src/inject.ts index b522e3610..7dd3c87bd 100644 --- a/packages/adena-extension/src/inject.ts +++ b/packages/adena-extension/src/inject.ts @@ -19,6 +19,11 @@ const init = (): void => { const response = await executor.addEstablish(name); return response; }, + async CheckConnection(name: string): Promise { + const executor = new AdenaExecutor(); + const response = await executor.checkConnection(name); + return response; + }, async DoContract(message: RequestDoContractMessage): Promise { const executor = new AdenaExecutor(); const response = await executor.doContract(message); diff --git a/packages/adena-extension/src/inject/executor/executor.ts b/packages/adena-extension/src/inject/executor/executor.ts index f9ae41e19..ecff09228 100644 --- a/packages/adena-extension/src/inject/executor/executor.ts +++ b/packages/adena-extension/src/inject/executor/executor.ts @@ -63,6 +63,13 @@ export class AdenaExecutor { return this.sendEventMessage(eventMessage); }; + public checkConnection = (name?: string): Promise => { + const eventMessage = AdenaExecutor.createEventMessage('CHECK_CONNECTION', { + name: name ?? 'Unknown', + }); + return this.sendEventMessage(eventMessage); + }; + public doContract = (params: RequestDoContractMessage): Promise => { const result = this.validateContractMessage(params); if (result) { @@ -139,7 +146,7 @@ export class AdenaExecutor { } }; - private sendEventMessage = (eventMessage: InjectionMessage): Promise => { + private sendEventMessage = async (eventMessage: InjectionMessage): Promise => { this.listen(); this.eventMessage = { ...eventMessage, diff --git a/packages/adena-extension/src/inject/message/message-handler.ts b/packages/adena-extension/src/inject/message/message-handler.ts index 7f8f6a63b..ffd09d23b 100644 --- a/packages/adena-extension/src/inject/message/message-handler.ts +++ b/packages/adena-extension/src/inject/message/message-handler.ts @@ -82,6 +82,9 @@ export class MessageHandler { case 'ADD_ESTABLISH': HandlerMethod.addEstablish(message, sendResponse); break; + case 'CHECK_CONNECTION': + HandlerMethod.checkEstablished(message, sendResponse); + break; case 'ADD_NETWORK': HandlerMethod.checkEstablished(message, sendResponse).then((isEstablished) => { if (isEstablished) { diff --git a/packages/adena-extension/src/inject/message/message.ts b/packages/adena-extension/src/inject/message/message.ts index 610cb2cd5..4df94bead 100644 --- a/packages/adena-extension/src/inject/message/message.ts +++ b/packages/adena-extension/src/inject/message/message.ts @@ -5,6 +5,10 @@ const MESSAGE_TYPES = { code: 0, description: 'Establish Connection.', }, + CHECK_CONNECTION: { + code: 0, + description: 'Check Connection status', + }, DO_CONTRACT: { code: 0, description: 'Do Contract.', @@ -193,6 +197,7 @@ export class InjectionMessageInstance { public getType = (): | 'ADD_ESTABLISH' + | 'CHECK_CONNECTION' | 'DO_CONTRACT' | 'GET_ACCOUNT' | 'SIGN_AMINO' From 1bee26084c5f9a43bed27ee743ab06f795b4f8fa Mon Sep 17 00:00:00 2001 From: Reza Rahemtola Date: Mon, 19 Aug 2024 02:29:56 +0200 Subject: [PATCH 2/3] fix: Review comments --- .../adena-extension/src/inject/message/methods/common.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/adena-extension/src/inject/message/methods/common.ts b/packages/adena-extension/src/inject/message/methods/common.ts index 92e5aaa63..d5b359a8e 100644 --- a/packages/adena-extension/src/inject/message/methods/common.ts +++ b/packages/adena-extension/src/inject/message/methods/common.ts @@ -75,13 +75,20 @@ export const checkEstablished = async ( ): Promise => { const core = new InjectCore(); const accountId = await core.getCurrentAccountId(); - + const isLocked = await core.walletService.isLocked(); const siteName = getSiteName(requestData.protocol, requestData.hostname); + + if (isLocked) { + sendResponse(InjectionMessageInstance.failure('WALLET_LOCKED', {}, requestData.key)) + return false + } + const isEstablished = await core.establishService.isEstablishedBy(accountId, siteName); if (!isEstablished) { sendResponse(InjectionMessageInstance.failure('NOT_CONNECTED', {}, requestData.key)); return false; } + sendResponse(InjectionMessageInstance.success('ALREADY_CONNECTED', {}, requestData.key)) return true; }; From fd609e3a430b949fd2a99dcbf388a312f9cae73b Mon Sep 17 00:00:00 2001 From: Reza Rahemtola Date: Mon, 19 Aug 2024 03:06:16 +0200 Subject: [PATCH 3/3] fix: Send established success response only for CheckEstablish --- .../adena-extension/src/inject/message/message-handler.ts | 2 +- .../adena-extension/src/inject/message/methods/common.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/adena-extension/src/inject/message/message-handler.ts b/packages/adena-extension/src/inject/message/message-handler.ts index ffd09d23b..80405c69e 100644 --- a/packages/adena-extension/src/inject/message/message-handler.ts +++ b/packages/adena-extension/src/inject/message/message-handler.ts @@ -83,7 +83,7 @@ export class MessageHandler { HandlerMethod.addEstablish(message, sendResponse); break; case 'CHECK_CONNECTION': - HandlerMethod.checkEstablished(message, sendResponse); + HandlerMethod.checkEstablished(message, sendResponse, true); break; case 'ADD_NETWORK': HandlerMethod.checkEstablished(message, sendResponse).then((isEstablished) => { diff --git a/packages/adena-extension/src/inject/message/methods/common.ts b/packages/adena-extension/src/inject/message/methods/common.ts index d5b359a8e..23c5781e9 100644 --- a/packages/adena-extension/src/inject/message/methods/common.ts +++ b/packages/adena-extension/src/inject/message/methods/common.ts @@ -72,6 +72,7 @@ export const removePopups = async (): Promise => { export const checkEstablished = async ( requestData: InjectionMessage, sendResponse: (response: any) => void, + sendSuccessResponse = false, ): Promise => { const core = new InjectCore(); const accountId = await core.getCurrentAccountId(); @@ -88,7 +89,9 @@ export const checkEstablished = async ( sendResponse(InjectionMessageInstance.failure('NOT_CONNECTED', {}, requestData.key)); return false; } - sendResponse(InjectionMessageInstance.success('ALREADY_CONNECTED', {}, requestData.key)) + if (sendSuccessResponse) { + sendResponse(InjectionMessageInstance.success('ALREADY_CONNECTED', {}, requestData.key)) + } return true; };