Hello,
I am trying to integrate authentication with Torus wallet and I am struggling with creation of my custom connector class.
Before moving to Moralis, I had been using web3modal connection:
const Torus = (await import('@toruslabs/torus-embed')).default
const providerOptions = {
torus: {
package: Torus, // required
},
}
const web3Modal = new Web3Modal({
network: 'mainnet', // optional
cacheProvider: true, // optional
providerOptions, // required
})
const connector = await web3Modal.connectTo('torus')
const provider = new ethers.providers.Web3Provider(connector)
[...]
So far I was able to implement the custom class like this:
class TorusWeb3Connector extends AbstractWeb3Connector {
type = 'Torus'
async activate({ chainId: providedChainId, mobileLinks } = {}) {
// Cleanup old data if present to avoid using previous sessions
try {
await this.deactivate()
} catch (error) {
// Do nothing
}
if (!this.provider) {
let TorusProvider
const config = {
rpc: getMoralisRpcs('Torus'),
chainId: providedChainId,
}
try {
TorusProvider = (await import('@toruslabs/torus-embed')).default
} catch (error) {
// Do nothing. User might not need Torus
}
if (!TorusProvider) {
throw new Error(
'Cannot enable Torus: dependency "@toruslabs/torus-embed" is missing'
)
}
if (typeof TorusProvider === 'function') {
this.provider = new TorusProvider(config)
} else {
this.provider = new window.TorusProvider()
}
}
if (!this.provider) {
throw new Error(
'Could not connect with Torus, error in connecting to provider'
)
}
console.log('this.provider', this.provider)
const accounts = await this.provider.ethereum.enable()
const account = accounts[0].toLowerCase()
const { chainId } = this.provider
const verifiedChainId = verifyChainId(chainId)
console.log('accounts', accounts)
this.account = account
this.chainId = verifiedChainId
this.subscribeToEvents(this.provider)
return { provider: this.provider, account, chainId: verifiedChainId }
}
async deactivate() {
this.unsubscribeToEvents(this.provider)
try {
if (window) {
window.localStorage.removeItem('Torus')
}
} catch (error) {
// Do nothing
}
this.account = null
this.chainId = null
if (this.provider) {
try {
await this.provider.disconnect()
} catch {
// Do nothing
}
}
}
}
export default TorusWeb3Connector
And then integrate it through authentication like below:
authenticate({ connector: TorusWeb3Connector })
Unfortunately, “ethereum” property in “this.provider” is null and I cannot get through with the authentication process. Does anyone know how to approach it so I can make my full migration towards Moralis?