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