Wallet API

Properties

window.havah

This property is true if the user has HAVAH wallet installed.

<example>

if (window.havah) {
  console.log('HAVAH wallet is installed!');
}

Methods

havah.accounts()

Returns the selected account on the HAVAH wallet if it is connected.

<interface>

interface AccountResult {
    address: string;
    nid: string;
    error?: string;
}

havah.accounts(): Promise<AccountResult>;

<example>

havah.accounts()
    .then(result => console.log(`User public key : ${result.address}`, `network ID : ${result.nid}`))
    .catch(error => console.error(`Error: ${error}`));

havah.connect()

It requests to connect the selected account on the HAVAH wallet to the caller’s site. If it has not been granted, HAVAH wallet will ask the user to allow it. If the user rejects or closes the wallet, it doesn’t emit an event.

<interface>

interface ConnectResult {
    address: string;
    nid: string;
    error?: string;
}

havah.connect(): Promise<ConnectResult>;

<example>

havah.connect()
    .then(result => console.log(`User public key : ${result.body.address}`, `network ID : ${result.body.nid}`))
    .catch(error => console.error(`Error: ${error}`));

havah.sign()

It signs the given data and returns it.

<interface>

interface SignResult {
    signature: string;
}

havah.sign(signData:string): Promise<SignResult>;

<example>

const time = Math.floor(Date.now().valueOf() / 1000); // Convert current time to seconds
const data = havahPrivKey + time; // Combine current time with Havah wallet address
window.havah.sign(data)
      .then(async (res: any) => {
        if (res.type === 'success') {
          console.log(`Signature: ${res.signData.signature}`, `result type: ${res.type}`)
        }
      })
      .catch((error: any) => {
        console.error(`Error: ${error}`);
      });

havah.sendTransaction()

It sends a transaction with the given data. Refer to the link for the Transaction structure.

<interface>

interface SendTransactionResult {
    txHash: string;
    type: string; //success, close, cancel, fail
}

havah.sendTransaction(transactionData:any): Promise<SendTransaction>;

<parameter>

  • to: EOA address to receive coins, or contract address to execute the transaction. It’s required.

  • value: Amount of HVH coin in loop to transfer. When omitted, it assumes 0. (1 HVH = 10 ^ 18 loop)

  • param: input param for the method to call

  • nonce: An arbitrary number used to prevent transaction hash collision

  • version: Protocol version (0x3 for JSON-RPC V3)

  • stepLimit: Maximum step allowance that can be used by the transaction

  • timestamp: Transaction creation time in microsecond.

<example>

const transactionData = {
      to: 'cx0000000000000000000000000000000000000000',
      method: 'claimPlanetReward',
      params: { ids: hex }
};
 
window.havah.sendTransaction(transactionData)
      .then(res => {
        console.log(`txHash: ${res.txHash}`, `result type : ${res.type}`));
      }
      .catch((error: any) => {
        console.error(`Error: ${error}`);
      });

Events

accountsChanged

Emits this event with a new account when the selected account on the HAVAH wallet changes.

window.havah.on('accountsChanged', handler: (address: string) => void);

networkChanged

Emits this event with a new network ID when the selected network on the HAVAH wallet changes.

window.havah.on('networkChanged', handler: (nid: string) => void);

Last updated