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';
const bladeConnector = await BladeConnector.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 default
const params = {
network: HederaNetwork.Mainnet,
dAppCode: "SomeAwesomeDApp" // optional while testing, request specific one by contacting us
}
const pairedAccountIds = await bladeConnector.createSession(params);
// retrieving the first available signer to perform all the Hedera operations
const bladeSigner = await bladeConnector.getSigners()[0];
Disconnecting
To disconnect the session, call .killSession() method.
Implementation example:
await bladeConnector.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';
const bladeSigner = bladeConnector.getSigners()[0];
const amount = 5;
const transaction = new TransferTransaction()
.addHbarTransfer(destinationAccountId, amount)
.addHbarTransfer(bladeSigner.getAccountId(), -amount);
// populate adds transaction ID and node IDs to the transaction
const populatedTransaction = await bladeSigner.populateTransaction(transaction);
const signedTransaction = await bladeSigner.signTransaction(transaction.freeze());
// call executes the transaction
const result = await bladeSigner.call(signedTransaction);
// or
const result = await signedTransaction.executeWithSigner(bladeSigner);
Second approach:
import {TransferTransaction, AccountId} from '@hashgraph/sdk';
const bladeSigner = bladeConnector.getSigners()[0];
const amount = 5;
const transaction = await new TransferTransaction()
.addHbarTransfer(destinationAccountId, amount)
.addHbarTransfer(bladeSigner.getAccountId(), -amount)
.setTransactionId(TransactionId.generate(bladeSigner.getAccountId()))
.setNodeAccountIds([new AccountId(3)])
.freeze();
const signedTransaction = await bladeSigner.signTransaction(transaction);
const result = await bladeSigner.call(signedTransaction);
// or
const result = await signedTransaction.executeWithSigner(bladeSigner);
Third approach:
import {TransferTransaction} from '@hashgraph/sdk';
const bladeSigner = bladeConnector.getSigners()[0];
const amount = 5;
const transaction = await new TransferTransaction()
.addHbarTransfer(destinationAccountId, amount)
.addHbarTransfer(bladeSigner.getAccountId(), -amount)
.freezeWithSigner(bladeSigner); // adds transaction ID and node IDs to the transaction as well
const signedTransaction = await bladeSigner.signTransaction(transaction);
const result = await bladeSigner.call(signedTransaction);
// or
const result = await signedTransaction.executeWithSigner(bladeSigner);
Getting a receipt
It is possible to get a receipt for any executed transaction withing the current network.
Implementation example:
import {TransactionReceiptQuery} from '@hashgraph/sdk';
const transactionId = "some-tx-id";
const bladeSigner = bladeConnector.getSigners()[0];
const result = await bladeSigner.call(new TransactionReceiptQuery({
transactionId
}));
Validation
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];
const amount = 5;
const transaction = await new TransferTransaction()
.addHbarTransfer(destinationAccountId, amount)
.addHbarTransfer(bladeSigner.getAccountId(), -amount)
.freezeWithSigner(bladeSigner);
bladeSigner = bladeConnector.getSigners()[1]; // using different account
try {
await bladeSigner.checkTransaction(transaction);
} catch (e) {
// transaction is not valid
}