Skip to content

Commit

Permalink
chore: improve sa + siwe flow. Add loading for auth connector setup i…
Browse files Browse the repository at this point in the history
…n ethers
  • Loading branch information
tomiir committed May 21, 2024
1 parent d613c59 commit 893f88e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion apps/laboratory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"playwright:debug:social": "npm run playwright:test:social -- --debug",
"playwright:debug:siwe": "npm run playwright:test:siwe -- --debug",
"playwright:debug:siwe-email": "npm run playwright:test:siwe-email -- --debug",
"playwright:debug:siwe-sa": "npm run playwright:test:siwe-smart-account -- --debug",
"playwright:debug:siwe-sa": "npm run playwright:test:siwe-sa -- --debug",
"playwright:debug:sa": "npm run playwright:test:sa -- --debug",
"playwright:debug:canary": "npm run playwright:test:canary -- --debug"
},
Expand Down
3 changes: 2 additions & 1 deletion apps/laboratory/src/utils/SiweUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const siweConfig = createSIWEConfig({
domain: window.location.host,
uri: window.location.origin,
chains: WagmiConstantsUtil.chains.map(chain => chain.id),
statement: 'Please sign with your account'
statement: 'Please sign with your account',
iat: new Date().toISOString()
}),
createMessage: ({ address, ...args }: SIWECreateMessageArgs) => formatMessage(args, address),
getNonce: async () => {
Expand Down
36 changes: 20 additions & 16 deletions apps/laboratory/tests/shared/fixtures/w3m-email-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,29 @@ export const testMEmail = timingFixture.extend<ModalFixture>({
}
})

export const testMEmailSiwe = siwe.extend<ModalFixture>({
modalPage: async ({ page, library, context }, use, testInfo) => {
const modalPage = new ModalWalletPage(page, library, 'all')
await modalPage.load()
export const testMEmailSiwe = timingFixture.extend<ModalFixture>({
library: ['wagmi', { option: true }],
modalPage: [
async ({ page, library, context }, use, testInfo) => {
const modalPage = new ModalWalletPage(page, library, 'all')
await modalPage.load()

const mailsacApiKey = process.env['MAILSAC_API_KEY']
if (!mailsacApiKey) {
throw new Error('MAILSAC_API_KEY is not set')
}
const email = new Email(mailsacApiKey)
const tempEmail = email.getEmailAddressToUse(testInfo.parallelIndex)
const mailsacApiKey = process.env['MAILSAC_API_KEY']
if (!mailsacApiKey) {
throw new Error('MAILSAC_API_KEY is not set')
}
const email = new Email(mailsacApiKey)
const tempEmail = email.getEmailAddressToUse(testInfo.parallelIndex)

await modalPage.emailFlow(tempEmail, context, mailsacApiKey)
await modalPage.promptSiwe()
await modalPage.approveSign()
await modalPage.page.waitForTimeout(1000)
await modalPage.emailFlow(tempEmail, context, mailsacApiKey)
await modalPage.promptSiwe()
await modalPage.approveSign()
await modalPage.page.waitForTimeout(1000)

await use(modalPage)
},
await use(modalPage)
},
{ timeout: 90_000 }
],
modalValidator: async ({ modalPage }, use) => {
const modalValidator = new ModalWalletValidator(modalPage.page)
await use(modalValidator)
Expand Down
7 changes: 6 additions & 1 deletion packages/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ export class Web3Modal extends Web3ModalScaffold {
window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.AUTH_CONNECTOR_ID)

if (this.authProvider) {
super.setLoading(true)
const { address, chainId, smartAccountDeployed, preferredAccountType } =
await this.authProvider.connect({ chainId: this.getChainId() })

Expand Down Expand Up @@ -1010,12 +1011,13 @@ export class Web3Modal extends Web3ModalScaffold {
if (!address) {
return
}
super.setLoading(true)
const chainId = NetworkUtil.caipNetworkIdToNumber(this.getCaipNetwork()?.id)
EthersStoreUtil.setAddress(address as Address)
EthersStoreUtil.setChainId(chainId)
EthersStoreUtil.setIsConnected(true)
EthersStoreUtil.setPreferredAccountType(type as W3mFrameTypes.AccountType)
this.syncAccount()
this.syncAccount().then(() => super.setLoading(false))
})
}
}
Expand Down Expand Up @@ -1282,6 +1284,7 @@ export class Web3Modal extends Web3ModalScaffold {
} else if (providerType === ConstantsUtil.AUTH_CONNECTOR_ID) {
if (this.authProvider && chain?.chainId) {
try {
super.setLoading(true)
await this.authProvider.switchNetwork(chain?.chainId)
EthersStoreUtil.setChainId(chain.chainId)

Expand All @@ -1296,6 +1299,8 @@ export class Web3Modal extends Web3ModalScaffold {
await this.syncAccount()
} catch {
throw new Error('Switching chain failed')
} finally {
super.setLoading(false)
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion packages/scaffold/src/modal/w3m-modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ export class W3mModal extends LitElement {

@state() private connected = AccountController.state.isConnected

@state() private loading = ModalController.state.loading

public constructor() {
super()
this.initializeTheming()
ApiController.prefetch()
this.unsubscribe.push(
ModalController.subscribeKey('open', val => (val ? this.onOpen() : this.onClose())),
ModalController.subscribeKey('loading', val => {
this.loading = val
this.onNewAddress(AccountController.state.caipAddress)
}),
AccountController.subscribeKey('isConnected', val => (this.connected = val)),
AccountController.subscribeKey('caipAddress', val => this.onNewAddress(val)),
OptionsController.subscribeKey('isSiweEnabled', val => (this.isSiweEnabled = val))
Expand Down Expand Up @@ -158,12 +164,17 @@ export class W3mModal extends LitElement {
}

private async onNewAddress(caipAddress?: CaipAddress) {
if (!this.connected) {
console.log('on new address loading', this.loading)

Check failure on line 167 in packages/scaffold/src/modal/w3m-modal/index.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Unexpected console statement
if (!this.connected || this.loading) {
return
}
console.log('prev caip address', this.caipAddress)

Check failure on line 171 in packages/scaffold/src/modal/w3m-modal/index.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Unexpected console statement
console.log('new caip address', caipAddress)

Check failure on line 172 in packages/scaffold/src/modal/w3m-modal/index.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Unexpected console statement
const hasNetworkChanged = this.caipAddress && this.caipAddress !== caipAddress
console.log('has network changed', hasNetworkChanged)

Check failure on line 174 in packages/scaffold/src/modal/w3m-modal/index.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Unexpected console statement
this.caipAddress = caipAddress
if (this.isSiweEnabled) {
// Case 2: Siwe session exists and network has changed
const { SIWEController } = await import('@web3modal/siwe')
const session = await SIWEController.getSession()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,10 @@ export class W3mAccountSettingsView extends LitElement {
}

this.loading = true
ModalController.setLoading(true)
await authConnector?.provider.setPreferredAccount(accountTypeTarget)
await ConnectionController.reconnectExternal(authConnector)
ModalController.setLoading(false)

this.text =
accountTypeTarget === W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { customElement } from '@web3modal/ui'
import {
ConnectionController,
ConnectorController,
ModalController,
RouterController,
RouterUtil,
SnackController
Expand Down Expand Up @@ -84,10 +85,12 @@ export class W3mUpgradeToSmartAccountView extends LitElement {
if (this.authConnector) {
try {
this.loading = true
ModalController.setLoading(true)
await this.authConnector.provider.setPreferredAccount(
W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT
)
await ConnectionController.reconnectExternal(this.authConnector)
ModalController.setLoading(false)
this.loading = false
RouterUtil.navigateAfterPreferredAccountTypeSelect()
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions packages/siwe/core/utils/TypeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface SIWECreateMessageArgs {
requestId?: string
resources?: string[]
expiry?: number
iat?: string
}
export type SIWEMessageArgs = {
chains: number[]
Expand Down

0 comments on commit 893f88e

Please sign in to comment.