Skip to content

Commit

Permalink
criando evento de mensagem criptografada
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioconselheiro committed Nov 12, 2023
1 parent fb67e0c commit 73265fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
23 changes: 20 additions & 3 deletions src/app/shared/nostr-api/nostr-event.factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { NostrEventKind } from '@domain/nostr-event-kind.enum';
import { NostrUser } from '@domain/nostr-user';
import { UnsignedEvent, getEventHash, getSignature, nip04 } from 'nostr-tools';
import { Event, UnsignedEvent, getEventHash, getSignature, nip04 } from 'nostr-tools';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -31,16 +31,33 @@ export class NostrEventFactory {
/**
* NIP 4
* https://github.com/nostr-protocol/nips/blob/master/04.md
* https://github.com/nbd-wtf/nostr-tools/blob/master/nip04.test.ts
*/
createEncryptedDirectMessage() {
async createEncryptedDirectMessage(you: NostrUser, stranger: NostrUser, message: string): Promise<Event<NostrEventKind.EncryptedDirectMessage>> {
const encriptedMessage = await nip04.encrypt(you.nostrSecret, stranger.nostrPublic, message);

const unsignedEvent: UnsignedEvent = {
kind: NostrEventKind.EncryptedDirectMessage,
content: encriptedMessage, // + '?iv=' + ivBase64,
pubkey: you.publicKeyHex,
// eslint-disable-next-line @typescript-eslint/naming-convention
created_at: this.getCurrentTimestamp(),
tags: [
['p', stranger.publicKeyHex]
]
};

const id = getEventHash(unsignedEvent);
const sig = getSignature(unsignedEvent, you.privateKeyHex);

return Promise.resolve({ id, sig, ...unsignedEvent });
}

/**
* NIP 38
* https://github.com/nostr-protocol/nips/blob/master/38.md
*/
createUserStatuses(user: NostrUser) {
createUserStatuses(user: NostrUser): Event<NostrEventKind.UserStatuses> {
const unsignedEvent: UnsignedEvent = {
kind: NostrEventKind.UserStatuses,
content: "#wannachat",
Expand Down
3 changes: 1 addition & 2 deletions src/app/shared/nostr-api/nostr.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable } from '@angular/core';
import { Event, Filter, SimplePool } from 'nostr-tools';
import { defaultRelays } from '../../default-relays.const';
import { Filter, Event, SimplePool, UnsignedEvent } from 'nostr-tools';
import { NostrUser } from 'src/app/domain/nostr-user';

@Injectable({
providedIn: 'root'
Expand Down

0 comments on commit 73265fb

Please sign in to comment.