WIP Add Nostr auth

This commit is contained in:
2026-04-18 18:36:09 +04:00
parent 2268a607d5
commit f875fc1877
6 changed files with 1110 additions and 3 deletions

View File

@@ -0,0 +1,60 @@
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { ExtensionSigner } from 'applesauce-signers';
const STORAGE_KEY = 'marco:nostr_pubkey';
export default class NostrAuthService extends Service {
@tracked pubkey = null;
signer = null;
constructor() {
super(...arguments);
const saved = localStorage.getItem(STORAGE_KEY);
if (saved) {
this.pubkey = saved;
}
}
get isConnected() {
return !!this.pubkey;
}
async login() {
if (typeof window.nostr === 'undefined') {
throw new Error('No NIP-07 Nostr extension found (e.g., Alby, nos2x).');
}
if (!this.signer) {
this.signer = new ExtensionSigner();
}
try {
this.pubkey = await this.signer.getPublicKey();
localStorage.setItem(STORAGE_KEY, this.pubkey);
return this.pubkey;
} catch (error) {
console.error('Failed to get public key from extension:', error);
throw error;
}
}
async signEvent(event) {
if (!this.signer) {
if (this.pubkey && typeof window.nostr !== 'undefined') {
this.signer = new ExtensionSigner();
} else {
throw new Error(
'Not connected or extension missing. Please connect Nostr again.'
);
}
}
return await this.signer.signEvent(event);
}
logout() {
this.pubkey = null;
this.signer = null;
localStorage.removeItem(STORAGE_KEY);
}
}

View File

@@ -0,0 +1,25 @@
import Service from '@ember/service';
import { RelayPool } from 'applesauce-relay';
export default class NostrRelayService extends Service {
pool = new RelayPool();
// For Phase 1, we hardcode the local relay
relays = ['ws://127.0.0.1:7777'];
async publish(event) {
// The publish method is a wrapper around the event method that returns a Promise<PublishResponse[]>
// and automatically handles reconnecting and retrying.
const responses = await this.pool.publish(this.relays, event);
// Check if at least one relay accepted the event
const success = responses.some((res) => res.ok);
if (!success) {
throw new Error(
`Failed to publish event. Responses: ${JSON.stringify(responses)}`
);
}
return responses;
}
}