From 4d6b8414dc10b51f5c180e143b4ee8520f306764 Mon Sep 17 00:00:00 2001 From: BenRey Date: Thu, 12 Sep 2024 23:45:11 +0200 Subject: [PATCH] Remove big-varint lib --- jest.browser.config.js | 1 - jest.config.ts | 1 - package-lock.json | 6 ---- package.json | 1 - src/operation/big-varint.ts | 60 +++++++++++++++++++++++++++++++ src/operation/operationManager.ts | 2 +- 6 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 src/operation/big-varint.ts diff --git a/jest.browser.config.js b/jest.browser.config.js index 37dd9b27..1b7620a6 100644 --- a/jest.browser.config.js +++ b/jest.browser.config.js @@ -10,5 +10,4 @@ module.exports = { ], '^.+\\.jsx?$': 'babel-jest', }, - transformIgnorePatterns: ['node_modules/(?!(big-varint)/)'], } diff --git a/jest.config.ts b/jest.config.ts index 889ef4ef..3f8d99d3 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -13,7 +13,6 @@ const config: Config.InitialOptions = { }, testMatch: ['/test/**/*.(spec|test).ts?(x)'], testPathIgnorePatterns: ['/node_modules/', '/dist/'], - transformIgnorePatterns: ['/node_modules/(?!big-varint).+\\.js$'], } export default config diff --git a/package-lock.json b/package-lock.json index 99196e73..c6809028 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,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", @@ -4190,11 +4189,6 @@ "node": ">=10.0.0" } }, - "node_modules/big-varint": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/big-varint/-/big-varint-0.1.3.tgz", - "integrity": "sha512-HgJQG8DtWAaYtrXTCasQukrMO0jbtQpd4BPEjr+JHoLRrbyIr8IxrMp01OZVJCoZBdqGynGHvp2iz0mabXz6dA==" - }, "node_modules/bl": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", diff --git a/package.json b/package.json index 00728899..c63ac9d5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/operation/big-varint.ts b/src/operation/big-varint.ts new file mode 100644 index 00000000..56f79b25 --- /dev/null +++ b/src/operation/big-varint.ts @@ -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 +} diff --git a/src/operation/operationManager.ts b/src/operation/operationManager.ts index addeae6e..e84f4ce8 100644 --- a/src/operation/operationManager.ts +++ b/src/operation/operationManager.ts @@ -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'