Last active
October 25, 2024 14:50
-
-
Save Anderson-Juhasc/7c83bcfc999bb93d002029304e0934ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Need for work on server | |
import { useWebSocketImplementation } from 'nostr-tools/pool' | |
import WebSocket from 'ws' | |
useWebSocketImplementation(WebSocket) | |
import { generateSecretKey, getPublicKey, finalizeEvent, getEventHash } from 'nostr-tools/pure' | |
import { SimplePool } from 'nostr-tools/pool' | |
import { v2 as nip44 } from 'nostr-tools/nip44' | |
const relayArray = [ | |
'wss://nos.lol', | |
] | |
function randomTimeUpTo2DaysInThePast() { | |
const now = Math.floor(Date.now() / 1000); // Current timestamp in seconds | |
const twoDaysInSeconds = 2 * 24 * 60 * 60; // 2 days in seconds | |
// Generate a random time within the past 2 days | |
const randomPastTime = now - Math.floor(Math.random() * twoDaysInSeconds); | |
return randomPastTime; | |
} | |
export function wrapGift(PrvKey, receiverPubKey, message = "Hola, que tal?", relayUrl = 'wss://relay.damus.io') { | |
const senderAccount = { | |
prvKey: PrvKey, | |
pubKey: getPublicKey(PrvKey), | |
} | |
const conversationKey = nip44.utils.getConversationKey(senderAccount.prvKey, receiverPubKey) | |
const unsignedKind14 = { | |
pubkey: senderAccount.pubKey, // sender's public key | |
created_at: Math.floor(Date.now() / 1000), // current time | |
kind: 14, // NIP-17 kind for private messages | |
tags: [ | |
["p", receiverPubKey, relayUrl], // receiver's pubkey and relay | |
// Other tags like subject or replies | |
], | |
content: message, // message in plain text | |
} | |
unsignedKind14.id = getEventHash(unsignedKind14) | |
let kind13EncryptedMessage = { | |
pubkey: senderAccount.pubKey, // sender's public key | |
created_at: randomTimeUpTo2DaysInThePast(), // randomize up to 2 days in the past if necessary | |
kind: 13, // NIP-59 seal | |
tags: [], // no tags for kind:13 | |
content: nip44.encrypt( | |
JSON.stringify(unsignedKind14), | |
conversationKey, | |
), | |
} | |
kind13EncryptedMessage = finalizeEvent(kind13EncryptedMessage, senderAccount.prvKey) | |
const randomSecretKey = generateSecretKey() | |
const conversationRandomKey = nip44.utils.getConversationKey(randomSecretKey, receiverPubKey) | |
let giftWrapped = { | |
pubkey: getPublicKey(randomSecretKey), // random public key for the gift wrap | |
created_at: randomTimeUpTo2DaysInThePast(), // randomize creation time | |
kind: 1059, // NIP-59 gift wrap | |
tags: [ | |
["p", receiverPubKey, relayUrl] // receiver's pubkey | |
], | |
content: nip44.encrypt( | |
JSON.stringify(kind13EncryptedMessage), | |
conversationRandomKey, | |
), | |
} | |
giftWrapped = finalizeEvent(giftWrapped, randomSecretKey) | |
return giftWrapped | |
} | |
export function unwrapGift(prvKey, gift) { | |
const receiverAccount = { | |
prvKey: prvKey, | |
pubKey: getPublicKey(prvKey), | |
} | |
let receiverConversationKey = nip44.utils.getConversationKey(receiverAccount.prvKey, gift.pubkey) | |
const decryptedKind13Message = nip44.decrypt(gift.content, receiverConversationKey) | |
const parsedKind13Message = JSON.parse(decryptedKind13Message) | |
const encryptedKind14Message = parsedKind13Message.content | |
receiverConversationKey = nip44.utils.getConversationKey(receiverAccount.prvKey, parsedKind13Message.pubkey) | |
const decryptedKind14Message = nip44.decrypt(encryptedKind14Message, receiverConversationKey) | |
const originalMessage = JSON.parse(decryptedKind14Message) | |
return originalMessage | |
} | |
export async function fetchGifts(pubKeys = []) { | |
const pool = new SimplePool() | |
try { | |
const events = await pool.querySync( | |
relayArray, | |
{ kinds: [1059], '#p': pubKeys } | |
) | |
pool.close(relayArray) | |
return events | |
} catch (error) { | |
console.error('Failed to retrieve gifts:', error) | |
} | |
} |
Thanks @Anderson-Juhasc . any links utilizing nostr-dev-kit/ndk ?
@turizspace I found this PR: nostr-dev-kit/ndk#233
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
going to try and implement this on https://rosecoco.site