diff --git a/package.json b/package.json index 6b2ae1f20..595119ee7 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "tone": "^14.7.77", "util": "^0.12.5", "web-vitals": "^2.1.0", + "webln": "^0.3.2", "zustand": "^4.1.1" }, "scripts": { diff --git a/src/utils/getLSat/index.ts b/src/utils/getLSat/index.ts index 6552bbe55..c2ee162b8 100644 --- a/src/utils/getLSat/index.ts +++ b/src/utils/getLSat/index.ts @@ -1,6 +1,7 @@ import { Lsat } from 'lsat-js' import * as sphinx from 'sphinx-bridge-kevkevinpal' import { API_URL } from '~/constants' +import { requestProvider } from 'webln' type Action = 'searching' | 'adding_node' | 'teachme' | 'ask_question' | 'sentiments' @@ -20,24 +21,30 @@ const ActionsMapper: Record = { */ export const getLSat = async (action: Action, search?: string) => { try { - const url = new URL(`${API_URL}/${action}`) - const searchParams = new URLSearchParams(search) + let webln - if (search) { - searchParams.forEach((value, key) => { - url.searchParams.append(key, value) - }) - } + try { + webln = await requestProvider() - const resp = await fetch(url, { - method: ActionsMapper[action] ?? 'GET', - }) + // getlsat invoice + const unpaidLsat = await getUnpaidLsat(action) + + // pay lsat invoice + const preimage = await webln.sendPayment(unpaidLsat.invoice) + + // create lsat + unpaidLsat.setPreimage(preimage.preimage) - const data = await resp.json() + const lsatToken = unpaidLsat.toToken() - const lsat = ['teachme', 'ask_question', 'sentiments'].includes(action) - ? Lsat.fromHeader(resp.headers.get('www-authenticate') || '') - : Lsat.fromHeader(data.headers) + return lsatToken + + // Now you can call all of the webln.* methods + } catch (err) { + // webln not enabled + } + + const lsat = await getUnpaidLsat(action, search) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -60,3 +67,26 @@ export const getLSat = async (action: Action, search?: string) => { return null } } + +export const getUnpaidLsat = async (action: Action, search?: string) => { + const url = new URL(`${API_URL}/${action}`) + const searchParams = new URLSearchParams(search) + + if (search) { + searchParams.forEach((value, key) => { + url.searchParams.append(key, value) + }) + } + + const resp = await fetch(url, { + method: ActionsMapper[action] ?? 'GET', + }) + + const data = await resp.json() + + const lsat = ['teachme', 'ask_question', 'sentiments'].includes(action) + ? Lsat.fromHeader(resp.headers.get('www-authenticate') || '') + : Lsat.fromHeader(data.headers) + + return lsat +} diff --git a/yarn.lock b/yarn.lock index 1bc649f41..636d3e394 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3730,6 +3730,15 @@ __metadata: languageName: node linkType: hard +"@types/chrome@npm:^0.0.74": + version: 0.0.74 + resolution: "@types/chrome@npm:0.0.74" + dependencies: + "@types/filesystem": "*" + checksum: 3c6659b9712200d7f7692b41250ae1581945ad9034ddb5875944ede59125aaaef918049934d534b0706a2632d3e79861e8861ba1f49a0397a2805b373b2a1232 + languageName: node + linkType: hard + "@types/d3-array@npm:*, @types/d3-array@npm:^3.0.3": version: 3.0.5 resolution: "@types/d3-array@npm:3.0.5" @@ -4026,6 +4035,22 @@ __metadata: languageName: node linkType: hard +"@types/filesystem@npm:*": + version: 0.0.33 + resolution: "@types/filesystem@npm:0.0.33" + dependencies: + "@types/filewriter": "*" + checksum: 49fc883317beef658d463994117676c0f494743c17d9d191e4c7c9764000490f7c657218b4a5427a076e399086cf6e2aa30e70fc6787a29646339813eb921a43 + languageName: node + linkType: hard + +"@types/filewriter@npm:*": + version: 0.0.30 + resolution: "@types/filewriter@npm:0.0.30" + checksum: 95d9484e1ba9ed8e2c670042ac49ddeab4c6f365e4297befdd9a3eee9596fd65808f064dd79e2a2ed2cecde46055c73736618d28e22d90dbdfd2f8be67d65ebe + languageName: node + linkType: hard + "@types/geojson@npm:*": version: 7946.0.10 resolution: "@types/geojson@npm:7946.0.10" @@ -13284,6 +13309,7 @@ __metadata: vite-plugin-svgr: ^3.2.0 vite-tsconfig-paths: ^4.2.0 web-vitals: ^2.1.0 + webln: ^0.3.2 zustand: ^4.1.1 languageName: unknown linkType: soft @@ -14696,6 +14722,15 @@ __metadata: languageName: node linkType: hard +"webln@npm:^0.3.2": + version: 0.3.2 + resolution: "webln@npm:0.3.2" + dependencies: + "@types/chrome": ^0.0.74 + checksum: 8aa2f773abb33baac8be824aeae2048fe04952bda98b8a21fc30b35ac8516cb6c85e4824390b199d467964b7df6bf34bbdc244443e7f0a3deaa433abed3f5dd0 + languageName: node + linkType: hard + "whatwg-encoding@npm:^2.0.0": version: 2.0.0 resolution: "whatwg-encoding@npm:2.0.0"