<commit_before>var _ = require('lodash'),
    q = require('q'),
    bitcoin = require('bitcoinjs-lib'),
    bitcoinMessage = require('bitcoinjs-message'),

    bip39 = require("bip39"),
    Wallet = require('./wallet'),
    RestClient = require('./rest_client'),
    Encryption = require('./encryption'),
    KeyDerivation = require('./keyderivation'),
    EncryptionMnemonic = require('./encryption_mnemonic'),
    blocktrail = require('./blocktrail'),
    randomBytes = require('randombytes'),
    CryptoJS = require('crypto-js'),
    webworkifier = require('./webworkifier');

var useWebWorker = require('./use-webworker')();

/**
 * Bindings to conssume the BlockTrail API
 *
 * @param options       object{
 *                          apiKey: 'API_KEY',
 *                          apiSecret: 'API_SECRET',
 *                          host: 'defaults to api.blocktrail.com',
 *                          network: 'BTC|LTC',
 *                          testnet: true|false
 *                      }
 * @constructor
 */
var APIClient = function(options) {
    var self = this;

    // handle constructor call without 'new'
    if (!(this instanceof APIClient)) {
        return new APIClient(options);
    }

    // BLOCKTRAIL_SDK_API_ENDPOINT overwrite for development
    if (process.env.BLOCKTRAIL_SDK_API_ENDPOINT) {
        options.host = process.env.BLOCKTRAIL_SDK_API_ENDPOINT;
    }

    // trim off leading https?://
    if (options.host && options.host.indexOf("https://") === 0) {
        options.https = true;
        options.host = options.host.substr(8);
    } else if (options.host && options.host.indexOf("http://") === 0) {
        options.https = false;
        options.host = options.host.substr(7);
    }

    if (typeof options.https === "undefined") {
        options.https = true;
    }

    if (!options.host) {
        options.host = 'api.blocktrail.com';
    }

    if (!options.port) {
        options.port = options.https ? 443 : 80;
    }

    self.testnet = options.testnet = options.testnet || false;
    if (self.testnet) {
        self.network = bitcoin.networks.testnet;
    } else {
        self.network = bitcoin.networks.bitcoin;
    }

    self.bitcoinCash = options.network && options.network === "BCC";

    if (!options.endpoint) {
        options.endpoint = "/" + (options.apiVersion || "v1") + "/" + (self.testnet ? "t" : "") + (options.network || 'BTC').toUpperCase();
    }

    /**
     * @type RestClient
     */
    self.client = new RestClient(options);
};

var determineDataStorageV2_3 = function(options) {
    return q.when(options)
        .then(function(options) {
            // legacy
            if (options.storePrimaryMnemonic) {
                options.storeDataOnServer = options.storePrimaryMnemonic;
            }

            // storeDataOnServer=false when primarySeed is provided<commit_msg>move initialization helper for RestClient out into seperate function to reuse.<commit_after>var _ = require('lodash'),
    q = require('q'),
    bitcoin = require('bitcoinjs-lib'),
    bitcoinMessage = require('bitcoinjs-message'),

    bip39 = require("bip39"),
    Wallet = require('./wallet'),
    RestClient = require('./rest_client'),
    Encryption = require('./encryption'),
    KeyDerivation = require('./keyderivation'),
    EncryptionMnemonic = require('./encryption_mnemonic'),
    blocktrail = require('./blocktrail'),
    randomBytes = require('randombytes'),
    CryptoJS = require('crypto-js'),
    webworkifier = require('./webworkifier');

var useWebWorker = require('./use-webworker')();

/**
 * Bindings to conssume the BlockTrail API
 *
 * @param options       object{
 *                          apiKey: 'API_KEY',
 *                          apiSecret: 'API_SECRET',
 *                          host: 'defaults to api.blocktrail.com',
 *                          network: 'BTC|LTC',
 *                          testnet: true|false
 *                      }
 * @constructor
 */
var APIClient = function(options) {
    var self = this;

    // handle constructor call without 'new'
    if (!(this instanceof APIClient)) {
        return new APIClient(options);
    }

    self.testnet = options.testnet = options.testnet || false;
    if (self.testnet) {
        self.network = bitcoin.networks.testnet;
    } else {
        self.network = bitcoin.networks.bitcoin;
    }

    self.bitcoinCash = options.network && options.network === "BCC";

    options.apiNetwork = options.apiNetwork || ((self.testnet ? "t" : "") + (options.network || 'BTC').toUpperCase());

    /**
     * @type RestClient
     */
    self.client = APIClient.initRestClient(options);
};

APIClient.initRestClient = function(options) {
    // BLOCKTRAIL_SDK_API_ENDPOINT overwrite for development
    if (process.env.BLOCKTRAIL_SDK_API_ENDPOINT) {
        options.host = process.env.BLOCKTRAIL_SDK_API_ENDPOINT;
    }

    // trim off leading https?://
    if (options.host && options.host.indexOf("https://") === 0) {
        options.https = true;
        options.host = options.host.substr(8);
    } else if (options.host && options.host.indexOf("http://") === 0) {
        options.https = false;
        options.host = options.host.substr(7);
    }

    if (typeof options.https === "undefined") {
        options.https = true;
    }

    if (!options.host) {
        options.host = 'api.blocktrail.com';
    }

    if (!options.port) {
        options.port = options.https ? 443 : 80;
    }

    if (!options.endpoint) {
        options.endpoint = "/" + (options.apiVersion || "v1") + (options.apiNetwork ? ("/" + options.apiNetwork) : "");
    }

    return new RestClient(options);
};

var determineDataStorageV2_3 = function(options) {
    return q.when(options)
        .then(function(options) {
            // legacy
            if (options.storePrimaryMnemonic) {
                options.storeDataOnServer = options.storePrimaryMnemonic;
            }

            // storeDataOnServer=false when primarySeed is provided
{'fraction_non_alphanumeric': 0.142809364548495, 'fraction_numerical': 0.005351170568561873, 'mean_word_length': 2.3301781737193763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2437481', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1748, 'n_words': 339}