Hello devs !
I am trying to create custom auth connector for torus wallet, but i keep getting this error:
My code calling the Moralis.authenticate();
const loginWithTorus = async() => {
let _torusConnector = new TorusWeb3Connector;
console.log(_torusConnector)
await Moralis.authenticate({ connector: _torusConnector });
}
And my connector code:
/* global window */
import AbstractWeb3Connector from "moralis/lib/browser/Web3Connector/AbstractWeb3Connector.js";
const getMoralisRpcs = speedyNodeKey => ({
1: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/eth/mainnet`,
3: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/eth/ropsten`,
4: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/eth/rinkeby`,
5: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/eth/goerli`,
42: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/eth/kovan`,
137: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/polygon/mainnet`,
80001: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/polygon/mumbai`,
56: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/bsc/mainnet`,
97: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/bsc/testnet`,
43114: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/avalanche/mainnet`,
43113: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/avalanche/testnet`,
250: `https://speedy-nodes-nyc.moralis.io/${speedyNodeKey}/fantom/mainnet`,
});
function fromDecimalToHex(number) {
if (typeof number !== 'number') throw 'The input provided should be a number';
return `0x${number.toString(16)}`;
}
function fromHexToDecimal(hex) {
if (typeof hex !== 'string') throw 'The input provided should be a string';
return parseInt(hex, 16);
}
function verifyChainId(chainId) {
if (typeof chainId === 'number') chainId = fromDecimalToHex(chainId);
return chainId;
}
/**
* Connector to connect an WalletConenct provider to Moralis
* Note: this assumes using WalletConnect v1
* // TODO: support WalletConnect v2
*/
class TorusWeb3Connector extends AbstractWeb3Connector {
type = 'Torus'
network="evm"
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(speedy_nodes_key),
chainId: 1,
}
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;
I dont even know if I am doing this right its my first time doing this custom connector.