@session.js/blinded-session-id 1.0.1 → 1.0.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Viktor Shchelochkov <hi@hloth.dev> (https://hloth.dev)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -37,4 +37,8 @@ Use Session messenger programmatically with [Session.js](https://github.com/sess
37
37
 
38
38
  ## Donate
39
39
 
40
- [hloth.dev/donate](https://hloth.dev/donate)
40
+ [hloth.dev/donate](https://hloth.dev/donate) · Tor: [hlothdevzkti6suoksy7lcy7hmpxnr3msu5waokzaslsi2mnx5ouu4qd.onion/donate](http://hlothdevzkti6suoksy7lcy7hmpxnr3msu5waokzaslsi2mnx5ouu4qd.onion/donate)
41
+
42
+ ## License
43
+
44
+ [MIT](./LICENSE)
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export declare function crypto_sign_curve25519_pk_to_ed25519(pk: Uint8Array): Uint8Array;
2
2
  export declare const convertToEd25519Key: (key: string) => string;
3
3
  export declare const convertToX25519Key: (key: string) => string;
4
+ export declare function crypto_core_ed25519_scalar_reduce(scalar: Uint8Array): Uint8Array;
4
5
  export declare const generateKA: (sessionId: string, serverPk: string) => Uint8Array;
5
6
  export declare const generateBlindedKeys: (sessionId: string, serverPk: string) => Uint8Array[];
6
7
  export declare const blindSessionId: ({ sessionId, serverPk }: {
package/dist/index.js CHANGED
@@ -1,9 +1,41 @@
1
1
  // Huge thanks to li0ard for this code
2
2
  // CREDIT: https://github.com/theinfinityway/session_id/
3
3
  /* eslint-disable prefer-const */
4
- import { cloneDeep } from 'lodash';
5
- import sodium from 'libsodium-wrappers-sumo';
6
- await sodium.ready;
4
+ import { ed25519 } from "@noble/curves/ed25519.js";
5
+ import { bytesToHex, hexToBytes } from "@noble/curves/utils.js";
6
+ import { blake2b } from "@noble/hashes/blake2.js";
7
+ import { mod } from "@noble/curves/abstract/modular.js";
8
+ function bytesToNumberLE(bytes) {
9
+ let value = 0n;
10
+ for (let i = bytes.length - 1; i >= 0; i--) {
11
+ value = (value << 8n) + BigInt(bytes[i]);
12
+ }
13
+ return value;
14
+ }
15
+ function numberToBytesLE(num, length) {
16
+ const out = new Uint8Array(length);
17
+ let n = num;
18
+ for (let i = 0; i < length; i++) {
19
+ out[i] = Number(n & 0xffn);
20
+ n >>= 8n;
21
+ }
22
+ return out;
23
+ }
24
+ function ed25519ScalarmultNoClamp(scalar32, point32) {
25
+ if (scalar32.length !== 32) {
26
+ throw new Error(`ed25519ScalarmultNoClamp: expected 32-byte scalar, got ${scalar32.length}`);
27
+ }
28
+ if (point32.length !== 32) {
29
+ throw new Error(`ed25519ScalarmultNoClamp: expected 32-byte point, got ${point32.length}`);
30
+ }
31
+ const L = ed25519.Point.Fn.ORDER;
32
+ const s = bytesToNumberLE(scalar32) % L;
33
+ const P = ed25519.Point.fromBytes(point32);
34
+ if (P.isSmallOrder()) {
35
+ throw new Error("ed25519ScalarmultNoClamp: invalid point (small order)");
36
+ }
37
+ return P.multiply(s).toBytes();
38
+ }
7
39
  function gf(init) {
8
40
  let i, r = new Float64Array(16);
9
41
  if (init)
@@ -507,27 +539,34 @@ function convertPublicKey(pk) {
507
539
  return z;
508
540
  }
509
541
  export const convertToEd25519Key = (key) => {
510
- const inbin = sodium.from_hex(key);
542
+ const inbin = hexToBytes(key);
511
543
  const xEd25519Key = crypto_sign_curve25519_pk_to_ed25519(inbin);
512
- return sodium.to_hex(xEd25519Key);
544
+ return bytesToHex(xEd25519Key);
513
545
  };
514
546
  export const convertToX25519Key = (key) => {
515
- const inbin = sodium.from_hex(key);
516
- const xEd25519Key = sodium.crypto_sign_ed25519_pk_to_curve25519(inbin);
517
- return sodium.to_hex(xEd25519Key);
547
+ const inbin = hexToBytes(key);
548
+ const xEd25519Key = ed25519.utils.toMontgomery(inbin);
549
+ return bytesToHex(xEd25519Key);
518
550
  };
519
551
  function combineKeys(lhsKeyBytes, rhsKeyBytes) {
520
- return sodium.crypto_scalarmult_ed25519_noclamp(lhsKeyBytes, rhsKeyBytes);
552
+ return ed25519ScalarmultNoClamp(lhsKeyBytes, rhsKeyBytes);
553
+ }
554
+ export function crypto_core_ed25519_scalar_reduce(scalar) {
555
+ const scalarNum = bytesToNumberLE(scalar);
556
+ const result = mod(scalarNum, ed25519.Point.Fn.ORDER);
557
+ return numberToBytesLE(result, 32);
521
558
  }
522
559
  const generateBlindingFactor = (serverPk) => {
523
- const hexServerPk = sodium.from_hex(serverPk);
524
- const serverPkHash = sodium.crypto_generichash(64, hexServerPk);
525
- return sodium.crypto_core_ed25519_scalar_reduce(serverPkHash);
560
+ const hexServerPk = hexToBytes(serverPk);
561
+ const serverPkHash = blake2b(hexServerPk, {
562
+ dkLen: 64,
563
+ });
564
+ return crypto_core_ed25519_scalar_reduce(serverPkHash);
526
565
  };
527
566
  export const generateKA = (sessionId, serverPk) => {
528
567
  const sessionIdNoPrefix = sessionId.substring(2);
529
568
  const kBytes = generateBlindingFactor(serverPk);
530
- const xEd25519Key = sodium.from_hex(convertToEd25519Key(sessionIdNoPrefix));
569
+ const xEd25519Key = hexToBytes(convertToEd25519Key(sessionIdNoPrefix));
531
570
  const kA = combineKeys(kBytes, xEd25519Key);
532
571
  return kA;
533
572
  };
@@ -535,14 +574,16 @@ export const generateBlindedKeys = (sessionId, serverPk) => {
535
574
  const kA = generateKA(sessionId, serverPk);
536
575
  const key1 = kA;
537
576
  const modifiedByte = kA[31] & 0x7F;
538
- const key2 = Buffer.concat([kA.slice(0, 31), Buffer.from([modifiedByte])]);
577
+ const key2 = new Uint8Array(32);
578
+ key2.set(kA.slice(0, 31), 0);
579
+ key2[31] = modifiedByte;
539
580
  return [key1, key2];
540
581
  };
541
582
  export const blindSessionId = ({ sessionId, serverPk }) => {
542
583
  const [key1, key2] = generateBlindedKeys(sessionId, serverPk);
543
584
  const isKey2 = key1[31] & 0x80;
544
585
  if (isKey2) {
545
- return '15' + sodium.to_hex(key2);
586
+ return '15' + bytesToHex(key2);
546
587
  }
547
- return '15' + sodium.to_hex(key1);
588
+ return '15' + bytesToHex(key1);
548
589
  };
package/package.json CHANGED
@@ -1,36 +1,37 @@
1
1
  {
2
2
  "name": "@session.js/blinded-session-id",
3
- "version": "1.0.1",
3
+ "type": "module",
4
+ "description": "Utility JavaScript library with methods to work with Session's blinded Session ID",
5
+ "version": "1.0.3",
6
+ "author": "Viktor Shchelochkov <hi@hloth.dev> (https://hloth.dev)",
4
7
  "main": "dist/index.js",
5
8
  "types": "dist/index.d.ts",
6
- "author": "Viktor Shchelochkov <hi@hloth.dev> (https://hloth.dev)",
7
9
  "repository": {
8
10
  "type": "git",
9
- "url": "git+https://github.com/sessionjs/blinded-session-id.git"
11
+ "url": "git+https://git.hloth.dev/session.js/blinded-session-id.git"
10
12
  },
11
13
  "bugs": {
12
- "url": "https://github.com/sessionjs/blinded-session-id/issues"
14
+ "url": "https://git.hloth.dev/session.js/blinded-session-id/issues"
13
15
  },
14
- "homepage": "https://github.com/sessionjs/blinded-session-id#readme",
15
- "description": "Utility JavaScript library with methods to work with Session's blinded Session ID",
16
- "type": "module",
17
- "devDependencies": {
18
- "@types/bun": "^1.1.6",
19
- "@types/libsodium-wrappers-sumo": "^0.7.8",
20
- "@types/lodash": "^4.17.7"
16
+ "homepage": "https://git.hloth.dev/session.js/blinded-session-id#readme",
17
+ "license": "MIT",
18
+ "funding": "https://hloth.dev/donate",
19
+ "scripts": {
20
+ "build": "rm -rf dist && tsc --project tsconfig.build.json"
21
21
  },
22
22
  "files": [
23
23
  "dist/index.js",
24
24
  "dist/index.d.ts"
25
25
  ],
26
- "scripts": {
27
- "build": "rm -rf dist && tsc --project tsconfig.build.json"
26
+ "dependencies": {
27
+ "@noble/curves": "^2.0.1",
28
+ "@noble/hashes": "^2.0.1"
28
29
  },
29
30
  "peerDependencies": {
30
31
  "typescript": "^5.0.0"
31
32
  },
32
- "dependencies": {
33
- "libsodium-wrappers-sumo": "^0.7.13",
34
- "lodash": "^4.17.21"
33
+ "devDependencies": {
34
+ "@types/bun": "^1.1.6",
35
+ "@types/lodash": "^4.17.7"
35
36
  }
36
37
  }