parent
12f1258f3e
commit
e99ad4f152
@ -38,43 +38,57 @@ export class Nostr21121Service {
|
||||
throw new Error('Invalid request event');
|
||||
}
|
||||
|
||||
// Extract private key
|
||||
let privateKeyStr = typeof serverPrivateKey === 'string' ? serverPrivateKey : '';
|
||||
if (typeof serverPrivateKey === 'string' && serverPrivateKey.startsWith('nsec')) {
|
||||
console.log('Using nsec key for signing');
|
||||
} else {
|
||||
console.log('Using hex or bytes key for signing');
|
||||
}
|
||||
|
||||
// Convert the private key to bytes if needed
|
||||
// Extract private key and convert to necessary formats
|
||||
let privateKeyNsec = '';
|
||||
let privateKeyHex = '';
|
||||
let privateKeyBytes: Uint8Array;
|
||||
|
||||
// Handle different private key formats
|
||||
if (typeof serverPrivateKey === 'string') {
|
||||
if (serverPrivateKey.startsWith('nsec')) {
|
||||
privateKeyNsec = serverPrivateKey;
|
||||
try {
|
||||
// Decode nsec
|
||||
const decoded = nostrTools.nip19.decode(serverPrivateKey);
|
||||
privateKeyBytes = decoded.data as Uint8Array;
|
||||
|
||||
// Convert bytes to hex if needed for getPublicKey
|
||||
privateKeyHex = Array.from(privateKeyBytes)
|
||||
.map(b => b.toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
|
||||
console.log('Successfully decoded nsec key');
|
||||
} catch (e) {
|
||||
console.error('Error decoding nsec:', e);
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
// Convert hex string to bytes
|
||||
// Already a hex string
|
||||
privateKeyHex = serverPrivateKey;
|
||||
// Convert hex to bytes
|
||||
privateKeyBytes = new Uint8Array(
|
||||
serverPrivateKey.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16)) || []
|
||||
privateKeyHex.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16)) || []
|
||||
);
|
||||
|
||||
// We need an nsec for the encryptWithNostrTools function
|
||||
// This is just for demo, in production you'd handle this differently
|
||||
privateKeyNsec = nostrTools.nip19.nsecEncode(privateKeyBytes);
|
||||
}
|
||||
} else {
|
||||
// Already bytes
|
||||
privateKeyBytes = serverPrivateKey;
|
||||
// Convert bytes to hex for getPublicKey
|
||||
privateKeyHex = Array.from(privateKeyBytes)
|
||||
.map(b => b.toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
|
||||
// Create nsec for encryption
|
||||
privateKeyNsec = nostrTools.nip19.nsecEncode(privateKeyBytes);
|
||||
}
|
||||
|
||||
// Get the public key from the private key
|
||||
const pubKey = nostrTools.getPublicKey(privateKeyBytes);
|
||||
console.log(`Using pubkey: ${pubKey.substring(0, 8)}...`);
|
||||
console.log(`Using pubkey: ${pubKey.substring(0, 8)}...`);
|
||||
|
||||
// Initialize tags array
|
||||
let tags: string[][] = [];
|
||||
@ -108,13 +122,11 @@ export class Nostr21121Service {
|
||||
}
|
||||
|
||||
// Encrypt the encryption key with NIP-44 using nostr-tools
|
||||
let encryptedKey: string = "";
|
||||
if (clientPubkey) {
|
||||
try {
|
||||
// Encrypt the encryption key using NIP-44 from nostr-tools
|
||||
console.log(`Encrypting key for client pubkey: ${clientPubkey.substring(0, 8)}...`);
|
||||
// We use privateKeyBytes here since we have it in the correct format
|
||||
encryptedKey = await encryptWithNostrTools(encryptionKey, privateKeyBytes, clientPubkey);
|
||||
const encryptedKey = await encryptWithNostrTools(encryptionKey, privateKeyNsec, clientPubkey);
|
||||
console.log("Successfully encrypted the symmetric key with NIP-44");
|
||||
|
||||
// Add p tag to reference the recipient
|
||||
@ -141,8 +153,6 @@ export class Nostr21121Service {
|
||||
// Sign the event properly using nostr-tools
|
||||
const event = nostrTools.finalizeEvent(unsignedEvent, privateKeyBytes);
|
||||
console.log(`Event created and signed successfully. ID: ${event.id.substring(0, 8)}...`);
|
||||
// The event is already properly signed
|
||||
};
|
||||
|
||||
// Publish to relay
|
||||
console.log(`Publishing event to relay: ${relayUrl}`);
|
||||
|
Loading…
x
Reference in New Issue
Block a user