@kangwifi-pro/waliwa 1.1.4 → 1.1.6

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.
@@ -1,137 +1,85 @@
1
1
  /**
2
2
  * Waliwa - Noise Protocol XX handshake implementation
3
3
  *
4
- * WhatsApp Web Multi-Device menggunakan Noise Protocol Framework (XX pattern)
5
- * untuk establish secure channel antara client dan WhatsApp server.
6
- *
7
- * Pattern XX (lihat noiseprotocol.com):
8
- * <- e
9
- * -> e, ee, s, es
10
- * <- s, se
11
- *
12
- * Handshake ini menghasilkan encryption key untuk semua komunikasi WebSocket selanjutnya.
13
- *
14
- * Original implementation - tidak copy dari Baileys/ZapoJS.
4
+ * Matches Baileys implementation exactly:
5
+ * - authenticate(NOISE_HEADER) + authenticate(staticPublic) at init
6
+ * - mixIntoKey: HKDF with empty info, 64 bytes, split [0:32]=salt, [32:64]=encKey
7
+ * - IV: 12 bytes with 32-bit counter at offset 8
8
+ * - AAD: current hash value
9
+ * - finalize: HKDF(empty, salt, empty, 64) → [0:32]=encKey, [32:64]=decKey
15
10
  */
16
11
  import { KeyPair } from './crypto.js';
17
12
  export declare class NoiseHandshake {
18
- private state;
19
- private cipher;
20
- private readonly cipherKeyLen;
21
- private readonly nonceLen;
13
+ private hash;
14
+ private salt;
15
+ private encKey;
16
+ private decKey;
17
+ private readCounter;
18
+ private writeCounter;
19
+ private isFinished;
20
+ private s;
21
+ private e;
22
+ private rs;
23
+ private re;
22
24
  constructor(staticKey?: KeyPair, initiator?: boolean);
23
- private initializeSymmetric;
24
25
  /**
25
- * Mix key into chaining key + derive new cipher key
26
+ * Authenticate - mix data into handshake hash
26
27
  */
27
- private mixKey;
28
+ private authenticate;
28
29
  /**
29
- * Mix hash data into handshake hash
30
+ * Mix key into chaining key via HKDF
31
+ * Result: [0:32] = new salt, [32:64] = new encKey (= decKey during handshake)
30
32
  */
31
- private mixHash;
33
+ private mixIntoKey;
32
34
  /**
33
- * Encrypt dengan handshake key
35
+ * Generate IV: 12 bytes, 4-byte counter at offset 8 (big-endian)
34
36
  */
35
- private encryptWithHash;
37
+ private generateIV;
36
38
  /**
37
- * Decrypt dengan handshake key
39
+ * Encrypt with current encKey, AAD = hash
40
+ * Result: ciphertext + GCM tag (concatenated)
38
41
  */
39
- private decryptWithHash;
42
+ private encrypt;
40
43
  /**
41
- * Generate GCM nonce dari counter (big-endian, 4-byte prefix zero)
44
+ * Decrypt with current decKey, AAD = hash
42
45
  */
43
- private generateNonce;
46
+ private decrypt;
44
47
  /**
45
- * Step 1: Send ephemeral key (initiator -> responder)
46
- * Output: bytes untuk dikirim ke server
48
+ * Finish init - derive final traffic keys
47
49
  */
48
- writeEphemeral(): Uint8Array;
50
+ private finishInit;
51
+ /**
52
+ * Build Message 1: Send client ephemeral (protobuf-wrapped)
53
+ * Returns: protobuf field 2 { field 1: ephemeral }
54
+ */
55
+ buildClientHello(): Uint8Array;
49
56
  /**
50
- * Step 2: Receive server response (protobuf-wrapped)
51
- *
52
- * Server sends: [protobuf]
53
- * field 3 (length-delimited): contains
54
- * field 1 (32 bytes): server ephemeral key
55
- * field 2 (48 bytes): encrypted server static key (32 ct + 16 GCM tag)
56
- * [remaining]: encrypted server payload
57
- *
58
- * @param payload The protobuf-wrapped server response (after stripping 3-byte length prefix)
57
+ * Process Message 2: Server handshake response
58
+ * Expects protobuf with field 3 containing { field 1: ephemeral, field 2: enc_static, field 3: enc_payload }
59
59
  */
60
60
  readResponderHandshake(payload: Uint8Array): {
61
61
  plaintext: Uint8Array;
62
62
  };
63
63
  /**
64
- * Extract a protobuf field from a protobuf message
65
- * Returns the field's value (for length-delimited fields)
66
- */
67
- private extractProtobufField;
68
- /**
69
- * Find data after a specific field (for extracting remaining bytes)
70
- */
71
- private findAfterField;
72
- private readVarint;
73
- private skipField;
74
- /**
75
- * Step 3: Send our static + SE + payload (protobuf-wrapped)
76
- * Returns: protobuf field 4 containing [field 1: enc_static] [field 3: enc_payload]
77
- * MUST be called AFTER readResponderHandshake
64
+ * Build Message 3: Send our static (encrypted) + payload (encrypted)
65
+ * Returns: protobuf field 4 { field 1: enc_static, field 3: enc_payload }
78
66
  */
79
67
  writeClientResponse(payload: Uint8Array): Uint8Array;
80
68
  /**
81
- * Build protobuf field encoding (returns array of byte chunks)
82
- */
83
- private buildProtobufField;
84
- /**
85
- * Encode a complete protobuf field (tag + length + value)
86
- */
87
- private encodeProtobufField;
88
- /**
89
- * Encode a varint
90
- */
91
- private encodeVarint;
92
- /**
93
- * Finalize handshake - split prk into send/recv keys
69
+ * Finalize - return traffic keys for post-handshake communication
94
70
  */
95
71
  finalize(): {
96
72
  sendKey: Uint8Array;
97
73
  recvKey: Uint8Array;
98
74
  };
99
- /**
100
- * Encrypt a frame post-handshake menggunakan traffic keys
101
- */
75
+ private extractProtobufField;
76
+ private encodeProtobufField;
77
+ private readVarint;
78
+ private skipField;
79
+ private encodeVarint;
102
80
  static encryptFrame(key: Uint8Array, counter: number, plaintext: Uint8Array): Uint8Array;
103
- /**
104
- * Decrypt a frame post-handshake
105
- */
106
81
  static decryptFrame(key: Uint8Array, counter: number, ciphertext: Uint8Array): Uint8Array;
107
- /**
108
- * Encode WA noise frame: [WA_HEADER] [VERSION] [LENGTH:3] [PAYLOAD]
109
- */
110
- static encodeFrame(payload: Uint8Array): Uint8Array;
111
- /**
112
- * Encode WA client payload (untuk handshake init message)
113
- * Format: [ephemeral_pubkey:32] [static_encrypted] [payload_encrypted]
114
- */
115
- static encodeClientHello(ephemeralPub: Uint8Array, encryptedStatic: Uint8Array, encryptedPayload: Uint8Array): Uint8Array;
116
- /**
117
- * Build initial client hello - Message 1 of Noise XX
118
- *
119
- * WA Protocol: Wrap ephemeral in protobuf field 2:
120
- * field 2 (length-delimited): contains
121
- * field 1 (32 bytes): ephemeral public key
122
- *
123
- * Encoding: 12 22 0a 20 [32 bytes ephemeral]
124
- * - 12 = field 2, wire type 2 (length-delimited)
125
- * - 22 = length 34
126
- * - 0a = field 1, wire type 2
127
- * - 20 = length 32
128
- * - [32 bytes] = ephemeral public key
129
- */
130
- buildClientHello(): Uint8Array;
131
82
  }
132
- /**
133
- * Traffic cipher untuk encrypt/decrypt frames setelah handshake done
134
- */
135
83
  export declare class TrafficCipher {
136
84
  private sendKey;
137
85
  private recvKey;
@@ -142,9 +90,5 @@ export declare class TrafficCipher {
142
90
  decrypt(ciphertext: Uint8Array): Uint8Array;
143
91
  reset(): void;
144
92
  }
145
- /**
146
- * Build initial client payload untuk WA Web multi-device connect
147
- * Berisi info browser, version, dan props
148
- */
149
93
  export declare function buildClientPayload(browser: [string, string, string]): Uint8Array;
150
94
  //# sourceMappingURL=noise.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"noise.d.ts","sourceRoot":"","sources":["../../src/core/noise.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAUL,OAAO,EAER,MAAM,aAAa,CAAC;AAyCrB,qBAAa,cAAc;IACzB,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAM;IACnC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAM;gBAEnB,SAAS,CAAC,EAAE,OAAO,EAAE,SAAS,GAAE,OAAc;IAe1D,OAAO,CAAC,mBAAmB;IAS3B;;OAEG;IACH,OAAO,CAAC,MAAM;IAad;;OAEG;IACH,OAAO,CAAC,OAAO;IAIf;;OAEG;IACH,OAAO,CAAC,eAAe;IAcvB;;OAEG;IACH,OAAO,CAAC,eAAe;IAevB;;OAEG;IACH,OAAO,CAAC,aAAa;IAWrB;;;OAGG;IACI,cAAc,IAAI,UAAU;IAQnC;;;;;;;;;;OAUG;IACI,sBAAsB,CAAC,OAAO,EAAE,UAAU,GAAG;QAAE,SAAS,EAAE,UAAU,CAAA;KAAE;IAwD7E;;;OAGG;IACH,OAAO,CAAC,oBAAoB;IAuB5B;;OAEG;IACH,OAAO,CA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