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 addEthereumChain
Signed-off-by: Kris Urbas <[email protected]>
- Loading branch information
Showing
9 changed files
with
140 additions
and
78 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", | ||
"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'", | ||
|
@@ -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,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; | ||
} | ||
} |
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,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[]; | ||
} |
This file was deleted.
Oops, something went wrong.
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,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(); | ||
}); | ||
}); |
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,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" }], | ||
}); | ||
}); | ||
}); | ||
}); |
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,5 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src"], | ||
"exclude": ["node_modules", "test"] | ||
} | ||
"extends": "./tsconfig.json", | ||
"include": ["src"] | ||
} |