Skip to content

Commit

Permalink
configure the plugin, add first method addEthereumChain
Browse files Browse the repository at this point in the history
Signed-off-by: Kris Urbas <[email protected]>
  • Loading branch information
krzysu committed Oct 24, 2024
1 parent a342b7b commit 32f2226
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 78 deletions.
60 changes: 40 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,54 @@
web3-plugin-template
===========
# Web3.js Plugin for Wallet RPC methods

This is a template for creating a repository for web3.js plugin.
This Web3.js plugin adds support for the following wallet-related RPC methods:

How to use
------------
- wallet_addEthereumChain (EIP-3085)
- wallet_updateEthereumChain (EIP-2015)
- wallet_switchEthereumChain (EIP-3326)
- wallet_getOwnedAssets (EIP-2256)
- wallet_watchAsset (EIP-747)
- wallet_requestPermissions (EIP-2255)
- wallet_getPermissions (EIP-2255)

1. Create your project out of this template.
## Installation

You can do so by pressing on `Use this template` on the above right corner and then select `Create new Repositor`. Please, use the convention `web3-plugin-<name>` for your repo name.
2. Update the `name` and `description` fileds at your `package.json`.
Use your preferred package manager. Ensure that `web3` is also installed and integrated into your project.

Chose a name like: `@<organization>/web3-plugin-<name>` (or the less better `web3-plugin-<name>`).
3. Update the code inside `src` folder.
```bash
npm install web3-plugin-wallet-rpc
```

4. Modify and add tests inside `test` folder.
```bash
yarn add web3-plugin-wallet-rpc
```

5. Publish to the npm registry.
```bash
pnpm add web3-plugin-wallet-rpc
```

You can publish with something like: `yarn build && npm publish --access public`.
## Usage

Contributing
------------
### Register plugin

Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.
```typescript
import { WalletRpcPlugin } from "web3-plugin-wallet-rpc";
web3 = new Web3(/* provider here */);
web3.registerPlugin(new WalletRpcPlugin());
```

Please make sure to update tests as appropriate.
### Methods

License
-------
#### addEthereumChain

```typescript
await web3.walletRpc.addEthereumChain({ chainId: "0x1388" }); // chainId 5000 is Mantle Mainnet
```

## Contributing

We welcome pull requests! For major changes, please open an issue first to discuss the proposed modifications.
Also, ensure that you update tests as needed to reflect the changes.

## License

[MIT](https://choosealicense.com/licenses/mit/)
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "web3-plugin-template",
"name": "web3-plugin-wallet-rpc",
"version": "1.0.0",
"description": "Template plugin to extend web3.js with additional methods",
"description": "Web3.js plugin to add support for wallet-related RPC methods",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"homepage": "https://github.com/web3/web3.js-plugin-template#readme",
"homepage": "https://github.com/web3/web3-wallet-rpc-utils#readme",
"bugs": {
"url": "https://github.com/web3/web3.js-plugin-template/issues"
"url": "https://github.com/web3/web3-wallet-rpc-utils/issues"
},
"scripts": {
"lint": "eslint '{src,test}/**/*.ts'",
Expand All @@ -19,10 +19,9 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "[email protected]:web3/web3.js-plugin-template.git"
},
"dependencies": {
"url": "[email protected]:web3/web3-wallet-rpc-utils.git"
},
"dependencies": {},
"devDependencies": {
"@chainsafe/eslint-config": "^2.0.0",
"@types/jest": "^29.5.2",
Expand Down
18 changes: 2 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
import { Web3PluginBase } from "web3";

export class TemplatePlugin extends Web3PluginBase {
public pluginNamespace = "template";

public test(param: string): void {
console.log(param);
}
}

// Module Augmentation
declare module "web3" {
interface Web3Context {
template: TemplatePlugin;
}
}
export * from "./plugin";
export * from "./types";
33 changes: 33 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Web3PluginBase, utils } from "web3";
import { AddEthereumChainRequest } from "./types";

type WalletRpcApi = {
wallet_addEthereumChain: (param: AddEthereumChainRequest) => null;
};

export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> {
public pluginNamespace = "walletRpc";

public constructor() {
super();
}

public async addEthereumChain(param: AddEthereumChainRequest): Promise<null> {
return this.requestManager.send({
method: "wallet_addEthereumChain",
params: [
{
...param,
chainId: utils.toHex(param.chainId),
},
],
});
}
}

// Module Augmentation
declare module "web3" {
interface Web3Context {
walletRpc: WalletRpcPlugin;
}
}
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Numbers } from "web3";

export interface NativeCurrencyData {
name: string;
symbol: string;
decimals: number;
}

export interface AddEthereumChainRequest {
chainId: Numbers;
blockExplorerUrls?: string[];
chainName?: string;
iconUrls?: string[];
nativeCurrency?: NativeCurrencyData;
rpcUrls?: string[];
}
31 changes: 0 additions & 31 deletions test/index.test.ts

This file was deleted.

10 changes: 10 additions & 0 deletions test/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { core } from "web3";
import { WalletRpcPlugin } from "../src";

describe("WalletRpcPlugin", () => {
it("should register the plugin on Web3Context instance", () => {
const web3Context = new core.Web3Context("http://127.0.0.1:8545");
web3Context.registerPlugin(new WalletRpcPlugin());
expect(web3Context.walletRpc).toBeDefined();
});
});
30 changes: 30 additions & 0 deletions test/wallet_addEthereumChain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Web3 } from "web3";
import { WalletRpcPlugin } from "../src";

describe("WalletRpcPlugin", () => {
describe("wallet_addEthereumChain", () => {
let requestManagerSendSpy: jest.Mock;
let web3: Web3;

beforeAll(() => {
web3 = new Web3("http://127.0.0.1:8545");
web3.registerPlugin(new WalletRpcPlugin());

requestManagerSendSpy = jest.fn();
web3.requestManager.send = requestManagerSendSpy;
});

afterAll(() => {
requestManagerSendSpy.mockClear();
});

it("should call the method with expected params", async () => {
await web3.walletRpc.addEthereumChain({ chainId: 5000 });

expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: "wallet_addEthereumChain",
params: [{ chainId: "0x1388" }],
});
});
});
});
7 changes: 3 additions & 4 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["node_modules", "test"]
}
"extends": "./tsconfig.json",
"include": ["src"]
}

0 comments on commit 32f2226

Please sign in to comment.