const axios = require('axios');
const Client = require('torus-client');

const client = new Client({
  host: '127.0.0.1',
  port: 24112,
  username: 'rpc',
  password: 'password123',
});

const verifySignature = async (address, message, signature) => {
  try {
    return client.verifyMessage(address, signature, message);
  } catch (err) {
    console.error(err);
    return false;
  }
};

const IQUIDUS_EXPLORER_URL = 'https://explorer.torus.cc';

const getBalance = async (address) => {
  try {
    const response = await axios.get(`${IQUIDUS_EXPLORER_URL}/ext/getaddresstxsajax/${address}`);
    const { data } = response.data;
    if (data.length === 0) {
      return 0;
    }
    const balance = data[0][4] / 10e7;
    return balance;
  } catch (err) {
    console.error(err);
    return -1;
  }
};

module.exports = { getBalance, verifySignature };
