Skip to content

Commit

Permalink
Merge pull request #234 from rsksmart/fix-VarInt
Browse files Browse the repository at this point in the history
Fixes VarInt
  • Loading branch information
marcos-iov authored Feb 26, 2025
2 parents 9f2a1dc + 0144744 commit c74e593
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/varint.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class VarInt {
constructor(value) {
constructor(value, offset = 0) {
if (typeof value === 'number' || typeof value === 'bigint') {
this.value = BigInt(value);
this.originallyEncodedSize = this.getSizeInBytes();
} else if (Buffer.isBuffer(value)) {
this.value = this.decode(value);
this.value = this.decode(value, offset);
this.originallyEncodedSize = this.getSizeInBytes();
} else {
throw new Error('Invalid input: value should be a number or buffer');
Expand Down Expand Up @@ -49,16 +49,16 @@ class VarInt {
}
}

decode(buffer) {
const first = buffer[0];
decode(buffer, offset = 0) {
const first = buffer[offset];
if (first < 253) {
return BigInt(first);
} else if (first === 253) {
return BigInt(buffer.readUInt16LE(1));
return BigInt(buffer.readUInt16LE(offset + 1));
} else if (first === 254) {
return BigInt(buffer.readUInt32LE(1));
return BigInt(buffer.readUInt32LE(offset + 1));
} else {
return buffer.readBigUInt64LE(1);
return buffer.readBigUInt64LE(offset + 1);
}
}
}
Expand Down

0 comments on commit c74e593

Please sign in to comment.