Skip to content

Commit

Permalink
Merge pull request #1210 from chainapsis/delivan/fix-evm-permission
Browse files Browse the repository at this point in the history
Fix an issue that some evm dapp can't connect to keplr
  • Loading branch information
Thunnini authored Sep 26, 2024
2 parents 0239dc1 + 91090cb commit 10f5086
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
21 changes: 11 additions & 10 deletions packages/background/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,6 @@ export function init(
analyticsService,
msgPrivilegedOrigins
);
const keyRingEthereumService = new KeyRingEthereum.KeyRingEthereumService(
chainsService,
keyRingV2Service,
keyRingCosmosService,
interactionService,
analyticsService,
permissionService,
backgroundTxEthereumService,
tokenERC20Service
);
const autoLockAccountService = new AutoLocker.AutoLockAccountService(
storeCreator("auto-lock-account"),
keyRingV2Service,
Expand All @@ -231,6 +221,17 @@ export function init(
chainsService
);

const keyRingEthereumService = new KeyRingEthereum.KeyRingEthereumService(
chainsService,
keyRingV2Service,
keyRingCosmosService,
interactionService,
analyticsService,
permissionService,
permissionInteractiveService,
backgroundTxEthereumService,
tokenERC20Service
);
const chainsUpdateService = new ChainsUpdate.ChainsUpdateService(
storeCreator("chains-update"),
chainsService,
Expand Down
37 changes: 25 additions & 12 deletions packages/background/src/keyring-ethereum/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { BackgroundTxEthereumService } from "../tx-ethereum";
import { TokenERC20Service } from "../token-erc20";
import { validateEVMChainId } from "./helper";
import { runInAction } from "mobx";
import { PermissionInteractiveService } from "../permission-interactive";

export class KeyRingEthereumService {
protected websocketSubscriptionMap = new Map<string, WebSocket>();
Expand All @@ -44,6 +45,7 @@ export class KeyRingEthereumService {
protected readonly interactionService: InteractionService,
protected readonly analyticsService: AnalyticsService,
protected readonly permissionService: PermissionService,
protected readonly permissionInteractiveService: PermissionInteractiveService,
protected readonly backgroundTxEthereumService: BackgroundTxEthereumService,
protected readonly tokenERC20Service: TokenERC20Service
) {}
Expand Down Expand Up @@ -329,8 +331,27 @@ export class KeyRingEthereumService {
selectedAddress: null,
} as T;
} else {
throw new Error(
`${origin} is not permitted. Please disconnect and reconnect to the website.`
// 처음 방식은 dapp에서 disconnect하면 currentChainId에 해당하는 체인의 권한만 제거하는 방식이었어서
// 특정 origin의 권한을 지우는 요청이 왔어도 그 origin에 권한이 있는 체인이 하나라도 있으면 에러를 뱉는 방식이었다.
// 하지만 dapp 입장에선 체인당 권한이라는 개념을 모르기 때문에 특정 origin의 권한을 지우는 요청에 체인을 특정하게 하는 것은 버그였다.
// 따라서 그 origin의 모든 체인의 basic access 권한을 없애고 다시 요청이 처리되도록 한다.
await this.permissionService.removeAllSpecificTypePermission(
[origin],
getBasicAccessPermissionType()
);

await this.permissionInteractiveService.ensureEnabledForEVM(
env,
origin
);

return this.request<T>(
env,
origin,
method,
params,
providerId,
chainId
);
}
}
Expand Down Expand Up @@ -358,11 +379,7 @@ export class KeyRingEthereumService {
};
}
case "keplr_disconnect": {
return this.permissionService.removePermission(
currentChainId,
getBasicAccessPermissionType(),
[origin]
);
return this.permissionService.removeAllTypePermission([origin]);
}
case "eth_chainId": {
return `0x${currentChainEVMInfo.chainId.toString(16)}`;
Expand Down Expand Up @@ -897,11 +914,7 @@ export class KeyRingEthereumService {
);
}

await this.permissionService.removePermission(
currentChainId,
getBasicAccessPermissionType(),
[origin]
);
await this.permissionService.removeAllTypePermission([origin]);

return null;
}
Expand Down
21 changes: 21 additions & 0 deletions packages/background/src/permission/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,27 @@ export class PermissionService {
}
}

@action
removeAllSpecificTypePermission(origins: string[], type: string) {
const deletes: string[] = [];

for (const key of this.permissionMap.keys()) {
for (const origin of origins) {
const typeAndOrigin =
PermissionKeyHelper.getChainAndTypeFromPermissionKey(origin, key);
if (typeAndOrigin && typeAndOrigin.type === type) {
deletes.push(key);
}

this.currentChainIdForEVMByOriginMap.delete(origin);
}
}

for (const key of deletes) {
this.permissionMap.delete(key);
}
}

@action
removeAllTypePermission(origins: string[]) {
const deletes: string[] = [];
Expand Down

0 comments on commit 10f5086

Please sign in to comment.