UNPKG

@cmdcode/tapscript

Version:

A basic library for working with Tapscript, signatures and Bitcoin transactions.

1 lines 834 kB
{"version":3,"file":"main.cjs","sources":["../node_modules/@cmdcode/buff-utils/dist/module.mjs","../src/lib/utils.ts","../src/lib/script/words.ts","../src/lib/check.ts","../src/lib/script/encode.ts","../src/lib/script/decode.ts","../src/lib/script/format.ts","../src/lib/script/index.ts","../node_modules/@cmdcode/crypto-utils/dist/module.mjs","../src/lib/addr/hash.ts","../src/lib/addr/p2pkh.ts","../src/lib/addr/p2sh.ts","../src/lib/addr/schema.ts","../src/lib/addr/p2w-pkh.ts","../src/lib/addr/p2w-sh.ts","../src/lib/tap/utils.ts","../src/lib/addr/p2tr.ts","../src/lib/tx/create.ts","../src/lib/tx/encode.ts","../src/lib/tx/decode.ts","../src/lib/tx/format.ts","../src/lib/tx/parse.ts","../src/lib/tx/index.ts","../src/lib/addr/utils.ts","../src/lib/addr/index.ts","../src/lib/sig/segwit/hash.ts","../src/lib/sig/segwit/sign.ts","../src/lib/sig/segwit/verify.ts","../src/lib/sig/segwit/index.ts","../src/lib/sig/taproot/hash.ts","../src/lib/sig/taproot/sign.ts","../src/lib/tap/tree.ts","../src/lib/tap/tweak.ts","../src/lib/tap/key.ts","../src/lib/sig/taproot/verify.ts","../src/lib/sig/taproot/index.ts","../src/lib/sig/index.ts","../src/lib/tap/index.ts","../src/class/Transaction/TxScript.ts","../src/class/Transaction/TxSequence.ts","../src/class/Transaction/TxOutput.ts","../src/class/Transaction/TxWitness.ts","../src/class/Transaction/TxInput.ts","../src/class/Transaction/TxLocktime.ts","../node_modules/zod/lib/index.mjs","../src/schema/check.ts","../src/class/Transaction/Transaction.ts"],"sourcesContent":["function number(n) {\n if (!Number.isSafeInteger(n) || n < 0)\n throw new Error(`Wrong positive integer: ${n}`);\n}\nfunction bool(b) {\n if (typeof b !== 'boolean')\n throw new Error(`Expected boolean, not ${b}`);\n}\nfunction bytes(b, ...lengths) {\n if (!(b instanceof Uint8Array))\n throw new TypeError('Expected Uint8Array');\n if (lengths.length > 0 && !lengths.includes(b.length))\n throw new TypeError(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);\n}\nfunction hash(hash) {\n if (typeof hash !== 'function' || typeof hash.create !== 'function')\n throw new Error('Hash should be wrapped by utils.wrapConstructor');\n number(hash.outputLen);\n number(hash.blockLen);\n}\nfunction exists(instance, checkFinished = true) {\n if (instance.destroyed)\n throw new Error('Hash instance has been destroyed');\n if (checkFinished && instance.finished)\n throw new Error('Hash#digest() has already been called');\n}\nfunction output(out, instance) {\n bytes(out);\n const min = instance.outputLen;\n if (out.length < min) {\n throw new Error(`digestInto() expects output buffer of length at least ${min}`);\n }\n}\nconst assert$1 = {\n number,\n bool,\n bytes,\n hash,\n exists,\n output,\n};\nvar assert$2 = assert$1;\n\n/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */\n// Cast array to view\nconst createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);\n// The rotate right (circular right shift) operation for uint32\nconst rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);\n// big-endian hardware is rare. Just in case someone still decides to run hashes:\n// early-throw an error because we don't support BE yet.\nconst isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;\nif (!isLE)\n throw new Error('Non little-endian hardware is not supported');\nArray.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0'));\nfunction utf8ToBytes(str) {\n if (typeof str !== 'string') {\n throw new TypeError(`utf8ToBytes expected string, got ${typeof str}`);\n }\n return new TextEncoder().encode(str);\n}\nfunction toBytes(data) {\n if (typeof data === 'string')\n data = utf8ToBytes(data);\n if (!(data instanceof Uint8Array))\n throw new TypeError(`Expected input type is Uint8Array (got ${typeof data})`);\n return data;\n}\n// For runtime check if class implements interface\nclass Hash {\n // Safe version that clones internal state\n clone() {\n return this._cloneInto();\n }\n}\nfunction wrapConstructor(hashConstructor) {\n const hashC = (message) => hashConstructor().update(toBytes(message)).digest();\n const tmp = hashConstructor();\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = () => hashConstructor();\n return hashC;\n}\n\n// Polyfill for Safari 14\nfunction setBigUint64(view, byteOffset, value, isLE) {\n if (typeof view.setBigUint64 === 'function')\n return view.setBigUint64(byteOffset, value, isLE);\n const _32n = BigInt(32);\n const _u32_max = BigInt(0xffffffff);\n const wh = Number((value >> _32n) & _u32_max);\n const wl = Number(value & _u32_max);\n const h = isLE ? 4 : 0;\n const l = isLE ? 0 : 4;\n view.setUint32(byteOffset + h, wh, isLE);\n view.setUint32(byteOffset + l, wl, isLE);\n}\n// Base SHA2 class (RFC 6234)\nclass SHA2 extends Hash {\n constructor(blockLen, outputLen, padOffset, isLE) {\n super();\n this.blockLen = blockLen;\n this.outputLen = outputLen;\n this.padOffset = padOffset;\n this.isLE = isLE;\n this.finished = false;\n this.length = 0;\n this.pos = 0;\n this.destroyed = false;\n this.buffer = new Uint8Array(blockLen);\n this.view = createView(this.buffer);\n }\n update(data) {\n assert$2.exists(this);\n const { view, buffer, blockLen } = this;\n data = toBytes(data);\n const len = data.length;\n for (let pos = 0; pos < len;) {\n const take = Math.min(blockLen - this.pos, len - pos);\n // Fast path: we have at least one block in input, cast it to view and process\n if (take === blockLen) {\n const dataView = createView(data);\n for (; blockLen <= len - pos; pos += blockLen)\n this.process(dataView, pos);\n continue;\n }\n buffer.set(data.subarray(pos, pos + take), this.pos);\n this.pos += take;\n pos += take;\n if (this.pos === blockLen) {\n this.process(view, 0);\n this.pos = 0;\n }\n }\n this.length += data.length;\n this.roundClean();\n return this;\n }\n digestInto(out) {\n assert$2.exists(this);\n assert$2.output(out, this);\n this.finished = true;\n // Padding\n // We can avoid allocation of buffer for padding completely if it\n // was previously not allocated here. But it won't change performance.\n const { buffer, view, blockLen, isLE } = this;\n let { pos } = this;\n // append the bit '1' to the message\n buffer[pos++] = 0b10000000;\n this.buffer.subarray(pos).fill(0);\n // we have less than padOffset left in buffer, so we cannot put length in current block, need process it and pad again\n if (this.padOffset > blockLen - pos) {\n this.process(view, 0);\n pos = 0;\n }\n // Pad until full block byte with zeros\n for (let i = pos; i < blockLen; i++)\n buffer[i] = 0;\n // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that\n // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.\n // So we just write lowest 64 bits of that value.\n setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);\n this.process(view, 0);\n const oview = createView(out);\n const len = this.outputLen;\n // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT\n if (len % 4)\n throw new Error('_sha2: outputLen should be aligned to 32bit');\n const outLen = len / 4;\n const state = this.get();\n if (outLen > state.length)\n throw new Error('_sha2: outputLen bigger than state');\n for (let i = 0; i < outLen; i++)\n oview.setUint32(4 * i, state[i], isLE);\n }\n digest() {\n const { buffer, outputLen } = this;\n this.digestInto(buffer);\n const res = buffer.slice(0, outputLen);\n this.destroy();\n return res;\n }\n _cloneInto(to) {\n to || (to = new this.constructor());\n to.set(...this.get());\n const { blockLen, buffer, length, finished, destroyed, pos } = this;\n to.length = length;\n to.pos = pos;\n to.finished = finished;\n to.destroyed = destroyed;\n if (length % blockLen)\n to.buffer.set(buffer);\n return to;\n }\n}\n\n// Choice: a ? b : c\nconst Chi = (a, b, c) => (a & b) ^ (~a & c);\n// Majority function, true if any two inpust is true\nconst Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);\n// Round constants:\n// first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)\n// prettier-ignore\nconst SHA256_K = new Uint32Array([\n 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\n 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\n 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\n 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\n 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\n 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\n 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\n 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2\n]);\n// Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):\n// prettier-ignore\nconst IV = new Uint32Array([\n 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19\n]);\n// Temporary buffer, not used to store anything between runs\n// Named this way because it matches specification.\nconst SHA256_W = new Uint32Array(64);\nclass SHA256 extends SHA2 {\n constructor() {\n super(64, 32, 8, false);\n // We cannot use array here since array allows indexing by variable\n // which means optimizer/compiler cannot use registers.\n this.A = IV[0] | 0;\n this.B = IV[1] | 0;\n this.C = IV[2] | 0;\n this.D = IV[3] | 0;\n this.E = IV[4] | 0;\n this.F = IV[5] | 0;\n this.G = IV[6] | 0;\n this.H = IV[7] | 0;\n }\n get() {\n const { A, B, C, D, E, F, G, H } = this;\n return [A, B, C, D, E, F, G, H];\n }\n // prettier-ignore\n set(A, B, C, D, E, F, G, H) {\n this.A = A | 0;\n this.B = B | 0;\n this.C = C | 0;\n this.D = D | 0;\n this.E = E | 0;\n this.F = F | 0;\n this.G = G | 0;\n this.H = H | 0;\n }\n process(view, offset) {\n // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array\n for (let i = 0; i < 16; i++, offset += 4)\n SHA256_W[i] = view.getUint32(offset, false);\n for (let i = 16; i < 64; i++) {\n const W15 = SHA256_W[i - 15];\n const W2 = SHA256_W[i - 2];\n const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);\n const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);\n SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;\n }\n // Compression function main loop, 64 rounds\n let { A, B, C, D, E, F, G, H } = this;\n for (let i = 0; i < 64; i++) {\n const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);\n const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;\n const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);\n const T2 = (sigma0 + Maj(A, B, C)) | 0;\n H = G;\n G = F;\n F = E;\n E = (D + T1) | 0;\n D = C;\n C = B;\n B = A;\n A = (T1 + T2) | 0;\n }\n // Add the compressed chunk to the current hash value\n A = (A + this.A) | 0;\n B = (B + this.B) | 0;\n C = (C + this.C) | 0;\n D = (D + this.D) | 0;\n E = (E + this.E) | 0;\n F = (F + this.F) | 0;\n G = (G + this.G) | 0;\n H = (H + this.H) | 0;\n this.set(A, B, C, D, E, F, G, H);\n }\n roundClean() {\n SHA256_W.fill(0);\n }\n destroy() {\n this.set(0, 0, 0, 0, 0, 0, 0, 0);\n this.buffer.fill(0);\n }\n}\n// Constants from https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf\nclass SHA224 extends SHA256 {\n constructor() {\n super();\n this.A = 0xc1059ed8 | 0;\n this.B = 0x367cd507 | 0;\n this.C = 0x3070dd17 | 0;\n this.D = 0xf70e5939 | 0;\n this.E = 0xffc00b31 | 0;\n this.F = 0x68581511 | 0;\n this.G = 0x64f98fa7 | 0;\n this.H = 0xbefa4fa4 | 0;\n this.outputLen = 28;\n }\n}\n/**\n * SHA2-256 hash function\n * @param message - data that would be hashed\n */\nconst sha256 = wrapConstructor(() => new SHA256());\nwrapConstructor(() => new SHA224());\n\nfunction within_size(data, size) {\n if (data.length > size) {\n throw new TypeError(`Data is larger than array size: ${data.length} > ${size}`);\n }\n}\nfunction is_hex(hex) {\n if (hex.match(/[^a-fA-f0-9]/) !== null) {\n throw new TypeError('Invalid characters in hex string: ' + hex);\n }\n if (hex.length % 2 !== 0) {\n throw new Error(`Length of hex string is invalid: ${hex.length}`);\n }\n}\nfunction is_json(str) {\n try {\n JSON.parse(str);\n }\n catch {\n throw new TypeError('JSON string is invalid!');\n }\n}\nfunction is_safe_num(num) {\n if (num > Number.MAX_SAFE_INTEGER) {\n throw new TypeError('Number exceeds safe bounds!');\n }\n}\n\nvar assert = /*#__PURE__*/Object.freeze({\n __proto__: null,\n is_hex: is_hex,\n is_json: is_json,\n is_safe_num: is_safe_num,\n within_size: within_size\n});\n\nconst { getRandomValues } = crypto ?? globalThis.crypto ?? window.crypto;\nfunction random(size = 32) {\n if (typeof getRandomValues === 'function') {\n return crypto.getRandomValues(new Uint8Array(size));\n }\n throw new Error('Crypto module missing getRandomValues!');\n}\nfunction set_buffer(data, size, endian = 'be') {\n if (size === undefined)\n size = data.length;\n within_size(data, size);\n const buffer = new Uint8Array(size).fill(0);\n const offset = (endian === 'be') ? 0 : size - data.length;\n buffer.set(data, offset);\n return buffer;\n}\nfunction join_array(arr) {\n let i, offset = 0;\n const size = arr.reduce((len, arr) => len + arr.length, 0);\n const buff = new Uint8Array(size);\n for (i = 0; i < arr.length; i++) {\n const a = arr[i];\n buff.set(a, offset);\n offset += a.length;\n }\n return buff;\n}\n\nvar utils = /*#__PURE__*/Object.freeze({\n __proto__: null,\n join_array: join_array,\n random: random,\n set_buffer: set_buffer\n});\n\nconst ec$2 = new TextEncoder();\nconst ALPHABETS = [\n {\n name: 'base58',\n charset: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'\n }\n];\nfunction getAlphabet(name) {\n for (const alpha of ALPHABETS) {\n if (alpha.name === name) {\n return alpha.charset;\n }\n }\n throw TypeError('Charset does not exist: ' + name);\n}\nfunction encode$1(data, charset, padding = false) {\n if (typeof data === 'string')\n data = ec$2.encode(data);\n const alphabet = getAlphabet(charset);\n const len = alphabet.length;\n const d = [];\n let s = '', i, j = 0, c, n;\n for (i = 0; i < data.length; i++) {\n j = 0;\n c = data[i];\n s += (c > 0 || (s.length ^ i) > 0) ? '' : '1';\n while (j in d || c > 0) {\n n = d[j];\n n = n > 0 ? n * 256 + c : c;\n c = n / len | 0;\n d[j] = n % len;\n j++;\n }\n }\n while (j-- > 0) {\n s += alphabet[d[j]];\n }\n return (padding && s.length % 4 > 0)\n ? s + '='.repeat(4 - s.length % 4)\n : s;\n}\nfunction decode$1(encoded, charset) {\n const alphabet = getAlphabet(charset);\n const len = alphabet.length, d = [], b = [];\n encoded = encoded.replace('=', '');\n let i, j = 0, c, n;\n for (i = 0; i < encoded.length; i++) {\n j = 0;\n c = alphabet.indexOf(encoded[i]);\n if (c < 0) {\n throw new Error(`Character range out of bounds: ${c}`);\n }\n if (!(c > 0 || (b.length ^ i) > 0))\n b.push(0);\n while (j in d || c > 0) {\n n = d[j];\n n = n > 0 ? n * len + c : c;\n c = n >> 8;\n d[j] = n % 256;\n j++;\n }\n }\n while (j-- > 0) {\n b.push(d[j]);\n }\n return new Uint8Array(b);\n}\nfunction hash256(data) {\n return sha256(sha256(data));\n}\nfunction addChecksum(data) {\n const sum = hash256(data);\n return join_array([data, sum.slice(0, 4)]);\n}\nfunction checkTheSum(data) {\n const ret = data.slice(0, -4);\n const chk = data.slice(-4);\n const sum = hash256(ret).slice(0, 4);\n if (sum.toString() !== chk.toString()) {\n throw new Error('Invalid checksum!');\n }\n return ret;\n}\nconst BaseX = {\n encode: encode$1,\n decode: decode$1\n};\nconst Base58 = {\n encode: (data) => BaseX.encode(data, 'base58'),\n decode: (data) => BaseX.decode(data, 'base58')\n};\nconst Base58C = {\n encode: (data) => {\n const withSum = addChecksum(data);\n return BaseX.encode(withSum, 'base58');\n },\n decode: (data) => {\n const decoded = BaseX.decode(data, 'base58');\n return checkTheSum(decoded);\n }\n};\n\nconst CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';\nconst GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];\nconst ENCODINGS = [\n { version: 0, name: 'bech32', const: 1 },\n { version: 1, name: 'bech32m', const: 0x2bc830a3 }\n];\nfunction polymod(values) {\n let chk = 1;\n for (let p = 0; p < values.length; ++p) {\n const top = chk >> 25;\n chk = (chk & 0x1ffffff) << 5 ^ values[p];\n for (let i = 0; i < 5; ++i) {\n if (((top >> i) & 1) !== 0) {\n chk ^= GENERATOR[i];\n }\n }\n }\n return chk;\n}\nfunction hrpExpand(hrp) {\n const ret = [];\n let p;\n for (p = 0; p < hrp.length; ++p) {\n ret.push(hrp.charCodeAt(p) >> 5);\n }\n ret.push(0);\n for (p = 0; p < hrp.length; ++p) {\n ret.push(hrp.charCodeAt(p) & 31);\n }\n return ret;\n}\nfunction verifyChecksum(hrp, data, enc) {\n const combined = hrpExpand(hrp).concat(data);\n return polymod(combined) === enc.const;\n}\nfunction createChecksum(hrp, data, enc) {\n const values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);\n const mod = polymod(values) ^ enc.const;\n const ret = [];\n for (let p = 0; p < 6; ++p) {\n ret.push((mod >> 5 * (5 - p)) & 31);\n }\n return ret;\n}\nfunction convertBits(data, fromBits, toBits, pad = true) {\n const ret = [];\n let acc = 0;\n let bits = 0;\n const maxVal = (1 << toBits) - 1;\n const maxAcc = (1 << (fromBits + toBits - 1)) - 1;\n for (const val of data) {\n if (val < 0 || (val >> fromBits) > 0) {\n throw new Error('Failed to perform base conversion. Invalid value: ' + String(val));\n }\n acc = ((acc << fromBits) | val) & maxAcc;\n bits += fromBits;\n while (bits >= toBits) {\n bits -= toBits;\n ret.push((acc >> bits) & maxVal);\n }\n }\n if (pad) {\n if (bits > 0) {\n ret.push((acc << (toBits - bits)) & maxVal);\n }\n }\n else if (bits >= fromBits || ((acc << (toBits - bits)) & maxVal) > 0) {\n throw new Error('Failed to perform base conversion. Invalid Size!');\n }\n return ret;\n}\nfunction encode(hrp, data, enc) {\n const combined = data.concat(createChecksum(hrp, data, enc));\n let ret = hrp + '1';\n for (let p = 0; p < combined.length; ++p) {\n ret += CHARSET.charAt(combined[p]);\n }\n return ret;\n}\nfunction decode(bechstr) {\n if (!checkBounds(bechstr)) {\n throw new Error('Encoded string goes out of bounds!');\n }\n bechstr = bechstr.toLowerCase();\n if (!checkSeparatorPos(bechstr)) {\n throw new Error('Encoded string has invalid separator!');\n }\n const data = [];\n const pos = bechstr.lastIndexOf('1');\n const hrp = bechstr.substring(0, pos);\n for (let p = pos + 1; p < bechstr.length; ++p) {\n const d = CHARSET.indexOf(bechstr.charAt(p));\n if (d === -1) {\n throw new Error('Character idx out of bounds: ' + String(p));\n }\n data.push(d);\n }\n const enc = ENCODINGS.find(e => e.version === data[0]) ?? ENCODINGS[0];\n if (!verifyChecksum(hrp, data, enc)) {\n throw new Error('Checksum verification failed!');\n }\n return [hrp, data.slice(0, data.length - 6)];\n}\nfunction checkBounds(bechstr) {\n let p;\n let char;\n let hasLower = false;\n let hasUpper = false;\n for (p = 0; p < bechstr.length; ++p) {\n char = bechstr.charCodeAt(p);\n if (char < 33 || char > 126) {\n return false;\n }\n if (char >= 97 && char <= 122) {\n hasLower = true;\n }\n if (char >= 65 && char <= 90) {\n hasUpper = true;\n }\n }\n if (hasLower && hasUpper)\n return false;\n return true;\n}\nfunction checkSeparatorPos(bechstr) {\n const pos = bechstr.lastIndexOf('1');\n return !(pos < 1 ||\n pos + 7 > bechstr.length ||\n bechstr.length > 90);\n}\nfunction b32encode(data, hrp = 'bc', version = 0) {\n const dat = [version, ...convertBits([...data], 8, 5)];\n const enc = ENCODINGS.find(e => e.version === version) ?? ENCODINGS[0];\n const str = encode(hrp, dat, enc);\n b32decode(str);\n return str;\n}\nfunction b32decode(str) {\n str = str.toLowerCase();\n const hrp = str.split('1', 1)[0];\n const [hrpgot, data] = decode(str);\n const decoded = convertBits(data.slice(1), 5, 8, false);\n const length = decoded.length;\n switch (true) {\n case (hrp !== hrpgot):\n throw new Error('Returned hrp string is invalid.');\n case (decoded === null || length < 2 || length > 40):\n throw new Error('Decoded string is invalid or out of spec.');\n case (data[0] > 16):\n throw new Error('Returned version bit is out of range.');\n default:\n return Uint8Array.from(decoded);\n }\n}\nfunction getVersion(str) {\n str = str.toLowerCase();\n const [_, data] = decode(str);\n return data[0];\n}\nconst Bech32 = {\n encode: b32encode,\n decode: b32decode,\n version: getVersion\n};\n\nconst BASE64_MAP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';\nconst B64URL_MAP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';\nconst ec$1 = new TextEncoder();\nfunction b64encode(input, urlSafe = false, padding = true) {\n if (typeof input === 'string')\n input = ec$1.encode(input);\n const map = urlSafe ? B64URL_MAP : BASE64_MAP;\n let output = '';\n let bits = 0;\n let buffer = 0;\n for (let i = 0; i < input.length; i++) {\n buffer = (buffer << 8) | input[i];\n bits += 8;\n while (bits >= 6) {\n bits -= 6;\n output += map[(buffer >> bits) & 0x3f];\n }\n }\n if (bits > 0) {\n buffer <<= 6 - bits;\n output += map[buffer & 0x3f];\n while (bits < 6) {\n output += padding ? '=' : '';\n bits += 2;\n }\n }\n return output;\n}\nfunction b64decode(input, urlSafe = false) {\n const map = (urlSafe || input.includes('-') || input.includes('_'))\n ? B64URL_MAP.split('')\n : BASE64_MAP.split('');\n input = input.replace(/=+$/, '');\n const chars = input.split('');\n let bits = 0;\n let value = 0;\n const bytes = [];\n for (let i = 0; i < chars.length; i++) {\n const c = chars[i];\n const index = map.indexOf(c);\n if (index === -1) {\n throw new Error('Invalid character: ' + c);\n }\n bits += 6;\n value <<= 6;\n value |= index;\n if (bits >= 8) {\n bits -= 8;\n bytes.push((value >>> bits) & 0xff);\n }\n }\n return new Uint8Array(bytes);\n}\nconst Base64 = {\n encode: b64encode,\n decode: b64decode\n};\nconst B64URL = {\n encode: (data) => b64encode(data, true, false),\n decode: (data) => b64decode(data, true)\n};\n\nconst _0n = BigInt(0);\nconst _255n = BigInt(255);\nconst _256n = BigInt(256);\nfunction big_size(big) {\n if (big <= 0xffn)\n return 1;\n if (big <= 0xffffn)\n return 2;\n if (big <= 0xffffffffn)\n return 4;\n if (big <= 0xffffffffffffffffn)\n return 8;\n if (big <= 0xffffffffffffffffffffffffffffffffn)\n return 16;\n if (big <= 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn) {\n return 32;\n }\n throw new TypeError('Must specify a fixed buffer size for bigints greater than 32 bytes.');\n}\nfunction bigToBytes(big, size, endian = 'be') {\n if (size === undefined)\n size = big_size(big);\n const use_le = (endian === 'le');\n const buffer = new ArrayBuffer(size);\n const dataView = new DataView(buffer);\n let offset = (use_le) ? 0 : size - 1;\n while (big > _0n) {\n const byte = big & _255n;\n const num = Number(byte);\n if (use_le) {\n dataView.setUint8(offset++, num);\n }\n else {\n dataView.setUint8(offset--, num);\n }\n big = (big - byte) / _256n;\n }\n return new Uint8Array(buffer);\n}\nfunction bytesToBig(bytes) {\n let num = BigInt(0);\n for (let i = bytes.length - 1; i >= 0; i--) {\n num = (num * _256n) + BigInt(bytes[i]);\n }\n return BigInt(num);\n}\n\nfunction binToBytes(binary) {\n const bins = binary.split('').map(Number);\n if (bins.length % 8 !== 0) {\n throw new Error(`Binary array is invalid length: ${binary.length}`);\n }\n const bytes = new Uint8Array(bins.length / 8);\n for (let i = 0, ct = 0; i < bins.length; i += 8, ct++) {\n let byte = 0;\n for (let j = 0; j < 8; j++) {\n byte |= (bins[i + j] << (7 - j));\n }\n bytes[ct] = byte;\n }\n return bytes;\n}\nfunction bytesToBin(bytes) {\n const bin = new Array(bytes.length * 8);\n let count = 0;\n for (const num of bytes) {\n if (num > 255) {\n throw new Error(`Invalid byte value: ${num}. Byte values must be between 0 and 255.`);\n }\n for (let i = 7; i >= 0; i--, count++) {\n bin[count] = (num >> i) & 1;\n }\n }\n return bin.join('');\n}\n\nfunction num_size(num) {\n if (num <= 0xFF)\n return 1;\n if (num <= 0xFFFF)\n return 2;\n if (num <= 0xFFFFFFFF)\n return 4;\n throw new TypeError('Numbers larger than 4 bytes must specify a fixed size!');\n}\nfunction numToBytes(num, size, endian = 'be') {\n if (size === undefined)\n size = num_size(num);\n const use_le = (endian === 'le');\n const buffer = new ArrayBuffer(size);\n const dataView = new DataView(buffer);\n let offset = (use_le) ? 0 : size - 1;\n while (num > 0) {\n const byte = num & 255;\n if (use_le) {\n dataView.setUint8(offset++, num);\n }\n else {\n dataView.setUint8(offset--, num);\n }\n num = (num - byte) / 256;\n }\n return new Uint8Array(buffer);\n}\nfunction bytesToNum(bytes) {\n let num = 0;\n for (let i = bytes.length - 1; i >= 0; i--) {\n num = (num * 256) + bytes[i];\n is_safe_num(num);\n }\n return num;\n}\n\nconst ec = new TextEncoder();\nconst dc = new TextDecoder();\nfunction strToBytes(str) {\n return ec.encode(str);\n}\nfunction bytesToStr(bytes) {\n return dc.decode(bytes);\n}\nfunction hex_size(hexstr, size) {\n is_hex(hexstr);\n const len = hexstr.length / 2;\n if (size === undefined)\n size = len;\n if (len > size) {\n throw new TypeError(`Hex string is larger than array size: ${len} > ${size}`);\n }\n return size;\n}\nfunction hexToBytes(hexstr, size, endian = 'le') {\n size = hex_size(hexstr, size);\n const use_le = (endian === 'le');\n const buffer = new ArrayBuffer(size);\n const dataView = new DataView(buffer);\n let offset = (use_le) ? 0 : size - 1;\n for (let i = 0; i < hexstr.length; i += 2) {\n const char = hexstr.substring(i, i + 2);\n const num = parseInt(char, 16);\n if (use_le) {\n dataView.setUint8(offset++, num);\n }\n else {\n dataView.setUint8(offset--, num);\n }\n }\n return new Uint8Array(buffer);\n}\nfunction bytesToHex(bytes) {\n let chars = '';\n for (let i = 0; i < bytes.length; i++) {\n chars += bytes[i].toString(16).padStart(2, '0');\n }\n return chars;\n}\nfunction jsonToBytes(obj) {\n const str = JSON.stringify(obj, (_, v) => {\n return typeof v === 'bigint'\n ? `${v}n`\n : v;\n });\n return strToBytes(str);\n}\nconst Hex = {\n encode: bytesToHex,\n decode: hexToBytes\n};\nconst Txt = {\n encode: strToBytes,\n decode: bytesToStr\n};\n\nfunction buffer(data, size, endian) {\n if (data instanceof ArrayBuffer) {\n return new Uint8Array(data);\n }\n if (data instanceof Uint8Array) {\n return set_buffer(data, size, endian);\n }\n if (typeof data === 'string') {\n return hexToBytes(data, size, endian);\n }\n if (typeof data === 'bigint') {\n return bigToBytes(data, size, endian);\n }\n if (typeof data === 'number') {\n return numToBytes(data, size, endian);\n }\n if (typeof data === 'boolean') {\n return Uint8Array.of(data ? 1 : 0);\n }\n throw TypeError('Unsupported format:' + String(typeof data));\n}\n\nclass Buff extends Uint8Array {\n static { this.num = numToBuff; }\n static { this.big = bigToBuff; }\n static { this.bin = binToBuff; }\n static { this.raw = rawToBuff; }\n static { this.str = strToBuff; }\n static { this.hex = hexToBuff; }\n static { this.bytes = bytesToBuff; }\n static { this.json = jsonToBuff; }\n static { this.base64 = base64ToBuff; }\n static { this.b64url = b64urlToBuff; }\n static { this.bech32 = bech32ToBuff; }\n static { this.b58chk = b58chkToBuff; }\n static { this.encode = strToBytes; }\n static { this.decode = bytesToStr; }\n static random(size = 32) {\n const rand = random(size);\n return new Buff(rand, size);\n }\n constructor(data, size, endian) {\n const buffer$1 = buffer(data, size, endian);\n super(buffer$1);\n }\n get arr() {\n return [...this];\n }\n get num() {\n return this.toNum();\n }\n get big() {\n return this.toBig();\n }\n get str() {\n return this.toStr();\n }\n get hex() {\n return this.toHex();\n }\n get raw() {\n return new Uint8Array(this);\n }\n get bin() {\n return this.toBin();\n }\n get b58chk() {\n return this.tob58chk();\n }\n get base64() {\n return this.toBase64();\n }\n get b64url() {\n return this.toB64url();\n }\n get digest() {\n return this.toHash();\n }\n get id() {\n return this.toHash().hex;\n }\n get stream() {\n return new Stream(this);\n }\n toNum(endian = 'be') {\n const bytes = (endian === 'be')\n ? this.reverse()\n : this;\n return bytesToNum(bytes);\n }\n toBin() {\n return bytesToBin(this);\n }\n toBig(endian = 'be') {\n const bytes = (endian === 'be')\n ? this.reverse()\n : this;\n return bytesToBig(bytes);\n }\n toHash() {\n const digest = sha256(this);\n return new Buff(digest);\n }\n toJson() {\n const str = bytesToStr(this);\n return JSON.parse(str);\n }\n toBech32(hrp, version = 0) {\n return Bech32.encode(this, hrp, version);\n }\n toStr() { return bytesToStr(this); }\n toHex() { return bytesToHex(this); }\n toBytes() { return new Uint8Array(this); }\n tob58chk() { return Base58C.encode(this); }\n toBase64() { return Base64.encode(this); }\n toB64url() { return B64URL.encode(this); }\n prepend(data) {\n return Buff.join([Buff.bytes(data), this]);\n }\n append(data) {\n return Buff.join([this, Buff.bytes(data)]);\n }\n slice(start, end) {\n const arr = new Uint8Array(this).slice(start, end);\n return new Buff(arr);\n }\n subarray(begin, end) {\n const arr = new Uint8Array(this).subarray(begin, end);\n return new Buff(arr);\n }\n reverse() {\n const arr = new Uint8Array(this).reverse();\n return new Buff(arr);\n }\n write(bytes, offset) {\n const b = Buff.bytes(bytes);\n this.set(b, offset);\n }\n prefixSize(endian) {\n const size = Buff.varInt(this.length, endian);\n return Buff.join([size, this]);\n }\n static from(data) {\n return new Buff(Uint8Array.from(data));\n }\n static of(...args) {\n return new Buff(Uint8Array.of(...args));\n }\n static join(arr) {\n const bytes = arr.map(e => Buff.bytes(e));\n const joined = join_array(bytes);\n return new Buff(joined);\n }\n static varInt(num, endian) {\n if (num < 0xFD) {\n return Buff.num(num, 1);\n }\n else if (num < 0x10000) {\n return Buff.of(0xFD, ...Buff.num(num, 2, endian));\n }\n else if (num < 0x100000000) {\n return Buff.of(0xFE, ...Buff.num(num, 4, endian));\n }\n else if (BigInt(num) < 0x10000000000000000n) {\n return Buff.of(0xFF, ...Buff.num(num, 8, endian));\n }\n else {\n throw new Error(`Value is too large: ${num}`);\n }\n }\n}\nfunction numToBuff(number, size, endian) {\n return new Buff(number, size, endian);\n}\nfunction binToBuff(data, size, endian) {\n return new Buff(binToBytes(data), size, endian);\n}\nfunction bigToBuff(bigint, size, endian) {\n return new Buff(bigint, size, endian);\n}\nfunction rawToBuff(data, size, endian) {\n return new Buff(data, size, endian);\n}\nfunction strToBuff(data, size, endian) {\n return new Buff(strToBytes(data), size, endian);\n}\nfunction hexToBuff(data, size, endian) {\n return new Buff(data, size, endian);\n}\nfunction bytesToBuff(data, size, endian) {\n return new Buff(data, size, endian);\n}\nfunction jsonToBuff(data) {\n return new Buff(jsonToBytes(data));\n}\nfunction base64ToBuff(data) {\n return new Buff(Base64.decode(data));\n}\nfunction b64urlToBuff(data) {\n return new Buff(B64URL.decode(data));\n}\nfunction bech32ToBuff(data) {\n return new Buff(Bech32.decode(data));\n}\nfunction b58chkToBuff(data) {\n return new Buff(Base58C.decode(data));\n}\nclass Stream {\n constructor(data) {\n this.data = Buff.bytes(data);\n this.size = this.data.length;\n }\n peek(size) {\n if (size > this.size) {\n throw new Error(`Size greater than stream: ${size} > ${this.size}`);\n }\n return new Buff(this.data.slice(0, size));\n }\n read(size) {\n size = size ?? this.readSize();\n const chunk = this.peek(size);\n this.data = this.data.slice(size);\n this.size = this.data.length;\n return chunk;\n }\n readSize(endian) {\n const num = this.read(1).num;\n switch (true) {\n case (num >= 0 && num < 0xFD):\n return num;\n case (num === 0xFD):\n return this.read(2).toNum(endian);\n case (num === 0xFE):\n return this.read(4).toNum(endian);\n case (num === 0xFF):\n return this.read(8).toNum(endian);\n default:\n throw new Error(`Varint is out of range: ${num}`);\n }\n }\n}\n\nexport { B64URL, Base58, Base58C, Base64, Bech32, Buff, Hex, Stream, Txt, assert, b64decode, buffer, utils as util };\n//# sourceMappingURL=module.mjs.map\n",null,null,null,null,null,null,null,"function number$2(n) {\n if (!Number.isSafeInteger(n) || n < 0)\n throw new Error(`Wrong positive integer: ${n}`);\n}\nfunction bool$2(b) {\n if (typeof b !== 'boolean')\n throw new Error(`Expected boolean, not ${b}`);\n}\nfunction bytes$2(b, ...lengths) {\n if (!(b instanceof Uint8Array))\n throw new TypeError('Expected Uint8Array');\n if (lengths.length > 0 && !lengths.includes(b.length))\n throw new TypeError(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);\n}\nfunction hash$3(hash) {\n if (typeof hash !== 'function' || typeof hash.create !== 'function')\n throw new Error('Hash should be wrapped by utils.wrapConstructor');\n number$2(hash.outputLen);\n number$2(hash.blockLen);\n}\nfunction exists$2(instance, checkFinished = true) {\n if (instance.destroyed)\n throw new Error('Hash instance has been destroyed');\n if (checkFinished && instance.finished)\n throw new Error('Hash#digest() has already been called');\n}\nfunction output$2(out, instance) {\n bytes$2(out);\n const min = instance.outputLen;\n if (out.length < min) {\n throw new Error(`digestInto() expects output buffer of length at least ${min}`);\n }\n}\nconst assert$3 = {\n number: number$2,\n bool: bool$2,\n bytes: bytes$2,\n hash: hash$3,\n exists: exists$2,\n output: output$2,\n};\nvar assert$4 = assert$3;\n\nconst crypto$1 = typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;\n\n/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */\n// We use `globalThis.crypto`, but node.js versions earlier than v19 don't\n// declare it in global scope. For node.js, package.json#exports field mapping\n// rewrites import from `crypto` to `cryptoNode`, which imports native module.\n// Makes the utils un-importable in browsers without a bundler.\n// Once node.js 18 is deprecated, we can just drop the import.\n// Cast array to view\nconst createView$2 = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);\n// The rotate right (circular right shift) operation for uint32\nconst rotr$2 = (word, shift) => (word << (32 - shift)) | (word >>> shift);\n// big-endian hardware is rare. Just in case someone still decides to run hashes:\n// early-throw an error because we don't support BE yet.\nconst isLE$2 = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;\nif (!isLE$2)\n throw new Error('Non little-endian hardware is not supported');\nArray.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0'));\nfunction utf8ToBytes$3(str) {\n if (typeof str !== 'string') {\n throw new TypeError(`utf8ToBytes expected string, got ${typeof str}`);\n }\n return new TextEncoder().encode(str);\n}\nfunction toBytes$2(data) {\n if (typeof data === 'string')\n data = utf8ToBytes$3(data);\n if (!(data instanceof Uint8Array))\n throw new TypeError(`Expected input type is Uint8Array (got ${typeof data})`);\n return data;\n}\n/**\n * Concats Uint8Array-s into one; like `Buffer.concat([buf1, buf2])`\n * @example concatBytes(buf1, buf2)\n */\nfunction concatBytes$1(...arrays) {\n if (!arrays.every((a) => a instanceof Uint8Array))\n throw new Error('Uint8Array list expected');\n if (arrays.length === 1)\n return arrays[0];\n const length = arrays.reduce((a, arr) => a + arr.length, 0);\n const result = new Uint8Array(length);\n for (let i = 0, pad = 0; i < arrays.length; i++) {\n const arr = arrays[i];\n result.set(arr, pad);\n pad += arr.length;\n }\n return result;\n}\n// For runtime check if class implements interface\nlet Hash$2 = class Hash {\n // Safe version that clones internal state\n clone() {\n return this._cloneInto();\n }\n};\nfunction wrapConstructor$2(hashConstructor) {\n const hashC = (message) => hashConstructor().update(toBytes$2(message)).digest();\n const tmp = hashConstructor();\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = () => hashConstructor();\n return hashC;\n}\n/**\n * Secure PRNG. Uses `globalThis.crypto` or node.js crypto module.\n */\nfunction randomBytes(bytesLength = 32) {\n if (crypto$1 && typeof crypto$1.getRandomValues === 'function') {\n return crypto$1.getRandomValues(new Uint8Array(bytesLength));\n }\n throw new Error('crypto.getRandomValues must be defined');\n}\n\n// Polyfill for Safari 14\nfunction setBigUint64$2(view, byteOffset, value, isLE) {\n if (typeof view.setBigUint64 === 'function')\n return view.setBigUint64(byteOffset, value, isLE);\n const _32n = BigInt(32);\n const _u32_max = BigInt(0xffffffff);\n const wh = Number((value >> _32n) & _u32_max);\n const wl = Number(value & _u32_max);\n const h = isLE ? 4 : 0;\n const l = isLE ? 0 : 4;\n view.setUint32(byteOffset + h, wh, isLE);\n view.setUint32(byteOffset + l, wl, isLE);\n}\n// Base SHA2 class (RFC 6234)\nlet SHA2$2 = class SHA2 extends Hash$2 {\n constructor(blockLen, outputLen, padOffset, isLE) {\n super();\n this.blockLen = blockLen;\n this.outputLen = outputLen;\n this.padOffset = padOffset;\n this.isLE = isLE;\n this.finished = false;\n this.length = 0;\n this.pos = 0;\n this.destroyed = false;\n this.buffer = new Uint8Array(blockLen);\n this.view = createView$2(this.buffer);\n }\n update(data) {\n assert$4.exists(this);\n const { view, buffer, blockLen } = this;\n data = toBytes$2(data);\n const len = data.length;\n for (let pos = 0; pos < len;) {\n const take = Math.min(blockLen - this.pos, len - pos);\n // Fast path: we have at least one block in input, cast it to view and process\n if (take === blockLen) {\n const dataView = createView$2(data);\n for (; blockLen <= len - pos; pos += blockLen)\n this.process(dataView, pos);\n continue;\n }\n buffer.set(data.subarray(pos, pos + take), this.pos);\n this.pos += take;\n pos += take;\n if (this.pos === blockLen) {\n this.process(view, 0);\n this.pos = 0;\n }\n }\n this.length += data.length;\n this.roundClean();\n return this;\n }\n digestInto(out) {\n assert$4.exists(this);\n assert$4.output(out, this);\n this.finished = true;\n // Padding\n // We can avoid allocation of buffer for padding completely if it\n // was previously not allocated here. But it won't change performance.\n const { buffer, view, blockLen, isLE } = this;\n let { pos } = this;\n // append the bit '1' to the message\n buffer[pos++] = 0b10000000;\n this.buffer.subarray(pos).fill(0);\n // we have less than padOffset left in buffer, so we cannot put length in current block, need process it and pad again\n if (this.padOffset > blockLen - pos) {\n this.process(view, 0);\n pos = 0;\n }\n // Pad until full block byte with zeros\n for (let i = pos; i < blockLen; i++)\n buffer[i] = 0;\n // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that\n // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.\n // So we just write lowest 64 bits of that value.\n setBigUint64$2(view, blockLen - 8, BigInt(this.length * 8), isLE);\n this.process(view, 0);\n const oview = createView$2(out);\n const len = this.outputLen;\n // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT\n if (len % 4)\n throw new Error('_sha2: outputLen should be aligned to 32bit');\n const outLen = len / 4;\n const state = this.get();\n if (outLen > state.length)\n throw new Error('_sha2: outputLen bigger than state');\n for (let i = 0; i < outLen; i++)\n oview.setUint32(4 * i, state[i], isLE);\n }\n digest() {\n const { buffer, outputLen } = this;\n this.digestInto(buffer);\n const res = buffer.slice(0, outputLen);\n this.destroy();\n return res;\n }\n _cloneInto(to) {\n to || (to = new this.constructor());\n to.set(...this.get());\n const { blockLen, buffer, length, finished, destroyed, pos } = this;\n to.length = length;\n to.pos = pos;\n to.finished = finished;\n to.destroyed = destroyed;\n if (length % blockLen)\n to.buffer.set(buffer);\n return to;\n }\n};\n\n// Choice: a ? b : c\nconst Chi$2 = (a, b, c) => (a & b) ^ (~a & c);\n// Majority function, true if any two inpust is true\nconst Maj$2 = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);\n// Round constants:\n// first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)\n// prettier-ignore\nconst SHA256_K$2 = new Uint32Array([\n 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\n 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\n 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\n 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\n 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\n 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\n 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\n 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2\n]);\n// Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):\n// prettier-ignore\nconst IV$2 = new Uint32Array([\n 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19\n]);\n// Temporary buffer, not used to store anything between runs\n// Named this way because it matches specification.\nconst SHA256_W$2 = new Uint32Array(64);\nlet SHA256$2 = class SHA256 extends SHA2$2 {\n constructor() {\n super(64, 32, 8, false);\n // We cannot use array here since array allows indexing by variable\n // which means optimizer/compiler cannot use registers.\n this.A = IV$2[0] | 0;\n this.B = IV$2[1] | 0;\n this.C = IV$2[2] | 0;\n this.D = IV$2[3] | 0;\n this.E = IV$2[4] | 0;\n this.F = IV$2[5] | 0;\n this.G = IV$2[6] | 0;\n this.H = IV$2[7] | 0;\n }\n get() {\n const { A, B, C, D, E, F, G, H } = this;\n return [A, B, C, D, E, F, G, H];\n }\n // prettier-ignore\n set(A, B, C, D, E, F, G, H) {\n this.A = A | 0;\n this.B = B | 0;\n this.C = C | 0;\n this.D = D | 0;\n this.E = E | 0;\n this.F = F | 0;\n this.G = G | 0;\n this.H = H | 0;\n }\n process(view, offset) {\n // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array\n for (let i = 0; i < 16; i++, offset += 4)\n SHA256_W$2[i] = view.getUint32(offset, false);\n for (let i = 16; i < 64; i++) {\n const W15 = SHA256_W$2[i - 15];\n const W2 = SHA256_W$2[i - 2];\n const s0 = rotr$2(W15, 7) ^ rotr$2(W15, 18) ^ (W15 >>> 3);\n const s1 = rotr$2(W2, 17) ^ rotr$2(W2, 19) ^ (W2 >>> 10);\n SHA256_W$2[i] = (s1 + SHA256_W$2[i - 7] + s0 + SHA256_W$2[i - 16]) | 0;\n }\n // Compression function main loop, 64 rounds\n let { A, B, C, D, E, F, G, H } = this;\n for (let i = 0; i < 64; i++) {\n const sigma1 = rotr$2(E, 6) ^ rotr$2(E, 11) ^ rotr$2(E, 25);\n const T1 = (H + sigma1 + Chi$2(E, F, G) + SHA256_K$2[i] + SHA256_W$2[i]) | 0;\n const sigma0 = rotr$2(A, 2) ^ rotr$2(A, 13) ^ rotr$2(A, 22);\n const T2 = (sigma0 + Maj$2(A, B, C)) | 0;\n H = G;\n G = F;\n F = E;\n E = (D + T1) | 0;\n D = C;\n C = B;\n