UNPKG

xclienttransaction

Version:

Generate Twitter/X Client-Transaction-ID header in JavaScript (Node & browser)

850 lines (837 loc) 33.9 kB
var XClientTransaction = (() => { var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // (disabled):crypto var require_crypto = __commonJS({ "(disabled):crypto"() { } }); // (disabled):buffer var require_buffer = __commonJS({ "(disabled):buffer"() { } }); // node_modules/js-sha256/src/sha256.js var require_sha256 = __commonJS({ "node_modules/js-sha256/src/sha256.js"(exports, module) { (function() { "use strict"; var ERROR = "input is invalid type"; var WINDOW = typeof window === "object"; var root = WINDOW ? window : {}; if (root.JS_SHA256_NO_WINDOW) { WINDOW = false; } var WEB_WORKER = !WINDOW && typeof self === "object"; var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === "object" && process.versions && process.versions.node && process.type != "renderer"; if (NODE_JS) { root = global; } else if (WEB_WORKER) { root = self; } var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === "object" && module.exports; var AMD = typeof define === "function" && define.amd; var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== "undefined"; var HEX_CHARS = "0123456789abcdef".split(""); var EXTRA = [-2147483648, 8388608, 32768, 128]; var SHIFT = [24, 16, 8, 0]; var K = [ 1116352408, 1899447441, 3049323471, 3921009573, 961987163, 1508970993, 2453635748, 2870763221, 3624381080, 310598401, 607225278, 1426881987, 1925078388, 2162078206, 2614888103, 3248222580, 3835390401, 4022224774, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, 2554220882, 2821834349, 2952996808, 3210313671, 3336571891, 3584528711, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, 2177026350, 2456956037, 2730485921, 2820302411, 3259730800, 3345764771, 3516065817, 3600352804, 4094571909, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, 2227730452, 2361852424, 2428436474, 2756734187, 3204031479, 3329325298 ]; var OUTPUT_TYPES = ["hex", "array", "digest", "arrayBuffer"]; var blocks = []; if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) { Array.isArray = function(obj) { return Object.prototype.toString.call(obj) === "[object Array]"; }; } if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) { ArrayBuffer.isView = function(obj) { return typeof obj === "object" && obj.buffer && obj.buffer.constructor === ArrayBuffer; }; } var createOutputMethod = function(outputType, is224) { return function(message) { return new Sha256(is224, true).update(message)[outputType](); }; }; var createMethod = function(is224) { var method = createOutputMethod("hex", is224); if (NODE_JS) { method = nodeWrap(method, is224); } method.create = function() { return new Sha256(is224); }; method.update = function(message) { return method.create().update(message); }; for (var i = 0; i < OUTPUT_TYPES.length; ++i) { var type = OUTPUT_TYPES[i]; method[type] = createOutputMethod(type, is224); } return method; }; var nodeWrap = function(method, is224) { var crypto = require_crypto(); var Buffer2 = require_buffer().Buffer; var algorithm = is224 ? "sha224" : "sha256"; var bufferFrom; if (Buffer2.from && !root.JS_SHA256_NO_BUFFER_FROM) { bufferFrom = Buffer2.from; } else { bufferFrom = function(message) { return new Buffer2(message); }; } var nodeMethod = function(message) { if (typeof message === "string") { return crypto.createHash(algorithm).update(message, "utf8").digest("hex"); } else { if (message === null || message === void 0) { throw new Error(ERROR); } else if (message.constructor === ArrayBuffer) { message = new Uint8Array(message); } } if (Array.isArray(message) || ArrayBuffer.isView(message) || message.constructor === Buffer2) { return crypto.createHash(algorithm).update(bufferFrom(message)).digest("hex"); } else { return method(message); } }; return nodeMethod; }; var createHmacOutputMethod = function(outputType, is224) { return function(key, message) { return new HmacSha256(key, is224, true).update(message)[outputType](); }; }; var createHmacMethod = function(is224) { var method = createHmacOutputMethod("hex", is224); method.create = function(key) { return new HmacSha256(key, is224); }; method.update = function(key, message) { return method.create(key).update(message); }; for (var i = 0; i < OUTPUT_TYPES.length; ++i) { var type = OUTPUT_TYPES[i]; method[type] = createHmacOutputMethod(type, is224); } return method; }; function Sha256(is224, sharedMemory) { if (sharedMemory) { blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; this.blocks = blocks; } else { this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; } if (is224) { this.h0 = 3238371032; this.h1 = 914150663; this.h2 = 812702999; this.h3 = 4144912697; this.h4 = 4290775857; this.h5 = 1750603025; this.h6 = 1694076839; this.h7 = 3204075428; } else { this.h0 = 1779033703; this.h1 = 3144134277; this.h2 = 1013904242; this.h3 = 2773480762; this.h4 = 1359893119; this.h5 = 2600822924; this.h6 = 528734635; this.h7 = 1541459225; } this.block = this.start = this.bytes = this.hBytes = 0; this.finalized = this.hashed = false; this.first = true; this.is224 = is224; } Sha256.prototype.update = function(message) { if (this.finalized) { return; } var notString, type = typeof message; if (type !== "string") { if (type === "object") { if (message === null) { throw new Error(ERROR); } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { message = new Uint8Array(message); } else if (!Array.isArray(message)) { if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) { throw new Error(ERROR); } } } else { throw new Error(ERROR); } notString = true; } var code, index = 0, i, length = message.length, blocks2 = this.blocks; while (index < length) { if (this.hashed) { this.hashed = false; blocks2[0] = this.block; this.block = blocks2[16] = blocks2[1] = blocks2[2] = blocks2[3] = blocks2[4] = blocks2[5] = blocks2[6] = blocks2[7] = blocks2[8] = blocks2[9] = blocks2[10] = blocks2[11] = blocks2[12] = blocks2[13] = blocks2[14] = blocks2[15] = 0; } if (notString) { for (i = this.start; index < length && i < 64; ++index) { blocks2[i >>> 2] |= message[index] << SHIFT[i++ & 3]; } } else { for (i = this.start; index < length && i < 64; ++index) { code = message.charCodeAt(index); if (code < 128) { blocks2[i >>> 2] |= code << SHIFT[i++ & 3]; } else if (code < 2048) { blocks2[i >>> 2] |= (192 | code >>> 6) << SHIFT[i++ & 3]; blocks2[i >>> 2] |= (128 | code & 63) << SHIFT[i++ & 3]; } else if (code < 55296 || code >= 57344) { blocks2[i >>> 2] |= (224 | code >>> 12) << SHIFT[i++ & 3]; blocks2[i >>> 2] |= (128 | code >>> 6 & 63) << SHIFT[i++ & 3]; blocks2[i >>> 2] |= (128 | code & 63) << SHIFT[i++ & 3]; } else { code = 65536 + ((code & 1023) << 10 | message.charCodeAt(++index) & 1023); blocks2[i >>> 2] |= (240 | code >>> 18) << SHIFT[i++ & 3]; blocks2[i >>> 2] |= (128 | code >>> 12 & 63) << SHIFT[i++ & 3]; blocks2[i >>> 2] |= (128 | code >>> 6 & 63) << SHIFT[i++ & 3]; blocks2[i >>> 2] |= (128 | code & 63) << SHIFT[i++ & 3]; } } } this.lastByteIndex = i; this.bytes += i - this.start; if (i >= 64) { this.block = blocks2[16]; this.start = i - 64; this.hash(); this.hashed = true; } else { this.start = i; } } if (this.bytes > 4294967295) { this.hBytes += this.bytes / 4294967296 << 0; this.bytes = this.bytes % 4294967296; } return this; }; Sha256.prototype.finalize = function() { if (this.finalized) { return; } this.finalized = true; var blocks2 = this.blocks, i = this.lastByteIndex; blocks2[16] = this.block; blocks2[i >>> 2] |= EXTRA[i & 3]; this.block = blocks2[16]; if (i >= 56) { if (!this.hashed) { this.hash(); } blocks2[0] = this.block; blocks2[16] = blocks2[1] = blocks2[2] = blocks2[3] = blocks2[4] = blocks2[5] = blocks2[6] = blocks2[7] = blocks2[8] = blocks2[9] = blocks2[10] = blocks2[11] = blocks2[12] = blocks2[13] = blocks2[14] = blocks2[15] = 0; } blocks2[14] = this.hBytes << 3 | this.bytes >>> 29; blocks2[15] = this.bytes << 3; this.hash(); }; Sha256.prototype.hash = function() { var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6, h = this.h7, blocks2 = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc; for (j = 16; j < 64; ++j) { t1 = blocks2[j - 15]; s0 = (t1 >>> 7 | t1 << 25) ^ (t1 >>> 18 | t1 << 14) ^ t1 >>> 3; t1 = blocks2[j - 2]; s1 = (t1 >>> 17 | t1 << 15) ^ (t1 >>> 19 | t1 << 13) ^ t1 >>> 10; blocks2[j] = blocks2[j - 16] + s0 + blocks2[j - 7] + s1 << 0; } bc = b & c; for (j = 0; j < 64; j += 4) { if (this.first) { if (this.is224) { ab = 300032; t1 = blocks2[0] - 1413257819; h = t1 - 150054599 << 0; d = t1 + 24177077 << 0; } else { ab = 704751109; t1 = blocks2[0] - 210244248; h = t1 - 1521486534 << 0; d = t1 + 143694565 << 0; } this.first = false; } else { s0 = (a >>> 2 | a << 30) ^ (a >>> 13 | a << 19) ^ (a >>> 22 | a << 10); s1 = (e >>> 6 | e << 26) ^ (e >>> 11 | e << 21) ^ (e >>> 25 | e << 7); ab = a & b; maj = ab ^ a & c ^ bc; ch = e & f ^ ~e & g; t1 = h + s1 + ch + K[j] + blocks2[j]; t2 = s0 + maj; h = d + t1 << 0; d = t1 + t2 << 0; } s0 = (d >>> 2 | d << 30) ^ (d >>> 13 | d << 19) ^ (d >>> 22 | d << 10); s1 = (h >>> 6 | h << 26) ^ (h >>> 11 | h << 21) ^ (h >>> 25 | h << 7); da = d & a; maj = da ^ d & b ^ ab; ch = h & e ^ ~h & f; t1 = g + s1 + ch + K[j + 1] + blocks2[j + 1]; t2 = s0 + maj; g = c + t1 << 0; c = t1 + t2 << 0; s0 = (c >>> 2 | c << 30) ^ (c >>> 13 | c << 19) ^ (c >>> 22 | c << 10); s1 = (g >>> 6 | g << 26) ^ (g >>> 11 | g << 21) ^ (g >>> 25 | g << 7); cd = c & d; maj = cd ^ c & a ^ da; ch = g & h ^ ~g & e; t1 = f + s1 + ch + K[j + 2] + blocks2[j + 2]; t2 = s0 + maj; f = b + t1 << 0; b = t1 + t2 << 0; s0 = (b >>> 2 | b << 30) ^ (b >>> 13 | b << 19) ^ (b >>> 22 | b << 10); s1 = (f >>> 6 | f << 26) ^ (f >>> 11 | f << 21) ^ (f >>> 25 | f << 7); bc = b & c; maj = bc ^ b & d ^ cd; ch = f & g ^ ~f & h; t1 = e + s1 + ch + K[j + 3] + blocks2[j + 3]; t2 = s0 + maj; e = a + t1 << 0; a = t1 + t2 << 0; this.chromeBugWorkAround = true; } this.h0 = this.h0 + a << 0; this.h1 = this.h1 + b << 0; this.h2 = this.h2 + c << 0; this.h3 = this.h3 + d << 0; this.h4 = this.h4 + e << 0; this.h5 = this.h5 + f << 0; this.h6 = this.h6 + g << 0; this.h7 = this.h7 + h << 0; }; Sha256.prototype.hex = function() { this.finalize(); var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5, h6 = this.h6, h7 = this.h7; var hex = HEX_CHARS[h0 >>> 28 & 15] + HEX_CHARS[h0 >>> 24 & 15] + HEX_CHARS[h0 >>> 20 & 15] + HEX_CHARS[h0 >>> 16 & 15] + HEX_CHARS[h0 >>> 12 & 15] + HEX_CHARS[h0 >>> 8 & 15] + HEX_CHARS[h0 >>> 4 & 15] + HEX_CHARS[h0 & 15] + HEX_CHARS[h1 >>> 28 & 15] + HEX_CHARS[h1 >>> 24 & 15] + HEX_CHARS[h1 >>> 20 & 15] + HEX_CHARS[h1 >>> 16 & 15] + HEX_CHARS[h1 >>> 12 & 15] + HEX_CHARS[h1 >>> 8 & 15] + HEX_CHARS[h1 >>> 4 & 15] + HEX_CHARS[h1 & 15] + HEX_CHARS[h2 >>> 28 & 15] + HEX_CHARS[h2 >>> 24 & 15] + HEX_CHARS[h2 >>> 20 & 15] + HEX_CHARS[h2 >>> 16 & 15] + HEX_CHARS[h2 >>> 12 & 15] + HEX_CHARS[h2 >>> 8 & 15] + HEX_CHARS[h2 >>> 4 & 15] + HEX_CHARS[h2 & 15] + HEX_CHARS[h3 >>> 28 & 15] + HEX_CHARS[h3 >>> 24 & 15] + HEX_CHARS[h3 >>> 20 & 15] + HEX_CHARS[h3 >>> 16 & 15] + HEX_CHARS[h3 >>> 12 & 15] + HEX_CHARS[h3 >>> 8 & 15] + HEX_CHARS[h3 >>> 4 & 15] + HEX_CHARS[h3 & 15] + HEX_CHARS[h4 >>> 28 & 15] + HEX_CHARS[h4 >>> 24 & 15] + HEX_CHARS[h4 >>> 20 & 15] + HEX_CHARS[h4 >>> 16 & 15] + HEX_CHARS[h4 >>> 12 & 15] + HEX_CHARS[h4 >>> 8 & 15] + HEX_CHARS[h4 >>> 4 & 15] + HEX_CHARS[h4 & 15] + HEX_CHARS[h5 >>> 28 & 15] + HEX_CHARS[h5 >>> 24 & 15] + HEX_CHARS[h5 >>> 20 & 15] + HEX_CHARS[h5 >>> 16 & 15] + HEX_CHARS[h5 >>> 12 & 15] + HEX_CHARS[h5 >>> 8 & 15] + HEX_CHARS[h5 >>> 4 & 15] + HEX_CHARS[h5 & 15] + HEX_CHARS[h6 >>> 28 & 15] + HEX_CHARS[h6 >>> 24 & 15] + HEX_CHARS[h6 >>> 20 & 15] + HEX_CHARS[h6 >>> 16 & 15] + HEX_CHARS[h6 >>> 12 & 15] + HEX_CHARS[h6 >>> 8 & 15] + HEX_CHARS[h6 >>> 4 & 15] + HEX_CHARS[h6 & 15]; if (!this.is224) { hex += HEX_CHARS[h7 >>> 28 & 15] + HEX_CHARS[h7 >>> 24 & 15] + HEX_CHARS[h7 >>> 20 & 15] + HEX_CHARS[h7 >>> 16 & 15] + HEX_CHARS[h7 >>> 12 & 15] + HEX_CHARS[h7 >>> 8 & 15] + HEX_CHARS[h7 >>> 4 & 15] + HEX_CHARS[h7 & 15]; } return hex; }; Sha256.prototype.toString = Sha256.prototype.hex; Sha256.prototype.digest = function() { this.finalize(); var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5, h6 = this.h6, h7 = this.h7; var arr = [ h0 >>> 24 & 255, h0 >>> 16 & 255, h0 >>> 8 & 255, h0 & 255, h1 >>> 24 & 255, h1 >>> 16 & 255, h1 >>> 8 & 255, h1 & 255, h2 >>> 24 & 255, h2 >>> 16 & 255, h2 >>> 8 & 255, h2 & 255, h3 >>> 24 & 255, h3 >>> 16 & 255, h3 >>> 8 & 255, h3 & 255, h4 >>> 24 & 255, h4 >>> 16 & 255, h4 >>> 8 & 255, h4 & 255, h5 >>> 24 & 255, h5 >>> 16 & 255, h5 >>> 8 & 255, h5 & 255, h6 >>> 24 & 255, h6 >>> 16 & 255, h6 >>> 8 & 255, h6 & 255 ]; if (!this.is224) { arr.push(h7 >>> 24 & 255, h7 >>> 16 & 255, h7 >>> 8 & 255, h7 & 255); } return arr; }; Sha256.prototype.array = Sha256.prototype.digest; Sha256.prototype.arrayBuffer = function() { this.finalize(); var buffer = new ArrayBuffer(this.is224 ? 28 : 32); var dataView = new DataView(buffer); dataView.setUint32(0, this.h0); dataView.setUint32(4, this.h1); dataView.setUint32(8, this.h2); dataView.setUint32(12, this.h3); dataView.setUint32(16, this.h4); dataView.setUint32(20, this.h5); dataView.setUint32(24, this.h6); if (!this.is224) { dataView.setUint32(28, this.h7); } return buffer; }; function HmacSha256(key, is224, sharedMemory) { var i, type = typeof key; if (type === "string") { var bytes = [], length = key.length, index = 0, code; for (i = 0; i < length; ++i) { code = key.charCodeAt(i); if (code < 128) { bytes[index++] = code; } else if (code < 2048) { bytes[index++] = 192 | code >>> 6; bytes[index++] = 128 | code & 63; } else if (code < 55296 || code >= 57344) { bytes[index++] = 224 | code >>> 12; bytes[index++] = 128 | code >>> 6 & 63; bytes[index++] = 128 | code & 63; } else { code = 65536 + ((code & 1023) << 10 | key.charCodeAt(++i) & 1023); bytes[index++] = 240 | code >>> 18; bytes[index++] = 128 | code >>> 12 & 63; bytes[index++] = 128 | code >>> 6 & 63; bytes[index++] = 128 | code & 63; } } key = bytes; } else { if (type === "object") { if (key === null) { throw new Error(ERROR); } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) { key = new Uint8Array(key); } else if (!Array.isArray(key)) { if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) { throw new Error(ERROR); } } } else { throw new Error(ERROR); } } if (key.length > 64) { key = new Sha256(is224, true).update(key).array(); } var oKeyPad = [], iKeyPad = []; for (i = 0; i < 64; ++i) { var b = key[i] || 0; oKeyPad[i] = 92 ^ b; iKeyPad[i] = 54 ^ b; } Sha256.call(this, is224, sharedMemory); this.update(iKeyPad); this.oKeyPad = oKeyPad; this.inner = true; this.sharedMemory = sharedMemory; } HmacSha256.prototype = new Sha256(); HmacSha256.prototype.finalize = function() { Sha256.prototype.finalize.call(this); if (this.inner) { this.inner = false; var innerHash = this.array(); Sha256.call(this, this.is224, this.sharedMemory); this.update(this.oKeyPad); this.update(innerHash); Sha256.prototype.finalize.call(this); } }; var exports2 = createMethod(); exports2.sha256 = exports2; exports2.sha224 = createMethod(true); exports2.sha256.hmac = createHmacMethod(); exports2.sha224.hmac = createHmacMethod(true); if (COMMON_JS) { module.exports = exports2; } else { root.sha256 = exports2.sha256; root.sha224 = exports2.sha224; if (AMD) { define(function() { return exports2; }); } } })(); } }); // js_client_transaction/index.js var index_exports = {}; __export(index_exports, { ClientTransaction: () => ClientTransaction, MathUtil: () => MathUtil, base64Decode: () => base64Decode, base64Encode: () => base64Encode, floatToHex: () => floatToHex, generateHeaders: () => generateHeaders, getOndemandFileUrl: () => getOndemandFileUrl, isOdd: () => isOdd }); // js_client_transaction/transaction.js var import_js_sha256 = __toESM(require_sha256(), 1); // js_client_transaction/cubicCurve.js var Cubic = class _Cubic { constructor(curves) { this.curves = curves; } getValue(time) { let startGradient = 0; let endGradient = 0; let start = 0; let mid = 0; let end = 1; if (time <= 0) { if (this.curves[0] > 0) { startGradient = this.curves[1] / this.curves[0]; } else if (this.curves[1] === 0 && this.curves[2] > 0) { startGradient = this.curves[3] / this.curves[2]; } return startGradient * time; } if (time >= 1) { if (this.curves[2] < 1) { endGradient = (this.curves[3] - 1) / (this.curves[2] - 1); } else if (this.curves[2] === 1 && this.curves[0] < 1) { endGradient = (this.curves[1] - 1) / (this.curves[0] - 1); } return 1 + endGradient * (time - 1); } while (start < end) { mid = (start + end) / 2; const xEst = _Cubic.calculate(this.curves[0], this.curves[2], mid); if (Math.abs(time - xEst) < 1e-5) { return _Cubic.calculate(this.curves[1], this.curves[3], mid); } if (xEst < time) { start = mid; } else { end = mid; } } return _Cubic.calculate(this.curves[1], this.curves[3], mid); } static calculate(a, b, m) { return 3 * a * (1 - m) * (1 - m) * m + 3 * b * (1 - m) * m * m + m * m * m; } }; // js_client_transaction/interpolate.js function interpolate(fromList, toList, f) { const len = Math.min(fromList.length, toList.length); const out = []; for (let i = 0; i < len; i++) { out.push(interpolateNum(fromList[i], toList[i], f)); } return out; } function interpolateNum(fromVal, toVal, f) { if (typeof fromVal === "number" && typeof toVal === "number") { return fromVal * (1 - f) + toVal * f; } if (typeof fromVal === "boolean" && typeof toVal === "boolean") { return f < 0.5 ? fromVal : toVal; } } // js_client_transaction/rotation.js function convertRotationToMatrix(rotation) { const rad = rotation * Math.PI / 180; return [Math.cos(rad), -Math.sin(rad), Math.sin(rad), Math.cos(rad)]; } // js_client_transaction/constants.js var ADDITIONAL_RANDOM_NUMBER = 3; var DEFAULT_KEYWORD = "obfiowerehiring"; var ON_DEMAND_FILE_URL = "https://abs.twimg.com/responsive-web/client-web/ondemand.s.{filename}a.js"; var ON_DEMAND_FILE_REGEX = /['"]ondemand\.s['"]:\s*['"]([\w]*)['"]/m; var INDICES_REGEX = /(\(\w\[(\d{1,2})\],\s*16\))/g; // js_client_transaction/utils.js var MathUtil = class { static round(num) { const x = Math.floor(num); if (num - x >= 0.5) { return Math.ceil(num); } return Math.sign(num) * x; } }; function generateHeaders() { return { "Authority": "x.com", "Accept-Language": "en-US,en;q=0.9", "Cache-Control": "no-cache", "Referer": "https://x.com", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36", "X-Twitter-Active-User": "yes", "X-Twitter-Client-Language": "en" }; } function validateResponse(response) { if (typeof response !== "string") { throw new TypeError(`the response object must be string, not ${typeof response}`); } } function getOndemandFileUrl(html) { const match = ON_DEMAND_FILE_REGEX.exec(html); if (!match) return null; return ON_DEMAND_FILE_URL.replace("{filename}", match[1]); } function floatToHex(x) { let result = []; let quotient = Math.trunc(x); let fraction = x - quotient; let q = quotient; while (q > 0) { const newQ = Math.trunc(q / 16); const remainder = q - newQ * 16; result.unshift(remainder > 9 ? String.fromCharCode(remainder + 55) : String(remainder)); q = newQ; } if (fraction === 0) return result.join(""); result.push("."); let f = fraction; while (f > 0) { f *= 16; const integer = Math.trunc(f); f -= integer; result.push(integer > 9 ? String.fromCharCode(integer + 55) : String(integer)); } return result.join(""); } function isOdd(num) { return num % 2 ? -1 : 0; } function base64Encode(input) { if (typeof Buffer !== "undefined") { const buf = typeof input === "string" ? Buffer.from(input) : Buffer.from(input); return buf.toString("base64"); } const bytes = typeof input === "string" ? new TextEncoder().encode(input) : input; let binary = ""; for (const b of bytes) binary += String.fromCharCode(b); return btoa(binary); } function base64Decode(input) { if (typeof Buffer !== "undefined") { return Buffer.from(input, "base64").toString(); } const binary = atob(input); return binary; } // js_client_transaction/transaction.js var ClientTransaction = class { constructor(homePageResponse, ondemandFileResponse, randomKeyword = null, randomNumber = null) { validateResponse(homePageResponse); validateResponse(ondemandFileResponse); this.homePageResponse = homePageResponse; this.ondemandFileResponse = ondemandFileResponse; this.randomKeyword = randomKeyword || DEFAULT_KEYWORD; this.randomNumber = randomNumber || ADDITIONAL_RANDOM_NUMBER; const indices = this.getIndices(this.ondemandFileResponse); this.rowIndex = indices[0]; this.keyBytesIndices = indices.slice(1); this.key = this.getKey(this.homePageResponse); this.keyBytes = this.getKeyBytes(this.key); this.animationKey = this.getAnimationKey(this.keyBytes, this.homePageResponse); } getIndices(ondemandFileResponse) { const matches = [...ondemandFileResponse.matchAll(INDICES_REGEX)]; if (matches.length === 0) { throw new Error("Couldn't get KEY_BYTE indices"); } const indices = matches.map((m) => parseInt(m[2], 10)); return [indices[0], ...indices.slice(1)]; } getKey(homePageResponse) { const match = homePageResponse.match(/<meta[^>]*name=['"]twitter-site-verification['"][^>]*content=['"]([^'"]+)['"]/); if (!match) { throw new Error("Couldn't get [twitter-site-verification] key from the page source"); } return match[1]; } getKeyBytes(key) { if (typeof Buffer !== "undefined") { return Array.from(Buffer.from(key, "base64")); } const binary = atob(key); return Array.from(binary, (c) => c.charCodeAt(0)); } getFrames(homePageResponse) { const regex = /id=['"]loading-x-anim-\d+['"][^>]*>.*?<path[^>]*d=['"]([^'"]+)['"]/gs; const frames = []; let m; while ((m = regex.exec(homePageResponse)) !== null) { frames.push(m[1]); } return frames; } get2dArray(keyBytes, homePageResponse, frames = null) { if (frames === null) { frames = this.getFrames(homePageResponse); } const path = frames[keyBytes[5] % 4]; const pieces = path.slice(9).split("C"); return pieces.map((item) => item.replace(/[^\d]+/g, " ").trim().split(/\s+/).map(Number)); } solve(value, minVal, maxVal, rounding) { const result = value * (maxVal - minVal) / 255 + minVal; return rounding ? Math.floor(result) : Math.round(result * 100) / 100; } animate(frames, targetTime) { const fromColor = frames.slice(0, 3).map(Number).concat([1]); const toColor = frames.slice(3, 6).map(Number).concat([1]); const fromRotation = [0]; const toRotation = [this.solve(frames[6], 60, 360, true)]; frames = frames.slice(7); const curves = frames.map((v, i) => this.solve(v, isOdd(i), 1, false)); const cubic = new Cubic(curves); const val = cubic.getValue(targetTime); let color = interpolate(fromColor, toColor, val); color = color.map((v) => Math.max(0, Math.min(255, v))); const rotation = interpolate(fromRotation, toRotation, val); const matrix = convertRotationToMatrix(rotation[0]); const strArr = color.slice(0, 3).map((v) => Math.round(v).toString(16)); for (const value of matrix) { let rounded = Math.round(value * 100) / 100; if (rounded < 0) rounded = -rounded; const hexValue = floatToHex(rounded); strArr.push(hexValue.startsWith(".") ? `0${hexValue}`.toLowerCase() : hexValue || "0"); } strArr.push("0", "0"); const animationKey = strArr.join("").replace(/[.-]/g, ""); return animationKey; } getAnimationKey(keyBytes, homePageResponse) { const totalTime = 4096; const rowIndex = keyBytes[this.rowIndex] % 16; const frameTime = this.keyBytesIndices.reduce((a, i) => a * (keyBytes[i] % 16), 1); const roundedFrameTime = MathUtil.round(frameTime / 10) * 10; const arr = this.get2dArray(keyBytes, homePageResponse); const frameRow = arr[rowIndex % arr.length]; const targetTime = roundedFrameTime / totalTime; return this.animate(frameRow, targetTime); } generateTransactionId(method, path, homePageResponse = null, key = null, animationKey = null, timeNow = null) { timeNow = timeNow || Math.floor((Date.now() - 16829244e5) / 1e3); const timeBytes = [timeNow >> 0 & 255, timeNow >> 8 & 255, timeNow >> 16 & 255, timeNow >> 24 & 255]; key = key || this.key || this.getKey(homePageResponse); const keyBytes = this.getKeyBytes(key); animationKey = animationKey || this.animationKey || this.getAnimationKey(keyBytes, homePageResponse); const hashBytes = import_js_sha256.sha256.array(`${method}!${path}!${timeNow}${this.randomKeyword}${animationKey}`); const randomNum = Math.floor(Math.random() * 256); const bytesArr = [...keyBytes, ...timeBytes, ...hashBytes.slice(0, 16), this.randomNumber]; const out = [randomNum, ...bytesArr.map((b) => b ^ randomNum)]; return base64Encode(Uint8Array.from(out)).replace(/=+$/, ""); } }; return __toCommonJS(index_exports); })(); /*! Bundled license information: js-sha256/src/sha256.js: (** * [js-sha256]{@link https://github.com/emn178/js-sha256} * * @version 0.11.1 * @author Chen, Yi-Cyuan [emn178@gmail.com] * @copyright Chen, Yi-Cyuan 2014-2025 * @license MIT *) */