From 2daed26ce42dc26ddd5523b9ced86cec0facbd71 Mon Sep 17 00:00:00 2001 From: Developer <> Date: Thu, 1 Aug 2019 10:56:14 -0400 Subject: [PATCH] add Nimiq support --- README.md | 2 + dist/wallet-address-validator.js | 182 +++++++++++++++++++-------- dist/wallet-address-validator.min.js | 2 +- package.json | 1 + src/currencies.js | 7 ++ src/iban_validator.js | 32 +++++ test/wallet_address_validator.js | 21 ++++ 7 files changed, 193 insertions(+), 54 deletions(-) create mode 100644 src/iban_validator.js diff --git a/README.md b/README.md index 4b3b78fd..5fe384ba 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,8 @@ npm install wallet-address-validator * NEO/NEO, `'NEO'` or `'NEO'` * NeoGas/GAS, `'neogas'` or `'GAS'` +* Nimiq/NIM, `'nimiq'` or `'NIM'` + * Peercoin/PPCoin/PPC, `'peercoin'` or `'PPC'` * Primecoin/XPM, `'primecoin'` or `'XPM'` * Protoshares/PTS, `'protoshares'` or `'PTS'` diff --git a/dist/wallet-address-validator.js b/dist/wallet-address-validator.js index 408a78e5..2606b686 100644 --- a/dist/wallet-address-validator.js +++ b/dist/wallet-address-validator.js @@ -1,4 +1,4 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.WAValidator = f()}})(function(){var define,module,exports;return (function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 0) { throw new Error('Invalid string. Length must be a multiple of 4') } - // the number of equal signs (place holders) - // if there are two placeholders, than the two characters before it - // represent one byte - // if there is only one, then the three characters before it represent 2 bytes - // this is just a cheap hack to not do indexOf twice - return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 + // Trim off extra bytes after placeholder bytes are found + // See: https://github.com/beatgammit/base64-js/issues/42 + var validLen = b64.indexOf('=') + if (validLen === -1) validLen = len + + var placeHoldersLen = validLen === len + ? 0 + : 4 - (validLen % 4) + + return [validLen, placeHoldersLen] } +// base64 is 4/3 + up to two characters of the original data function byteLength (b64) { - // base64 is 4/3 + up to two characters of the original data - return (b64.length * 3 / 4) - placeHoldersCount(b64) + var lens = getLens(b64) + var validLen = lens[0] + var placeHoldersLen = lens[1] + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen +} + +function _byteLength (b64, validLen, placeHoldersLen) { + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen } function toByteArray (b64) { - var i, l, tmp, placeHolders, arr - var len = b64.length - placeHolders = placeHoldersCount(b64) + var tmp + var lens = getLens(b64) + var validLen = lens[0] + var placeHoldersLen = lens[1] - arr = new Arr((len * 3 / 4) - placeHolders) + var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen)) + + var curByte = 0 // if there are placeholders, only get up to the last complete 4 chars - l = placeHolders > 0 ? len - 4 : len + var len = placeHoldersLen > 0 + ? validLen - 4 + : validLen - var L = 0 + for (var i = 0; i < len; i += 4) { + tmp = + (revLookup[b64.charCodeAt(i)] << 18) | + (revLookup[b64.charCodeAt(i + 1)] << 12) | + (revLookup[b64.charCodeAt(i + 2)] << 6) | + revLookup[b64.charCodeAt(i + 3)] + arr[curByte++] = (tmp >> 16) & 0xFF + arr[curByte++] = (tmp >> 8) & 0xFF + arr[curByte++] = tmp & 0xFF + } - for (i = 0; i < l; i += 4) { - tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] - arr[L++] = (tmp >> 16) & 0xFF - arr[L++] = (tmp >> 8) & 0xFF - arr[L++] = tmp & 0xFF + if (placeHoldersLen === 2) { + tmp = + (revLookup[b64.charCodeAt(i)] << 2) | + (revLookup[b64.charCodeAt(i + 1)] >> 4) + arr[curByte++] = tmp & 0xFF } - if (placeHolders === 2) { - tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) - arr[L++] = tmp & 0xFF - } else if (placeHolders === 1) { - tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) - arr[L++] = (tmp >> 8) & 0xFF - arr[L++] = tmp & 0xFF + if (placeHoldersLen === 1) { + tmp = + (revLookup[b64.charCodeAt(i)] << 10) | + (revLookup[b64.charCodeAt(i + 1)] << 4) | + (revLookup[b64.charCodeAt(i + 2)] >> 2) + arr[curByte++] = (tmp >> 8) & 0xFF + arr[curByte++] = tmp & 0xFF } return arr } function tripletToBase64 (num) { - return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] + return lookup[num >> 18 & 0x3F] + + lookup[num >> 12 & 0x3F] + + lookup[num >> 6 & 0x3F] + + lookup[num & 0x3F] } function encodeChunk (uint8, start, end) { var tmp var output = [] for (var i = start; i < end; i += 3) { - tmp = ((uint8[i] << 16) & 0xFF0000) + ((uint8[i + 1] << 8) & 0xFF00) + (uint8[i + 2] & 0xFF) + tmp = + ((uint8[i] << 16) & 0xFF0000) + + ((uint8[i + 1] << 8) & 0xFF00) + + (uint8[i + 2] & 0xFF) output.push(tripletToBase64(tmp)) } return output.join('') @@ -182,31 +214,34 @@ function fromByteArray (uint8) { var tmp var len = uint8.length var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes - var output = '' var parts = [] var maxChunkLength = 16383 // must be multiple of 3 // go through the array every three bytes, we'll deal with trailing stuff later for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { - parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) + parts.push(encodeChunk( + uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength) + )) } // pad the end with zeros, but make sure to not forget the extra bytes if (extraBytes === 1) { tmp = uint8[len - 1] - output += lookup[tmp >> 2] - output += lookup[(tmp << 4) & 0x3F] - output += '==' + parts.push( + lookup[tmp >> 2] + + lookup[(tmp << 4) & 0x3F] + + '==' + ) } else if (extraBytes === 2) { - tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) - output += lookup[tmp >> 10] - output += lookup[(tmp >> 4) & 0x3F] - output += lookup[(tmp << 2) & 0x3F] - output += '=' + tmp = (uint8[len - 2] << 8) + uint8[len - 1] + parts.push( + lookup[tmp >> 10] + + lookup[(tmp >> 4) & 0x3F] + + lookup[(tmp << 2) & 0x3F] + + '=' + ) } - parts.push(output) - return parts.join('') } @@ -1951,7 +1986,7 @@ function numberIsNaN (obj) { },{"base64-js":2,"ieee754":4}],4:[function(require,module,exports){ exports.read = function (buffer, offset, isLE, mLen, nBytes) { var e, m - var eLen = nBytes * 8 - mLen - 1 + var eLen = (nBytes * 8) - mLen - 1 var eMax = (1 << eLen) - 1 var eBias = eMax >> 1 var nBits = -7 @@ -1964,12 +1999,12 @@ exports.read = function (buffer, offset, isLE, mLen, nBytes) { e = s & ((1 << (-nBits)) - 1) s >>= (-nBits) nBits += eLen - for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} + for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {} m = e & ((1 << (-nBits)) - 1) e >>= (-nBits) nBits += mLen - for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} + for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {} if (e === 0) { e = 1 - eBias @@ -1984,7 +2019,7 @@ exports.read = function (buffer, offset, isLE, mLen, nBytes) { exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { var e, m, c - var eLen = nBytes * 8 - mLen - 1 + var eLen = (nBytes * 8) - mLen - 1 var eMax = (1 << eLen) - 1 var eBias = eMax >> 1 var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) @@ -2017,7 +2052,7 @@ exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { m = 0 e = eMax } else if (e + eBias >= 1) { - m = (value * c - 1) * Math.pow(2, mLen) + m = ((value * c) - 1) * Math.pow(2, mLen) e = e + eBias } else { m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) @@ -5506,6 +5541,7 @@ var ETHValidator = require('./ethereum_validator'); var BTCValidator = require('./bitcoin_validator'); var XMRValidator = require('./monero_validator'); var NANOValidator = require('./nano_validator'); +var IBANValidator = require('./iban_validator'); // defines P2PKH and P2SH address types for standard (prod) and testnet networks var CURRENCIES = [{ @@ -5712,6 +5748,12 @@ var CURRENCIES = [{ name: 'raiblocks', symbol: 'xrb', validator: NANOValidator, +},{ + name: 'nimiq', + symbol: 'nim', + countryCode: 'NQ', + length: 36, + validator: IBANValidator }]; @@ -5728,7 +5770,7 @@ module.exports = { } }; -},{"./bitcoin_validator":8,"./ethereum_validator":19,"./monero_validator":20,"./nano_validator":21,"./ripple_validator":22}],19:[function(require,module,exports){ +},{"./bitcoin_validator":8,"./ethereum_validator":19,"./iban_validator":20,"./monero_validator":21,"./nano_validator":22,"./ripple_validator":23}],19:[function(require,module,exports){ var cryptoUtils = require('./crypto/utils'); module.exports = { @@ -5765,6 +5807,40 @@ module.exports = { }; },{"./crypto/utils":17}],20:[function(require,module,exports){ +function ibanCheck(address) { + var num = address.split('').map(function(c) { + var code = c.toUpperCase().charCodeAt(0); + return code >= 48 && code <= 57 ? c : (code - 55).toString(); + }).join(''); + var tmp = ''; + + for (var i = 0; i < Math.ceil(num.length / 6); i++) { + tmp = (parseInt(tmp + num.substr(i * 6, 6)) % 97).toString(); + } + + return parseInt(tmp); +} + +module.exports = { + isValidAddress: function (address, currency) { + currency = currency || {}; + address = address.replace(/ /g, ''); + + if (address.substr(0, 2).toUpperCase() !== currency.countryCode) { + return false; + } + if (address.length !== currency.length) { + return false; + } + if (ibanCheck(address.substr(4) + address.substr(0, 4)) !== 1) { + return false; + } + + return true; + } +}; + +},{}],21:[function(require,module,exports){ var cryptoUtils = require('./crypto/utils'); var cnBase58 = require('./crypto/cnBase58'); @@ -5826,7 +5902,7 @@ module.exports = { } }; -},{"./crypto/cnBase58":14,"./crypto/utils":17}],21:[function(require,module,exports){ +},{"./crypto/cnBase58":14,"./crypto/utils":17}],22:[function(require,module,exports){ var cryptoUtils = require('./crypto/utils'); var baseX = require('base-x'); @@ -5855,7 +5931,7 @@ module.exports = { } }; -},{"./crypto/utils":17,"base-x":1}],22:[function(require,module,exports){ +},{"./crypto/utils":17,"base-x":1}],23:[function(require,module,exports){ var cryptoUtils = require('./crypto/utils'); var baseX = require('base-x'); @@ -5885,7 +5961,7 @@ module.exports = { } }; -},{"./crypto/utils":17,"base-x":1}],23:[function(require,module,exports){ +},{"./crypto/utils":17,"base-x":1}],24:[function(require,module,exports){ var currencies = require('./currencies'); var DEFAULT_CURRENCY_NAME = 'bitcoin'; @@ -5902,5 +5978,5 @@ module.exports = { }, }; -},{"./currencies":18}]},{},[23])(23) -}); \ No newline at end of file +},{"./currencies":18}]},{},[24])(24) +}); diff --git a/dist/wallet-address-validator.min.js b/dist/wallet-address-validator.min.js index 407ffa71..4f33acbf 100644 --- a/dist/wallet-address-validator.min.js +++ b/dist/wallet-address-validator.min.js @@ -1 +1 @@ -!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).WAValidator=t()}}(function(){return function o(s,a,u){function f(e,t){if(!a[e]){if(!s[e]){var r="function"==typeof require&&require;if(!t&&r)return r(e,!0);if(h)return h(e,!0);var n=new Error("Cannot find module '"+e+"'");throw n.code="MODULE_NOT_FOUND",n}var i=a[e]={exports:{}};s[e][0].call(i.exports,function(t){var r=s[e][1][t];return f(r||t)},i,i.exports,o,s,a,u)}return a[e].exports}for(var h="function"==typeof require&&require,t=0;t>=8;for(;0>=8}for(var s=0;t[s]===h&&s>16&255,o[a++]=n>>8&255,o[a++]=255&n;2===i?(n=f[t.charCodeAt(r)]<<2|f[t.charCodeAt(r+1)]>>4,o[a++]=255&n):1===i&&(n=f[t.charCodeAt(r)]<<10|f[t.charCodeAt(r+1)]<<4|f[t.charCodeAt(r+2)]>>2,o[a++]=n>>8&255,o[a++]=255&n);return o},e.fromByteArray=function(t){for(var r,e=t.length,n=e%3,i="",o=[],s=0,a=e-n;s>2],i+=u[r<<4&63],i+="=="):2===n&&(r=(t[e-2]<<8)+t[e-1],i+=u[r>>10],i+=u[r>>4&63],i+=u[r<<2&63],i+="=");return o.push(i),o.join("")};for(var u=[],f=[],h="undefined"!=typeof Uint8Array?Uint8Array:Array,n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",i=0,o=n.length;i>18&63]+u[i>>12&63]+u[i>>6&63]+u[63&i]);return o.join("")}f["-".charCodeAt(0)]=62,f["_".charCodeAt(0)]=63},{}],3:[function(t,r,e){"use strict";var n=t("base64-js"),o=t("ieee754");e.Buffer=c,e.SlowBuffer=function(t){+t!=t&&(t=0);return c.alloc(+t)},e.INSPECT_MAX_BYTES=50;var i=2147483647;function s(t){if(i>>1;case"base64":return O(t).length;default:if(n)return L(t).length;r=(""+r).toLowerCase(),n=!0}}function d(t,r,e){var n=t[r];t[r]=t[e],t[e]=n}function g(t,r,e,n,i){if(0===t.length)return-1;if("string"==typeof e?(n=e,e=0):2147483647=t.length){if(i)return-1;e=t.length-1}else if(e<0){if(!i)return-1;e=0}if("string"==typeof r&&(r=c.from(r,n)),c.isBuffer(r))return 0===r.length?-1:y(t,r,e,n,i);if("number"==typeof r)return r&=255,"function"==typeof Uint8Array.prototype.indexOf?i?Uint8Array.prototype.indexOf.call(t,r,e):Uint8Array.prototype.lastIndexOf.call(t,r,e):y(t,[r],e,n,i);throw new TypeError("val must be string, number or Buffer")}function y(t,r,e,n,i){var o,s=1,a=t.length,u=r.length;if(void 0!==n&&("ucs2"===(n=String(n).toLowerCase())||"ucs-2"===n||"utf16le"===n||"utf-16le"===n)){if(t.length<2||r.length<2)return-1;a/=s=2,u/=2,e/=2}function f(t,r){return 1===s?t[r]:t.readUInt16BE(r*s)}if(i){var h=-1;for(o=e;o>>10&1023|55296),h=56320|1023&h),n.push(h),i+=c}return function(t){var r=t.length;if(r<=_)return String.fromCharCode.apply(String,t);var e="",n=0;for(;nthis.length)return"";if((void 0===e||e>this.length)&&(e=this.length),e<=0)return"";if((e>>>=0)<=(r>>>=0))return"";for(t||(t="utf8");;)switch(t){case"hex":return x(this,r,e);case"utf8":case"utf-8":return m(this,r,e);case"ascii":return A(this,r,e);case"latin1":case"binary":return E(this,r,e);case"base64":return b(this,r,e);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return B(this,r,e);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=!0}}.apply(this,arguments)},c.prototype.equals=function(t){if(!c.isBuffer(t))throw new TypeError("Argument must be a Buffer");return this===t||0===c.compare(this,t)},c.prototype.inspect=function(){var t="",r=e.INSPECT_MAX_BYTES;return 0r&&(t+=" ... ")),""},c.prototype.compare=function(t,r,e,n,i){if(!c.isBuffer(t))throw new TypeError("Argument must be a Buffer");if(void 0===r&&(r=0),void 0===e&&(e=t?t.length:0),void 0===n&&(n=0),void 0===i&&(i=this.length),r<0||e>t.length||n<0||i>this.length)throw new RangeError("out of range index");if(i<=n&&e<=r)return 0;if(i<=n)return-1;if(e<=r)return 1;if(this===t)return 0;for(var o=(i>>>=0)-(n>>>=0),s=(e>>>=0)-(r>>>=0),a=Math.min(o,s),u=this.slice(n,i),f=t.slice(r,e),h=0;h>>=0,isFinite(e)?(e>>>=0,void 0===n&&(n="utf8")):(n=e,e=void 0)}var i=this.length-r;if((void 0===e||ithis.length)throw new RangeError("Attempt to write outside buffer bounds");n||(n="utf8");for(var o,s,a,u,f,h,c,l,p,d=!1;;)switch(n){case"hex":return v(this,t,r,e);case"utf8":case"utf-8":return l=r,p=e,M(L(t,(c=this).length-l),c,l,p);case"ascii":return w(this,t,r,e);case"latin1":case"binary":return w(this,t,r,e);case"base64":return u=this,f=r,h=e,M(O(t),u,f,h);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return s=r,a=e,M(function(t,r){for(var e,n,i,o=[],s=0;s>8,i=e%256,o.push(i),o.push(n);return o}(t,(o=this).length-s),o,s,a);default:if(d)throw new TypeError("Unknown encoding: "+n);n=(""+n).toLowerCase(),d=!0}},c.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var _=4096;function A(t,r,e){var n="";e=Math.min(t.length,e);for(var i=r;it.length)throw new RangeError("Index out of range")}function T(t,r,e,n,i,o){if(e+n>t.length)throw new RangeError("Index out of range");if(e<0)throw new RangeError("Index out of range")}function S(t,r,e,n,i){return r=+r,e>>>=0,i||T(t,0,e,4),o.write(t,r,e,n,23,4),e+4}function C(t,r,e,n,i){return r=+r,e>>>=0,i||T(t,0,e,8),o.write(t,r,e,n,52,8),e+8}c.prototype.slice=function(t,r){var e=this.length;(t=~~t)<0?(t+=e)<0&&(t=0):e>>=0,r>>>=0,e||k(t,r,this.length);for(var n=this[t],i=1,o=0;++o>>=0,r>>>=0,e||k(t,r,this.length);for(var n=this[t+--r],i=1;0>>=0,r||k(t,1,this.length),this[t]},c.prototype.readUInt16LE=function(t,r){return t>>>=0,r||k(t,2,this.length),this[t]|this[t+1]<<8},c.prototype.readUInt16BE=function(t,r){return t>>>=0,r||k(t,2,this.length),this[t]<<8|this[t+1]},c.prototype.readUInt32LE=function(t,r){return t>>>=0,r||k(t,4,this.length),(this[t]|this[t+1]<<8|this[t+2]<<16)+16777216*this[t+3]},c.prototype.readUInt32BE=function(t,r){return t>>>=0,r||k(t,4,this.length),16777216*this[t]+(this[t+1]<<16|this[t+2]<<8|this[t+3])},c.prototype.readIntLE=function(t,r,e){t>>>=0,r>>>=0,e||k(t,r,this.length);for(var n=this[t],i=1,o=0;++o>>=0,r>>>=0,e||k(t,r,this.length);for(var n=r,i=1,o=this[t+--n];0>>=0,r||k(t,1,this.length),128&this[t]?-1*(255-this[t]+1):this[t]},c.prototype.readInt16LE=function(t,r){t>>>=0,r||k(t,2,this.length);var e=this[t]|this[t+1]<<8;return 32768&e?4294901760|e:e},c.prototype.readInt16BE=function(t,r){t>>>=0,r||k(t,2,this.length);var e=this[t+1]|this[t]<<8;return 32768&e?4294901760|e:e},c.prototype.readInt32LE=function(t,r){return t>>>=0,r||k(t,4,this.length),this[t]|this[t+1]<<8|this[t+2]<<16|this[t+3]<<24},c.prototype.readInt32BE=function(t,r){return t>>>=0,r||k(t,4,this.length),this[t]<<24|this[t+1]<<16|this[t+2]<<8|this[t+3]},c.prototype.readFloatLE=function(t,r){return t>>>=0,r||k(t,4,this.length),o.read(this,t,!0,23,4)},c.prototype.readFloatBE=function(t,r){return t>>>=0,r||k(t,4,this.length),o.read(this,t,!1,23,4)},c.prototype.readDoubleLE=function(t,r){return t>>>=0,r||k(t,8,this.length),o.read(this,t,!0,52,8)},c.prototype.readDoubleBE=function(t,r){return t>>>=0,r||k(t,8,this.length),o.read(this,t,!1,52,8)},c.prototype.writeUIntLE=function(t,r,e,n){(t=+t,r>>>=0,e>>>=0,n)||U(this,t,r,e,Math.pow(2,8*e)-1,0);var i=1,o=0;for(this[r]=255&t;++o>>=0,e>>>=0,n)||U(this,t,r,e,Math.pow(2,8*e)-1,0);var i=e-1,o=1;for(this[r+i]=255&t;0<=--i&&(o*=256);)this[r+i]=t/o&255;return r+e},c.prototype.writeUInt8=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,1,255,0),this[r]=255&t,r+1},c.prototype.writeUInt16LE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,2,65535,0),this[r]=255&t,this[r+1]=t>>>8,r+2},c.prototype.writeUInt16BE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,2,65535,0),this[r]=t>>>8,this[r+1]=255&t,r+2},c.prototype.writeUInt32LE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,4,4294967295,0),this[r+3]=t>>>24,this[r+2]=t>>>16,this[r+1]=t>>>8,this[r]=255&t,r+4},c.prototype.writeUInt32BE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,4,4294967295,0),this[r]=t>>>24,this[r+1]=t>>>16,this[r+2]=t>>>8,this[r+3]=255&t,r+4},c.prototype.writeIntLE=function(t,r,e,n){if(t=+t,r>>>=0,!n){var i=Math.pow(2,8*e-1);U(this,t,r,e,i-1,-i)}var o=0,s=1,a=0;for(this[r]=255&t;++o>0)-a&255;return r+e},c.prototype.writeIntBE=function(t,r,e,n){if(t=+t,r>>>=0,!n){var i=Math.pow(2,8*e-1);U(this,t,r,e,i-1,-i)}var o=e-1,s=1,a=0;for(this[r+o]=255&t;0<=--o&&(s*=256);)t<0&&0===a&&0!==this[r+o+1]&&(a=1),this[r+o]=(t/s>>0)-a&255;return r+e},c.prototype.writeInt8=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,1,127,-128),t<0&&(t=255+t+1),this[r]=255&t,r+1},c.prototype.writeInt16LE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,2,32767,-32768),this[r]=255&t,this[r+1]=t>>>8,r+2},c.prototype.writeInt16BE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,2,32767,-32768),this[r]=t>>>8,this[r+1]=255&t,r+2},c.prototype.writeInt32LE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,4,2147483647,-2147483648),this[r]=255&t,this[r+1]=t>>>8,this[r+2]=t>>>16,this[r+3]=t>>>24,r+4},c.prototype.writeInt32BE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),this[r]=t>>>24,this[r+1]=t>>>16,this[r+2]=t>>>8,this[r+3]=255&t,r+4},c.prototype.writeFloatLE=function(t,r,e){return S(this,t,r,!0,e)},c.prototype.writeFloatBE=function(t,r,e){return S(this,t,r,!1,e)},c.prototype.writeDoubleLE=function(t,r,e){return C(this,t,r,!0,e)},c.prototype.writeDoubleBE=function(t,r,e){return C(this,t,r,!1,e)},c.prototype.copy=function(t,r,e,n){if(!c.isBuffer(t))throw new TypeError("argument should be a Buffer");if(e||(e=0),n||0===n||(n=this.length),r>=t.length&&(r=t.length),r||(r=0),0=this.length)throw new RangeError("Index out of range");if(n<0)throw new RangeError("sourceEnd out of bounds");n>this.length&&(n=this.length),t.length-r>>=0,e=void 0===e?this.length:e>>>0,t||(t=0),"number"==typeof t)for(o=r;o>6|192,63&e|128)}else if(e<65536){if((r-=3)<0)break;o.push(e>>12|224,e>>6&63|128,63&e|128)}else{if(!(e<1114112))throw new Error("Invalid code point");if((r-=4)<0)break;o.push(e>>18|240,e>>12&63|128,e>>6&63|128,63&e|128)}}return o}function O(t){return n.toByteArray(function(t){if((t=(t=t.split("=")[0]).trim().replace(I,"")).length<2)return"";for(;t.length%4!=0;)t+="=";return t}(t))}function M(t,r,e,n){for(var i=0;i=r.length||i>=t.length);++i)r[i+e]=t[i];return i}function H(t){return t instanceof ArrayBuffer||null!=t&&null!=t.constructor&&"ArrayBuffer"===t.constructor.name&&"number"==typeof t.byteLength}function F(t){return t!=t}},{"base64-js":2,ieee754:4}],4:[function(t,r,e){e.read=function(t,r,e,n,i){var o,s,a=8*i-n-1,u=(1<>1,h=-7,c=e?i-1:0,l=e?-1:1,p=t[r+c];for(c+=l,o=p&(1<<-h)-1,p>>=-h,h+=a;0>=-h,h+=n;0>1,l=23===i?Math.pow(2,-24)-Math.pow(2,-77):0,p=n?0:o-1,d=n?1:-1,g=r<0||0===r&&1/r<0?1:0;for(r=Math.abs(r),isNaN(r)||r===1/0?(a=isNaN(r)?1:0,s=h):(s=Math.floor(Math.log(r)/Math.LN2),r*(u=Math.pow(2,-s))<1&&(s--,u*=2),2<=(r+=1<=s+c?l/u:l*Math.pow(2,1-c))*u&&(s++,u/=2),h<=s+c?(a=0,s=h):1<=s+c?(a=(r*u-1)*Math.pow(2,i),s+=c):(a=r*Math.pow(2,c-1)*Math.pow(2,i),s=0));8<=i;t[e+p]=255&a,p+=d,a/=256,i-=8);for(s=s<= 1");if(0!==s.lastIndexOf("SHA-",0))throw Error("Chosen SHA variant is not supported");if(c=function(t,r){return F(t,r,s)},l=function(t,r,e,n){var i,o;if("SHA-224"!==s&&"SHA-256"!==s)throw Error("Unexpected error in SHA-2 implementation");for(i=15+(r+65>>>9<<4),o=16;t.length<=i;)t.push(0);for(t[r>>>5]|=128<<24-r%32,r+=e,t[i]=4294967295&r,t[i-1]=r/4294967296|0,e=t.length,r=0;r>>3)/4-1,n>>5;for(t=(r=u(t,y,v)).binLen,e=r.value,r=t>>>5,n=0;n>>5),v=t%h,m=!0},this.getHash=function(t,r){var e,n,i,o;if(!0===w)throw Error("Cannot call getHash after setting HMAC key");switch(i=B(r),t){case"HEX":e=function(t){return _(t,f,i)};break;case"B64":e=function(t){return A(t,f,i)};break;case"BYTES":e=function(t){return E(t,f)};break;case"ARRAYBUFFER":try{n=new ArrayBuffer(0)}catch(t){throw Error("ARRAYBUFFER not supported by this environment")}e=function(t){return x(t,f)};break;default:throw Error("format must be HEX, B64, BYTES, or ARRAYBUFFER")}for(o=l(y.slice(),v,g,p(a)),n=1;n>>2]>>>8*(3+n%4*-1),o+="0123456789abcdef".charAt(i>>>4&15)+"0123456789abcdef".charAt(15&i);return e.outputUpper?o.toUpperCase():o}function A(t,r,e){var n,i,o,s="",a=r/8;for(n=0;n>>2]:0,o=n+2>>2]:0,o=(t[n>>>2]>>>8*(3+n%4*-1)&255)<<16|(i>>>8*(3+(n+1)%4*-1)&255)<<8|o>>>8*(3+(n+2)%4*-1)&255,i=0;i<4;i+=1)s+=8*n+6*i<=r?"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(o>>>6*(3-i)&63):e.b64Pad;return s}function E(t,r){var e,n,i="",o=r/8;for(e=0;e>>2]>>>8*(3+e%4*-1)&255,i+=String.fromCharCode(n);return i}function x(t,r){var e,n,i=r/8,o=new ArrayBuffer(i);for(n=new Uint8Array(o),e=0;e>>2]>>>8*(3+e%4*-1)&255;return o}function B(t){var r={outputUpper:!1,b64Pad:"=",shakeLen:-1};if(t=t||{},r.outputUpper=t.outputUpper||!1,!0===t.hasOwnProperty("b64Pad")&&(r.b64Pad=t.b64Pad),"boolean"!=typeof r.outputUpper)throw Error("Invalid outputUpper formatting option");if("string"!=typeof r.b64Pad)throw Error("Invalid b64Pad formatting option");return r}function k(t,l){var r;switch(l){case"UTF8":case"UTF16BE":case"UTF16LE":break;default:throw Error("encoding must be UTF8, UTF16BE, or UTF16LE")}switch(t){case"HEX":r=function(t,r,e){var n,i,o,s,a,u=t.length;if(0!=u%2)throw Error("String of HEX type must be in byte increments");for(r=r||[0],a=(e=e||0)>>>3,n=0;n>>1)+a)>>>2;r.length<=o;)r.push(0);r[o]|=i<<8*(3+s%4*-1)}return{value:r,binLen:4*u+e}};break;case"TEXT":r=function(t,r,e){var n,i,o,s,a,u,f,h,c=0;if(r=r||[0],a=(e=e||0)>>>3,"UTF8"===l)for(h=3,o=0;o>>6),i.push(128|63&n)):n<55296||57344<=n?i.push(224|n>>>12,128|n>>>6&63,128|63&n):(o+=1,n=65536+((1023&n)<<10|1023&t.charCodeAt(o)),i.push(240|n>>>18,128|n>>>12&63,128|n>>>6&63,128|63&n)),s=0;s>>2;r.length<=u;)r.push(0);r[u]|=i[s]<<8*(h+f%4*-1),c+=1}else if("UTF16BE"===l||"UTF16LE"===l)for(h=2,i="UTF16LE"===l||"UTF16LE"!==l&&!1,o=0;o>>8),u=(f=c+a)>>>2;r.length<=u;)r.push(0);r[u]|=n<<8*(h+f%4*-1),c+=2}return{value:r,binLen:8*c+e}};break;case"B64":r=function(t,r,e){var n,i,o,s,a,u,f,h=0;if(-1===t.search(/^[a-zA-Z0-9=+\/]+$/))throw Error("Invalid character in base-64 string");if(i=t.indexOf("="),t=t.replace(/\=/g,""),-1!==i&&i>=8;for(;0>=8}for(var s=0;t[s]===h&&s>16&255,o[s++]=r>>8&255,o[s++]=255&r;var f,h;2===i&&(r=c[t.charCodeAt(u)]<<2|c[t.charCodeAt(u+1)]>>4,o[s++]=255&r);1===i&&(r=c[t.charCodeAt(u)]<<10|c[t.charCodeAt(u+1)]<<4|c[t.charCodeAt(u+2)]>>2,o[s++]=r>>8&255,o[s++]=255&r);return o},e.fromByteArray=function(t){for(var r,e=t.length,n=e%3,i=[],o=0,s=e-n;o>2]+a[r<<4&63]+"==")):2===n&&(r=(t[e-2]<<8)+t[e-1],i.push(a[r>>10]+a[r>>4&63]+a[r<<2&63]+"="));return i.join("")};for(var a=[],c=[],l="undefined"!=typeof Uint8Array?Uint8Array:Array,n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",i=0,o=n.length;i>18&63]+a[i>>12&63]+a[i>>6&63]+a[63&i]);return o.join("")}c["-".charCodeAt(0)]=62,c["_".charCodeAt(0)]=63},{}],3:[function(t,r,e){"use strict";var n=t("base64-js"),o=t("ieee754");e.Buffer=c,e.SlowBuffer=function(t){+t!=t&&(t=0);return c.alloc(+t)},e.INSPECT_MAX_BYTES=50;var i=2147483647;function s(t){if(i>>1;case"base64":return O(t).length;default:if(n)return L(t).length;r=(""+r).toLowerCase(),n=!0}}function d(t,r,e){var n=t[r];t[r]=t[e],t[e]=n}function g(t,r,e,n,i){if(0===t.length)return-1;if("string"==typeof e?(n=e,e=0):2147483647=t.length){if(i)return-1;e=t.length-1}else if(e<0){if(!i)return-1;e=0}if("string"==typeof r&&(r=c.from(r,n)),c.isBuffer(r))return 0===r.length?-1:y(t,r,e,n,i);if("number"==typeof r)return r&=255,"function"==typeof Uint8Array.prototype.indexOf?i?Uint8Array.prototype.indexOf.call(t,r,e):Uint8Array.prototype.lastIndexOf.call(t,r,e):y(t,[r],e,n,i);throw new TypeError("val must be string, number or Buffer")}function y(t,r,e,n,i){var o,s=1,a=t.length,u=r.length;if(void 0!==n&&("ucs2"===(n=String(n).toLowerCase())||"ucs-2"===n||"utf16le"===n||"utf-16le"===n)){if(t.length<2||r.length<2)return-1;a/=s=2,u/=2,e/=2}function f(t,r){return 1===s?t[r]:t.readUInt16BE(r*s)}if(i){var h=-1;for(o=e;o>>10&1023|55296),h=56320|1023&h),n.push(h),i+=c}return function(t){var r=t.length;if(r<=_)return String.fromCharCode.apply(String,t);var e="",n=0;for(;nthis.length)return"";if((void 0===e||e>this.length)&&(e=this.length),e<=0)return"";if((e>>>=0)<=(r>>>=0))return"";for(t||(t="utf8");;)switch(t){case"hex":return x(this,r,e);case"utf8":case"utf-8":return m(this,r,e);case"ascii":return A(this,r,e);case"latin1":case"binary":return E(this,r,e);case"base64":return b(this,r,e);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return B(this,r,e);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=!0}}.apply(this,arguments)},c.prototype.equals=function(t){if(!c.isBuffer(t))throw new TypeError("Argument must be a Buffer");return this===t||0===c.compare(this,t)},c.prototype.inspect=function(){var t="",r=e.INSPECT_MAX_BYTES;return 0r&&(t+=" ... ")),""},c.prototype.compare=function(t,r,e,n,i){if(!c.isBuffer(t))throw new TypeError("Argument must be a Buffer");if(void 0===r&&(r=0),void 0===e&&(e=t?t.length:0),void 0===n&&(n=0),void 0===i&&(i=this.length),r<0||e>t.length||n<0||i>this.length)throw new RangeError("out of range index");if(i<=n&&e<=r)return 0;if(i<=n)return-1;if(e<=r)return 1;if(this===t)return 0;for(var o=(i>>>=0)-(n>>>=0),s=(e>>>=0)-(r>>>=0),a=Math.min(o,s),u=this.slice(n,i),f=t.slice(r,e),h=0;h>>=0,isFinite(e)?(e>>>=0,void 0===n&&(n="utf8")):(n=e,e=void 0)}var i=this.length-r;if((void 0===e||ithis.length)throw new RangeError("Attempt to write outside buffer bounds");n||(n="utf8");for(var o,s,a,u,f,h,c,l,p,d=!1;;)switch(n){case"hex":return v(this,t,r,e);case"utf8":case"utf-8":return l=r,p=e,M(L(t,(c=this).length-l),c,l,p);case"ascii":return w(this,t,r,e);case"latin1":case"binary":return w(this,t,r,e);case"base64":return u=this,f=r,h=e,M(O(t),u,f,h);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return s=r,a=e,M(function(t,r){for(var e,n,i,o=[],s=0;s>8,i=e%256,o.push(i),o.push(n);return o}(t,(o=this).length-s),o,s,a);default:if(d)throw new TypeError("Unknown encoding: "+n);n=(""+n).toLowerCase(),d=!0}},c.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var _=4096;function A(t,r,e){var n="";e=Math.min(t.length,e);for(var i=r;it.length)throw new RangeError("Index out of range")}function S(t,r,e,n,i,o){if(e+n>t.length)throw new RangeError("Index out of range");if(e<0)throw new RangeError("Index out of range")}function T(t,r,e,n,i){return r=+r,e>>>=0,i||S(t,0,e,4),o.write(t,r,e,n,23,4),e+4}function C(t,r,e,n,i){return r=+r,e>>>=0,i||S(t,0,e,8),o.write(t,r,e,n,52,8),e+8}c.prototype.slice=function(t,r){var e=this.length;(t=~~t)<0?(t+=e)<0&&(t=0):e>>=0,r>>>=0,e||k(t,r,this.length);for(var n=this[t],i=1,o=0;++o>>=0,r>>>=0,e||k(t,r,this.length);for(var n=this[t+--r],i=1;0>>=0,r||k(t,1,this.length),this[t]},c.prototype.readUInt16LE=function(t,r){return t>>>=0,r||k(t,2,this.length),this[t]|this[t+1]<<8},c.prototype.readUInt16BE=function(t,r){return t>>>=0,r||k(t,2,this.length),this[t]<<8|this[t+1]},c.prototype.readUInt32LE=function(t,r){return t>>>=0,r||k(t,4,this.length),(this[t]|this[t+1]<<8|this[t+2]<<16)+16777216*this[t+3]},c.prototype.readUInt32BE=function(t,r){return t>>>=0,r||k(t,4,this.length),16777216*this[t]+(this[t+1]<<16|this[t+2]<<8|this[t+3])},c.prototype.readIntLE=function(t,r,e){t>>>=0,r>>>=0,e||k(t,r,this.length);for(var n=this[t],i=1,o=0;++o>>=0,r>>>=0,e||k(t,r,this.length);for(var n=r,i=1,o=this[t+--n];0>>=0,r||k(t,1,this.length),128&this[t]?-1*(255-this[t]+1):this[t]},c.prototype.readInt16LE=function(t,r){t>>>=0,r||k(t,2,this.length);var e=this[t]|this[t+1]<<8;return 32768&e?4294901760|e:e},c.prototype.readInt16BE=function(t,r){t>>>=0,r||k(t,2,this.length);var e=this[t+1]|this[t]<<8;return 32768&e?4294901760|e:e},c.prototype.readInt32LE=function(t,r){return t>>>=0,r||k(t,4,this.length),this[t]|this[t+1]<<8|this[t+2]<<16|this[t+3]<<24},c.prototype.readInt32BE=function(t,r){return t>>>=0,r||k(t,4,this.length),this[t]<<24|this[t+1]<<16|this[t+2]<<8|this[t+3]},c.prototype.readFloatLE=function(t,r){return t>>>=0,r||k(t,4,this.length),o.read(this,t,!0,23,4)},c.prototype.readFloatBE=function(t,r){return t>>>=0,r||k(t,4,this.length),o.read(this,t,!1,23,4)},c.prototype.readDoubleLE=function(t,r){return t>>>=0,r||k(t,8,this.length),o.read(this,t,!0,52,8)},c.prototype.readDoubleBE=function(t,r){return t>>>=0,r||k(t,8,this.length),o.read(this,t,!1,52,8)},c.prototype.writeUIntLE=function(t,r,e,n){(t=+t,r>>>=0,e>>>=0,n)||U(this,t,r,e,Math.pow(2,8*e)-1,0);var i=1,o=0;for(this[r]=255&t;++o>>=0,e>>>=0,n)||U(this,t,r,e,Math.pow(2,8*e)-1,0);var i=e-1,o=1;for(this[r+i]=255&t;0<=--i&&(o*=256);)this[r+i]=t/o&255;return r+e},c.prototype.writeUInt8=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,1,255,0),this[r]=255&t,r+1},c.prototype.writeUInt16LE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,2,65535,0),this[r]=255&t,this[r+1]=t>>>8,r+2},c.prototype.writeUInt16BE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,2,65535,0),this[r]=t>>>8,this[r+1]=255&t,r+2},c.prototype.writeUInt32LE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,4,4294967295,0),this[r+3]=t>>>24,this[r+2]=t>>>16,this[r+1]=t>>>8,this[r]=255&t,r+4},c.prototype.writeUInt32BE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,4,4294967295,0),this[r]=t>>>24,this[r+1]=t>>>16,this[r+2]=t>>>8,this[r+3]=255&t,r+4},c.prototype.writeIntLE=function(t,r,e,n){if(t=+t,r>>>=0,!n){var i=Math.pow(2,8*e-1);U(this,t,r,e,i-1,-i)}var o=0,s=1,a=0;for(this[r]=255&t;++o>0)-a&255;return r+e},c.prototype.writeIntBE=function(t,r,e,n){if(t=+t,r>>>=0,!n){var i=Math.pow(2,8*e-1);U(this,t,r,e,i-1,-i)}var o=e-1,s=1,a=0;for(this[r+o]=255&t;0<=--o&&(s*=256);)t<0&&0===a&&0!==this[r+o+1]&&(a=1),this[r+o]=(t/s>>0)-a&255;return r+e},c.prototype.writeInt8=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,1,127,-128),t<0&&(t=255+t+1),this[r]=255&t,r+1},c.prototype.writeInt16LE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,2,32767,-32768),this[r]=255&t,this[r+1]=t>>>8,r+2},c.prototype.writeInt16BE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,2,32767,-32768),this[r]=t>>>8,this[r+1]=255&t,r+2},c.prototype.writeInt32LE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,4,2147483647,-2147483648),this[r]=255&t,this[r+1]=t>>>8,this[r+2]=t>>>16,this[r+3]=t>>>24,r+4},c.prototype.writeInt32BE=function(t,r,e){return t=+t,r>>>=0,e||U(this,t,r,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),this[r]=t>>>24,this[r+1]=t>>>16,this[r+2]=t>>>8,this[r+3]=255&t,r+4},c.prototype.writeFloatLE=function(t,r,e){return T(this,t,r,!0,e)},c.prototype.writeFloatBE=function(t,r,e){return T(this,t,r,!1,e)},c.prototype.writeDoubleLE=function(t,r,e){return C(this,t,r,!0,e)},c.prototype.writeDoubleBE=function(t,r,e){return C(this,t,r,!1,e)},c.prototype.copy=function(t,r,e,n){if(!c.isBuffer(t))throw new TypeError("argument should be a Buffer");if(e||(e=0),n||0===n||(n=this.length),r>=t.length&&(r=t.length),r||(r=0),0=this.length)throw new RangeError("Index out of range");if(n<0)throw new RangeError("sourceEnd out of bounds");n>this.length&&(n=this.length),t.length-r>>=0,e=void 0===e?this.length:e>>>0,t||(t=0),"number"==typeof t)for(o=r;o>6|192,63&e|128)}else if(e<65536){if((r-=3)<0)break;o.push(e>>12|224,e>>6&63|128,63&e|128)}else{if(!(e<1114112))throw new Error("Invalid code point");if((r-=4)<0)break;o.push(e>>18|240,e>>12&63|128,e>>6&63|128,63&e|128)}}return o}function O(t){return n.toByteArray(function(t){if((t=(t=t.split("=")[0]).trim().replace(I,"")).length<2)return"";for(;t.length%4!=0;)t+="=";return t}(t))}function M(t,r,e,n){for(var i=0;i=r.length||i>=t.length);++i)r[i+e]=t[i];return i}function H(t){return t instanceof ArrayBuffer||null!=t&&null!=t.constructor&&"ArrayBuffer"===t.constructor.name&&"number"==typeof t.byteLength}function F(t){return t!=t}},{"base64-js":2,ieee754:4}],4:[function(t,r,e){e.read=function(t,r,e,n,i){var o,s,a=8*i-n-1,u=(1<>1,h=-7,c=e?i-1:0,l=e?-1:1,p=t[r+c];for(c+=l,o=p&(1<<-h)-1,p>>=-h,h+=a;0>=-h,h+=n;0>1,l=23===i?Math.pow(2,-24)-Math.pow(2,-77):0,p=n?0:o-1,d=n?1:-1,g=r<0||0===r&&1/r<0?1:0;for(r=Math.abs(r),isNaN(r)||r===1/0?(a=isNaN(r)?1:0,s=h):(s=Math.floor(Math.log(r)/Math.LN2),r*(u=Math.pow(2,-s))<1&&(s--,u*=2),2<=(r+=1<=s+c?l/u:l*Math.pow(2,1-c))*u&&(s++,u/=2),h<=s+c?(a=0,s=h):1<=s+c?(a=(r*u-1)*Math.pow(2,i),s+=c):(a=r*Math.pow(2,c-1)*Math.pow(2,i),s=0));8<=i;t[e+p]=255&a,p+=d,a/=256,i-=8);for(s=s<= 1");if(0!==s.lastIndexOf("SHA-",0))throw Error("Chosen SHA variant is not supported");if(c=function(t,r){return F(t,r,s)},l=function(t,r,e,n){var i,o;if("SHA-224"!==s&&"SHA-256"!==s)throw Error("Unexpected error in SHA-2 implementation");for(i=15+(r+65>>>9<<4),o=16;t.length<=i;)t.push(0);for(t[r>>>5]|=128<<24-r%32,r+=e,t[i]=4294967295&r,t[i-1]=r/4294967296|0,e=t.length,r=0;r>>3)/4-1,n>>5;for(t=(r=u(t,y,v)).binLen,e=r.value,r=t>>>5,n=0;n>>5),v=t%h,m=!0},this.getHash=function(t,r){var e,n,i,o;if(!0===w)throw Error("Cannot call getHash after setting HMAC key");switch(i=B(r),t){case"HEX":e=function(t){return _(t,f,i)};break;case"B64":e=function(t){return A(t,f,i)};break;case"BYTES":e=function(t){return E(t,f)};break;case"ARRAYBUFFER":try{n=new ArrayBuffer(0)}catch(t){throw Error("ARRAYBUFFER not supported by this environment")}e=function(t){return x(t,f)};break;default:throw Error("format must be HEX, B64, BYTES, or ARRAYBUFFER")}for(o=l(y.slice(),v,g,p(a)),n=1;n>>2]>>>8*(3+n%4*-1),o+="0123456789abcdef".charAt(i>>>4&15)+"0123456789abcdef".charAt(15&i);return e.outputUpper?o.toUpperCase():o}function A(t,r,e){var n,i,o,s="",a=r/8;for(n=0;n>>2]:0,o=n+2>>2]:0,o=(t[n>>>2]>>>8*(3+n%4*-1)&255)<<16|(i>>>8*(3+(n+1)%4*-1)&255)<<8|o>>>8*(3+(n+2)%4*-1)&255,i=0;i<4;i+=1)s+=8*n+6*i<=r?"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(o>>>6*(3-i)&63):e.b64Pad;return s}function E(t,r){var e,n,i="",o=r/8;for(e=0;e>>2]>>>8*(3+e%4*-1)&255,i+=String.fromCharCode(n);return i}function x(t,r){var e,n,i=r/8,o=new ArrayBuffer(i);for(n=new Uint8Array(o),e=0;e>>2]>>>8*(3+e%4*-1)&255;return o}function B(t){var r={outputUpper:!1,b64Pad:"=",shakeLen:-1};if(t=t||{},r.outputUpper=t.outputUpper||!1,!0===t.hasOwnProperty("b64Pad")&&(r.b64Pad=t.b64Pad),"boolean"!=typeof r.outputUpper)throw Error("Invalid outputUpper formatting option");if("string"!=typeof r.b64Pad)throw Error("Invalid b64Pad formatting option");return r}function k(t,l){var r;switch(l){case"UTF8":case"UTF16BE":case"UTF16LE":break;default:throw Error("encoding must be UTF8, UTF16BE, or UTF16LE")}switch(t){case"HEX":r=function(t,r,e){var n,i,o,s,a,u=t.length;if(0!=u%2)throw Error("String of HEX type must be in byte increments");for(r=r||[0],a=(e=e||0)>>>3,n=0;n>>1)+a)>>>2;r.length<=o;)r.push(0);r[o]|=i<<8*(3+s%4*-1)}return{value:r,binLen:4*u+e}};break;case"TEXT":r=function(t,r,e){var n,i,o,s,a,u,f,h,c=0;if(r=r||[0],a=(e=e||0)>>>3,"UTF8"===l)for(h=3,o=0;o>>6),i.push(128|63&n)):n<55296||57344<=n?i.push(224|n>>>12,128|n>>>6&63,128|63&n):(o+=1,n=65536+((1023&n)<<10|1023&t.charCodeAt(o)),i.push(240|n>>>18,128|n>>>12&63,128|n>>>6&63,128|63&n)),s=0;s>>2;r.length<=u;)r.push(0);r[u]|=i[s]<<8*(h+f%4*-1),c+=1}else if("UTF16BE"===l||"UTF16LE"===l)for(h=2,i="UTF16LE"===l||"UTF16LE"!==l&&!1,o=0;o>>8),u=(f=c+a)>>>2;r.length<=u;)r.push(0);r[u]|=n<<8*(h+f%4*-1),c+=2}return{value:r,binLen:8*c+e}};break;case"B64":r=function(t,r,e){var n,i,o,s,a,u,f,h=0;if(-1===t.search(/^[a-zA-Z0-9=+\/]+$/))throw Error("Invalid character in base-64 string");if(i=t.indexOf("="),t=t.replace(/\=/g,""),-1!==i&&i= 48 && code <= 57 ? c : (code - 55).toString(); + }).join(''); + var tmp = ''; + + for (var i = 0; i < Math.ceil(num.length / 6); i++) { + tmp = (parseInt(tmp + num.substr(i * 6, 6)) % 97).toString(); + } + + return parseInt(tmp); +} + +module.exports = { + isValidAddress: function (address, currency) { + currency = currency || {}; + address = address.replace(/ /g, ''); + + if (address.substr(0, 2).toUpperCase() !== currency.countryCode) { + return false; + } + if (address.length !== currency.length) { + return false; + } + if (ibanCheck(address.substr(4) + address.substr(0, 4)) !== 1) { + return false; + } + + return true; + } +}; diff --git a/test/wallet_address_validator.js b/test/wallet_address_validator.js index 808c8de5..c8aa1fcd 100644 --- a/test/wallet_address_validator.js +++ b/test/wallet_address_validator.js @@ -382,6 +382,17 @@ describe('WAValidator.validate()', function () { valid('xrb_1q79ahdr36uqn38p5tp5sqwkn73rnpj1k8obtuetdbjcx37d5gahhd1u9cuh', 'nano'); valid('nano_1q79ahdr36uqn38p5tp5sqwkn73rnpj1k8obtuetdbjcx37d5gahhd1u9cuh', 'nano'); }); + + it('should return true for correct nimiq addresses', function () { + valid('NQ09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNT', 'nimiq'); + valid('nq09 qcg8 hg0t nebp 4djn 81xv cju2 9ly8 bvnt', 'nimiq'); + valid('NQ09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNT', 'nimiq'); + valid('nq09qcg8hg0tnebp4djn81xvcju29ly8bvnt', 'nimiq'); + valid('NQ09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNT', 'NIM'); + valid('nq09 qcg8 hg0t nebp 4djn 81xv cju2 9ly8 bvnt', 'NIM'); + valid('NQ09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNT', 'NIM'); + valid('nq09qcg8hg0tnebp4djn81xvcju29ly8bvnt', 'NIM'); + }); }); describe('invalid results', function () { @@ -595,5 +606,15 @@ describe('WAValidator.validate()', function () { invalid('xrb_1111111112111111111111111111111111111111111111111111hifc8npp', 'nano'); invalid('nano_111111111111111111111111111111111111111111111111111hifc8npp', 'nano'); }); + + it('should return false for incorrect nimiq addresses', function () { + commonTests('nimiq'); + invalid('NQ09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNX', 'nimiq'); + invalid('NQ09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNX', 'nimiq'); + invalid('09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNT', 'nimiq'); + invalid('09 qcg8 hg0t nebp 4djn 81xv cju2 9ly8 bvnt', 'nimiq'); + invalid('09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNT', 'nimiq'); + invalid('09qcg8hg0tnebp4djn81xvcju29ly8bvnt', 'nimiq'); + }); }); });