Interact with wallets
The MoonPay Authenticate SDK facilitates seamless integration with Ethereum wallets, empowering users to sign messages and send transactions securely. This guide outlines the steps to enable transaction functionalities with the user's Ethereum wallet, ensuring a straightforward experience for newcomers.
Accessing the User's Ethereum wallet
Once the SDK is initialized and ready, load the provider and signer:
import { Web3Provider } from '@ethersproject/providers';
const provider = sdk.getProvider() as Web3Provider;
const signer = provider!.getSigner();
- Provider: The Ethereum wallet provider connects your application to the Ethereum network, enabling access to blockchain data and functionalities.
- Signer: Obtained from the provider, the signer allows your application to perform actions on behalf of the user, such as signing messages and sending transactions.
Signing messages and making Transactions
Once you have a signer, you can easily retrieve the user's Ethereum wallet address, sign messages and make transactions.
Get the user's address:
const address = await signer.getAddress();
Sign a message:
const signedMsg = await signer.signMessage(msg);
Make a transaction:
import { parseEther } from 'viem'
const amt = parseEther('0.001');
const txEth = {
to: recipientAddress,
value: amt
};
const txResponse = await signer.sendTransaction(txEth);
await txResponse.wait();
You can implement additional methods supported by ethers.js version 5.7 for further functionality. Refer to the ethers.js documentation for a comprehensive list of available methods: ethers.js v5 Documentation.
Accessing the User's Bitcoin wallet
Once the SDK is initialized and ready, load bitcoin:
const bitcoin = sdk.getBitcoin();
Signing messages and making Transactions
Once you have the bitcoin variable, you can easily retrieve the user's Bitcoin wallet address and make transactions.
Get the user's address:
const accounts = await bitcoin.accounts();
const account = accounts[0];
Sign a message:
const signedMsg = await signer.signMessage(msg);
Updated 8 months ago