Fix ignore disallowed conn reqs, fix publishAppPerms

This commit is contained in:
artur
2024-02-22 16:08:12 +03:00
parent b8a57c33d6
commit 17c1c13ad7
2 changed files with 50 additions and 15 deletions

View File

@@ -5,6 +5,7 @@ import NDK, {
NDKEvent, NDKEvent,
NDKNip46Backend, NDKNip46Backend,
NDKPrivateKeySigner, NDKPrivateKeySigner,
NDKRelaySet,
NDKSigner, NDKSigner,
NDKSubscription, NDKSubscription,
NDKSubscriptionCacheUsage, NDKSubscriptionCacheUsage,
@@ -20,6 +21,9 @@ import {
DOMAIN, DOMAIN,
REQ_TTL, REQ_TTL,
KIND_DATA, KIND_DATA,
OUTBOX_RELAYS,
BROADCAST_RELAY,
APP_TAG,
} from '../utils/consts' } from '../utils/consts'
// import { Nip04 } from './nip04' // import { Nip04 } from './nip04'
import { fetchNip05, getReqPerm, getShortenNpub, isPackagePerm } from '@/utils/helpers/helpers' import { fetchNip05, getReqPerm, getShortenNpub, isPackagePerm } from '@/utils/helpers/helpers'
@@ -67,11 +71,6 @@ interface IAllowCallbackParams {
params?: any params?: any
} }
const ndkGlobal = new NDK({
explicitRelayUrls: ['wss://relay.nostr.band', 'wss://nos.lol', 'wss://purplepag.es'],
})
ndkGlobal.connect()
class Watcher { class Watcher {
private ndk: NDK private ndk: NDK
private signer: NDKSigner private signer: NDKSigner
@@ -250,7 +249,7 @@ export class NoauthBackend {
private notifCallback: (() => void) | null = null private notifCallback: (() => void) | null = null
private pendingNpubEvents = new Map<string, NDKEvent[]>() private pendingNpubEvents = new Map<string, NDKEvent[]>()
private ndk = new NDK({ private ndk = new NDK({
explicitRelayUrls: NIP46_RELAYS, explicitRelayUrls: [...NIP46_RELAYS, ...OUTBOX_RELAYS, BROADCAST_RELAY],
enableOutboxModel: false, enableOutboxModel: false,
}) })
@@ -339,6 +338,29 @@ export class NoauthBackend {
// ensure we're subscribed on the server // ensure we're subscribed on the server
if (sub) await this.sendSubscriptionToServer(k.npub, sub) if (sub) await this.sendSubscriptionToServer(k.npub, sub)
} }
this.subscribeToAppPerms()
}
private async subscribeToAppPerms() {
const sub = this.ndk.subscribe(
{
kinds: [KIND_DATA],
'#t': [APP_TAG],
},
{
closeOnEose: false,
cacheUsage: NDKSubscriptionCacheUsage.ONLY_RELAY,
},
NDKRelaySet.fromRelayUrls(OUTBOX_RELAYS, this.ndk),
true // auto-start
)
sub.on('event', (e) => {
// parse,
// merge w/ existing apps/perms
// write to db
// if written - notify UI
})
} }
public setNotifCallback(cb: () => void) { public setNotifCallback(cb: () => void) {
@@ -726,6 +748,10 @@ export class NoauthBackend {
if (perm) { if (perm) {
console.log('req', req, 'perm', reqPerm, 'value', perm, appPerms) console.log('req', req, 'perm', reqPerm, 'value', perm, appPerms)
// connect reqs are always 'ignore' if were disallowed
if (perm.perm === 'connect' && perm.value === '0') return DECISION.IGNORE
// all other reqs are not ignored
return perm.value === '1' ? DECISION.ALLOW : DECISION.DISALLOW return perm.value === '1' ? DECISION.ALLOW : DECISION.DISALLOW
} }
@@ -750,26 +776,27 @@ export class NoauthBackend {
name: app.name, name: app.name,
icon: app.icon, icon: app.icon,
url: app.url, url: app.url,
timestamp: app.timestamp,
perms, perms,
} }
const id = await this.sha256(`nsec.app_${npub}_${appNpub}`) const id = await this.sha256(`nsec.app_${npub}_${appNpub}`)
const { type, data: pubkey } = nip19.decode(npub) const { type, data: pubkey } = nip19.decode(npub)
if (type !== 'npub') throw new Error('Bad npub') if (type !== 'npub') throw new Error('Bad npub')
const content = await key.signer.encrypt(new NDKUser({ pubkey }), JSON.stringify(data)) const content = await key.signer.encrypt(new NDKUser({ pubkey }), JSON.stringify(data))
const event = new NDKEvent(ndkGlobal, { const event = new NDKEvent(this.ndk, {
pubkey, pubkey,
kind: KIND_DATA, kind: KIND_DATA,
content, content,
created_at: Math.floor(Date.now() / 1000), created_at: Math.floor(Date.now() / 1000),
tags: [ tags: [
['d', id], ['d', id],
['t', 'nsec.app/perm'], ['t', APP_TAG],
], ],
}) })
event.sig = await event.sign(key.signer) event.sig = await event.sign(key.signer)
console.log('app perms event', event.rawEvent(), 'payload', data) console.log('app perms event', event.rawEvent(), 'payload', data)
await event.publish() const relays = await event.publish(NDKRelaySet.fromRelayUrls([...OUTBOX_RELAYS, BROADCAST_RELAY], this.ndk))
console.log('app perm event published', event.id) console.log('app perm event published', event.id, 'to', relays)
} }
private async connectApp({ private async connectApp({
@@ -1087,11 +1114,15 @@ export class NoauthBackend {
const { data: pubkey } = nip19.decode(npub) const { data: pubkey } = nip19.decode(npub)
const { data: appPubkey } = nip19.decode(appNpub) const { data: appPubkey } = nip19.decode(appNpub)
const events = await this.ndk.fetchEvents({ const events = await this.ndk.fetchEvents(
kinds: [KIND_RPC], {
'#p': [pubkey as string], kinds: [KIND_RPC],
authors: [appPubkey as string], '#p': [pubkey as string],
}) authors: [appPubkey as string],
},
undefined,
NDKRelaySet.fromRelayUrls(NIP46_RELAYS, this.ndk)
)
console.log('fetched pending for', npub, events.size) console.log('fetched pending for', npub, events.size)
this.pendingNpubEvents.set(npub, [...events.values()]) this.pendingNpubEvents.set(npub, [...events.values()])
} }

View File

@@ -3,6 +3,10 @@ export const WEB_PUSH_PUBKEY = process.env.REACT_APP_WEB_PUSH_PUBKEY
export const DOMAIN = process.env.REACT_APP_DOMAIN export const DOMAIN = process.env.REACT_APP_DOMAIN
export const RELAY = process.env.REACT_APP_RELAY || 'wss://relay.nsec.app' export const RELAY = process.env.REACT_APP_RELAY || 'wss://relay.nsec.app'
export const NIP46_RELAYS = [RELAY] export const NIP46_RELAYS = [RELAY]
export const OUTBOX_RELAYS = ['wss://relay.nostr.band', 'wss://nos.lol', 'wss://purplepag.es']
export const BROADCAST_RELAY = 'wss://nostr.mutinywallet.com'
export const APP_TAG = 'nsec.app/perm'
export const MIN_POW = 14 export const MIN_POW = 14
export const MAX_POW = 19 export const MAX_POW = 19