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: duplicate pop-up authn Iframe #367

Merged
merged 6 commits into from
Jan 11, 2024
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
6 changes: 6 additions & 0 deletions .changeset/unlucky-masks-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@blocto/web3-react-connector': patch
'@blocto/sdk': patch
---

fix web3-react-connector duplicate authn popup
84 changes: 40 additions & 44 deletions adapters/web3-react-connector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,47 @@ function parseChainId(chainId: string | number): number {
* @param options - Options to pass to Blocto SDK.
* @param onError - Handler to report errors thrown from eventListeners.
*/
type BloctoOptions = {
chainId: number;
rpc: string;
};

export interface BloctoConstructorArgs {
actions: Actions;
options: {
chainId: number;
rpc: string;
};
options: BloctoOptions;
onError?: (error: Error) => void;
}

export class BloctoConnector extends Connector {
public provider: any;
private bloctoSDK: BloctoSDK;

constructor({ actions, options, onError }: BloctoConstructorArgs) {
super(actions, onError);
this.bloctoSDK = new BloctoSDK({
const bloctoSDK = new BloctoSDK({
ethereum: {
chainId: options.chainId,
rpc: options.rpc,
},
});
this.provider = this.bloctoSDK.ethereum;
}

private async isomorphicInitialize(): Promise<void> {
this.provider.on('connect', ({ chainId }: ProviderConnectInfo): void => {
this.actions.update({ chainId: parseChainId(chainId) });
});
this.provider = bloctoSDK.ethereum;

this.provider.on(
'connect',
async ({ chainId }: ProviderConnectInfo): Promise<void> => {
const accounts = await this.provider.request({
method: 'eth_accounts',
});
this.actions.update({ chainId: parseChainId(chainId), accounts });
}
);
this.provider.on('disconnect', (error: ProviderRpcError): void => {
this.actions.resetState();
this.onError?.(error);
});
this.provider.on('chainChanged', (chainId: string): void => {
this.actions.update({ chainId: parseChainId(chainId) });
this.provider.on('chainChanged', async (chainId: string): Promise<void> => {
const accounts = await this.provider.request({ method: 'eth_accounts' });
this.actions.update({ chainId: parseChainId(chainId), accounts });
});
this.provider.on('accountsChanged', (accounts: string[]): void => {
if (accounts.length === 0) this.actions.resetState();
Expand All @@ -65,49 +72,38 @@ export class BloctoConnector extends Connector {
typeof desiredChainIdOrChainParameters === 'number'
? desiredChainIdOrChainParameters
: desiredChainIdOrChainParameters?.chainId;
await this.isomorphicInitialize();

if (!this.provider) throw new Error('No provider');
if (
!desiredChainId ||
parseChainId(desiredChainId) === parseChainId(this.provider.chainId)
) {
const accounts = await this.provider.request({ method: 'eth_requestAccounts' });
const accounts = await this.provider.request({
method: 'eth_requestAccounts',
});

return this.actions.update({
chainId: parseChainId(this.provider.chainId),
accounts,
});
} else if (typeof desiredChainIdOrChainParameters === 'number') {
await this.provider
.request({
method: 'wallet_addEthereumChain',
params: [{ chainId: desiredChainId }],
})
.then(() => {
this.provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredChainId }],
});
});
await this.activate(desiredChainId);
} else {
// AddEthereumChainParameter
await this.provider
.request({
method: 'wallet_addEthereumChain',
params: [desiredChainIdOrChainParameters],
})
.then(() => {
this.provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredChainId }],
});
});
await this.activate(desiredChainId);
}
const addEthereumChainParameters =
typeof desiredChainIdOrChainParameters === 'number'
? { chainId: desiredChainId }
: desiredChainIdOrChainParameters;

await this.provider.request({
method: 'wallet_addEthereumChain',
params: [addEthereumChainParameters],
});
await this.provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredChainId }],
});
mordochi marked this conversation as resolved.
Show resolved Hide resolved
}

public deactivate(): void {
this.provider?.handleDisconnect();
this.provider.request({ method: 'wallet_disconnect' });
this.actions.resetState();
}
}
14 changes: 9 additions & 5 deletions packages/blocto-sdk/src/providers/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,13 @@ export default class EthereumProvider
`Get support chain failed: ${this.networkVersion} might not be supported yet.`
);
}
const walletServer =
this.injectedWalletServer ||
ETH_ENV_WALLET_SERVER_MAPPING[blocto_service_environment];
this._blocto = {
...this._blocto,
sessionKeyEnv: ETH_SESSION_KEY_MAPPING[blocto_service_environment],
walletServer:
this.injectedWalletServer ||
ETH_ENV_WALLET_SERVER_MAPPING[blocto_service_environment],
walletServer,
blockchainName: name,
networkType: network_type,
switchableNetwork: {
Expand All @@ -163,7 +164,7 @@ export default class EthereumProvider
name,
display_name,
network_type,
wallet_web_url: this._blocto.walletServer,
wallet_web_url: walletServer,
rpc_url: this.rpc,
},
},
Expand Down Expand Up @@ -443,6 +444,8 @@ export default class EthereumProvider
case 'wallet_addEthereumChain': {
return this.loadSwitchableNetwork(payload?.params || []);
}
case 'eth_blockNumber':
Copy link
Member Author

Choose a reason for hiding this comment

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

uniswap makes periodic calls to eth_blockNumber, adding eth_blockNumber to avoid falling into the default switch block that requires user to be connected (L527)

case 'web3_clientVersion':
case 'eth_call': {
const response = await this.handleReadRequests(payload);
if (!response || (response && !response.result && response.error)) {
Expand All @@ -462,8 +465,9 @@ export default class EthereumProvider
case 'wallet_disconnect': {
return this.handleDisconnect();
}
case 'eth_accounts':
case 'eth_accounts': {
return getEvmAddress(sessionKeyEnv, blockchainName) || [];
}
}

// Method that requires user to be connected
q20274982 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading