Using window.ssi

window.ssi is accessible anywhere that window is visible. This means that generally it can be accessed by web apps loaded in tab, and in-page scripts (in some cases content scripts as well) in web extensions, etc.

It mainly serves the access to user’s public key and the mediation to the tasks by user’s secret key.

Note

There are various ways to implement window.ssi, such as native window system, web extension, etc. Depending on how it’s implemented, it may also be injected into which frame. In any case it is always present in at least the top frame.

Note

If implemented by a web extension (currently so), window.ssi is inserted around the DOMContentLoaded event and it is the best that you access after the load event.

Basic usage

You call it in the same way as window.location, window.navigator, etc.

const publicKey = await window.ssi.nostr.getPublicKey()
const user = someNostrService.getUser(publicKey)

Listening to event

You listen the event as CustomEvent.

const accountChangedHandler = (event: CustomEvent<string>) => {
  const newPublicKey = event.detail
  doSomething(newPublicKey)
}
window.ssi.nostr.addEventListener("accountChanged", accountChangedHandler)

Wrapping in protocol standard

In most cases, you would wrap just the parts about the key management in a protocol standard like NIP-07.

const Nip07 = {
  async getPublicKey(): Promise<PulicKey> {
    return window.ssi.nostr.getPublicKey()
  },
  async signEvent(event: {
    kind: number
    content: string
    tags: string[][]
    created_at: number
  }): Promise<NostrEvent> {
    event.pubkey = await this.getPublicKey()
    const eventHash = serializeEvent(event)
    const sig = await window.ssi.nostr.sign(eventHash, { type: "signEvent", event })
    return {...event, id: eventHash, sig}
  }
}
window.nostr = Nip07