Skip to content

Commit

Permalink
Fix issues with readBufferFromBigInt (#648)
Browse files Browse the repository at this point in the history
- Return the expected byte representation for unsigned bigints.
  • Loading branch information
rojvv authored Mar 12, 2024
1 parent 5da44f3 commit 2a13c07
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions gramjs/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,14 @@ export function readBufferFromBigInt(
if (!signed && bigIntVar.lesser(bigInt(0))) {
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");
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) {
buffer = buffer.reverse();
}
Expand Down

0 comments on commit 2a13c07

Please sign in to comment.