Skip to content

Commit

Permalink
feat: added webln support (#467)
Browse files Browse the repository at this point in the history
the boost functionality still only works for sphinx browser's

Co-authored-by: Github Actions <[email protected]>
  • Loading branch information
kevkevinpal and Github Actions authored Oct 2, 2023
1 parent e5537b8 commit 04bda9c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
58 changes: 44 additions & 14 deletions src/utils/getLSat/index.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -20,24 +21,30 @@ const ActionsMapper: Record<Action, string> = {
*/
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
Expand All @@ -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
}
35 changes: 35 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 04bda9c

Please sign in to comment.