From beb0d6bbd2465c8331ae93138c410d978b7acdce Mon Sep 17 00:00:00 2001 From: Andrei O Date: Mon, 14 Oct 2024 05:34:31 +0300 Subject: [PATCH] chore: improve SWIF --- src/extension/inject.ts | 6 +++--- src/utils/farcaster.ts | 31 ++++++++++++++++++++++++++----- src/utils/platform.ts | 10 +++++++++- src/views/FarcasterActions.vue | 15 ++++++++++----- src/views/UnlockModal.vue | 2 +- 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/extension/inject.ts b/src/extension/inject.ts index 0780825..af3dd19 100644 --- a/src/extension/inject.ts +++ b/src/extension/inject.ts @@ -137,7 +137,7 @@ class MetaMaskAPI { _eventsCount = 2 _jsonRpcConnection = {} _log = {} - _maxListeners= 100 + _maxListeners = 10 _metamask = new Proxy({ isUnlocked: () => { return Promise.resolve(true) @@ -153,7 +153,6 @@ class MetaMaskAPI { return false } // for maximum compatibility since is cloning the same API - enable() { return sendMessage({ method: 'eth_requestAccounts', params: Array(0)}) } @@ -314,7 +313,7 @@ class MetaMaskAPI { } getMaxListeners() { - return 100 + return 10 } _getExperimentalApi () { return this._metamask @@ -380,6 +379,7 @@ const listner = function(event: any) { (eth).selectedAddress = eventDataData?.address?.[0] ?? null; (eth).accounts = eventDataData.address?.[0] ? [eventDataData.address?.[0]] : []; (eth)._state.accounts = (eth).accounts; + (eth)._state.isConnected = true; (eth).isConnected = () => true; loadEIP1193Provider(eth) } else if( listnerName === 'chainChanged' ) { diff --git a/src/utils/farcaster.ts b/src/utils/farcaster.ts index 7d57031..0441d1d 100644 --- a/src/utils/farcaster.ts +++ b/src/utils/farcaster.ts @@ -1,9 +1,11 @@ import { signMsg, getSelectedAddress, getOptimismProvider } from './wallet' import { FARCASTER_PARTIAL_KEY_ABI } from './abis' import { ethers } from 'ethers' -import { getUrl } from './platform' -import { generateApiToken } from './warpcast-auth' +import { getUrl, setCachedFcAuthToken, getCachedFcAuthToken } from './platform' +import { generateApiToken} from './warpcast-auth' import { getQRCode } from './QR' +import { wait } from './misc' + export interface TChannelTokenStatusResponse { state: string; nonce: string; @@ -114,7 +116,8 @@ export const constructWarpcastSWIEMsg = ({ return `${domain} wants you to sign in with your Ethereum account:\n${custodyAddress}\n\nFarcaster Auth\n\nURI: ${siweUri}\nVersion: 1\nChain ID: 10\nNonce: ${nonce}${notBefore ? `\nIssued At: ${notBefore}` : `\nIssued At: ${new Date(Date.now() - 1000).toISOString()}`}${expirationTime ? `\nExpiration Time: ${expirationTime}` : ''}${notBefore ? `\nNot Before: ${notBefore}` : ''}\nResources:\n- farcaster://fid/${fid}` } - +// WC API has become slow authtoken is many times not considered valid until a few retries +// we use cached older token first export const signInWithFarcaster = async ({ channelToken, message, @@ -126,11 +129,20 @@ export const signInWithFarcaster = async ({ signature: string, authToken: string }) => { - const response = await fetch(`${EP_SIGNIN}`, { + const maxRetries = 4 + let retries = 1 + let response: Response + const newToken = authToken + const cachedToken = await getCachedFcAuthToken() + let token = cachedToken || newToken + const isCachedToken = token === cachedToken + do { + await wait(200 * retries) + response = await fetch(`${EP_SIGNIN}`, { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Authorization': `Bearer ${authToken}` + 'Authorization': `Bearer ${token}` }, body: JSON.stringify({ channelToken, @@ -138,6 +150,15 @@ export const signInWithFarcaster = async ({ signature, }) }); + if (response.ok) { + setCachedFcAuthToken(token) + return response.json(); + } + if (isCachedToken) { + token = newToken + } + retries++ + } while (retries < maxRetries) return response.json(); } diff --git a/src/utils/platform.ts b/src/utils/platform.ts index e729c37..a76ace9 100644 --- a/src/utils/platform.ts +++ b/src/utils/platform.ts @@ -161,7 +161,6 @@ export const removeAllAbis = async (): Promise => { await storageSave('abis', defaultAbis) } - export const readCAGetAll = async (): Promise => { return ((await storageGet('read-actions'))?.['read-actions'] ?? {}) as ContractActions } @@ -208,6 +207,15 @@ export const writeCAWipe = async (): Promise => { await storageSave('write-actions', {}) } +export const setCachedFcAuthToken = async (token: string): Promise => { + await storageSave('fcAuthToken', token) +} + +export const getCachedFcAuthToken = async (): Promise => { + return (await storageGet('fcAuthToken'))?.fcAuthToken ?? '' +} + + export const blockLockout = async (): Promise => { const settings = await getSettings() settings.lockOutBlocked = true diff --git a/src/views/FarcasterActions.vue b/src/views/FarcasterActions.vue index bd3eb2b..4058131 100644 --- a/src/views/FarcasterActions.vue +++ b/src/views/FarcasterActions.vue @@ -131,7 +131,8 @@

Try to scan QR

- (must be visible on current page) + (must be visible on current page, might fail if QR is small or too + complex)

@@ -143,16 +144,20 @@

Alternative: paste link from QR

-

- similar to: https://warpcast.com/~/siwf?channelToken=AXLUD4S4 +

+ Privy has copy link, if you see `I am on mobile` you can also right click + to copy. Link is similar to: + https://warpcast.com/~/siwf?channelToken=AXXXXXXX

-

Account needs to own a fid

-

QR needs to be visible on the website you click authorize

+

+ Account needs to own a fid, WC API has become slow you might need to try + multiple, times if you don't get signed in +

diff --git a/src/views/UnlockModal.vue b/src/views/UnlockModal.vue index a2b20e7..1dd8cd0 100644 --- a/src/views/UnlockModal.vue +++ b/src/views/UnlockModal.vue @@ -44,7 +44,7 @@ :clear-on-edit="false" :spellcheck="false" :tabindex="0" - @ionInput="mpPass = $event.target.value" + @ionInput="(e: any) => (mpPass = String(e.target.value))" id="pass-input" >
(Password)