Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

alby-tools

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alby-tools - npm Package Compare versions

Comparing version 1.2.1 to 1.3.0

dist/utils/keysend.d.ts

2

dist/index.modern.js

@@ -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 i=r.store||new n;a||(a={}),a.cache="no-store",a.mode="cors",a.headers||(a.headers={});const o=i.getItem(e);if(o){const r=JSON.parse(o);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(","),m=h[0].replace("LSAT macaroon=","").trim(),u=h[1].replace("invoice=","").trim();await s.enable();const y=await s.sendPayment(u);return i.setItem(e,JSON.stringify({mac:m,preimage:y.preimage})),a.headers.Authorization=`LSAT ${m}:${y.preimage}`,await t(e,a)};var i={__proto__:null,fetchWithLsat:s,default:s};const o=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:o,default:o};const l=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,h=t=>!!t&&l.test(t),m=({amount:t,min:e,max:a})=>{const r=t>0&&t>=e&&t<=a;return r&&e===a?t===e:r},u=t=>{if("payRequest"!==t.tag)throw new Error("Invalid pay service params");const r=(t.callback+"").trim(),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 i,o;try{i=JSON.parse(t.metadata+""),o=a(t.metadata+"").toString(e)}catch(t){i=[],o=a("[]").toString(e)}let c,l="",h="",m="";for(let t=0;t<i.length;t++){const[e,a]=i[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(r).hostname}catch(t){}return{callback:r,fixed:n===s,min:n,max:s,domain:c,metadata:i,metadataHash:o,identifier:m,description:h,image:l,commentAllowed:Number(t.commentAllowed)||0,rawData:t,allowsNostr:t.allowsNostr||!1}};class y{constructor(t){var e,a;this.paymentRequest=void 0,this.paymentHash=void 0,this.preimage=void 0,this.verify=void 0,this.paymentRequest=t.pr,this.paymentHash=(t=>{if(!t)return null;try{const e=r(t);if(!e||!e.sections)return null;const a=e.sections.find(t=>"payment_hash"===t.name);return a&&a.value?a.value.toString():null}catch(t){return null}})(this.paymentRequest),this.verify=null!=(e=t.verify)?e:null,this.preimage=null!=(a=t.preimage)?a:null}async isPaid(){if(this.preimage)return this.validatePreimage(this.preimage);if(this.verify)return await this.verifyPayment();throw new Error("Could not verify payment")}validatePreimage(t){if(!t||!this.paymentHash)return!1;try{const r=a(e.parse(t)).toString(e);return this.paymentHash===r}catch(t){return!1}}async verifyPayment(){if(!this.verify)throw new Error("LNURL verify not available");const e=await t(this.verify),a=await e.json();return a.preimage&&(this.preimage=a.preimage),a.settled}}const 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,}))$/;class w{constructor(t,e){this.address=void 0,this.options=void 0,this.username=void 0,this.domain=void 0,this.pubkey=void 0,this.keysendData=void 0,this.lnurlpData=void 0,this.address=t,this.options={proxy:"https://lnaddressproxy.getalby.com/lightning-address-details"},this.options=Object.assign(this.options,e),this.parse(),this.lnurlpData={},this.keysendData={}}parse(){const t=p.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}?${new URLSearchParams({ln:this.address}).toString()}`),a=await e.json();this.lnurlpData=a.lnurlp,this.keysendData=a.keysend}async fetchWithoutProxy(){try{const e=await t(this.lnurlpUrl());this.lnurlpData=await e.json()}catch(t){}try{const e=await t(this.keysendUrl());this.keysendData=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){const a=await t(e),r=await a.json(),n=r&&r.pr&&r.pr.toString();if(!n)throw new Error("Invalid pay service invoice");const s={pr:n};return r&&r.verify&&(s.verify=r.verify.toString()),new y(s)}async requestInvoice(t){const e=1e3*t.satoshi,{callback:a,commentAllowed:r,min:n,max:s}=u(this.lnurlpData);if(!m({amount:e,min:n,max:s}))throw new Error("Invalid amount");if(!h(a))throw new Error("Callback must be a valid url");if(t.comment&&r>0&&t.comment.length>r)throw new Error(`The comment length must be ${r} characters or fewer`);const i={amount:e.toString()};t.comment&&(i.comment=t.comment);let o=new URL(a);return o.search=new URLSearchParams(i).toString(),this.generateInvoice(o)}async zap({satoshi:t,comment:r,relays:n,p:s,e:i}){const o=1e3*t,{callback:c,allowsNostr:l,min:y,max:p}=u(this.lnurlpData);if(!m({amount:o,min:y,max:p}))throw new Error("Invalid amount");if(!h(c))throw new Error("Callback must be a valid url");if(!l)throw new Error("Your provider does not support zaps");const w=await async function({satoshi:t,comment:r,p:n,e:s,relays:i}){if(!globalThis.nostr)throw new Error("Please use a nostr extension");const o=[["p",n],["relays",...i],["amount",t.toString()]];s&&o.push(["e",s]);const c={pubkey:await globalThis.nostr.getPublicKey(),created_at:Math.floor(Date.now()/1e3),kind:9734,tags:o,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:o,comment:r,p:s,e:i,relays:n}),d={amount:o.toString(),nostr:JSON.stringify(w)};let g=new URL(c);return g.search=new URLSearchParams(d).toString(),this.generateInvoice(g)}}const d=async e=>{const a="https://getalby.com/api/rates/"+e.toLowerCase()+".json",r=await t(a);return(await r.json()).rate_float/1e8},g=async({satoshi:t,currency:e})=>{const a=await d(e);return Number(t)*a};var f={__proto__:null,getFiatBtcRate:d,getFiatValue:g,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 g({satoshi:t,currency:e})).toLocaleString(a,{style:"currency",currency:e}))};export{y as Invoice,w as LightningAddress,c as boostagrams,s as fetchWithLsat,f as fiat,i as lsat,o as sendBoostagram};
import t from"cross-fetch";import e from"crypto-js/enc-hex.js";import a from"crypto-js/sha256.js";import{decode as r}from"light-bolt11-decoder";class s{constructor(t){this.storage=void 0,this.storage=t||{}}getItem(t){return this.storage[t]}setItem(t,e){this.storage[t]=e}}const n=async(e,a,r)=>{r||(r={});const n=r.webln||globalThis.webln;if(!n)throw new Error("WebLN is missing");let o=r.store||new s;a||(a={}),a.cache="no-store",a.mode="cors",a.headers||(a.headers={});const i=o.getItem(e);if(i){const r=JSON.parse(i);return a.headers.Authorization=`LSAT ${r.mac}:${r.preimage}`,await t(e,a)}a.headers["Accept-Authenticate"]="LSAT";const c=await t(e,a),l=c.headers.get("www-authenticate");if(!l)return c;const u=l.split(","),h=u[0].replace("LSAT macaroon=","").trim(),m=u[1].replace("invoice=","").trim();await n.enable();const y=await n.sendPayment(m);return o.setItem(e,JSON.stringify({mac:h,preimage:y.preimage})),a.headers.Authorization=`LSAT ${h}:${y.preimage}`,await t(e,a)};var o={__proto__:null,fetchWithLsat:n,default:n};const i=async(t,e)=>{let{boost:a,amount:r}=t;e||(e={});const s=e.webln||globalThis.webln;r||(r=Math.floor(a.value_msat/1e3));let n={destination:t.destination,amount:r,customRecords:{7629169:JSON.stringify(a)}};return t.customKey&&t.customValue&&(n.customRecords[t.customKey]=t.customValue),await s.enable(),await s.keysend(n)};var 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 u=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}},h=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,m=t=>!!t&&h.test(t),y=({amount:t,min:e,max:a})=>{const r=t>0&&t>=e&&t<=a;return r&&e===a?t===e:r},p=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 s=Math.ceil(Number(t.minSendable||0)),n=Math.floor(Number(t.maxSendable));if(!s||!n||s>n)throw new Error("Invalid pay service params");let o,i;try{o=JSON.parse(t.metadata+""),i=a(t.metadata+"").toString(e)}catch(t){o=[],i=a("[]").toString(e)}let c,l="",u="",h="";for(let t=0;t<o.length;t++){const[e,a]=o[t];switch(e){case"text/plain":u=a;break;case"text/identifier":h=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:s===n,min:s,max:n,domain:c,metadata:o,metadataHash:i,identifier:h,description:u,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=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 w=/^((?:[^<>()\[\]\\.,;:\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.address=t,this.options={proxy:"https://lnaddressproxy.getalby.com"},this.options=Object.assign(this.options,e),this.parse(),this.lnurlpData={},this.keysendData={}}parse(){const t=w.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=p(a.lnurlp),this.keysendData=u(a.keysend)}async fetchWithoutProxy(){try{const e=await t(this.lnurlpUrl());this.lnurlpData=p(await e.json())}catch(t){}try{const e=await t(this.keysendUrl());this.keysendData=u(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 s=await t(r);a=await s.json()}const r=a&&a.pr&&a.pr.toString();if(!r)throw new Error("Invalid pay service invoice");const s={pr:r};return a&&a.verify&&(s.verify=a.verify.toString()),new d(s)}async requestInvoice(t){const e=1e3*t.satoshi,{commentAllowed:a,min:r,max:s}=this.lnurlpData;if(!y({amount:e,min:r,max:s}))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:r,customValue:s}=this.keysendData;return i({destination:a,customKey:r,customValue:s,amount:e,boost:t})}async zap({satoshi:t,comment:r,relays:s,p:n,e:o}){const i=1e3*t,{allowsNostr:c,min:l,max:u}=this.lnurlpData;if(!y({amount:i,min:l,max:u}))throw new Error("Invalid amount");if(!c)throw new Error("Your provider does not support zaps");const h=await async function({satoshi:t,comment:r,p:s,e:n,relays:o}){if(!globalThis.nostr)throw new Error("Please use a nostr extension");const i=[["p",s],["relays",...o],["amount",t.toString()]];n&&i.push(["e",n]);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:n,e:o,relays:s}),m={amount:i.toString(),nostr:JSON.stringify(h)};return this.generateInvoice(m)}}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},v=async({satoshi:t,currency:e})=>{const a=await g(e);return Number(t)*a};var b={__proto__:null,getFiatBtcRate:g,getFiatValue:v,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 v({satoshi:t,currency:e})).toLocaleString(a,{style:"currency",currency:e}))};export{d as Invoice,f as LightningAddress,c as boostagrams,n as fetchWithLsat,b as fiat,o as lsat,i as sendBoostagram};
//# sourceMappingURL=index.modern.js.map

@@ -1,2 +0,2 @@

import e from"cross-fetch";import r from"crypto-js/enc-hex.js";import t from"crypto-js/sha256.js";import{decode as n}from"light-bolt11-decoder";var o=/*#__PURE__*/function(){function e(e){this.storage=void 0,this.storage=e||{}}var r=e.prototype;return r.getItem=function(e){return this.storage[e]},r.setItem=function(e,r){this.storage[e]=r},e}(),i=function(r,t,n){try{var i,a=function(n){return i?n:(t.headers["Accept-Authenticate"]="LSAT",Promise.resolve(e(r,t)).then(function(n){var o=n.headers.get("www-authenticate");if(!o)return n;var 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(r,JSON.stringify({mac:a,preimage:n.preimage})),t.headers.Authorization="LSAT "+a+":"+n.preimage,Promise.resolve(e(r,t))})})}))};n||(n={});var s=n.webln||globalThis.webln;if(!s)throw new Error("WebLN is missing");var u=n.store||new o;t||(t={}),t.cache="no-store",t.mode="cors",t.headers||(t.headers={});var c=u.getItem(r),l=function(){if(c){var n=JSON.parse(c);return t.headers.Authorization="LSAT "+n.mac+":"+n.preimage,Promise.resolve(e(r,t)).then(function(e){return 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,r){try{var t=e.boost,n=e.amount;r||(r={});var o=r.webln||globalThis.webln;n||(n=Math.floor(t.value_msat/1e3));var i={destination:e.destination,amount:n,customRecords:{7629169:JSON.stringify(t)}};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},c=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,l=function(e){return!!e&&c.test(e)},m=function(e){var r=e.amount,t=e.min,n=e.max,o=r>0&&r>=t&&r<=n;return o&&t===n?r===t:o},h=function(e){if("payRequest"!==e.tag)throw new Error("Invalid pay service params");var n,o,i=(e.callback+"").trim(),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{n=JSON.parse(e.metadata+""),o=t(e.metadata+"").toString(r)}catch(e){n=[],o=t("[]").toString(r)}for(var u,c="",l="",m="",h=0;h<n.length;h++){var f=n[h],v=f[0],p=f[1];switch(v){case"text/plain":l=p;break;case"text/identifier":m=p;break;case"image/png;base64":case"image/jpeg;base64":c="data:"+v+","+p}}try{u=new URL(i).hostname}catch(e){}return{callback:i,fixed:a===s,min:a,max:s,domain:u,metadata:n,metadataHash:o,identifier:m,description:l,image:c,commentAllowed:Number(e.commentAllowed)||0,rawData:e,allowsNostr:e.allowsNostr||!1}},f=/*#__PURE__*/function(){function o(e){var r,t;this.paymentRequest=void 0,this.paymentHash=void 0,this.preimage=void 0,this.verify=void 0,this.paymentRequest=e.pr,this.paymentHash=function(e){if(!e)return null;try{var r=n(e);if(!r||!r.sections)return null;var t=r.sections.find(function(e){return"payment_hash"===e.name});return t&&t.value?t.value.toString():null}catch(e){return null}}(this.paymentRequest),this.verify=null!=(r=e.verify)?r:null,this.preimage=null!=(t=e.preimage)?t:null}var 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=t(r.parse(e)).toString(r);return this.paymentHash===n}catch(e){return!1}},i.verifyPayment=function(){try{var r=this;if(!r.verify)throw new Error("LNURL verify not available");return Promise.resolve(e(r.verify)).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.preimage&&(r.preimage=e.preimage),e.settled})})}catch(e){return Promise.reject(e)}},o}();function v(e,r){try{var t=e()}catch(e){return r(e)}return t&&t.then?t.then(void 0,r):t}var 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,}))$/,y=/*#__PURE__*/function(){function n(e,r){this.address=void 0,this.options=void 0,this.username=void 0,this.domain=void 0,this.pubkey=void 0,this.keysendData=void 0,this.lnurlpData=void 0,this.address=e,this.options={proxy:"https://lnaddressproxy.getalby.com/lightning-address-details"},this.options=Object.assign(this.options,r),this.parse(),this.lnurlpData={},this.keysendData={}}var o=n.prototype;return o.parse=function(){var e=p.exec(this.address.toLowerCase());e&&(this.username=e[1],this.domain=e[2])},o.fetch=function(){try{var e=this;return Promise.resolve(e.options.proxy?e.fetchWithProxy():e.fetchWithoutProxy())}catch(e){return Promise.reject(e)}},o.fetchWithProxy=function(){try{var r=this;return Promise.resolve(e(r.options.proxy+"?"+new URLSearchParams({ln:r.address}).toString())).then(function(e){return Promise.resolve(e.json()).then(function(e){r.lnurlpData=e.lnurlp,r.keysendData=e.keysend})})}catch(e){return Promise.reject(e)}},o.fetchWithoutProxy=function(){try{var r=function(){var r=v(function(){return Promise.resolve(e(t.keysendUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.keysendData=e})})},function(){});if(r&&r.then)return r.then(function(){})},t=this,n=v(function(){return Promise.resolve(e(t.lnurlpUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.lnurlpData=e})})},function(){});return Promise.resolve(n&&n.then?n.then(r):r())}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(r){try{return Promise.resolve(e(r)).then(function(e){return Promise.resolve(e.json()).then(function(e){var r=e&&e.pr&&e.pr.toString();if(!r)throw new Error("Invalid pay service invoice");var t={pr:r};return e&&e.verify&&(t.verify=e.verify.toString()),new f(t)})})}catch(e){return Promise.reject(e)}},o.requestInvoice=function(e){try{var r=1e3*e.satoshi,t=h(this.lnurlpData),n=t.callback,o=t.commentAllowed;if(!m({amount:r,min:t.min,max:t.max}))throw new Error("Invalid amount");if(!l(n))throw new Error("Callback must be a valid url");if(e.comment&&o>0&&e.comment.length>o)throw new Error("The comment length must be "+o+" characters or fewer");var i={amount:r.toString()};e.comment&&(i.comment=e.comment);var a=new URL(n);return a.search=new URLSearchParams(i).toString(),Promise.resolve(this.generateInvoice(a))}catch(e){return Promise.reject(e)}},o.zap=function(e){var n=e.satoshi,o=e.comment,i=e.relays,a=e.p,s=e.e;try{var u=this,c=1e3*n,f=h(u.lnurlpData),v=f.callback,p=f.allowsNostr;if(!m({amount:c,min:f.min,max:f.max}))throw new Error("Invalid amount");if(!l(v))throw new Error("Callback must be a valid url");if(!p)throw new Error("Your provider does not support zaps");return Promise.resolve(function(e){var n=e.satoshi,o=e.comment,i=e.p,a=e.e,s=e.relays;try{if(!globalThis.nostr)throw new Error("Please use a nostr extension");var u=[["p",i],["relays"].concat(s),["amount",n.toString()]];return a&&u.push(["e",a]),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=t(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 r=0;r<e.tags.length;r++){var t=e.tags[r];if(!Array.isArray(t))return!1;for(var n=0;n<t.length;n++)if("object"==typeof t[n])return!1}return!0}(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(r),Promise.resolve(globalThis.nostr.signEvent(n))})}catch(a){return Promise.reject(a)}}({satoshi:c,comment:o,p:a,e:s,relays:i})).then(function(e){var r={amount:c.toString(),nostr:JSON.stringify(e)},t=new URL(v);return t.search=new URLSearchParams(r).toString(),u.generateInvoice(t)})}catch(s){return Promise.reject(s)}},n}(),d=function(r){try{var t="https://getalby.com/api/rates/"+r.toLowerCase()+".json";return Promise.resolve(e(t)).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.rate_float/1e8})})}catch(e){return Promise.reject(e)}},g=function(e){var r=e.satoshi;return Promise.resolve(d(e.currency)).then(function(e){return Number(r)*e})},w={__proto__:null,getFiatBtcRate:d,getFiatValue:g,getSatoshiValue:function(e){var r=e.amount;return Promise.resolve(d(e.currency)).then(function(e){return Math.floor(Number(r)/e)})},getFormattedFiatValue:function(e){var r=e.currency,t=e.locale;return t||(t="en"),Promise.resolve(g({satoshi:e.satoshi,currency:r})).then(function(e){return e.toLocaleString(t,{style:"currency",currency:r})})}};export{f as Invoice,y as LightningAddress,u as boostagrams,i as fetchWithLsat,w as fiat,a 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}(),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,}))$/,g=/*#__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.address=e,this.options={proxy:"https://lnaddressproxy.getalby.com"},this.options=Object.assign(this.options,t),this.parse(),this.lnurlpData={},this.keysendData={}}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}))}catch(e){return Promise.reject(e)}},o.zap=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 u.generateInvoice(t)})}catch(s){return Promise.reject(s)}},n}(),w=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)}},P=function(e){var t=e.satoshi;return Promise.resolve(w(e.currency)).then(function(e){return Number(t)*e})},b={__proto__:null,getFiatBtcRate:w,getFiatValue:P,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(w(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(P({satoshi:e.satoshi,currency:t})).then(function(e){return e.toLocaleString(r,{style:"currency",currency:t})})}};export{p as Invoice,g as LightningAddress,u as boostagrams,a as fetchWithLsat,b as fiat,i as lsat,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},v=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,d=function(e){return!!e&&v.test(e)},p=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},y=function(e){if("payRequest"!==e.tag)throw new Error("Invalid pay service params");var t,r,n=(e.callback+"").trim(),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{t=JSON.parse(e.metadata+""),r=u.default(e.metadata+"").toString(s.default)}catch(e){t=[],r=u.default("[]").toString(s.default)}for(var i,c="",l="",h="",m=0;m<t.length;m++){var f=t[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(n).hostname}catch(e){}return{callback:n,fixed:o===a,min:o,max:a,domain:i,metadata:t,metadataHash:r,identifier:h,description:l,image:c,commentAllowed:Number(e.commentAllowed)||0,rawData:e,allowsNostr:e.allowsNostr||!1}},g=/*#__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 w(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}var 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 e(e,t){this.address=void 0,this.options=void 0,this.username=void 0,this.domain=void 0,this.pubkey=void 0,this.keysendData=void 0,this.lnurlpData=void 0,this.address=e,this.options={proxy:"https://lnaddressproxy.getalby.com/lightning-address-details"},this.options=Object.assign(this.options,t),this.parse(),this.lnurlpData={},this.keysendData={}}var t=e.prototype;return t.parse=function(){var e=P.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+"?"+new URLSearchParams({ln:e.address}).toString())).then(function(t){return Promise.resolve(t.json()).then(function(t){e.lnurlpData=t.lnurlp,e.keysendData=t.keysend})})}catch(e){return Promise.reject(e)}},t.fetchWithoutProxy=function(){try{var e=function(){var e=w(function(){return Promise.resolve(i.default(t.keysendUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.keysendData=e})})},function(){});if(e&&e.then)return e.then(function(){})},t=this,r=w(function(){return Promise.resolve(i.default(t.lnurlpUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.lnurlpData=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{return Promise.resolve(i.default(e)).then(function(e){return Promise.resolve(e.json()).then(function(e){var t=e&&e.pr&&e.pr.toString();if(!t)throw new Error("Invalid pay service invoice");var r={pr:t};return e&&e.verify&&(r.verify=e.verify.toString()),new g(r)})})}catch(e){return Promise.reject(e)}},t.requestInvoice=function(e){try{var t=1e3*e.satoshi,r=y(this.lnurlpData),n=r.callback,o=r.commentAllowed;if(!p({amount:t,min:r.min,max:r.max}))throw new Error("Invalid amount");if(!d(n))throw new Error("Callback must be a valid url");if(e.comment&&o>0&&e.comment.length>o)throw new Error("The comment length must be "+o+" characters or fewer");var a={amount:t.toString()};e.comment&&(a.comment=e.comment);var i=new URL(n);return i.search=new URLSearchParams(a).toString(),Promise.resolve(this.generateInvoice(i))}catch(e){return Promise.reject(e)}},t.zap=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=y(i.lnurlpData),h=l.callback,m=l.allowsNostr;if(!p({amount:c,min:l.min,max:l.max}))throw new Error("Invalid amount");if(!d(h))throw new Error("Callback must be a valid url");if(!m)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)},r=new URL(h);return r.search=new URLSearchParams(t).toString(),i.generateInvoice(r)})}catch(a){return Promise.reject(a)}},e}(),j=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(j(e.currency)).then(function(e){return Number(t)*e})},x={__proto__:null,getFiatBtcRate:j,getFiatValue:S,getSatoshiValue:function(e){var t=e.amount;return Promise.resolve(j(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=g,e.LightningAddress=b,e.boostagrams=f,e.fetchWithLsat=l,e.fiat=x,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 d(){return d=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},d.apply(this,arguments)}var v=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],d=f[0],v=f[1];switch(d){case"text/plain":l=v;break;case"text/identifier":h=v;break;case"image/png;base64":case"image/jpeg;base64":c="data:"+d+","+v}}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}},P=/*#__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 b(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.address=e,this.options={proxy:"https://lnaddressproxy.getalby.com"},this.options=Object.assign(this.options,t),this.parse(),this.lnurlpData={},this.keysendData={}}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=v(t.keysend)})})}catch(e){return Promise.reject(e)}},t.fetchWithoutProxy=function(){try{var e=function(){var e=b(function(){return Promise.resolve(i.default(t.keysendUrl())).then(function(e){return Promise.resolve(e.json()).then(function(e){t.keysendData=v(e)})})},function(){});if(e&&e.then)return e.then(function(){})},t=this,r=b(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 P(n)},n=this,o=function(){if(n.options.proxy)return Promise.resolve(i.default(n.options.proxy+"/generate-invoice?"+new URLSearchParams(d({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}))}catch(e){return Promise.reject(e)}},t.zap=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 i.generateInvoice(t)})}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=P,e.LightningAddress=k,e.boostagrams=f,e.fetchWithLsat=l,e.fiat=D,e.lsat=h,e.sendBoostagram=m});
//# sourceMappingURL=index.umd.js.map
import Invoice from './invoice';
import { RequestInvoiceArgs, ZapArgs } from './types';
import type { Boost } from './podcasting2/boostagrams';
declare type LightningAddressOptions = {
proxy?: string | false;
};
export default class LightningAddress {
address: string;
options: {
proxy: string;
};
options: LightningAddressOptions;
username: string | undefined;
domain: string | undefined;
pubkey: string | undefined;
keysendData: unknown;
lnurlpData: Record<string, any>;
constructor(address: string, options: {
proxy: string;
});
keysendData: Record<string, any>;
constructor(address: string, options?: LightningAddressOptions);
parse(): void;

@@ -22,6 +22,8 @@ fetch(): Promise<void>;

keysendUrl(): string;
generateInvoice(url: URL): Promise<Invoice>;
generateInvoice(params: Record<string, string>): Promise<Invoice>;
requestInvoice(args: RequestInvoiceArgs): Promise<Invoice>;
boost(boost: Boost, amount?: number): Promise<any>;
zap({ satoshi, comment, relays, p, e }: ZapArgs): Promise<Invoice>;
}
export {};
//# sourceMappingURL=lightning-address.d.ts.map

@@ -11,3 +11,3 @@ declare type BoostOptions = {

};
declare type Boost = {
export declare type Boost = {
action: string;

@@ -14,0 +14,0 @@ value_msat: number;

@@ -0,1 +1,6 @@

export declare type KeysendResponse = {
customKey: string;
customValue: string;
destination: string;
};
export declare type LnUrlPayResponse = {

@@ -2,0 +7,0 @@ callback: string;

{
"name": "alby-tools",
"version": "1.2.1",
"version": "1.3.0",
"description": "Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps",

@@ -26,3 +26,3 @@ "type": "module",

"prepack": "yarn run build",
"test": "echo tests are missing",
"test": "jest",
"clean": "rm -rf dist",

@@ -39,4 +39,8 @@ "build": "microbundle",

"@types/crypto-js": "^4.1.1",
"@types/jest": "^29.4.0",
"@types/node": "^18.11.0",
"jest": "^29.5.0",
"microbundle": "^0.15.1",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"

@@ -43,0 +47,0 @@ },

@@ -26,3 +26,3 @@ <p align="center">

```js
const { LightningAddress } = require("alby-tools");
import { LightningAddress } from "alby-tools";

@@ -44,3 +44,3 @@ const ln = new LightningAddress("satoshi@getalby.com");

```js
const { LightningAddress } = require("alby-tools");
import { LightningAddress } from "alby-tools";

@@ -61,3 +61,3 @@ const ln = new LightningAddress("satoshi@getalby.com");

```js
const { LightningAddress } = require("alby-tools");
import { LightningAddress } from "alby-tools";
const ln = new LightningAddress("satoshi@getalby.com");

@@ -106,3 +106,3 @@ await ln.fetch();

```js
const { LightningAddress } = require("alby-tools");
import { LightningAddress } from "alby-tools";
const ln = new LightningAddress("satoshi@getalby.com");

@@ -128,2 +128,14 @@ await ln.fetch();

### Lightning Address Proxy
alby-tools uses a [proxy](https://github.com/getAlby/lightning-address-details-proxy) to simplify requests to lightning providers.
- Many ln addresses don't support CORS, which means fetching the data directly in a browser environment will not always work.
- Two requests are required to retrieve lnurlp and keysend data for a lightning address. The proxy will do these for you with a single request.
You can disable the proxy by explicitly setting the proxy to false when initializing a lightning address:
```
const lightningAddress = new LightningAddress("hello@getalby.com", {proxy: false});
```
#### Methods

@@ -130,0 +142,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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc