ssi.nostr.encryptSync()
Callback type of encrypt.
Syntax
 window.ssi.nostr.encryptSync(
	plaintext, // string
	options, // object
	callback, // object
)
Parameters
plaintext
string. The plain text to encrypt. If it’s not a string it must be stringified.
options
object. Direction about encryption detail.
pubkey(optional)
string. The conversation partner’s public key. If type is"nip04"or"nip44", then this is required.type
NostrEncryptType. The encryption spec. e.g.,"nip04","nip44".version(optional)
string. The version to define encryption algorithms if the type is"nip44".
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 ciphertext.
error
Error.ciphertext
string.
Return value
None (undefined).
Examples
Encryption in NIP-44
See also the spec.
const callback = (error, ciphertext) => {
  if (error) {
    throw new Error("Failed to encrypt")
  }
  console.log(ciphertext)
}
window.ssi.nostr.encryptSync(
  "The computer can be used as a tool to liberate and protect people, rather than to control them.",
  {
    type: "nip44",
    pubkey: "3327e31cfbef92d143c699e1559e207d977639303d81bb132d9541bff99af3b4"
  },
  callback
)
// callback result
// "AkeXqSWNnU7VrlEUHnnGIs9rqXwHLFVxCsfQTRLbERVWh6fWJqfaRw/BC+cFgtfzPSle1csyfdJ+qf/xaCVmVQ2tXPQg6jw9EHwZxNUwz1EJYZStRo6uCXRnvXraMrPfd4Gx046tHyJ+KJIKUGXOFlWtyni+H+Kr151jvxt0PW5O48AMTxfos3/GxY/EF0yWwsJ8JG82JBEDrmzAz4ph8iXbJg=="
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 nip44Encrypt(pubkey, plaintext) {
  return new window.Promise((resolve, reject) => {
    window.wrappedJSObject.ssi.nostr.encryptSync(
      plaintext,
      cloneInto(
        {
          type: "nip44",
          pubkey,
        },
        window
      ),
      exportFunction((error, ciphertext) => {
        if (error) {
          reject(error)
        }
        resolve(ciphertext)
      }, window)
    )
    XPCNativeWrapper(window.wrappedJSObject.ssi)
  })
}
window.wrappedJSObject.nostr.nip44.encrypt = exportFunction(nip44Encrypt, window)
// In page-script
const ciphertext = await window.nostr.nip44.encrypt(pubkey, plaintext)
This documentation is derived from window.ssi.type.ts in gecko-dev-for-ssi.