Plan ยท area

@welshman/napplet

@welshman/napplet is a host core for NIP-5D written from scratch. It verifies napplets, binds sessions to ports and scopes, routes requests to domain handlers, and asks policies before acting. It knows nothing about relays, signers or UI, so the glue supplies those.

Why not Kehto#

Applesauce wraps Kehto's shell, and Grimoire works around it. The workarounds come from Kehto's core, so a welshman binding would inherit them, and fixing them upstream would mean rewriting Kehto's runtime. These comparisons were checked against Kehto's runtime 0.31.1 on 30 November.

Kehto@welshman/napplet
ConsentACL checks are synchronous. The runtime rejects the call, then asks.A policy returns a promise, so a request waits for the user's answer.
Default policyPermissive unless the host passes aclDefaultPolicyDeny unless a policy allows it
Signing consentThe destructive-signing type is declared and never raisedEvery publish passes through policy, with the kind and scope
Grant storageThe runtime persists all live grants on destroyGrants are data the shell stores. The core never persists anything.
Host integrationA ShellAdapter with hooks for relay tiers, a worker relay, hotkeys and windowsOne handler per domain, written against the shell's own SDK
ScopesNone. Napplets name scoped relays.Scopes are part of each session
Size and churnAbout 36,000 lines in shell, runtime and services, released about twice a weekReuses @kehto/nip and frame preparation, pinned exactly

The core reuses two Kehto modules: resolveNapplet from @kehto/nip/5d, which verifies signatures and artifact hashes, and prepareNappletSrcdoc, which the host page bundles. It uses @napplet/core for envelope types. @napplet/conformance keeps it compatible with other shells.

Modules#

  • packages
    • util/src
      • Kinds.tsNAPPLET_SNAPSHOT, NAPPLET_ROOT, NAPPLET
    • domain/src/kinds
      • Napplet.tsreader for the three manifest kinds
    • napplet/src
      • resolve.tsartifact fetch, verification and cache
      • session.tsport binding, identity, scope, teardown
      • dispatch.tsrouting by domain, results and errors
      • policy.tsrequests, decisions and combinators
      • scope.tsscope types, filter and template rewrites
      • catalog.tsinstalled napplets and intent matching
      • index.ts
    • napplet-host
      • host.htmlthe host page, a separate package

The manifest reader lives in @welshman/domain, beside the other kinds, so Flotilla can render manifest cards in M0 without loading the host core.

Sessions#

A session is one napplet instance: its identity, its scope and its port. The shell opens one when the host page hands over a port, and the core closes it when the host page reports a fault or the port closes.

sketchts
type Scope =
| {type: "global"}
| {type: "space"; url: string}
| {type: "room"; url: string; h: string}
type NappletIdentity = {address: string; artifactHash: string; pubkey: string}
type Session = {
id: string
napplet: NappletIdentity
scope: Scope
domains: string[]
}

The identity carries the author's pubkey and the napplet's address as well as the hash. That covers the NIP-5D review item about identities without a d tag, and it lets grants and storage use different keys, as the trust model describes.

Dispatch and policy#

Each domain has one handler. A handler turns a message into a policy request, waits for a decision, and then acts. Handlers return a promise for request and result pairs, or an async iterable for subscriptions.

sketchts
type PolicyRequest =
| {type: "read"; filters: Filter[]}
| {type: "publish"; template: EventTemplate; relays: string[]}
| {type: "upload"; size: number; mime: string}
| {type: "domain"; domain: string}
type Decision = "allow" | "deny" | "ask"
type Policy = (request: PolicyRequest, session: Session) => Promise<Maybe<Decision>>
type Decide = (request: PolicyRequest) => Promise<boolean>
type DomainHandler = (message: NappletMessage, session: Session, decide: Decide) =>
Promise<Maybe<NappletMessage>> | AsyncIterable<NappletMessage>
declare function createHost(options: {
domains: Record<string, DomainHandler>
policies: Policy[]
ask: (request: PolicyRequest, session: Session) => Promise<boolean>
}): NappletHost

The host asks each policy in order and uses the first decision. A request that no policy decides is denied, and an ask decision calls the shell's ask. Flotilla's policies are functions such as allowInstalled(spaceApps), refuseOutsideScope and askOncePerKind(grants).

Unknown message types are dropped silently, as NIP-5D requires. Failures in a known domain return the domain's error result, and the core defines one error shape for every handler, following the NIP-5D review item about errors.

Scopes#

scope.ts holds the rules that make one build work in every space:

  • scopeFilters(scope, filters) adds #h in a room
  • scopeTemplate(scope, template) adds h in a room
  • scopeRelays(scope, requested) returns the scope's relay, or refuses any other relay

The glue calls these functions before it touches the network. Public kinds that the scope exempts for reads, such as profiles and relay lists, are listed in one place so the policy and the handlers agree.

Intents and the catalog#

The catalog holds the napplets a shell has installed, with their verified manifests and the scopes they're installed in. It matches conventions exactly, so napplet:kind-30818/view and napplet:kind-1068/view find different napplets. The shell registers its own pages as handlers too, so napplet:profile/open reaches Flotilla's profile page and napplet:kind-1068/view reaches its native poll page.

Shell-initiated surfaces, such as opening a content type's list page, are deliveries the core sends on a new session after intent.onDelivery is registered.

Work#

ItemMilestone
Napplet manifest kinds in @welshman/utilM0
Napplet reader in @welshman/domainM0
resolve: fetch, verify and cache artifacts through @kehto/nipM1
session: port binding and teardown, with the host pageM1
dispatch: async routing by domain, correlation ids, errorsM1
policy: types and combinatorsM1
scope: scope types and the request and template rewritesM2
catalog: installed napplets and exact intent matchingM2
Conformance fixtures for P1 to P5M2