alby-tools
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -8,3 +8,4 @@ import fetchWithLsat from "./lsat/fetch"; | ||
export * as fiat from "./utils/fiat"; | ||
export * as nostr from "./utils/nostr"; | ||
export { fetchWithLsat, sendBoostagram, LightningAddress, Invoice }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -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 n{constructor(t){this.storage=void 0,this.storage=t||{}}getItem(t){return this.storage[t]}setItem(t,e){this.storage[t]=e}}const s=async(e,a,r)=>{r||(r={});const s=r.webln||globalThis.webln;if(!s)throw new Error("WebLN is missing");let o=r.store||new n;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 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 s.enable();const y=await s.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:s,default:s};const i=async(t,e)=>{let{boost:a,amount:r}=t;e||(e={});const n=e.webln||globalThis.webln;r||(r=Math.floor(a.value_msat/1e3));let s={destination:t.destination,amount:r,customRecords:{7629169:JSON.stringify(a)}};return t.customKey&&t.customValue&&(s.customRecords[t.customKey]=t.customValue),await n.enable(),await n.keysend(s)};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 r in a)Object.prototype.hasOwnProperty.call(a,r)&&(t[r]=a[r])}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 r=t>0&&t>=e&&t<=a;return r&&e===a?t===e:r},w=t=>{if("payRequest"!==t.tag)throw new Error("Invalid pay service params");const r=(t.callback+"").trim();if(!m(r))throw new Error("Callback must be a valid url");const n=Math.ceil(Number(t.minSendable||0)),s=Math.floor(Number(t.maxSendable));if(!n||!s||n>s)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(r).hostname}catch(t){}return{callback:r,fixed:n===s,min:n,max:s,domain:c,metadata:o,metadataHash:i,identifier:u,description:h,image:l,commentAllowed:Number(t.commentAllowed)||0,rawData:t,allowsNostr:t.allowsNostr||!1}};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}}const d=/^((?:[^<>()\[\]\\.,;:\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 f{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.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.webln=this.options.webln}parse(){const t=d.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.lnurlpData=w(a.lnurlp),this.keysendData=h(a.keysend)}async fetchWithoutProxy(){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){}}lnurlpUrl(){return`https://${this.domain}/.well-known/lnurlp/${this.username}`}keysendUrl(){return`https://${this.domain}/.well-known/keysend/${this.username}`}async generateInvoice(e){let a;if(this.options.proxy){const r=await t(`${this.options.proxy}/generate-invoice?${new URLSearchParams(l({ln:this.address},e)).toString()}`);a=(await r.json()).invoice}else{if(!this.lnurlpData.callback||!m(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 n=await t(r);a=await n.json()}const r=a&&a.pr&&a.pr.toString();if(!r)throw new Error("Invalid pay service invoice");const n={pr:r};return a&&a.verify&&(n.verify=a.verify.toString()),new p(n)}async requestInvoice(t){const e=1e3*t.satoshi,{commentAllowed:a,min:r,max:n}=this.lnurlpData;if(!y({amount:e,min:r,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 s={amount:e.toString()};return t.comment&&(s.comment=t.comment),this.generateInvoice(s)}async boost(t,e=0){const{destination:a,customKey:r,customValue:n}=this.keysendData;return i({destination:a,customKey:r,customValue:n,amount:e,boost:t},{webln:this.webln})}async zapInvoice({satoshi:t,comment:r,relays:n,p:s,e:o}){const i=1e3*t,{allowsNostr:c,min:l,max:h}=this.lnurlpData;if(!y({amount:i,min:l,max:h}))throw new Error("Invalid amount");if(!c)throw new Error("Your provider does not support zaps");const u=await async function({satoshi:t,comment:r,p:n,e:s,relays:o}){if(!globalThis.nostr)throw new Error("Please use a nostr extension");const i=[["p",n],["relays",...o],["amount",t.toString()]];s&&i.push(["e",s]);const c={pubkey:await globalThis.nostr.getPublicKey(),created_at:Math.floor(Date.now()/1e3),kind:9734,tags:i,content:null!=r?r:""};return c.id=a(function(t){if(!function(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}(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])}(c)).toString(e),await globalThis.nostr.signEvent(c)}({satoshi:i,comment:r,p:s,e:o,relays:n}),m={amount:i.toString(),nostr:JSON.stringify(u)};return await this.generateInvoice(m)}async zap({satoshi:t,comment:e,relays:a,p:r,e:n}){const s=this.zapInvoice({satoshi:t,comment:e,relays:a,p:r,e:n});if(!this.webln)throw new Error("WebLN not available");return await this.webln.enable(),this.webln.sendPayment((await s).paymentRequest)}}const g=async e=>{const a="https://getalby.com/api/rates/"+e.toLowerCase()+".json",r=await t(a);return(await r.json()).rate_float/1e8},b=async({satoshi:t,currency:e})=>{const a=await g(e);return Number(t)*a};var v={__proto__:null,getFiatBtcRate:g,getFiatValue:b,getSatoshiValue:async({amount:t,currency:e})=>{const a=await g(e);return Math.floor(Number(t)/a)},getFormattedFiatValue:async({satoshi:t,currency:e,locale:a})=>(a||(a="en"),(await b({satoshi:t,currency:e})).toLocaleString(a,{style:"currency",currency:e}))};export{p as Invoice,f as LightningAddress,c as boostagrams,s as fetchWithLsat,v as fiat,o as lsat,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 n}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 s=async(e,a,n)=>{n||(n={});const s=n.webln||globalThis.webln;if(!s)throw new Error("WebLN is missing");let o=n.store||new r;a||(a={}),a.cache="no-store",a.mode="cors",a.headers||(a.headers={});const i=o.getItem(e);if(i){const n=JSON.parse(i);return a.headers.Authorization=`LSAT ${n.mac}:${n.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 s.enable();const w=await s.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:s,default:s};const i=async(t,e)=>{let{boost:a,amount:n}=t;e||(e={});const r=e.webln||globalThis.webln;n||(n=Math.floor(a.value_msat/1e3));let s={destination:t.destination,amount:n,customRecords:{7629169:JSON.stringify(a)}};return t.customKey&&t.customValue&&(s.customRecords[t.customKey]=t.customValue),await r.enable(),await r.keysend(s)};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 n in a)Object.prototype.hasOwnProperty.call(a,n)&&(t[n]=a[n])}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),w=({amount:t,min:e,max:a})=>{const n=t>0&&t>=e&&t<=a;return n&&e===a?t===e:n},y=t=>{if("payRequest"!==t.tag)throw new Error("Invalid pay service params");const n=(t.callback+"").trim();if(!m(n))throw new Error("Callback must be a valid url");const r=Math.ceil(Number(t.minSendable||0)),s=Math.floor(Number(t.maxSendable));if(!r||!s||r>s)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(n).hostname}catch(t){}return{callback:n,fixed:r===s,min:r,max:s,domain:c,metadata:o,metadataHash:i,identifier:u,description:h,image:l,commentAllowed:Number(t.commentAllowed)||0,rawData:t,allowsNostr:t.allowsNostr||!1}};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=n(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 n=a(e.parse(t)).toString(e);return this.paymentHash===n}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:n,relays:r},s={}){const o=s.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]),n&&i.push(["e",n]);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:d,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 S{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.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 n;this.lnurlpData=y(a.lnurlp),this.keysendData=h(a.keysend),this.nostrData=a.nostr.names,this.username&&(this.nostrPubkey=null==(n=this.nostrData)?void 0:n[this.username])}async fetchWithoutProxy(){if(this.domain&&this.username){try{const e=await t(this.lnurlpUrl());this.lnurlpData=y(await e.json())}catch(t){}try{const e=await t(this.keysendUrl());this.keysendData=h(await e.json())}catch(t){}try{var e;const a=await t(this.nostrUrl()),n=await a.json();this.nostrData=n.names,this.nostrPubkey=null==(e=this.nostrData)?void 0:e[this.username]}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 n=await t(`${this.options.proxy}/generate-invoice?${new URLSearchParams(l({ln:this.address},e)).toString()}`);a=(await n.json()).invoice}else{if(!this.lnurlpData.callback||!m(this.lnurlpData.callback))throw new Error("Valid callback does not exist in lnurlpData");const n=new URL(this.lnurlpData.callback);n.search=new URLSearchParams(e).toString();const r=await t(n);a=await r.json()}const n=a&&a.pr&&a.pr.toString();if(!n)throw new Error("Invalid pay service invoice");const r={pr:n};return a&&a.verify&&(r.verify=a.verify.toString()),new p(r)}async requestInvoice(t){const e=1e3*t.satoshi,{commentAllowed:a,min:n,max:r}=this.lnurlpData;if(!w({amount:e,min:n,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 s={amount:e.toString()};return t.comment&&(s.comment=t.comment),this.generateInvoice(s)}async boost(t,e=0){const{destination:a,customKey:n,customValue:r}=this.keysendData;return i({destination:a,customKey:n,customValue:r,amount:e,boost:t},{webln:this.webln})}async zapInvoice({satoshi:t,comment:e,relays:a,e:n},r={}){if(!this.nostrPubkey)throw new Error("Nostr Pubkey is missing");const s=this.nostrPubkey,o=1e3*t,{allowsNostr:i,min:c,max:l}=this.lnurlpData;if(!w({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 d({satoshi:o,comment:e,p:s,e:n,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)}}const D=async e=>{const a="https://getalby.com/api/rates/"+e.toLowerCase()+".json",n=await t(a);return(await n.json()).rate_float/1e8},E=async({satoshi:t,currency:e})=>{const a=await D(e);return Number(t)*a};var x={__proto__:null,getFiatBtcRate:D,getFiatValue:E,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 E({satoshi:t,currency:e})).toLocaleString(a,{style:"currency",currency:e}))};export{p as Invoice,S as LightningAddress,c as boostagrams,s as fetchWithLsat,x as fiat,o as lsat,b 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)},f=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=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="",f=0;f<o.length;f++){var v=o[f],p=v[0],y=v[1];switch(p){case"text/plain":l=y;break;case"text/identifier":m=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: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}},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}();function y(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var d=/^((?:[^<>()\[\]\\.,;:\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,}))$/,w=/*#__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.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.webln=this.options.webln}var o=n.prototype;return o.parse=function(){var e=d.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.lnurlpData=v(e.lnurlp),t.keysendData=l(e.keysend)})})}catch(e){return Promise.reject(e)}},o.fetchWithoutProxy=function(){try{var t=function(){var t=y(function(){return Promise.resolve(e(r.keysendUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){r.keysendData=l(e)})})},function(){});if(t&&t.then)return t.then(function(){})},r=this,n=y(function(){return Promise.resolve(e(r.lnurlpUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){r.lnurlpData=v(e)})})},function(){});return Promise.resolve(n&&n.then?n.then(t):t())}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.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(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)}},o.requestInvoice=function(e){try{var t=1e3*e.satoshi,r=this.lnurlpData,n=r.commentAllowed;if(!f({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){var n=e.satoshi,o=e.comment,a=e.relays,i=e.p,s=e.e;try{var u=this,c=1e3*n,l=u.lnurlpData,m=l.allowsNostr;if(!f({amount:c,min:l.min,max:l.max}))throw new Error("Invalid amount");if(!m)throw new Error("Your provider does not support zaps");return Promise.resolve(function(e){var n=e.satoshi,o=e.comment,a=e.p,i=e.e,s=e.relays;try{if(!globalThis.nostr)throw new Error("Please use a nostr extension");var u=[["p",a],["relays"].concat(s),["amount",n.toString()]];return i&&u.push(["e",i]),Promise.resolve(globalThis.nostr.getPublicKey()).then(function(e){var n={pubkey:e,created_at:Math.floor(Date.now()/1e3),kind:9734,tags:u,content:null!=o?o:""};return n.id=r(function(e){if(!function(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}(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])}(n)).toString(t),Promise.resolve(globalThis.nostr.signEvent(n))})}catch(i){return Promise.reject(i)}}({satoshi:c,comment:o,p:i,e:s,relays:a})).then(function(e){var t={amount:c.toString(),nostr:JSON.stringify(e)};return Promise.resolve(u.generateInvoice(t))})}catch(s){return Promise.reject(s)}},o.zap=function(e){var t=e.satoshi,r=e.comment,n=e.relays,o=e.p,a=e.e;try{var i=this,s=i.zapInvoice({satoshi:t,comment:r,relays:n,p:o,e:a});if(!i.webln)throw new Error("WebLN not available");return Promise.resolve(i.webln.enable()).then(function(){var e=i.webln,t=e.sendPayment;return Promise.resolve(s).then(function(r){return t.call(e,r.paymentRequest)})})}catch(a){return Promise.reject(a)}},n}(),g=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)}},b=function(e){var t=e.satoshi;return Promise.resolve(g(e.currency)).then(function(e){return Number(t)*e})},P={__proto__:null,getFiatBtcRate:g,getFiatValue:b,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(g(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(b({satoshi:e.satoshi,currency:t})).then(function(e){return e.toLocaleString(r,{style:"currency",currency:t})})}};export{p as Invoice,w as LightningAddress,u as boostagrams,a as fetchWithLsat,P as fiat,i as lsat,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=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,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="",m="",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":m=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:m,description:l,image:c,commentAllowed:Number(e.commentAllowed)||0,rawData:e,allowsNostr:e.allowsNostr||!1}},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 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}(),y=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=g(t),Promise.resolve(s.signEvent(t))})}catch(i){return Promise.reject(i)}};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)}var P={__proto__:null,generateZapEvent:y,validateEvent:d,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.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;t.lnurlpData=f(e.lnurlp),t.keysendData=l(e.keysend),t.nostrData=e.nostr.names,t.username&&(t.nostrPubkey=null==(r=t.nostrData)?void 0:r[t.username])})})}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;r.nostrData=e.names,r.nostrPubkey=null==(t=r.nostrData)?void 0:t[r.username]})})},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 p(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||!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(i&&i.then?i.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,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(!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: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)}},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}(),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,u as boostagrams,i as fetchWithLsat,E as fiat,a as lsat,P 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)},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},w=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}();function P(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var j=/^((?:[^<>()\[\]\\.,;:\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,}))$/,k=/*#__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.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.webln=this.options.webln}var t=e.prototype;return t.parse=function(){var e=j.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.lnurlpData=w(t.lnurlp),e.keysendData=d(t.keysend)})})}catch(e){return Promise.reject(e)}},t.fetchWithoutProxy=function(){try{var e=function(){var e=P(function(){return Promise.resolve(i.default(t.keysendUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.keysendData=d(e)})})},function(){});if(e&&e.then)return e.then(function(){})},t=this,r=P(function(){return Promise.resolve(i.default(t.lnurlpUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.lnurlpData=w(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.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(!g({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){var t=e.satoshi,r=e.comment,n=e.relays,o=e.p,a=e.e;try{var i=this,c=1e3*t,l=i.lnurlpData,h=l.allowsNostr;if(!g({amount:c,min:l.min,max:l.max}))throw new Error("Invalid amount");if(!h)throw new Error("Your provider does not support zaps");return Promise.resolve(function(e){var t=e.satoshi,r=e.comment,n=e.p,o=e.e,a=e.relays;try{if(!globalThis.nostr)throw new Error("Please use a nostr extension");var i=[["p",n],["relays"].concat(a),["amount",t.toString()]];return o&&i.push(["e",o]),Promise.resolve(globalThis.nostr.getPublicKey()).then(function(e){var t={pubkey:e,created_at:Math.floor(Date.now()/1e3),kind:9734,tags:i,content:null!=r?r:""};return t.id=u.default(function(e){if(!function(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}(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])}(t)).toString(s.default),Promise.resolve(globalThis.nostr.signEvent(t))})}catch(o){return Promise.reject(o)}}({satoshi:c,comment:r,p:o,e:a,relays:n})).then(function(e){var t={amount:c.toString(),nostr:JSON.stringify(e)};return Promise.resolve(i.generateInvoice(t))})}catch(a){return Promise.reject(a)}},t.zap=function(e){var t=e.satoshi,r=e.comment,n=e.relays,o=e.p,a=e.e;try{var i=this,s=i.zapInvoice({satoshi:t,comment:r,relays:n,p:o,e:a});if(!i.webln)throw new Error("WebLN not available");return Promise.resolve(i.webln.enable()).then(function(){var e=i.webln,t=e.sendPayment;return Promise.resolve(s).then(function(r){return t.call(e,r.paymentRequest)})})}catch(a){return Promise.reject(a)}},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)}},S=function(e){var t=e.satoshi;return Promise.resolve(x(e.currency)).then(function(e){return Number(t)*e})},D={__proto__:null,getFiatBtcRate:x,getFiatValue:S,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(S({satoshi:e.satoshi,currency:t})).then(function(e){return e.toLocaleString(r,{style:"currency",currency:t})})}};e.Invoice=b,e.LightningAddress=k,e.boostagrams=f,e.fetchWithLsat=l,e.fiat=D,e.lsat=h,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),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 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 D(e){return u.default(k(e)).toString(s.default)}var S={__proto__:null,generateZapEvent:P,validateEvent:j,serializeEvent:k,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.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;e.lnurlpData=g(t.lnurlp),e.keysendData=d(t.keysend),e.nostrData=t.nostr.names,e.username&&(e.nostrPubkey=null==(r=e.nostrData)?void 0:r[e.username])})})}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;t.nostrData=e.names,t.nostrPubkey=null==(r=t.nostrData)?void 0:r[t.username]})})},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}); | ||
//# sourceMappingURL=index.umd.js.map |
import Invoice from './invoice'; | ||
import { RequestInvoiceArgs, ZapArgs } from './types'; | ||
import { RequestInvoiceArgs, ZapArgs, ZapOptions } from './types'; | ||
import type { Boost } from './podcasting2/boostagrams'; | ||
@@ -17,2 +17,4 @@ import { WebLNProvider, SendPaymentResponse } from "@webbtc/webln-types"; | ||
keysendData: Record<string, any>; | ||
nostrData: [Record<string, string>?]; | ||
nostrPubkey: string | undefined; | ||
webln: WebLNProvider | undefined; | ||
@@ -26,9 +28,10 @@ constructor(address: string, options?: LightningAddressOptions); | ||
keysendUrl(): string; | ||
nostrUrl(): string; | ||
generateInvoice(params: Record<string, string>): Promise<Invoice>; | ||
requestInvoice(args: RequestInvoiceArgs): Promise<Invoice>; | ||
boost(boost: Boost, amount?: number): Promise<any>; | ||
zapInvoice({ satoshi, comment, relays, p, e }: ZapArgs): Promise<Invoice>; | ||
zap({ satoshi, comment, relays, p, e }: ZapArgs): Promise<SendPaymentResponse>; | ||
zapInvoice({ satoshi, comment, relays, e }: ZapArgs, options?: ZapOptions): Promise<Invoice>; | ||
zap(args: ZapArgs, options?: ZapOptions): Promise<SendPaymentResponse>; | ||
} | ||
export {}; | ||
//# sourceMappingURL=lightning-address.d.ts.map |
@@ -41,5 +41,12 @@ export declare type KeysendResponse = { | ||
relays: string[]; | ||
p: string; | ||
p?: string; | ||
e?: string; | ||
}; | ||
export declare type NostrProvider = { | ||
getPublicKey(): Promise<string>; | ||
signEvent(): Promise<Event>; | ||
}; | ||
export declare type ZapOptions = { | ||
nostr?: NostrProvider; | ||
}; | ||
export declare type RequestInvoiceArgs = { | ||
@@ -46,0 +53,0 @@ satoshi: number; |
@@ -1,3 +0,3 @@ | ||
import { Event, ZapArgs } from '../types'; | ||
export declare function generateZapEvent({ satoshi, comment, p, e, relays }: ZapArgs): Promise<Event>; | ||
import { Event, ZapArgs, ZapOptions } from '../types'; | ||
export declare function generateZapEvent({ satoshi, comment, p, e, relays }: ZapArgs, options?: ZapOptions): Promise<Event>; | ||
export declare function validateEvent(event: Event): boolean; | ||
@@ -4,0 +4,0 @@ export declare function serializeEvent(evt: Event): string; |
{ | ||
"name": "alby-tools", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -93,3 +93,3 @@ <p align="center"> | ||
#### Boost an LN address: | ||
#### Boost a LN address: | ||
@@ -119,3 +119,3 @@ You can also attach additional metadata information like app name, version, name of the podcast which is boosted etc. to the keysend payment. | ||
#### Zapping an event on Nostr: | ||
#### Zapping a LN address on Nostr: | ||
@@ -132,2 +132,6 @@ Nostr is a simple, open protocol that enables truly censorship-resistant and global value-for-value publishing on the web. Nostr integrates deeply with Lightning. [more info](https://nostr.how/) | ||
if (!ln.nostrPubkey) { | ||
alert('No nostr pubkey available'); // seems the lightning address is no NIP05 address | ||
} | ||
const zapArgs = { | ||
@@ -137,3 +141,2 @@ satoshi: 1000, | ||
relays: ["wss://relay.damus.io"], | ||
p: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245", | ||
e: "44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245" | ||
@@ -150,3 +153,2 @@ } | ||
await invoice.isPaid(); // check the payment status as descibed above | ||
``` | ||
@@ -153,0 +155,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
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
194472
355
190