Skip to content

Commit

Permalink
Remove Big varint lib (#677)
Browse files Browse the repository at this point in the history
* Remove big-varint lib

* Move big-varint to utils
  • Loading branch information
Ben-Rey authored Sep 13, 2024
1 parent d582466 commit 6cbade5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
1 change: 0 additions & 1 deletion jest.browser.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ module.exports = {
],
'^.+\\.jsx?$': 'babel-jest',
},
transformIgnorePatterns: ['node_modules/(?!(big-varint)/)'],
}
1 change: 0 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const config: Config.InitialOptions = {
},
testMatch: ['<rootDir>/test/**/*.(spec|test).ts?(x)'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/dist/'],
transformIgnorePatterns: ['/node_modules/(?!big-varint).+\\.js$'],
}

export default config
6 changes: 0 additions & 6 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"@noble/hashes": "^1.2.0",
"@open-rpc/client-js": "^1.8.1",
"@open-rpc/schema-utils-js": "1.15.0",
"big-varint": "^0.1.3",
"bs58check": "^4.0.0",
"decimal.js": "^10.4.3",
"dotenv": "^16.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/operation/operationManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { unsigned } from 'big-varint'
import * as unsigned from '../utils/big-varint'
import { Address } from '../basicElements/address'
import { PrivateKey, PublicKey } from '../basicElements/keys'
import { PublicAPI } from '../client'
Expand Down
60 changes: 60 additions & 0 deletions src/utils/big-varint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable @typescript-eslint/no-magic-numbers */
// This code was taken from the lib big-varint. https://github.com/joeltg/big-varint/blob/65346e5688245b20f05e5ce2dd8c784eb3ae3e15/src/unsigned.ts#L1C1-L58C2
const LIMIT = 0x7fn

export function encodingLength(value: bigint): number {
let i = 0

for (; value >= 0x80n; i++) {
value >>= 7n
}

return i + 1
}

export function encode(
i: bigint,
buffer?: ArrayBuffer,
byteOffset?: number
): Uint8Array {
if (i < 0n) {
throw new RangeError('value must be unsigned')
}

const byteLength = encodingLength(i)
buffer = buffer || new ArrayBuffer(byteLength)
byteOffset = byteOffset || 0
if (buffer.byteLength < byteOffset + byteLength) {
throw new RangeError(
'the buffer is too small to encode the number at the offset'
)
}

const array = new Uint8Array(buffer, byteOffset)

let offset = 0
while (LIMIT < i) {
array[offset++] = Number(i & LIMIT) | 0x80
i >>= 7n
}

array[offset] = Number(i)

return array
}

export function decode(data: Uint8Array, offset = 0): bigint {
let i = 0n
let n = 0
let b: number
do {
b = data[offset + n]
if (b === undefined) {
throw new RangeError('offset out of range')
}

i += BigInt(b & 0x7f) << BigInt(n * 7)
n++
} while (0x80 <= b)
return i
}

1 comment on commit 6cbade5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for experimental massa-web3

St.
Category Percentage Covered / Total
🟡 Statements 63.46% 1134/1787
🔴 Branches 43.91% 191/435
🔴 Functions 46.76% 209/447
🟡 Lines 63.76% 1128/1769

Test suite run success

129 tests passing in 13 suites.

Report generated by 🧪jest coverage report action from 6cbade5

Please sign in to comment.