Skip to content

Commit

Permalink
feat: add permissions methods
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Oct 28, 2024
1 parent ebae44d commit 5ac2062
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 65 deletions.
85 changes: 58 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
This Web3.js plugin adds support for the following wallet-related RPC methods:

- [wallet_addEthereumChain (EIP-3085)](https://eips.ethereum.org/EIPS/eip-3085)
- [wallet_updateEthereumChain (EIP-2015)](https://eips.ethereum.org/EIPS/eip-2015)
- [wallet_switchEthereumChain (EIP-3326)](https://eips.ethereum.org/EIPS/eip-3326)
- [wallet_getOwnedAssets (EIP-2256)](https://eips.ethereum.org/EIPS/eip-2256)
- [wallet_watchAsset (EIP-747)](https://eips.ethereum.org/EIPS/eip-747)

Not implemented yet:

- [wallet_requestPermissions (EIP-2255)](https://eips.ethereum.org/EIPS/eip-2255)
- [wallet_getPermissions (EIP-2255)](https://eips.ethereum.org/EIPS/eip-2255)
- [wallet_revokePermissions](https://docs.metamask.io/wallet/reference/json-rpc-methods/wallet_revokepermissions/)

Experimental:

- [wallet_updateEthereumChain (EIP-2015)](https://eips.ethereum.org/EIPS/eip-2015)
- [wallet_getOwnedAssets (EIP-2256)](https://eips.ethereum.org/EIPS/eip-2256)

## Installation

Expand Down Expand Up @@ -62,6 +63,58 @@ await web3.walletRpc.addEthereumChain({
});
```

#### switchEthereumChain

Invokes the `wallet_switchEthereumChain` method as defined in [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326).

```typescript
await web3.walletRpc.switchEthereumChain(5000);
```

#### watchAsset

Invokes the `wallet_watchAsset` method as defined in [EIP-747](https://eips.ethereum.org/EIPS/eip-747).

```typescript
await web3.walletRpc.watchAsset({
type: "ERC20",
options: {
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
symbol: "USDC",
},
});
```

#### requestPermissions

Invokes the `wallet_requestPermissions` method as defined in [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255).

```typescript
const permissions = await web3.walletRpc.requestPermissions({
eth_accounts: {},
});
```

#### getPermissions

Invokes the `wallet_getPermissions` method as defined in [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255).

```typescript
const permissions = await web3.walletRpc.getPermissions();
```

#### revokePermissions

Invokes the `wallet_revokePermissions` method as defined in [MetaMask docs](https://docs.metamask.io/wallet/reference/json-rpc-methods/wallet_revokepermissions/).

```typescript
const permissions = await web3.walletRpc.revokePermissions({
eth_accounts: {},
});
```

### Experimental methods

#### updateEthereumChain

Invokes the `wallet_updateEthereumChain` method as defined in [EIP-2015](https://eips.ethereum.org/EIPS/eip-2015).
Expand All @@ -80,14 +133,6 @@ await web3.walletRpc.updateEthereumChain({
});
```

#### switchEthereumChain

Invokes the `wallet_switchEthereumChain` method as defined in [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326).

```typescript
await web3.walletRpc.switchEthereumChain({ chainId: 5000 });
```

#### getOwnedAssets

Invokes the `wallet_getOwnedAssets` method as defined in [EIP-2256](https://eips.ethereum.org/EIPS/eip-2256).
Expand All @@ -98,20 +143,6 @@ const ownedAssets = await web3.walletRpc.getOwnedAssets({
});
```

#### watchAsset

Invokes the `wallet_watchAsset` method as defined in [EIP-747](https://eips.ethereum.org/EIPS/eip-747).

```typescript
await web3.walletRpc.watchAsset({
type: "ERC20",
options: {
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
symbol: "USDC",
},
});
```

## Contributing

We welcome pull requests! For major changes, please open an issue first to discuss the proposed modifications.
Expand Down
115 changes: 103 additions & 12 deletions packages/web3-plugin-wallet-rpc/src/WalletRpcPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
AddEthereumChainRequest,
GetOwnedAssetsRequest,
OwnedAsset,
Permission,
PermissionRequest,
UpdateEthereumChainRequest,
WatchAssetRequest,
} from "./types";
Expand All @@ -16,6 +18,9 @@ type WalletRpcApi = {
wallet_switchEthereumChain: (chainId: Numbers) => void;
wallet_getOwnedAssets: (param: GetOwnedAssetsRequest) => OwnedAsset[];
wallet_watchAsset: (param: WatchAssetRequest) => boolean;
wallet_requestPermissions: (param: PermissionRequest) => Permission[];
wallet_getPermissions: () => Permission[];
wallet_revokePermissions: (param: PermissionRequest) => void;
};

/**
Expand Down Expand Up @@ -44,8 +49,22 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> {
*
* See [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085) for more details.
*
* @param param - Details of the chain to add
* @returns a Promise that resolves if the request is successful
* @param param - Details of the chain to add.
* @returns A Promise that resolves if the request is successful.
*
* @example
* await web3.walletRpc.addEthereumChain({
* chainId: 5000,
* blockExplorerUrls: ["https://mantlescan.xyz"],
* chainName: "Mantle",
* iconUrls: ["https://icons.llamao.fi/icons/chains/rsz_mantle.jpg"],
* nativeCurrency: {
* name: "Mantle",
* symbol: "MNT",
* decimals: 18,
* },
* rpcUrls: ["https://rpc.mantle.xyz"],
* });
*/
public async addEthereumChain(param: AddEthereumChainRequest): Promise<void> {
return this.requestManager.send({
Expand All @@ -64,8 +83,9 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> {
*
* See [EIP-2015](https://eips.ethereum.org/EIPS/eip-2015) for more details.
*
* @param param - Details of the chain to switch to and possibly add
* @returns a Promise that resolves if the request is successful
* @param param - Details of the chain to switch to and possibly add.
* @returns A Promise that resolves if the request is successful.
* @experimental
*/
public async updateEthereumChain(
param: UpdateEthereumChainRequest,
Expand All @@ -82,12 +102,17 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> {
}

/**
* Switch the wallet’s currently active chain.
* Switch the wallet's currently active chain.
* If the specified chain does not exist in the wallet, an error will be thrown.
* To prevent errors, ensure the chain has been added first or handle the call within a try/catch block.
*
* See [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326) for more details.
*
* @param chainId - Chain ID of the chain to switch to
* @returns a Promise that resolves if the request is successful
* @param chainId - The ID of the chain to switch to.
* @returns A Promise that resolves if the chain switch is successful.
*
* @example
* await web3.walletRpc.switchEthereumChain(5000);
*/
public async switchEthereumChain(chainId: Numbers): Promise<void> {
return this.requestManager.send({
Expand All @@ -105,8 +130,9 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> {
*
* See [EIP-2256](https://eips.ethereum.org/EIPS/eip-2256) for more details.
*
* @param param - Details of the request for owned assets
* @returns a Promise that resolves to a list of owned assets
* @param param - Details of the request for owned assets.
* @returns A Promise that resolves to a list of owned assets.
* @experimental
*/
public async getOwnedAssets(
param: GetOwnedAssetsRequest,
Expand All @@ -131,18 +157,83 @@ export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> {
*
* See [EIP-747](https://eips.ethereum.org/EIPS/eip-747) for more details.
*
* @param param - Details of the asset to watch
* @returns a Promise that resolves to `true` if the request is successful
* @param param - Details of the asset to watch.
* @returns A Promise that resolves to `true` if the request is successful.
*
* @example
* await web3.walletRpc.watchAsset({
* type: "ERC20",
* options: {
* address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
* symbol: "USDC",
* },
* });
*/
public async watchAsset(param: WatchAssetRequest): Promise<boolean> {
return this.requestManager.send({
method: "wallet_watchAsset",
params: [param],
});
}

/**
* Request permissions for a dApp.
*
* See [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255) for more details.
*
* @param param - Details of the permission request.
* @returns A Promise that resolves to an array of granted permissions.
*
* @example
* const permissions = await web3.walletRpc.requestPermissions({
* eth_accounts: {}
* });
*/
public async requestPermissions(
param: PermissionRequest,
): Promise<Permission[]> {
return this.requestManager.send({
method: "wallet_requestPermissions",
params: [param],
});
}

/**
* Retrieve the list of permissions granted to the dApp.
*
* See [EIP-2255](https://eips.ethereum.org/EIPS/eip-2255) for more details.
*
* @returns A Promise that resolves to an array of granted permissions.
*
* @example
* const permissions = await web3.walletRpc.getPermissions();
*/
public async getPermissions(): Promise<Permission[]> {
return this.requestManager.send({
method: "wallet_getPermissions",
params: [],
});
}

/**
* Revoke permissions granted to the dApp.
*
* @param param - Details of the permissions to revoke.
* @returns A Promise that resolves if the request is successful.
*
* @example
* await web3.walletRpc.revokePermissions({
* eth_accounts: {}
* });
*/
public async revokePermissions(param: PermissionRequest): Promise<void> {
return this.requestManager.send({
method: "wallet_revokePermissions",
params: [param],
});
}
}

// Module Augmentation
declare module "web3" {
interface Web3Context {
walletRpc: WalletRpcPlugin;
Expand Down
Loading

0 comments on commit 5ac2062

Please sign in to comment.