diff --git a/js/payments/zapReceiptValidator.js b/js/payments/zapReceiptValidator.js index be9a3d99..675f0b89 100644 --- a/js/payments/zapReceiptValidator.js +++ b/js/payments/zapReceiptValidator.js @@ -3,9 +3,7 @@ import { nostrToolsReady } from "../nostrToolsBootstrap.js"; import { normalizeToolkitCandidate, readToolkitFromScope } from "../nostr/toolkit.js"; import { userLogger } from "../utils/logger.js"; -import { bech32 } from "@scure/base"; -import { sha256 } from "@noble/hashes/sha256"; -import { bytesToHex } from "@noble/hashes/utils"; +import { bech32, bytesToHex, sha256 } from "../../vendor/crypto-helpers.bundle.min.js"; const ZAP_RECEIPT_KIND = 9735; const BOLT11_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; diff --git a/vendor/crypto-helpers.LICENSE b/vendor/crypto-helpers.LICENSE new file mode 100644 index 00000000..dc2bc64d --- /dev/null +++ b/vendor/crypto-helpers.LICENSE @@ -0,0 +1,22 @@ +This bundle includes code from the following MIT-licensed projects: + +- @scure/base (c) 2022 Paul Miller (https://paulmillr.com) +- @noble/hashes (c) 2022 Paul Miller (https://paulmillr.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/vendor/crypto-helpers.bundle.min.js b/vendor/crypto-helpers.bundle.min.js new file mode 100644 index 00000000..27a5a43c --- /dev/null +++ b/vendor/crypto-helpers.bundle.min.js @@ -0,0 +1,10 @@ +/* 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;at?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<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<=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>25,r=(e&33554431)<<5;for(let n=0;n>n&1)===1&&(r^=P[n]);return r}function K(e,t,r=1){let n=e.length,o=1;for(let s=0;s126)throw new Error(`Invalid prefix (${e})`);o=L(o)^i>>5}o=L(o);for(let s=0;su)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.lengthe 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;re().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;io-i&&(this.process(n,0),i=0);for(let f=i;fc.length)throw new Error("_sha2: outputLen bigger than state");for(let f=0;fe&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) *) +*/ diff --git a/vendor/crypto-helpers.entry.js b/vendor/crypto-helpers.entry.js new file mode 100644 index 00000000..96b36172 --- /dev/null +++ b/vendor/crypto-helpers.entry.js @@ -0,0 +1,3 @@ +export { bech32 } from "@scure/base"; +export { sha256 } from "@noble/hashes/sha256"; +export { bytesToHex } from "@noble/hashes/utils";