// Generated by IcedCoffeeScript 1.7.1-e
(function() {
  var SHA256, base58, bufeq_secure, check, decode, kbpgp,
    __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

  kbpgp = require('kbpgp');

  SHA256 = kbpgp.hash.SHA256;

  base58 = kbpgp.base58;

  bufeq_secure = require('pgp-utils').util.bufeq_secure;

  decode = function(s) {
    var buf, err;
    try {
      buf = base58.decode(s);
      return [null, buf];
    } catch (_error) {
      err = _error;
      return [err, null];
    }
  };

  exports.check = check = function(s, opts) {
    var buf, checksum1, checksum2, err, pkhash, ret, v, versions, _ref;
    if (opts == null) {
      opts = {};
    }
    versions = opts.versions || [0, 5];
    _ref = decode(s), err = _ref[0], buf = _ref[1];
    if (err != null) {
      return [err, null];
    }
    v = buf.readUInt8(0);
    err = !(__indexOf.call(versions, v) >= 0) ? new Error("Bad version found: " + v) : (pkhash = buf.slice(0, -4), checksum1 = buf.slice(-4), checksum2 = (SHA256(SHA256(pkhash))).slice(0, 4), !bufeq_secure(checksum1, checksum2) ? new Error("Checksum mismatch") : null);
    ret = err != null ? null : {
      version: v,
      pkhash: pkhash.slice(1)
    };
    return [err, ret];
  };

}).call(this);
