DEV Community

Tetsuya Kaneko
Tetsuya Kaneko

Posted on

Nostr NIP-05 verification with Cloudflare Workers

If you have a domain managed by Cloudflare, you can use Cloudflare Workers to get Nostr NIP-05 verification.

1. Create a Service

export default {
  async fetch(request) {

    const data = {
      "names": {
        "<username>": "<hex key>"
      }
    };

    const json = JSON.stringify(data, null, 2);

    return new Response(json, {
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json;charset=UTF-8',
      },
    });
  },
};
Enter fullscreen mode Exit fullscreen mode

If you're a Damus user, you can use damus key converter to convert a damus key to a hex key. You also need to add the CORS header: Access-Control-Allow-Origin: * so it can be validated from the app.

2. Create Workers Routes

Once the service has created, you need to add Worker Route.

[route]
pattern = "tetsuya.dev/.well-known/nostr.json*"
zone_name = "tetsuya.dev"
Enter fullscreen mode Exit fullscreen mode

Note that because route patterns cannot contain query parameters, you must use a wildcard * to have a route pattern match URLs with query parameters.

Result

$ curl --include 'https://tetsuya.dev/.well-known/nostr.json?user=tetsuya'
...
access-control-allow-origin: *
...

{
  "names": {
    "tetsuya": "28d1243fb879d353c15c4cfc7537bf88f6fe18db2160c553beee408147eb0be5"
  }
}⏎
Enter fullscreen mode Exit fullscreen mode

NIP-05 verified

Follow me on Nostr: npub19rgjg0ac08f48s2ufn782dal3rm0uxxmy9sv25a7aeqgz3ltp0js9txe3x

Top comments (2)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
bithacker profile image
bit

Hello, I'm having trouble following these instructions. Could you please help? I've made a worker node over here workers-playground-delicate-bonus-... but I cannot access it over the domain that's hosted on CF. curl --include 'hacker.pk/.well-known/nostr.json?u...' returns could not resolve host. The route is already added uner 'Routes' in addition to the default worker. Thanks.