react-native-webln
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -9,6 +9,6 @@ "use strict"; | ||
exports.injectJs = (options = { debug: false, checkTags: true }) => { | ||
const pre = `window.reactNativeWebLNDebug = ${!!options.debug}; | ||
window.reactNativeWebLNCheckTags = ${!!options.checkTags};`; | ||
const pre = `window.reactNativeWebLNDebug = ${options.debug ?? false}; | ||
window.reactNativeWebLNCheckTags = ${options.checkTags ?? true};`; | ||
return pre + inject_txt_1.default; | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
declare const _default: "(() => {\n \"use strict\";\n if (!window.ReactNativeWebView) {\n return;\n }\n window.WebLN = window.WebLN || {};\n const WebLNPromiseCallback = {};\n const timeout = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));\n let requestId = 0;\n let weblnEnabled = false;\n window.WebLN.requestProvider = (async () => {\n if (weblnEnabled) {\n return window.WebLN;\n }\n const webln = {\n enable: async () => { return; },\n getInfo: async () => {\n return await postMessage({\n type: \"getInfo\",\n data: null,\n });\n },\n makeInvoice: async (args) => {\n const result = await postMessage({\n type: \"makeInvoice\",\n data: args,\n });\n checkedInvoices.push((\"lightning:\" + result.paymentRequest).toUpperCase());\n return result;\n },\n sendPayment: async (paymentRequest) => {\n return await postMessage({\n type: \"sendPayment\",\n data: paymentRequest,\n });\n },\n signMessage: async () => {\n return {\n message: \"\",\n signature: \"\",\n };\n },\n verifyMessage: async () => {\n return;\n },\n };\n window.WebLN = { ...window.WebLN, ...webln };\n weblnEnabled = true;\n return webln;\n });\n const postMessage = async (message, waitForCallback = true) => {\n const currentId = requestId++;\n window.ReactNativeWebView.postMessage(JSON.stringify({\n ...message,\n id: currentId,\n }));\n if (!waitForCallback) {\n return;\n }\n while (!WebLNPromiseCallback[currentId]) {\n await timeout(1000);\n }\n if (WebLNPromiseCallback[currentId] instanceof Error) {\n throw WebLNPromiseCallback[currentId];\n }\n return WebLNPromiseCallback[currentId];\n };\n document.addEventListener(\"webln\", (event) => {\n WebLNPromiseCallback[event.detail.id] = event.detail.data;\n });\n const checkedInvoices = [];\n if (window.reactNativeWebLNCheckTags) {\n const checkATags = async () => {\n if (weblnEnabled) {\n return;\n }\n const aTags = document.querySelectorAll(\"a\");\n for (const aTag of aTags) {\n if (aTag.href &&\n aTag.href.toUpperCase().startsWith(\"LIGHTNING:\") &&\n aTag.href.length > \"LIGHTNING:\".length) {\n debug(\"Found: \" + aTag.href);\n const invoice = aTag.href.toUpperCase().replace(\"LIGHTNING:\", \"\");\n if (checkedInvoices.includes(invoice)) {\n return;\n }\n checkedInvoices.push(invoice);\n await postMessage({\n type: \"nonwebln_foundInvoice\",\n data: invoice\n }, false);\n break;\n }\n }\n };\n const check = setInterval(() => {\n if (weblnEnabled) {\n clearInterval(check);\n }\n checkATags();\n }, 1000);\n }\n const debug = async (message) => {\n if (window.reactNativeWebLNDebug) {\n await postMessage({\n type: \"debug\",\n data: message,\n }, false);\n }\n };\n})();"; | ||
declare const _default: "(() => {\n \"use strict\";\n if (!window.ReactNativeWebView) {\n return;\n }\n const WebLNPromiseCallback = {};\n const timeout = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));\n let requestId = 0;\n let weblnEnabled = false;\n window.webln = {\n enable: async () => {\n weblnEnabled = true;\n return;\n },\n getInfo: async () => {\n return await postMessage({\n type: \"getInfo\",\n data: null,\n });\n },\n makeInvoice: async (args) => {\n const result = await postMessage({\n type: \"makeInvoice\",\n data: args,\n });\n checkedInvoices.push((\"lightning:\" + result.paymentRequest).toUpperCase());\n return result;\n },\n sendPayment: async (paymentRequest) => {\n return await postMessage({\n type: \"sendPayment\",\n data: paymentRequest,\n });\n },\n signMessage: async () => {\n return {\n message: \"\",\n signature: \"\",\n };\n },\n verifyMessage: async () => {\n return;\n },\n };\n const postMessage = async (message, waitForCallback = true) => {\n const currentId = requestId++;\n window.ReactNativeWebView.postMessage(JSON.stringify({\n ...message,\n id: currentId,\n }));\n if (!waitForCallback) {\n return;\n }\n while (!WebLNPromiseCallback[currentId]) {\n await timeout(1000);\n }\n if (WebLNPromiseCallback[currentId] instanceof Error) {\n throw WebLNPromiseCallback[currentId];\n }\n return WebLNPromiseCallback[currentId];\n };\n document.addEventListener(\"webln\", (event) => {\n WebLNPromiseCallback[event.detail.id] = event.detail.data;\n });\n const checkedInvoices = [];\n if (window.reactNativeWebLNCheckTags) {\n const checkATags = async () => {\n if (weblnEnabled) {\n return;\n }\n const aTags = document.querySelectorAll(\"a\");\n for (const aTag of aTags) {\n if (aTag.href &&\n aTag.href.toUpperCase().startsWith(\"LIGHTNING:\") &&\n aTag.href.length > \"LIGHTNING:\".length) {\n const invoice = aTag.href.toUpperCase().replace(\"LIGHTNING:\", \"\");\n if (checkedInvoices.includes(invoice)) {\n return;\n }\n debug(\"Found: \" + aTag.href);\n checkedInvoices.push(invoice);\n await postMessage({\n type: \"nonwebln_foundInvoice\",\n data: invoice\n }, false);\n break;\n }\n }\n };\n const check = setInterval(() => {\n if (weblnEnabled) {\n clearInterval(check);\n }\n checkATags();\n }, 900);\n }\n const debug = async (message) => {\n if (window.reactNativeWebLNDebug) {\n await postMessage({\n type: \"debug\",\n data: message,\n }, false);\n }\n };\n})();"; | ||
export default _default; |
@@ -8,3 +8,2 @@ "use strict"; | ||
} | ||
window.WebLN = window.WebLN || {}; | ||
const WebLNPromiseCallback = {}; | ||
@@ -14,42 +13,37 @@ const timeout = (time) => new Promise((resolve) => setTimeout(() => resolve(), time)); | ||
let weblnEnabled = false; | ||
window.WebLN.requestProvider = (async () => { | ||
if (weblnEnabled) { | ||
return window.WebLN; | ||
} | ||
const webln = { | ||
enable: async () => { return; }, | ||
getInfo: async () => { | ||
return await postMessage({ | ||
type: "getInfo", | ||
data: null, | ||
}); | ||
}, | ||
makeInvoice: async (args) => { | ||
const result = await postMessage({ | ||
type: "makeInvoice", | ||
data: args, | ||
}); | ||
checkedInvoices.push(("lightning:" + result.paymentRequest).toUpperCase()); | ||
return result; | ||
}, | ||
sendPayment: async (paymentRequest) => { | ||
return await postMessage({ | ||
type: "sendPayment", | ||
data: paymentRequest, | ||
}); | ||
}, | ||
signMessage: async () => { | ||
return { | ||
message: "", | ||
signature: "", | ||
}; | ||
}, | ||
verifyMessage: async () => { | ||
return; | ||
}, | ||
}; | ||
window.WebLN = { ...window.WebLN, ...webln }; | ||
weblnEnabled = true; | ||
return webln; | ||
}); | ||
window.webln = { | ||
enable: async () => { | ||
weblnEnabled = true; | ||
return; | ||
}, | ||
getInfo: async () => { | ||
return await postMessage({ | ||
type: "getInfo", | ||
data: null, | ||
}); | ||
}, | ||
makeInvoice: async (args) => { | ||
const result = await postMessage({ | ||
type: "makeInvoice", | ||
data: args, | ||
}); | ||
checkedInvoices.push(("lightning:" + result.paymentRequest).toUpperCase()); | ||
return result; | ||
}, | ||
sendPayment: async (paymentRequest) => { | ||
return await postMessage({ | ||
type: "sendPayment", | ||
data: paymentRequest, | ||
}); | ||
}, | ||
signMessage: async () => { | ||
return { | ||
message: "", | ||
signature: "", | ||
}; | ||
}, | ||
verifyMessage: async () => { | ||
return; | ||
}, | ||
}; | ||
const postMessage = async (message, waitForCallback = true) => { | ||
@@ -86,3 +80,2 @@ const currentId = requestId++; | ||
aTag.href.length > "LIGHTNING:".length) { | ||
debug("Found: " + aTag.href); | ||
const invoice = aTag.href.toUpperCase().replace("LIGHTNING:", ""); | ||
@@ -92,2 +85,3 @@ if (checkedInvoices.includes(invoice)) { | ||
} | ||
debug("Found: " + aTag.href); | ||
checkedInvoices.push(invoice); | ||
@@ -107,3 +101,3 @@ await postMessage({ | ||
checkATags(); | ||
}, 1000); | ||
}, 900); | ||
} | ||
@@ -110,0 +104,0 @@ const debug = async (message) => { |
@@ -1,2 +0,2 @@ | ||
import { requestProvider, WebLNProvider, RequestInvoiceResponse } from "webln"; | ||
import { WebLNProvider, RequestInvoiceResponse } from "webln"; | ||
@@ -6,3 +6,4 @@ declare global { | ||
ReactNativeWebView: any; | ||
WebLN: WebLNProvider & { requestProvider: typeof requestProvider; }; | ||
WebLN: WebLNProvider; | ||
webln: WebLNProvider; | ||
reactNativeWebLNDebug?: boolean; | ||
@@ -34,3 +35,2 @@ reactNativeWebLNCheckTags?: boolean; | ||
} | ||
window.WebLN = window.WebLN || {}; | ||
const WebLNPromiseCallback: { [key: string]: Promise<any> } = {}; | ||
@@ -40,45 +40,38 @@ const timeout = (time: number) => new Promise((resolve) => setTimeout(() => resolve(), time)); | ||
let weblnEnabled = false; | ||
window.webln = { | ||
enable: async () => { | ||
weblnEnabled = true; | ||
return; | ||
}, | ||
getInfo: async () => { | ||
return await postMessage({ | ||
type: "getInfo", | ||
data: null, | ||
}); | ||
}, | ||
makeInvoice: async (args) => { | ||
const result: RequestInvoiceResponse = await postMessage({ | ||
type: "makeInvoice", | ||
data: args, | ||
}); | ||
checkedInvoices.push(("lightning:" + result.paymentRequest).toUpperCase()); | ||
return result; | ||
}, | ||
sendPayment: async (paymentRequest) => { | ||
return await postMessage({ | ||
type: "sendPayment", | ||
data: paymentRequest, | ||
}); | ||
}, | ||
signMessage: async () => { | ||
return { | ||
message: "", | ||
signature: "", | ||
}; | ||
}, | ||
verifyMessage: async () => { | ||
return; | ||
}, | ||
}; | ||
window.WebLN.requestProvider = (async () => { | ||
if (weblnEnabled) { | ||
return window.WebLN; | ||
} | ||
const webln: WebLNProvider = { | ||
enable: async () => { return; }, | ||
getInfo: async () => { | ||
return await postMessage({ | ||
type: "getInfo", | ||
data: null, | ||
}); | ||
}, | ||
makeInvoice: async (args) => { | ||
const result: RequestInvoiceResponse = await postMessage({ | ||
type: "makeInvoice", | ||
data: args, | ||
}); | ||
checkedInvoices.push(("lightning:" + result.paymentRequest).toUpperCase()); | ||
return result; | ||
}, | ||
sendPayment: async (paymentRequest) => { | ||
return await postMessage({ | ||
type: "sendPayment", | ||
data: paymentRequest, | ||
}); | ||
}, | ||
signMessage: async () => { | ||
return { | ||
message: "", | ||
signature: "", | ||
}; | ||
}, | ||
verifyMessage: async () => { | ||
return; | ||
}, | ||
}; | ||
window.WebLN = { ...window.WebLN, ...webln }; | ||
weblnEnabled = true; | ||
return webln; | ||
}) as typeof requestProvider; | ||
const postMessage = async (message: PostMessage, waitForCallback = true) => { | ||
@@ -125,3 +118,2 @@ const currentId = requestId++; | ||
) { | ||
debug("Found: " + aTag.href); | ||
const invoice = aTag.href.toUpperCase().replace("LIGHTNING:", ""); | ||
@@ -131,2 +123,3 @@ if (checkedInvoices.includes(invoice)) { | ||
} | ||
debug("Found: " + aTag.href); | ||
checkedInvoices.push(invoice); | ||
@@ -147,3 +140,3 @@ await postMessage({ | ||
checkATags(); | ||
}, 1000); | ||
}, 900); | ||
} | ||
@@ -150,0 +143,0 @@ |
@@ -11,6 +11,6 @@ export * from "./postMessage-handler"; | ||
const pre = | ||
`window.reactNativeWebLNDebug = ${!!options.debug}; | ||
window.reactNativeWebLNCheckTags = ${!!options.checkTags};`; | ||
`window.reactNativeWebLNDebug = ${options.debug ?? false}; | ||
window.reactNativeWebLNCheckTags = ${options.checkTags ?? true};`; | ||
return pre + WebviewInjection; | ||
} |
{ | ||
"name": "react-native-webln", | ||
"description": "Library for supporting WebLN in a WebView", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"main": "dist/index", | ||
@@ -6,0 +6,0 @@ "scripts": { |
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
26475
498