Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/not changing the switched chain id in in app browser #288

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions packages/blocto-sdk/src/providers/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -111,9 +115,8 @@ export default class AptosProvider
async signTransaction(
transaction: unknown
): Promise<AptosTypes.SubmitTransactionRequest> {
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();
Expand All @@ -127,9 +130,8 @@ export default class AptosProvider
}

async disconnect(): Promise<void> {
const existedSDK = (window as any).bloctoAptos;
if (existedSDK) {
await existedSDK.disconnect();
if (this.existedSDK) {
await this.existedSDK.disconnect();
return;
}
removeChainAddress(this.sessionKey, CHAIN.APTOS);
Expand All @@ -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();
Expand Down Expand Up @@ -216,12 +217,11 @@ export default class AptosProvider
}

async signMessage(payload: SignMessagePayload): Promise<SignMessageResponse> {
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();
Expand Down Expand Up @@ -288,11 +288,10 @@ export default class AptosProvider
}

async connect(): Promise<PublicAccount> {
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)
);
}

Expand Down Expand Up @@ -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;
}
59 changes: 38 additions & 21 deletions packages/blocto-sdk/src/providers/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -307,13 +309,16 @@ export default class EthereumProvider
async request(payload: EIP1193RequestPayload): Promise<any> {
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
Expand All @@ -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': {
Expand Down Expand Up @@ -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)
);
}

Expand Down Expand Up @@ -872,9 +873,9 @@ export default class EthereumProvider
}

async handleDisconnect(): Promise<void> {
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);
Expand Down Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the condition if (this.existedSDK?.isBlocto) is more concise and achieves the same effect as const existedSDK = (window as any).ethereum; if (existedSDK && existedSDK.isBlocto). If there are no issues, I'll replace const existedSDK = (window as any).ethereum; if (existedSDK && existedSDK.isBlocto) with it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced all the const existedSDK = (window as any).ethereum; if (existedSDK && existedSDK.isBlocto) with if (this.existedSDK?.isBlocto) in this commit refactor: enhance existedSDK check condition

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;
}
Loading