@thorwallet/xchain-litecoin
Version:
Custom Litecoin client and utilities used by XChainJS clients
1,936 lines (1,573 loc) • 2.42 MB
JavaScript
import buffer from 'buffer';
import readableStream from 'readable-stream';
import stream from 'stream';
import string_decoder$1 from 'string_decoder';
import axios from 'axios';
import crypto$2$1 from 'crypto';
import events from 'events';
import { NativeModules } from 'react-native';
import hexLite from 'hex-lite';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function unwrapExports (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
function getCjsExportFromNamespace (n) {
return n && n['default'] || n;
}
var inherits_browser = createCommonjsModule(function (module) {
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
module.exports = function inherits(ctor, superCtor) {
if (superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
}
};
} else {
// old school shim for old browsers
module.exports = function inherits(ctor, superCtor) {
if (superCtor) {
ctor.super_ = superCtor;
var TempCtor = function () {};
TempCtor.prototype = superCtor.prototype;
ctor.prototype = new TempCtor();
ctor.prototype.constructor = ctor;
}
};
}
});
var safeBuffer = createCommonjsModule(function (module, exports) {
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
/* eslint-disable node/no-deprecated-api */
var Buffer = buffer.Buffer;
// alternative to using Object.keys for old browsers
function copyProps (src, dst) {
for (var key in src) {
dst[key] = src[key];
}
}
if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
module.exports = buffer;
} else {
// Copy properties from require('buffer')
copyProps(buffer, exports);
exports.Buffer = SafeBuffer;
}
function SafeBuffer (arg, encodingOrOffset, length) {
return Buffer(arg, encodingOrOffset, length)
}
SafeBuffer.prototype = Object.create(Buffer.prototype);
// Copy static methods from Buffer
copyProps(Buffer, SafeBuffer);
SafeBuffer.from = function (arg, encodingOrOffset, length) {
if (typeof arg === 'number') {
throw new TypeError('Argument must not be a number')
}
return Buffer(arg, encodingOrOffset, length)
};
SafeBuffer.alloc = function (size, fill, encoding) {
if (typeof size !== 'number') {
throw new TypeError('Argument must be a number')
}
var buf = Buffer(size);
if (fill !== undefined) {
if (typeof encoding === 'string') {
buf.fill(fill, encoding);
} else {
buf.fill(fill);
}
} else {
buf.fill(0);
}
return buf
};
SafeBuffer.allocUnsafe = function (size) {
if (typeof size !== 'number') {
throw new TypeError('Argument must be a number')
}
return Buffer(size)
};
SafeBuffer.allocUnsafeSlow = function (size) {
if (typeof size !== 'number') {
throw new TypeError('Argument must be a number')
}
return buffer.SlowBuffer(size)
};
});
var safeBuffer_1 = safeBuffer.Buffer;
var Buffer$1 = safeBuffer.Buffer;
var Transform = readableStream.Transform;
function throwIfNotStringOrBuffer (val, prefix) {
if (!Buffer$1.isBuffer(val) && typeof val !== 'string') {
throw new TypeError(prefix + ' must be a string or a buffer')
}
}
function HashBase (blockSize) {
Transform.call(this);
this._block = Buffer$1.allocUnsafe(blockSize);
this._blockSize = blockSize;
this._blockOffset = 0;
this._length = [0, 0, 0, 0];
this._finalized = false;
}
inherits_browser(HashBase, Transform);
HashBase.prototype._transform = function (chunk, encoding, callback) {
var error = null;
try {
this.update(chunk, encoding);
} catch (err) {
error = err;
}
callback(error);
};
HashBase.prototype._flush = function (callback) {
var error = null;
try {
this.push(this.digest());
} catch (err) {
error = err;
}
callback(error);
};
HashBase.prototype.update = function (data, encoding) {
throwIfNotStringOrBuffer(data, 'Data');
if (this._finalized) throw new Error('Digest already called')
if (!Buffer$1.isBuffer(data)) data = Buffer$1.from(data, encoding);
// consume data
var block = this._block;
var offset = 0;
while (this._blockOffset + data.length - offset >= this._blockSize) {
for (var i = this._blockOffset; i < this._blockSize;) block[i++] = data[offset++];
this._update();
this._blockOffset = 0;
}
while (offset < data.length) block[this._blockOffset++] = data[offset++];
// update length
for (var j = 0, carry = data.length * 8; carry > 0; ++j) {
this._length[j] += carry;
carry = (this._length[j] / 0x0100000000) | 0;
if (carry > 0) this._length[j] -= 0x0100000000 * carry;
}
return this
};
HashBase.prototype._update = function () {
throw new Error('_update is not implemented')
};
HashBase.prototype.digest = function (encoding) {
if (this._finalized) throw new Error('Digest already called')
this._finalized = true;
var digest = this._digest();
if (encoding !== undefined) digest = digest.toString(encoding);
// reset state
this._block.fill(0);
this._blockOffset = 0;
for (var i = 0; i < 4; ++i) this._length[i] = 0;
return digest
};
HashBase.prototype._digest = function () {
throw new Error('_digest is not implemented')
};
var hashBase = HashBase;
var Buffer$2 = safeBuffer.Buffer;
var ARRAY16 = new Array(16);
function MD5 () {
hashBase.call(this, 64);
// state
this._a = 0x67452301;
this._b = 0xefcdab89;
this._c = 0x98badcfe;
this._d = 0x10325476;
}
inherits_browser(MD5, hashBase);
MD5.prototype._update = function () {
var M = ARRAY16;
for (var i = 0; i < 16; ++i) M[i] = this._block.readInt32LE(i * 4);
var a = this._a;
var b = this._b;
var c = this._c;
var d = this._d;
a = fnF(a, b, c, d, M[0], 0xd76aa478, 7);
d = fnF(d, a, b, c, M[1], 0xe8c7b756, 12);
c = fnF(c, d, a, b, M[2], 0x242070db, 17);
b = fnF(b, c, d, a, M[3], 0xc1bdceee, 22);
a = fnF(a, b, c, d, M[4], 0xf57c0faf, 7);
d = fnF(d, a, b, c, M[5], 0x4787c62a, 12);
c = fnF(c, d, a, b, M[6], 0xa8304613, 17);
b = fnF(b, c, d, a, M[7], 0xfd469501, 22);
a = fnF(a, b, c, d, M[8], 0x698098d8, 7);
d = fnF(d, a, b, c, M[9], 0x8b44f7af, 12);
c = fnF(c, d, a, b, M[10], 0xffff5bb1, 17);
b = fnF(b, c, d, a, M[11], 0x895cd7be, 22);
a = fnF(a, b, c, d, M[12], 0x6b901122, 7);
d = fnF(d, a, b, c, M[13], 0xfd987193, 12);
c = fnF(c, d, a, b, M[14], 0xa679438e, 17);
b = fnF(b, c, d, a, M[15], 0x49b40821, 22);
a = fnG(a, b, c, d, M[1], 0xf61e2562, 5);
d = fnG(d, a, b, c, M[6], 0xc040b340, 9);
c = fnG(c, d, a, b, M[11], 0x265e5a51, 14);
b = fnG(b, c, d, a, M[0], 0xe9b6c7aa, 20);
a = fnG(a, b, c, d, M[5], 0xd62f105d, 5);
d = fnG(d, a, b, c, M[10], 0x02441453, 9);
c = fnG(c, d, a, b, M[15], 0xd8a1e681, 14);
b = fnG(b, c, d, a, M[4], 0xe7d3fbc8, 20);
a = fnG(a, b, c, d, M[9], 0x21e1cde6, 5);
d = fnG(d, a, b, c, M[14], 0xc33707d6, 9);
c = fnG(c, d, a, b, M[3], 0xf4d50d87, 14);
b = fnG(b, c, d, a, M[8], 0x455a14ed, 20);
a = fnG(a, b, c, d, M[13], 0xa9e3e905, 5);
d = fnG(d, a, b, c, M[2], 0xfcefa3f8, 9);
c = fnG(c, d, a, b, M[7], 0x676f02d9, 14);
b = fnG(b, c, d, a, M[12], 0x8d2a4c8a, 20);
a = fnH(a, b, c, d, M[5], 0xfffa3942, 4);
d = fnH(d, a, b, c, M[8], 0x8771f681, 11);
c = fnH(c, d, a, b, M[11], 0x6d9d6122, 16);
b = fnH(b, c, d, a, M[14], 0xfde5380c, 23);
a = fnH(a, b, c, d, M[1], 0xa4beea44, 4);
d = fnH(d, a, b, c, M[4], 0x4bdecfa9, 11);
c = fnH(c, d, a, b, M[7], 0xf6bb4b60, 16);
b = fnH(b, c, d, a, M[10], 0xbebfbc70, 23);
a = fnH(a, b, c, d, M[13], 0x289b7ec6, 4);
d = fnH(d, a, b, c, M[0], 0xeaa127fa, 11);
c = fnH(c, d, a, b, M[3], 0xd4ef3085, 16);
b = fnH(b, c, d, a, M[6], 0x04881d05, 23);
a = fnH(a, b, c, d, M[9], 0xd9d4d039, 4);
d = fnH(d, a, b, c, M[12], 0xe6db99e5, 11);
c = fnH(c, d, a, b, M[15], 0x1fa27cf8, 16);
b = fnH(b, c, d, a, M[2], 0xc4ac5665, 23);
a = fnI(a, b, c, d, M[0], 0xf4292244, 6);
d = fnI(d, a, b, c, M[7], 0x432aff97, 10);
c = fnI(c, d, a, b, M[14], 0xab9423a7, 15);
b = fnI(b, c, d, a, M[5], 0xfc93a039, 21);
a = fnI(a, b, c, d, M[12], 0x655b59c3, 6);
d = fnI(d, a, b, c, M[3], 0x8f0ccc92, 10);
c = fnI(c, d, a, b, M[10], 0xffeff47d, 15);
b = fnI(b, c, d, a, M[1], 0x85845dd1, 21);
a = fnI(a, b, c, d, M[8], 0x6fa87e4f, 6);
d = fnI(d, a, b, c, M[15], 0xfe2ce6e0, 10);
c = fnI(c, d, a, b, M[6], 0xa3014314, 15);
b = fnI(b, c, d, a, M[13], 0x4e0811a1, 21);
a = fnI(a, b, c, d, M[4], 0xf7537e82, 6);
d = fnI(d, a, b, c, M[11], 0xbd3af235, 10);
c = fnI(c, d, a, b, M[2], 0x2ad7d2bb, 15);
b = fnI(b, c, d, a, M[9], 0xeb86d391, 21);
this._a = (this._a + a) | 0;
this._b = (this._b + b) | 0;
this._c = (this._c + c) | 0;
this._d = (this._d + d) | 0;
};
MD5.prototype._digest = function () {
// create padding and handle blocks
this._block[this._blockOffset++] = 0x80;
if (this._blockOffset > 56) {
this._block.fill(0, this._blockOffset, 64);
this._update();
this._blockOffset = 0;
}
this._block.fill(0, this._blockOffset, 56);
this._block.writeUInt32LE(this._length[0], 56);
this._block.writeUInt32LE(this._length[1], 60);
this._update();
// produce result
var buffer = Buffer$2.allocUnsafe(16);
buffer.writeInt32LE(this._a, 0);
buffer.writeInt32LE(this._b, 4);
buffer.writeInt32LE(this._c, 8);
buffer.writeInt32LE(this._d, 12);
return buffer
};
function rotl (x, n) {
return (x << n) | (x >>> (32 - n))
}
function fnF (a, b, c, d, m, k, s) {
return (rotl((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + b) | 0
}
function fnG (a, b, c, d, m, k, s) {
return (rotl((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + b) | 0
}
function fnH (a, b, c, d, m, k, s) {
return (rotl((a + (b ^ c ^ d) + m + k) | 0, s) + b) | 0
}
function fnI (a, b, c, d, m, k, s) {
return (rotl((a + ((c ^ (b | (~d)))) + m + k) | 0, s) + b) | 0
}
var md5_js = MD5;
var Buffer$3 = buffer.Buffer;
var ARRAY16$1 = new Array(16);
var zl = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
];
var zr = [
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
];
var sl = [
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
];
var sr = [
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
];
var hl = [0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e];
var hr = [0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000];
function RIPEMD160 () {
hashBase.call(this, 64);
// state
this._a = 0x67452301;
this._b = 0xefcdab89;
this._c = 0x98badcfe;
this._d = 0x10325476;
this._e = 0xc3d2e1f0;
}
inherits_browser(RIPEMD160, hashBase);
RIPEMD160.prototype._update = function () {
var words = ARRAY16$1;
for (var j = 0; j < 16; ++j) words[j] = this._block.readInt32LE(j * 4);
var al = this._a | 0;
var bl = this._b | 0;
var cl = this._c | 0;
var dl = this._d | 0;
var el = this._e | 0;
var ar = this._a | 0;
var br = this._b | 0;
var cr = this._c | 0;
var dr = this._d | 0;
var er = this._e | 0;
// computation
for (var i = 0; i < 80; i += 1) {
var tl;
var tr;
if (i < 16) {
tl = fn1(al, bl, cl, dl, el, words[zl[i]], hl[0], sl[i]);
tr = fn5(ar, br, cr, dr, er, words[zr[i]], hr[0], sr[i]);
} else if (i < 32) {
tl = fn2(al, bl, cl, dl, el, words[zl[i]], hl[1], sl[i]);
tr = fn4(ar, br, cr, dr, er, words[zr[i]], hr[1], sr[i]);
} else if (i < 48) {
tl = fn3(al, bl, cl, dl, el, words[zl[i]], hl[2], sl[i]);
tr = fn3(ar, br, cr, dr, er, words[zr[i]], hr[2], sr[i]);
} else if (i < 64) {
tl = fn4(al, bl, cl, dl, el, words[zl[i]], hl[3], sl[i]);
tr = fn2(ar, br, cr, dr, er, words[zr[i]], hr[3], sr[i]);
} else { // if (i<80) {
tl = fn5(al, bl, cl, dl, el, words[zl[i]], hl[4], sl[i]);
tr = fn1(ar, br, cr, dr, er, words[zr[i]], hr[4], sr[i]);
}
al = el;
el = dl;
dl = rotl$1(cl, 10);
cl = bl;
bl = tl;
ar = er;
er = dr;
dr = rotl$1(cr, 10);
cr = br;
br = tr;
}
// update state
var t = (this._b + cl + dr) | 0;
this._b = (this._c + dl + er) | 0;
this._c = (this._d + el + ar) | 0;
this._d = (this._e + al + br) | 0;
this._e = (this._a + bl + cr) | 0;
this._a = t;
};
RIPEMD160.prototype._digest = function () {
// create padding and handle blocks
this._block[this._blockOffset++] = 0x80;
if (this._blockOffset > 56) {
this._block.fill(0, this._blockOffset, 64);
this._update();
this._blockOffset = 0;
}
this._block.fill(0, this._blockOffset, 56);
this._block.writeUInt32LE(this._length[0], 56);
this._block.writeUInt32LE(this._length[1], 60);
this._update();
// produce result
var buffer = Buffer$3.alloc ? Buffer$3.alloc(20) : new Buffer$3(20);
buffer.writeInt32LE(this._a, 0);
buffer.writeInt32LE(this._b, 4);
buffer.writeInt32LE(this._c, 8);
buffer.writeInt32LE(this._d, 12);
buffer.writeInt32LE(this._e, 16);
return buffer
};
function rotl$1 (x, n) {
return (x << n) | (x >>> (32 - n))
}
function fn1 (a, b, c, d, e, m, k, s) {
return (rotl$1((a + (b ^ c ^ d) + m + k) | 0, s) + e) | 0
}
function fn2 (a, b, c, d, e, m, k, s) {
return (rotl$1((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + e) | 0
}
function fn3 (a, b, c, d, e, m, k, s) {
return (rotl$1((a + ((b | (~c)) ^ d) + m + k) | 0, s) + e) | 0
}
function fn4 (a, b, c, d, e, m, k, s) {
return (rotl$1((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + e) | 0
}
function fn5 (a, b, c, d, e, m, k, s) {
return (rotl$1((a + (b ^ (c | (~d))) + m + k) | 0, s) + e) | 0
}
var ripemd160 = RIPEMD160;
var Buffer$4 = safeBuffer.Buffer;
// prototype class for hash functions
function Hash (blockSize, finalSize) {
this._block = Buffer$4.alloc(blockSize);
this._finalSize = finalSize;
this._blockSize = blockSize;
this._len = 0;
}
Hash.prototype.update = function (data, enc) {
if (typeof data === 'string') {
enc = enc || 'utf8';
data = Buffer$4.from(data, enc);
}
var block = this._block;
var blockSize = this._blockSize;
var length = data.length;
var accum = this._len;
for (var offset = 0; offset < length;) {
var assigned = accum % blockSize;
var remainder = Math.min(length - offset, blockSize - assigned);
for (var i = 0; i < remainder; i++) {
block[assigned + i] = data[offset + i];
}
accum += remainder;
offset += remainder;
if ((accum % blockSize) === 0) {
this._update(block);
}
}
this._len += length;
return this
};
Hash.prototype.digest = function (enc) {
var rem = this._len % this._blockSize;
this._block[rem] = 0x80;
// zero (rem + 1) trailing bits, where (rem + 1) is the smallest
// non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
this._block.fill(0, rem + 1);
if (rem >= this._finalSize) {
this._update(this._block);
this._block.fill(0);
}
var bits = this._len * 8;
// uint32
if (bits <= 0xffffffff) {
this._block.writeUInt32BE(bits, this._blockSize - 4);
// uint64
} else {
var lowBits = (bits & 0xffffffff) >>> 0;
var highBits = (bits - lowBits) / 0x100000000;
this._block.writeUInt32BE(highBits, this._blockSize - 8);
this._block.writeUInt32BE(lowBits, this._blockSize - 4);
}
this._update(this._block);
var hash = this._hash();
return enc ? hash.toString(enc) : hash
};
Hash.prototype._update = function () {
throw new Error('_update must be implemented by subclass')
};
var hash = Hash;
/*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-0, as defined
* in FIPS PUB 180-1
* This source code is derived from sha1.js of the same repository.
* The difference between SHA-0 and SHA-1 is just a bitwise rotate left
* operation was added.
*/
var Buffer$5 = safeBuffer.Buffer;
var K = [
0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
];
var W = new Array(80);
function Sha () {
this.init();
this._w = W;
hash.call(this, 64, 56);
}
inherits_browser(Sha, hash);
Sha.prototype.init = function () {
this._a = 0x67452301;
this._b = 0xefcdab89;
this._c = 0x98badcfe;
this._d = 0x10325476;
this._e = 0xc3d2e1f0;
return this
};
function rotl5 (num) {
return (num << 5) | (num >>> 27)
}
function rotl30 (num) {
return (num << 30) | (num >>> 2)
}
function ft (s, b, c, d) {
if (s === 0) return (b & c) | ((~b) & d)
if (s === 2) return (b & c) | (b & d) | (c & d)
return b ^ c ^ d
}
Sha.prototype._update = function (M) {
var W = this._w;
var a = this._a | 0;
var b = this._b | 0;
var c = this._c | 0;
var d = this._d | 0;
var e = this._e | 0;
for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4);
for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
for (var j = 0; j < 80; ++j) {
var s = ~~(j / 20);
var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0;
e = d;
d = c;
c = rotl30(b);
b = a;
a = t;
}
this._a = (a + this._a) | 0;
this._b = (b + this._b) | 0;
this._c = (c + this._c) | 0;
this._d = (d + this._d) | 0;
this._e = (e + this._e) | 0;
};
Sha.prototype._hash = function () {
var H = Buffer$5.allocUnsafe(20);
H.writeInt32BE(this._a | 0, 0);
H.writeInt32BE(this._b | 0, 4);
H.writeInt32BE(this._c | 0, 8);
H.writeInt32BE(this._d | 0, 12);
H.writeInt32BE(this._e | 0, 16);
return H
};
var sha = Sha;
/*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* in FIPS PUB 180-1
* Version 2.1a Copyright Paul Johnston 2000 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details.
*/
var Buffer$6 = safeBuffer.Buffer;
var K$1 = [
0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
];
var W$1 = new Array(80);
function Sha1 () {
this.init();
this._w = W$1;
hash.call(this, 64, 56);
}
inherits_browser(Sha1, hash);
Sha1.prototype.init = function () {
this._a = 0x67452301;
this._b = 0xefcdab89;
this._c = 0x98badcfe;
this._d = 0x10325476;
this._e = 0xc3d2e1f0;
return this
};
function rotl1 (num) {
return (num << 1) | (num >>> 31)
}
function rotl5$1 (num) {
return (num << 5) | (num >>> 27)
}
function rotl30$1 (num) {
return (num << 30) | (num >>> 2)
}
function ft$1 (s, b, c, d) {
if (s === 0) return (b & c) | ((~b) & d)
if (s === 2) return (b & c) | (b & d) | (c & d)
return b ^ c ^ d
}
Sha1.prototype._update = function (M) {
var W = this._w;
var a = this._a | 0;
var b = this._b | 0;
var c = this._c | 0;
var d = this._d | 0;
var e = this._e | 0;
for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4);
for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]);
for (var j = 0; j < 80; ++j) {
var s = ~~(j / 20);
var t = (rotl5$1(a) + ft$1(s, b, c, d) + e + W[j] + K$1[s]) | 0;
e = d;
d = c;
c = rotl30$1(b);
b = a;
a = t;
}
this._a = (a + this._a) | 0;
this._b = (b + this._b) | 0;
this._c = (c + this._c) | 0;
this._d = (d + this._d) | 0;
this._e = (e + this._e) | 0;
};
Sha1.prototype._hash = function () {
var H = Buffer$6.allocUnsafe(20);
H.writeInt32BE(this._a | 0, 0);
H.writeInt32BE(this._b | 0, 4);
H.writeInt32BE(this._c | 0, 8);
H.writeInt32BE(this._d | 0, 12);
H.writeInt32BE(this._e | 0, 16);
return H
};
var sha1 = Sha1;
/**
* A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
* in FIPS 180-2
* Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
*
*/
var Buffer$7 = safeBuffer.Buffer;
var K$2 = [
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
];
var W$2 = new Array(64);
function Sha256 () {
this.init();
this._w = W$2; // new Array(64)
hash.call(this, 64, 56);
}
inherits_browser(Sha256, hash);
Sha256.prototype.init = function () {
this._a = 0x6a09e667;
this._b = 0xbb67ae85;
this._c = 0x3c6ef372;
this._d = 0xa54ff53a;
this._e = 0x510e527f;
this._f = 0x9b05688c;
this._g = 0x1f83d9ab;
this._h = 0x5be0cd19;
return this
};
function ch (x, y, z) {
return z ^ (x & (y ^ z))
}
function maj (x, y, z) {
return (x & y) | (z & (x | y))
}
function sigma0 (x) {
return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10)
}
function sigma1 (x) {
return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7)
}
function gamma0 (x) {
return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3)
}
function gamma1 (x) {
return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10)
}
Sha256.prototype._update = function (M) {
var W = this._w;
var a = this._a | 0;
var b = this._b | 0;
var c = this._c | 0;
var d = this._d | 0;
var e = this._e | 0;
var f = this._f | 0;
var g = this._g | 0;
var h = this._h | 0;
for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4);
for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0;
for (var j = 0; j < 64; ++j) {
var T1 = (h + sigma1(e) + ch(e, f, g) + K$2[j] + W[j]) | 0;
var T2 = (sigma0(a) + maj(a, b, c)) | 0;
h = g;
g = f;
f = e;
e = (d + T1) | 0;
d = c;
c = b;
b = a;
a = (T1 + T2) | 0;
}
this._a = (a + this._a) | 0;
this._b = (b + this._b) | 0;
this._c = (c + this._c) | 0;
this._d = (d + this._d) | 0;
this._e = (e + this._e) | 0;
this._f = (f + this._f) | 0;
this._g = (g + this._g) | 0;
this._h = (h + this._h) | 0;
};
Sha256.prototype._hash = function () {
var H = Buffer$7.allocUnsafe(32);
H.writeInt32BE(this._a, 0);
H.writeInt32BE(this._b, 4);
H.writeInt32BE(this._c, 8);
H.writeInt32BE(this._d, 12);
H.writeInt32BE(this._e, 16);
H.writeInt32BE(this._f, 20);
H.writeInt32BE(this._g, 24);
H.writeInt32BE(this._h, 28);
return H
};
var sha256 = Sha256;
/**
* A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
* in FIPS 180-2
* Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
*
*/
var Buffer$8 = safeBuffer.Buffer;
var W$3 = new Array(64);
function Sha224 () {
this.init();
this._w = W$3; // new Array(64)
hash.call(this, 64, 56);
}
inherits_browser(Sha224, sha256);
Sha224.prototype.init = function () {
this._a = 0xc1059ed8;
this._b = 0x367cd507;
this._c = 0x3070dd17;
this._d = 0xf70e5939;
this._e = 0xffc00b31;
this._f = 0x68581511;
this._g = 0x64f98fa7;
this._h = 0xbefa4fa4;
return this
};
Sha224.prototype._hash = function () {
var H = Buffer$8.allocUnsafe(28);
H.writeInt32BE(this._a, 0);
H.writeInt32BE(this._b, 4);
H.writeInt32BE(this._c, 8);
H.writeInt32BE(this._d, 12);
H.writeInt32BE(this._e, 16);
H.writeInt32BE(this._f, 20);
H.writeInt32BE(this._g, 24);
return H
};
var sha224 = Sha224;
var Buffer$9 = safeBuffer.Buffer;
var K$3 = [
0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
];
var W$4 = new Array(160);
function Sha512 () {
this.init();
this._w = W$4;
hash.call(this, 128, 112);
}
inherits_browser(Sha512, hash);
Sha512.prototype.init = function () {
this._ah = 0x6a09e667;
this._bh = 0xbb67ae85;
this._ch = 0x3c6ef372;
this._dh = 0xa54ff53a;
this._eh = 0x510e527f;
this._fh = 0x9b05688c;
this._gh = 0x1f83d9ab;
this._hh = 0x5be0cd19;
this._al = 0xf3bcc908;
this._bl = 0x84caa73b;
this._cl = 0xfe94f82b;
this._dl = 0x5f1d36f1;
this._el = 0xade682d1;
this._fl = 0x2b3e6c1f;
this._gl = 0xfb41bd6b;
this._hl = 0x137e2179;
return this
};
function Ch (x, y, z) {
return z ^ (x & (y ^ z))
}
function maj$1 (x, y, z) {
return (x & y) | (z & (x | y))
}
function sigma0$1 (x, xl) {
return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25)
}
function sigma1$1 (x, xl) {
return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23)
}
function Gamma0 (x, xl) {
return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7)
}
function Gamma0l (x, xl) {
return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25)
}
function Gamma1 (x, xl) {
return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6)
}
function Gamma1l (x, xl) {
return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26)
}
function getCarry (a, b) {
return (a >>> 0) < (b >>> 0) ? 1 : 0
}
Sha512.prototype._update = function (M) {
var W = this._w;
var ah = this._ah | 0;
var bh = this._bh | 0;
var ch = this._ch | 0;
var dh = this._dh | 0;
var eh = this._eh | 0;
var fh = this._fh | 0;
var gh = this._gh | 0;
var hh = this._hh | 0;
var al = this._al | 0;
var bl = this._bl | 0;
var cl = this._cl | 0;
var dl = this._dl | 0;
var el = this._el | 0;
var fl = this._fl | 0;
var gl = this._gl | 0;
var hl = this._hl | 0;
for (var i = 0; i < 32; i += 2) {
W[i] = M.readInt32BE(i * 4);
W[i + 1] = M.readInt32BE(i * 4 + 4);
}
for (; i < 160; i += 2) {
var xh = W[i - 15 * 2];
var xl = W[i - 15 * 2 + 1];
var gamma0 = Gamma0(xh, xl);
var gamma0l = Gamma0l(xl, xh);
xh = W[i - 2 * 2];
xl = W[i - 2 * 2 + 1];
var gamma1 = Gamma1(xh, xl);
var gamma1l = Gamma1l(xl, xh);
// W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
var Wi7h = W[i - 7 * 2];
var Wi7l = W[i - 7 * 2 + 1];
var Wi16h = W[i - 16 * 2];
var Wi16l = W[i - 16 * 2 + 1];
var Wil = (gamma0l + Wi7l) | 0;
var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0;
Wil = (Wil + gamma1l) | 0;
Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0;
Wil = (Wil + Wi16l) | 0;
Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0;
W[i] = Wih;
W[i + 1] = Wil;
}
for (var j = 0; j < 160; j += 2) {
Wih = W[j];
Wil = W[j + 1];
var majh = maj$1(ah, bh, ch);
var majl = maj$1(al, bl, cl);
var sigma0h = sigma0$1(ah, al);
var sigma0l = sigma0$1(al, ah);
var sigma1h = sigma1$1(eh, el);
var sigma1l = sigma1$1(el, eh);
// t1 = h + sigma1 + ch + K[j] + W[j]
var Kih = K$3[j];
var Kil = K$3[j + 1];
var chh = Ch(eh, fh, gh);
var chl = Ch(el, fl, gl);
var t1l = (hl + sigma1l) | 0;
var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0;
t1l = (t1l + chl) | 0;
t1h = (t1h + chh + getCarry(t1l, chl)) | 0;
t1l = (t1l + Kil) | 0;
t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0;
t1l = (t1l + Wil) | 0;
t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0;
// t2 = sigma0 + maj
var t2l = (sigma0l + majl) | 0;
var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0;
hh = gh;
hl = gl;
gh = fh;
gl = fl;
fh = eh;
fl = el;
el = (dl + t1l) | 0;
eh = (dh + t1h + getCarry(el, dl)) | 0;
dh = ch;
dl = cl;
ch = bh;
cl = bl;
bh = ah;
bl = al;
al = (t1l + t2l) | 0;
ah = (t1h + t2h + getCarry(al, t1l)) | 0;
}
this._al = (this._al + al) | 0;
this._bl = (this._bl + bl) | 0;
this._cl = (this._cl + cl) | 0;
this._dl = (this._dl + dl) | 0;
this._el = (this._el + el) | 0;
this._fl = (this._fl + fl) | 0;
this._gl = (this._gl + gl) | 0;
this._hl = (this._hl + hl) | 0;
this._ah = (this._ah + ah + getCarry(this._al, al)) | 0;
this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0;
this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0;
this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0;
this._eh = (this._eh + eh + getCarry(this._el, el)) | 0;
this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0;
this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0;
this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0;
};
Sha512.prototype._hash = function () {
var H = Buffer$9.allocUnsafe(64);
function writeInt64BE (h, l, offset) {
H.writeInt32BE(h, offset);
H.writeInt32BE(l, offset + 4);
}
writeInt64BE(this._ah, this._al, 0);
writeInt64BE(this._bh, this._bl, 8);
writeInt64BE(this._ch, this._cl, 16);
writeInt64BE(this._dh, this._dl, 24);
writeInt64BE(this._eh, this._el, 32);
writeInt64BE(this._fh, this._fl, 40);
writeInt64BE(this._gh, this._gl, 48);
writeInt64BE(this._hh, this._hl, 56);
return H
};
var sha512 = Sha512;
var Buffer$a = safeBuffer.Buffer;
var W$5 = new Array(160);
function Sha384 () {
this.init();
this._w = W$5;
hash.call(this, 128, 112);
}
inherits_browser(Sha384, sha512);
Sha384.prototype.init = function () {
this._ah = 0xcbbb9d5d;
this._bh = 0x629a292a;
this._ch = 0x9159015a;
this._dh = 0x152fecd8;
this._eh = 0x67332667;
this._fh = 0x8eb44a87;
this._gh = 0xdb0c2e0d;
this._hh = 0x47b5481d;
this._al = 0xc1059ed8;
this._bl = 0x367cd507;
this._cl = 0x3070dd17;
this._dl = 0xf70e5939;
this._el = 0xffc00b31;
this._fl = 0x68581511;
this._gl = 0x64f98fa7;
this._hl = 0xbefa4fa4;
return this
};
Sha384.prototype._hash = function () {
var H = Buffer$a.allocUnsafe(48);
function writeInt64BE (h, l, offset) {
H.writeInt32BE(h, offset);
H.writeInt32BE(l, offset + 4);
}
writeInt64BE(this._ah, this._al, 0);
writeInt64BE(this._bh, this._bl, 8);
writeInt64BE(this._ch, this._cl, 16);
writeInt64BE(this._dh, this._dl, 24);
writeInt64BE(this._eh, this._el, 32);
writeInt64BE(this._fh, this._fl, 40);
return H
};
var sha384 = Sha384;
var sha_js = createCommonjsModule(function (module) {
var exports = module.exports = function SHA (algorithm) {
algorithm = algorithm.toLowerCase();
var Algorithm = exports[algorithm];
if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)')
return new Algorithm()
};
exports.sha = sha;
exports.sha1 = sha1;
exports.sha224 = sha224;
exports.sha256 = sha256;
exports.sha384 = sha384;
exports.sha512 = sha512;
});
var Buffer$b = safeBuffer.Buffer;
var Transform$1 = stream.Transform;
var StringDecoder = string_decoder$1.StringDecoder;
function CipherBase (hashMode) {
Transform$1.call(this);
this.hashMode = typeof hashMode === 'string';
if (this.hashMode) {
this[hashMode] = this._finalOrDigest;
} else {
this.final = this._finalOrDigest;
}
if (this._final) {
this.__final = this._final;
this._final = null;
}
this._decoder = null;
this._encoding = null;
}
inherits_browser(CipherBase, Transform$1);
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
if (typeof data === 'string') {
data = Buffer$b.from(data, inputEnc);
}
var outData = this._update(data);
if (this.hashMode) return this
if (outputEnc) {
outData = this._toString(outData, outputEnc);
}
return outData
};
CipherBase.prototype.setAutoPadding = function () {};
CipherBase.prototype.getAuthTag = function () {
throw new Error('trying to get auth tag in unsupported state')
};
CipherBase.prototype.setAuthTag = function () {
throw new Error('trying to set auth tag in unsupported state')
};
CipherBase.prototype.setAAD = function () {
throw new Error('trying to set aad in unsupported state')
};
CipherBase.prototype._transform = function (data, _, next) {
var err;
try {
if (this.hashMode) {
this._update(data);
} else {
this.push(this._update(data));
}
} catch (e) {
err = e;
} finally {
next(err);
}
};
CipherBase.prototype._flush = function (done) {
var err;
try {
this.push(this.__final());
} catch (e) {
err = e;
}
done(err);
};
CipherBase.prototype._finalOrDigest = function (outputEnc) {
var outData = this.__final() || Buffer$b.alloc(0);
if (outputEnc) {
outData = this._toString(outData, outputEnc, true);
}
return outData
};
CipherBase.prototype._toString = function (value, enc, fin) {
if (!this._decoder) {
this._decoder = new StringDecoder(enc);
this._encoding = enc;
}
if (this._encoding !== enc) throw new Error('can\'t switch encodings')
var out = this._decoder.write(value);
if (fin) {
out += this._decoder.end();
}
return out
};
var cipherBase = CipherBase;
function Hash$1 (hash) {
cipherBase.call(this, 'digest');
this._hash = hash;
}
inherits_browser(Hash$1, cipherBase);
Hash$1.prototype._update = function (data) {
this._hash.update(data);
};
Hash$1.prototype._final = function () {
return this._hash.digest()
};
var browser = function createHash (alg) {
alg = alg.toLowerCase();
if (alg === 'md5') return new md5_js()
if (alg === 'rmd160' || alg === 'ripemd160') return new ripemd160()
return new Hash$1(sha_js(alg))
};
var Buffer$c = safeBuffer.Buffer;
var ZEROS = Buffer$c.alloc(128);
var blocksize = 64;
function Hmac (alg, key) {
cipherBase.call(this, 'digest');
if (typeof key === 'string') {
key = Buffer$c.from(key);
}
this._alg = alg;
this._key = key;
if (key.length > blocksize) {
key = alg(key);
} else if (key.length < blocksize) {
key = Buffer$c.concat([key, ZEROS], blocksize);
}
var ipad = this._ipad = Buffer$c.allocUnsafe(blocksize);
var opad = this._opad = Buffer$c.allocUnsafe(blocksize);
for (var i = 0; i < blocksize; i++) {
ipad[i] = key[i] ^ 0x36;
opad[i] = key[i] ^ 0x5C;
}
this._hash = [ipad];
}
inherits_browser(Hmac, cipherBase);
Hmac.prototype._update = function (data) {
this._hash.push(data);
};
Hmac.prototype._final = function () {
var h = this._alg(Buffer$c.concat(this._hash));
return this._alg(Buffer$c.concat([this._opad, h]))
};
var legacy = Hmac;
var md5 = function (buffer) {
return new md5_js().update(buffer).digest()
};
var Buffer$d = safeBuffer.Buffer;
var ZEROS$1 = Buffer$d.alloc(128);
function Hmac$1 (alg, key) {
cipherBase.call(this, 'digest');
if (typeof key === 'string') {
key = Buffer$d.from(key);
}
var blocksize = (alg === 'sha512' || alg === 'sha384') ? 128 : 64;
this._alg = alg;
this._key = key;
if (key.length > blocksize) {
var hash = alg === 'rmd160' ? new ripemd160() : sha_js(alg);
key = hash.update(key).digest();
} else if (key.length < blocksize) {
key = Buffer$d.concat([key, ZEROS$1], blocksize);
}
var ipad = this._ipad = Buffer$d.allocUnsafe(blocksize);
var opad = this._opad = Buffer$d.allocUnsafe(blocksize);
for (var i = 0; i < blocksize; i++) {
ipad[i] = key[i] ^ 0x36;
opad[i] = key[i] ^ 0x5C;
}
this._hash = alg === 'rmd160' ? new ripemd160() : sha_js(alg);
this._hash.update(ipad);
}
inherits_browser(Hmac$1, cipherBase);
Hmac$1.prototype._update = function (data) {
this._hash.update(data);
};
Hmac$1.prototype._final = function () {
var h = this._hash.digest();
var hash = this._alg === 'rmd160' ? new ripemd160() : sha_js(this._alg);
return hash.update(this._opad).update(h).digest()
};
var browser$1 = function createHmac (alg, key) {
alg = alg.toLowerCase();
if (alg === 'rmd160' || alg === 'ripemd160') {
return new Hmac$1('rmd160', key)
}
if (alg === 'md5') {
return new legacy(md5, key)
}
return new Hmac$1(alg, key)
};
var crypto$1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
function hash160(buffer) {
const sha256Hash = browser('sha256')
.update(buffer)
.digest();
try {
return browser('rmd160')
.update(sha256Hash)
.digest();
}
catch (err) {
return browser('ripemd160')
.update(sha256Hash)
.digest();
}
}
exports.hash160 = hash160;
function hmacSHA512(key, data) {
return browser$1('sha512', key)
.update(data)
.digest();
}
exports.hmacSHA512 = hmacSHA512;
});
unwrapExports(crypto$1);
var crypto_1 = crypto$1.hash160;
var crypto_2 = crypto$1.hmacSHA512;
// base-x encoding / decoding
// Copyright (c) 2018 base-x contributors
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
// @ts-ignore
var _Buffer = safeBuffer.Buffer;
function base (ALPHABET) {
if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
var BASE_MAP = new Uint8Array(256);
for (var j = 0; j < BASE_MAP.length; j++) {
BASE_MAP[j] = 255;
}
for (var i = 0; i < ALPHABET.length; i++) {
var x = ALPHABET.charAt(i);
var xc = x.charCodeAt(0);
if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
BASE_MAP[xc] = i;
}
var BASE = ALPHABET.length;
var LEADER = ALPHABET.charAt(0);
var FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
var iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
function encode (source) {
if (Array.isArray(source) || source instanceof Uint8Array) { source = _Buffer.from(source); }
if (!_Buffer.isBuffer(source)) { throw new TypeError('Expected Buffer') }
if (source.length === 0) { return '' }
// Skip & count leading zeroes.
var zeroes = 0;
var length = 0;
var pbegin = 0;
var pend = source.length;
while (pbegin !== pend && source[pbegin] === 0) {
pbegin++;
zeroes++;
}
// Allocate enough space in big-endian base58 representation.
var size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
var b58 = new Uint8Array(size);
// Process the bytes.
while (pbegin !== pend) {
var carry = source[pbegin];
// Apply "b58 = b58 * 256 + ch".
var i = 0;
for (var it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
carry += (256 * b58[it1]) >>> 0;
b58[it1] = (carry % BASE) >>> 0;
carry = (carry / BASE) >>> 0;
}
if (carry !== 0) { throw new Error('Non-zero carry') }
length = i;
pbegin++;
}
// Skip leading zeroes in base58 result.
var it2 = size - length;
while (it2 !== size && b58[it2] === 0) {
it2++;
}
// Translate the result into a string.
var str = LEADER.repeat(zeroes);
for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]); }
return str
}
function decodeUnsafe (source) {
if (typeof source !== 'string') { throw new TypeError('Expected String') }
if (source.length === 0) { return _Buffer.alloc(0) }
var psz = 0;
// Skip leading spaces.
if (source[psz] === ' ') { return }
// Skip and count leading '1's.
var zeroes = 0;
var length = 0;
while (source[psz] === LEADER) {
zeroes++;
psz++;
}
// Allocate enough space in big-endian base256 representation.
var size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
var b256 = new Uint8Array(size);
// Process the characters.
while (source[psz]) {
// Decode character
var carry = BASE_MAP[source.charCodeAt(psz)];
// Invalid character
if (carry === 255) { return }
var i = 0;
for (var it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
carry += (BASE * b256[it3]) >>> 0;
b256[it3] = (carry % 256) >>> 0;
carry = (carry / 256) >>> 0;
}
if (carry !== 0) { throw new Error('Non-zero carry') }
length = i;
psz++;
}
// Skip trailing spaces.
if (source[psz] === ' ') { return }
// Skip leading zeroes in b256.
var it4 = size - length;
while (it4 !== size && b256[it4] === 0) {
it4++;
}
var vch = _Buffer.allocUnsafe(zeroes + (size - it4));
vch.fill(0x00, 0, zeroes);
var j = zeroes;
while (it4 !== size) {
vch[j++] = b256[it4++];
}
return vch
}
function decode (string) {
var buffer = decodeUnsafe(string);
if (buffer) { return buffer }
throw new Error('Non-base' + BASE + ' character')
}
return {
encode: encode,
decodeUnsafe: decodeUnsafe,
decode: decode
}
}
var src = base;
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
var bs58 = src(ALPHABET);
var Buffer$e = safeBuffer.Buffer;
var base$1 = function (checksumFn) {
// Encode a buffer as a base58-check encoded string
function encode (payload) {
var checksum = checksumFn(payload);
return bs58.encode(Buffer$e.concat([
payload,
checksum
], payload.length + 4))
}
function decodeRaw (buffer) {
var payload = buffer.slice(0, -4);
var checksum = buffer.slice(-4);
var newChecksum = checksumFn(payload);
if (checksum[0] ^ newChecksum[0] |
checksum[1] ^ newChecksum[1] |
checksum[2] ^ newChecksum[2] |
checksum[3] ^ newChecksum[3]) return
return payload
}
// Decode a base58-check encoded string to a buffer, no result if checksum is wrong
function decodeUnsafe (string) {
var buffer = bs58.decodeUnsafe(string);
if (!buffer) return
return decodeRaw(buffer)
}
function decode (string) {
var buffer = bs58.decode(string);
var payload = decodeRaw(buffer);
if (!payload) throw new Error('Invalid checksum')
return payload
}
return {
encode: encode,
decode: decode,
decodeUnsafe: decodeUnsafe
}
};
// SHA256(SHA256(buffer))
function sha256x2 (buffer) {
var tmp = browser('sha256').update(buffer).digest();
return browser('sha256').update(tmp).digest()
}
var bs58check = base$1(sha256x2);
var bn = createCommonjsModule(function (module) {
(function (module, exports) {
// Utils
function assert (val, msg) {
if (!val) throw new Error(msg || 'Assertion failed');
}
// Could use `inherits` module, but don't want to move from single file
// architecture yet.
function inherits (ctor, superCtor) {
ctor.super_ = superCtor;
var TempCtor = function () {};
TempCtor.prototype = superCtor.prototype;
ctor.prototype = new TempCtor();
ctor.prototype.constructor = ctor;
}
// BN
function BN (number, base, endian) {
if (BN.isBN(number)) {
return number;
}
this.negative = 0;
this.words = null;
this.length = 0;
// Reduction context
this.red = null;
if (number !== null) {
if (base === 'le' || base === 'be') {
endian = base;
base = 10;
}
this._init(number || 0, base || 10, endian || 'be');
}
}
if (typeof module === 'object') {
module.exports = BN;
} else {
exports.BN = BN;
}
BN.BN = BN;
BN.wordSize = 26;
var Buffer;
try {
Buffer = buffer.Buffer;
} catch (e) {
}
BN.isBN = function isBN (num) {
if (num instanceof BN) {
return true;
}
return num !== null && typeof num === 'object' &&
num.constructor.wordSize === BN.wordSize && Array.isArray(num.words);
};
BN.max = function max (left, right) {
if (left.cmp(right) > 0) return left;
return right;
};
BN.min = function min (left, right) {
if (left.cmp(right) < 0) return left;
return right;
};
BN.prototype._init = function init (number, base, endian) {
if (typeof number === 'number') {
return this._initNumber(number, base, endian);
}
if (typeof number === 'object') {
return this._initArray(number, base, endian);
}
if (base === 'hex') {
base = 16;
}
assert(base === (base | 0) && base >= 2 && base <= 36);
number = number.toString().replace(/\s+/g, '');
var start = 0;
if (number[0] === '-') {
start++;
}
if (base === 16) {
this._parseHex(number, start);
} else {
this._parseBase(number, base, start);
}
if (number[0] === '-') {
this.negative = 1;
}
this.strip();
if (endian !== 'le') return;
this._initArray(this.toArray(), base, endian);
};
BN.prototype._initNumber = function _initNumber (number, base, endian) {
if (number < 0) {
this.negative = 1;
number = -number;
}
if (number < 0x4000000) {
this.words = [ number & 0x3ffffff ];
this.length = 1;
} else if (number < 0x10000000000000) {
this.words = [
number & 0x3ffffff,
(number / 0x4000000) & 0x3ffffff
];
this.length = 2;
} else {
assert(number < 0x20000000000000); // 2 ^ 53 (unsafe)
this.words = [
number & 0x3ffffff,
(number / 0x4000000) & 0x3ffffff,
1
];
this.length = 3;
}
if (endian !== 'le') return;
// Reverse the bytes
this._init