1
0
forked from Kieran/snort

nip-59 gift wrap construct

This commit is contained in:
Kieran 2023-06-15 15:51:07 +01:00
parent 0fdcd36877
commit a9f3216102
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 27 additions and 3 deletions

View File

@ -11,6 +11,7 @@ enum EventKind {
BadgeAward = 8, // NIP-58
SnortSubscriptions = 1000, // NIP-XX
Polls = 6969, // NIP-69
GiftWrap = 1059, // NIP-59
FileHeader = 1063, // NIP-94
Relays = 10002, // NIP-65
Ephemeral = 20_000,

View File

@ -1,12 +1,13 @@
import * as secp from "@noble/curves/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import { unwrap, barrierQueue, processWorkQueue, WorkQueueItem } from "@snort/shared";
import { unwrap, barrierQueue, processWorkQueue, WorkQueueItem, getPublicKey } from "@snort/shared";
import {
EventKind,
FullRelaySettings,
HexKey,
Lists,
Nip44Encryptor,
NostrEvent,
RelaySettings,
TaggedRawEvent,
@ -16,6 +17,7 @@ import {
import { EventBuilder } from "./EventBuilder";
import { EventExt } from "./EventExt";
import { findTag } from "./Utils";
const Nip7Queue: Array<WorkQueueItem> = [];
processWorkQueue(Nip7Queue);
@ -309,4 +311,25 @@ export class EventPublisher {
fnHook(eb);
return await this.#sign(eb);
}
/**
* NIP-59 Gift Wrap event with ephemeral key
*/
async giftWrap(inner: NostrEvent) {
const secret = utils.bytesToHex(secp.secp256k1.utils.randomPrivateKey());
const pTag = findTag(inner, "p");
if (!pTag) throw new Error("Inner event must have a p tag");
const eb = new EventBuilder();
eb.pubKey(getPublicKey(secret));
eb.kind(EventKind.GiftWrap);
eb.tag(["p", pTag]);
const enc = new Nip44Encryptor();
const shared = enc.getSharedSecret(secret, pTag);
eb.content(enc.encryptData(JSON.stringify(inner), shared));
return await eb.buildAndSign(secret);
}
}