diff --git a/packages/blocto-sdk/src/providers/aptos.ts b/packages/blocto-sdk/src/providers/aptos.ts index b6bf9b8f..b14487f4 100644 --- a/packages/blocto-sdk/src/providers/aptos.ts +++ b/packages/blocto-sdk/src/providers/aptos.ts @@ -66,6 +66,10 @@ export default class AptosProvider api?: string; sessionKey: KEY_SESSION; + private get existedSDK() { + return (window as any).bloctoAptos; + } + constructor({ chainId, server, appId }: AptosProviderConfig) { super(); @@ -111,9 +115,8 @@ export default class AptosProvider async signTransaction( transaction: unknown ): Promise { - const existedSDK = (window as any).bloctoAptos; - if (existedSDK) { - return existedSDK.signTransaction(transaction); + if (this.existedSDK) { + return this.existedSDK.signTransaction(transaction); } const hasConnected = await this.isConnected(); @@ -127,9 +130,8 @@ export default class AptosProvider } async disconnect(): Promise { - const existedSDK = (window as any).bloctoAptos; - if (existedSDK) { - await existedSDK.disconnect(); + if (this.existedSDK) { + await this.existedSDK.disconnect(); return; } removeChainAddress(this.sessionKey, CHAIN.APTOS); @@ -145,10 +147,9 @@ export default class AptosProvider transaction: AptosTypes.TransactionPayload, txOptions: TxOptions = {} ): Promise<{ hash: AptosTypes.HexEncodedBytes }> { - const existedSDK = (window as any).bloctoAptos; - if (existedSDK) { - return existedSDK.signAndSubmitTransaction(transaction, txOptions); + if (this.existedSDK) { + return this.existedSDK.signAndSubmitTransaction(transaction, txOptions); } const hasConnected = await this.isConnected(); @@ -216,12 +217,11 @@ export default class AptosProvider } async signMessage(payload: SignMessagePayload): Promise { - const existedSDK = (window as any).bloctoAptos; const formattedPayload = checkMessagePayloadFormat(payload); - if (existedSDK) { - return existedSDK.signMessage(formattedPayload); + if (this.existedSDK) { + return this.existedSDK.signMessage(formattedPayload); } const hasConnected = await this.isConnected(); @@ -288,11 +288,10 @@ export default class AptosProvider } async connect(): Promise { - const existedSDK = (window as any).bloctoAptos; - if (existedSDK) { + if (this.existedSDK) { return new Promise((resolve, reject) => // add a small delay to make sure the network has been switched - setTimeout(() => existedSDK.connect().then(resolve).catch(reject), 10) + setTimeout(() => this.existedSDK.connect().then(resolve).catch(reject), 10) ); } @@ -405,4 +404,20 @@ export default class AptosProvider setChainAddress(this.sessionKey, CHAIN.APTOS, accounts); return accounts?.[0] || ''; } + + override on(event: string, listener: (arg: any) => void): void { + if (this.existedSDK) + this.existedSDK.on(event, listener); + + super.on(event, listener); + } + + override removeListener(event: string, listener: (arg: any) => void): void { + if (this.existedSDK) + this.existedSDK.off(event, listener); + + super.off(event, listener); + } + + off = this.removeListener; } diff --git a/packages/blocto-sdk/src/providers/ethereum.ts b/packages/blocto-sdk/src/providers/ethereum.ts index a67636fe..b8737bdf 100644 --- a/packages/blocto-sdk/src/providers/ethereum.ts +++ b/packages/blocto-sdk/src/providers/ethereum.ts @@ -71,6 +71,10 @@ export default class EthereumProvider switchableNetwork: SwitchableNetwork; }; + private get existedSDK() { + return (window as any).ethereum; + } + constructor({ chainId, rpc, walletServer, appId }: EthereumProviderConfig) { super(); // setup chainId @@ -170,11 +174,9 @@ export default class EthereumProvider } #checkNetworkMatched(): void { - const existedSDK = (window as any).ethereum; if ( - existedSDK && - existedSDK.isBlocto && - parseChainId(existedSDK.chainId) !== parseChainId(this.chainId) + this.existedSDK?.isBlocto && + parseChainId(this.existedSDK.chainId) !== parseChainId(this.chainId) ) { throw ethErrors.provider.chainDisconnected(); } @@ -307,13 +309,16 @@ export default class EthereumProvider async request(payload: EIP1193RequestPayload): Promise { if (!payload?.method) throw ethErrors.rpc.invalidRequest(); - const existedSDK = (window as any).ethereum; - if (existedSDK && existedSDK.isBlocto) { + + const { blockchainName, switchableNetwork, sessionKey } = + await this.#getBloctoProperties(); + + if (this.existedSDK?.isBlocto) { if (payload.method === 'wallet_switchEthereumChain') { if (!payload?.params?.[0]?.chainId) { throw ethErrors.rpc.invalidParams(); } - return existedSDK.request(payload).then(() => { + return this.existedSDK.request(payload).then(() => { this.networkVersion = `${parseChainId(payload?.params?.[0].chainId)}`; this.chainId = `0x${parseChainId( payload?.params?.[0].chainId @@ -322,12 +327,9 @@ export default class EthereumProvider return null; }); } - return existedSDK.request(payload); + return this.existedSDK.request(payload); } - const { blockchainName, switchableNetwork, sessionKey } = - await this.#getBloctoProperties(); - // method that doesn't require user to be connected switch (payload.method) { case 'eth_chainId': { @@ -537,22 +539,21 @@ export default class EthereumProvider const { walletServer, blockchainName, sessionKey } = await this.#getBloctoProperties(); - const existedSDK = (window as any).ethereum; - if (existedSDK && existedSDK.isBlocto) { - if (existedSDK.chainId !== this.chainId) { - await existedSDK.request({ + if (this.existedSDK?.isBlocto) { + if (this.existedSDK.chainId !== this.chainId) { + await this.existedSDK.request({ method: 'wallet_addEthereumChain', params: [{ chainId: this.chainId }], }); - await existedSDK.request({ + await this.existedSDK.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: this.chainId }], }); - setEvmAddress(sessionKey, blockchainName, [existedSDK.address]); + setEvmAddress(sessionKey, blockchainName, [this.existedSDK.address]); } return new Promise((resolve, reject) => // add a small delay to make sure the network has been switched - setTimeout(() => existedSDK.enable().then(resolve).catch(reject), 10) + setTimeout(() => this.existedSDK.enable().then(resolve).catch(reject), 10) ); } @@ -872,9 +873,9 @@ export default class EthereumProvider } async handleDisconnect(): Promise { - const existedSDK = (window as any).ethereum; - if (existedSDK && existedSDK.isBlocto) { - return existedSDK.disconnect(); + + if (this.existedSDK?.isBlocto) { + return this.existedSDK.request({ method: 'wallet_disconnect' }); } const { sessionKey } = await this.#getBloctoProperties(); removeAllEvmAddress(sessionKey); @@ -908,4 +909,20 @@ export default class EthereumProvider throw ethErrors.rpc.invalidParams('Empty networkList'); } } + + override on(event: string, listener: (arg: any) => void): void { + if (this.existedSDK?.isBlocto) + this.existedSDK.on(event, listener); + + super.on(event, listener); + } + + override removeListener(event: string, listener: (arg: any) => void): void { + if (this.existedSDK?.isBlocto) + this.existedSDK.off(event, listener); + + super.off(event, listener); + } + + off = this.removeListener; }