Crypto Wallet login

Authenticate with Ethereum, Binance Smart Chain, Polygon, Elrond, and many others to come!

Ethereum, BSC, Polygon Login

Metamask

Authenticating users is simple:

Moralis.Web3.authenticate().then(function (user) {
console.log(user.get('ethAddress'))
})

This will connect Metamask and request a signature (no gas required!)

It works the same way for all Ethereum Virtual Machine (EVM) compatible chains- like Binanace Smart Chain and Polygon (Matic)- as they all share the same Ethereum addresses.

WalletConnect

Moralis also supports authentication using WalletConnect. First add the provider.

<script src="https://github.com/WalletConnect/walletconnect-monorepo/releases/download/1.4.1/web3-provider.min.js"></script>

Then call authenticate like above, but with a provider option

const user = await Moralis.Web3.authenticate({ provider: "walletconnect" })

Custom Wallet Login

Although Moralis offers native support for MetaMask and WalletConnect it is possible to use any Web3 provider. The scope of this guide is to demonstrate how to supply any provider.

This guide will use Tourus and the Binance Smart Chain. The Tourus documentation is available at this url: https://docs.tor.us/

The Moralis.Web3 class has a method called enable. The first step is to overwrite this method in order to use a custom Moralis wallet provider class.

Import the provider

<script src="https://cdn.jsdelivr.net/npm/@toruslabs/torus-embed"></script>

Overwrite the `enable` method

Moralis.Web3.enable = async () => {
const web3Provider = new MoralisTorusProvider();
const web3 = await web3Provider.activate();
return web3;
}

Create the Torus Provider

class MoralisTorusProvider {
torus = new Torus({})
async activate() {
this.provider = await this.torus.init(
{
enableLogging: true,
network: {
host: "<YOUR BINANCE SPEEDY NODE>",
networkName: "Smart Chain - Testnet",
chainId: 97,
blockExplorer: "https://testnet.bscscan.com",
ticker: 'BNB',
tickerName: 'BNB',
},
})
await this.torus.login();
const MWeb3 = typeof Web3 === 'function' ? Web3 : window.Web3;
this.web3 = new MWeb3(this.torus.provider);
this.isActivated = true;
return this.web3;
}
}

You are good to go, you can now enable Moralis and connect to web3 by

window.web3 = await Moralis.Web3.enable();

Non EVM Chain Login

Authenticating with non Ethereum Virtual Machine (EVM) compatible chains requires a bit more setup.

Elrond

Prerequisites: In order to login with Elrond you will need a Ledger which can be purchased here. Once you have a ledger follow the steps in the Elrond Ledger documentation to add the Elrond App to the device.

Once the above steps are completed we can continue with the actual auth process.

If using the CDN you must add the erdjs script to your head.

<script src="https://npmcdn.com/@elrondnetwork/[email protected]/out-browser/erdjs.js"></script>

Now that all dependencies are in place we can authenticate.

Moralis.authenticate({ type: 'erd' }).then(function (user) {
console.log(user.get('erdAddress'))
})

Important: Notice the prefix of the address has changed from eth to erd

User Object

After the signature is completed the promise will resolve with a User object. The shape of the created user looks like this:

{
className: "_User"
id: String
_objCount: 0
attributes: {
accounts: Array,
ACL: Object,
authData: Object,
createdAt: Date,
email: String, // empty
ethAddress: String,
sessionToken: String,
updatedAT: Date,
username: String, // random value
}
}

For new users the username will be a randomly generated alphanumeric string and the email property will not exist. This can be set or changed by the app. See Objects and User docs.

It will also create a new entry in the Moralis._EthAddress class, corresponding to the Ethereum Address used for this user. The schema of the created entry in the Moralis._EthAddress object looks like this:

{
"objectId": String,
"transactions_synced": Number,
"ACL": ACL,
"last_eth_sync": Date,
"data": String,
"user": Pointer <_User>,
"updatedAt": Date,
"is_syncing": Boolean,
"last_eth_sync_completed": Date,
"signature": String,
"last_eth_sync_block": Number,
"transactions_total": Number,
"createdAt": Date,
"last_eth_sync_error": String
}

Chain specific user properties

If you enable syncing data from chains other than Ethereum some additional user properties will be created. These will be prefixed with the chain. I.e. for Binance Smart Chain the prefix is bsc. There will be an additional set of properties for each chain.

  • User.bscAddress (String)

  • User.bscAccounts (Array)

The same for chain specific class tables on the Moralis Server. An additional set of classes will be created with the corresponding chain prefixes.

  • BscTransactions

  • BscTokenBalance

  • BscTokenTransfers

  • BscNFTOwners

  • BscNFTTransfers

Chain Prefixes

Chain

Prefix

Ethereum Mainnet, Ropsten, Goerli, Rinkeby, Kovan

Eth

Binance Smart Chain Mainnet, Testnet

Bsc

Polygon (Matic) Mainnet, Mumbai Testnet

Polygon

Elrond

Erd

Manually Deleting Users

During testing if you manually delete a user also be sure the delete the corresponding rows in the following collections (where "xxx" is the chain prefix). Not doing so may cause unexpected behavior when authenticating via Web3 from the address that was just deleted.

  • Session

  • xxxBalance

  • _xxxAddress

Changing sign-in message

The message the user sees when signing in with Web3 can be changed by doing the following in the browser.

Moralis.Web3.getSigningData = () => 'My custom message'

Changing app icon in Metamask

It's also possible to change the icon the user sees when interacting with your smart contract by adding a favicon to your dapp. Follow the instructions in the Metamask docs.