In this tutorial we will show how you can integrate Banana Wallet to your JavaScript or TypeScript-based frontend. We will demonstrate how to create a new wallet or connect an existing Banana Wallet on any dApp on Astar Network.
We will have a simple front end with some buttons to interact with the blockchain. Although Banana SDK provides you with a smart contract wallet you don't need worry about its deployment. Everything is handled by us in the SDK so you can concentrate on building your dApp.
Developers need to call the createWallet method which will inherently ask the user for a wallet name. Once username is provided, the wallet is initialized for the user, and the method returns an instance of the wallet.
Connecting wallet
constconnectWallet=async () => {// checking does wallet name is cached in cookie constwalletName=bananaSdkInstance.getWalletName();// if cached we will use it if (walletName) {setIsLoading(true);// connect wallet with cached wallet nameconstwallet=awaitbananaSdkInstance.connectWallet(walletName);setWalletInstance(wallet);// extracting wallet address for display purposeconstaddress=awaitwallet.getAddress();setWalletAddress(address);setOutput("Wallet Address: "+ address);setIsLoading(false); } else {setIsLoading(false);alert("You don't have wallet created!"); } };
When the user wallet is created the wallet's public data is cached in the user's cookie. Once the getWalletName function fetches walletName from the cookie, we pass walletName into connectWallet which initializes and configures some wallet parameters internally, and returns a wallet instance.
Getting chainId is pretty straight forward. Developers should extract the signer from the wallet and use getChainId to obtain the chainId of the current network.
Extracting the network is as easy as it looks. Developers should extract the provider from the wallet and use the getNetwork method to obtain the chain info.
To initiate a transaction you will create a transaction object. Extract signer from the wallet instance and initiate a transaction by passing the transaction object to the send transaction method. PS: Make sure your wallet is funded before you initiate transactions.
Signing a message is as simple as it looks. Pass a message that needs to be signed, and the method will return an object { messageSigned: "", signature: "" }