import TcpSocket from 'react-native-tcp-socket';
import pools from '../configs/ledgers/indy';
export const canConnectToHost = async host => new Promise(resolve => {
  const socketTimeoutInMs = 3000;
  const client = TcpSocket.createConnection(host, () => {
    resolve(true);
    client.destroy();
  });

  // Other events that can be safely be ignored. See the
  // library for more details:
  // https://www.npmjs.com/package/react-native-tcp-socket

  client.on('error', () => {
    client.destroy();
    resolve(false);
  });
  client.on('timeout', () => {
    client.destroy();
    client.removeAllListeners();
    resolve(false);
  });
  client.setTimeout(socketTimeoutInMs);
});
export const fetchLedgerNodes = (indyNamespace = 'sovrin') => {
  const [pool] = pools.filter(p => p.indyNamespace === indyNamespace);
  if (!pool) {
    return [];
  }
  const genesisTransactionsAsString = pool.genesisTransactions;
  let genesisTransactions = [];
  if (genesisTransactionsAsString) {
    try {
      genesisTransactions = genesisTransactionsAsString.split('\n').map(g => JSON.parse(g));
    } catch (error) {
      return [];
    }
  }
  const nodes = genesisTransactions.map(g => {
    return {
      host: g.txn.data.data.client_ip,
      port: parseInt(g.txn.data.data.client_port)
    };
  });
  return nodes;
};
//# sourceMappingURL=network.js.map