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

feat: Injected Universal Provider #3177

Merged
merged 20 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
22 changes: 22 additions & 0 deletions .changeset/lovely-dragons-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@reown/appkit-scaffold-ui': patch
'@reown/appkit': patch
'@reown/appkit-core': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-common': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
'@reown/appkit-wallet-button': patch
---

Adds walletConnectProvider and enableUniversalProviderManualControl flags to inject a UniversalProvider instance to be used by AppKit'
58 changes: 58 additions & 0 deletions apps/laboratory/src/pages/library/custom-universal-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { AppKit, createAppKit } from '@reown/appkit/react'
import { ThemeStore } from '../../utils/StoreUtil'
import { ConstantsUtil } from '../../utils/ConstantsUtil'
import { AppKitButtons } from '../../components/AppKitButtons'
import { mainnet } from '@reown/appkit/networks'
import { MultiChainInfo } from '../../components/MultiChainInfo'
import { UpaTests } from '../../components/UPA/UpaTests'
import Provider, { UniversalProvider } from '@walletconnect/universal-provider'
import { useEffect, useState } from 'react'

const networks = ConstantsUtil.EvmNetworks

export default function MultiChainWagmiAdapterOnly() {
const [uprovider, setUprovider] = useState<Provider | null>(null)
const [appkit, setAppKit] = useState<AppKit | null>(null)

async function initializeUniversalProvider() {
const provider = await UniversalProvider.init({
projectId: ConstantsUtil.ProjectId
})

const modal = createAppKit({
networks,
defaultNetwork: mainnet,
projectId: ConstantsUtil.ProjectId,
metadata: ConstantsUtil.Metadata,
enableUniversalProviderManualControl: true,
walletConnectProvider: provider
})

ThemeStore.setModal(modal)
setAppKit(modal)

provider.on('display_uri', (connection: string) => {
modal.open({ view: 'ConnectingWalletConnectBasic', uri: connection })
})

setUprovider(provider)
}

useEffect(() => {
initializeUniversalProvider()
}, [])

return (
<>
{uprovider && appkit ? (
<>
<AppKitButtons />
<MultiChainInfo />
<UpaTests />
</>
) : (
''
)}
</>
)
}
3 changes: 2 additions & 1 deletion packages/appkit-new/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,8 @@ export class AppKit {
}
}

this.universalProvider = await UniversalProvider.init(universalProviderOptions)
this.universalProvider =
tomiir marked this conversation as resolved.
Show resolved Hide resolved
this.options.walletConnectProvider ?? (await UniversalProvider.init(universalProviderOptions))
}

public async getUniversalProvider() {
Expand Down
12 changes: 7 additions & 5 deletions packages/appkit-new/src/utils/TypesUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AppKitNetwork, CaipNetwork, ThemeVariables } from '@reown/appkit-common'
import type { ChainAdapter, Metadata, OptionsControllerState, ThemeMode } from '@reown/appkit-core'
import type { AppKitSIWEClient } from '@reown/appkit-siwe'
import type { IUniversalProvider } from '@walletconnect/universal-provider'

export type AppKitOptions = {
/**
Expand Down Expand Up @@ -72,9 +73,10 @@ export type AppKitOptions = {
* @see https://cloud.walletconnect.com/
*/
metadata?: Metadata
/**
* UniversalProvider instance to be used by AppKit.
* AppKit will generate its own instance by default in none provided
* @default undefined
*/
walletConnectProvider?: IUniversalProvider
tomiir marked this conversation as resolved.
Show resolved Hide resolved
} & OptionsControllerState

export type AppKitOptionsWithCaipNetworks = Omit<AppKitOptions, 'defaultNetwork' | 'networks'> & {
defaultNetwork?: CaipNetwork
networks: [CaipNetwork, ...CaipNetwork[]]
}
2 changes: 1 addition & 1 deletion packages/appkit/exports/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const PACKAGE_VERSION = '1.5.3'
export const PACKAGE_VERSION = '1.6.0'
1 change: 1 addition & 0 deletions packages/appkit/src/adapters/ChainAdapterBlueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ export namespace AdapterBlueprint {
namespace?: ChainNamespace
networks?: CaipNetwork[]
projectId?: string
enableUniversalProviderManualControl?: boolean
}

export type SwitchNetworkParams = {
Expand Down
16 changes: 14 additions & 2 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@

// -- Types --------------------------------------------------------------------
export interface OpenOptions {
view: 'Account' | 'Connect' | 'Networks' | 'ApproveTransaction' | 'OnRampProviders'
view:
| 'Account'
| 'Connect'
| 'Networks'
| 'ApproveTransaction'
| 'OnRampProviders'
| 'ConnectingWalletConnectBasic'
uri?: string
}

type Adapters = Record<ChainNamespace, AdapterBlueprint>
Expand Down Expand Up @@ -237,6 +244,10 @@
// -- Public -------------------------------------------------------------------
public async open(options?: OpenOptions) {
await this.initOrContinue()
if (options?.uri && this.universalAdapter) {
console.log('options.uri', options.uri)

Check failure on line 248 in packages/appkit/src/client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Unexpected console statement
tomiir marked this conversation as resolved.
Show resolved Hide resolved
ConnectionController.setUri(options.uri)
}
ModalController.open(options)
}

Expand Down Expand Up @@ -1778,7 +1789,8 @@
logger
}

this.universalProvider = await UniversalProvider.init(universalProviderOptions)
this.universalProvider =
this.options.walletConnectProvider ?? (await UniversalProvider.init(universalProviderOptions))
}

public async getUniversalProvider() {
Expand Down
1 change: 1 addition & 0 deletions packages/appkit/src/library/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { ChainNamespace } from '@reown/appkit-common'

type OpenOptions = {
view: 'Account' | 'Connect' | 'Networks' | 'ApproveTransaction' | 'OnRampProviders'
uri?: string
}

type ThemeModeOptions = AppKitOptions['themeMode']
Expand Down
15 changes: 14 additions & 1 deletion packages/appkit/src/universal-adapter/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import type UniversalProvider from '@walletconnect/universal-provider'
import { AdapterBlueprint } from '../adapters/ChainAdapterBlueprint.js'
import { WcHelpersUtil } from '../utils/index.js'
import { ChainController, CoreHelperUtil } from '@reown/appkit-core'
import { ChainController, CoreHelperUtil, ConnectionController } from '@reown/appkit-core'
import bs58 from 'bs58'
import { ConstantsUtil, type ChainNamespace } from '@reown/appkit-common'

export class UniversalAdapter extends AdapterBlueprint {
private enableUniversalProviderManualControl: boolean
public constructor(options?: AdapterBlueprint.Params) {
super(options)
this.enableUniversalProviderManualControl = Boolean(
options?.enableUniversalProviderManualControl
)
}
public async connectWalletConnect(onUri: (uri: string) => void) {
const connector = this.connectors.find(c => c.type === 'WALLET_CONNECT')

Expand All @@ -17,6 +24,12 @@ export class UniversalAdapter extends AdapterBlueprint {
)
}

if (this.enableUniversalProviderManualControl && ConnectionController.state.wcUri) {
onUri(ConnectionController.state.wcUri)

return
}

provider.on('display_uri', (uri: string) => {
onUri(uri)
})
Expand Down
14 changes: 8 additions & 6 deletions packages/appkit/src/utils/TypesUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AppKitNetwork, CaipNetwork, ThemeVariables } from '@reown/appkit-common'
import type { AppKitNetwork, ThemeVariables } from '@reown/appkit-common'
import type { ChainAdapter, Metadata, OptionsControllerState, ThemeMode } from '@reown/appkit-core'
import type { AppKitSIWEClient } from '@reown/appkit-siwe'
import type UniversalProvider from '@walletconnect/universal-provider'

export type AppKitOptions = {
/**
Expand Down Expand Up @@ -72,9 +73,10 @@ export type AppKitOptions = {
* @see https://cloud.walletconnect.com/
*/
metadata?: Metadata
/**
* UniversalProvider instance to be used by AppKit.
* AppKit will generate its own instance by default in none provided
* @default undefined
*/
walletConnectProvider?: UniversalProvider
} & OptionsControllerState

export type AppKitOptionsWithCaipNetworks = Omit<AppKitOptions, 'defaultNetwork' | 'networks'> & {
defaultNetwork?: CaipNetwork
networks: [CaipNetwork, ...CaipNetwork[]]
}
10 changes: 6 additions & 4 deletions packages/core/src/controllers/ConnectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface ConnectionControllerState {
recentWallet?: WcWallet
buffering: boolean
status?: 'connecting' | 'connected' | 'disconnected'
enableUniversalProviderManualControl?: boolean
connectionControllerClient?: ConnectionControllerClient
}

Expand Down Expand Up @@ -133,10 +134,7 @@ export const ConnectionController = {
state.wcPairingExpiry = undefined
this.state.status = 'connected'
} else {
await this._getClient()?.connectWalletConnect?.(uri => {
state.wcUri = uri
state.wcPairingExpiry = CoreHelperUtil.getPairingExpiry()
})
await this._getClient()?.connectWalletConnect?.(uri => this.setUri(uri))
}
},

Expand Down Expand Up @@ -225,6 +223,10 @@ export const ConnectionController = {
TransactionsController.resetTransactions()
StorageUtil.deleteWalletConnectDeepLink()
},
setUri(uri: string) {
enesozturk marked this conversation as resolved.
Show resolved Hide resolved
state.wcUri = uri
state.wcPairingExpiry = CoreHelperUtil.getPairingExpiry()
},

setWcLinking(wcLinking: ConnectionControllerState['wcLinking']) {
state.wcLinking = wcLinking
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/controllers/OptionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ export interface OptionsControllerStatePublic {
* @default false
*/
enableEmbedded?: boolean
/**
* Enable manual control of WalletConnect connections.
* @default false
*/
enableUniversalProviderManualControl?: boolean
tomiir marked this conversation as resolved.
Show resolved Hide resolved
}

export interface OptionsControllerStateInternal {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/controllers/RouterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface RouterControllerState {
| 'ConnectingExternal'
| 'ConnectingFarcaster'
| 'ConnectingWalletConnect'
| 'ConnectingWalletConnectBasic'
| 'ConnectingSiwe'
| 'ConnectingSocial'
| 'ConnectSocials'
Expand Down
1 change: 1 addition & 0 deletions packages/scaffold-ui/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from '../src/views/w3m-connect-view/index.js'
export * from '../src/views/w3m-connecting-external-view/index.js'
export * from '../src/views/w3m-connecting-multi-chain-view/index.js'
export * from '../src/views/w3m-connecting-wc-view/index.js'
export * from '../src/views/w3m-connecting-wc-basic-view/index.js'
export * from '../src/views/w3m-choose-account-name-view/index.js'
export * from '../src/views/w3m-downloads-view/index.js'
export * from '../src/views/w3m-get-wallet-view/index.js'
Expand Down
2 changes: 2 additions & 0 deletions packages/scaffold-ui/src/modal/w3m-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export class W3mRouter extends LitElement {
return html`<w3m-connect-view walletGuide="explore"></w3m-connect-view>`
case 'ConnectingWalletConnect':
return html`<w3m-connecting-wc-view></w3m-connecting-wc-view>`
case 'ConnectingWalletConnectBasic':
return html`<w3m-connecting-wc-basic-view></w3m-connecting-wc-basic-view>`
case 'ConnectingExternal':
return html`<w3m-connecting-external-view></w3m-connecting-external-view>`
case 'ConnectingSiwe':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import { html } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { W3mConnectingWidget } from '../../utils/w3m-connecting-widget/index.js'
import styles from './styles.js'
import { state } from 'lit/decorators.js'

@customElement('w3m-connecting-wc-qrcode')
export class W3mConnectingWcQrcode extends W3mConnectingWidget {
public static override styles = styles

// -- State & Properties -------------------------------- //
@state() private enableUniversalProviderManualControl =
ConnectionController.state.enableUniversalProviderManualControl

public constructor() {
super()
window.addEventListener('resize', this.forceUpdate)
Expand Down Expand Up @@ -47,8 +52,12 @@ export class W3mConnectingWcQrcode extends W3mConnectingWidget {
</wui-text>
${this.copyTemplate()}
</wui-flex>

<w3m-mobile-download-links .wallet=${this.wallet}></w3m-mobile-download-links>
${this.enableUniversalProviderManualControl
? html`<wui-flex flexDirection="column" .padding=${['0', 'xl', 'xl', 'xl']} gap="xl">
<w3m-all-wallets-widget></w3m-all-wallets-widget>
</wui-flex>`
: null}
`
}

Expand Down
1 change: 1 addition & 0 deletions packages/scaffold-ui/src/partials/w3m-header/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function headings() {
BuyInProgress: 'Buy',
ConnectingExternal: name ?? 'Connect Wallet',
ConnectingWalletConnect: name ?? 'WalletConnect',
ConnectingWalletConnectBasic: 'WalletConnect',
ConnectingSiwe: 'Sign In',
Convert: 'Convert',
ConvertSelectToken: 'Select token',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ApiController, CoreHelperUtil, OptionsController, StorageUtil } from '@reown/appkit-core'
import { customElement } from '@reown/appkit-ui'
import { LitElement, html } from 'lit'
import { state } from 'lit/decorators.js'

@customElement('w3m-connecting-wc-basic-view')
export class W3mConnectingWcBasicView extends LitElement {
@state() private isMobile = CoreHelperUtil.isMobile()

public constructor() {
super()
}
// -- Render -------------------------------------------- //
public override render() {
// eslint-disable-next-line no-console
console.log('W3mConnectingWcBasicView.constructor()', this.isMobile)

if (this.isMobile) {
const { featured, recommended } = ApiController.state
const { customWallets } = OptionsController.state
const recent = StorageUtil.getRecentWallets()

const showConnectors =
featured.length || recommended.length || customWallets?.length || recent.length
// eslint-disable-next-line no-console
console.log('W3mConnectingWcBasicView.constructor()', recommended)
tomiir marked this conversation as resolved.
Show resolved Hide resolved

return html`<wui-flex
flexDirection="column"
gap="xs"
.margin=${['3xs', 's', 's', 's'] as const}
>
${showConnectors ? html`<w3m-connector-list></w3m-connector-list>` : null}
<w3m-all-wallets-widget></w3m-all-wallets-widget>
</wui-flex>`
}

return html`<w3m-connecting-wc-view></w3m-connecting-wc-view>`
}
}
declare global {
interface HTMLElementTagNameMap {
'w3m-connecting-wc-basic-view': W3mConnectingWcBasicView
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ export class W3mConnectingWcView extends LitElement {

// -- Render -------------------------------------------- //
public override render() {
if (!this.wallet) {
return html`<w3m-connecting-wc-qrcode></w3m-connecting-wc-qrcode>`
}

tomiir marked this conversation as resolved.
Show resolved Hide resolved
return html`
${this.headerTemplate()}
<div>${this.platformTemplate()}</div>
Expand Down
Loading