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

Alpha #25

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ EVM_NFT_BALANCE_TEST_AMOUNT=2
EVM_TRANSFER_TEST_AMOUNT=0.01
EVM_TOKEN_TRANSFER_TEST_AMOUNT=1
EVM_TOKEN_APPROVE_TEST_AMOUNT=100
EVM_NFT_TRANSFER_ID=7
EVM_NFT_TRANSFER_ID=11

EVM_BALANCE_TEST_ADDRESS='0x760A4d3D03928D1e8541A7644B34370c1b79aa9F'
EVM_SENDER_PRIVATE_KEY='0x14bd9af4e87981b37b7b2e8a0d1d249b7fcdb7a3bc579c4c31488842d372c0e9'
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/evm-chains/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiplechain/evm-chains",
"version": "0.4.6",
"version": "0.4.7",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.es.js",
Expand Down
109 changes: 73 additions & 36 deletions packages/networks/evm-chains/src/browser/adapters/WalletConnect.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
import icons from './icons'
import { networks } from '../../index'
import type { EIP1193Provider } from './EIP6963'
import type { Provider } from '../../services/Provider'
import type { EvmNetworkConfigInterface, Provider } from '../../services/Provider'
import { EthereumProvider } from '@walletconnect/ethereum-provider'
import { ErrorTypeEnum, WalletPlatformEnum } from '@multiplechain/types'
import type { WalletConnectConfig, WalletAdapterInterface } from '@multiplechain/types'

let isConnected = false
let walletProvider: EIP1193Provider | undefined
let connector: InstanceType<typeof EthereumProvider> | undefined

const initConnector = async (
config: WalletConnectConfig,
network: EvmNetworkConfigInterface
): Promise<InstanceType<typeof EthereumProvider>> => {
if (connector !== undefined) {
return connector
}

const otherNetworkIds = networks
.getAll()
.map((x) => x.id)
.filter((id) => id !== network.id)

const chains: [number, ...number[]] = [network.id, ...otherNetworkIds]

connector = await EthereumProvider.init({
chains,
projectId: config.projectId,
relayUrl: 'wss://relay.walletconnect.com',
optionalChains: chains,
showQrModal: true,
qrModalOptions: {
themeMode: config.themeMode,
themeVariables: {
'--wcm-z-index': '999999999999'
},
explorerExcludedWalletIds: 'ALL',
explorerRecommendedWalletIds: [
'c57ca95b47569778a828d19178114f4db188b89b763c899ba0be274e97267d96',
'4622a2b2d6af1c9844944291e5e7351a6aa24cd7b23099efac1b2fd875da31a0',
'1ae92b26df02f0abca6304df07debccd18262fdf5fe82daa81593582dac9a369',
'0b415a746fb9ee99cce155c2ceca0c6f6061b1dbca2d722b3ba16381d0562150',
'971e689d0a5be527bac79629b4ee9b925e82208e5168b733496a09c0faed0709',
'c03dfee351b6fcc421b4494ea33b9d4b92a984f87aa76d1663bb28705e95034a',
'ecc4036f814562b41a5268adc86270fba1365471402006302e70169465b7ac18',
'ef333840daf915aafdc4a004525502d6d49d77bd9c65e0642dbaefb3c2893bef',
'bc949c5d968ae81310268bf9193f9c9fb7bb4e1283e1284af8f2bd4992535fd6',
'74f8092562bd79675e276d8b2062a83601a4106d30202f2d509195e30e19673d',
'afbd95522f4041c71dd4f1a065f971fd32372865b416f95a0b1db759ae33f2a7',
'20459438007b75f4f4acb98bf29aa3b800550309646d375da5fd4aac6c2a2c66',
'8837dd9413b1d9b585ee937d27a816590248386d9dbf59f5cd3422dbbb65683e',
'c286eebc742a537cd1d6818363e9dc53b21759a1e8e5d9b263d0c03ec7703576',
'38f5d18bd8522c244bdd70cb4a68e0e718865155811c043f052fb9f1c51de662',
'85db431492aa2e8672e93f4ea7acf10c88b97b867b0d373107af63dc4880f041'
]
}
})

return connector
}

const WalletConnect: WalletAdapterInterface<Provider, EIP1193Provider> = {
id: 'walletconnect',
Expand All @@ -18,6 +71,19 @@ const WalletConnect: WalletAdapterInterface<Provider, EIP1193Provider> = {
isConnected: () => isConnected,
disconnect: () => {
isConnected = false
Object.keys(localStorage)
.filter((x) => {
return (
x.startsWith('wc@2') ||
x.startsWith('@w3m') ||
x.startsWith('W3M') ||
x.startsWith('-walletlink')
)
})
.forEach((x) => {
localStorage.removeItem(x)
})

indexedDB.deleteDatabase('WALLET_CONNECT_V2_INDEXED_DB')
},
connect: async (
Expand All @@ -40,44 +106,15 @@ const WalletConnect: WalletAdapterInterface<Provider, EIP1193Provider> = {
const network = provider.network
// @ts-expect-error allow number index
rpcIdMapping[network.id] = network.rpcUrl

const connector = await EthereumProvider.init({
projectId: config.projectId,
relayUrl: 'wss://relay.walletconnect.com',
optionalChains: [network.id],
rpcMap: rpcIdMapping,
chains: [network.id],
showQrModal: true,
qrModalOptions: {
themeMode: config.themeMode,
themeVariables: {
'--wcm-z-index': '999999999999'
},
explorerExcludedWalletIds: 'ALL',
explorerRecommendedWalletIds: [
'c57ca95b47569778a828d19178114f4db188b89b763c899ba0be274e97267d96',
'4622a2b2d6af1c9844944291e5e7351a6aa24cd7b23099efac1b2fd875da31a0',
'1ae92b26df02f0abca6304df07debccd18262fdf5fe82daa81593582dac9a369',
'0b415a746fb9ee99cce155c2ceca0c6f6061b1dbca2d722b3ba16381d0562150',
'971e689d0a5be527bac79629b4ee9b925e82208e5168b733496a09c0faed0709',
'c03dfee351b6fcc421b4494ea33b9d4b92a984f87aa76d1663bb28705e95034a',
'ecc4036f814562b41a5268adc86270fba1365471402006302e70169465b7ac18',
'ef333840daf915aafdc4a004525502d6d49d77bd9c65e0642dbaefb3c2893bef',
'bc949c5d968ae81310268bf9193f9c9fb7bb4e1283e1284af8f2bd4992535fd6',
'74f8092562bd79675e276d8b2062a83601a4106d30202f2d509195e30e19673d',
'afbd95522f4041c71dd4f1a065f971fd32372865b416f95a0b1db759ae33f2a7',
'20459438007b75f4f4acb98bf29aa3b800550309646d375da5fd4aac6c2a2c66',
'8837dd9413b1d9b585ee937d27a816590248386d9dbf59f5cd3422dbbb65683e',
'c286eebc742a537cd1d6818363e9dc53b21759a1e8e5d9b263d0c03ec7703576',
'38f5d18bd8522c244bdd70cb4a68e0e718865155811c043f052fb9f1c51de662',
'85db431492aa2e8672e93f4ea7acf10c88b97b867b0d373107af63dc4880f041'
]
}
})
const connector = await initConnector(config, network)

return await new Promise((resolve, reject) => {
connector
.enable()
.connect({
rpcMap: rpcIdMapping,
chains: [network.id],
optionalChains: [network.id]
})
.then(() => {
isConnected = true
resolve((walletProvider = connector as EIP1193Provider))
Expand Down
29 changes: 10 additions & 19 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,27 +236,18 @@
return false
}

const navigator = window.navigator

/* eslint-disable */
if (window.WebViewJavascriptBridge) {
return true
}

if (window.webkit && window.webkit.messageHandlers) {
return true
}

if (navigator.userAgent.match(/WebView/i)) {
return true
}

const navigator = window.navigator
const userAgent = navigator.userAgent
const normalizedUserAgent = userAgent.toLowerCase()

Check warning on line 242 in packages/utils/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/index.ts#L240-L242

Added lines #L240 - L242 were not covered by tests
const standalone = navigator.standalone
const userAgent = navigator.userAgent.toLowerCase()
const safari = /safari/.test(userAgent)
const ios = /iphone|ipod|ipad/.test(userAgent)

return ios ? !standalone && !safari : userAgent.includes('wv')
const isIos = /ip(ad|hone|od)/.test(normalizedUserAgent)
|| navigator.platform === 'MacIntel'
&& navigator.maxTouchPoints > 1
const isAndroid = /android/.test(normalizedUserAgent)
const isSafari = /safari/.test(normalizedUserAgent)

Check warning on line 248 in packages/utils/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/index.ts#L245-L248

Added lines #L245 - L248 were not covered by tests
return (isAndroid && /; wv\)/.test(normalizedUserAgent))
|| (isIos && !standalone && !isSafari)

Check warning on line 250 in packages/utils/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/index.ts#L250

Added line #L250 was not covered by tests
/* eslint-enable */
}

Expand Down