All files / lib index.js

100% Statements 149/149
100% Branches 42/42
100% Functions 35/35
100% Lines 146/146

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 3181x 1x 1x   1x 1x 1x   1x       26x 25x   24x   24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x     45x   45x   45x 45x         45x           70x 70x 26x 24x   44x   44x               44x   44x 23x   21x       23x 23x   23x   23x 22x     23x 23x 23x   1x   22x 1x   21x     23x 23x 23x     21x 21x 21x   21x   21x 20x     21x 21x 21x   1x     20x 1x   19x     21x 21x 21x       9x     9x     6x 3x 3x   6x     6x     2x 1x 1x   2x     2x     6x                 6x     3x 1x         2x 7x     2x 1x       1x       1x 4x 4x 4x   4x 3x     4x     1x     3x     3x     3x     3x     3x 3x 2x   1x                 1x     2x 1x 1x   2x     2x     2x 2x     3x 3x     3x     3x     6x                 6x     2x     2x     4x     4x     2x 1x 1x   2x     2x     3x 3x     3x 3x     2x 2x     3x     3x     4x     4x       1x  
const https = require(`https`),
  crypto = require(`crypto`),
  qs = require(`querystring`);
 
const CoinpaymentsConfig = require(`./config`),
  CoinpaymentsError = require(`./error`),
  CoinpaymentsUtil = require(`./util`);
 
const { API_VERSION, API_PROTOCOL, API_HOST, API_PATH } = CoinpaymentsConfig;
 
class Coinpayments {
  constructor({ key = ``, secret = `` }) {
    if (!key) throw new CoinpaymentsError(`Missing public key`);
    if (!secret) throw new CoinpaymentsError(`Missing private key`);
 
    this.credentials = { key, secret };
 
    this.getBasicInfo = this.getBasicInfo.bind(this);
    this.rates = this.rates.bind(this);
    this.balances = this.balances.bind(this);
    this.getDepositAddress = this.getDepositAddress.bind(this);
    this.createTransaction = this.createTransaction.bind(this);
    this.getCallbackAddress = this.getCallbackAddress.bind(this);
    this.getTx = this.getTx.bind(this);
    this.getTxList = this.getTxList.bind(this);
    this.getTxMulti = this.getTxMulti.bind(this);
    this.createTransfer = this.createTransfer.bind(this);
    this.convertCoins = this.convertCoins.bind(this);
    this.convertLimits = this.convertLimits.bind(this);
    this.getWithdrawalHistory = this.getWithdrawalHistory.bind(this);
    this.getWithdrawalInfo = this.getWithdrawalInfo.bind(this);
    this.getConversionInfo = this.getConversionInfo.bind(this);
    this.getProfile = this.getProfile.bind(this);
    this.tagList = this.tagList.bind(this);
    this.updateTagProfile = this.updateTagProfile.bind(this);
    this.claimTag = this.claimTag.bind(this);
  }
  _getPrivateHeaders(parameters) {
    const { secret, key } = this.credentials;
 
    parameters.key = key;
 
    const paramString = qs.stringify(parameters);
    const signature = crypto
      .createHmac(`sha512`, secret)
      .update(paramString)
      .digest(`hex`);
 
    return {
      "Content-Type": `application/x-www-form-urlencoded`,
      HMAC: signature
    };
  }
  request(parameters, callback) {
    const assertResult = CoinpaymentsUtil.assertPayload(parameters);
    if (assertResult.isError) {
      if (callback) return callback(assertResult.error);
      return Promise.reject(assertResult.error);
    }
    parameters.version = API_VERSION;
 
    const options = {
      protocol: API_PROTOCOL,
      method: `post`,
      host: API_HOST,
      path: API_PATH,
      headers: this._getPrivateHeaders(parameters)
    };
 
    const query = qs.stringify(parameters);
 
    if (callback) {
      return this.callbackRequest(options, query, callback);
    } else {
      return this.promiseRequest(options, query);
    }
  }
  callbackRequest(options, query, callback) {
    const req = https.request(options, res => {
      let data = ``;
 
      res.setEncoding(`utf8`);
 
      res.on(`data`, chunk => {
        data += chunk;
      });
 
      res.on(`end`, () => {
        try {
          data = JSON.parse(data);
        } catch (e) {
          return callback(new CoinpaymentsError(`Invalid response`, data));
        }
        if (data.error !== `ok`) {
          return callback(new CoinpaymentsError(data.error, data));
        }
        return callback(null, data.result);
      });
    });
    req.on(`error`, callback);
    req.write(query);
    req.end();
  }
  promiseRequest(options, query) {
    return new Promise((resolve, reject) => {
      const req = https.request(options, res => {
        let data = ``;
 
        res.setEncoding(`utf8`);
 
        res.on(`data`, chunk => {
          data += chunk;
        });
 
        res.on(`end`, () => {
          try {
            data = JSON.parse(data);
          } catch (e) {
            return reject(new CoinpaymentsError(`Invalid response`, data));
          }
 
          if (data.error !== `ok`) {
            return reject(new CoinpaymentsError(data.error, data));
          }
          return resolve(data.result);
        });
      });
      req.on(`error`, reject);
      req.write(query);
      req.end();
    });
  }
  createTransaction(options, callback) {
    options = Object.assign({}, options, {
      cmd: `create_transaction`
    });
    return this.request(options, callback);
  }
  rates(options = {}, callback) {
    if (typeof options == `function`) {
      callback = options;
      options = {};
    }
    options = Object.assign({}, options, {
      cmd: `rates`
    });
    return this.request(options, callback);
  }
  balances(options = {}, callback) {
    if (typeof options == `function`) {
      callback = options;
      options = {};
    }
    options = Object.assign({}, options, {
      cmd: `balances`
    });
    return this.request(options, callback);
  }
  createWithdrawal(options, callback) {
    options = Object.assign(
      {
        auto_confirm: 1
      },
      options,
      {
        cmd: `create_withdrawal`
      }
    );
    return this.request(options, callback);
  }
  createMassWithdrawal(withdrawalArray, callback) {
    if (!(withdrawalArray instanceof Array)) {
      return callback(
        new CoinpaymentsError(`Invalid withdrawal array`.withdrawalArray)
      );
    }
 
    withdrawalArray = withdrawalArray.filter(function(w) {
      return w.currency && w.amount && w.address;
    });
 
    if (!withdrawalArray.length)
      return callback(
        new CoinpaymentsError(`Invalid withdrawal array`.withdrawalArray)
      );
 
    const options = {
      cmd: `create_mass_withdrawal`
    };
 
    withdrawalArray.reduce(function(options, w, index) {
      options[`wd[wd${index + 1}][amount]`] = w.amount;
      options[`wd[wd${index + 1}][address]`] = w.address;
      options[`wd[wd${index + 1}][currency]`] = w.currency;
 
      if (w.dest_tag) {
        options[`wd[wd${index + 1}][dest_tag]`] = w.dest_tag;
      }
 
      return options;
    }, options);
 
    return this.request(options, callback);
  }
  getTx(options, callback) {
    options = Object.assign({}, options, {
      cmd: `get_tx_info`
    });
    return this.request(options, callback);
  }
  getWithdrawalInfo(options, callback) {
    options = Object.assign({}, options, {
      cmd: `get_withdrawal_info`
    });
    return this.request(options, callback);
  }
  getTxMulti(tx_id_array = [], callback) {
    const options = {};
    if (!(tx_id_array instanceof Array) || !tx_id_array.length) {
      return callback(new CoinpaymentsError(`Invalid argument`, tx_id_array));
    }
    Object.assign(
      options,
      {
        txid: tx_id_array.join(`|`)
      },
      {
        cmd: `get_tx_info_multi`
      }
    );
    return this.request(options, callback);
  }
  getTxList(options = {}, callback) {
    if (typeof options == `function`) {
      callback = options;
      options = {};
    }
    options = Object.assign({}, options, {
      cmd: `get_tx_ids`
    });
    return this.request(options, callback);
  }
  getBasicInfo(callback) {
    const options = { cmd: `get_basic_info` };
    return this.request(options, callback);
  }
  getDepositAddress(options, callback) {
    options = Object.assign({}, options, { cmd: `get_deposit_address` });
    return this.request(options, callback);
  }
  getCallbackAddress(options, callback) {
    options = Object.assign({}, options, {
      cmd: `get_callback_address`
    });
    return this.request(options, callback);
  }
  createTransfer(options, callback) {
    options = Object.assign(
      {
        auto_confirm: 1
      },
      options,
      {
        cmd: `create_transfer`
      }
    );
    return this.request(options, callback);
  }
  convertCoins(options, callback) {
    options = Object.assign({}, options, {
      cmd: `convert`
    });
    return this.request(options, callback);
  }
  convertLimits(options, callback) {
    options = Object.assign({}, options, {
      cmd: `convert_limits`
    });
    return this.request(options, callback);
  }
  getWithdrawalHistory(options = {}, callback) {
    if (typeof options == `function`) {
      callback = options;
      options = {};
    }
    options = Object.assign({}, options, {
      cmd: `get_withdrawal_history`
    });
    return this.request(options, callback);
  }
  getConversionInfo(options, callback) {
    options = Object.assign({}, options, { cmd: `get_conversion_info` });
    return this.request(options, callback);
  }
  getProfile(options, callback) {
    options = Object.assign({}, options, { cmd: `get_pbn_info` });
    return this.request(options, callback);
  }
  tagList(callback) {
    const options = { cmd: `get_pbn_list` };
    return this.request(options, callback);
  }
  updateTagProfile(options, callback) {
    options = Object.assign({}, options, {
      cmd: `update_pbn_tag`
    });
    return this.request(options, callback);
  }
  claimTag(options, callback) {
    options = Object.assign({}, options, {
      cmd: `claim_pbn_tag`
    });
    return this.request(options, callback);
  }
}
 
module.exports = Coinpayments;