-
Notifications
You must be signed in to change notification settings - Fork 255
feat: add Base Account SDK connector #2340
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
Open
youssefea
wants to merge
1
commit into
Web3Auth:v10
Choose a base branch
from
youssefea:feat/base-account-connector
base: v10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 @@ | ||
| export * from "@web3auth/no-modal/connectors/base-account-connector"; |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -33,12 +33,16 @@ | |
| ], | ||
| "peerDependencies": { | ||
| "@babel/runtime": "^7.x", | ||
| "@base-org/account": "^2.5.1", | ||
| "@coinbase/wallet-sdk": "^4.3.x", | ||
| "react": ">=18", | ||
| "viem": ">=2.29", | ||
| "vue": "^3.x" | ||
| }, | ||
| "peerDependenciesMeta": { | ||
| "@base-org/account": { | ||
| "optional": true | ||
| }, | ||
| "@coinbase/wallet-sdk": { | ||
| "optional": true | ||
| }, | ||
|
|
@@ -92,6 +96,7 @@ | |
| "xrpl": "^2.14.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@base-org/account": "^2.5.1", | ||
| "@coinbase/wallet-sdk": "^4.3.7", | ||
| "@types/elliptic": "6.4.18", | ||
| "@types/json-rpc-random-id": "^1.0.3", | ||
|
|
@@ -139,6 +144,11 @@ | |
| "require": "./dist/lib.cjs/vue/wagmi/index.js", | ||
| "types": "./dist/lib.cjs/types/vue/wagmi/index.d.ts" | ||
| }, | ||
| "./connectors/base-account-connector": { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to follow our conventions, pls re-export from modal package as well |
||
| "import": "./dist/lib.esm/connectors/base-account-connector/index.js", | ||
| "require": "./dist/lib.cjs/connectors/base-account-connector/index.js", | ||
| "types": "./dist/lib.cjs/types/connectors/base-account-connector/index.d.ts" | ||
| }, | ||
| "./connectors/coinbase-connector": { | ||
| "import": "./dist/lib.esm/connectors/coinbase-connector/index.js", | ||
| "require": "./dist/lib.cjs/connectors/coinbase-connector/index.js", | ||
|
|
@@ -172,6 +182,9 @@ | |
| "vue/solana": [ | ||
| "./dist/lib.cjs/types/vue/solana/index.d.ts" | ||
| ], | ||
| "connectors/base-account-connector": [ | ||
| "./dist/lib.cjs/types/connectors/base-account-connector/index.d.ts" | ||
| ], | ||
| "connectors/coinbase-connector": [ | ||
| "./dist/lib.cjs/types/connectors/coinbase-connector/index.d.ts" | ||
| ], | ||
|
|
||
This file contains hidden or 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
236 changes: 236 additions & 0 deletions
236
packages/no-modal/src/connectors/base-account-connector/baseAccountConnector.ts
This file contains hidden or 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,236 @@ | ||
| import type { AppMetadata, Preference } from "@base-org/account"; | ||
|
|
||
| import { | ||
| BaseConnectorLoginParams, | ||
| BaseConnectorSettings, | ||
| CHAIN_NAMESPACES, | ||
| ChainNamespaceType, | ||
| CONNECTED_EVENT_DATA, | ||
| CONNECTOR_CATEGORY, | ||
| CONNECTOR_CATEGORY_TYPE, | ||
| CONNECTOR_EVENTS, | ||
| CONNECTOR_NAMESPACES, | ||
| CONNECTOR_STATUS, | ||
| CONNECTOR_STATUS_TYPE, | ||
| ConnectorFn, | ||
| ConnectorInitOptions, | ||
| ConnectorNamespaceType, | ||
| ConnectorParams, | ||
| IdentityTokenInfo, | ||
| IProvider, | ||
| UserInfo, | ||
| WALLET_CONNECTOR_TYPE, | ||
| WALLET_CONNECTORS, | ||
| WalletLoginError, | ||
| Web3AuthError, | ||
| } from "../../base"; | ||
| import { BaseEvmConnector } from "../base-evm-connector"; | ||
| import { getSiteIcon, getSiteName } from "../utils"; | ||
|
|
||
| export type BaseAccountSDKOptions = Partial<AppMetadata & { preference?: Preference; paymasterUrls?: Record<number, string> }>; | ||
|
|
||
| export interface BaseAccountConnectorOptions extends BaseConnectorSettings { | ||
| connectorSettings?: BaseAccountSDKOptions; | ||
| } | ||
|
|
||
| interface BaseAccountProvider { | ||
| request<T>(args: { method: string; params?: unknown[] }): Promise<T>; | ||
| on(event: string, listener: (...args: unknown[]) => void): void; | ||
| once(event: string, listener: (...args: unknown[]) => void): void; | ||
| removeAllListeners(): void; | ||
| } | ||
|
|
||
| interface ProviderRpcError extends Error { | ||
| code: number; | ||
| } | ||
|
|
||
| class BaseAccountConnector extends BaseEvmConnector<void> { | ||
| readonly connectorNamespace: ConnectorNamespaceType = CONNECTOR_NAMESPACES.EIP155; | ||
|
|
||
| readonly currentChainNamespace: ChainNamespaceType = CHAIN_NAMESPACES.EIP155; | ||
|
|
||
| readonly type: CONNECTOR_CATEGORY_TYPE = CONNECTOR_CATEGORY.EXTERNAL; | ||
|
|
||
| readonly name: WALLET_CONNECTOR_TYPE = WALLET_CONNECTORS.BASE_ACCOUNT; | ||
|
|
||
| public status: CONNECTOR_STATUS_TYPE = CONNECTOR_STATUS.NOT_READY; | ||
|
|
||
| private baseAccountProvider: BaseAccountProvider | null = null; | ||
|
|
||
| private baseAccountOptions: BaseAccountSDKOptions = { appName: "Web3Auth" }; | ||
|
|
||
| constructor(connectorOptions: BaseAccountConnectorOptions) { | ||
| super(connectorOptions); | ||
| this.baseAccountOptions = { ...this.baseAccountOptions, ...connectorOptions.connectorSettings }; | ||
| } | ||
|
|
||
| get provider(): IProvider | null { | ||
| if (this.status !== CONNECTOR_STATUS.NOT_READY && this.baseAccountProvider) { | ||
| return this.baseAccountProvider as unknown as IProvider; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| set provider(_: IProvider | null) { | ||
| throw new Error("Not implemented"); | ||
| } | ||
|
|
||
| async init(options: ConnectorInitOptions): Promise<void> { | ||
| await super.init(options); | ||
| const chainConfig = this.coreOptions.chains.find((x) => x.chainId === options.chainId); | ||
| super.checkInitializationRequirements({ chainConfig }); | ||
|
|
||
| const { createBaseAccountSDK } = await import("@base-org/account"); | ||
|
|
||
| // Derive defaults from site metadata if available | ||
| let appName = this.baseAccountOptions.appName || "Web3Auth"; | ||
| let appLogoUrl = this.baseAccountOptions.appLogoUrl || ""; | ||
|
|
||
| if (typeof window !== "undefined") { | ||
| if (!this.baseAccountOptions.appName) { | ||
| appName = getSiteName(window) || "Web3Auth"; | ||
| } | ||
| if (!this.baseAccountOptions.appLogoUrl) { | ||
| appLogoUrl = (await getSiteIcon(window)) || ""; | ||
| } | ||
| } | ||
|
|
||
| // Derive appChainIds from all EIP155 chains | ||
| const appChainIds = this.coreOptions.chains | ||
| .filter((x) => x.chainNamespace === CHAIN_NAMESPACES.EIP155) | ||
| .map((x) => Number.parseInt(x.chainId, 16)); | ||
|
|
||
| const sdk = createBaseAccountSDK({ | ||
| ...this.baseAccountOptions, | ||
| appName, | ||
| appLogoUrl: appLogoUrl || null, | ||
| appChainIds: this.baseAccountOptions.appChainIds || appChainIds, | ||
| }); | ||
|
|
||
| this.baseAccountProvider = sdk.getProvider() as unknown as BaseAccountProvider; | ||
| this.status = CONNECTOR_STATUS.READY; | ||
| this.emit(CONNECTOR_EVENTS.READY, WALLET_CONNECTORS.BASE_ACCOUNT); | ||
|
|
||
| try { | ||
| if (options.autoConnect) { | ||
| this.rehydrated = true; | ||
| const provider = await this.connect({ chainId: options.chainId, getIdentityToken: options.getIdentityToken }); | ||
| if (!provider) { | ||
| this.rehydrated = false; | ||
| throw WalletLoginError.connectionError("Failed to rehydrate."); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| this.emit(CONNECTOR_EVENTS.REHYDRATION_ERROR, error as Web3AuthError); | ||
| } | ||
| } | ||
|
|
||
| async connect({ chainId, getIdentityToken }: BaseConnectorLoginParams): Promise<IProvider | null> { | ||
| super.checkConnectionRequirements(); | ||
| if (!this.baseAccountProvider) throw WalletLoginError.notConnectedError("Connector is not initialized"); | ||
|
|
||
| this.status = CONNECTOR_STATUS.CONNECTING; | ||
| this.emit(CONNECTOR_EVENTS.CONNECTING, { connector: WALLET_CONNECTORS.BASE_ACCOUNT }); | ||
|
|
||
| try { | ||
| const chainConfig = this.coreOptions.chains.find((x) => x.chainId === chainId); | ||
| if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available"); | ||
|
|
||
| await this.baseAccountProvider.request({ method: "eth_requestAccounts" }); | ||
| const currentChainId = await this.baseAccountProvider.request<string>({ method: "eth_chainId" }); | ||
|
|
||
| if (currentChainId !== chainConfig.chainId) { | ||
| await this.switchChain(chainConfig, true); | ||
| } | ||
|
|
||
| this.status = CONNECTOR_STATUS.CONNECTED; | ||
| if (!this.provider) throw WalletLoginError.notConnectedError("Failed to connect with provider"); | ||
|
|
||
| this.provider.once("disconnect", () => { | ||
| this.disconnect(); | ||
| }); | ||
|
|
||
| let identityTokenInfo: IdentityTokenInfo | undefined; | ||
|
|
||
| this.emit(CONNECTOR_EVENTS.CONNECTED, { | ||
| connector: WALLET_CONNECTORS.BASE_ACCOUNT, | ||
| reconnected: this.rehydrated, | ||
| provider: this.provider, | ||
| identityTokenInfo, | ||
| } as CONNECTED_EVENT_DATA); | ||
|
|
||
| if (getIdentityToken) { | ||
| identityTokenInfo = await this.getIdentityToken(); | ||
| } | ||
|
|
||
| return this.provider; | ||
| } catch (error) { | ||
| this.status = CONNECTOR_STATUS.READY; | ||
| if (!this.rehydrated) this.emit(CONNECTOR_EVENTS.ERRORED, error as Web3AuthError); | ||
| this.rehydrated = false; | ||
| if (error instanceof Web3AuthError) throw error; | ||
| throw WalletLoginError.connectionError("Failed to login with Base Account", error); | ||
| } | ||
| } | ||
|
|
||
| async disconnect(options: { cleanup: boolean } = { cleanup: false }): Promise<void> { | ||
| await super.disconnectSession(); | ||
| this.provider?.removeAllListeners(); | ||
| if (options.cleanup) { | ||
| this.status = CONNECTOR_STATUS.NOT_READY; | ||
| this.baseAccountProvider = null; | ||
| } else { | ||
| this.status = CONNECTOR_STATUS.READY; | ||
| } | ||
| await super.disconnect(); | ||
| } | ||
|
|
||
| async getUserInfo(): Promise<Partial<UserInfo>> { | ||
| if (!this.canAuthorize) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); | ||
| return {}; | ||
| } | ||
|
|
||
| public async switchChain(params: { chainId: string }, init = false): Promise<void> { | ||
| super.checkSwitchChainRequirements(params, init); | ||
| try { | ||
| await this.baseAccountProvider?.request({ method: "wallet_switchEthereumChain", params: [{ chainId: params.chainId }] }); | ||
| } catch (switchError: unknown) { | ||
| if ((switchError as ProviderRpcError).code === 4902) { | ||
| const chainConfig = this.coreOptions.chains.find((x) => x.chainId === params.chainId); | ||
| if (!chainConfig) throw WalletLoginError.connectionError("Chain config is not available"); | ||
| await this.baseAccountProvider?.request({ | ||
| method: "wallet_addEthereumChain", | ||
| params: [ | ||
| { | ||
| chainId: chainConfig.chainId, | ||
| rpcUrls: [chainConfig.rpcTarget], | ||
| chainName: chainConfig.displayName, | ||
| nativeCurrency: { name: chainConfig.tickerName, symbol: chainConfig.ticker, decimals: chainConfig.decimals || 18 }, | ||
| blockExplorerUrls: [chainConfig.blockExplorerUrl], | ||
| iconUrls: [chainConfig.logo], | ||
| }, | ||
| ], | ||
| }); | ||
| return; | ||
| } | ||
| throw switchError; | ||
| } | ||
| } | ||
|
|
||
| public async enableMFA(): Promise<void> { | ||
| throw new Error("Method Not implemented"); | ||
| } | ||
|
|
||
| public async manageMFA(): Promise<void> { | ||
| throw new Error("Method Not implemented"); | ||
| } | ||
| } | ||
|
|
||
| export const baseAccountConnector = (params?: BaseAccountSDKOptions): ConnectorFn => { | ||
| return ({ coreOptions }: ConnectorParams) => { | ||
| return new BaseAccountConnector({ | ||
| connectorSettings: params, | ||
| coreOptions, | ||
| }); | ||
| }; | ||
| }; |
1 change: 1 addition & 0 deletions
1
packages/no-modal/src/connectors/base-account-connector/index.ts
This file contains hidden or 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 @@ | ||
| export * from "./baseAccountConnector"; |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused constant
BASE_ACCOUNT_LOGOis exported but never usedLow Severity
The
BASE_ACCOUNT_LOGOconstant is defined and exported but is not imported or used anywhere in the codebase. UnlikeWALLET_CONNECT_LOGOwhich is used inConnectWalletQrCode.tsx, this constant is dead code.