@gsknnft/bigint-buffer 1.4.7 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -144
- package/dist/build/Release/bigint_buffer.node +0 -0
- package/dist/conversion/bundle.iife.js +421 -0
- package/dist/conversion/bundle.umd.js +423 -0
- package/dist/conversion/cjs/converter.d.ts +14 -0
- package/dist/conversion/cjs/index.d.ts +33 -0
- package/dist/conversion/cjs/index.js +418 -0
- package/dist/conversion/cjs/index.node.js +4 -0
- package/dist/conversion/cjs/package.json +1 -0
- package/dist/conversion/converter.d.ts +14 -0
- package/dist/conversion/esm/bundle.js +416 -0
- package/dist/conversion/esm/bundle.min.js +1 -0
- package/dist/conversion/esm/converter.d.ts +14 -0
- package/dist/conversion/esm/index.browser.js +416 -0
- package/dist/conversion/esm/index.d.ts +33 -0
- package/dist/conversion/esm/index.js +416 -0
- package/dist/conversion/esm/index.node.js +2 -0
- package/dist/conversion/esm/package.json +1 -0
- package/dist/conversion/index.d.ts +1 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +190 -250
- package/dist/index.umd.js +1 -1
- package/dist/types/conversion/index.d.ts +1 -2
- package/dist/types/conversion/src/index.d.ts +2 -0
- package/dist/types/conversion/src/ts/index.d.ts +4 -2
- package/dist/types/index.d.ts +4 -3
- package/package.json +141 -136
- package/scripts/postinstall.cjs +5 -3
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types/bigint-buffer.test.d.ts +0 -1
- package/dist/types/conversion/test/bigintToBase64.test.d.ts +0 -1
- package/dist/types/conversion/test/bigintToBuf.test.d.ts +0 -1
- package/dist/types/conversion/test/bigintToHex.test.d.ts +0 -1
- package/dist/types/conversion/test/bigintToText.test.d.ts +0 -1
- package/dist/types/conversion/test/bufToBigint.test.d.ts +0 -1
- package/dist/types/conversion/test/fixedPoint.test.d.ts +0 -1
- package/dist/types/conversion/test/hexToBigint.test.d.ts +0 -1
- package/dist/types/conversion/test/hexToBuf.test.d.ts +0 -1
- package/dist/types/conversion/test/parseHex.test.d.ts +0 -1
- package/dist/types/conversion/test/setup.test.d.ts +0 -1
- package/dist/types/conversion/test/textToBuf.test.d.ts +0 -1
- package/dist/types/conversion/vite.config.d.ts +0 -2
- package/dist/types/conversion/vitest.config.d.ts +0 -2
- package/dist/types/index.bench.d.ts +0 -1
- package/dist/types/index.spec.d.ts +0 -1
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
3
|
+
factory();
|
|
4
|
+
})((function () { 'use strict';
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.toBigIntValue = exports.toHexString = exports.ZERO_FIXED_POINT = exports.FIXED_POINT_PATTERN = exports.FIXED_POINT_DECIMALS = void 0;
|
|
9
|
+
exports.toBigIntLE = toBigIntLE;
|
|
10
|
+
exports.validateBigIntBuffer = validateBigIntBuffer;
|
|
11
|
+
exports.toBigIntBE = toBigIntBE;
|
|
12
|
+
exports.toBufferLE = toBufferLE;
|
|
13
|
+
exports.toBufferBE = toBufferBE;
|
|
14
|
+
exports.parseHex = parseHex;
|
|
15
|
+
exports.bigintToBuf = bigintToBuf;
|
|
16
|
+
exports.bufToBigint = bufToBigint;
|
|
17
|
+
exports.bigintToHex = bigintToHex;
|
|
18
|
+
exports.hexToBigint = hexToBigint;
|
|
19
|
+
exports.bigintToText = bigintToText;
|
|
20
|
+
exports.textToBigint = textToBigint;
|
|
21
|
+
exports.bufToText = bufToText;
|
|
22
|
+
exports.textToBuf = textToBuf;
|
|
23
|
+
exports.bufToHex = bufToHex;
|
|
24
|
+
exports.hexToBuf = hexToBuf;
|
|
25
|
+
exports.bigintToBase64 = bigintToBase64;
|
|
26
|
+
exports.base64ToBigint = base64ToBigint;
|
|
27
|
+
exports.toFixedPoint = toFixedPoint;
|
|
28
|
+
exports.fromFixedPoint = fromFixedPoint;
|
|
29
|
+
exports.addFixedPoint = addFixedPoint;
|
|
30
|
+
exports.subtractFixedPoint = subtractFixedPoint;
|
|
31
|
+
exports.averageFixedPoint = averageFixedPoint;
|
|
32
|
+
exports.compareFixedPoint = compareFixedPoint;
|
|
33
|
+
exports.fixedPointToBigInt = fixedPointToBigInt;
|
|
34
|
+
const converter_1 = require("./converter");
|
|
35
|
+
let converter;
|
|
36
|
+
const hasRead64LE = typeof Buffer.prototype.readBigUInt64LE === "function";
|
|
37
|
+
const hasRead64BE = typeof Buffer.prototype.readBigUInt64BE === "function";
|
|
38
|
+
const hasWrite64LE = typeof Buffer.prototype.writeBigUInt64LE === "function";
|
|
39
|
+
const hasWrite64BE = typeof Buffer.prototype.writeBigUInt64BE === "function";
|
|
40
|
+
const read64LE = hasRead64LE
|
|
41
|
+
? (buf, offset) => buf.readBigUInt64LE(offset)
|
|
42
|
+
: (buf, offset) => {
|
|
43
|
+
let out = 0n;
|
|
44
|
+
for (let i = 7; i >= 0; i--) {
|
|
45
|
+
out = (out << 8n) + BigInt(buf[offset + i]);
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
};
|
|
49
|
+
const read64BE = hasRead64BE
|
|
50
|
+
? (buf, offset) => buf.readBigUInt64BE(offset)
|
|
51
|
+
: (buf, offset) => {
|
|
52
|
+
let out = 0n;
|
|
53
|
+
for (let i = 0; i < 8; i++) {
|
|
54
|
+
out = (out << 8n) + BigInt(buf[offset + i]);
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
};
|
|
58
|
+
const write64LE = hasWrite64LE
|
|
59
|
+
? (buf, offset, value) => {
|
|
60
|
+
buf.writeBigUInt64LE(value, offset);
|
|
61
|
+
}
|
|
62
|
+
: (buf, offset, value) => {
|
|
63
|
+
let temp = value;
|
|
64
|
+
for (let i = 0; i < 8; i++) {
|
|
65
|
+
buf[offset + i] = Number(temp & 0xffn);
|
|
66
|
+
temp >>= 8n;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const write64BE = hasWrite64BE
|
|
70
|
+
? (buf, offset, value) => {
|
|
71
|
+
buf.writeBigUInt64BE(value, offset);
|
|
72
|
+
}
|
|
73
|
+
: (buf, offset, value) => {
|
|
74
|
+
let temp = value;
|
|
75
|
+
for (let i = 7; i >= 0; i--) {
|
|
76
|
+
buf[offset + i] = Number(temp & 0xffn);
|
|
77
|
+
temp >>= 8n;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const bufferToBigIntLE = (buf) => {
|
|
81
|
+
let result = 0n;
|
|
82
|
+
let multiplier = 1n;
|
|
83
|
+
const len = buf.length;
|
|
84
|
+
const remainder = len & 7;
|
|
85
|
+
for (let i = 0; i < remainder; i++) {
|
|
86
|
+
result += BigInt(buf[i]) * multiplier;
|
|
87
|
+
multiplier <<= 8n;
|
|
88
|
+
}
|
|
89
|
+
for (let offset = remainder; offset < len; offset += 8) {
|
|
90
|
+
const chunk = read64LE(buf, offset);
|
|
91
|
+
result += chunk * multiplier;
|
|
92
|
+
multiplier <<= 64n;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
};
|
|
96
|
+
const bufferToBigIntBE = (buf) => {
|
|
97
|
+
const len = buf.length;
|
|
98
|
+
if (len === 0)
|
|
99
|
+
return 0n;
|
|
100
|
+
let result = 0n;
|
|
101
|
+
const remainder = len & 7;
|
|
102
|
+
let offset = 0;
|
|
103
|
+
if (remainder !== 0) {
|
|
104
|
+
for (; offset < remainder; offset++) {
|
|
105
|
+
result = (result << 8n) + BigInt(buf[offset]);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (; offset < len; offset += 8) {
|
|
109
|
+
const chunk = read64BE(buf, offset);
|
|
110
|
+
result = (result << 64n) + chunk;
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
};
|
|
114
|
+
const writeBigIntToBufferLE = (num, target) => {
|
|
115
|
+
const width = target.length;
|
|
116
|
+
let remaining = num;
|
|
117
|
+
let offset = 0;
|
|
118
|
+
const limit = width - (width % 8);
|
|
119
|
+
for (; offset < limit; offset += 8) {
|
|
120
|
+
write64LE(target, offset, remaining & 0xffffffffffffffffn);
|
|
121
|
+
remaining >>= 64n;
|
|
122
|
+
}
|
|
123
|
+
for (; offset < width; offset++) {
|
|
124
|
+
target[offset] = Number(remaining & 0xffn);
|
|
125
|
+
remaining >>= 8n;
|
|
126
|
+
}
|
|
127
|
+
return target;
|
|
128
|
+
};
|
|
129
|
+
const writeBigIntToBufferBE = (num, target) => {
|
|
130
|
+
const width = target.length;
|
|
131
|
+
let remaining = num;
|
|
132
|
+
let offset = width;
|
|
133
|
+
const limit = width & ~7;
|
|
134
|
+
while (offset > limit) {
|
|
135
|
+
offset--;
|
|
136
|
+
target[offset] = Number(remaining & 0xffn);
|
|
137
|
+
remaining >>= 8n;
|
|
138
|
+
}
|
|
139
|
+
for (; offset > 0; offset -= 8) {
|
|
140
|
+
const chunk = remaining & 0xffffffffffffffffn;
|
|
141
|
+
write64BE(target, offset - 8, chunk);
|
|
142
|
+
remaining >>= 64n;
|
|
143
|
+
}
|
|
144
|
+
return target;
|
|
145
|
+
};
|
|
146
|
+
const toBufferInput = (input) => {
|
|
147
|
+
if (Buffer.isBuffer(input))
|
|
148
|
+
return input;
|
|
149
|
+
if (input instanceof Uint8Array) {
|
|
150
|
+
return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
|
|
151
|
+
}
|
|
152
|
+
return Buffer.from(input);
|
|
153
|
+
};
|
|
154
|
+
const assertWidth = (width, fnName) => {
|
|
155
|
+
if (!Number.isInteger(width) || width < 0) {
|
|
156
|
+
throw new RangeError(`${fnName} width must be a non-negative integer`);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
if (!converter_1.IS_BROWSER) {
|
|
160
|
+
converter = (0, converter_1.loadNative)();
|
|
161
|
+
}
|
|
162
|
+
if (converter === undefined) {
|
|
163
|
+
converter = {
|
|
164
|
+
toBigInt: (buf, bigEndian = true) => bigEndian ? bufferToBigIntBE(buf) : bufferToBigIntLE(buf),
|
|
165
|
+
fromBigInt: (num, buf, bigEndian = true) => bigEndian
|
|
166
|
+
? writeBigIntToBufferBE(num, buf)
|
|
167
|
+
: writeBigIntToBufferLE(num, buf),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function toBigIntLE(buf) {
|
|
171
|
+
if (converter === undefined)
|
|
172
|
+
return 0n;
|
|
173
|
+
return converter.toBigInt(toBufferInput(buf), false);
|
|
174
|
+
}
|
|
175
|
+
function validateBigIntBuffer() {
|
|
176
|
+
try {
|
|
177
|
+
const test = toBigIntLE(Buffer.from([0x01, 0x00]));
|
|
178
|
+
return test === BigInt(1);
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function toBigIntBE(buf) {
|
|
185
|
+
if (converter === undefined)
|
|
186
|
+
return 0n;
|
|
187
|
+
return converter.toBigInt(toBufferInput(buf), true);
|
|
188
|
+
}
|
|
189
|
+
function toBufferLE(num, width) {
|
|
190
|
+
assertWidth(width, "toBufferLE");
|
|
191
|
+
if (converter === undefined)
|
|
192
|
+
return Buffer.alloc(0);
|
|
193
|
+
return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
|
|
194
|
+
}
|
|
195
|
+
function toBufferBE(num, width) {
|
|
196
|
+
assertWidth(width, "toBufferBE");
|
|
197
|
+
if (converter === undefined)
|
|
198
|
+
return Buffer.alloc(0);
|
|
199
|
+
return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
|
|
200
|
+
}
|
|
201
|
+
function parseHex(a, prefix0x = false, byteLength) {
|
|
202
|
+
const hexMatch = a.match(/^(0x)?([\da-fA-F]+)$/);
|
|
203
|
+
if (hexMatch == null) {
|
|
204
|
+
throw new RangeError("input must be a hexadecimal string, e.g. '0x124fe3a' or '0214f1b2'");
|
|
205
|
+
}
|
|
206
|
+
let hex = hexMatch[2];
|
|
207
|
+
if (byteLength !== undefined) {
|
|
208
|
+
if (byteLength < hex.length / 2) {
|
|
209
|
+
throw new RangeError(`expected byte length ${byteLength} < input hex byte length ${Math.ceil(hex.length / 2)}`);
|
|
210
|
+
}
|
|
211
|
+
hex = hex.padStart(byteLength * 2, "0");
|
|
212
|
+
}
|
|
213
|
+
return prefix0x ? "0x" + hex : hex;
|
|
214
|
+
}
|
|
215
|
+
function bigintToBuf(a, returnArrayBuffer = false) {
|
|
216
|
+
if (a < 0) {
|
|
217
|
+
throw RangeError("a should be a non-negative integer. Negative values are not supported");
|
|
218
|
+
}
|
|
219
|
+
return hexToBuf(bigintToHex(a), returnArrayBuffer);
|
|
220
|
+
}
|
|
221
|
+
function bufToBigint(buf) {
|
|
222
|
+
let bits = 8n;
|
|
223
|
+
if (ArrayBuffer.isView(buf)) {
|
|
224
|
+
bits = BigInt(buf.BYTES_PER_ELEMENT * 8);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
buf = new Uint8Array(buf);
|
|
228
|
+
}
|
|
229
|
+
let ret = 0n;
|
|
230
|
+
for (const i of buf.values()) {
|
|
231
|
+
const bi = BigInt(i);
|
|
232
|
+
ret = (ret << bits) + bi;
|
|
233
|
+
}
|
|
234
|
+
return ret;
|
|
235
|
+
}
|
|
236
|
+
function bigintToHex(a, prefix0x = false, byteLength) {
|
|
237
|
+
if (a < 0) {
|
|
238
|
+
throw RangeError("a should be a non-negative integer. Negative values are not supported");
|
|
239
|
+
}
|
|
240
|
+
return parseHex(a.toString(16), prefix0x, byteLength);
|
|
241
|
+
}
|
|
242
|
+
function hexToBigint(hexStr) {
|
|
243
|
+
return BigInt(parseHex(hexStr, true));
|
|
244
|
+
}
|
|
245
|
+
function bigintToText(a) {
|
|
246
|
+
if (a < 0) {
|
|
247
|
+
throw RangeError("a should be a non-negative integer. Negative values are not supported");
|
|
248
|
+
}
|
|
249
|
+
return bufToText(hexToBuf(a.toString(16)));
|
|
250
|
+
}
|
|
251
|
+
function textToBigint(text) {
|
|
252
|
+
return hexToBigint(bufToHex(textToBuf(text)));
|
|
253
|
+
}
|
|
254
|
+
function toBuffer(input) {
|
|
255
|
+
if (Buffer.isBuffer(input)) {
|
|
256
|
+
return input;
|
|
257
|
+
}
|
|
258
|
+
if (ArrayBuffer.isView(input)) {
|
|
259
|
+
return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
|
|
260
|
+
}
|
|
261
|
+
if (input instanceof ArrayBuffer) {
|
|
262
|
+
return Buffer.from(new Uint8Array(input));
|
|
263
|
+
}
|
|
264
|
+
throw new TypeError("Unsupported input type for Buffer.from");
|
|
265
|
+
}
|
|
266
|
+
function bufToText(buf) {
|
|
267
|
+
const input = toBuffer(buf);
|
|
268
|
+
if (converter_1.IS_BROWSER) {
|
|
269
|
+
return new TextDecoder().decode(new Uint8Array(input));
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
return Buffer.from(input).toString();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function textToBuf(str, returnArrayBuffer = false) {
|
|
276
|
+
if (!converter_1.IS_BROWSER && !returnArrayBuffer) {
|
|
277
|
+
return Buffer.from(new TextEncoder().encode(str).buffer);
|
|
278
|
+
}
|
|
279
|
+
return new TextEncoder().encode(str).buffer;
|
|
280
|
+
}
|
|
281
|
+
function bufToHex(buf, prefix0x = false, byteLength) {
|
|
282
|
+
if (converter_1.IS_BROWSER) {
|
|
283
|
+
let s = "";
|
|
284
|
+
const h = "0123456789abcdef";
|
|
285
|
+
if (ArrayBuffer.isView(buf)) {
|
|
286
|
+
buf = new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength));
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
buf = new Uint8Array(buf);
|
|
290
|
+
}
|
|
291
|
+
buf.forEach((v) => {
|
|
292
|
+
s += h[v >> 4] + h[v & 15];
|
|
293
|
+
});
|
|
294
|
+
return parseHex(s, prefix0x, byteLength);
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
const input = toBuffer(buf);
|
|
298
|
+
if (ArrayBuffer.isView(input)) {
|
|
299
|
+
buf = new Uint8Array(input.buffer.slice(input.byteOffset, input.byteOffset + input.byteLength));
|
|
300
|
+
}
|
|
301
|
+
return parseHex(Buffer.from(toBuffer(buf)).toString("hex"), prefix0x, byteLength);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
function hexToBuf(hexStr, returnArrayBuffer = false) {
|
|
305
|
+
let hex = parseHex(hexStr);
|
|
306
|
+
hex = parseHex(hexStr, false, Math.ceil(hex.length / 2));
|
|
307
|
+
if (converter_1.IS_BROWSER) {
|
|
308
|
+
return Uint8Array.from(hex.match(/[\da-fA-F]{2}/g).map((h) => {
|
|
309
|
+
return Number("0x" + h);
|
|
310
|
+
})).buffer;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
const b = Buffer.from(hex, "hex");
|
|
314
|
+
return returnArrayBuffer
|
|
315
|
+
? b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength)
|
|
316
|
+
: b;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function bigintToBase64(a, urlsafe = false, padding = true) {
|
|
320
|
+
if (a < 0n) {
|
|
321
|
+
throw new RangeError("negative bigint");
|
|
322
|
+
}
|
|
323
|
+
const buf = bigintToBuf(a);
|
|
324
|
+
let base64 = Buffer.isBuffer(buf)
|
|
325
|
+
? buf.toString("base64")
|
|
326
|
+
: Buffer.from(buf).toString("base64");
|
|
327
|
+
if (urlsafe) {
|
|
328
|
+
base64 = base64.replace(/\+/g, "-").replace(/\//g, "_");
|
|
329
|
+
}
|
|
330
|
+
if (!padding) {
|
|
331
|
+
base64 = base64.replace(/=+$/, "");
|
|
332
|
+
}
|
|
333
|
+
return base64;
|
|
334
|
+
}
|
|
335
|
+
function base64ToBigint(a) {
|
|
336
|
+
if (!a || a.trim() === "") {
|
|
337
|
+
return 0n;
|
|
338
|
+
}
|
|
339
|
+
const cleaned = a.trim();
|
|
340
|
+
if (!/^[A-Za-z0-9+/=_-]*$/.test(cleaned)) {
|
|
341
|
+
throw new RangeError("invalid base64");
|
|
342
|
+
}
|
|
343
|
+
let base64 = cleaned.replace(/-/g, "+").replace(/_/g, "/");
|
|
344
|
+
while (base64.length % 4 !== 0) {
|
|
345
|
+
base64 += "=";
|
|
346
|
+
}
|
|
347
|
+
const buf = Buffer.from(base64, "base64");
|
|
348
|
+
return bufToBigint(buf);
|
|
349
|
+
}
|
|
350
|
+
exports.FIXED_POINT_DECIMALS = 9;
|
|
351
|
+
exports.FIXED_POINT_PATTERN = /^-?0x[0-9a-f]+$/i;
|
|
352
|
+
exports.ZERO_FIXED_POINT = "0x0";
|
|
353
|
+
const normalizeHex = (value) => value.startsWith("0x") || value.startsWith("0X") ? value : `0x${value}`;
|
|
354
|
+
const toHexString = (value) => {
|
|
355
|
+
if (value === 0n) {
|
|
356
|
+
return exports.ZERO_FIXED_POINT;
|
|
357
|
+
}
|
|
358
|
+
const isNegative = value < 0n;
|
|
359
|
+
const absValue = isNegative ? -value : value;
|
|
360
|
+
const hexValue = bigintToHex(absValue);
|
|
361
|
+
return `${isNegative ? "-" : ""}0x${hexValue}`;
|
|
362
|
+
};
|
|
363
|
+
exports.toHexString = toHexString;
|
|
364
|
+
const toBigIntValue = (value) => {
|
|
365
|
+
if (!value) {
|
|
366
|
+
return 0n;
|
|
367
|
+
}
|
|
368
|
+
const trimmed = value.trim();
|
|
369
|
+
if (trimmed.length === 0) {
|
|
370
|
+
return 0n;
|
|
371
|
+
}
|
|
372
|
+
const isNegative = trimmed.startsWith("-");
|
|
373
|
+
const body = isNegative ? trimmed.slice(1) : trimmed;
|
|
374
|
+
const normalized = normalizeHex(body);
|
|
375
|
+
const bigValue = hexToBigint(normalized);
|
|
376
|
+
return isNegative ? -bigValue : bigValue;
|
|
377
|
+
};
|
|
378
|
+
exports.toBigIntValue = toBigIntValue;
|
|
379
|
+
function toFixedPoint(value, decimals = exports.FIXED_POINT_DECIMALS) {
|
|
380
|
+
if (!Number.isFinite(value)) {
|
|
381
|
+
return exports.ZERO_FIXED_POINT;
|
|
382
|
+
}
|
|
383
|
+
const scale = 10n ** BigInt(decimals);
|
|
384
|
+
const scaled = BigInt(Math.round(value * Number(scale)));
|
|
385
|
+
return (0, exports.toHexString)(scaled);
|
|
386
|
+
}
|
|
387
|
+
function fromFixedPoint(value, decimals = 9) {
|
|
388
|
+
if (!value)
|
|
389
|
+
return 0;
|
|
390
|
+
const trimmed = value.trim();
|
|
391
|
+
if (trimmed.length === 0)
|
|
392
|
+
return 0;
|
|
393
|
+
const isNegative = trimmed.startsWith('-');
|
|
394
|
+
const body = isNegative ? trimmed.slice(1) : trimmed;
|
|
395
|
+
const bigValue = isNegative ? -BigInt(body) : BigInt(body);
|
|
396
|
+
const scale = 10 ** decimals;
|
|
397
|
+
return Number(bigValue) / scale;
|
|
398
|
+
}
|
|
399
|
+
function addFixedPoint(a, b) {
|
|
400
|
+
return (0, exports.toHexString)((0, exports.toBigIntValue)(a) + (0, exports.toBigIntValue)(b));
|
|
401
|
+
}
|
|
402
|
+
function subtractFixedPoint(a, b) {
|
|
403
|
+
return (0, exports.toHexString)((0, exports.toBigIntValue)(a) - (0, exports.toBigIntValue)(b));
|
|
404
|
+
}
|
|
405
|
+
function averageFixedPoint(values) {
|
|
406
|
+
if (values.length === 0) {
|
|
407
|
+
return exports.ZERO_FIXED_POINT;
|
|
408
|
+
}
|
|
409
|
+
const sum = values.reduce((acc, value) => acc + (0, exports.toBigIntValue)(value), 0n);
|
|
410
|
+
return (0, exports.toHexString)(sum / BigInt(values.length));
|
|
411
|
+
}
|
|
412
|
+
function compareFixedPoint(a, b) {
|
|
413
|
+
const diff = (0, exports.toBigIntValue)(a) - (0, exports.toBigIntValue)(b);
|
|
414
|
+
if (diff === 0n) {
|
|
415
|
+
return 0;
|
|
416
|
+
}
|
|
417
|
+
return diff > 0n ? 1 : -1;
|
|
418
|
+
}
|
|
419
|
+
function fixedPointToBigInt(value) {
|
|
420
|
+
return (0, exports.toBigIntValue)(value);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
}));
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type PathModule = typeof import("path");
|
|
2
|
+
export type FsModule = typeof import("fs");
|
|
3
|
+
export type BindingsLoader = ((name: string) => unknown) & ((options: {
|
|
4
|
+
bindings: string;
|
|
5
|
+
module_root?: string;
|
|
6
|
+
}) => unknown);
|
|
7
|
+
export interface ConverterInterface {
|
|
8
|
+
toBigInt: (buf: Buffer, bigEndian?: boolean) => bigint;
|
|
9
|
+
fromBigInt: (num: bigint, buf: Buffer, bigEndian?: boolean) => Buffer;
|
|
10
|
+
}
|
|
11
|
+
export declare let isNative: boolean;
|
|
12
|
+
export declare const IS_BROWSER: boolean;
|
|
13
|
+
export declare const findModuleRoot: () => string | undefined;
|
|
14
|
+
export declare function loadNative(): ConverterInterface | undefined;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
type ByteInput = Buffer | Uint8Array | ArrayBuffer;
|
|
2
|
+
export declare function toBigIntLE(buf: ByteInput): bigint;
|
|
3
|
+
export declare function validateBigIntBuffer(): boolean;
|
|
4
|
+
export declare function toBigIntBE(buf: ByteInput): bigint;
|
|
5
|
+
export declare function toBufferLE(num: bigint, width: number): Buffer;
|
|
6
|
+
export declare function toBufferBE(num: bigint, width: number): Buffer;
|
|
7
|
+
export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
|
|
8
|
+
export declare function parseHex(a: string, prefix0x?: boolean, byteLength?: number): string;
|
|
9
|
+
export declare function bigintToBuf(a: bigint, returnArrayBuffer?: boolean): ArrayBuffer | Buffer;
|
|
10
|
+
export declare function bufToBigint(buf: ArrayBuffer | TypedArray | Buffer): bigint;
|
|
11
|
+
export declare function bigintToHex(a: bigint, prefix0x?: boolean, byteLength?: number): string;
|
|
12
|
+
export declare function hexToBigint(hexStr: string): bigint;
|
|
13
|
+
export declare function bigintToText(a: bigint): string;
|
|
14
|
+
export declare function textToBigint(text: string): bigint;
|
|
15
|
+
export declare function bufToText(buf: ArrayBuffer | TypedArray | Buffer): string;
|
|
16
|
+
export declare function textToBuf(str: string, returnArrayBuffer?: boolean): ArrayBuffer | Buffer;
|
|
17
|
+
export declare function bufToHex(buf: ArrayBuffer | TypedArray | Buffer, prefix0x?: boolean, byteLength?: number): string;
|
|
18
|
+
export declare function hexToBuf(hexStr: string, returnArrayBuffer?: boolean): ArrayBuffer | Buffer;
|
|
19
|
+
export declare function bigintToBase64(a: bigint, urlsafe?: boolean, padding?: boolean): string;
|
|
20
|
+
export declare function base64ToBigint(a: string): bigint;
|
|
21
|
+
export declare const FIXED_POINT_DECIMALS = 9;
|
|
22
|
+
export declare const FIXED_POINT_PATTERN: RegExp;
|
|
23
|
+
export declare const ZERO_FIXED_POINT: string;
|
|
24
|
+
export declare const toHexString: (value: bigint) => string;
|
|
25
|
+
export declare const toBigIntValue: (value?: string) => bigint;
|
|
26
|
+
export declare function toFixedPoint(value: number, decimals?: number): string;
|
|
27
|
+
export declare function fromFixedPoint(value?: string, decimals?: number): number;
|
|
28
|
+
export declare function addFixedPoint(a: string, b: string): string;
|
|
29
|
+
export declare function subtractFixedPoint(a: string, b: string): string;
|
|
30
|
+
export declare function averageFixedPoint(values: string[]): string;
|
|
31
|
+
export declare function compareFixedPoint(a: string, b: string): number;
|
|
32
|
+
export declare function fixedPointToBigInt(value?: string): bigint;
|
|
33
|
+
export {};
|