-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update playwright and add @types/node * Add support for latest metamask dapp * Partially implement Wallet Permissions System
- Loading branch information
Showing
8 changed files
with
188 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// EIP-2255: Wallet Permissions System (https://eips.ethereum.org/EIPS/eip-2255) | ||
|
||
import type { Web3RequestKind } from '../utils' | ||
|
||
// Caveat for `eth_accounts` could be: | ||
// { "type": "requiredMethods", "value": ["signTypedData_v3"] } | ||
interface Caveat { | ||
type: string | ||
value: any // JSON object, meaning depends on the caveat type | ||
} | ||
|
||
interface Permission { | ||
invoker: string // DApp origin | ||
parentCapability: string // RPC method name | ||
caveats: Caveat[] | ||
} | ||
|
||
type ShorthandPermission = Web3RequestKind | string | ||
|
||
export class WalletPermissionSystem { | ||
#permissions: Map<string, Permission[]> = new Map() | ||
#wildcardOrigin: string = '*' | ||
|
||
constructor(perms: ShorthandPermission[] = []) { | ||
this.#permissions.set( | ||
this.#wildcardOrigin, | ||
perms.map((perm) => ({ | ||
invoker: this.#wildcardOrigin, | ||
parentCapability: perm, | ||
caveats: [], | ||
})) | ||
) | ||
} | ||
|
||
/** | ||
* @param rpcMethod | ||
* @param origin not used in the current implementation, empty string can be passed | ||
*/ | ||
permit(rpcMethod: Web3RequestKind | string, origin: string) { | ||
const permissions = this.#permissions.get(origin) || [] | ||
|
||
const updatedPermissions = permissions.filter( | ||
(permission) => permission.parentCapability !== rpcMethod | ||
) | ||
updatedPermissions.push({ | ||
invoker: origin, | ||
parentCapability: rpcMethod, | ||
caveats: [], // Caveats are not implemented | ||
}) | ||
|
||
this.#permissions.set(origin, updatedPermissions) | ||
} | ||
|
||
/** | ||
* @param rpcMethod | ||
* @param origin not used in the current implementation, empty string can be passed | ||
*/ | ||
revoke(rpcMethod: Web3RequestKind | string, origin: string) { | ||
const permissions = this.#permissions.get(origin) || [] | ||
const updatedPermissions = permissions.filter( | ||
(permission) => permission.parentCapability !== rpcMethod | ||
) | ||
this.#permissions.set(origin, updatedPermissions) | ||
} | ||
|
||
/** | ||
* @param rpcMethod | ||
* @param origin not used in the current implementation, empty string can be passed | ||
*/ | ||
isPermitted(rpcMethod: Web3RequestKind | string, origin: string): boolean { | ||
const permissions = this.#permissions.get(origin) || [] | ||
const wildcardPermissions = | ||
this.#permissions.get(this.#wildcardOrigin) || [] | ||
|
||
return ( | ||
permissions.some( | ||
(permission) => permission.parentCapability === rpcMethod | ||
) || | ||
wildcardPermissions.some( | ||
(permission) => permission.parentCapability === rpcMethod | ||
) | ||
) | ||
} | ||
} |
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,31 @@ | ||
import { expect, test, describe } from '../fixtures' | ||
import { Web3ProviderBackend, Web3RequestKind } from '../../src' | ||
|
||
let wallet: Web3ProviderBackend | ||
|
||
describe('Auto connected', () => { | ||
test.beforeEach(async ({ page, injectWeb3Provider }) => { | ||
// Inject window.ethereum instance | ||
wallet = await injectWeb3Provider(undefined, [Web3RequestKind.Accounts]) | ||
|
||
// In order to make https://metamask.github.io/test-dapp/ work flag should be set | ||
await page.addInitScript(() => (window.ethereum.isMetaMask = true)) | ||
|
||
await page.goto('https://metamask.github.io/test-dapp/') | ||
}) | ||
|
||
test('my address', async ({ page, accounts }) => { | ||
// Already connected, no need to connect again | ||
await page | ||
.getByRole('button', { name: 'Connected', exact: true }) | ||
.isDisabled() | ||
|
||
// Verify if the wallet is really connected | ||
await expect(page.locator('text=' + accounts[0])).toBeVisible() | ||
|
||
// Accounts should be available | ||
await expect(page.locator('#getAccountsResult')).toBeEmpty() | ||
await page.getByRole('button', { name: 'ETH_ACCOUNTS' }).click() | ||
await expect(page.locator('#getAccountsResult')).toContainText(accounts[0]) | ||
}) | ||
}) |
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
Oops, something went wrong.