A JavaScript/TypeScript library for DApps development using Blade Wallet on Hedera Network. Blade Wallet uses the Hedera Signature and Wallet Interface as defined here.
Also, it is recommended to look through the Hedera documentation.
Initialization
To interact with the Blade Extension programmatically, instantiate a BladeConnector object.
It is possible to pass a preferred pairing strategy. By default (ConnectorStrategy.AUTO), the pairing is handled as follows:
If there is Blade Wallet extension, a wallet user will be asked to select needed accounts;
If there is no Blade Wallet extension, a QR code modal will be shown.
If preferred strategy is extension strategy (ConnectorStrategy.EXTENSION), the library will throw the error, if the extension was not detected.
If preferred strategy is WalletConnect strategy (ConnectorStrategy.WALLET_CONNECT), only the QR code modal will be used.
Implementation example:
import {BladeConnector, ConnectorStrategy} from'@bladelabs/blade-web3.js';constbladeConnector=awaitBladeConnector.init(ConnectorStrategy.WALLET_CONNECT,// preferred strategy is optional { // dApp metadata options are optional, but are highly recommended to use name:"Awesome DApp", description:"DApp description", url:"https://awesome-dapp.io/", icons: ["some-image-url.png"] });
Pairing
Implementation example:
import {HederaNetwork} from'@bladelabs/blade-web3.js';// params are optional, and Mainnet is used as a defaultconstparams= { network:HederaNetwork.Mainnet, dAppCode:"SomeAwesomeDApp"// optional while testing, request specific one by contacting us}constpairedAccountIds=awaitbladeConnector.createSession(params);// retrieving the first available signer to perform all the Hedera operationsconstbladeSigner=awaitbladeConnector.getSigners()[0];
Disconnecting
To disconnect the session, call .killSession() method.
Implementation example:
awaitbladeConnector.killSession();
Handshake
Wallet handshake allows to generate an authentication signature for use on a backend side. It may be useful, when it is important for a backend to make sure, that client user surely has access to a wallet.
Workflow:
Client requests backend for payload to sign, and data to sign with;
For signing to be successful, transaction should be actually signed and executed. Some transaction types require premature populating — e.g. TransferTransaction, TokenCreateTransaction etc. There are multiple ways to populate and execute transaction. To execute a transaction, you can use the .call() method, but it is also possible to call the .executeWithSigner() on a transaction itself.
First approach:
import {TransferTransaction} from'@hashgraph/sdk';constbladeSigner=bladeConnector.getSigners()[0];constamount=5;consttransaction=newTransferTransaction().addHbarTransfer(destinationAccountId, amount).addHbarTransfer(bladeSigner.getAccountId(),-amount);// populate adds transaction ID and node IDs to the transactionconstpopulatedTransaction=awaitbladeSigner.populateTransaction(transaction);constsignedTransaction=awaitbladeSigner.signTransaction(transaction.freeze());// call executes the transactionconstresult=awaitbladeSigner.call(signedTransaction);// orconstresult=awaitsignedTransaction.executeWithSigner(bladeSigner);
import {TransferTransaction} from'@hashgraph/sdk';constbladeSigner=bladeConnector.getSigners()[0];constamount=5;consttransaction=awaitnewTransferTransaction().addHbarTransfer(destinationAccountId, amount).addHbarTransfer(bladeSigner.getAccountId(),-amount).freezeWithSigner(bladeSigner); // adds transaction ID and node IDs to the transaction as wellconstsignedTransaction=awaitbladeSigner.signTransaction(transaction);constresult=awaitbladeSigner.call(signedTransaction);// orconstresult=awaitsignedTransaction.executeWithSigner(bladeSigner);
Getting a receipt
It is possible to get a receipt for any executed transaction withing the current network.
If there is a need to check transaction validity, .checkTransaction() method may be useful for that. It checks if node accounts are valid for the current network, and if transaction composed with the account signer, which this method is called on.
Implementation example:
import {TransferTransaction} from'@hashgraph/sdk';let bladeSigner =bladeConnector.getSigners()[0];constamount=5;consttransaction=awaitnewTransferTransaction().addHbarTransfer(destinationAccountId, amount).addHbarTransfer(bladeSigner.getAccountId(),-amount).freezeWithSigner(bladeSigner);bladeSigner =bladeConnector.getSigners()[1]; // using different accounttry {awaitbladeSigner.checkTransaction(transaction); } catch (e) {// transaction is not valid}