Make nip46 backend ignore all methods except create_account

This commit is contained in:
artur 2024-02-20 09:38:59 +03:00
parent d5bf5a0faf
commit 6b7b48cc12
2 changed files with 60 additions and 5 deletions

View File

@ -10,7 +10,7 @@ const {
const { createHash } = require("node:crypto");
const express = require("express");
const bodyParser = require("body-parser");
const { nip19, getPublicKey } = require("nostr-tools");
const { nip19, getPublicKey, verifySignature } = require("nostr-tools");
const {
makePwh2,
countLeadingZeros,
@ -1167,9 +1167,52 @@ class CreateAccountHandlingStrategy {
}
}
// copycat of ndk's implementations with one exception:
// it ignores the unknown methods (except create_account)
class Nip46Backend extends NDKNip46Backend {
constructor(ndk, signer) {
super(ndk, signer, () => Promise.resolve(true))
signer.user().then((u) => (this.npub = nip19.npubEncode(u.pubkey)))
}
async handleIncomingEvent(event) {
const { id, method, params } = await this.rpc.parseEvent(event)
const remotePubkey = event.pubkey
let response
// validate signature explicitly
if (!verifySignature(event.rawEvent())) {
this.debug('invalid signature', event.rawEvent())
return
}
const strategy = this.handlers[method]
if (strategy) {
try {
response = await strategy.handle(this, id, remotePubkey, params)
console.log(Date.now(), 'req', id, 'method', method, 'result', response)
} catch (e) {
this.debug('error handling event', e, { id, method, params })
this.rpc.sendResponse(id, remotePubkey, 'error', undefined, e.message)
}
} else {
this.debug('unsupported method', { method, params })
// ignore unsupported methods
return;
}
if (response) {
this.debug(`sending response to ${remotePubkey}`, response)
this.rpc.sendResponse(id, remotePubkey, response)
} else {
this.rpc.sendResponse(id, remotePubkey, 'error', undefined, 'Not authorized')
}
}
}
async function startBunker() {
const { data: bunkerSk } = nip19.decode(BUNKER_NSEC);
bunkerSigner = new NDKNip46Backend(
bunkerSigner = new Nip46Backend(
ndk,
new NDKPrivateKeySigner(bunkerSk),
() => true

View File

@ -193,16 +193,28 @@ if (process.argv.length >= 3) {
console.log("transferred")
await sendPostAuthd({
sk: sk1,
method: "DELETE",
method: "POST",
url: LOCAL
? "http://localhost:8000/name"
: "https://noauthd.nsec.app/name",
body: JSON.stringify({
npub: npub1,
name: name,
name,
}),
pow: 16
});
console.log("deleted");
// await sendPostAuthd({
// sk: sk1,
// method: "DELETE",
// url: LOCAL
// ? "http://localhost:8000/name"
// : "https://noauthd.nsec.app/name",
// body: JSON.stringify({
// npub: npub1,
// name: name,
// }),
// });
// console.log("deleted");
}
test()
}