alby-tools
Advanced tools
Comparing version 2.2.1 to 2.3.0
@@ -1,2 +0,2 @@ | ||
import t from"cross-fetch";import e from"crypto-js/enc-hex.js";import a from"crypto-js/sha256.js";import{decode as r}from"light-bolt11-decoder";class s{constructor(t){this.storage=void 0,this.storage=t||{}}getItem(t){return this.storage[t]}setItem(t,e){this.storage[t]=e}}const n=async(e,a,r)=>{r||(r={});const n=r.webln||globalThis.webln;if(!n)throw new Error("WebLN is missing");let o=r.store||new s;a||(a={}),a.cache="no-store",a.mode="cors",a.headers||(a.headers={});const i=o.getItem(e);if(i){const r=JSON.parse(i);return a.headers.Authorization=`LSAT ${r.mac}:${r.preimage}`,await t(e,a)}a.headers["Accept-Authenticate"]="LSAT";const l=await t(e,a),c=l.headers.get("www-authenticate");if(!c)return l;const h=c.split(","),u=h[0].replace("LSAT macaroon=","").trim(),m=h[1].replace("invoice=","").trim();await n.enable();const p=await n.sendPayment(m);return o.setItem(e,JSON.stringify({mac:u,preimage:p.preimage})),a.headers.Authorization=`LSAT ${u}:${p.preimage}`,await t(e,a)};var o={__proto__:null,fetchWithLsat:n,default:n};const i=async(t,e)=>{let{boost:a,amount:r}=t;e||(e={});const s=e.webln||globalThis.webln;r||(r=Math.floor(a.value_msat/1e3));let n={destination:t.destination,amount:r,customRecords:{7629169:JSON.stringify(a)}};return t.customKey&&t.customValue&&(n.customRecords[t.customKey]=t.customValue),await s.enable(),await s.keysend(n)};var l={__proto__:null,boost:i,default:i};function c(){return c=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var a=arguments[e];for(var r in a)Object.prototype.hasOwnProperty.call(a,r)&&(t[r]=a[r])}return t},c.apply(this,arguments)}const h=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,u=t=>!!t&&h.test(t),m=({amount:t,min:e,max:a})=>{const r=t>0&&t>=e&&t<=a;return r&&e===a?t===e:r};class p{constructor(t){var e,a;this.paymentRequest=void 0,this.paymentHash=void 0,this.preimage=void 0,this.verify=void 0,this.paymentRequest=t.pr,this.paymentHash=(t=>{if(!t)return null;try{const e=r(t);if(!e||!e.sections)return null;const a=e.sections.find(t=>"payment_hash"===t.name);return a&&a.value?a.value.toString():null}catch(t){return null}})(this.paymentRequest),this.verify=null!=(e=t.verify)?e:null,this.preimage=null!=(a=t.preimage)?a:null}async isPaid(){if(this.preimage)return this.validatePreimage(this.preimage);if(this.verify)return await this.verifyPayment();throw new Error("Could not verify payment")}validatePreimage(t){if(!t||!this.paymentHash)return!1;try{const r=a(e.parse(t)).toString(e);return this.paymentHash===r}catch(t){return!1}}async verifyPayment(){if(!this.verify)throw new Error("LNURL verify not available");const e=await t(this.verify),a=await e.json();return a.preimage&&(this.preimage=a.preimage),a.settled}}async function w({satoshi:t,comment:e,p:a,e:r,relays:s},n={}){const o=n.nostr||globalThis.nostr;if(!o)throw new Error("nostr option or window.nostr is not available");const i=[["relays",...s],["amount",t.toString()]];a&&i.push(["p",a]),r&&i.push(["e",r]);const l={pubkey:await o.getPublicKey(),created_at:Math.floor(Date.now()/1e3),kind:9734,tags:i,content:null!=e?e:""};return l.id=f(l),await o.signEvent(l)}function y(t){if("string"!=typeof t.content)return!1;if("number"!=typeof t.created_at)return!1;if(!Array.isArray(t.tags))return!1;for(let e=0;e<t.tags.length;e++){const a=t.tags[e];if(!Array.isArray(a))return!1;for(let t=0;t<a.length;t++)if("object"==typeof a[t])return!1}return!0}function d(t){if(!y(t))throw new Error("can't serialize event with wrong or missing properties");return JSON.stringify([0,t.pubkey,t.created_at,t.kind,t.tags,t.content])}function f(t){return a(d(t)).toString(e)}function g(t,e){let a,r;var s,n;return e&&t&&(a=null==(s=t.names)?void 0:s[e],r=a?null==(n=t.relays)?void 0:n[a]:void 0),[t,a,r]}var v={__proto__:null,generateZapEvent:w,validateEvent:y,serializeEvent:d,getEventHash:f,parseNostrResponse:g};const b=/^((?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@((?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;class k{constructor(t,e){this.address=void 0,this.options=void 0,this.username=void 0,this.domain=void 0,this.pubkey=void 0,this.lnurlpData=void 0,this.keysendData=void 0,this.nostrData=void 0,this.nostrPubkey=void 0,this.nostrRelays=void 0,this.webln=void 0,this.address=t,this.options={proxy:"https://lnaddressproxy.getalby.com",webln:globalThis.webln},this.options=Object.assign(this.options,e),this.parse(),this.webln=this.options.webln}parse(){const t=b.exec(this.address.toLowerCase());t&&(this.username=t[1],this.domain=t[2])}async fetch(){return this.options.proxy?this.fetchWithProxy():this.fetchWithoutProxy()}async fetchWithProxy(){const e=await t(`${this.options.proxy}/lightning-address-details?${new URLSearchParams({ln:this.address}).toString()}`),a=await e.json();this.parseResponse(a.lnurlp,a.keysend,a.nostr)}async fetchWithoutProxy(){if(!this.domain||!this.username)return;const e=await t(this.lnurlpUrl()),a=await t(this.keysendUrl()),r=await t(this.nostrUrl());let s,n,o;e.ok&&(s=await e.json()),a.ok&&(n=await a.json()),r.ok&&(o=await r.json()),this.parseResponse(s,n,o)}lnurlpUrl(){return`https://${this.domain}/.well-known/lnurlp/${this.username}`}keysendUrl(){return`https://${this.domain}/.well-known/keysend/${this.username}`}nostrUrl(){return`https://${this.domain}/.well-known/nostr.json?name=${this.username}`}async generateInvoice(e){let a;if(this.options.proxy){const r=await t(`${this.options.proxy}/generate-invoice?${new URLSearchParams(c({ln:this.address},e)).toString()}`);a=(await r.json()).invoice}else{if(!this.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!this.lnurlpData.callback||!u(this.lnurlpData.callback))throw new Error("Valid callback does not exist in lnurlpData");const r=new URL(this.lnurlpData.callback);r.search=new URLSearchParams(e).toString();const s=await t(r);a=await s.json()}const r=a&&a.pr&&a.pr.toString();if(!r)throw new Error("Invalid pay service invoice");const s={pr:r};return a&&a.verify&&(s.verify=a.verify.toString()),new p(s)}async requestInvoice(t){if(!this.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");const e=1e3*t.satoshi,{commentAllowed:a,min:r,max:s}=this.lnurlpData;if(!m({amount:e,min:r,max:s}))throw new Error("Invalid amount");if(t.comment&&a&&a>0&&t.comment.length>a)throw new Error(`The comment length must be ${a} characters or fewer`);const n={amount:e.toString()};return t.comment&&(n.comment=t.comment),t.payerdata&&(n.payerdata=JSON.stringify(t.payerdata)),this.generateInvoice(n)}async boost(t,e=0){if(!this.keysendData)throw new Error("No keysendData available. Please call fetch() first.");const{destination:a,customKey:r,customValue:s}=this.keysendData;return i({destination:a,customKey:r,customValue:s,amount:e,boost:t},{webln:this.webln})}async zapInvoice({satoshi:t,comment:e,relays:a,e:r},s={}){if(!this.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!this.nostrPubkey)throw new Error("Nostr Pubkey is missing");const n=this.nostrPubkey,o=1e3*t,{allowsNostr:i,min:l,max:c}=this.lnurlpData;if(!m({amount:o,min:l,max:c}))throw new Error("Invalid amount");if(!i)throw new Error("Your provider does not support zaps");const h=await w({satoshi:o,comment:e,p:n,e:r,relays:a},s),u={amount:o.toString(),nostr:JSON.stringify(h)};return await this.generateInvoice(u)}async zap(t,e={}){const a=this.zapInvoice(t,e);if(!this.webln)throw new Error("WebLN not available");return await this.webln.enable(),this.webln.sendPayment((await a).paymentRequest)}parseResponse(t,r,s){t&&(this.lnurlpData=(t=>{if("payRequest"!==t.tag)throw new Error("Invalid pay service params");const r=(t.callback+"").trim();if(!u(r))throw new Error("Callback must be a valid url");const s=Math.ceil(Number(t.minSendable||0)),n=Math.floor(Number(t.maxSendable));if(!s||!n||s>n)throw new Error("Invalid pay service params");let o,i;try{o=JSON.parse(t.metadata+""),i=a(t.metadata+"").toString(e)}catch(t){o=[],i=a("[]").toString(e)}let l="",c="",h="";for(let t=0;t<o.length;t++){const[e,a]=o[t];switch(e){case"text/plain":c=a;break;case"text/identifier":h=a;break;case"image/png;base64":case"image/jpeg;base64":l="data:"+e+","+a}}let m,p=t.payerData;try{m=new URL(r).hostname}catch(t){}return{callback:r,fixed:s===n,min:s,max:n,domain:m,metadata:o,metadataHash:i,identifier:h,description:c,image:l,payerData:p,commentAllowed:Number(t.commentAllowed)||0,rawData:t,allowsNostr:t.allowsNostr||!1}})(t)),r&&(this.keysendData=(t=>{if("keysend"!==t.tag)throw new Error("Invalid keysend params");if("OK"!==t.status)throw new Error("Keysend status not OK");if(!("customKey"in t.customData[0])||"696969"!=t.customData[0].customKey)throw new Error("Unable to find customKey");if(!("customValue"in t.customData[0])||!t.customData[0].customValue)throw new Error("Unable to find customValue");if(!t.pubkey)throw new Error("Pubkey does not exist");return{destination:t.pubkey,customKey:t.customData[0].customKey,customValue:t.customData[0].customValue}})(r)),s&&([this.nostrData,this.nostrPubkey,this.nostrRelays]=g(s,this.username))}}const D=async e=>{const a="https://getalby.com/api/rates/"+e.toLowerCase()+".json",r=await t(a);return(await r.json()).rate_float/1e8},S=async({satoshi:t,currency:e})=>{const a=await D(e);return Number(t)*a};var E={__proto__:null,getFiatBtcRate:D,getFiatValue:S,getSatoshiValue:async({amount:t,currency:e})=>{const a=await D(e);return Math.floor(Number(t)/a)},getFormattedFiatValue:async({satoshi:t,currency:e,locale:a})=>(a||(a="en"),(await S({satoshi:t,currency:e})).toLocaleString(a,{style:"currency",currency:e}))};export{p as Invoice,k as LightningAddress,l as boostagrams,n as fetchWithLsat,E as fiat,o as lsat,v as nostr,i as sendBoostagram}; | ||
import t from"cross-fetch";import e from"crypto-js/enc-hex.js";import a from"crypto-js/sha256.js";import{decode as r}from"light-bolt11-decoder";class s{constructor(t){this.storage=void 0,this.storage=t||{}}getItem(t){return this.storage[t]}setItem(t,e){this.storage[t]=e}}var n={__proto__:null,MemoryStorage:s,NoStorage:class{constructor(t){}getItem(t){return null}setItem(t,e){}},default:s};const o=new s,i=async(e,a,r)=>{r||(r={});const s=r.webln||globalThis.webln;if(!s)throw new Error("WebLN is missing");let n=r.store||o;a||(a={}),a.cache="no-store",a.mode="cors",a.headers||(a.headers={});const i=n.getItem(e);if(i){const r=JSON.parse(i);return a.headers.Authorization=`LSAT ${r.mac}:${r.preimage}`,await t(e,a)}a.headers["Accept-Authenticate"]="LSAT";const l=await t(e,a),c=l.headers.get("www-authenticate");if(!c)return l;const h=c.split(","),u=h[0].replace("LSAT macaroon=","").trim(),m=h[1].replace("invoice=","").trim();await s.enable();const p=await s.sendPayment(m);return n.setItem(e,JSON.stringify({mac:u,preimage:p.preimage})),a.headers.Authorization=`LSAT ${u}:${p.preimage}`,await t(e,a)};var l={__proto__:null,Storage:n,fetchWithLsat:i,default:i};const c=async(t,e)=>{let{boost:a,amount:r}=t;e||(e={});const s=e.webln||globalThis.webln;r||(r=Math.floor(a.value_msat/1e3));let n={destination:t.destination,amount:r,customRecords:{7629169:JSON.stringify(a)}};return t.customKey&&t.customValue&&(n.customRecords[t.customKey]=t.customValue),await s.enable(),await s.keysend(n)};var h={__proto__:null,boost:c,default:c};function u(){return u=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var a=arguments[e];for(var r in a)Object.prototype.hasOwnProperty.call(a,r)&&(t[r]=a[r])}return t},u.apply(this,arguments)}const m=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,p=t=>!!t&&m.test(t),w=({amount:t,min:e,max:a})=>{const r=t>0&&t>=e&&t<=a;return r&&e===a?t===e:r};class y{constructor(t){var e,a;this.paymentRequest=void 0,this.paymentHash=void 0,this.preimage=void 0,this.verify=void 0,this.paymentRequest=t.pr,this.paymentHash=(t=>{if(!t)return null;try{const e=r(t);if(!e||!e.sections)return null;const a=e.sections.find(t=>"payment_hash"===t.name);return a&&a.value?a.value.toString():null}catch(t){return null}})(this.paymentRequest),this.verify=null!=(e=t.verify)?e:null,this.preimage=null!=(a=t.preimage)?a:null}async isPaid(){if(this.preimage)return this.validatePreimage(this.preimage);if(this.verify)return await this.verifyPayment();throw new Error("Could not verify payment")}validatePreimage(t){if(!t||!this.paymentHash)return!1;try{const r=a(e.parse(t)).toString(e);return this.paymentHash===r}catch(t){return!1}}async verifyPayment(){if(!this.verify)throw new Error("LNURL verify not available");const e=await t(this.verify),a=await e.json();return a.preimage&&(this.preimage=a.preimage),a.settled}}async function d({satoshi:t,comment:e,p:a,e:r,relays:s},n={}){const o=n.nostr||globalThis.nostr;if(!o)throw new Error("nostr option or window.nostr is not available");const i=[["relays",...s],["amount",t.toString()]];a&&i.push(["p",a]),r&&i.push(["e",r]);const l={pubkey:await o.getPublicKey(),created_at:Math.floor(Date.now()/1e3),kind:9734,tags:i,content:null!=e?e:""};return l.id=v(l),await o.signEvent(l)}function f(t){if("string"!=typeof t.content)return!1;if("number"!=typeof t.created_at)return!1;if(!Array.isArray(t.tags))return!1;for(let e=0;e<t.tags.length;e++){const a=t.tags[e];if(!Array.isArray(a))return!1;for(let t=0;t<a.length;t++)if("object"==typeof a[t])return!1}return!0}function g(t){if(!f(t))throw new Error("can't serialize event with wrong or missing properties");return JSON.stringify([0,t.pubkey,t.created_at,t.kind,t.tags,t.content])}function v(t){return a(g(t)).toString(e)}function b(t,e){let a,r;var s,n;return e&&t&&(a=null==(s=t.names)?void 0:s[e],r=a?null==(n=t.relays)?void 0:n[a]:void 0),[t,a,r]}var k={__proto__:null,generateZapEvent:d,validateEvent:f,serializeEvent:g,getEventHash:v,parseNostrResponse:b};const S=/^((?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@((?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;class D{constructor(t,e){this.address=void 0,this.options=void 0,this.username=void 0,this.domain=void 0,this.pubkey=void 0,this.lnurlpData=void 0,this.keysendData=void 0,this.nostrData=void 0,this.nostrPubkey=void 0,this.nostrRelays=void 0,this.webln=void 0,this.address=t,this.options={proxy:"https://lnaddressproxy.getalby.com",webln:globalThis.webln},this.options=Object.assign(this.options,e),this.parse(),this.webln=this.options.webln}parse(){const t=S.exec(this.address.toLowerCase());t&&(this.username=t[1],this.domain=t[2])}async fetch(){return this.options.proxy?this.fetchWithProxy():this.fetchWithoutProxy()}async fetchWithProxy(){const e=await t(`${this.options.proxy}/lightning-address-details?${new URLSearchParams({ln:this.address}).toString()}`),a=await e.json();this.parseResponse(a.lnurlp,a.keysend,a.nostr)}async fetchWithoutProxy(){if(!this.domain||!this.username)return;const e=await t(this.lnurlpUrl()),a=await t(this.keysendUrl()),r=await t(this.nostrUrl());let s,n,o;e.ok&&(s=await e.json()),a.ok&&(n=await a.json()),r.ok&&(o=await r.json()),this.parseResponse(s,n,o)}lnurlpUrl(){return`https://${this.domain}/.well-known/lnurlp/${this.username}`}keysendUrl(){return`https://${this.domain}/.well-known/keysend/${this.username}`}nostrUrl(){return`https://${this.domain}/.well-known/nostr.json?name=${this.username}`}async generateInvoice(e){let a;if(this.options.proxy){const r=await t(`${this.options.proxy}/generate-invoice?${new URLSearchParams(u({ln:this.address},e)).toString()}`);a=(await r.json()).invoice}else{if(!this.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!this.lnurlpData.callback||!p(this.lnurlpData.callback))throw new Error("Valid callback does not exist in lnurlpData");const r=new URL(this.lnurlpData.callback);r.search=new URLSearchParams(e).toString();const s=await t(r);a=await s.json()}const r=a&&a.pr&&a.pr.toString();if(!r)throw new Error("Invalid pay service invoice");const s={pr:r};return a&&a.verify&&(s.verify=a.verify.toString()),new y(s)}async requestInvoice(t){if(!this.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");const e=1e3*t.satoshi,{commentAllowed:a,min:r,max:s}=this.lnurlpData;if(!w({amount:e,min:r,max:s}))throw new Error("Invalid amount");if(t.comment&&a&&a>0&&t.comment.length>a)throw new Error(`The comment length must be ${a} characters or fewer`);const n={amount:e.toString()};return t.comment&&(n.comment=t.comment),t.payerdata&&(n.payerdata=JSON.stringify(t.payerdata)),this.generateInvoice(n)}async boost(t,e=0){if(!this.keysendData)throw new Error("No keysendData available. Please call fetch() first.");const{destination:a,customKey:r,customValue:s}=this.keysendData;return c({destination:a,customKey:r,customValue:s,amount:e,boost:t},{webln:this.webln})}async zapInvoice({satoshi:t,comment:e,relays:a,e:r},s={}){if(!this.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!this.nostrPubkey)throw new Error("Nostr Pubkey is missing");const n=this.nostrPubkey,o=1e3*t,{allowsNostr:i,min:l,max:c}=this.lnurlpData;if(!w({amount:o,min:l,max:c}))throw new Error("Invalid amount");if(!i)throw new Error("Your provider does not support zaps");const h=await d({satoshi:o,comment:e,p:n,e:r,relays:a},s),u={amount:o.toString(),nostr:JSON.stringify(h)};return await this.generateInvoice(u)}async zap(t,e={}){const a=this.zapInvoice(t,e);if(!this.webln)throw new Error("WebLN not available");return await this.webln.enable(),this.webln.sendPayment((await a).paymentRequest)}parseResponse(t,r,s){t&&(this.lnurlpData=(t=>{if("payRequest"!==t.tag)throw new Error("Invalid pay service params");const r=(t.callback+"").trim();if(!p(r))throw new Error("Callback must be a valid url");const s=Math.ceil(Number(t.minSendable||0)),n=Math.floor(Number(t.maxSendable));if(!s||!n||s>n)throw new Error("Invalid pay service params");let o,i;try{o=JSON.parse(t.metadata+""),i=a(t.metadata+"").toString(e)}catch(t){o=[],i=a("[]").toString(e)}let l="",c="",h="";for(let t=0;t<o.length;t++){const[e,a]=o[t];switch(e){case"text/plain":c=a;break;case"text/identifier":h=a;break;case"image/png;base64":case"image/jpeg;base64":l="data:"+e+","+a}}let u,m=t.payerData;try{u=new URL(r).hostname}catch(t){}return{callback:r,fixed:s===n,min:s,max:n,domain:u,metadata:o,metadataHash:i,identifier:h,description:c,image:l,payerData:m,commentAllowed:Number(t.commentAllowed)||0,rawData:t,allowsNostr:t.allowsNostr||!1}})(t)),r&&(this.keysendData=(t=>{if("keysend"!==t.tag)throw new Error("Invalid keysend params");if("OK"!==t.status)throw new Error("Keysend status not OK");if(!("customKey"in t.customData[0])||"696969"!=t.customData[0].customKey)throw new Error("Unable to find customKey");if(!("customValue"in t.customData[0])||!t.customData[0].customValue)throw new Error("Unable to find customValue");if(!t.pubkey)throw new Error("Pubkey does not exist");return{destination:t.pubkey,customKey:t.customData[0].customKey,customValue:t.customData[0].customValue}})(r)),s&&([this.nostrData,this.nostrPubkey,this.nostrRelays]=b(s,this.username))}}const E=async e=>{const a="https://getalby.com/api/rates/"+e.toLowerCase()+".json",r=await t(a);return(await r.json()).rate_float/1e8},_=async({satoshi:t,currency:e})=>{const a=await E(e);return Number(t)*a};var P={__proto__:null,getFiatBtcRate:E,getFiatValue:_,getSatoshiValue:async({amount:t,currency:e})=>{const a=await E(e);return Math.floor(Number(t)/a)},getFormattedFiatValue:async({satoshi:t,currency:e,locale:a})=>(a||(a="en"),(await _({satoshi:t,currency:e})).toLocaleString(a,{style:"currency",currency:e}))};export{y as Invoice,D as LightningAddress,h as boostagrams,i as fetchWithLsat,P as fiat,l as lsat,k as nostr,c as sendBoostagram}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
import e from"cross-fetch";import r from"crypto-js/enc-hex.js";import t from"crypto-js/sha256.js";import{decode as n}from"light-bolt11-decoder";var o=/*#__PURE__*/function(){function e(e){this.storage=void 0,this.storage=e||{}}var r=e.prototype;return r.getItem=function(e){return this.storage[e]},r.setItem=function(e,r){this.storage[e]=r},e}(),a=function(r,t,n){try{var a,i=function(n){return a?n:(t.headers["Accept-Authenticate"]="LSAT",Promise.resolve(e(r,t)).then(function(n){var o=n.headers.get("www-authenticate");if(!o)return n;var a=o.split(","),i=a[0].replace("LSAT macaroon=","").trim(),c=a[1].replace("invoice=","").trim();return Promise.resolve(s.enable()).then(function(){return Promise.resolve(s.sendPayment(c)).then(function(n){return u.setItem(r,JSON.stringify({mac:i,preimage:n.preimage})),t.headers.Authorization="LSAT "+i+":"+n.preimage,Promise.resolve(e(r,t))})})}))};n||(n={});var s=n.webln||globalThis.webln;if(!s)throw new Error("WebLN is missing");var u=n.store||new o;t||(t={}),t.cache="no-store",t.mode="cors",t.headers||(t.headers={});var c=u.getItem(r),l=function(){if(c){var n=JSON.parse(c);return t.headers.Authorization="LSAT "+n.mac+":"+n.preimage,Promise.resolve(e(r,t)).then(function(e){return a=1,e})}}();return Promise.resolve(l&&l.then?l.then(i):i(l))}catch(e){return Promise.reject(e)}},i={__proto__:null,fetchWithLsat:a,default:a},s=function(e,r){try{var t=e.boost,n=e.amount;r||(r={});var o=r.webln||globalThis.webln;n||(n=Math.floor(t.value_msat/1e3));var a={destination:e.destination,amount:n,customRecords:{7629169:JSON.stringify(t)}};return e.customKey&&e.customValue&&(a.customRecords[e.customKey]=e.customValue),Promise.resolve(o.enable()).then(function(){return Promise.resolve(o.keysend(a))})}catch(e){return Promise.reject(e)}},u={__proto__:null,boost:s,default:s};function c(){return c=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},c.apply(this,arguments)}var l=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,m=function(e){return!!e&&l.test(e)},h=function(e){var r=e.amount,t=e.min,n=e.max,o=r>0&&r>=t&&r<=n;return o&&t===n?r===t:o},v=/*#__PURE__*/function(){function o(e){var r,t;this.paymentRequest=void 0,this.paymentHash=void 0,this.preimage=void 0,this.verify=void 0,this.paymentRequest=e.pr,this.paymentHash=function(e){if(!e)return null;try{var r=n(e);if(!r||!r.sections)return null;var t=r.sections.find(function(e){return"payment_hash"===e.name});return t&&t.value?t.value.toString():null}catch(e){return null}}(this.paymentRequest),this.verify=null!=(r=e.verify)?r:null,this.preimage=null!=(t=e.preimage)?t:null}var a=o.prototype;return a.isPaid=function(){try{var e=this;if(e.preimage)return Promise.resolve(e.validatePreimage(e.preimage));if(e.verify)return Promise.resolve(e.verifyPayment());throw new Error("Could not verify payment")}catch(e){return Promise.reject(e)}},a.validatePreimage=function(e){if(!e||!this.paymentHash)return!1;try{var n=t(r.parse(e)).toString(r);return this.paymentHash===n}catch(e){return!1}},a.verifyPayment=function(){try{var r=this;if(!r.verify)throw new Error("LNURL verify not available");return Promise.resolve(e(r.verify)).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.preimage&&(r.preimage=e.preimage),e.settled})})}catch(e){return Promise.reject(e)}},o}(),f=function(e,r){var t=e.satoshi,n=e.comment,o=e.p,a=e.e,i=e.relays;void 0===r&&(r={});try{var s=r.nostr||globalThis.nostr;if(!s)throw new Error("nostr option or window.nostr is not available");var u=[["relays"].concat(i),["amount",t.toString()]];return o&&u.push(["p",o]),a&&u.push(["e",a]),Promise.resolve(s.getPublicKey()).then(function(e){var r={pubkey:e,created_at:Math.floor(Date.now()/1e3),kind:9734,tags:u,content:null!=n?n:""};return r.id=d(r),Promise.resolve(s.signEvent(r))})}catch(a){return Promise.reject(a)}};function p(e){if("string"!=typeof e.content)return!1;if("number"!=typeof e.created_at)return!1;if(!Array.isArray(e.tags))return!1;for(var r=0;r<e.tags.length;r++){var t=e.tags[r];if(!Array.isArray(t))return!1;for(var n=0;n<t.length;n++)if("object"==typeof t[n])return!1}return!0}function y(e){if(!p(e))throw new Error("can't serialize event with wrong or missing properties");return JSON.stringify([0,e.pubkey,e.created_at,e.kind,e.tags,e.content])}function d(e){return t(y(e)).toString(r)}function w(e,r){var t,n,o,a;return r&&e&&(n=(t=null==(o=e.names)?void 0:o[r])?null==(a=e.relays)?void 0:a[t]:void 0),[e,t,n]}var g={__proto__:null,generateZapEvent:f,validateEvent:p,serializeEvent:y,getEventHash:d,parseNostrResponse:w},P=/^((?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@((?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,b=/*#__PURE__*/function(){function n(e,r){this.address=void 0,this.options=void 0,this.username=void 0,this.domain=void 0,this.pubkey=void 0,this.lnurlpData=void 0,this.keysendData=void 0,this.nostrData=void 0,this.nostrPubkey=void 0,this.nostrRelays=void 0,this.webln=void 0,this.address=e,this.options={proxy:"https://lnaddressproxy.getalby.com",webln:globalThis.webln},this.options=Object.assign(this.options,r),this.parse(),this.webln=this.options.webln}var o=n.prototype;return o.parse=function(){var e=P.exec(this.address.toLowerCase());e&&(this.username=e[1],this.domain=e[2])},o.fetch=function(){try{var e=this;return Promise.resolve(e.options.proxy?e.fetchWithProxy():e.fetchWithoutProxy())}catch(e){return Promise.reject(e)}},o.fetchWithProxy=function(){try{var r=this;return Promise.resolve(e(r.options.proxy+"/lightning-address-details?"+new URLSearchParams({ln:r.address}).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){r.parseResponse(e.lnurlp,e.keysend,e.nostr)})})}catch(e){return Promise.reject(e)}},o.fetchWithoutProxy=function(){try{var r=this;return r.domain&&r.username?Promise.resolve(e(r.lnurlpUrl())).then(function(t){return Promise.resolve(e(r.keysendUrl())).then(function(n){return Promise.resolve(e(r.nostrUrl())).then(function(e){function o(){function t(){function t(){r.parseResponse(a,o,n)}var n,i=function(){if(e.ok)return Promise.resolve(e.json()).then(function(e){n=e})}();return i&&i.then?i.then(t):t()}var o,i=function(){if(n.ok)return Promise.resolve(n.json()).then(function(e){o=e})}();return i&&i.then?i.then(t):t()}var a,i=function(){if(t.ok)return Promise.resolve(t.json()).then(function(e){a=e})}();return i&&i.then?i.then(o):o()})})}):Promise.resolve()}catch(e){return Promise.reject(e)}},o.lnurlpUrl=function(){return"https://"+this.domain+"/.well-known/lnurlp/"+this.username},o.keysendUrl=function(){return"https://"+this.domain+"/.well-known/keysend/"+this.username},o.nostrUrl=function(){return"https://"+this.domain+"/.well-known/nostr.json?name="+this.username},o.generateInvoice=function(r){try{var t,n=function(e){var r=t&&t.pr&&t.pr.toString();if(!r)throw new Error("Invalid pay service invoice");var n={pr:r};return t&&t.verify&&(n.verify=t.verify.toString()),new v(n)},o=this,a=function(){if(o.options.proxy)return Promise.resolve(e(o.options.proxy+"/generate-invoice?"+new URLSearchParams(c({ln:o.address},r)).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){t=e.invoice})});if(!o.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!o.lnurlpData.callback||!m(o.lnurlpData.callback))throw new Error("Valid callback does not exist in lnurlpData");var n=new URL(o.lnurlpData.callback);return n.search=new URLSearchParams(r).toString(),Promise.resolve(e(n)).then(function(e){return Promise.resolve(e.json()).then(function(e){t=e})})}();return Promise.resolve(a&&a.then?a.then(n):n())}catch(e){return Promise.reject(e)}},o.requestInvoice=function(e){try{var r=this;if(!r.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");var t=1e3*e.satoshi,n=r.lnurlpData,o=n.commentAllowed;if(!h({amount:t,min:n.min,max:n.max}))throw new Error("Invalid amount");if(e.comment&&o&&o>0&&e.comment.length>o)throw new Error("The comment length must be "+o+" characters or fewer");var a={amount:t.toString()};return e.comment&&(a.comment=e.comment),e.payerdata&&(a.payerdata=JSON.stringify(e.payerdata)),Promise.resolve(r.generateInvoice(a))}catch(e){return Promise.reject(e)}},o.boost=function(e,r){void 0===r&&(r=0);try{var t=this;if(!t.keysendData)throw new Error("No keysendData available. Please call fetch() first.");var n=t.keysendData;return Promise.resolve(s({destination:n.destination,customKey:n.customKey,customValue:n.customValue,amount:r,boost:e},{webln:t.webln}))}catch(e){return Promise.reject(e)}},o.zapInvoice=function(e,r){var t=e.satoshi,n=e.comment,o=e.relays,a=e.e;void 0===r&&(r={});try{var i=this;if(!i.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!i.nostrPubkey)throw new Error("Nostr Pubkey is missing");var s=i.nostrPubkey,u=1e3*t,c=i.lnurlpData,l=c.allowsNostr;if(!h({amount:u,min:c.min,max:c.max}))throw new Error("Invalid amount");if(!l)throw new Error("Your provider does not support zaps");return Promise.resolve(f({satoshi:u,comment:n,p:s,e:a,relays:o},r)).then(function(e){var r={amount:u.toString(),nostr:JSON.stringify(e)};return Promise.resolve(i.generateInvoice(r))})}catch(a){return Promise.reject(a)}},o.zap=function(e,r){void 0===r&&(r={});try{var t=this,n=t.zapInvoice(e,r);if(!t.webln)throw new Error("WebLN not available");return Promise.resolve(t.webln.enable()).then(function(){var e=t.webln,r=e.sendPayment;return Promise.resolve(n).then(function(t){return r.call(e,t.paymentRequest)})})}catch(e){return Promise.reject(e)}},o.parseResponse=function(e,n,o){if(e&&(this.lnurlpData=function(e){if("payRequest"!==e.tag)throw new Error("Invalid pay service params");var n=(e.callback+"").trim();if(!m(n))throw new Error("Callback must be a valid url");var o,a,i=Math.ceil(Number(e.minSendable||0)),s=Math.floor(Number(e.maxSendable));if(!i||!s||i>s)throw new Error("Invalid pay service params");try{o=JSON.parse(e.metadata+""),a=t(e.metadata+"").toString(r)}catch(e){o=[],a=t("[]").toString(r)}for(var u="",c="",l="",h=0;h<o.length;h++){var v=o[h],f=v[0],p=v[1];switch(f){case"text/plain":c=p;break;case"text/identifier":l=p;break;case"image/png;base64":case"image/jpeg;base64":u="data:"+f+","+p}}var y,d=e.payerData;try{y=new URL(n).hostname}catch(e){}return{callback:n,fixed:i===s,min:i,max:s,domain:y,metadata:o,metadataHash:a,identifier:l,description:c,image:u,payerData:d,commentAllowed:Number(e.commentAllowed)||0,rawData:e,allowsNostr:e.allowsNostr||!1}}(e)),n&&(this.keysendData=function(e){if("keysend"!==e.tag)throw new Error("Invalid keysend params");if("OK"!==e.status)throw new Error("Keysend status not OK");if(!("customKey"in e.customData[0])||"696969"!=e.customData[0].customKey)throw new Error("Unable to find customKey");if(!("customValue"in e.customData[0])||!e.customData[0].customValue)throw new Error("Unable to find customValue");if(!e.pubkey)throw new Error("Pubkey does not exist");return{destination:e.pubkey,customKey:e.customData[0].customKey,customValue:e.customData[0].customValue}}(n)),o){var a=w(o,this.username);this.nostrData=a[0],this.nostrPubkey=a[1],this.nostrRelays=a[2]}},n}(),k=function(r){try{var t="https://getalby.com/api/rates/"+r.toLowerCase()+".json";return Promise.resolve(e(t)).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.rate_float/1e8})})}catch(e){return Promise.reject(e)}},j=function(e){var r=e.satoshi;return Promise.resolve(k(e.currency)).then(function(e){return Number(r)*e})},D={__proto__:null,getFiatBtcRate:k,getFiatValue:j,getSatoshiValue:function(e){var r=e.amount;return Promise.resolve(k(e.currency)).then(function(e){return Math.floor(Number(r)/e)})},getFormattedFiatValue:function(e){var r=e.currency,t=e.locale;return t||(t="en"),Promise.resolve(j({satoshi:e.satoshi,currency:r})).then(function(e){return e.toLocaleString(t,{style:"currency",currency:r})})}};export{v as Invoice,b as LightningAddress,u as boostagrams,a as fetchWithLsat,D as fiat,i as lsat,g as nostr,s as sendBoostagram}; | ||
import e from"cross-fetch";import t from"crypto-js/enc-hex.js";import r from"crypto-js/sha256.js";import{decode as n}from"light-bolt11-decoder";var o=/*#__PURE__*/function(){function e(e){this.storage=void 0,this.storage=e||{}}var t=e.prototype;return t.getItem=function(e){return this.storage[e]},t.setItem=function(e,t){this.storage[e]=t},e}(),a={__proto__:null,MemoryStorage:o,NoStorage:/*#__PURE__*/function(){function e(e){}var t=e.prototype;return t.getItem=function(e){return null},t.setItem=function(e,t){},e}(),default:o},i=new o,s=function(t,r,n){try{var o,a=function(n){return o?n:(r.headers["Accept-Authenticate"]="LSAT",Promise.resolve(e(t,r)).then(function(n){var o=n.headers.get("www-authenticate");if(!o)return n;var a=o.split(","),i=a[0].replace("LSAT macaroon=","").trim(),c=a[1].replace("invoice=","").trim();return Promise.resolve(s.enable()).then(function(){return Promise.resolve(s.sendPayment(c)).then(function(n){return u.setItem(t,JSON.stringify({mac:i,preimage:n.preimage})),r.headers.Authorization="LSAT "+i+":"+n.preimage,Promise.resolve(e(t,r))})})}))};n||(n={});var s=n.webln||globalThis.webln;if(!s)throw new Error("WebLN is missing");var u=n.store||i;r||(r={}),r.cache="no-store",r.mode="cors",r.headers||(r.headers={});var c=u.getItem(t),l=function(){if(c){var n=JSON.parse(c);return r.headers.Authorization="LSAT "+n.mac+":"+n.preimage,Promise.resolve(e(t,r)).then(function(e){return o=1,e})}}();return Promise.resolve(l&&l.then?l.then(a):a(l))}catch(e){return Promise.reject(e)}},u={__proto__:null,Storage:a,fetchWithLsat:s,default:s},c=function(e,t){try{var r=e.boost,n=e.amount;t||(t={});var o=t.webln||globalThis.webln;n||(n=Math.floor(r.value_msat/1e3));var a={destination:e.destination,amount:n,customRecords:{7629169:JSON.stringify(r)}};return e.customKey&&e.customValue&&(a.customRecords[e.customKey]=e.customValue),Promise.resolve(o.enable()).then(function(){return Promise.resolve(o.keysend(a))})}catch(e){return Promise.reject(e)}},l={__proto__:null,boost:c,default:c};function m(){return m=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},m.apply(this,arguments)}var h=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,f=function(e){return!!e&&h.test(e)},v=function(e){var t=e.amount,r=e.min,n=e.max,o=t>0&&t>=r&&t<=n;return o&&r===n?t===r:o},p=/*#__PURE__*/function(){function o(e){var t,r;this.paymentRequest=void 0,this.paymentHash=void 0,this.preimage=void 0,this.verify=void 0,this.paymentRequest=e.pr,this.paymentHash=function(e){if(!e)return null;try{var t=n(e);if(!t||!t.sections)return null;var r=t.sections.find(function(e){return"payment_hash"===e.name});return r&&r.value?r.value.toString():null}catch(e){return null}}(this.paymentRequest),this.verify=null!=(t=e.verify)?t:null,this.preimage=null!=(r=e.preimage)?r:null}var a=o.prototype;return a.isPaid=function(){try{var e=this;if(e.preimage)return Promise.resolve(e.validatePreimage(e.preimage));if(e.verify)return Promise.resolve(e.verifyPayment());throw new Error("Could not verify payment")}catch(e){return Promise.reject(e)}},a.validatePreimage=function(e){if(!e||!this.paymentHash)return!1;try{var n=r(t.parse(e)).toString(t);return this.paymentHash===n}catch(e){return!1}},a.verifyPayment=function(){try{var t=this;if(!t.verify)throw new Error("LNURL verify not available");return Promise.resolve(e(t.verify)).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.preimage&&(t.preimage=e.preimage),e.settled})})}catch(e){return Promise.reject(e)}},o}(),y=function(e,t){var r=e.satoshi,n=e.comment,o=e.p,a=e.e,i=e.relays;void 0===t&&(t={});try{var s=t.nostr||globalThis.nostr;if(!s)throw new Error("nostr option or window.nostr is not available");var u=[["relays"].concat(i),["amount",r.toString()]];return o&&u.push(["p",o]),a&&u.push(["e",a]),Promise.resolve(s.getPublicKey()).then(function(e){var t={pubkey:e,created_at:Math.floor(Date.now()/1e3),kind:9734,tags:u,content:null!=n?n:""};return t.id=g(t),Promise.resolve(s.signEvent(t))})}catch(a){return Promise.reject(a)}};function d(e){if("string"!=typeof e.content)return!1;if("number"!=typeof e.created_at)return!1;if(!Array.isArray(e.tags))return!1;for(var t=0;t<e.tags.length;t++){var r=e.tags[t];if(!Array.isArray(r))return!1;for(var n=0;n<r.length;n++)if("object"==typeof r[n])return!1}return!0}function w(e){if(!d(e))throw new Error("can't serialize event with wrong or missing properties");return JSON.stringify([0,e.pubkey,e.created_at,e.kind,e.tags,e.content])}function g(e){return r(w(e)).toString(t)}function P(e,t){var r,n,o,a;return t&&e&&(n=(r=null==(o=e.names)?void 0:o[t])?null==(a=e.relays)?void 0:a[r]:void 0),[e,r,n]}var b={__proto__:null,generateZapEvent:y,validateEvent:d,serializeEvent:w,getEventHash:g,parseNostrResponse:P},k=/^((?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@((?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,j=/*#__PURE__*/function(){function n(e,t){this.address=void 0,this.options=void 0,this.username=void 0,this.domain=void 0,this.pubkey=void 0,this.lnurlpData=void 0,this.keysendData=void 0,this.nostrData=void 0,this.nostrPubkey=void 0,this.nostrRelays=void 0,this.webln=void 0,this.address=e,this.options={proxy:"https://lnaddressproxy.getalby.com",webln:globalThis.webln},this.options=Object.assign(this.options,t),this.parse(),this.webln=this.options.webln}var o=n.prototype;return o.parse=function(){var e=k.exec(this.address.toLowerCase());e&&(this.username=e[1],this.domain=e[2])},o.fetch=function(){try{var e=this;return Promise.resolve(e.options.proxy?e.fetchWithProxy():e.fetchWithoutProxy())}catch(e){return Promise.reject(e)}},o.fetchWithProxy=function(){try{var t=this;return Promise.resolve(e(t.options.proxy+"/lightning-address-details?"+new URLSearchParams({ln:t.address}).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.parseResponse(e.lnurlp,e.keysend,e.nostr)})})}catch(e){return Promise.reject(e)}},o.fetchWithoutProxy=function(){try{var t=this;return t.domain&&t.username?Promise.resolve(e(t.lnurlpUrl())).then(function(r){return Promise.resolve(e(t.keysendUrl())).then(function(n){return Promise.resolve(e(t.nostrUrl())).then(function(e){function o(){function r(){function r(){t.parseResponse(a,o,n)}var n,i=function(){if(e.ok)return Promise.resolve(e.json()).then(function(e){n=e})}();return i&&i.then?i.then(r):r()}var o,i=function(){if(n.ok)return Promise.resolve(n.json()).then(function(e){o=e})}();return i&&i.then?i.then(r):r()}var a,i=function(){if(r.ok)return Promise.resolve(r.json()).then(function(e){a=e})}();return i&&i.then?i.then(o):o()})})}):Promise.resolve()}catch(e){return Promise.reject(e)}},o.lnurlpUrl=function(){return"https://"+this.domain+"/.well-known/lnurlp/"+this.username},o.keysendUrl=function(){return"https://"+this.domain+"/.well-known/keysend/"+this.username},o.nostrUrl=function(){return"https://"+this.domain+"/.well-known/nostr.json?name="+this.username},o.generateInvoice=function(t){try{var r,n=function(e){var t=r&&r.pr&&r.pr.toString();if(!t)throw new Error("Invalid pay service invoice");var n={pr:t};return r&&r.verify&&(n.verify=r.verify.toString()),new p(n)},o=this,a=function(){if(o.options.proxy)return Promise.resolve(e(o.options.proxy+"/generate-invoice?"+new URLSearchParams(m({ln:o.address},t)).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){r=e.invoice})});if(!o.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!o.lnurlpData.callback||!f(o.lnurlpData.callback))throw new Error("Valid callback does not exist in lnurlpData");var n=new URL(o.lnurlpData.callback);return n.search=new URLSearchParams(t).toString(),Promise.resolve(e(n)).then(function(e){return Promise.resolve(e.json()).then(function(e){r=e})})}();return Promise.resolve(a&&a.then?a.then(n):n())}catch(e){return Promise.reject(e)}},o.requestInvoice=function(e){try{var t=this;if(!t.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");var r=1e3*e.satoshi,n=t.lnurlpData,o=n.commentAllowed;if(!v({amount:r,min:n.min,max:n.max}))throw new Error("Invalid amount");if(e.comment&&o&&o>0&&e.comment.length>o)throw new Error("The comment length must be "+o+" characters or fewer");var a={amount:r.toString()};return e.comment&&(a.comment=e.comment),e.payerdata&&(a.payerdata=JSON.stringify(e.payerdata)),Promise.resolve(t.generateInvoice(a))}catch(e){return Promise.reject(e)}},o.boost=function(e,t){void 0===t&&(t=0);try{var r=this;if(!r.keysendData)throw new Error("No keysendData available. Please call fetch() first.");var n=r.keysendData;return Promise.resolve(c({destination:n.destination,customKey:n.customKey,customValue:n.customValue,amount:t,boost:e},{webln:r.webln}))}catch(e){return Promise.reject(e)}},o.zapInvoice=function(e,t){var r=e.satoshi,n=e.comment,o=e.relays,a=e.e;void 0===t&&(t={});try{var i=this;if(!i.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!i.nostrPubkey)throw new Error("Nostr Pubkey is missing");var s=i.nostrPubkey,u=1e3*r,c=i.lnurlpData,l=c.allowsNostr;if(!v({amount:u,min:c.min,max:c.max}))throw new Error("Invalid amount");if(!l)throw new Error("Your provider does not support zaps");return Promise.resolve(y({satoshi:u,comment:n,p:s,e:a,relays:o},t)).then(function(e){var t={amount:u.toString(),nostr:JSON.stringify(e)};return Promise.resolve(i.generateInvoice(t))})}catch(a){return Promise.reject(a)}},o.zap=function(e,t){void 0===t&&(t={});try{var r=this,n=r.zapInvoice(e,t);if(!r.webln)throw new Error("WebLN not available");return Promise.resolve(r.webln.enable()).then(function(){var e=r.webln,t=e.sendPayment;return Promise.resolve(n).then(function(r){return t.call(e,r.paymentRequest)})})}catch(e){return Promise.reject(e)}},o.parseResponse=function(e,n,o){if(e&&(this.lnurlpData=function(e){if("payRequest"!==e.tag)throw new Error("Invalid pay service params");var n=(e.callback+"").trim();if(!f(n))throw new Error("Callback must be a valid url");var o,a,i=Math.ceil(Number(e.minSendable||0)),s=Math.floor(Number(e.maxSendable));if(!i||!s||i>s)throw new Error("Invalid pay service params");try{o=JSON.parse(e.metadata+""),a=r(e.metadata+"").toString(t)}catch(e){o=[],a=r("[]").toString(t)}for(var u="",c="",l="",m=0;m<o.length;m++){var h=o[m],v=h[0],p=h[1];switch(v){case"text/plain":c=p;break;case"text/identifier":l=p;break;case"image/png;base64":case"image/jpeg;base64":u="data:"+v+","+p}}var y,d=e.payerData;try{y=new URL(n).hostname}catch(e){}return{callback:n,fixed:i===s,min:i,max:s,domain:y,metadata:o,metadataHash:a,identifier:l,description:c,image:u,payerData:d,commentAllowed:Number(e.commentAllowed)||0,rawData:e,allowsNostr:e.allowsNostr||!1}}(e)),n&&(this.keysendData=function(e){if("keysend"!==e.tag)throw new Error("Invalid keysend params");if("OK"!==e.status)throw new Error("Keysend status not OK");if(!("customKey"in e.customData[0])||"696969"!=e.customData[0].customKey)throw new Error("Unable to find customKey");if(!("customValue"in e.customData[0])||!e.customData[0].customValue)throw new Error("Unable to find customValue");if(!e.pubkey)throw new Error("Pubkey does not exist");return{destination:e.pubkey,customKey:e.customData[0].customKey,customValue:e.customData[0].customValue}}(n)),o){var a=P(o,this.username);this.nostrData=a[0],this.nostrPubkey=a[1],this.nostrRelays=a[2]}},n}(),S=function(t){try{var r="https://getalby.com/api/rates/"+t.toLowerCase()+".json";return Promise.resolve(e(r)).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.rate_float/1e8})})}catch(e){return Promise.reject(e)}},D=function(e){var t=e.satoshi;return Promise.resolve(S(e.currency)).then(function(e){return Number(t)*e})},E={__proto__:null,getFiatBtcRate:S,getFiatValue:D,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(S(e.currency)).then(function(e){return Math.floor(Number(t)/e)})},getFormattedFiatValue:function(e){var t=e.currency,r=e.locale;return r||(r="en"),Promise.resolve(D({satoshi:e.satoshi,currency:t})).then(function(e){return e.toLocaleString(r,{style:"currency",currency:t})})}};export{p as Invoice,j as LightningAddress,l as boostagrams,s as fetchWithLsat,E as fiat,u as lsat,b as nostr,c as sendBoostagram}; | ||
//# sourceMappingURL=index.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("cross-fetch"),require("crypto-js/enc-hex.js"),require("crypto-js/sha256.js"),require("light-bolt11-decoder")):"function"==typeof define&&define.amd?define(["exports","cross-fetch","crypto-js/enc-hex.js","crypto-js/sha256.js","light-bolt11-decoder"],t):t((e||self).albyTools={},e.crossFetch,e.Hex,e.sha256,e.lightBolt11Decoder)}(this,function(e,t,r,n,o){function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=/*#__PURE__*/a(t),s=/*#__PURE__*/a(r),u=/*#__PURE__*/a(n),l=/*#__PURE__*/function(){function e(e){this.storage=void 0,this.storage=e||{}}var t=e.prototype;return t.getItem=function(e){return this.storage[e]},t.setItem=function(e,t){this.storage[e]=t},e}(),c=function(e,t,r){try{var n,o=function(r){return n?r:(t.headers["Accept-Authenticate"]="LSAT",Promise.resolve(i.default(e,t)).then(function(r){var n=r.headers.get("www-authenticate");if(!n)return r;var o=n.split(","),u=o[0].replace("LSAT macaroon=","").trim(),l=o[1].replace("invoice=","").trim();return Promise.resolve(a.enable()).then(function(){return Promise.resolve(a.sendPayment(l)).then(function(r){return s.setItem(e,JSON.stringify({mac:u,preimage:r.preimage})),t.headers.Authorization="LSAT "+u+":"+r.preimage,Promise.resolve(i.default(e,t))})})}))};r||(r={});var a=r.webln||globalThis.webln;if(!a)throw new Error("WebLN is missing");var s=r.store||new l;t||(t={}),t.cache="no-store",t.mode="cors",t.headers||(t.headers={});var u=s.getItem(e),c=function(){if(u){var r=JSON.parse(u);return t.headers.Authorization="LSAT "+r.mac+":"+r.preimage,Promise.resolve(i.default(e,t)).then(function(e){return n=1,e})}}();return Promise.resolve(c&&c.then?c.then(o):o(c))}catch(e){return Promise.reject(e)}},h={__proto__:null,fetchWithLsat:c,default:c},m=function(e,t){try{var r=e.boost,n=e.amount;t||(t={});var o=t.webln||globalThis.webln;n||(n=Math.floor(r.value_msat/1e3));var a={destination:e.destination,amount:n,customRecords:{7629169:JSON.stringify(r)}};return e.customKey&&e.customValue&&(a.customRecords[e.customKey]=e.customValue),Promise.resolve(o.enable()).then(function(){return Promise.resolve(o.keysend(a))})}catch(e){return Promise.reject(e)}},f={__proto__:null,boost:m,default:m};function v(){return v=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},v.apply(this,arguments)}var d=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,p=function(e){return!!e&&d.test(e)},y=function(e){var t=e.amount,r=e.min,n=e.max,o=t>0&&t>=r&&t<=n;return o&&r===n?t===r:o},w=/*#__PURE__*/function(){function e(e){var t,r;this.paymentRequest=void 0,this.paymentHash=void 0,this.preimage=void 0,this.verify=void 0,this.paymentRequest=e.pr,this.paymentHash=function(e){if(!e)return null;try{var t=o.decode(e);if(!t||!t.sections)return null;var r=t.sections.find(function(e){return"payment_hash"===e.name});return r&&r.value?r.value.toString():null}catch(e){return null}}(this.paymentRequest),this.verify=null!=(t=e.verify)?t:null,this.preimage=null!=(r=e.preimage)?r:null}var t=e.prototype;return t.isPaid=function(){try{var e=this;if(e.preimage)return Promise.resolve(e.validatePreimage(e.preimage));if(e.verify)return Promise.resolve(e.verifyPayment());throw new Error("Could not verify payment")}catch(e){return Promise.reject(e)}},t.validatePreimage=function(e){if(!e||!this.paymentHash)return!1;try{var t=u.default(s.default.parse(e)).toString(s.default);return this.paymentHash===t}catch(e){return!1}},t.verifyPayment=function(){try{var e=this;if(!e.verify)throw new Error("LNURL verify not available");return Promise.resolve(i.default(e.verify)).then(function(t){return Promise.resolve(t.json()).then(function(t){return t.preimage&&(e.preimage=t.preimage),t.settled})})}catch(e){return Promise.reject(e)}},e}(),g=function(e,t){var r=e.satoshi,n=e.comment,o=e.p,a=e.e,i=e.relays;void 0===t&&(t={});try{var s=t.nostr||globalThis.nostr;if(!s)throw new Error("nostr option or window.nostr is not available");var u=[["relays"].concat(i),["amount",r.toString()]];return o&&u.push(["p",o]),a&&u.push(["e",a]),Promise.resolve(s.getPublicKey()).then(function(e){var t={pubkey:e,created_at:Math.floor(Date.now()/1e3),kind:9734,tags:u,content:null!=n?n:""};return t.id=j(t),Promise.resolve(s.signEvent(t))})}catch(a){return Promise.reject(a)}};function b(e){if("string"!=typeof e.content)return!1;if("number"!=typeof e.created_at)return!1;if(!Array.isArray(e.tags))return!1;for(var t=0;t<e.tags.length;t++){var r=e.tags[t];if(!Array.isArray(r))return!1;for(var n=0;n<r.length;n++)if("object"==typeof r[n])return!1}return!0}function P(e){if(!b(e))throw new Error("can't serialize event with wrong or missing properties");return JSON.stringify([0,e.pubkey,e.created_at,e.kind,e.tags,e.content])}function j(e){return u.default(P(e)).toString(s.default)}function k(e,t){var r,n,o,a;return t&&e&&(n=(r=null==(o=e.names)?void 0:o[t])?null==(a=e.relays)?void 0:a[r]:void 0),[e,r,n]}var D={__proto__:null,generateZapEvent:g,validateEvent:b,serializeEvent:P,getEventHash:j,parseNostrResponse:k},S=/^((?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@((?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,E=/*#__PURE__*/function(){function e(e,t){this.address=void 0,this.options=void 0,this.username=void 0,this.domain=void 0,this.pubkey=void 0,this.lnurlpData=void 0,this.keysendData=void 0,this.nostrData=void 0,this.nostrPubkey=void 0,this.nostrRelays=void 0,this.webln=void 0,this.address=e,this.options={proxy:"https://lnaddressproxy.getalby.com",webln:globalThis.webln},this.options=Object.assign(this.options,t),this.parse(),this.webln=this.options.webln}var t=e.prototype;return t.parse=function(){var e=S.exec(this.address.toLowerCase());e&&(this.username=e[1],this.domain=e[2])},t.fetch=function(){try{var e=this;return Promise.resolve(e.options.proxy?e.fetchWithProxy():e.fetchWithoutProxy())}catch(e){return Promise.reject(e)}},t.fetchWithProxy=function(){try{var e=this;return Promise.resolve(i.default(e.options.proxy+"/lightning-address-details?"+new URLSearchParams({ln:e.address}).toString())).then(function(t){return Promise.resolve(t.json()).then(function(t){e.parseResponse(t.lnurlp,t.keysend,t.nostr)})})}catch(e){return Promise.reject(e)}},t.fetchWithoutProxy=function(){try{var e=this;return e.domain&&e.username?Promise.resolve(i.default(e.lnurlpUrl())).then(function(t){return Promise.resolve(i.default(e.keysendUrl())).then(function(r){return Promise.resolve(i.default(e.nostrUrl())).then(function(n){function o(){function t(){function t(){e.parseResponse(a,o,r)}var r,i=function(){if(n.ok)return Promise.resolve(n.json()).then(function(e){r=e})}();return i&&i.then?i.then(t):t()}var o,i=function(){if(r.ok)return Promise.resolve(r.json()).then(function(e){o=e})}();return i&&i.then?i.then(t):t()}var a,i=function(){if(t.ok)return Promise.resolve(t.json()).then(function(e){a=e})}();return i&&i.then?i.then(o):o()})})}):Promise.resolve()}catch(e){return Promise.reject(e)}},t.lnurlpUrl=function(){return"https://"+this.domain+"/.well-known/lnurlp/"+this.username},t.keysendUrl=function(){return"https://"+this.domain+"/.well-known/keysend/"+this.username},t.nostrUrl=function(){return"https://"+this.domain+"/.well-known/nostr.json?name="+this.username},t.generateInvoice=function(e){try{var t,r=function(e){var r=t&&t.pr&&t.pr.toString();if(!r)throw new Error("Invalid pay service invoice");var n={pr:r};return t&&t.verify&&(n.verify=t.verify.toString()),new w(n)},n=this,o=function(){if(n.options.proxy)return Promise.resolve(i.default(n.options.proxy+"/generate-invoice?"+new URLSearchParams(v({ln:n.address},e)).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){t=e.invoice})});if(!n.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!n.lnurlpData.callback||!p(n.lnurlpData.callback))throw new Error("Valid callback does not exist in lnurlpData");var r=new URL(n.lnurlpData.callback);return r.search=new URLSearchParams(e).toString(),Promise.resolve(i.default(r)).then(function(e){return Promise.resolve(e.json()).then(function(e){t=e})})}();return Promise.resolve(o&&o.then?o.then(r):r())}catch(e){return Promise.reject(e)}},t.requestInvoice=function(e){try{var t=this;if(!t.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");var r=1e3*e.satoshi,n=t.lnurlpData,o=n.commentAllowed;if(!y({amount:r,min:n.min,max:n.max}))throw new Error("Invalid amount");if(e.comment&&o&&o>0&&e.comment.length>o)throw new Error("The comment length must be "+o+" characters or fewer");var a={amount:r.toString()};return e.comment&&(a.comment=e.comment),e.payerdata&&(a.payerdata=JSON.stringify(e.payerdata)),Promise.resolve(t.generateInvoice(a))}catch(e){return Promise.reject(e)}},t.boost=function(e,t){void 0===t&&(t=0);try{var r=this;if(!r.keysendData)throw new Error("No keysendData available. Please call fetch() first.");var n=r.keysendData;return Promise.resolve(m({destination:n.destination,customKey:n.customKey,customValue:n.customValue,amount:t,boost:e},{webln:r.webln}))}catch(e){return Promise.reject(e)}},t.zapInvoice=function(e,t){var r=e.satoshi,n=e.comment,o=e.relays,a=e.e;void 0===t&&(t={});try{var i=this;if(!i.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!i.nostrPubkey)throw new Error("Nostr Pubkey is missing");var s=i.nostrPubkey,u=1e3*r,l=i.lnurlpData,c=l.allowsNostr;if(!y({amount:u,min:l.min,max:l.max}))throw new Error("Invalid amount");if(!c)throw new Error("Your provider does not support zaps");return Promise.resolve(g({satoshi:u,comment:n,p:s,e:a,relays:o},t)).then(function(e){var t={amount:u.toString(),nostr:JSON.stringify(e)};return Promise.resolve(i.generateInvoice(t))})}catch(a){return Promise.reject(a)}},t.zap=function(e,t){void 0===t&&(t={});try{var r=this,n=r.zapInvoice(e,t);if(!r.webln)throw new Error("WebLN not available");return Promise.resolve(r.webln.enable()).then(function(){var e=r.webln,t=e.sendPayment;return Promise.resolve(n).then(function(r){return t.call(e,r.paymentRequest)})})}catch(e){return Promise.reject(e)}},t.parseResponse=function(e,t,r){if(e&&(this.lnurlpData=function(e){if("payRequest"!==e.tag)throw new Error("Invalid pay service params");var t=(e.callback+"").trim();if(!p(t))throw new Error("Callback must be a valid url");var r,n,o=Math.ceil(Number(e.minSendable||0)),a=Math.floor(Number(e.maxSendable));if(!o||!a||o>a)throw new Error("Invalid pay service params");try{r=JSON.parse(e.metadata+""),n=u.default(e.metadata+"").toString(s.default)}catch(e){r=[],n=u.default("[]").toString(s.default)}for(var i="",l="",c="",h=0;h<r.length;h++){var m=r[h],f=m[0],v=m[1];switch(f){case"text/plain":l=v;break;case"text/identifier":c=v;break;case"image/png;base64":case"image/jpeg;base64":i="data:"+f+","+v}}var d,y=e.payerData;try{d=new URL(t).hostname}catch(e){}return{callback:t,fixed:o===a,min:o,max:a,domain:d,metadata:r,metadataHash:n,identifier:c,description:l,image:i,payerData:y,commentAllowed:Number(e.commentAllowed)||0,rawData:e,allowsNostr:e.allowsNostr||!1}}(e)),t&&(this.keysendData=function(e){if("keysend"!==e.tag)throw new Error("Invalid keysend params");if("OK"!==e.status)throw new Error("Keysend status not OK");if(!("customKey"in e.customData[0])||"696969"!=e.customData[0].customKey)throw new Error("Unable to find customKey");if(!("customValue"in e.customData[0])||!e.customData[0].customValue)throw new Error("Unable to find customValue");if(!e.pubkey)throw new Error("Pubkey does not exist");return{destination:e.pubkey,customKey:e.customData[0].customKey,customValue:e.customData[0].customValue}}(t)),r){var n=k(r,this.username);this.nostrData=n[0],this.nostrPubkey=n[1],this.nostrRelays=n[2]}},e}(),x=function(e){try{var t="https://getalby.com/api/rates/"+e.toLowerCase()+".json";return Promise.resolve(i.default(t)).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.rate_float/1e8})})}catch(e){return Promise.reject(e)}},N=function(e){var t=e.satoshi;return Promise.resolve(x(e.currency)).then(function(e){return Number(t)*e})},_={__proto__:null,getFiatBtcRate:x,getFiatValue:N,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(x(e.currency)).then(function(e){return Math.floor(Number(t)/e)})},getFormattedFiatValue:function(e){var t=e.currency,r=e.locale;return r||(r="en"),Promise.resolve(N({satoshi:e.satoshi,currency:t})).then(function(e){return e.toLocaleString(r,{style:"currency",currency:t})})}};e.Invoice=w,e.LightningAddress=E,e.boostagrams=f,e.fetchWithLsat=c,e.fiat=_,e.lsat=h,e.nostr=D,e.sendBoostagram=m}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("cross-fetch"),require("crypto-js/enc-hex.js"),require("crypto-js/sha256.js"),require("light-bolt11-decoder")):"function"==typeof define&&define.amd?define(["exports","cross-fetch","crypto-js/enc-hex.js","crypto-js/sha256.js","light-bolt11-decoder"],t):t((e||self).albyTools={},e.crossFetch,e.Hex,e.sha256,e.lightBolt11Decoder)}(this,function(e,t,r,n,o){function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=/*#__PURE__*/a(t),s=/*#__PURE__*/a(r),u=/*#__PURE__*/a(n),l=/*#__PURE__*/function(){function e(e){this.storage=void 0,this.storage=e||{}}var t=e.prototype;return t.getItem=function(e){return this.storage[e]},t.setItem=function(e,t){this.storage[e]=t},e}(),c={__proto__:null,MemoryStorage:l,NoStorage:/*#__PURE__*/function(){function e(e){}var t=e.prototype;return t.getItem=function(e){return null},t.setItem=function(e,t){},e}(),default:l},h=new l,f=function(e,t,r){try{var n,o=function(r){return n?r:(t.headers["Accept-Authenticate"]="LSAT",Promise.resolve(i.default(e,t)).then(function(r){var n=r.headers.get("www-authenticate");if(!n)return r;var o=n.split(","),u=o[0].replace("LSAT macaroon=","").trim(),l=o[1].replace("invoice=","").trim();return Promise.resolve(a.enable()).then(function(){return Promise.resolve(a.sendPayment(l)).then(function(r){return s.setItem(e,JSON.stringify({mac:u,preimage:r.preimage})),t.headers.Authorization="LSAT "+u+":"+r.preimage,Promise.resolve(i.default(e,t))})})}))};r||(r={});var a=r.webln||globalThis.webln;if(!a)throw new Error("WebLN is missing");var s=r.store||h;t||(t={}),t.cache="no-store",t.mode="cors",t.headers||(t.headers={});var u=s.getItem(e),l=function(){if(u){var r=JSON.parse(u);return t.headers.Authorization="LSAT "+r.mac+":"+r.preimage,Promise.resolve(i.default(e,t)).then(function(e){return n=1,e})}}();return Promise.resolve(l&&l.then?l.then(o):o(l))}catch(e){return Promise.reject(e)}},m={__proto__:null,Storage:c,fetchWithLsat:f,default:f},v=function(e,t){try{var r=e.boost,n=e.amount;t||(t={});var o=t.webln||globalThis.webln;n||(n=Math.floor(r.value_msat/1e3));var a={destination:e.destination,amount:n,customRecords:{7629169:JSON.stringify(r)}};return e.customKey&&e.customValue&&(a.customRecords[e.customKey]=e.customValue),Promise.resolve(o.enable()).then(function(){return Promise.resolve(o.keysend(a))})}catch(e){return Promise.reject(e)}},d={__proto__:null,boost:v,default:v};function p(){return p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},p.apply(this,arguments)}var y=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,w=function(e){return!!e&&y.test(e)},g=function(e){var t=e.amount,r=e.min,n=e.max,o=t>0&&t>=r&&t<=n;return o&&r===n?t===r:o},b=/*#__PURE__*/function(){function e(e){var t,r;this.paymentRequest=void 0,this.paymentHash=void 0,this.preimage=void 0,this.verify=void 0,this.paymentRequest=e.pr,this.paymentHash=function(e){if(!e)return null;try{var t=o.decode(e);if(!t||!t.sections)return null;var r=t.sections.find(function(e){return"payment_hash"===e.name});return r&&r.value?r.value.toString():null}catch(e){return null}}(this.paymentRequest),this.verify=null!=(t=e.verify)?t:null,this.preimage=null!=(r=e.preimage)?r:null}var t=e.prototype;return t.isPaid=function(){try{var e=this;if(e.preimage)return Promise.resolve(e.validatePreimage(e.preimage));if(e.verify)return Promise.resolve(e.verifyPayment());throw new Error("Could not verify payment")}catch(e){return Promise.reject(e)}},t.validatePreimage=function(e){if(!e||!this.paymentHash)return!1;try{var t=u.default(s.default.parse(e)).toString(s.default);return this.paymentHash===t}catch(e){return!1}},t.verifyPayment=function(){try{var e=this;if(!e.verify)throw new Error("LNURL verify not available");return Promise.resolve(i.default(e.verify)).then(function(t){return Promise.resolve(t.json()).then(function(t){return t.preimage&&(e.preimage=t.preimage),t.settled})})}catch(e){return Promise.reject(e)}},e}(),P=function(e,t){var r=e.satoshi,n=e.comment,o=e.p,a=e.e,i=e.relays;void 0===t&&(t={});try{var s=t.nostr||globalThis.nostr;if(!s)throw new Error("nostr option or window.nostr is not available");var u=[["relays"].concat(i),["amount",r.toString()]];return o&&u.push(["p",o]),a&&u.push(["e",a]),Promise.resolve(s.getPublicKey()).then(function(e){var t={pubkey:e,created_at:Math.floor(Date.now()/1e3),kind:9734,tags:u,content:null!=n?n:""};return t.id=S(t),Promise.resolve(s.signEvent(t))})}catch(a){return Promise.reject(a)}};function j(e){if("string"!=typeof e.content)return!1;if("number"!=typeof e.created_at)return!1;if(!Array.isArray(e.tags))return!1;for(var t=0;t<e.tags.length;t++){var r=e.tags[t];if(!Array.isArray(r))return!1;for(var n=0;n<r.length;n++)if("object"==typeof r[n])return!1}return!0}function k(e){if(!j(e))throw new Error("can't serialize event with wrong or missing properties");return JSON.stringify([0,e.pubkey,e.created_at,e.kind,e.tags,e.content])}function S(e){return u.default(k(e)).toString(s.default)}function D(e,t){var r,n,o,a;return t&&e&&(n=(r=null==(o=e.names)?void 0:o[t])?null==(a=e.relays)?void 0:a[r]:void 0),[e,r,n]}var E={__proto__:null,generateZapEvent:P,validateEvent:j,serializeEvent:k,getEventHash:S,parseNostrResponse:D},x=/^((?:[^<>()\[\]\\.,;:\s@"]+(?:\.[^<>()\[\]\\.,;:\s@"]+)*)|(?:".+"))@((?:\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(?:(?:[a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,_=/*#__PURE__*/function(){function e(e,t){this.address=void 0,this.options=void 0,this.username=void 0,this.domain=void 0,this.pubkey=void 0,this.lnurlpData=void 0,this.keysendData=void 0,this.nostrData=void 0,this.nostrPubkey=void 0,this.nostrRelays=void 0,this.webln=void 0,this.address=e,this.options={proxy:"https://lnaddressproxy.getalby.com",webln:globalThis.webln},this.options=Object.assign(this.options,t),this.parse(),this.webln=this.options.webln}var t=e.prototype;return t.parse=function(){var e=x.exec(this.address.toLowerCase());e&&(this.username=e[1],this.domain=e[2])},t.fetch=function(){try{var e=this;return Promise.resolve(e.options.proxy?e.fetchWithProxy():e.fetchWithoutProxy())}catch(e){return Promise.reject(e)}},t.fetchWithProxy=function(){try{var e=this;return Promise.resolve(i.default(e.options.proxy+"/lightning-address-details?"+new URLSearchParams({ln:e.address}).toString())).then(function(t){return Promise.resolve(t.json()).then(function(t){e.parseResponse(t.lnurlp,t.keysend,t.nostr)})})}catch(e){return Promise.reject(e)}},t.fetchWithoutProxy=function(){try{var e=this;return e.domain&&e.username?Promise.resolve(i.default(e.lnurlpUrl())).then(function(t){return Promise.resolve(i.default(e.keysendUrl())).then(function(r){return Promise.resolve(i.default(e.nostrUrl())).then(function(n){function o(){function t(){function t(){e.parseResponse(a,o,r)}var r,i=function(){if(n.ok)return Promise.resolve(n.json()).then(function(e){r=e})}();return i&&i.then?i.then(t):t()}var o,i=function(){if(r.ok)return Promise.resolve(r.json()).then(function(e){o=e})}();return i&&i.then?i.then(t):t()}var a,i=function(){if(t.ok)return Promise.resolve(t.json()).then(function(e){a=e})}();return i&&i.then?i.then(o):o()})})}):Promise.resolve()}catch(e){return Promise.reject(e)}},t.lnurlpUrl=function(){return"https://"+this.domain+"/.well-known/lnurlp/"+this.username},t.keysendUrl=function(){return"https://"+this.domain+"/.well-known/keysend/"+this.username},t.nostrUrl=function(){return"https://"+this.domain+"/.well-known/nostr.json?name="+this.username},t.generateInvoice=function(e){try{var t,r=function(e){var r=t&&t.pr&&t.pr.toString();if(!r)throw new Error("Invalid pay service invoice");var n={pr:r};return t&&t.verify&&(n.verify=t.verify.toString()),new b(n)},n=this,o=function(){if(n.options.proxy)return Promise.resolve(i.default(n.options.proxy+"/generate-invoice?"+new URLSearchParams(p({ln:n.address},e)).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){t=e.invoice})});if(!n.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!n.lnurlpData.callback||!w(n.lnurlpData.callback))throw new Error("Valid callback does not exist in lnurlpData");var r=new URL(n.lnurlpData.callback);return r.search=new URLSearchParams(e).toString(),Promise.resolve(i.default(r)).then(function(e){return Promise.resolve(e.json()).then(function(e){t=e})})}();return Promise.resolve(o&&o.then?o.then(r):r())}catch(e){return Promise.reject(e)}},t.requestInvoice=function(e){try{var t=this;if(!t.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");var r=1e3*e.satoshi,n=t.lnurlpData,o=n.commentAllowed;if(!g({amount:r,min:n.min,max:n.max}))throw new Error("Invalid amount");if(e.comment&&o&&o>0&&e.comment.length>o)throw new Error("The comment length must be "+o+" characters or fewer");var a={amount:r.toString()};return e.comment&&(a.comment=e.comment),e.payerdata&&(a.payerdata=JSON.stringify(e.payerdata)),Promise.resolve(t.generateInvoice(a))}catch(e){return Promise.reject(e)}},t.boost=function(e,t){void 0===t&&(t=0);try{var r=this;if(!r.keysendData)throw new Error("No keysendData available. Please call fetch() first.");var n=r.keysendData;return Promise.resolve(v({destination:n.destination,customKey:n.customKey,customValue:n.customValue,amount:t,boost:e},{webln:r.webln}))}catch(e){return Promise.reject(e)}},t.zapInvoice=function(e,t){var r=e.satoshi,n=e.comment,o=e.relays,a=e.e;void 0===t&&(t={});try{var i=this;if(!i.lnurlpData)throw new Error("No lnurlpData available. Please call fetch() first.");if(!i.nostrPubkey)throw new Error("Nostr Pubkey is missing");var s=i.nostrPubkey,u=1e3*r,l=i.lnurlpData,c=l.allowsNostr;if(!g({amount:u,min:l.min,max:l.max}))throw new Error("Invalid amount");if(!c)throw new Error("Your provider does not support zaps");return Promise.resolve(P({satoshi:u,comment:n,p:s,e:a,relays:o},t)).then(function(e){var t={amount:u.toString(),nostr:JSON.stringify(e)};return Promise.resolve(i.generateInvoice(t))})}catch(a){return Promise.reject(a)}},t.zap=function(e,t){void 0===t&&(t={});try{var r=this,n=r.zapInvoice(e,t);if(!r.webln)throw new Error("WebLN not available");return Promise.resolve(r.webln.enable()).then(function(){var e=r.webln,t=e.sendPayment;return Promise.resolve(n).then(function(r){return t.call(e,r.paymentRequest)})})}catch(e){return Promise.reject(e)}},t.parseResponse=function(e,t,r){if(e&&(this.lnurlpData=function(e){if("payRequest"!==e.tag)throw new Error("Invalid pay service params");var t=(e.callback+"").trim();if(!w(t))throw new Error("Callback must be a valid url");var r,n,o=Math.ceil(Number(e.minSendable||0)),a=Math.floor(Number(e.maxSendable));if(!o||!a||o>a)throw new Error("Invalid pay service params");try{r=JSON.parse(e.metadata+""),n=u.default(e.metadata+"").toString(s.default)}catch(e){r=[],n=u.default("[]").toString(s.default)}for(var i="",l="",c="",h=0;h<r.length;h++){var f=r[h],m=f[0],v=f[1];switch(m){case"text/plain":l=v;break;case"text/identifier":c=v;break;case"image/png;base64":case"image/jpeg;base64":i="data:"+m+","+v}}var d,p=e.payerData;try{d=new URL(t).hostname}catch(e){}return{callback:t,fixed:o===a,min:o,max:a,domain:d,metadata:r,metadataHash:n,identifier:c,description:l,image:i,payerData:p,commentAllowed:Number(e.commentAllowed)||0,rawData:e,allowsNostr:e.allowsNostr||!1}}(e)),t&&(this.keysendData=function(e){if("keysend"!==e.tag)throw new Error("Invalid keysend params");if("OK"!==e.status)throw new Error("Keysend status not OK");if(!("customKey"in e.customData[0])||"696969"!=e.customData[0].customKey)throw new Error("Unable to find customKey");if(!("customValue"in e.customData[0])||!e.customData[0].customValue)throw new Error("Unable to find customValue");if(!e.pubkey)throw new Error("Pubkey does not exist");return{destination:e.pubkey,customKey:e.customData[0].customKey,customValue:e.customData[0].customValue}}(t)),r){var n=D(r,this.username);this.nostrData=n[0],this.nostrPubkey=n[1],this.nostrRelays=n[2]}},e}(),N=function(e){try{var t="https://getalby.com/api/rates/"+e.toLowerCase()+".json";return Promise.resolve(i.default(t)).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.rate_float/1e8})})}catch(e){return Promise.reject(e)}},A=function(e){var t=e.satoshi;return Promise.resolve(N(e.currency)).then(function(e){return Number(t)*e})},R={__proto__:null,getFiatBtcRate:N,getFiatValue:A,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(N(e.currency)).then(function(e){return Math.floor(Number(t)/e)})},getFormattedFiatValue:function(e){var t=e.currency,r=e.locale;return r||(r="en"),Promise.resolve(A({satoshi:e.satoshi,currency:t})).then(function(e){return e.toLocaleString(r,{style:"currency",currency:t})})}};e.Invoice=b,e.LightningAddress=_,e.boostagrams=d,e.fetchWithLsat=f,e.fiat=R,e.lsat=m,e.nostr=E,e.sendBoostagram=v}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -0,3 +1,4 @@ | ||
export * as Storage from "../utils/Storage"; | ||
export declare const fetchWithLsat: (url: string, fetchArgs: Record<string, any>, options: Record<string, any>) => Promise<Response>; | ||
export default fetchWithLsat; | ||
//# sourceMappingURL=fetch.d.ts.map |
{ | ||
"name": "alby-tools", | ||
"version": "2.2.1", | ||
"version": "2.3.0", | ||
"description": "Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
210891
407