Dapps Integration
Flash wallet using WalletConnect open protocol to communicate securely to dapps.
Links
Demo https://flash-wallet-demo.vercel.app/
Source Code https://github.com/mintmasterapp/flash-wallet-demo
Wallet Connect https://docs.walletconnect.com/
Install
npm install --save @walletconnect/client
OR
yarn add @walletconnect/clientInitiate Connection
import WalletConnect from "@walletconnect/client";
// Create a connector
const connector = new WalletConnect({
bridge: "https://bridge.walletconnect.org", // Required
});
// Check if connection is already established
if (!connector.connected) {
// create new session
connector.createSession();
}
// Subscribe to connection events
connector.on("connect", (error, payload) => {
if (error) {
throw error;
}
// Get provided accounts and chainId
const { accounts, chainId } = payload.params[0];
});
connector.on("session_update", (error, payload) => {
if (error) {
throw error;
}
// Get updated accounts and chainId
const { accounts, chainId } = payload.params[0];
});
connector.on("disconnect", (error, payload) => {
if (error) {
throw error;
}
// Delete connector
});Send Transaction
const request = {
id: 1110,
jsonrpc: '2.0',
method: 'hedera_sendTransaction',
params: [
{
from: account // solidityAddress,
data: transByte // format: hex,
},
],
};
// Send Request
connector
.sendCustomRequest(request)
.then((result) => {
// Returns request result
console.log(result);
})
.catch((error) => {
// Error returned when rejected
console.error(error);
});
Create Transaction Example
Transaction
export async function senHbar(
sender: string,
receiver: string,
amount: string,
) {
const senderAccountId = AccountId.fromSolidityAddress(sender).toString();
const newAccountTransaction = new TransferTransaction()
.addHbarTransfer(senderAccountId, new Hbar(-amount))
.addHbarTransfer(receiver, new Hbar(amount))
.setTransactionMemo('Hello buddy sign and enjoy!');
const bytes = await makeTransBytes(newAccountTransaction, senderAccountId);
return bytes;
}Generate Byte
export const makeTransBytes = async (trans: any, accountId: string) => {
const transId = TransactionId.generate(accountId);
trans.setTransactionId(transId);
trans.setNodeAccountIds([new AccountId(3)]);
await trans.freeze();
return Buffer.from(trans.toBytes()).toString('hex');
};Supported Hedera Network
Flash Wallet supported all hedera networks e.g MAINNET, TESTNET, and PREVIEWNET.
After the connection is approved by the flash wallet. Flash Wallet responds accountId (solidityAddress) and chainId.
For MAINNET chainId is 1.
For TESTNET chainId is 2.
For PREVIEWNET chainId is 3.
Last updated