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.
- Loading branch information
Showing
12 changed files
with
590 additions
and
58 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
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,152 @@ | ||
import { Web3PluginBase, utils, validator } from "web3"; | ||
import { | ||
AddEthereumChainRequest, | ||
GetOwnedAssetsRequest, | ||
GetOwnedAssetsResult, | ||
SwitchEthereumChainRequest, | ||
UpdateEthereumChainRequest, | ||
WatchAssetRequest, | ||
} from "./types"; | ||
import { parseToGetOwnedAssetsResult } from "./utils"; | ||
|
||
type WalletRpcApi = { | ||
wallet_addEthereumChain: (param: AddEthereumChainRequest) => void; | ||
wallet_updateEthereumChain: (param: UpdateEthereumChainRequest) => void; | ||
wallet_switchEthereumChain: (param: SwitchEthereumChainRequest) => void; | ||
wallet_getOwnedAssets: (param: GetOwnedAssetsRequest) => GetOwnedAssetsResult; | ||
wallet_watchAsset: (param: WatchAssetRequest) => boolean; | ||
}; | ||
|
||
/** | ||
* This Web3.js plugin adds support for various wallet-related RPC methods. | ||
* | ||
* @example | ||
* Initialize the plugin | ||
* | ||
* ```typescript | ||
* import { Web3 } from "web3"; | ||
* import { WalletRpcPlugin } from "web3-plugin-wallet-rpc"; | ||
* | ||
* const web3 = new Web3("https://eth.llamarpc.com"); | ||
* web3.registerPlugin(new WalletRpcPlugin()); | ||
* ``` | ||
*/ | ||
export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> { | ||
public pluginNamespace = "walletRpc"; | ||
|
||
public constructor() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Request to add a new chain to the user's wallet. | ||
* | ||
* 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 | ||
*/ | ||
public async addEthereumChain(param: AddEthereumChainRequest): Promise<void> { | ||
return this.requestManager.send({ | ||
method: "wallet_addEthereumChain", | ||
params: [ | ||
{ | ||
...param, | ||
chainId: utils.toHex(param.chainId), | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
/** | ||
* Switch to a new chain and register it with the user’s wallet if it isn’t already recognized. | ||
* | ||
* 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 | ||
*/ | ||
public async updateEthereumChain( | ||
param: UpdateEthereumChainRequest | ||
): Promise<void> { | ||
return this.requestManager.send({ | ||
method: "wallet_updateEthereumChain", | ||
params: [ | ||
{ | ||
...param, | ||
chainId: utils.toHex(param.chainId), | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
/** | ||
* Switch the wallet’s currently active chain. | ||
* | ||
* See [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326) for more details. | ||
* | ||
* @param param - See {@link SwitchEthereumChainRequest} | ||
* @returns a Promise that resolves if the request is successful | ||
*/ | ||
public async switchEthereumChain( | ||
param: SwitchEthereumChainRequest | ||
): Promise<void> { | ||
return this.requestManager.send({ | ||
method: "wallet_switchEthereumChain", | ||
params: [ | ||
{ | ||
...param, | ||
chainId: utils.toHex(param.chainId), | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
/** | ||
* Return a list of owned assets for the given address. | ||
* | ||
* 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, see {@link GetOwnedAssetsResult} | ||
*/ | ||
public async getOwnedAssets( | ||
param: GetOwnedAssetsRequest | ||
): Promise<GetOwnedAssetsResult> { | ||
validator.validator.validate(["address"], [param.address]); | ||
|
||
const trueParam = { ...param }; | ||
if (trueParam.options?.chainId) { | ||
trueParam.options.chainId = utils.toHex(trueParam.options.chainId); | ||
} | ||
|
||
const result = await this.requestManager.send({ | ||
method: "wallet_getOwnedAssets", | ||
params: [trueParam], | ||
}); | ||
|
||
return parseToGetOwnedAssetsResult(result); | ||
} | ||
|
||
/** | ||
* Add an asset to the user's wallet. | ||
* | ||
* 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 | ||
*/ | ||
public async watchAsset(param: WatchAssetRequest): Promise<boolean> { | ||
return this.requestManager.send({ | ||
method: "wallet_watchAsset", | ||
params: [param], | ||
}); | ||
} | ||
} | ||
|
||
// 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./plugin"; | ||
export * from "./WalletRpcPlugin"; | ||
export * from "./types"; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.