Skip to content

Commit

Permalink
Resolve bug when connect dual walletconnect and connect ledger live o…
Browse files Browse the repository at this point in the history
…n walletconnect modal
  • Loading branch information
Thiendekaco committed Mar 7, 2024
1 parent 0124c9c commit d477a9b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 31 deletions.
25 changes: 11 additions & 14 deletions packages/core/src/views/connect/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@
const { chains } = state.get()
const { provider, instance } = await getInterface({
chains: chains.filter((chain) => chain.namespace === type),
BigNumber,
Expand All @@ -182,7 +181,7 @@
instance,
accounts: [],
chains: [{ namespace: type, id: (type === 'evm' ?
'0x1' : '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3') }]
'0x1' : '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3') }]
}
connectingErrorMessage = ''
Expand Down Expand Up @@ -249,16 +248,16 @@
if(qrModalConnect$.value.modal){
qrModalConnect$.value.modal
.subscribeModal(async ({ open }) => {
if(!open
&& !(selectedWallet?.accounts
&& selectedWallet.accounts.length !== 0))
{
connectionRejected = true;
removeStateModalListener();
removeUriListener();
}
})
.subscribeModal(async ({ open }) => {
if(!open
&& !(selectedWallet?.accounts
&& selectedWallet.accounts.length !== 0))
{
connectionRejected = true;
removeStateModalListener();
removeUriListener();
}
})
}
cancelPreviousConnect$.next()
let chain: string | undefined = undefined;
Expand All @@ -271,8 +270,6 @@
})
try {
const valueResponse = await Promise.race([
// resolved account
type === 'evm' ? await requestAccounts(provider as EIP1193Provider) : await enable(provider as SubstrateProvider) ,
Expand Down
12 changes: 9 additions & 3 deletions packages/demo/src/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export const NetworkInfo : Record<string, NetworkItemType> = {
namespace: 'substrate',
wsProvider: "wss://westend-rpc.polkadot.io"
},
'Ethereum Mainnet': {
'Ethereum': {
slug: 'ethereum',
name: 'Ethereum Mainnet',
name: 'Ethereum',
namespace: 'evm'
},
'Moonbeam': {
Expand Down Expand Up @@ -56,11 +56,17 @@ export const NetworkInfo : Record<string, NetworkItemType> = {
name: 'Shiden',
namespace: 'evm'
},
'Mumbai': {
'Polygon': {
slug: 'polygon',
name: 'Polygon',
namespace: 'evm'
},
'Binance': {
slug: 'binance',
name: 'Binance',
namespace: 'evm'
},

'Polkadot Asset Hub': {
slug: 'statemint',
namespace: 'substrate',
Expand Down
18 changes: 17 additions & 1 deletion packages/demo/src/web3-onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default init({
// the native token symbol, eg ETH, BNB, MATIC
token: 'ETH',
// used for display, eg Ethereum Mainnet
label: 'Ethereum Mainnet',
label: 'Ethereum',
// used for network requests
rpcUrl: `https://ethereum.publicnode.com`,
decimal: 18
Expand Down Expand Up @@ -143,6 +143,22 @@ export default init({
namespace : 'evm',
decimal: 18
},
{
id: '0x38',
token: 'BNB',
label: 'Binance',
rpcUrl: 'https://bsc-dataseed.binance.org/',
secondaryTokens: [
{
address: '0x4d61577d8fd2208a0afb814ea089fdeae19ed202',
icon: `https://assets.coingecko.com/coins/images/15363/small/vfox2.png?1629870083`
},
{
address: '0xde2f075f6f14eb9d96755b24e416a53e736ca363',
icon: `https://assets.coingecko.com/coins/images/13423/small/frax_share.png?1608478989`
}
]
},

],

Expand Down
30 changes: 17 additions & 13 deletions packages/walletconnect/src/walletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ function walletConnect(options: WalletConnectOptions): WalletInit {

const { ProviderRpcError, ProviderRpcErrorCode } = await import(
'@subwallet-connect/common'
)
)

const { default: EthereumProvider, REQUIRED_METHODS } = await import(
'@walletconnect/ethereum-provider'
)
)

const { Subject, fromEvent } = await import('rxjs')
const { takeUntil, take } = await import('rxjs/operators')
Expand All @@ -72,10 +72,10 @@ function walletConnect(options: WalletConnectOptions): WalletInit {
const url = dappUrl || appMetadata.explore || ''

!url &&
!url.length &&
console.warn(
`It is strongly recommended to supply a dappUrl as it is required by some wallets (i.e. MetaMask) to allow connection.`
)
!url.length &&
console.warn(
`It is strongly recommended to supply a dappUrl as it is required by some wallets (i.e. MetaMask) to allow connection.`
)
const wcMetaData: CoreTypes.Metadata = {
name: appMetadata.name,
description: appMetadata.description || '',
Expand All @@ -95,15 +95,18 @@ function walletConnect(options: WalletConnectOptions): WalletInit {
return wcMetaData
}

const requiredChainForLedger = [1, 137, 56, 42161, 10, 43114, 5]

// default to mainnet
const requiredChainsParsed: number[] =
Array.isArray(requiredChains) &&
requiredChains.length &&
requiredChains.every(num => !isNaN(num))
? // @ts-ignore
// Required as WC package does not support hex numbers
requiredChains.map(chainID => parseInt(chainID))
: []
requiredChains.map(chainID => parseInt(chainID))
: chains.map(({ id }) => parseInt(id, 16))
.filter(value => requiredChainForLedger.find((value_) => value_ === value))

// Defaults to the chains provided within the web3-onboard init chain property
const optionalChainsParsed: number[] =
Expand All @@ -112,7 +115,7 @@ function walletConnect(options: WalletConnectOptions): WalletInit {
optionalChains.every(num => !isNaN(num))
? // @ts-ignore
// Required as WC package does not support hex numbers
optionalChains.map(chainID => parseInt(chainID))
optionalChains.map(chainID => parseInt(chainID))
: chains.map(({ id }) => parseInt(id, 16))

const requiredMethodsSet = new Set(
Expand Down Expand Up @@ -160,9 +163,9 @@ function walletConnect(options: WalletConnectOptions): WalletInit {
private disconnected$: InstanceType<typeof Subject>

constructor({
connector,
chains
}: {
connector,
chains
}: {
connector: InstanceType<typeof EthereumProvider>
chains: Chain[]
}) {
Expand Down Expand Up @@ -214,7 +217,7 @@ function walletConnect(options: WalletConnectOptions): WalletInit {
this.emit('accountsChanged', [])
this.disconnected$.next(true)
typeof localStorage !== 'undefined' &&
localStorage.removeItem('walletconnect')
localStorage.removeItem('walletconnect')
},
error: console.warn
})
Expand Down Expand Up @@ -264,6 +267,7 @@ function walletConnect(options: WalletConnectOptions): WalletInit {
}

if (method === 'eth_requestAccounts') {

return new Promise<ProviderAccounts>(
async (resolve, reject) => {
// Subscribe to connection events
Expand Down
8 changes: 8 additions & 0 deletions packages/walletconnectPolkadot/src/walletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ function walletConnect(options: WalletConnectOptions): WalletInit {
events: ['chainChanged", "accountsChanged', 'connect']
},
},
optionalNamespaces: {
polkadot: {
methods: optionalMethods,
chains: convertChainIdToCaipId(),
events: ['chainChanged", "accountsChanged', 'connect']
},
}
}).catch(err => {
console.error('err creating new session: ', err)
reject(
Expand All @@ -352,6 +359,7 @@ function walletConnect(options: WalletConnectOptions): WalletInit {
)
}


if (method === 'polkadot_signMessage') {

if(!this.connector.session) {
Expand Down

0 comments on commit d477a9b

Please sign in to comment.