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..80405c69e 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, true); + 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' diff --git a/packages/adena-extension/src/inject/message/methods/common.ts b/packages/adena-extension/src/inject/message/methods/common.ts index 92e5aaa63..23c5781e9 100644 --- a/packages/adena-extension/src/inject/message/methods/common.ts +++ b/packages/adena-extension/src/inject/message/methods/common.ts @@ -72,16 +72,26 @@ 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(); - + 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; } + if (sendSuccessResponse) { + sendResponse(InjectionMessageInstance.success('ALREADY_CONNECTED', {}, requestData.key)) + } return true; };