User Login

Retain the ownership of user transactions using Smart Contract Wallets

Skip this section if you are using Native Meta Transactions

Biconomy uses an upgradable Contract Wallet to relay the user’s transaction to your DApp smart contract so msg.sender in your smart contracts will be user’s contract wallet address, so user first needs to login to biconomy to use meta transactions. We just need users signature and public address to login.

Login API

When you use third party Wallets

Mexa exposes a login method to let your users login to biconomy to have their contract wallet created if user comes for the first time or just return existing contract wallet address for existing users.

This API internally trigger eth_signTypedData RPC method to get user's signature which is handled by your web3 provider. Mostly this requires user to manually sign the login message.

Using Callbacks
Using Promises
Using Callbacks
biconomy.login(<public wallet address>, (error, response) => {
if(error) {
// Error while user login to biconomy
return;
}
if(response.transactionHash) {
// First time user. Contract wallet transaction pending.
// Wait for confirmation.
} else if(response.userContract) {
// Existing user login successful
}
});
Using Promises
try{
let response = await biconomy.login(<public wallet address>);
if(response && response.transactionHash) {
// First time user. Contract wallet transaction pending.
// Wait for confirmation.
} else if (response && response.userContract) {
// Existing user login successful
}
} catch(error) {
console.log(`Error Code: ${error.code} Error Message: ${error.message}`);
}

When you implement your own Wallet

Mexa has accountLogin() method for the cases when your code has access to User's private key, in those cases you do not want user to sign another message on a popup screen.

Method takes user's public wallet address and a signature as an arguments. Data to be signed is fetched using the method getLoginMessageToSign()

Using Callbacks
Using Promises
Using Callbacks
const sigUtil = require('eth-sig-util')
let message = await biconomy.getLoginMessageToSign(<wallet public address>);
let signature = sigUtil.signTypedMessage(new Buffer.from(<private key>, 'hex'),
{data: message}, 'V3');
// USING CALLBACKS
biconomy.accountLogin(<wallet public address>, signature, (error, response)=>{
if(error) {
console.log(error);
return;
}
if(response.transactionHash) {
// First time user. Contract wallet transaction pending.
// Wait for confirmation using Contract Wallet Confirmation method.
} else if(response.userContract) {
// Existing user login successful
}
});
Using Promises
const sigUtil = require('eth-sig-util')
let message = await biconomy.getLoginMessageToSign(<wallet public address>);
let signature = sigUtil.signTypedMessage(new Buffer.from(<private key>, 'hex'),
{data: message}, 'V3');
// USING PROMISES
try {
let response = await biconomy.accountLogin(<wallet public address>, signature);
if(response.transactionHash) {
// First time user. Contract wallet transaction pending.
// Wait for confirmation using Contract Wallet Confirmation method.
} else if(response.userContract) {
// Existing user login successful
}
} catch(error) {
console.log(`Error Code: ${error.code} Error Message: ${error.message}`);
}

Contract Wallet Confirmation

When user login for the first time, you’ll get the transaction hash for user’s contract wallet creation transaction. On transaction confirmation mexa will emit a confirmation event.

biconomy.onEvent(biconomy.LOGIN_CONFIRMATION, (log) => {
// User's Contract Wallet creation successful
console.log(`User contract wallet address: ${log.userContract}`);
});