Skip to content

Commit

Permalink
Merge pull request #89 from tinymanorg/fix/bytes-to-int-util-function
Browse files Browse the repository at this point in the history
Fix int to byte conversion for large values
  • Loading branch information
gulcinuras authored Aug 7, 2024
2 parents ddb09e9 + 8938dd5 commit fcd809f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tinymanorg/tinyman-js-sdk",
"version": "4.0.2",
"version": "4.0.3",
"description": "Tinyman JS SDK",
"author": "Tinyman Core Team",
"license": "MIT",
Expand Down
14 changes: 7 additions & 7 deletions src/governance/util/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable no-bitwise */
export function intToBytes(num: number, length = 8): Uint8Array {
const byteArray = new Uint8Array(length);
let newNum = num;
let newNum = BigInt(num);

for (let i = length - 1; i >= 0; i--) {
byteArray[i] = newNum & 0xff;
newNum >>= 8;
byteArray[i] = Number(newNum & BigInt(0xff));
newNum >>= BigInt(8);
}

return byteArray;
Expand All @@ -16,16 +16,16 @@ export function areBuffersEqual(buf1: Uint8Array, buf2: Uint8Array) {
}

export function bytesToInt(buffer: Uint8Array): number {
let num = 0;
let num = 0n;

for (let i = 0; i < buffer.byteLength; i++) {
const byte = buffer[i];

num *= 0x100;
num += byte;
num *= BigInt(0x100);
num += BigInt(byte);
}

return num;
return Number(num);
}

export function sum(values: number[]): number {
Expand Down

0 comments on commit fcd809f

Please sign in to comment.