Fall back to server-side signing when no extension is available
This commit is contained in:
4 files changed
+93
-15
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { signAndPublish, hasNostrExtension } from "lib/nostr_publish"
|
||||
import { signAndPublish } from "lib/nostr_publish"
|
||||
import { canSign, signEvent } from "lib/nostr_signer"
|
||||
import { csrfToken } from "lib/csrf"
|
||||
|
||||
const PROFILE_KIND = 0
|
||||
@@ -43,8 +44,8 @@ export default class extends Controller {
|
||||
this.showError("Please enter a valid website URL, e.g. https://example.com.")
|
||||
return
|
||||
}
|
||||
if (!hasNostrExtension()) {
|
||||
this.showError("No Nostr extension (NIP-07) found.")
|
||||
if (!canSign()) {
|
||||
this.showError("No Nostr signer is available.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -98,7 +99,7 @@ export default class extends Controller {
|
||||
if (fields.length === 0) return
|
||||
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
const signedEvent = await window.nostr.signEvent({
|
||||
const signedEvent = await signEvent({
|
||||
kind: AUTH_KIND,
|
||||
created_at: now,
|
||||
tags: [
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { signAndPublish, hasNostrExtension } from "lib/nostr_publish"
|
||||
import { signAndPublish } from "lib/nostr_publish"
|
||||
import { canSign } from "lib/nostr_signer"
|
||||
import { updateAddedBadge } from "lib/added_badge"
|
||||
|
||||
export default class extends Controller {
|
||||
@@ -48,8 +49,8 @@ export default class extends Controller {
|
||||
this.showError(this.invalidMessageValue || "Please check the URLs.")
|
||||
return
|
||||
}
|
||||
if (!hasNostrExtension()) {
|
||||
this.showError("No Nostr extension (NIP-07) found.")
|
||||
if (!canSign()) {
|
||||
this.showError("No Nostr signer is available.")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
import { csrfToken } from "lib/csrf"
|
||||
import { canSign, signEvent } from "lib/nostr_signer"
|
||||
|
||||
const DEFAULT_URL = "/settings/nostr_publish"
|
||||
|
||||
export function hasNostrExtension () {
|
||||
return Boolean(window.nostr && window.nostr.signEvent)
|
||||
}
|
||||
|
||||
export async function signAndPublish (eventTemplate, { url = DEFAULT_URL } = {}) {
|
||||
if (!hasNostrExtension()) {
|
||||
throw new Error("No Nostr extension (NIP-07) found")
|
||||
if (!canSign()) {
|
||||
throw new Error("No Nostr signer is available")
|
||||
}
|
||||
|
||||
const signedEvent = await window.nostr.signEvent(eventTemplate)
|
||||
const signedEvent = await signEvent(eventTemplate)
|
||||
await publish(signedEvent, { url })
|
||||
return signedEvent
|
||||
}
|
||||
@@ -35,4 +32,4 @@ export async function publish (signedEvent, { url = DEFAULT_URL } = {}) {
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import { csrfToken } from "lib/csrf"
|
||||
|
||||
const SIGN_URL = "/settings/nostr_sign"
|
||||
const POLL_INTERVAL = 1500
|
||||
const MAX_ATTEMPTS = 120
|
||||
|
||||
export function hasNostrExtension () {
|
||||
return Boolean(window.nostr && window.nostr.signEvent)
|
||||
}
|
||||
|
||||
export function hasRemoteSigner () {
|
||||
return document.documentElement.dataset.remoteSigner === "true"
|
||||
}
|
||||
|
||||
export function canSign () {
|
||||
return hasNostrExtension() || hasRemoteSigner()
|
||||
}
|
||||
|
||||
export async function signEvent (eventTemplate) {
|
||||
if (hasNostrExtension()) {
|
||||
return window.nostr.signEvent(eventTemplate)
|
||||
}
|
||||
|
||||
if (!hasRemoteSigner()) {
|
||||
throw new Error("No Nostr signer is available")
|
||||
}
|
||||
|
||||
const created = await postJSON(SIGN_URL, { event: eventTemplate })
|
||||
if (!created.token) {
|
||||
throw new Error(created.error || "Could not start the signing request")
|
||||
}
|
||||
|
||||
return await pollSignedEvent(created.token)
|
||||
}
|
||||
|
||||
async function pollSignedEvent (token) {
|
||||
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
|
||||
const response = await fetch(`${SIGN_URL}/${encodeURIComponent(token)}`, {
|
||||
credentials: "include",
|
||||
headers: { "Accept": "application/json" }
|
||||
})
|
||||
const data = await response.json().catch(() => ({}))
|
||||
|
||||
if (data.status === "signed") {
|
||||
return data.signed_event
|
||||
}
|
||||
if (data.status === "failed" || data.status === "expired" || response.status === 410) {
|
||||
throw new Error(data.error || "Signing failed")
|
||||
}
|
||||
|
||||
await sleep(POLL_INTERVAL)
|
||||
}
|
||||
|
||||
throw new Error("Timed out waiting for your signer")
|
||||
}
|
||||
|
||||
async function postJSON (url, body) {
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
headers: {
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"X-CSRF-Token": csrfToken()
|
||||
},
|
||||
body: JSON.stringify(body)
|
||||
})
|
||||
const data = await response.json().catch(() => ({}))
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || `HTTP ${response.status}`)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
function sleep (ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
Reference in new issue
Block a user