-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update permissions for extension
- Loading branch information
Showing
11 changed files
with
162 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./service"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { AllowedPermissions, LocalStorage, PermissionKind } from "storage"; | ||
|
||
export class PermissionsService { | ||
constructor(protected readonly localStorage: LocalStorage) {} | ||
|
||
async enablePermissions( | ||
domain: string, | ||
chainId: string, | ||
allowed: AllowedPermissions | ||
): Promise<void> { | ||
const existingPermissions = await this.localStorage.getPermissions(); | ||
const newPermissions = [...new Set<PermissionKind>(allowed)]; | ||
|
||
if (existingPermissions) { | ||
existingPermissions[domain] = existingPermissions[domain] || {}; | ||
existingPermissions[domain][chainId] = newPermissions; | ||
return await this.localStorage.setPermissions(existingPermissions); | ||
} | ||
|
||
return await this.localStorage.setPermissions({ | ||
[domain]: { | ||
[chainId]: newPermissions, | ||
}, | ||
}); | ||
} | ||
|
||
async revokeChainPermissions(domain: string, chainId: string): Promise<void> { | ||
const updatedPermissions = await this.localStorage.getPermissions(); | ||
if ( | ||
!updatedPermissions || | ||
!updatedPermissions[domain] || | ||
!updatedPermissions[domain][chainId] | ||
) { | ||
return; | ||
} | ||
delete updatedPermissions[domain][chainId]; | ||
await this.localStorage.setPermissions(updatedPermissions); | ||
} | ||
|
||
async permissionsByDomain( | ||
domain: string | ||
): Promise<Record<string, AllowedPermissions> | undefined> { | ||
const permissions = await this.localStorage.getPermissions(); | ||
|
||
if (permissions && permissions[domain]) { | ||
return permissions[domain]; | ||
} | ||
} | ||
|
||
async permissionsByChain( | ||
domain: string, | ||
chainId: string | ||
): Promise<AllowedPermissions | undefined> { | ||
const permissions = await this.localStorage.getPermissions(); | ||
if (permissions && permissions[domain] && permissions[domain][chainId]) { | ||
return permissions[domain][chainId]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters