Skip to content
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

feat: Check connection method #539

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions packages/adena-extension/src/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const init = (): void => {
const response = await executor.addEstablish(name);
return response;
},
async CheckConnection(name: string): Promise<unknown> {
Copy link
Member

@jinoosss jinoosss Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a parameter of name necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, to check if the wallet is connected with the site that has this name, like the name passed in AddEstablish no ?

Or is it passed in AddEstablish just to be displayed in the popup while the value used to check the connection is the hostname ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the name variable passed in AddEstablish will only display the service name in the popup.

It is not needed for functions that simply check for connections.

const executor = new AdenaExecutor();
const response = await executor.checkConnection(name);
return response;
},
async DoContract(message: RequestDoContractMessage): Promise<unknown> {
const executor = new AdenaExecutor();
const response = await executor.doContract(message);
Expand Down
9 changes: 8 additions & 1 deletion packages/adena-extension/src/inject/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ export class AdenaExecutor {
return this.sendEventMessage(eventMessage);
};

public checkConnection = (name?: string): Promise<unknown> => {
const eventMessage = AdenaExecutor.createEventMessage('CHECK_CONNECTION', {
name: name ?? 'Unknown',
});
return this.sendEventMessage(eventMessage);
};

public doContract = (params: RequestDoContractMessage): Promise<unknown> => {
const result = this.validateContractMessage(params);
if (result) {
Expand Down Expand Up @@ -139,7 +146,7 @@ export class AdenaExecutor {
}
};

private sendEventMessage = (eventMessage: InjectionMessage): Promise<unknown> => {
private sendEventMessage = async (eventMessage: InjectionMessage): Promise<unknown> => {
this.listen();
this.eventMessage = {
...eventMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions packages/adena-extension/src/inject/message/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -193,6 +197,7 @@ export class InjectionMessageInstance {

public getType = ():
| 'ADD_ESTABLISH'
| 'CHECK_CONNECTION'
| 'DO_CONTRACT'
| 'GET_ACCOUNT'
| 'SIGN_AMINO'
Expand Down
12 changes: 11 additions & 1 deletion packages/adena-extension/src/inject/message/methods/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,26 @@ export const removePopups = async (): Promise<void> => {
export const checkEstablished = async (
requestData: InjectionMessage,
sendResponse: (response: any) => void,
sendSuccessResponse = false,
): Promise<boolean> => {
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;
};

Expand Down
Loading