SDK Methods for v0.1.13

Compatible Networks

  • Optimism Goerli

  • Ethereum Goerli

  • Polygon Mumbai

Instance creation and imports

Devs can directly get access to Banana module through our SDK just by importing the banana package from banana-wallet-sdk

import { Banana } from '@rize-labs/banana-wallet-sdk/dist/BananaProvider'

Once you successfully import Banana Module, you can start accessing the methods it exposes just by creating an instance of it by specifying the appropriate chain.

You can create a banana Instance as:

// importing chains and banana module from banana wallet sdk
import { Banana } from '@rize-labs/banana-wallet-sdk/dist/BananaProvider'
import { Chains } from '@rize-labs/banana-wallet-sdk/dist/Constants' 

// creating chain specific instance of banana module
const bananaInstance = new Banana(Chains.goerli, jsonRpcProviderUrl)

Methods by Banana Module

Once the instance is ready, devs can access multiple methods provided by the banana module.

Methods offered by the banana module are:

  • createWallet

  • connectWallet

  • execute

  • getWalletName

  • getBananaProvider

  • getEOAAddress

  • isWalletNameUnique

  • resetWallet

  • signMessage

  • verifySignature

createWallet

createWallet the method enables developers to create a wallet for their users. This method should be called only when the user has to create a wallet. It expects a unique wallet name and returns the object containing the process status and generated wallet address the uniqueness of the wallet name entered by the user can be checked via isWalletNameUnqiue. Once the wallet is created the wallet metadata would be saved to the user's browser cookie storage.

Usage


/**
 * @method createWallet
 * @param { string } walletIdentifier
 * @returns 
 * success: returns { status: success, address: walletAddress }
 * error: returns { status: error, address: "" }
 * set up signers and provider and create walletmetadata corresponding to the new wallet request.
 */
   
const walletCreationResponse = await bananaInstance.createWallet(walletName)
const walletAddress = walletCreationResponse.address;

connectWallet

connectWallet the method is to be used when developers know that the user has already created a wallet on a dapp or the user either knows his wallet name or wallet metadata is present in browser cookie storage (this can be checked using the getWalletName method). connectWallet expects a walletName and it returns an object containing process status and walletAddress.

Usage


/** 
 * @method connectWallet
 * @param { string } walletIdentifier
 * @returns 
 * success: returns { status: success, address: walletAddress }
 * error: returns { status: error, address: "" }
 * set up providers and signers at the sdk end in case user's already has a wallet created before.
 */
const walletConnectionResponse = bananaInstance.connectWallet(walletName);
const walletAddress = walletConnectionResponse.address;

execute

Developers can utilize the execute method of banana to execute a transaction via users' smart contract wallet. To use this method, developers must pass on callData a function (call data of function which user wants to call), destination address (contract address whose function is getting called), and value (in case of payable function is called).

For now, we are supporting single transaction execution. Bundling is under development.

Please don't use this method for view function calls. For just to read any contract data you can directly call it from your contract instance.

Usage

// fetching wallet name for user
const walletName: string = bananaInstance.getWalletName();

const bananaProvider = await bananaInstance.getBananaProvider();

// extracting bananaSigner
const bananaSigner = bananaProvider.getSigner();

// initializing sample greeter contract with bananaSigner
const GreeterContract = new ethers.Contract(
     addresses.Greeter,
     GreeterArtifact.abi,
     bananaSigner
    );

// greeter contract greet calldata
const greetCallData = GreeterContract.interface.encodeFunctionData(
      "greet",
       []
    );


/**
 * @method execute
 * @param { string } functionCallData, { string } value, { string } destination
 * @returns { string } requestId
 * handles the transaction to be initiated using user smart contract wallet.
 */
const requestId = await bananaInstance.execute(greetCallData, Greeter.address, amount)

// example for view call 
const greetings = await GreeterContract.getGreeting();

getWalletName

The getWalletName the method enables developers to read the user's cookie and retrieve the saved wallet name for display purposes. If this method does not return anything, it indicates that the user may have cleared their cookies. In such cases, developers should manually prompt the user for the wallet name.

Usage

/*
 * @params none
 * @returns walletName: string
 * accessed from banana instance and be used to fetch walletname.
 * cached in user's cookie storage 
 */
const walletName: string = bananaInstance.getWalletName();

getBananaProvider

The getBananaProvider the method enables developers to obtain ERC 4337-based providers for their smart contract wallet, which they can use in various ways, including creating contract instances with signers.

Usage


/**
 * @method getBananaProvider
 * @params none
 * @returns { ERC4337EthersProvider } bananaProvider
 * create ERC4337Provider instance of user's smart contract wallet. Used as BananaProvider.
 */
```
const bananaProvider: ERC4337EthersProvider = await bananaInstance.getBananaProvider();

getEOAAddress

getEOAAddress the method enables developers to retrieve the q values, which are the public address representation of the address generated by the device. The q values returned can be used for verifying the signature.

Usage


/**
 * @method getOwnerAddress
 * @param { string } walletName
 * @returns { string } eoaAddress
 * method to return the hardware address for a specific walletName.
 */
const qValues = await bananaInstance.getEOAAdress();

isWalletNameUnique

isWalletNameUnique the method enables developers to check if the wallet name suggested by their user is unique or not. This is possible because the wallet name serves as the primary key in the wallet credentials database on our server. As a suggestion, users can choose to keep their Gmail account username as their wallet name, as it is always unique.

Usage

/**
 * @method isWalletNameUnique
 * @param { string } walletName
 * @returns { boolean } isWalletNameTaken
 * check isWalletName user is giving is already taken or not
 */
const isUnqiue: boolean = await bananaInstance.isWalletNameUnique(walletName);

resetWallet

resetWallet the method enables developers to clear the current wallet metadata from the cookie's local storage. This enables the user to deploy a new wallet with a new username if needed. The method does not delete the existing wallet; instead, the user can roll back and use their previous wallet on the dapp by simply entering their previous wallet username by passing the previous walletName in connectWallet the function. This method does not require any input parameters and does not return any values. Developers can call it directly to clear the wallet metadata.

Usage

/**
 * @method resetWallet
 * @param none
 * @returns { string } walletName
 * method to remove current wallet metadata from browser
 */ 
bananaInstance.resetWallet()

signMessage

Developers can utilize the signMessage method of bananaInstance to enable users to sign messages using their wallets. During signing, the message passed will be hashed and then signed by the wallet. The method expects only one argument, which is the message to be signed, and returns an object containing the actual message that was signed and the signature.

Usage

/**
 * @params message: string
 * @returns object { messageSigned: signedMessage.toHexString(), signature: finalSignature }
 * signature: Signature retrieved after signing message.
 * messageToBeSigned: Message which is signed.
 */
const sampleMsg = "Hello World";
const signedMessageResponse = await bananaInstance.signMessage(sampleMsg);
const signature = signedMessageResponse.signature; 

verifySignature

Developers can utilize the verifySignature method of bananaInstance to verify the signed message signature against the user's hardware (EOA) address. The method expects three arguments: signature, messageSigned, and eoaAddress.

Usage

/**
 * @method verify
 * @param { string } signature, { string } messageSigned, { string } eoaAddress
 * @returns { boolean } isVerified
 * method to verify message against signature
 */
const sampleMsg = "Hello World";
const eoaAddress = await bananaInstance.getEOAAddress();
const signMessageResponse = await bananaInstance.signMessage(sampleMsg);
const messageSigned = signMessageResponse.messageSigned;
const signature = signMessageResponse.signature;
const isVerified = await bananaInstance.verifySignature(signature, messageSigned, eoaAddress);

PS: if after following this documentation you are experiencing webpack polyfill issue please refer to this page

🧊pageWebpack 5 Polyfill Issue

If you have any questions please post them Banana SDK Discord forum.

Last updated