Skip to content

Commit

Permalink
Remove big-varint lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Sep 12, 2024
1 parent d582466 commit 4d6b841
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
60 changes: 60 additions & 0 deletions src/operation/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

Check warning on line 9 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

return i + 1
}

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

Check warning on line 21 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 22 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

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'
)

Check warning on line 30 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 31 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const array = new Uint8Array(buffer, byteOffset)

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

Check warning on line 37 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
i >>= 7n

Check warning on line 38 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

array[offset] = Number(i)

return array
}

export function decode(data: Uint8Array, offset = 0): bigint {

Check warning on line 46 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
let i = 0n
let n = 0
let b: number
do {
b = data[offset + n]
if (b === undefined) {
throw new RangeError('offset out of range')

Check warning on line 53 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 54 in src/operation/big-varint.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

i += BigInt(b & 0x7f) << BigInt(n * 7)
n++
} while (0x80 <= b)
return i
}
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 './big-varint'
import { Address } from '../basicElements/address'
import { PrivateKey, PublicKey } from '../basicElements/keys'
import { PublicAPI } from '../client'
Expand Down

0 comments on commit 4d6b841

Please sign in to comment.