# Wallet API

## Properties

### window\.havah

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

#### **Example**

```javascript
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**

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

havah.accounts(): Promise<AccountResult>;
```

#### **Example**

```icon
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**

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

havah.connect(): Promise<ConnectResult>;
```

#### **Example**

```icon
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**

```icon
interface SignResult {
    signature: string;
}

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

#### **Example**

```icon
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 ](https://github.com/icon-project/goloop/blob/master/doc/jsonrpc_v3.md#icx_sendtransaction)for the Transaction structure.

#### **Interface**

```icon
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). Note that it's number format in HVH.
* **method**: name of contract function to call. Equivalent to "method" under "data" for contract function call.
* **params**: JSON object of input parameters for the function to call. Equivalent to "params" object under "data" for contract function call.
* **version**: Protocol version (0x3 for JSON-RPC V3)
* **stepLimit**: Maximum step allowance that can be used by the transaction. If it's not set, wallet estimates TX fee and sets it.
* **timestamp**: Transaction creation time in microsecond
* **nonce**: An arbitrary number used to prevent transaction hash collision

#### **Example - Contract Call**

```javascript
const transactionData = {
      to: 'cx0547834cfa6fde720fe6760a79410152f85cd9c3',
      value: 5,
      method: 'purchaseHeartWithHVH',
      params: {
            '_babyId': 2991
      }
};
 
window.havah.sendTransaction(transactionData)
      .then(res => {
        console.log(`txHash: ${res.txHash}`, `result type : ${res.type}`));
      }
      .catch((error: any) => {
        console.error(`Error: ${error}`);
      });
```

Wallet will call JSON RPC API as follows.

```json
{
  "jsonrpc": "2.0",
  "id": 1721290151334,
  "method": "icx_sendTransaction",
  "params": {
    "to": "cx0547834cfa6fde720fe6760a79410152f85cd9c3",
    "from": "hxd346c400de5334b67b2b2285e688a3f7a41bca1c",
    "nid": "0x100",
    "version": "0x3",
    "timestamp": "0x61d81180177c0",
    "stepLimit": "0xabb53",
    "value": "0x4563918244f40000",
    "nonce": "0x1",
    "dataType": "call",
    "data": {
      "method": "purchaseHeartWithHVH",
      "params": {
        "_babyId": "2991"
      }
    },
    "signature": "f7deRl...rYKwE="
  }
}
```

#### **Example - HVH Transfer**

```javascript
const transactionData = {
      to: 'hxa8a0499635a25f67e4c9318bc8b2b71f7ad0080f',
      value: 10.5
};
 
window.havah.sendTransaction(transactionData)
      .then(res => {
        console.log(`txHash: ${res.txHash}`, `result type : ${res.type}`));
      }
      .catch((error: any) => {
        console.error(`Error: ${error}`);
      });
```

Wallet will call JSON RPC API as follows.

```json
{
  "jsonrpc": "2.0",
  "id": 1721291570342,
  "method": "icx_sendTransaction",
  "params": {
    "to": "hxa8a0499635a25f67e4c9318bc8b2b71f7ad0080f",
    "from": "hxd346c400de5334b67b2b2285e688a3f7a41bca1c",
    "nid": "0x100",
    "version": "0x3",
    "timestamp": "0x61d816c95b080",
    "stepLimit": "0x186a0",
    "value": "0x91b77e5e5d9a0000",
    "nonce": "0x1",
    "signature": "SbuC/F...pingE="
  }
}
```

## Events

### accountsChanged

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

```icon
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.

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