Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added webln support #467

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
*/
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 @@ -55,8 +62,31 @@

return token
} catch (e) {
console.warn(e)

Check warning on line 65 in src/utils/getLSat/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run

Unexpected console statement

Check warning on line 65 in src/utils/getLSat/index.ts

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

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
Loading