Skip to content

Commit

Permalink
chore: abstract trust wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRalee committed Oct 16, 2024
1 parent 6b52505 commit ed95846
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/wallets/wallet-evm/src/strategy/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
ErrorContext,
WalletException,
BitGetException,
OkxWalletException,
ConcreteException,
MetamaskException,
OkxWalletException,
UnspecifiedErrorCode,
TransactionException,
ConcreteException,
TrustWalletException,
} from '@injectivelabs/exceptions'
import {
Wallet,
Expand All @@ -31,13 +32,15 @@ import {
getPhantomProvider,
getMetamaskProvider,
getOkxWalletProvider,
getTrustWalletProvider,
} from './utils'

const evmWallets = [
Wallet.BitGet,
Wallet.Phantom,
Wallet.Metamask,
Wallet.OkxWallet,
Wallet.TrustWallet,
]

export class EvmWallet
Expand Down Expand Up @@ -84,6 +87,10 @@ export class EvmWallet
return new MetamaskException(error, context)
}

if (this.wallet === Wallet.TrustWallet) {
return new TrustWalletException(error, context)
}

return new WalletException(error, context)
}

Expand Down Expand Up @@ -355,6 +362,8 @@ export class EvmWallet
? await getBitGetProvider()
: this.wallet === Wallet.OkxWallet
? await getOkxWalletProvider()
: this.wallet === Wallet.TrustWallet
? await getTrustWalletProvider()
: undefined

if (!provider) {
Expand Down
1 change: 1 addition & 0 deletions packages/wallets/wallet-evm/src/strategy/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { getOkxWalletProvider } from './Okx'
export { getBitGetProvider } from './bitget'
export { getPhantomProvider } from './phantom'
export { getMetamaskProvider } from './metamask'
export { getTrustWalletProvider } from './trustWallet'
69 changes: 69 additions & 0 deletions packages/wallets/wallet-evm/src/strategy/utils/trustWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { isServerSide } from '@injectivelabs/sdk-ts'
import {
BrowserEip1993Provider,
WindowWithEip1193Provider,
} from '@injectivelabs/wallet-base'

const $window = (isServerSide()
? {}
: window) as unknown as WindowWithEip1193Provider

export async function getTrustWalletProvider({ timeout } = { timeout: 3000 }) {
const provider = getTrustWalletFromWindow()

if (provider) {
return provider
}

return listenForTrustWalletInitialized({
timeout,
}) as Promise<BrowserEip1993Provider>
}

async function listenForTrustWalletInitialized(
{ timeout } = { timeout: 3000 },
) {
return new Promise((resolve) => {
const handleInitialization = () => {
resolve(getTrustWalletFromWindow())
}

$window.addEventListener('trustwallet#initialized', handleInitialization, {
once: true,
})

setTimeout(() => {
$window.removeEventListener(
'trustwallet#initialized',
handleInitialization,
)
resolve(null)
}, timeout)
})
}

function getTrustWalletFromWindow() {
const injectedProviderExist =
typeof window !== 'undefined' &&
(typeof $window.ethereum !== 'undefined' ||
typeof $window.trustWallet !== 'undefined')

// No injected providers exist.
if (!injectedProviderExist) {
return
}

if ($window.trustWallet) {
return $window.trustWallet
}

if ($window.ethereum.isTrustWallet || $window.ethereum.isTrust) {
return $window.ethereum
}

if ($window.providers) {
return $window.providers.find((p) => p.isTrustWallet)
}

return
}

0 comments on commit ed95846

Please sign in to comment.