nostr-double-ratchet 0.0.24 → 0.0.26

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/Session.ts CHANGED
@@ -46,29 +46,54 @@ export class Session {
46
46
  * @param name Optional name for the session (for debugging)
47
47
  * @returns A new Session instance
48
48
  */
49
- static init(nostrSubscribe: NostrSubscribe, theirNextNostrPublicKey: string, ourCurrentPrivateKey: Uint8Array, isInitiator: boolean, sharedSecret: Uint8Array, name?: string): Session {
49
+ static init(
50
+ nostrSubscribe: NostrSubscribe,
51
+ theirEphemeralNostrPublicKey: string,
52
+ ourEphemeralNostrPrivateKey: Uint8Array,
53
+ isInitiator: boolean,
54
+ sharedSecret: Uint8Array,
55
+ name?: string
56
+ ): Session {
50
57
  const ourNextPrivateKey = generateSecretKey();
51
- const [rootKey, sendingChainKey] = kdf(sharedSecret, nip44.getConversationKey(ourNextPrivateKey, theirNextNostrPublicKey), 2);
52
- let ourCurrentNostrKey;
53
- let ourNextNostrKey;
58
+
59
+ let rootKey: Uint8Array;
60
+ let sendingChainKey: Uint8Array | undefined;
61
+ let ourCurrentNostrKey: { publicKey: string, privateKey: Uint8Array } | undefined;
62
+ let ourNextNostrKey: { publicKey: string, privateKey: Uint8Array };
63
+
54
64
  if (isInitiator) {
55
- ourCurrentNostrKey = { publicKey: getPublicKey(ourCurrentPrivateKey), privateKey: ourCurrentPrivateKey };
56
- ourNextNostrKey = { publicKey: getPublicKey(ourNextPrivateKey), privateKey: ourNextPrivateKey };
65
+ [rootKey, sendingChainKey] = kdf(sharedSecret, nip44.getConversationKey(ourNextPrivateKey, theirEphemeralNostrPublicKey), 2);
66
+ ourCurrentNostrKey = {
67
+ publicKey: getPublicKey(ourEphemeralNostrPrivateKey),
68
+ privateKey: ourEphemeralNostrPrivateKey
69
+ };
70
+ ourNextNostrKey = {
71
+ publicKey: getPublicKey(ourNextPrivateKey),
72
+ privateKey: ourNextPrivateKey
73
+ };
57
74
  } else {
58
- ourNextNostrKey = { publicKey: getPublicKey(ourCurrentPrivateKey), privateKey: ourCurrentPrivateKey };
75
+ rootKey = sharedSecret;
76
+ sendingChainKey = undefined;
77
+ ourCurrentNostrKey = undefined;
78
+ ourNextNostrKey = {
79
+ publicKey: getPublicKey(ourEphemeralNostrPrivateKey),
80
+ privateKey: ourEphemeralNostrPrivateKey
81
+ };
59
82
  }
83
+
60
84
  const state: SessionState = {
61
- rootKey: isInitiator ? rootKey : sharedSecret,
62
- theirNextNostrPublicKey,
85
+ rootKey,
86
+ theirNextNostrPublicKey: theirEphemeralNostrPublicKey,
63
87
  ourCurrentNostrKey,
64
88
  ourNextNostrKey,
65
89
  receivingChainKey: undefined,
66
- sendingChainKey: isInitiator ? sendingChainKey : undefined,
90
+ sendingChainKey,
67
91
  sendingChainMessageNumber: 0,
68
92
  receivingChainMessageNumber: 0,
69
93
  previousSendingChainMessageCount: 0,
70
94
  skippedKeys: {},
71
95
  };
96
+
72
97
  const session = new Session(nostrSubscribe, state);
73
98
  if (name) session.name = name;
74
99
  return session;
@@ -219,7 +244,9 @@ export class Session {
219
244
 
220
245
  // 3. MESSAGE KEY FUNCTIONS
221
246
  private skipMessageKeys(until: number, nostrSender: string) {
222
- if (this.state.receivingChainMessageNumber + MAX_SKIP < until) {
247
+ if (until <= this.state.receivingChainMessageNumber) return
248
+
249
+ if (until > this.state.receivingChainMessageNumber + MAX_SKIP) {
223
250
  throw new Error("Too many skipped messages");
224
251
  }
225
252
 
package/src/types.ts CHANGED
@@ -66,6 +66,9 @@ export type Unsubscribe = () => void;
66
66
  * Function that subscribes to Nostr events matching a filter and calls onEvent for each event.
67
67
  */
68
68
  export type NostrSubscribe = (filter: Filter, onEvent: (e: VerifiedEvent) => void) => Unsubscribe;
69
+ export type EncryptFunction = (plaintext: string, pubkey: string) => Promise<string>;
70
+ export type DecryptFunction = (ciphertext: string, pubkey: string) => Promise<string>;
71
+ export type NostrPublish = (event: UnsignedEvent) => Promise<VerifiedEvent>;
69
72
 
70
73
  export type Rumor = UnsignedEvent & { id: string }
71
74
 
@@ -78,7 +81,7 @@ export type EventCallback = (event: Rumor, outerEvent: VerifiedEvent) => void;
78
81
  /**
79
82
  * Message event kind
80
83
  */
81
- export const MESSAGE_EVENT_KIND = 30078;
84
+ export const MESSAGE_EVENT_KIND = 1060;
82
85
 
83
86
  /**
84
87
  * Invite event kind
@@ -100,7 +103,3 @@ export type NostrEvent = {
100
103
  content: string;
101
104
  sig: string;
102
105
  }
103
-
104
- export type EncryptFunction = (plaintext: string, pubkey: string) => Promise<string>;
105
- export type DecryptFunction = (ciphertext: string, pubkey: string) => Promise<string>;
106
- export type NostrPublish = (event: UnsignedEvent) => Promise<VerifiedEvent>;