signer.mjs raw

   1  // TinyJS Runtime — NIP-07 Browser Signer Bridge
   2  
   3  export function HasSigner() {
   4    return typeof window !== 'undefined' && !!window.nostr;
   5  }
   6  
   7  export function HasMLS() {
   8    return typeof window !== 'undefined' && !!window.nostr && !!window.nostr.mls;
   9  }
  10  
  11  export function GetPublicKey(fn) {
  12    if (!window.nostr) { fn(''); return; }
  13    window.nostr.getPublicKey()
  14      .then(pubkey => fn(pubkey))
  15      .catch(() => fn(''));
  16  }
  17  
  18  export function SignEvent(eventJSON, fn) {
  19    if (!window.nostr) { console.error('signer: window.nostr not available'); fn(''); return; }
  20    try {
  21      const ev = JSON.parse(eventJSON);
  22      window.nostr.signEvent(ev)
  23        .then(signed => fn(JSON.stringify(signed)))
  24        .catch(e => { console.error('signer: signEvent rejected:', e); fn(''); });
  25    } catch(e) { console.error('signer: signEvent exception:', e); fn(''); }
  26  }
  27  
  28  export function Nip04Decrypt(peerPubkey, ciphertext, fn) {
  29    if (!window.nostr || !window.nostr.nip04) { fn(''); return; }
  30    window.nostr.nip04.decrypt(peerPubkey, ciphertext)
  31      .then(plain => fn(plain))
  32      .catch(() => fn(''));
  33  }
  34  
  35  export function Nip04Encrypt(peerPubkey, plaintext, fn) {
  36    if (!window.nostr || !window.nostr.nip04) { fn(''); return; }
  37    window.nostr.nip04.encrypt(peerPubkey, plaintext)
  38      .then(ct => fn(ct))
  39      .catch(() => fn(''));
  40  }
  41  
  42  export function Nip44Decrypt(peerPubkey, ciphertext, fn) {
  43    if (!window.nostr || !window.nostr.nip44) { fn(''); return; }
  44    window.nostr.nip44.decrypt(peerPubkey, ciphertext)
  45      .then(plain => fn(plain))
  46      .catch(() => fn(''));
  47  }
  48  
  49  export function Nip44Encrypt(peerPubkey, plaintext, fn) {
  50    if (!window.nostr || !window.nostr.nip44) { fn(''); return; }
  51    window.nostr.nip44.encrypt(peerPubkey, plaintext)
  52      .then(ct => fn(ct))
  53      .catch(() => fn(''));
  54  }
  55