Files
bitvid/vendor/crypto-helpers.bundle.min.js

11 lines
14 KiB
JavaScript

/* Bundled bech32 and noble hashes helpers for bitvid (MIT Licensed). Source modules: @scure/base, @noble/hashes. */
function m(e){if(!Number.isSafeInteger(e))throw new Error(`Wrong integer: ${e}`)}function w(...e){let t=(o,s)=>i=>o(s(i)),r=Array.from(e).reverse().reduce((o,s)=>o?t(o,s.encode):s.encode,void 0),n=e.reduce((o,s)=>o?t(o,s.decode):s.decode,void 0);return{encode:r,decode:n}}function x(e){return{encode:t=>{if(!Array.isArray(t)||t.length&&typeof t[0]!="number")throw new Error("alphabet.encode input should be an array of numbers");return t.map(r=>{if(m(r),r<0||r>=e.length)throw new Error(`Digit index outside alphabet: ${r} (alphabet: ${e.length})`);return e[r]})},decode:t=>{if(!Array.isArray(t)||t.length&&typeof t[0]!="string")throw new Error("alphabet.decode input should be array of strings");return t.map(r=>{if(typeof r!="string")throw new Error(`alphabet.decode: not string element=${r}`);let n=e.indexOf(r);if(n===-1)throw new Error(`Unknown letter: "${r}". Allowed: ${e}`);return n})}}}function y(e=""){if(typeof e!="string")throw new Error("join separator should be string");return{encode:t=>{if(!Array.isArray(t)||t.length&&typeof t[0]!="string")throw new Error("join.encode input should be array of strings");for(let r of t)if(typeof r!="string")throw new Error(`join.encode: non-string input=${r}`);return t.join(e)},decode:t=>{if(typeof t!="string")throw new Error("join.decode input should be string");return t.split(e)}}}function k(e,t="="){if(m(e),typeof t!="string")throw new Error("padding chr should be string");return{encode(r){if(!Array.isArray(r)||r.length&&typeof r[0]!="string")throw new Error("padding.encode input should be array of strings");for(let n of r)if(typeof n!="string")throw new Error(`padding.encode: non-string input=${n}`);for(;r.length*e%8;)r.push(t);return r},decode(r){if(!Array.isArray(r)||r.length&&typeof r[0]!="string")throw new Error("padding.encode input should be array of strings");for(let o of r)if(typeof o!="string")throw new Error(`padding.decode: non-string input=${o}`);let n=r.length;if(n*e%8)throw new Error("Invalid padding: string should have whole number of bytes");for(;n>0&&r[n-1]===t;n--)if(!((n-1)*e%8))throw new Error("Invalid padding: string has too much padding");return r.slice(0,n)}}}function z(e){if(typeof e!="function")throw new Error("normalize fn should be function");return{encode:t=>t,decode:t=>e(t)}}function G(e,t,r){if(t<2)throw new Error(`convertRadix: wrong from=${t}, base cannot be less than 2`);if(r<2)throw new Error(`convertRadix: wrong to=${r}, base cannot be less than 2`);if(!Array.isArray(e))throw new Error("convertRadix: data should be array");if(!e.length)return[];let n=0,o=[],s=Array.from(e);for(s.forEach(i=>{if(m(i),i<0||i>=t)throw new Error(`Wrong integer: ${i}`)});;){let i=0,h=!0;for(let a=n;a<s.length;a++){let d=s[a],c=t*i+d;if(!Number.isSafeInteger(c)||t*i/t!==i||c-d!==t*i)throw new Error("convertRadix: carry overflow");if(i=c%r,s[a]=Math.floor(c/r),!Number.isSafeInteger(s[a])||s[a]*r+i!==c)throw new Error("convertRadix: carry overflow");if(h)s[a]?h=!1:n=a;else continue}if(o.push(i),h)break}for(let i=0;i<e.length-1&&e[i]===0;i++)o.push(0);return o.reverse()}var X=(e,t)=>t?X(t,e%t):e,$=(e,t)=>e+(t-X(e,t));function I(e,t,r,n){if(!Array.isArray(e))throw new Error("convertRadix2: data should be array");if(t<=0||t>32)throw new Error(`convertRadix2: wrong from=${t}`);if(r<=0||r>32)throw new Error(`convertRadix2: wrong to=${r}`);if($(t,r)>32)throw new Error(`convertRadix2: carry overflow from=${t} to=${r} carryBits=${$(t,r)}`);let o=0,s=0,i=2**r-1,h=[];for(let a of e){if(m(a),a>=2**t)throw new Error(`convertRadix2: invalid data word=${a} from=${t}`);if(o=o<<t|a,s+t>32)throw new Error(`convertRadix2: carry overflow pos=${s} from=${t}`);for(s+=t;s>=r;s-=r)h.push((o>>s-r&i)>>>0);o&=2**s-1}if(o=o<<r-s&i,!n&&s>=t)throw new Error("Excess padding");if(!n&&o)throw new Error(`Non-zero padding: ${o}`);return n&&s>0&&h.push(o>>>0),h}function q(e){return m(e),{encode:t=>{if(!(t instanceof Uint8Array))throw new Error("radix.encode input should be Uint8Array");return G(Array.from(t),2**8,e)},decode:t=>{if(!Array.isArray(t)||t.length&&typeof t[0]!="number")throw new Error("radix.decode input should be array of strings");return Uint8Array.from(G(t,e,2**8))}}}function g(e,t=!1){if(m(e),e<=0||e>32)throw new Error("radix2: bits should be in (0..32]");if($(8,e)>32||$(e,8)>32)throw new Error("radix2: carry overflow");return{encode:r=>{if(!(r instanceof Uint8Array))throw new Error("radix2.encode input should be Uint8Array");return I(Array.from(r),8,e,!t)},decode:r=>{if(!Array.isArray(r)||r.length&&typeof r[0]!="number")throw new Error("radix2.decode input should be array of strings");return Uint8Array.from(I(r,e,8,t))}}}function V(e){if(typeof e!="function")throw new Error("unsafeWrapper fn should be function");return function(...t){try{return e.apply(null,t)}catch{}}}var Z=w(g(4),x("0123456789ABCDEF"),y("")),ee=w(g(5),x("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"),k(5),y("")),Ue=w(g(5),x("0123456789ABCDEFGHIJKLMNOPQRSTUV"),k(5),y("")),Le=w(g(5),x("0123456789ABCDEFGHJKMNPQRSTVWXYZ"),y(""),z(e=>e.toUpperCase().replace(/O/g,"0").replace(/[IL]/g,"1"))),te=w(g(6),x("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),k(6),y("")),re=w(g(6),x("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"),k(6),y("")),j=e=>w(q(58),x(e),y("")),R=j("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"),$e=j("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"),ke=j("rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"),M=[0,2,3,5,6,7,9,10,11],ne={encode(e){let t="";for(let r=0;r<e.length;r+=8){let n=e.subarray(r,r+8);t+=R.encode(n).padStart(M[n.length],"1")}return t},decode(e){let t=[];for(let r=0;r<e.length;r+=11){let n=e.slice(r,r+11),o=M.indexOf(n.length),s=R.decode(n);for(let i=0;i<s.length-o;i++)if(s[i]!==0)throw new Error("base58xmr: wrong padding");t=t.concat(Array.from(s.slice(s.length-o)))}return Uint8Array.from(t)}};var N=w(x("qpzry9x8gf2tvdw0s3jn54khce6mua7l"),y("")),P=[996825010,642813549,513874426,1027748829,705979059];function L(e){let t=e>>25,r=(e&33554431)<<5;for(let n=0;n<P.length;n++)(t>>n&1)===1&&(r^=P[n]);return r}function K(e,t,r=1){let n=e.length,o=1;for(let s=0;s<n;s++){let i=e.charCodeAt(s);if(i<33||i>126)throw new Error(`Invalid prefix (${e})`);o=L(o)^i>>5}o=L(o);for(let s=0;s<n;s++)o=L(o)^e.charCodeAt(s)&31;for(let s of t)o=L(o)^s;for(let s=0;s<6;s++)o=L(o);return o^=r,N.encode(I([o%2**30],30,5,!1))}function J(e){let t=e==="bech32"?1:734539939,r=g(5),n=r.decode,o=r.encode,s=V(n);function i(c,f,u=90){if(typeof c!="string")throw new Error(`bech32.encode prefix should be string, not ${typeof c}`);if(!Array.isArray(f)||f.length&&typeof f[0]!="number")throw new Error(`bech32.encode words should be array of numbers, not ${typeof f}`);let l=c.length+7+f.length;if(u!==!1&&l>u)throw new TypeError(`Length ${l} exceeds limit ${u}`);return c=c.toLowerCase(),`${c}1${N.encode(f)}${K(c,f,t)}`}function h(c,f=90){if(typeof c!="string")throw new Error(`bech32.decode input should be string, not ${typeof c}`);if(c.length<8||f!==!1&&c.length>f)throw new TypeError(`Wrong string length: ${c.length} (${c}). Expected (8..${f})`);let u=c.toLowerCase();if(c!==u&&c!==c.toUpperCase())throw new Error("String must be lowercase or uppercase");c=u;let l=c.lastIndexOf("1");if(l===0||l===-1)throw new Error('Letter "1" must be present between prefix and data only');let U=c.slice(0,l),A=c.slice(l+1);if(A.length<6)throw new Error("Data must be at least 6 characters long");let _=N.decode(A).slice(0,-6),F=K(U,_,t);if(!A.endsWith(F))throw new Error(`Invalid checksum in ${c}: expected "${F}"`);return{prefix:U,words:_}}let a=V(h);function d(c){let{prefix:f,words:u}=h(c,!1);return{prefix:f,words:u,bytes:n(u)}}return{encode:i,decode:h,decodeToBytes:d,decodeUnsafe:a,fromWords:n,fromWordsUnsafe:s,toWords:o}}var oe=J("bech32"),Be=J("bech32m"),se={encode:e=>new TextDecoder().decode(e),decode:e=>new TextEncoder().encode(e)},ie=w(g(4),x("0123456789abcdef"),y(""),z(e=>{if(typeof e!="string"||e.length%2)throw new TypeError(`hex.decode: expected string, got ${typeof e} with length ${e.length}`);return e.toLowerCase()})),ce={utf8:se,hex:ie,base16:Z,base32:ee,base64:te,base64url:re,base58:R,base58xmr:ne},Te=`Invalid encoding type. Available types: ${Object.keys(ce).join(", ")}`;function D(e){if(!Number.isSafeInteger(e)||e<0)throw new Error(`Wrong positive integer: ${e}`)}function fe(e){if(typeof e!="boolean")throw new Error(`Expected boolean, not ${e}`)}function Q(e,...t){if(!(e instanceof Uint8Array))throw new Error("Expected Uint8Array");if(t.length>0&&!t.includes(e.length))throw new Error(`Expected Uint8Array of length ${t}, not of length=${e.length}`)}function he(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash should be wrapped by utils.wrapConstructor");D(e.outputLen),D(e.blockLen)}function ae(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function de(e,t){Q(e);let r=t.outputLen;if(e.length<r)throw new Error(`digestInto() expects output buffer of length at least ${r}`)}var ue={number:D,bool:fe,bytes:Q,hash:he,exists:ae,output:de},B=ue;var le=typeof globalThis=="object"&&"crypto"in globalThis?globalThis.crypto:void 0;var Y=e=>e instanceof Uint8Array;var v=e=>new DataView(e.buffer,e.byteOffset,e.byteLength),p=(e,t)=>e<<32-t|e>>>t,pe=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;if(!pe)throw new Error("Non little-endian hardware is not supported");var we=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function xe(e){if(!Y(e))throw new Error("Uint8Array expected");let t="";for(let r=0;r<e.length;r++)t+=we[e[r]];return t}function ye(e){if(typeof e!="string")throw new Error(`utf8ToBytes expected string, got ${typeof e}`);return new Uint8Array(new TextEncoder().encode(e))}function S(e){if(typeof e=="string"&&(e=ye(e)),!Y(e))throw new Error(`expected Uint8Array, got ${typeof e}`);return e}var T=class{clone(){return this._cloneInto()}};function W(e){let t=n=>e().update(S(n)).digest(),r=e();return t.outputLen=r.outputLen,t.blockLen=r.blockLen,t.create=()=>e(),t}function ge(e,t,r,n){if(typeof e.setBigUint64=="function")return e.setBigUint64(t,r,n);let o=BigInt(32),s=BigInt(4294967295),i=Number(r>>o&s),h=Number(r&s),a=n?4:0,d=n?0:4;e.setUint32(t+a,i,n),e.setUint32(t+d,h,n)}var C=class extends T{constructor(t,r,n,o){super(),this.blockLen=t,this.outputLen=r,this.padOffset=n,this.isLE=o,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(t),this.view=v(this.buffer)}update(t){B.exists(this);let{view:r,buffer:n,blockLen:o}=this;t=S(t);let s=t.length;for(let i=0;i<s;){let h=Math.min(o-this.pos,s-i);if(h===o){let a=v(t);for(;o<=s-i;i+=o)this.process(a,i);continue}n.set(t.subarray(i,i+h),this.pos),this.pos+=h,i+=h,this.pos===o&&(this.process(r,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){B.exists(this),B.output(t,this),this.finished=!0;let{buffer:r,view:n,blockLen:o,isLE:s}=this,{pos:i}=this;r[i++]=128,this.buffer.subarray(i).fill(0),this.padOffset>o-i&&(this.process(n,0),i=0);for(let f=i;f<o;f++)r[f]=0;ge(n,o-8,BigInt(this.length*8),s),this.process(n,0);let h=v(t),a=this.outputLen;if(a%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let d=a/4,c=this.get();if(d>c.length)throw new Error("_sha2: outputLen bigger than state");for(let f=0;f<d;f++)h.setUint32(4*f,c[f],s)}digest(){let{buffer:t,outputLen:r}=this;this.digestInto(t);let n=t.slice(0,r);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());let{blockLen:r,buffer:n,length:o,finished:s,destroyed:i,pos:h}=this;return t.length=o,t.pos=h,t.finished=s,t.destroyed=i,o%r&&t.buffer.set(n),t}};var be=(e,t,r)=>e&t^~e&r,Ee=(e,t,r)=>e&t^e&r^t&r,Ae=new Uint32Array([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]),b=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),E=new Uint32Array(64),O=class extends C{constructor(){super(64,32,8,!1),this.A=b[0]|0,this.B=b[1]|0,this.C=b[2]|0,this.D=b[3]|0,this.E=b[4]|0,this.F=b[5]|0,this.G=b[6]|0,this.H=b[7]|0}get(){let{A:t,B:r,C:n,D:o,E:s,F:i,G:h,H:a}=this;return[t,r,n,o,s,i,h,a]}set(t,r,n,o,s,i,h,a){this.A=t|0,this.B=r|0,this.C=n|0,this.D=o|0,this.E=s|0,this.F=i|0,this.G=h|0,this.H=a|0}process(t,r){for(let f=0;f<16;f++,r+=4)E[f]=t.getUint32(r,!1);for(let f=16;f<64;f++){let u=E[f-15],l=E[f-2],U=p(u,7)^p(u,18)^u>>>3,A=p(l,17)^p(l,19)^l>>>10;E[f]=A+E[f-7]+U+E[f-16]|0}let{A:n,B:o,C:s,D:i,E:h,F:a,G:d,H:c}=this;for(let f=0;f<64;f++){let u=p(h,6)^p(h,11)^p(h,25),l=c+u+be(h,a,d)+Ae[f]+E[f]|0,A=(p(n,2)^p(n,13)^p(n,22))+Ee(n,o,s)|0;c=d,d=a,a=h,h=i+l|0,i=s,s=o,o=n,n=l+A|0}n=n+this.A|0,o=o+this.B|0,s=s+this.C|0,i=i+this.D|0,h=h+this.E|0,a=a+this.F|0,d=d+this.G|0,c=c+this.H|0,this.set(n,o,s,i,h,a,d,c)}roundClean(){E.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}},H=class extends O{constructor(){super(),this.A=-1056596264,this.B=914150663,this.C=812702999,this.D=-150054599,this.E=-4191439,this.F=1750603025,this.G=1694076839,this.H=-1090891868,this.outputLen=28}},me=W(()=>new O),He=W(()=>new H);export{oe as bech32,xe as bytesToHex,me as sha256};
/*! Bundled license information:
@scure/base/lib/esm/index.js:
(*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
@noble/hashes/esm/utils.js:
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
*/