ssi.nostr.signSync()

Callback type of sign.

Syntax

 window.ssi.nostr.signSync(
	message, // string
	options, // object
	callback, // object
)

Parameters

message

string. The message to sign. If it’s not a string it must be stringified.

options

object. Direction about sign detail.

type

signEvent. The signature spec. e.g., "signEvent".

callback

object. A reference to a function that should be called in the near future, when the result is returned. The callback function is passed two arguments — 1. Error object if failed otherwise null, 2. The resulting signature.

error

Error.

signature

string.

Return value

None (undefined).

Examples

Signing event in NIP-07

See also the spec.

const event = {
  kind: 1,
  content: "learning curve proceeds, API and DB schema changed largely. It's time to write document! \nDon't do it before developing except for spec summary and sequence :)",
  created_at: 1737375898,
  pubkey: "3589b793b977c4f025175afd792e7c51d26ef683b45cbc66c56c4d14ad53847e",
  tags: [],
}
const eventHash = bytesToHex(
  sha256(new TextEncoder().encode(JSON.stringify([
    0,
    event.pubkey,
    event.created_at,
    event.kind,
    event.tags,
    event.content,
  ])))
)

const callback = (error, signature) => {
  if (error) {
    throw new Error("Failed to sign")
  }

  console.log(signature)
}
window.ssi.nostr.signSync(
  eventHash,
  {
    type: "signEvent",
  },
  callback
)

// callback result
// "4034db40469721e4a5b95722a695bf943131cfab466f1a7f5a6aa70a3f8237dbacf08e06cc6a3f8dbe314313359450b64d75806dfd2e0bb7573ea6e68f43aa86"

In the WebExtension on Firefox

When you care about security and privacy, combining Sync method with Xray Vision can help prevent eavesdropping via postMessage and prototype chain pollution, with some trade-offs. See also “Share objects with page scripts”.

// In content-script

function signEvent(event) {
  event.pubkey = getPubkey()
  const eventHash = bytesToHex(
    sha256(new window.TextEncoder().encode(serializeEvent(event)))
  )
  return new window.Promise((resolve, reject) => {
    window.wrappedJSObject.ssi.nostr.signSync(
      window.JSON.stringify(event),
      cloneInto(
        {
          type: "signEvent",
        },
        window
      ),
      exportFunction((error, signature) => {
        if (error) {
          reject(error)
        }
        event.id = eventHash
        event.sig = signature
        resolve(cloneInto(event, window))
      }, window)
    )
    XPCNativeWrapper(window.wrappedJSObject.ssi)
  }, window)
}
window.wrappedJSObject.nostr.signEvent = exportFunction(signEvent, window)


// In page-script

const singedEvent = await window.nostr.signEvent({kind:1...})

Note

This documentation is derived from window.ssi.type.ts in gecko-dev-for-ssi.