-
Notifications
You must be signed in to change notification settings - Fork 1
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
5 changed files
with
134 additions
and
5 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,116 @@ | ||
import CyberApp from "./CyberApp"; | ||
import type { EIP1193Provider } from "./types"; | ||
import { availableChains, type ChainName } from "./config/chains"; | ||
import { EventEmitter } from "events"; | ||
import { | ||
ProviderDisconnectedError, | ||
createPublicClient, | ||
http, | ||
type PublicClient, | ||
} from "viem"; | ||
import Chain from "./Chain"; | ||
|
||
class CyberProvider extends EventEmitter implements EIP1193Provider { | ||
public name: "CyberWallet"; | ||
public cyberApp: CyberApp; | ||
public chainId: number; | ||
public readonly isMetaMask: any; | ||
public _events: { | ||
connect?: () => void; | ||
}; | ||
public chain?: Chain; | ||
public publicClient: PublicClient; | ||
|
||
constructor({ app, chainId }: { app: CyberApp; chainId: number }) { | ||
super(); | ||
this.name = "CyberWallet"; | ||
this.cyberApp = app; | ||
this.chainId = chainId; | ||
this.isMetaMask = false; | ||
this.chain = this.getChainByChainId(this.chainId); | ||
this._events = {}; | ||
this.publicClient = this.setPublicClient(this.chainId); | ||
} | ||
|
||
setPublicClient(chainId: number): PublicClient { | ||
return createPublicClient({ | ||
chain: Object.values(availableChains).find( | ||
(chain) => chain.id === chainId, | ||
)!, | ||
transport: http(), | ||
}); | ||
} | ||
|
||
async connect(): Promise<void> { | ||
this.emit("connect", { chainId: `0x${this.chainId.toString(16)}` }); | ||
return; | ||
} | ||
|
||
async disconnect(): Promise<void> { | ||
this.emit("disconnect", new ProviderDisconnectedError(Error())); | ||
return; | ||
} | ||
|
||
private getChainKeyByChainId(chainId: number) { | ||
const chainObj = Object.entries(availableChains).find( | ||
([_, chain]) => chain.id === chainId, | ||
); | ||
|
||
if (!chainObj) { | ||
throw new Error(`ChainId ${chainId} is not supported.`); | ||
} | ||
|
||
return chainObj[0] as ChainName; | ||
} | ||
|
||
private getChainByChainId(id: number) { | ||
const chainKey = this.getChainKeyByChainId(id); | ||
return this.cyberApp.cyberWallet?.[chainKey]; | ||
} | ||
|
||
async request(request: { method: string; params?: any[] }): Promise<any> { | ||
const { method, params = [] } = request; | ||
|
||
switch (method) { | ||
case "wallet_switchEthereumChain": { | ||
this.chainId = params[0].chainId; | ||
this.chain = this.getChainByChainId(Number(this.chainId)); | ||
this.publicClient = this.setPublicClient(Number(this.chainId)); | ||
this.emit("chainChanged", this.chainId); | ||
return; | ||
} | ||
|
||
case "eth_chainId": | ||
return `0x${this.chainId.toString(16)}`; | ||
|
||
case "eth_sendTransaction": { | ||
const data = params[0].data || "0x"; | ||
|
||
return this.chain?.sendTransaction({ ...params[0], data }); | ||
} | ||
|
||
case "eth_requestAccounts": { | ||
if (!this.cyberApp.cyberWallet?.cyberAccount) { | ||
await this.cyberApp.connect(); | ||
} | ||
|
||
return [this.cyberApp.cyberWallet?.cyberAccount?.address]; | ||
} | ||
|
||
case "eth_accounts": { | ||
if (!this.cyberApp.cyberWallet?.cyberAccount) { | ||
await this.cyberApp.connect(); | ||
} | ||
return [this.cyberApp.cyberWallet?.cyberAccount?.address]; | ||
} | ||
|
||
default: | ||
return await this.publicClient.request({ | ||
method: method as any, | ||
params: params as any, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default CyberProvider; |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { default as CyberApp } from "./CyberApp"; | ||
export { default as CyberWallet } from "./CyberWallet"; | ||
export { default as EventError } from "./EventError"; | ||
export { default as CyberProvider } from "./CyberProvider"; | ||
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