Skip to content

React-native bigint64 deserialization problem - refactoring/bigint64le #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
34 changes: 34 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
],
"license": "Apache-2.0",
"devDependencies": {
"@types/big-integer": "^0.0.31",
"@types/jest": "^26.0.23",
"jest": "^27.3.1",
"prettier": "^2.3.0",
Expand All @@ -39,6 +40,7 @@
"dependencies": {
"@solana/web3.js": "^1.30.2",
"assert": "^2.0.0",
"big-integer": "^1.6.51",
"buffer": "^6.0.1"
}
}
9 changes: 4 additions & 5 deletions src/readBig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer'
import BigInt from 'big-integer'

// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L758
const ERR_BUFFER_OUT_OF_BOUNDS = () => new Error('Attempt to access memory outside buffer bounds')
Expand Down Expand Up @@ -36,10 +37,8 @@ export function readBigInt64LE(buffer: Buffer, offset = 0): bigint {
if (first === undefined || last === undefined) boundsError(offset, buffer.length - 8)
// tslint:disable-next-line:no-bitwise
const val = buffer[offset + 4] + buffer[offset + 5] * 2 ** 8 + buffer[offset + 6] * 2 ** 16 + (last << 24) // Overflow
return (
(BigInt(val) << BigInt(32)) + // tslint:disable-line:no-bitwise
BigInt(first + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + buffer[++offset] * 2 ** 24)
)
const result = BigInt(val).shiftLeft(32).add(BigInt(first + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + buffer[++offset] * 2 ** 24))
return result;
}

// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L89-L107
Expand All @@ -53,5 +52,5 @@ export function readBigUInt64LE(buffer: Buffer, offset = 0): bigint {

const hi = buffer[++offset] + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + last * 2 ** 24

return BigInt(lo) + (BigInt(hi) << BigInt(32)) // tslint:disable-line:no-bitwise
return BigInt(hi).shiftLeft(32).add(lo) // tslint:disable-line:no-bitwise
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"esModuleInterop": true,
"strict": true
},
"include": ["src"],
Expand Down