✅ Hashconnect

What is the Hashconnect?

We support the Hashconnect, a tool that facilitates communication between Dapps and wallets using the Hedera. You can find more details on the following link.


Pair the Hashconnect

If you pair the Hashconnect, you can connect the wallet to the dapp.

face.hedera.getHashConnect().init() initializes the Hashconnect in the Face Wallet SDK and establishes a connection with the Hashconnect relay server.

face.hedera.getHashConnect().pair(pairingString) connects the Hashconnect to the wallet using the pairing string generated in the Dapp.

import { Face, Network } from '@haechi-labs/face-sdk';
import { HashConnect, HashConnectTypes } from 'hashconnect';

const metadata: HashConnectTypes.AppMetadata = {
     name: 'Face Wallet Sample Dapp',
     description: 'for face wallet test',
     icon: 'https://my.facewallet.xyz/icon',
};

// init hashconnect on dapp and wallet
const initData = await hashConnect.init(metadata, 'testnet', true);
await face.hedera.getHashConnect().init();

// connect dapp and wallet
const pairingString = hashConnect.generatePairingString(initData.topic, 'testnet', false);
const pairData = await face.hedera.getHashConnect().pair(pairingString);

console.log(pairData);
// {
//    "accountId": "0.0.num" or "0.0.evmAddress",
//    "topic" : ${topic}
// }


Send Transaction (Sign Transaction and Delegate Tx Fee)

You can sign the transaction to be requested using the Hashconnect.

success parameter of the signed transaction object means that the signing and sending the transaction has been failed. If success is false, the account accountToSign is different with the wallet's account. Or it means that user's account has not been created yet.

If the user's account has not been created yet, the error will include NOT_CREATED_ACCOUNT.

import { Face, Network } from '@haechi-labs/face-sdk';
import { HashConnect, HashConnectTypes } from 'hashconnect';
import {
  Client,
  TokenAssociateTransaction,
  Transaction,
  TransferTransaction,
} from '@hashgraph/sdk';


const payerPK = `PAYER_PK`; 
const payerId = 'PAYER_ID';

const client = Client.(forTestnet() | forMainnet())
.setOperator(payerId, payerPK);

const account = await face.hedera.getAccountId();

const transaction = await new TransferTransaction()
  		.addNftTransfer(tokenId, tokenNum, account, receiver)
  		.freezeWith(client)
  |  await new TransferTransaction()
      .addHbarTransfer(account, -0.1)
      .addHbarTransfer(receiver, 0.1)
      .freezeWith(client);
	|  await new TokenAssociateTransaction()
      .setAccountId(account)
      .setTokenIds([tokenId])
      .freezeWith(client)

const signed = await hashConnect.sendTransaction(topic, {
  topic,
  byteArray: transaction.toBytes(),
  metadata: {
    accountToSign: account,
    returnTransaction: true,
  },
});

if (!signed.success) {
  console.log(`Hedera Signed Fail ${signed.error}`);
  return;
}

const signedTransaction = Transaction.fromBytes(signed.signedTransaction as Uint8Array);
const result = await signedTransaction.execute(client);