alby-tools
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -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 s}from"light-bolt11-decoder";class n{constructor(t){this.storage=void 0,this.storage=t||{}}getItem(t){return this.storage[t]}setItem(t,e){this.storage[t]=e}}const r=async(e,a,s)=>{s||(s={});const r=s.webln||globalThis.webln;if(!r)throw new Error("WebLN is missing");let o=s.store||new n;a||(a={}),a.cache="no-store",a.mode="cors",a.headers||(a.headers={});const i=o.getItem(e);if(i){const s=JSON.parse(i);return a.headers.Authorization=`LSAT ${s.mac}:${s.preimage}`,await t(e,a)}a.headers["Accept-Authenticate"]="LSAT";const c=await t(e,a),l=c.headers.get("www-authenticate");if(!l)return c;const h=l.split(","),u=h[0].replace("LSAT macaroon=","").trim(),m=h[1].replace("invoice=","").trim();await r.enable();const y=await r.sendPayment(m);return o.setItem(e,JSON.stringify({mac:u,preimage:y.preimage})),a.headers.Authorization=`LSAT ${u}:${y.preimage}`,await t(e,a)};var o={__proto__:null,fetchWithLsat:r,default:r};const i=async(t,e)=>{let{boost:a,amount:s}=t;e||(e={});const n=e.webln||globalThis.webln;s||(s=Math.floor(a.value_msat/1e3));let r={destination:t.destination,amount:s,customRecords:{7629169:JSON.stringify(a)}};return t.customKey&&t.customValue&&(r.customRecords[t.customKey]=t.customValue),await n.enable(),await n.keysend(r)};var c={__proto__:null,boost:i,default:i};function l(){return l=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var a=arguments[e];for(var s in a)Object.prototype.hasOwnProperty.call(a,s)&&(t[s]=a[s])}return t},l.apply(this,arguments)}const h=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}},u=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,m=t=>!!t&&u.test(t),y=({amount:t,min:e,max:a})=>{const s=t>0&&t>=e&&t<=a;return s&&e===a?t===e:s},w=t=>{if("payRequest"!==t.tag)throw new Error("Invalid pay service params");const s=(t.callback+"").trim();if(!m(s))throw new Error("Callback must be a valid url");const n=Math.ceil(Number(t.minSendable||0)),r=Math.floor(Number(t.maxSendable));if(!n||!r||n>r)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 c,l="",h="",u="";for(let t=0;t<o.length;t++){const[e,a]=o[t];switch(e){case"text/plain":h=a;break;case"text/identifier":u=a;break;case"image/png;base64":case"image/jpeg;base64":l="data:"+e+","+a}}try{c=new URL(s).hostname}catch(t){}return{callback:s,fixed:n===r,min:n,max:r,domain:c,metadata:o,metadataHash:i,identifier:u,description:h,image:l,commentAllowed:Number(t.commentAllowed)||0,rawData:t,allowsNostr:t.allowsNostr||!1}};class d{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=s(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 s=a(e.parse(t)).toString(e);return this.paymentHash===s}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 p({satoshi:t,comment:e,p:a,e:s,relays:n},r={}){const o=r.nostr||globalThis.nostr;if(!o)throw new Error("nostr option or window.nostr is not available");const i=[["relays",...n],["amount",t.toString()]];a&&i.push(["p",a]),s&&i.push(["e",s]);const c={pubkey:await o.getPublicKey(),created_at:Math.floor(Date.now()/1e3),kind:9734,tags:i,content:null!=e?e:""};return c.id=v(c),await o.signEvent(c)}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)}var b={__proto__:null,generateZapEvent:p,validateEvent:f,serializeEvent:g,getEventHash:v};const 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,}))$/;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.lnurlpData={},this.keysendData={},this.nostrData=[],this.webln=this.options.webln}parse(){const t=k.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();var s,n;this.lnurlpData=w(a.lnurlp),this.keysendData=h(a.keysend),this.nostrData=a.nostr,this.username&&(this.nostrPubkey=null==(s=this.nostrData.names)?void 0:s[this.username],this.nostrRelays=this.nostrPubkey?null==(n=this.nostrData.relays)?void 0:n[this.nostrPubkey]:void 0)}async fetchWithoutProxy(){if(this.domain&&this.username){try{const e=await t(this.lnurlpUrl());this.lnurlpData=w(await e.json())}catch(t){}try{const e=await t(this.keysendUrl());this.keysendData=h(await e.json())}catch(t){}try{var e,a;const s=await t(this.nostrUrl()),n=await s.json();this.nostrData=n,this.nostrPubkey=null==(e=this.nostrData.names)?void 0:e[this.username],this.nostrRelays=this.nostrPubkey?null==(a=this.nostrData.relays)?void 0:a[this.nostrPubkey]:void 0}catch(t){}}}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 s=await t(`${this.options.proxy}/generate-invoice?${new URLSearchParams(l({ln:this.address},e)).toString()}`);a=(await s.json()).invoice}else{if(!this.lnurlpData.callback||!m(this.lnurlpData.callback))throw new Error("Valid callback does not exist in lnurlpData");const s=new URL(this.lnurlpData.callback);s.search=new URLSearchParams(e).toString();const n=await t(s);a=await n.json()}const s=a&&a.pr&&a.pr.toString();if(!s)throw new Error("Invalid pay service invoice");const n={pr:s};return a&&a.verify&&(n.verify=a.verify.toString()),new d(n)}async requestInvoice(t){const e=1e3*t.satoshi,{commentAllowed:a,min:s,max:n}=this.lnurlpData;if(!y({amount:e,min:s,max:n}))throw new Error("Invalid amount");if(t.comment&&a>0&&t.comment.length>a)throw new Error(`The comment length must be ${a} characters or fewer`);const r={amount:e.toString()};return t.comment&&(r.comment=t.comment),this.generateInvoice(r)}async boost(t,e=0){const{destination:a,customKey:s,customValue:n}=this.keysendData;return i({destination:a,customKey:s,customValue:n,amount:e,boost:t},{webln:this.webln})}async zapInvoice({satoshi:t,comment:e,relays:a,e:s},n={}){if(!this.nostrPubkey)throw new Error("Nostr Pubkey is missing");const r=this.nostrPubkey,o=1e3*t,{allowsNostr:i,min:c,max:l}=this.lnurlpData;if(!y({amount:o,min:c,max:l}))throw new Error("Invalid amount");if(!i)throw new Error("Your provider does not support zaps");const h=await p({satoshi:o,comment:e,p:r,e:s,relays:a},n),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)}}const S=async e=>{const a="https://getalby.com/api/rates/"+e.toLowerCase()+".json",s=await t(a);return(await s.json()).rate_float/1e8},P=async({satoshi:t,currency:e})=>{const a=await S(e);return Number(t)*a};var E={__proto__:null,getFiatBtcRate:S,getFiatValue:P,getSatoshiValue:async({amount:t,currency:e})=>{const a=await S(e);return Math.floor(Number(t)/a)},getFormattedFiatValue:async({satoshi:t,currency:e,locale:a})=>(a||(a="en"),(await P({satoshi:t,currency:e})).toLocaleString(a,{style:"currency",currency:e}))};export{d as Invoice,D as LightningAddress,c as boostagrams,r as fetchWithLsat,E as fiat,o as lsat,b 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 s}from"light-bolt11-decoder";class r{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,s)=>{s||(s={});const n=s.webln||globalThis.webln;if(!n)throw new Error("WebLN is missing");let o=s.store||new r;a||(a={}),a.cache="no-store",a.mode="cors",a.headers||(a.headers={});const i=o.getItem(e);if(i){const s=JSON.parse(i);return a.headers.Authorization=`LSAT ${s.mac}:${s.preimage}`,await t(e,a)}a.headers["Accept-Authenticate"]="LSAT";const c=await t(e,a),l=c.headers.get("www-authenticate");if(!l)return c;const h=l.split(","),u=h[0].replace("LSAT macaroon=","").trim(),m=h[1].replace("invoice=","").trim();await n.enable();const w=await n.sendPayment(m);return o.setItem(e,JSON.stringify({mac:u,preimage:w.preimage})),a.headers.Authorization=`LSAT ${u}:${w.preimage}`,await t(e,a)};var o={__proto__:null,fetchWithLsat:n,default:n};const i=async(t,e)=>{let{boost:a,amount:s}=t;e||(e={});const r=e.webln||globalThis.webln;s||(s=Math.floor(a.value_msat/1e3));let n={destination:t.destination,amount:s,customRecords:{7629169:JSON.stringify(a)}};return t.customKey&&t.customValue&&(n.customRecords[t.customKey]=t.customValue),await r.enable(),await r.keysend(n)};var c={__proto__:null,boost:i,default:i};function l(){return l=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var a=arguments[e];for(var s in a)Object.prototype.hasOwnProperty.call(a,s)&&(t[s]=a[s])}return t},l.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 s=t>0&&t>=e&&t<=a;return s&&e===a?t===e:s};class w{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=s(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 s=a(e.parse(t)).toString(e);return this.paymentHash===s}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 p({satoshi:t,comment:e,p:a,e:s,relays:r},n={}){const o=n.nostr||globalThis.nostr;if(!o)throw new Error("nostr option or window.nostr is not available");const i=[["relays",...r],["amount",t.toString()]];a&&i.push(["p",a]),s&&i.push(["e",s]);const c={pubkey:await o.getPublicKey(),created_at:Math.floor(Date.now()/1e3),kind:9734,tags:i,content:null!=e?e:""};return c.id=f(c),await o.signEvent(c)}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,s;var r,n;return e&&t&&(a=null==(r=t.names)?void 0:r[e],s=a?null==(n=t.relays)?void 0:n[a]:void 0),[t,a,s]}var v={__proto__:null,generateZapEvent:p,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.lnurlpData={},this.keysendData={},this.nostrData={},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()),s=await t(this.nostrUrl());let r,n,o;e.ok&&(r=await e.json()),a.ok&&(n=await a.json()),s.ok&&(o=await s.json()),this.parseResponse(r,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 s=await t(`${this.options.proxy}/generate-invoice?${new URLSearchParams(l({ln:this.address},e)).toString()}`);a=(await s.json()).invoice}else{if(!this.lnurlpData.callback||!u(this.lnurlpData.callback))throw new Error("Valid callback does not exist in lnurlpData");const s=new URL(this.lnurlpData.callback);s.search=new URLSearchParams(e).toString();const r=await t(s);a=await r.json()}const s=a&&a.pr&&a.pr.toString();if(!s)throw new Error("Invalid pay service invoice");const r={pr:s};return a&&a.verify&&(r.verify=a.verify.toString()),new w(r)}async requestInvoice(t){const e=1e3*t.satoshi,{commentAllowed:a,min:s,max:r}=this.lnurlpData;if(!m({amount:e,min:s,max:r}))throw new Error("Invalid amount");if(t.comment&&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),this.generateInvoice(n)}async boost(t,e=0){const{destination:a,customKey:s,customValue:r}=this.keysendData;return i({destination:a,customKey:s,customValue:r,amount:e,boost:t},{webln:this.webln})}async zapInvoice({satoshi:t,comment:e,relays:a,e:s},r={}){if(!this.nostrPubkey)throw new Error("Nostr Pubkey is missing");const n=this.nostrPubkey,o=1e3*t,{allowsNostr:i,min:c,max:l}=this.lnurlpData;if(!m({amount:o,min:c,max:l}))throw new Error("Invalid amount");if(!i)throw new Error("Your provider does not support zaps");const h=await p({satoshi:o,comment:e,p:n,e:s,relays:a},r),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,s,r){t&&(this.lnurlpData=(t=>{if("payRequest"!==t.tag)throw new Error("Invalid pay service params");const s=(t.callback+"").trim();if(!u(s))throw new Error("Callback must be a valid url");const r=Math.ceil(Number(t.minSendable||0)),n=Math.floor(Number(t.maxSendable));if(!r||!n||r>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 c,l="",h="",m="";for(let t=0;t<o.length;t++){const[e,a]=o[t];switch(e){case"text/plain":h=a;break;case"text/identifier":m=a;break;case"image/png;base64":case"image/jpeg;base64":l="data:"+e+","+a}}try{c=new URL(s).hostname}catch(t){}return{callback:s,fixed:r===n,min:r,max:n,domain:c,metadata:o,metadataHash:i,identifier:m,description:h,image:l,commentAllowed:Number(t.commentAllowed)||0,rawData:t,allowsNostr:t.allowsNostr||!1}})(t)),s&&(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}})(s)),r&&([this.nostrData,this.nostrPubkey,this.nostrRelays]=g(r,this.username))}}const S=async e=>{const a="https://getalby.com/api/rates/"+e.toLowerCase()+".json",s=await t(a);return(await s.json()).rate_float/1e8},E=async({satoshi:t,currency:e})=>{const a=await S(e);return Number(t)*a};var x={__proto__:null,getFiatBtcRate:S,getFiatValue:E,getSatoshiValue:async({amount:t,currency:e})=>{const a=await S(e);return Math.floor(Number(t)/a)},getFormattedFiatValue:async({satoshi:t,currency:e,locale:a})=>(a||(a="en"),(await E({satoshi:t,currency:e})).toLocaleString(a,{style:"currency",currency:e}))};export{w as Invoice,k as LightningAddress,c as boostagrams,n as fetchWithLsat,x as fiat,o as lsat,v as nostr,i as sendBoostagram}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
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=function(t,r,n){try{var a,i=function(n){return a?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||new o;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 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,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)}},u={__proto__:null,boost:s,default:s};function c(){return c=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},c.apply(this,arguments)}var l=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}},m=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,h=function(e){return!!e&&m.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},f=function(e){if("payRequest"!==e.tag)throw new Error("Invalid pay service params");var n=(e.callback+"").trim();if(!h(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="",v=0;v<o.length;v++){var f=o[v],y=f[0],d=f[1];switch(y){case"text/plain":l=d;break;case"text/identifier":m=d;break;case"image/png;base64":case"image/jpeg;base64":c="data:"+y+","+d}}try{u=new URL(n).hostname}catch(e){}return{callback:n,fixed:i===s,min:i,max:s,domain:u,metadata:o,metadataHash:a,identifier:m,description:l,image:c,commentAllowed:Number(e.commentAllowed)||0,rawData:e,allowsNostr:e.allowsNostr||!1}},y=/*#__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}(),d=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 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 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(!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 g(e){return r(w(e)).toString(t)}var P={__proto__:null,generateZapEvent:d,validateEvent:p,serializeEvent:w,getEventHash:g};function b(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var 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 t(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.lnurlpData={},this.keysendData={},this.nostrData=[],this.webln=this.options.webln}var r=t.prototype;return r.parse=function(){var e=k.exec(this.address.toLowerCase());e&&(this.username=e[1],this.domain=e[2])},r.fetch=function(){try{var e=this;return Promise.resolve(e.options.proxy?e.fetchWithProxy():e.fetchWithoutProxy())}catch(e){return Promise.reject(e)}},r.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){var r,n;t.lnurlpData=f(e.lnurlp),t.keysendData=l(e.keysend),t.nostrData=e.nostr,t.username&&(t.nostrPubkey=null==(r=t.nostrData.names)?void 0:r[t.username],t.nostrRelays=t.nostrPubkey?null==(n=t.nostrData.relays)?void 0:n[t.nostrPubkey]:void 0)})})}catch(e){return Promise.reject(e)}},r.fetchWithoutProxy=function(){try{var t=function(){function t(){var t=b(function(){return Promise.resolve(e(r.nostrUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){var t,n;r.nostrData=e,r.nostrPubkey=null==(t=r.nostrData.names)?void 0:t[r.username],r.nostrRelays=r.nostrPubkey?null==(n=r.nostrData.relays)?void 0:n[r.nostrPubkey]:void 0})})},function(){});if(t&&t.then)return t.then(function(){})}var n=b(function(){return Promise.resolve(e(r.keysendUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){r.keysendData=l(e)})})},function(){});return n&&n.then?n.then(t):t()},r=this;if(!r.domain||!r.username)return Promise.resolve();var n=b(function(){return Promise.resolve(e(r.lnurlpUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){r.lnurlpData=f(e)})})},function(){});return Promise.resolve(n&&n.then?n.then(t):t())}catch(e){return Promise.reject(e)}},r.lnurlpUrl=function(){return"https://"+this.domain+"/.well-known/lnurlp/"+this.username},r.keysendUrl=function(){return"https://"+this.domain+"/.well-known/keysend/"+this.username},r.nostrUrl=function(){return"https://"+this.domain+"/.well-known/nostr.json?name="+this.username},r.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 y(n)},o=this,a=function(){if(o.options.proxy)return Promise.resolve(e(o.options.proxy+"/generate-invoice?"+new URLSearchParams(c({ln:o.address},t)).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){r=e.invoice})});if(!o.lnurlpData.callback||!h(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)}},r.requestInvoice=function(e){try{var t=1e3*e.satoshi,r=this.lnurlpData,n=r.commentAllowed;if(!v({amount:t,min:r.min,max:r.max}))throw new Error("Invalid amount");if(e.comment&&n>0&&e.comment.length>n)throw new Error("The comment length must be "+n+" characters or fewer");var o={amount:t.toString()};return e.comment&&(o.comment=e.comment),Promise.resolve(this.generateInvoice(o))}catch(e){return Promise.reject(e)}},r.boost=function(e,t){void 0===t&&(t=0);try{var r=this.keysendData;return Promise.resolve(s({destination:r.destination,customKey:r.customKey,customValue:r.customValue,amount:t,boost:e},{webln:this.webln}))}catch(e){return Promise.reject(e)}},r.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.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(d({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)}},r.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}(),D=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)}},S=function(e){var t=e.satoshi;return Promise.resolve(D(e.currency)).then(function(e){return Number(t)*e})},E={__proto__:null,getFiatBtcRate:D,getFiatValue:S,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(D(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(S({satoshi:e.satoshi,currency:t})).then(function(e){return e.toLocaleString(r,{style:"currency",currency:t})})}};export{y as Invoice,j as LightningAddress,u as boostagrams,a as fetchWithLsat,E as fiat,i as lsat,P 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}(),i=function(t,r,n){try{var i,a=function(n){return i?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 i=o.split(","),a=i[0].replace("LSAT macaroon=","").trim(),c=i[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:a,preimage:n.preimage})),r.headers.Authorization="LSAT "+a+":"+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||new o;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 i=1,e})}}();return Promise.resolve(l&&l.then?l.then(a):a(l))}catch(e){return Promise.reject(e)}},a={__proto__:null,fetchWithLsat:i,default:i},s=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 i={destination:e.destination,amount:n,customRecords:{7629169:JSON.stringify(r)}};return e.customKey&&e.customValue&&(i.customRecords[e.customKey]=e.customValue),Promise.resolve(o.enable()).then(function(){return Promise.resolve(o.keysend(i))})}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 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},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 t=e.amount,r=e.min,n=e.max,o=t>0&&t>=r&&t<=n;return o&&r===n?t===r:o},v=/*#__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 i=o.prototype;return i.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)}},i.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}},i.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}(),f=function(e,t){var r=e.satoshi,n=e.comment,o=e.p,i=e.e,a=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(a),["amount",r.toString()]];return o&&u.push(["p",o]),i&&u.push(["e",i]),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=d(t),Promise.resolve(s.signEvent(t))})}catch(i){return Promise.reject(i)}};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 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 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 r(y(e)).toString(t)}function w(e,t){var r,n,o,i;return t&&e&&(n=(r=null==(o=e.names)?void 0:o[t])?null==(i=e.relays)?void 0:i[r]:void 0),[e,r,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,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.lnurlpData={},this.keysendData={},this.nostrData={},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 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(i,o,n)}var n,a=function(){if(e.ok)return Promise.resolve(e.json()).then(function(e){n=e})}();return a&&a.then?a.then(r):r()}var o,a=function(){if(n.ok)return Promise.resolve(n.json()).then(function(e){o=e})}();return a&&a.then?a.then(r):r()}var i,a=function(){if(r.ok)return Promise.resolve(r.json()).then(function(e){i=e})}();return a&&a.then?a.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 v(n)},o=this,i=function(){if(o.options.proxy)return Promise.resolve(e(o.options.proxy+"/generate-invoice?"+new URLSearchParams(c({ln:o.address},t)).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){r=e.invoice})});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(t).toString(),Promise.resolve(e(n)).then(function(e){return Promise.resolve(e.json()).then(function(e){r=e})})}();return Promise.resolve(i&&i.then?i.then(n):n())}catch(e){return Promise.reject(e)}},o.requestInvoice=function(e){try{var t=1e3*e.satoshi,r=this.lnurlpData,n=r.commentAllowed;if(!h({amount:t,min:r.min,max:r.max}))throw new Error("Invalid amount");if(e.comment&&n>0&&e.comment.length>n)throw new Error("The comment length must be "+n+" characters or fewer");var o={amount:t.toString()};return e.comment&&(o.comment=e.comment),Promise.resolve(this.generateInvoice(o))}catch(e){return Promise.reject(e)}},o.boost=function(e,t){void 0===t&&(t=0);try{var r=this.keysendData;return Promise.resolve(s({destination:r.destination,customKey:r.customKey,customValue:r.customValue,amount:t,boost:e},{webln:this.webln}))}catch(e){return Promise.reject(e)}},o.zapInvoice=function(e,t){var r=e.satoshi,n=e.comment,o=e.relays,i=e.e;void 0===t&&(t={});try{var a=this;if(!a.nostrPubkey)throw new Error("Nostr Pubkey is missing");var s=a.nostrPubkey,u=1e3*r,c=a.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:i,relays:o},t)).then(function(e){var t={amount:u.toString(),nostr:JSON.stringify(e)};return Promise.resolve(a.generateInvoice(t))})}catch(i){return Promise.reject(i)}},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(!m(n))throw new Error("Callback must be a valid url");var o,i,a=Math.ceil(Number(e.minSendable||0)),s=Math.floor(Number(e.maxSendable));if(!a||!s||a>s)throw new Error("Invalid pay service params");try{o=JSON.parse(e.metadata+""),i=r(e.metadata+"").toString(t)}catch(e){o=[],i=r("[]").toString(t)}for(var u,c="",l="",h="",v=0;v<o.length;v++){var f=o[v],p=f[0],y=f[1];switch(p){case"text/plain":l=y;break;case"text/identifier":h=y;break;case"image/png;base64":case"image/jpeg;base64":c="data:"+p+","+y}}try{u=new URL(n).hostname}catch(e){}return{callback:n,fixed:a===s,min:a,max:s,domain:u,metadata:o,metadataHash:i,identifier:h,description:l,image:c,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 i=w(o,this.username);this.nostrData=i[0],this.nostrPubkey=i[1],this.nostrRelays=i[2]}},n}(),k=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)}},j=function(e){var t=e.satoshi;return Promise.resolve(k(e.currency)).then(function(e){return Number(t)*e})},S={__proto__:null,getFiatBtcRate:k,getFiatValue:j,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(k(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(j({satoshi:e.satoshi,currency:t})).then(function(e){return e.toLocaleString(r,{style:"currency",currency:t})})}};export{v as Invoice,b as LightningAddress,u as boostagrams,i as fetchWithLsat,S as fiat,a as lsat,g as nostr,s 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),c=/*#__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}(),l=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(),c=o[1].replace("invoice=","").trim();return Promise.resolve(a.enable()).then(function(){return Promise.resolve(a.sendPayment(c)).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 c;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)}},h={__proto__:null,fetchWithLsat:l,default:l},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=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}},y=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,p=function(e){return!!e&&y.test(e)},w=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},g=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,c="",l="",h="",m=0;m<r.length;m++){var f=r[m],v=f[0],d=f[1];switch(v){case"text/plain":l=d;break;case"text/identifier":h=d;break;case"image/png;base64":case"image/jpeg;base64":c="data:"+v+","+d}}try{i=new URL(t).hostname}catch(e){}return{callback:t,fixed:o===a,min:o,max:a,domain:i,metadata:r,metadataHash:n,identifier:h,description:l,image:c,commentAllowed:Number(e.commentAllowed)||0,rawData:e,allowsNostr:e.allowsNostr||!1}},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=D(t),Promise.resolve(s.signEvent(t))})}catch(a){return Promise.reject(a)}};function k(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 j(e){if(!k(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 u.default(j(e)).toString(s.default)}var S={__proto__:null,generateZapEvent:P,validateEvent:k,serializeEvent:j,getEventHash:D};function x(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var E=/^((?:[^<>()\[\]\\.,;:\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.lnurlpData={},this.keysendData={},this.nostrData=[],this.webln=this.options.webln}var t=e.prototype;return t.parse=function(){var e=E.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){var r,n;e.lnurlpData=g(t.lnurlp),e.keysendData=d(t.keysend),e.nostrData=t.nostr,e.username&&(e.nostrPubkey=null==(r=e.nostrData.names)?void 0:r[e.username],e.nostrRelays=e.nostrPubkey?null==(n=e.nostrData.relays)?void 0:n[e.nostrPubkey]:void 0)})})}catch(e){return Promise.reject(e)}},t.fetchWithoutProxy=function(){try{var e=function(){function e(){var e=x(function(){return Promise.resolve(i.default(t.nostrUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){var r,n;t.nostrData=e,t.nostrPubkey=null==(r=t.nostrData.names)?void 0:r[t.username],t.nostrRelays=t.nostrPubkey?null==(n=t.nostrData.relays)?void 0:n[t.nostrPubkey]:void 0})})},function(){});if(e&&e.then)return e.then(function(){})}var r=x(function(){return Promise.resolve(i.default(t.keysendUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.keysendData=d(e)})})},function(){});return r&&r.then?r.then(e):e()},t=this;if(!t.domain||!t.username)return Promise.resolve();var r=x(function(){return Promise.resolve(i.default(t.lnurlpUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.lnurlpData=g(e)})})},function(){});return Promise.resolve(r&&r.then?r.then(e):e())}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(v({ln:n.address},e)).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){t=e.invoice})});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=1e3*e.satoshi,r=this.lnurlpData,n=r.commentAllowed;if(!w({amount:t,min:r.min,max:r.max}))throw new Error("Invalid amount");if(e.comment&&n>0&&e.comment.length>n)throw new Error("The comment length must be "+n+" characters or fewer");var o={amount:t.toString()};return e.comment&&(o.comment=e.comment),Promise.resolve(this.generateInvoice(o))}catch(e){return Promise.reject(e)}},t.boost=function(e,t){void 0===t&&(t=0);try{var r=this.keysendData;return Promise.resolve(m({destination:r.destination,customKey:r.customKey,customValue:r.customValue,amount:t,boost:e},{webln:this.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.nostrPubkey)throw new Error("Nostr Pubkey is missing");var s=i.nostrPubkey,u=1e3*r,c=i.lnurlpData,l=c.allowsNostr;if(!w({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(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)}},e}(),A=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)}},L=function(e){var t=e.satoshi;return Promise.resolve(A(e.currency)).then(function(e){return Number(t)*e})},N={__proto__:null,getFiatBtcRate:A,getFiatValue:L,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(A(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(L({satoshi:e.satoshi,currency:t})).then(function(e){return e.toLocaleString(r,{style:"currency",currency:t})})}};e.Invoice=b,e.LightningAddress=_,e.boostagrams=f,e.fetchWithLsat=l,e.fiat=N,e.lsat=h,e.nostr=S,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 i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=/*#__PURE__*/i(t),s=/*#__PURE__*/i(r),u=/*#__PURE__*/i(n),c=/*#__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}(),l=function(e,t,r){try{var n,o=function(r){return n?r:(t.headers["Accept-Authenticate"]="LSAT",Promise.resolve(a.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(),c=o[1].replace("invoice=","").trim();return Promise.resolve(i.enable()).then(function(){return Promise.resolve(i.sendPayment(c)).then(function(r){return s.setItem(e,JSON.stringify({mac:u,preimage:r.preimage})),t.headers.Authorization="LSAT "+u+":"+r.preimage,Promise.resolve(a.default(e,t))})})}))};r||(r={});var i=r.webln||globalThis.webln;if(!i)throw new Error("WebLN is missing");var s=r.store||new c;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(a.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)}},h={__proto__:null,fetchWithLsat:l,default:l},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 i={destination:e.destination,amount:n,customRecords:{7629169:JSON.stringify(r)}};return e.customKey&&e.customValue&&(i.customRecords[e.customKey]=e.customValue),Promise.resolve(o.enable()).then(function(){return Promise.resolve(o.keysend(i))})}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(a.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,i=e.e,a=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(a),["amount",r.toString()]];return o&&u.push(["p",o]),i&&u.push(["e",i]),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(i){return Promise.reject(i)}};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,i;return t&&e&&(n=(r=null==(o=e.names)?void 0:o[t])?null==(i=e.relays)?void 0:i[r]:void 0),[e,r,n]}var S={__proto__:null,generateZapEvent:g,validateEvent:b,serializeEvent:P,getEventHash:j,parseNostrResponse:k},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,}))$/,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.lnurlpData={},this.keysendData={},this.nostrData={},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(a.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(a.default(e.lnurlpUrl())).then(function(t){return Promise.resolve(a.default(e.keysendUrl())).then(function(r){return Promise.resolve(a.default(e.nostrUrl())).then(function(n){function o(){function t(){function t(){e.parseResponse(i,o,r)}var r,a=function(){if(n.ok)return Promise.resolve(n.json()).then(function(e){r=e})}();return a&&a.then?a.then(t):t()}var o,a=function(){if(r.ok)return Promise.resolve(r.json()).then(function(e){o=e})}();return a&&a.then?a.then(t):t()}var i,a=function(){if(t.ok)return Promise.resolve(t.json()).then(function(e){i=e})}();return a&&a.then?a.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(a.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.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(a.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=1e3*e.satoshi,r=this.lnurlpData,n=r.commentAllowed;if(!y({amount:t,min:r.min,max:r.max}))throw new Error("Invalid amount");if(e.comment&&n>0&&e.comment.length>n)throw new Error("The comment length must be "+n+" characters or fewer");var o={amount:t.toString()};return e.comment&&(o.comment=e.comment),Promise.resolve(this.generateInvoice(o))}catch(e){return Promise.reject(e)}},t.boost=function(e,t){void 0===t&&(t=0);try{var r=this.keysendData;return Promise.resolve(m({destination:r.destination,customKey:r.customKey,customValue:r.customValue,amount:t,boost:e},{webln:this.webln}))}catch(e){return Promise.reject(e)}},t.zapInvoice=function(e,t){var r=e.satoshi,n=e.comment,o=e.relays,i=e.e;void 0===t&&(t={});try{var a=this;if(!a.nostrPubkey)throw new Error("Nostr Pubkey is missing");var s=a.nostrPubkey,u=1e3*r,c=a.lnurlpData,l=c.allowsNostr;if(!y({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(g({satoshi:u,comment:n,p:s,e:i,relays:o},t)).then(function(e){var t={amount:u.toString(),nostr:JSON.stringify(e)};return Promise.resolve(a.generateInvoice(t))})}catch(i){return Promise.reject(i)}},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)),i=Math.floor(Number(e.maxSendable));if(!o||!i||o>i)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 a,c="",l="",h="",m=0;m<r.length;m++){var f=r[m],v=f[0],d=f[1];switch(v){case"text/plain":l=d;break;case"text/identifier":h=d;break;case"image/png;base64":case"image/jpeg;base64":c="data:"+v+","+d}}try{a=new URL(t).hostname}catch(e){}return{callback:t,fixed:o===i,min:o,max:i,domain:a,metadata:r,metadataHash:n,identifier:h,description:l,image:c,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}(),D=function(e){try{var t="https://getalby.com/api/rates/"+e.toLowerCase()+".json";return Promise.resolve(a.default(t)).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.rate_float/1e8})})}catch(e){return Promise.reject(e)}},_=function(e){var t=e.satoshi;return Promise.resolve(D(e.currency)).then(function(e){return Number(t)*e})},A={__proto__:null,getFiatBtcRate:D,getFiatValue:_,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(D(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(_({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=l,e.fiat=A,e.lsat=h,e.nostr=S,e.sendBoostagram=m}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -5,2 +5,3 @@ import Invoice from './invoice'; | ||
import { WebLNProvider, SendPaymentResponse } from "@webbtc/webln-types"; | ||
export declare const DEFAULT_PROXY = "https://lnaddressproxy.getalby.com"; | ||
declare type LightningAddressOptions = { | ||
@@ -35,4 +36,5 @@ proxy?: string | false; | ||
zap(args: ZapArgs, options?: ZapOptions): Promise<SendPaymentResponse>; | ||
private parseResponse; | ||
} | ||
export {}; | ||
//# sourceMappingURL=lightning-address.d.ts.map |
@@ -56,2 +56,6 @@ export declare type KeysendResponse = { | ||
}; | ||
export declare type NostrResponse = { | ||
names: Record<string, string>; | ||
relays: Record<string, string[]>; | ||
}; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -6,2 +6,3 @@ import { Event, ZapArgs, ZapOptions } from '../types'; | ||
export declare function getEventHash(event: Event): string; | ||
export declare function parseNostrResponse(nostrData: Record<string, string>, username: string | undefined): readonly [Record<string, string>, string | undefined, string[] | undefined]; | ||
//# sourceMappingURL=nostr.d.ts.map |
{ | ||
"name": "alby-tools", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -28,3 +28,3 @@ <p align="center"> | ||
const ln = new LightningAddress("satoshi@getalby.com"); | ||
const ln = new LightningAddress("hello@getalby.com"); | ||
@@ -46,3 +46,3 @@ // fetch the LNURL data | ||
const ln = new LightningAddress("satoshi@getalby.com"); | ||
const ln = new LightningAddress("hello@getalby.com"); | ||
@@ -62,3 +62,3 @@ await ln.fetch(); | ||
import { LightningAddress } from "alby-tools"; | ||
const ln = new LightningAddress("satoshi@getalby.com"); | ||
const ln = new LightningAddress("hello@getalby.com"); | ||
await ln.fetch(); | ||
@@ -65,0 +65,0 @@ |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
201338
39
364