parent
e99ad4f152
commit
8850feddf4
client/src/services
@ -98,9 +98,6 @@ export class Nostr21121Service {
|
||||
tags.push(['e', requestEvent.id, '']);
|
||||
}
|
||||
|
||||
// Add kind reference
|
||||
tags.push(['k', '21120']);
|
||||
|
||||
// Get the pubkey of the request creator (client) for encryption
|
||||
const clientPubkey = requestEvent.pubkey;
|
||||
if (!clientPubkey) {
|
||||
@ -129,9 +126,6 @@ export class Nostr21121Service {
|
||||
const encryptedKey = await encryptWithNostrTools(encryptionKey, privateKeyNsec, clientPubkey);
|
||||
console.log("Successfully encrypted the symmetric key with NIP-44");
|
||||
|
||||
// Add p tag to reference the recipient
|
||||
tags.push(['p', clientPubkey, '']);
|
||||
|
||||
// Add encrypted key as a tag
|
||||
tags.push(['encrypted_key', encryptedKey]);
|
||||
} catch (encryptError) {
|
||||
|
@ -390,71 +390,22 @@ export class NostrEventService {
|
||||
const httpResponse = await httpClient.sendHttpRequest(decryptedContent);
|
||||
console.log("HTTP request executed successfully, creating 21121 response...");
|
||||
|
||||
// Import crypto utilities for encryption
|
||||
const cryptoUtils = await import('../utils/crypto-utils');
|
||||
const nostrTools = await import('nostr-tools');
|
||||
// Import and use our dedicated Nostr21121Service for creating and publishing the response
|
||||
const { Nostr21121Service } = await import('./Nostr21121Service');
|
||||
const nostr21121Service = new Nostr21121Service(this.relayService, this.cacheService);
|
||||
|
||||
// Generate a random key for content encryption
|
||||
const randomKey = Array.from(crypto.getRandomValues(new Uint8Array(32)))
|
||||
.map(b => b.toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
// Create and publish the 21121 response event using our service
|
||||
const responseEvent = await nostr21121Service.createAndPublish21121Event(
|
||||
requestEvent,
|
||||
httpResponse,
|
||||
serverNsec,
|
||||
relayUrl
|
||||
);
|
||||
|
||||
// Encrypt the response content with the random key
|
||||
const encryptedContent = await cryptoUtils.encryptWithWebCrypto(httpResponse, randomKey);
|
||||
|
||||
// Get the server pubkey from the nsec
|
||||
const decoded = nostrTools.nip19.decode(serverNsec);
|
||||
if (decoded.type !== 'nsec') {
|
||||
throw new Error("Invalid server nsec format");
|
||||
if (!responseEvent) {
|
||||
throw new Error("Failed to create 21121 response event");
|
||||
}
|
||||
|
||||
const serverPrivateKeyBytes = decoded.data as Uint8Array;
|
||||
const serverPubkey = nostrTools.getPublicKey(serverPrivateKeyBytes);
|
||||
|
||||
// Create tags for the 21121 event
|
||||
const tags: string[][] = [
|
||||
["e", requestEvent.id as string],
|
||||
["key", randomKey], // Store the key directly for now, we'll handle encryption differently
|
||||
["expiration", (Math.floor(Date.now() / 1000) + 3600).toString()] // 1 hour expiration
|
||||
];
|
||||
|
||||
// Create the 21121 event
|
||||
const eventBody = {
|
||||
kind: EventKind.HttpResponse,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
tags: tags,
|
||||
content: encryptedContent,
|
||||
pubkey: serverPubkey
|
||||
};
|
||||
|
||||
// Compute the event ID (hash)
|
||||
const id = nostrTools.getEventHash(eventBody as any);
|
||||
|
||||
// Sign the event
|
||||
let sig: string;
|
||||
|
||||
// Try to use window.nostr if available, otherwise use a simulated signature
|
||||
if (window.nostr && window.nostr.signEvent) {
|
||||
try {
|
||||
const signResult = await window.nostr.signEvent(eventBody);
|
||||
sig = typeof signResult === 'object' && signResult.sig
|
||||
? signResult.sig
|
||||
: 'simulated_signature_for_21121_response';
|
||||
} catch (signError) {
|
||||
console.error("Error signing with window.nostr:", signError);
|
||||
sig = 'simulated_signature_for_21121_response';
|
||||
}
|
||||
} else {
|
||||
sig = 'simulated_signature_for_21121_response';
|
||||
}
|
||||
|
||||
// Create the complete signed event
|
||||
const responseEvent: NostrEvent = {
|
||||
...eventBody,
|
||||
id,
|
||||
sig
|
||||
};
|
||||
|
||||
// Ensure ID is defined for logging
|
||||
const shortId = responseEvent.id ? responseEvent.id.substring(0, 8) + '...' : 'undefined';
|
||||
|
||||
@ -469,28 +420,17 @@ export class NostrEventService {
|
||||
|
||||
// Create a DOM element to display the raw JSON in the UI
|
||||
this.displayRawJsonInUI(responseEvent);
|
||||
// Add the event to our EventManager for tracking, storing the original HTTP response as decryptedContent
|
||||
this.eventManager.addEvent(
|
||||
responseEvent, // event
|
||||
true, // decrypted
|
||||
httpResponse, // decryptedContent - store the original HTTP response here
|
||||
true // validateRelationships
|
||||
);
|
||||
|
||||
// Publish the event to the relay if we have valid IDs
|
||||
// Store relationship between request and response
|
||||
if (responseEvent.id && requestEvent.id) {
|
||||
const relayPool = this.relayService.getRelayPool();
|
||||
if (relayPool) {
|
||||
try {
|
||||
// Use relayPool.send instead of publish for better type compatibility
|
||||
const pub = relayPool.publish([relayUrl], responseEvent as any);
|
||||
await pub;
|
||||
console.log(`Published 21121 response event to ${relayUrl}`);
|
||||
|
||||
// Add the event to our EventManager for tracking
|
||||
this.processEvent(responseEvent);
|
||||
|
||||
// Store relationship between request and response
|
||||
this.eventManager.associateResponseWithRequest(responseEvent.id, requestEvent.id);
|
||||
} catch (pubError) {
|
||||
console.error("Error publishing 21121 response:", pubError);
|
||||
}
|
||||
} else {
|
||||
console.error("Cannot publish 21121 response: Relay pool not available");
|
||||
}
|
||||
this.eventManager.associateResponseWithRequest(responseEvent.id, requestEvent.id);
|
||||
}
|
||||
|
||||
// Show success notification
|
||||
|
Loading…
x
Reference in New Issue
Block a user