nostr-double-ratchet 0.0.13 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { hexToBytes, bytesToHex } from "@noble/hashes/utils";
2
- import { SessionState, Message } from "./types";
2
+ import { Rumor, SessionState } from "./types";
3
3
  import { Session } from "./Session.ts";
4
4
  import { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf';
5
5
  import { sha256 } from '@noble/hashes/sha256';
@@ -7,7 +7,8 @@ import { sha256 } from '@noble/hashes/sha256';
7
7
  export function serializeSessionState(state: SessionState): string {
8
8
  return JSON.stringify({
9
9
  rootKey: bytesToHex(state.rootKey),
10
- theirNostrPublicKey: state.theirNostrPublicKey,
10
+ theirCurrentNostrPublicKey: state.theirCurrentNostrPublicKey,
11
+ theirNextNostrPublicKey: state.theirNextNostrPublicKey,
11
12
  ourCurrentNostrKey: state.ourCurrentNostrKey ? {
12
13
  publicKey: state.ourCurrentNostrKey.publicKey,
13
14
  privateKey: bytesToHex(state.ourCurrentNostrKey.privateKey),
@@ -40,7 +41,8 @@ export function deserializeSessionState(data: string): SessionState {
40
41
  const state = JSON.parse(data);
41
42
  return {
42
43
  rootKey: hexToBytes(state.rootKey),
43
- theirNostrPublicKey: state.theirNostrPublicKey,
44
+ theirCurrentNostrPublicKey: state.theirCurrentNostrPublicKey,
45
+ theirNextNostrPublicKey: state.theirNextNostrPublicKey,
44
46
  ourCurrentNostrKey: state.ourCurrentNostrKey ? {
45
47
  publicKey: state.ourCurrentNostrKey.publicKey,
46
48
  privateKey: hexToBytes(state.ourCurrentNostrKey.privateKey),
@@ -69,16 +71,16 @@ export function deserializeSessionState(data: string): SessionState {
69
71
  };
70
72
  }
71
73
 
72
- export async function* createMessageStream(session: Session): AsyncGenerator<Message, void, unknown> {
73
- const messageQueue: Message[] = [];
74
- let resolveNext: ((value: Message) => void) | null = null;
74
+ export async function* createEventStream(session: Session): AsyncGenerator<Rumor, void, unknown> {
75
+ const messageQueue: Rumor[] = [];
76
+ let resolveNext: ((value: Rumor) => void) | null = null;
75
77
 
76
- const unsubscribe = session.onMessage((message) => {
78
+ const unsubscribe = session.onEvent((event) => {
77
79
  if (resolveNext) {
78
- resolveNext(message);
80
+ resolveNext(event);
79
81
  resolveNext = null;
80
82
  } else {
81
- messageQueue.push(message);
83
+ messageQueue.push(event);
82
84
  }
83
85
  });
84
86
 
@@ -87,7 +89,7 @@ export async function* createMessageStream(session: Session): AsyncGenerator<Mes
87
89
  if (messageQueue.length > 0) {
88
90
  yield messageQueue.shift()!;
89
91
  } else {
90
- yield new Promise<Message>(resolve => {
92
+ yield new Promise<Rumor>(resolve => {
91
93
  resolveNext = resolve;
92
94
  });
93
95
  }