Skip to content

nosdav/nosdav-shim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nosdav-shim

nosdav-shim

Documentation


GitHub license npm npm Github Stars

Introduction

This shim intercepts all requests made by a web page using the "fetch" API. If the request method is "PUT", it creates a Nostr event object, signs it, and adds the signed object to the "authorization" header of the request before sending it out. This allows the web page to authenticate the request with Nostr. Otherwise, the shim simply passes the request through to the original "fetch" method.

Usage

<script src="https://unpkg.com/nosdav-shim"></script>

Code

const originalFetch = window.fetch;

window.fetch = async function(url, options) {
  const newOptions = { ...options };


  if (newOptions.method === 'PUT') {
    const event = {
      kind: 27235,
      created_at: Math.floor(Date.now() / 1000),
      tags: [['u', url]],
      content: ''
    }
    const signedEvent = await window.nostr.signEvent(event)
    var auth = `Nostr ${btoa(JSON.stringify(signedEvent))}`
  

    newOptions.headers = {
      ...newOptions.headers,
      'authorization': auth
    };
  }

  return originalFetch.call(this, url, newOptions);
};

License

  • MIT

Releases

No releases published

Packages

No packages published