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: add get/request/revoke permissions methods #9

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "all"
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"eslint.workingDirectories": [
"packages/example-react-app",
"packages/web3-plugin-wallet-rpc"
]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this file be checked-in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, removed for now, let's wait if others will have similar problems

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you link the EIPs in the descriptions below, I think these links should point to the anchors for the respective method descriptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done per function in the examples below, here is the link to the full EIP

- [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:
krzysu marked this conversation as resolved.
Show resolved Hide resolved

- [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
4 changes: 2 additions & 2 deletions packages/example-react-app/public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"short_name": "Web3.js + React dApp",
"name": "Web3.js + React Demonstration dApp",
"short_name": "Web3.js Wallet RPC Demo",
"name": "Web3.js Wallet RPC Methods Demonstration dApp",
"icons": [
{
"src": "favicon.ico",
Expand Down
3 changes: 2 additions & 1 deletion packages/web3-plugin-wallet-rpc/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
lib
lib
docs
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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since other libraries don’t implement this method and wallets appear not to support them, I’ve moved it to experimental. I’ll eventually remove it if I don’t find a wallet that supports it.

*/
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 param - 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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since other libraries don’t implement this method and wallets appear not to support them, I’ve moved it to experimental. I’ll eventually remove it if I don’t find a wallet that supports it.

*/
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
Loading