small fix dataView buffer

This commit is contained in:
invocamanman
2020-08-21 13:57:48 +02:00
parent e0d1a9dee5
commit e9a84dd342
2 changed files with 10 additions and 2 deletions

View File

@@ -89,7 +89,7 @@ export function beInt2Buff(n, len) {
export function leBuff2int(buff) {
let res = 0n;
let i = 0;
const buffV = new DataView(buff.buffer);
const buffV = new DataView(buff.buffer, buff.byteOffset);
while (i<buff.length) {
if (i + 4 <= buff.length) {
res += BigInt(buffV.getUint32(i, true)) << BigInt( i*8);

View File

@@ -12,10 +12,18 @@ describe("Utils native", () => {
it("Should convert integer to buffer little-endian", () => {
const buff = utilsN.leInt2Buff(num, 32);
const numFromBuff = utilsN.leBuff2int(buff);
assert(ScalarN.eq(num, numFromBuff), true);
});
it("Should convert integer to buffer little-endian 2", () => {
const buff1 = Buffer.from("Rollup_DB_EthAddr");
const buff2= Buffer.from("Rollup_DB_ChainID");
const numFromBuff = utilsN.leBuff2int(buff1);
const numFromBuff2 = utilsN.leBuff2int(buff2);
assert.equal(ScalarN.eq(numFromBuff, numFromBuff2), false)
});
it("Should convert integer to buffer big-endian", () => {
const buff = utilsN.beInt2Buff(num, 32);
const numFromBuff = utilsN.beBuff2int(buff);