working rss feed to nostr publish
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
// nip26.ts
|
||||
import { schnorr as schnorr2 } from "@noble/curves/secp256k1";
|
||||
import { bytesToHex as bytesToHex2 } from "@noble/hashes/utils";
|
||||
import { sha256 } from "@noble/hashes/sha256";
|
||||
|
||||
// utils.ts
|
||||
var utf8Decoder = new TextDecoder("utf-8");
|
||||
var utf8Encoder = new TextEncoder();
|
||||
|
||||
// keys.ts
|
||||
import { schnorr } from "@noble/curves/secp256k1";
|
||||
import { bytesToHex } from "@noble/hashes/utils";
|
||||
function getPublicKey(privateKey) {
|
||||
return bytesToHex(schnorr.getPublicKey(privateKey));
|
||||
}
|
||||
|
||||
// nip26.ts
|
||||
function createDelegation(privateKey, parameters) {
|
||||
let conditions = [];
|
||||
if ((parameters.kind || -1) >= 0)
|
||||
conditions.push(`kind=${parameters.kind}`);
|
||||
if (parameters.until)
|
||||
conditions.push(`created_at<${parameters.until}`);
|
||||
if (parameters.since)
|
||||
conditions.push(`created_at>${parameters.since}`);
|
||||
let cond = conditions.join("&");
|
||||
if (cond === "")
|
||||
throw new Error("refusing to create a delegation without any conditions");
|
||||
let sighash = sha256(utf8Encoder.encode(`nostr:delegation:${parameters.pubkey}:${cond}`));
|
||||
let sig = bytesToHex2(schnorr2.sign(sighash, privateKey));
|
||||
return {
|
||||
from: getPublicKey(privateKey),
|
||||
to: parameters.pubkey,
|
||||
cond,
|
||||
sig
|
||||
};
|
||||
}
|
||||
function getDelegator(event) {
|
||||
let tag = event.tags.find((tag2) => tag2[0] === "delegation" && tag2.length >= 4);
|
||||
if (!tag)
|
||||
return null;
|
||||
let pubkey = tag[1];
|
||||
let cond = tag[2];
|
||||
let sig = tag[3];
|
||||
let conditions = cond.split("&");
|
||||
for (let i = 0; i < conditions.length; i++) {
|
||||
let [key, operator, value] = conditions[i].split(/\b/);
|
||||
if (key === "kind" && operator === "=" && event.kind === parseInt(value))
|
||||
continue;
|
||||
else if (key === "created_at" && operator === "<" && event.created_at < parseInt(value))
|
||||
continue;
|
||||
else if (key === "created_at" && operator === ">" && event.created_at > parseInt(value))
|
||||
continue;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
let sighash = sha256(utf8Encoder.encode(`nostr:delegation:${event.pubkey}:${cond}`));
|
||||
if (!schnorr2.verify(sig, sighash, pubkey))
|
||||
return null;
|
||||
return pubkey;
|
||||
}
|
||||
export {
|
||||
createDelegation,
|
||||
getDelegator
|
||||
};
|
||||
Reference in New Issue
Block a user