ssi.nostr.signWithCallback()

Callback type of sign.

Syntax

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

Parameters

message

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

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.

options

object. Direction about sign detail

type

signEvent. e.g., ‘signEvent’

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.signWithCallback(
  eventHash,
  callback,
  {
    type: "signEvent",
  },
)

// callback result
// "4034db40469721e4a5b95722a695bf943131cfab466f1a7f5a6aa70a3f8237dbacf08e06cc6a3f8dbe314313359450b64d75806dfd2e0bb7573ea6e68f43aa86"

In the WebExtension on Firefox

When you care about security and privacy, combining withCallback 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) {
  const signedEvent = { ...event };
  let eventHash = "";
  return new window.Promise((resolve, reject) => {
    // Attach your holding public key to verify it is the same as the current primary key.
    window.wrappedJSObject.ssi.nostr.getPublicKeyWithCallback(
      exportFunction((error, pubkey) => {
        if (error) {
          reject(error);
        }
        signedEvent.pubkey = pubkey;
        eventHash = bytesToHex(
          sha256(new window.TextEncoder().encode(serializeEvent(signedEvent)))
        );
        window.wrappedJSObject.ssi.nostr.signWithCallback(
          window.JSON.stringify(signedEvent),
          exportFunction((error, signature) => {
            if (error) {
              reject(error);
            }
            signedEvent.id = eventHash;
            signedEvent.sig = signature;
            resolve(cloneInto(signedEvent, window));
          }, window),
          cloneInto(
            {
              type: "signEvent",
            },
            window
          )
        );
        XPCNativeWrapper(window.wrappedJSObject.ssi);
      }, window)
    );
    XPCNativeWrapper(window.wrappedJSObject.ssi);
  });
}
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.