Преглед изворни кода

Fix issues with readBufferFromBigInt (#648)

- Return the expected byte representation for unsigned bigints.
Roj пре 1 година
родитељ
комит
2a13c074f5
1 измењених фајлова са 3 додато и 10 уклоњено
  1. 3 10
      gramjs/Helpers.ts

+ 3 - 10
gramjs/Helpers.ts

@@ -137,21 +137,14 @@ export function readBufferFromBigInt(
     if (!signed && bigIntVar.lesser(bigInt(0))) {
     if (!signed && bigIntVar.lesser(bigInt(0))) {
         throw new Error("Cannot convert to unsigned");
         throw new Error("Cannot convert to unsigned");
     }
     }
-    let below = false;
-    if (bigIntVar.lesser(bigInt(0))) {
-        below = true;
-        bigIntVar = bigIntVar.abs();
+
+    if (signed && bigIntVar.lesser(bigInt(0))) {
+        bigIntVar = bigInt(2).pow(bigInt(bytesNumber).multiply(8)).add(bigIntVar);
     }
     }
 
 
     const hex = bigIntVar.toString(16).padStart(bytesNumber * 2, "0");
     const hex = bigIntVar.toString(16).padStart(bytesNumber * 2, "0");
     let buffer = Buffer.from(hex, "hex");
     let buffer = Buffer.from(hex, "hex");
 
 
-    if (signed && below) {
-        buffer[buffer.length - 1] = 256 - buffer[buffer.length - 1];
-        for (let i = 0; i < buffer.length - 1; i++) {
-            buffer[i] = 255 - buffer[i];
-        }
-    }
     if (little) {
     if (little) {
         buffer = buffer.reverse();
         buffer = buffer.reverse();
     }
     }