generated from web3/web3.js-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configure the plugin, add first method
- Loading branch information
Kris Urbas
committed
Oct 22, 2024
1 parent
a342b7b
commit d90a0b9
Showing
6 changed files
with
116 additions
and
56 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
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/) |
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 |
---|---|---|
@@ -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", | ||
"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'", | ||
|
@@ -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", | ||
|
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 |
---|---|---|
@@ -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"; |
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,38 @@ | ||
import { Web3PluginBase } from "web3"; | ||
import { AddEthereumChainParameter } from "./types"; | ||
|
||
type WalletRpcApi = { | ||
addEthereumChain: () => string; | ||
updateEthereumChain: () => string; | ||
switchEthereumChain: () => string; | ||
getOwnedAssets: () => string; | ||
watchAsset: () => string; | ||
requestPermissions: () => string; | ||
getPermissions: () => string; | ||
}; | ||
|
||
export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> { | ||
public pluginNamespace = "walletRpc"; | ||
|
||
public constructor() { | ||
super(); | ||
} | ||
|
||
public async addEthereumChain( | ||
param: AddEthereumChainParameter | ||
): Promise<null> { | ||
// validate ? | ||
|
||
return this.requestManager.send({ | ||
method: "wallet_addEthereumChain", | ||
params: [param], // encode? | ||
}); | ||
} | ||
} | ||
|
||
// Module Augmentation | ||
declare module "web3" { | ||
interface Web3Context { | ||
walletRpc: WalletRpcPlugin; | ||
} | ||
} |
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,12 @@ | ||
export interface AddEthereumChainParameter { | ||
chainId: string; // should be a 0x-prefixed hexadecimal string per EIP-695 | ||
blockExplorerUrls?: string[]; | ||
chainName?: string; | ||
iconUrls?: string[]; | ||
nativeCurrency?: { | ||
name: string; | ||
symbol: string; | ||
decimals: number; | ||
}; | ||
rpcUrls?: string[]; | ||
} |
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 |
---|---|---|
@@ -1,31 +1,36 @@ | ||
import { Web3, core } from "web3"; | ||
import { TemplatePlugin } from "../src"; | ||
import { WalletRpcPlugin } from "../src"; | ||
|
||
describe("TemplatePlugin Tests", () => { | ||
it("should register TemplatePlugin plugin on Web3Context instance", () => { | ||
describe("WalletRpcPlugin Tests", () => { | ||
it("should register the plugin on Web3Context instance", () => { | ||
const web3Context = new core.Web3Context("http://127.0.0.1:8545"); | ||
web3Context.registerPlugin(new TemplatePlugin()); | ||
expect(web3Context.template).toBeDefined(); | ||
web3Context.registerPlugin(new WalletRpcPlugin()); | ||
expect(web3Context.walletRpc).toBeDefined(); | ||
}); | ||
|
||
describe("TemplatePlugin method tests", () => { | ||
let consoleSpy: jest.SpiedFunction<typeof global.console.log>; | ||
|
||
describe("plugin method tests", () => { | ||
let requestManagerSendSpy: jest.Mock; | ||
let web3: Web3; | ||
|
||
beforeAll(() => { | ||
web3 = new Web3("http://127.0.0.1:8545"); | ||
web3.registerPlugin(new TemplatePlugin()); | ||
consoleSpy = jest.spyOn(global.console, "log").mockImplementation(); | ||
web3.registerPlugin(new WalletRpcPlugin()); | ||
|
||
requestManagerSendSpy = jest.fn(); | ||
web3.requestManager.send = requestManagerSendSpy; | ||
}); | ||
|
||
afterAll(() => { | ||
consoleSpy.mockRestore(); | ||
requestManagerSendSpy.mockClear(); | ||
}); | ||
|
||
it("should call TempltyPlugin test method with expected param", () => { | ||
web3.template.test("test-param"); | ||
expect(consoleSpy).toHaveBeenCalledWith("test-param"); | ||
it("should call addEthereumChain method with expected param", async () => { | ||
await web3.walletRpc.addEthereumChain({ chainId: "5000" }); | ||
|
||
expect(requestManagerSendSpy).toHaveBeenCalledWith({ | ||
method: "wallet_addEthereumChain", | ||
params: [{ chainId: "5000" }], | ||
}); | ||
}); | ||
}); | ||
}); |