BSafe SDK Beta

New version here 👇

nft://undefined/undefined/undefined?showBuying=true&showMeta=true

Welcome! In this post, we will guide you through the process of integrating your DApp with BSAFE's SDK.

Before diving into the technical aspects of this tutorial, it's important to understand the purpose of BSAFE and its SDK.

BSAFE is a non-custodial multisign wallet that empowers users to create vaults with specific members who can sign transactions for approval. It also enables the configuration of transaction approval rules, such as the required number of addresses needed to execute a transaction.

Currently, our SDK allows you to perform the following actions:

  • Create a vault

  • Register an address

  • Set transaction approval rules

  • Create and execute single transactions or batch payments

  • Sign transactions

  • Check the vault's balance

Let's begin by taking it step by step...

To fully grasp the concept of how BSAFE is built, it's crucial to understand the foundation. Our Vault is constructed using the Fuel Predicate. Once the Vault is built with its ABI and Buffer, a Fuel address is generated. This address serves as a storage space for all your assets.

During transaction execution, a validation process takes place to ensure that all addresses have signed the transaction. If this condition is met, the transaction is executed.

Now, let's dive in and make it happen.

1 - Installation

Install BSAFE SDK in your Typescript project.

npm install bsafe

To do this step we recommend install Fuel TS in the project.

npm install fuels

Alright, the installations are complete. Now, let's focus on the Vaults.

2 - Vaults

Our Vault extends all the features of Fuel Predicate, below we will explain how to generate an instance and manipulate your vault.

In this steps we will recommend install Fuel Wallet (Chrome extensions). To understand how to use the Fuel wallet, you can refer to the Fuel documentation.

2.1 Creating a vault

In this step, we will create a vault and set the address and rules.

To create an instance of the Vault, you need to pass the following parameters in the Configurables:

  • network: URL of Fuel Network; in this version, it is working with (beta-4)

  • SIGNATURES_COUNT: length of required signatures

  • SIGNERS: all addresses included in the Vault

import {Vault} from "bsafe";
 
new Vault({
    configurable: {
        network: 'FUEL_NETWORK',
        SIGNATURES_COUNT: 1,
        SIGNERS: ['FUEL_ACCOUNT_ADDRESS']
    }
})

2.2 Address

In this section, you'll learn how to identify the signer in your vault.

When instantiating your Vault, you can obtain the address of your Vault by accessing the property: vault.address

import {Vault} from "bsafe";
 
const vault = new Vault({
    configurable: {
        network: 'FUEL_NETWORK',
        SIGNATURES_COUNT: 1,
        SIGNERS: ['FUEL_ACCOUNT_ADDRESS']
    }
});
 
console.log(vault.address); // fuel1123klajsdh....

2.3 Balance

So, now let's learn how to check the balance in ETH on the vault.

To add assets to your vault, use the FUEL faucet.

With your Vault instance, you can search for the balance in ETH with the following method: vault.getBalance(). This method will return a  BN. To convert it to a string, execute: balance.format()

import {Vault} from "bsafe";
 
const vault = new Vault({
    configurable: {
        network: 'FUEL_NETWORK',
        SIGNATURES_COUNT: 1,
        SIGNERS: ['FUEL_ACCOUNT_ADDRESS']
    }
});
 
const balance = await vault.getBalance();
 
console.log(balance.format()); // Result: 0.0000000

2.4 Assets

But how can we check all assets?

To search for all assets in your predicate, simply execute the vault.getBalances() method. This method will return a list of CoinQuantities with the following properties:

  • amount: value of your Asset in BN

  • assetId: identifier of your asset "0x000...."

import {Vault} from "bsafe";
 
const vault = new Vault({
    configurable: {
        network: 'FUEL_NETWORK',
        SIGNATURES_COUNT: 1,
        SIGNERS: ['FUEL_ACCOUNT_ADDRESS']
    }
});
 
const assets = await vault.getBalances();
 
console.log(assets); // CoinQuantity[]

At this point, we already know how to create, set rules, check the balance, and manage assets in a vault. Now, let's learn how to use the transactions module.

3 - Transactions

Once you have created your Vault instance and transferred the balance to it, you can interact with it by creating transactions and sending them to Fuel accounts.

3.1 Instance Transfer

To create a transfer instance, you will need your Vault instance. Execute the vault.includeTransaction() method passing ITransferAsset[] with the following properties:

import {Vault} from "bsafe";
 
const vault = new Vault({
    configurable: {
        network: 'FUEL_NETWORK',
        SIGNATURES_COUNT: 1,
        SIGNERS: ['FUEL_ACCOUNT_ADDRESS']
    }
});
 
const transfer = vault.includeTransaction([
    {
        amount: bn(1_000).format(),
        assetId: 'ASSET_ID',
        to: 'FUEL_ADDRESS'
    }
], []);
 
console.log(tranfer);

3.2 Sign transfer

To execute the transfers, signing is required. This can be done by using the fuel wallet and signing the message with the transaction hash using the method wallet.signMessage(transfer.transaction.getHashTxId()) and adding this signature to the transaction through from the transfer.transaction.witnesses property.

import {Vault} from "bsafe";
 
const vault = new Vault({
    configurable: {
        network: 'FUEL_NETWORK',
        SIGNATURES_COUNT: 1,
        SIGNERS: ['FUEL_ACCOUNT_ADDRESS']
    }
});
 
const transfer = vault.includeTransaction([
    {
        amount: bn(1_000).format(),
        assetId: 'ASSET_ID',
        to: 'FUEL_ADDRESS'
    }
], []);
 
console.log(tranfer);

3.3 Send transaction

Once you have obtained all the necessary signatures, you can proceed to execute your transaction by using the method transfer.transaction.sendTransaction() . This will initiate the transfer within the chain, subjecting it to Vault validation. The method will then return the result along with the following response:

const transfer = vault.includeTransaction([
    {
        amount: bn(1_000).format(),
        assetId: 'ASSET_ID',
        to: 'FUEL_ADDRESS'
    }
], []);
 
// Set all signatures and check if all account signed
 
const transactionResponse = await transfer.transaction.sendTransaction();
 
console.log(transactionResponse)

4 - Conclusion

That's it! It's simple. Now you're ready to use BSAFE's SDK.

If you have any questions or suggestions, please join our Discord community and contribute with us to improve security on the FUEL ecosystem.

Useful links:

Subscribe to Bako Safe
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.