@gsknnft/bigint-buffer 1.4.1 → 1.4.2
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 +3 -3
- package/build/Release/bigint_buffer.exp +0 -0
- package/build/Release/bigint_buffer.iobj +0 -0
- package/build/Release/bigint_buffer.ipdb +0 -0
- package/build/Release/bigint_buffer.lib +0 -0
- package/build/Release/bigint_buffer.node +0 -0
- package/build/Release/bigint_buffer.pdb +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.node.recipe +11 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/CL.command.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/CL.read.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/CL.write.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/Cl.items.tlog +2 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/bigint_buffer.lastbuildstate +2 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.command.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.read.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.secondary.1.tlog +5 -0
- package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.write.1.tlog +0 -0
- package/build/Release/obj/bigint_buffer/src/bigint-buffer.obj +0 -0
- package/build/Release/obj/bigint_buffer/win_delay_load_hook.obj +0 -0
- package/build/bigint_buffer.vcxproj +148 -0
- package/build/bigint_buffer.vcxproj.filters +67 -0
- package/build/binding.sln +19 -0
- package/build/config.gypi +522 -0
- package/dist/types/conversion/src/ts/index.d.ts +168 -0
- package/dist/types/conversion/test/bigintToBase64.test.d.ts +1 -0
- package/dist/types/conversion/test/bigintToBuf.test.d.ts +1 -0
- package/dist/types/conversion/test/bigintToHex.test.d.ts +1 -0
- package/dist/types/conversion/test/bigintToText.test.d.ts +1 -0
- package/dist/types/conversion/test/bufToBigint.test.d.ts +1 -0
- package/dist/types/conversion/test/hexToBigint.test.d.ts +1 -0
- package/dist/types/conversion/test/hexToBuf.test.d.ts +1 -0
- package/dist/types/conversion/test/parseHex.test.d.ts +1 -0
- package/dist/types/conversion/test/setup.test.d.ts +1 -0
- package/dist/types/conversion/test/textToBuf.test.d.ts +1 -0
- package/package.json +42 -27
- package/src/bigint-buffer.c +203 -0
- package/src/bigint-buffer.test.ts +11 -0
- package/src/conversion/.github/workflows/build-and-test.yml +116 -0
- package/src/conversion/CODE_OF_CONDUCT.md +134 -0
- package/src/conversion/LICENSE +21 -0
- package/src/conversion/README.md +48 -0
- package/src/conversion/docs/README.md +34 -0
- package/src/conversion/docs/functions/base64ToBigint.md +27 -0
- package/src/conversion/docs/functions/bigintToBase64.md +43 -0
- package/src/conversion/docs/functions/bigintToBuf.md +35 -0
- package/src/conversion/docs/functions/bigintToHex.md +43 -0
- package/src/conversion/docs/functions/bigintToText.md +31 -0
- package/src/conversion/docs/functions/bufToBigint.md +25 -0
- package/src/conversion/docs/functions/bufToHex.md +37 -0
- package/src/conversion/docs/functions/bufToText.md +27 -0
- package/src/conversion/docs/functions/hexToBigint.md +29 -0
- package/src/conversion/docs/functions/hexToBuf.md +37 -0
- package/src/conversion/docs/functions/parseHex.md +45 -0
- package/src/conversion/docs/functions/textToBigint.md +27 -0
- package/src/conversion/docs/functions/textToBuf.md +33 -0
- package/src/conversion/docs/functions/toBigIntBE.md +27 -0
- package/src/conversion/docs/functions/toBigIntLE.md +27 -0
- package/src/conversion/docs/functions/toBufferBE.md +33 -0
- package/src/conversion/docs/functions/toBufferLE.md +33 -0
- package/src/conversion/docs/functions/validateBigIntBuffer.md +15 -0
- package/src/conversion/docs/type-aliases/TypedArray.md +11 -0
- package/src/conversion/docs/variables/isNative.md +11 -0
- package/src/conversion/example.cjs +9 -0
- package/src/conversion/example.esm.js +11 -0
- package/src/conversion/index.ts +1 -0
- package/src/conversion/package.json +163 -0
- package/src/conversion/src/docs/index.md +47 -0
- package/src/conversion/src/ts/index.ts +514 -0
- package/src/conversion/test/bigintToBase64.test.ts +37 -0
- package/src/conversion/test/bigintToBuf.test.ts +43 -0
- package/src/conversion/test/bigintToHex.test.ts +52 -0
- package/src/conversion/test/bigintToText.test.ts +30 -0
- package/src/conversion/test/bufToBigint.test.ts +20 -0
- package/src/conversion/test/hexToBigint.test.ts +22 -0
- package/src/conversion/test/hexToBuf.test.ts +39 -0
- package/src/conversion/test/parseHex.test.ts +35 -0
- package/src/conversion/test/setup.test.ts +9 -0
- package/src/conversion/test/textToBuf.test.ts +26 -0
- package/src/conversion/tsconfig.json +57 -0
- package/src/conversion/tsconfig.rollup.json +9 -0
- package/src/conversion/typedoc.json +5 -0
- package/src/conversion/types/bindings.d.t.s +4 -0
- package/src/conversion/vite.config.ts +10 -0
- package/src/conversion/vitest.config.ts +15 -0
- package/src/index.bench.ts +206 -0
- package/src/index.spec.ts +318 -0
- package/src/index.ts +215 -0
- package/.travis.yml +0 -51
- package/PR_TEMPLATE.md +0 -53
- package/WHY_BIGINT.md +0 -127
- package/benchmark.md +0 -38
- package/eslint.config.ts +0 -12
- package/karma.conf.js +0 -62
- package/pnpm-workspace.yaml +0 -14
- package/rollup.cjs.config.js +0 -13
- package/rollup.conversion.cjs.config.js +0 -13
- package/rollup.conversion.esm.config.js +0 -24
- package/rollup.esm.config.js +0 -24
- package/tsconfig.tsbuildinfo +0 -1
- package/vite.config.ts +0 -44
- package/vitest.config.ts +0 -20
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { beforeAll, describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
let lib: Awaited<typeof import("./index.js")>;
|
|
4
|
+
let toBigIntBE: Awaited<typeof import("./index.js")>["toBigIntBE"];
|
|
5
|
+
let toBigIntLE: Awaited<typeof import("./index.js")>["toBigIntLE"];
|
|
6
|
+
let toBufferBE: Awaited<typeof import("./index.js")>["toBufferBE"];
|
|
7
|
+
let toBufferLE: Awaited<typeof import("./index.js")>["toBufferLE"];
|
|
8
|
+
// Removed unused variable assignments to fix lint errors
|
|
9
|
+
|
|
10
|
+
beforeAll(async () => {
|
|
11
|
+
lib = await import("./index.js");
|
|
12
|
+
toBigIntBE = lib.toBigIntBE;
|
|
13
|
+
toBigIntLE = lib.toBigIntLE;
|
|
14
|
+
toBufferBE = lib.toBufferBE;
|
|
15
|
+
toBufferLE = lib.toBufferLE;
|
|
16
|
+
// Removed assignments to undeclared variables
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const assertEquals = (n0: bigint, n1: bigint) => {
|
|
20
|
+
expect(n0.toString(16)).toBe(n1.toString(16));
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe("Try buffer conversion (little endian)", () => {
|
|
24
|
+
it("0 should equal 0n", () => {
|
|
25
|
+
assertEquals(toBigIntLE(Buffer.from([0])), BigInt("0"));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("1 should equal 1n", async () => {
|
|
29
|
+
assertEquals(toBigIntLE(Buffer.from([1])), BigInt("1"));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("0xdead should equal 0xdeadn", async () => {
|
|
33
|
+
assertEquals(toBigIntLE(Buffer.from([0xad, 0xde])), BigInt("0xdead"));
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("0xdeadbeef should equal 0xdeadbeefn", async () => {
|
|
37
|
+
assertEquals(
|
|
38
|
+
toBigIntLE(Buffer.from([0xef, 0xbe, 0xad, 0xde])),
|
|
39
|
+
BigInt("0xdeadbeef")
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("0xbadc0ffee0 should equal 0xbadc0ffee0n", async () => {
|
|
44
|
+
assertEquals(
|
|
45
|
+
toBigIntLE(Buffer.from([0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
|
|
46
|
+
BigInt("0xbadc0ffee0")
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("0xbadc0ffee0dd should equal 0xbadc0ffee0ddn", async () => {
|
|
51
|
+
assertEquals(
|
|
52
|
+
toBigIntLE(Buffer.from([0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
|
|
53
|
+
BigInt("0xbadc0ffee0dd")
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("0xbadc0ffee0ddf0 should equal 0xbadc0ffee0ddf0n", async () => {
|
|
58
|
+
assertEquals(
|
|
59
|
+
toBigIntLE(Buffer.from([0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
|
|
60
|
+
BigInt("0xbadc0ffee0ddf0")
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn", async () => {
|
|
65
|
+
assertEquals(
|
|
66
|
+
toBigIntLE(Buffer.from([0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
|
|
67
|
+
BigInt("0xbadc0ffee0ddf00d")
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn", async () => {
|
|
72
|
+
assertEquals(
|
|
73
|
+
toBigIntLE(
|
|
74
|
+
Buffer.from([
|
|
75
|
+
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc,
|
|
76
|
+
0xba,
|
|
77
|
+
])
|
|
78
|
+
),
|
|
79
|
+
BigInt("0xbadc0ffee0ddf00ddeadbeef")
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefn", async () => {
|
|
84
|
+
assertEquals(
|
|
85
|
+
toBigIntLE(
|
|
86
|
+
Buffer.from([
|
|
87
|
+
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc,
|
|
88
|
+
0xba, 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f,
|
|
89
|
+
0xdc, 0xba,
|
|
90
|
+
])
|
|
91
|
+
),
|
|
92
|
+
BigInt("0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef")
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbeef should equal 0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbeefn", async () => {
|
|
97
|
+
assertEquals(
|
|
98
|
+
toBigIntLE(
|
|
99
|
+
Buffer.from([
|
|
100
|
+
0xef, 0xbe, 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe,
|
|
101
|
+
0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0,
|
|
102
|
+
0xfe, 0x0f, 0xdc, 0xba,
|
|
103
|
+
])
|
|
104
|
+
),
|
|
105
|
+
BigInt("0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbeef")
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe("Try buffer conversion (big endian)", () => {
|
|
111
|
+
it("0 should equal 0n", () => {
|
|
112
|
+
assertEquals(toBigIntBE(Buffer.from([0])), BigInt("0"));
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("1 should equal 1n", async () => {
|
|
116
|
+
assertEquals(toBigIntBE(Buffer.from([1])), BigInt("1"));
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("0xdead should equal 0xdeadn", async () => {
|
|
120
|
+
assertEquals(toBigIntBE(Buffer.from([0xde, 0xad])), BigInt("0xdead"));
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("0xdeadbeef should equal 0xdeadbeefn", async () => {
|
|
124
|
+
assertEquals(
|
|
125
|
+
toBigIntBE(Buffer.from([0xde, 0xad, 0xbe, 0xef])),
|
|
126
|
+
BigInt("0xdeadbeef")
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("0xbadc0ffee0 should equal 0xbadc0ffee0n", async () => {
|
|
131
|
+
assertEquals(
|
|
132
|
+
toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0])),
|
|
133
|
+
BigInt("0xbadc0ffee0")
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("0xbadc0ffee0dd should equal 0xbadc0ffee0ddn", async () => {
|
|
138
|
+
assertEquals(
|
|
139
|
+
toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd])),
|
|
140
|
+
BigInt("0xbadc0ffee0dd")
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("0xbadc0ffee0ddf0 should equal 0xbadc0ffee0ddf0n", async () => {
|
|
145
|
+
assertEquals(
|
|
146
|
+
toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0])),
|
|
147
|
+
BigInt("0xbadc0ffee0ddf0")
|
|
148
|
+
);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn", async () => {
|
|
152
|
+
assertEquals(
|
|
153
|
+
toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d])),
|
|
154
|
+
BigInt("0xbadc0ffee0ddf00d")
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn", async () => {
|
|
159
|
+
assertEquals(
|
|
160
|
+
toBigIntBE(Buffer.from("badc0ffee0ddf00ddeadbeef", "hex")),
|
|
161
|
+
BigInt("0xbadc0ffee0ddf00ddeadbeef")
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("long value should equal long val", async () => {
|
|
166
|
+
assertEquals(
|
|
167
|
+
toBigIntBE(
|
|
168
|
+
Buffer.from(
|
|
169
|
+
"badc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef",
|
|
170
|
+
"hex"
|
|
171
|
+
)
|
|
172
|
+
),
|
|
173
|
+
BigInt(
|
|
174
|
+
"0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef"
|
|
175
|
+
)
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("other long value should equal long val", async () => {
|
|
180
|
+
assertEquals(
|
|
181
|
+
toBigIntBE(
|
|
182
|
+
Buffer.from(
|
|
183
|
+
"d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544",
|
|
184
|
+
"hex"
|
|
185
|
+
)
|
|
186
|
+
),
|
|
187
|
+
BigInt(
|
|
188
|
+
"0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544"
|
|
189
|
+
)
|
|
190
|
+
);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe("Try bigint conversion (little endian)", () => {
|
|
195
|
+
it("0 should equal 0n", () => {
|
|
196
|
+
expect(toBufferLE(BigInt("0"), 8)).toEqual(
|
|
197
|
+
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0])
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("1 should equal 1n", async () => {
|
|
202
|
+
expect(toBufferLE(BigInt("1"), 8)).toEqual(
|
|
203
|
+
Buffer.from([1, 0, 0, 0, 0, 0, 0, 0])
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("1 should equal 1n (32 byte)", async () => {
|
|
208
|
+
expect(toBufferLE(BigInt("1"), 32)).toEqual(
|
|
209
|
+
Buffer.from(
|
|
210
|
+
"0100000000000000000000000000000000000000000000000000000000000000",
|
|
211
|
+
"hex"
|
|
212
|
+
)
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("0xdead should equal 0xdeadn (6 byte)", async () => {
|
|
217
|
+
expect(toBufferLE(BigInt("0xdead"), 6)).toEqual(
|
|
218
|
+
Buffer.from([0xad, 0xde, 0, 0, 0, 0])
|
|
219
|
+
);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)", async () => {
|
|
223
|
+
expect(toBufferLE(BigInt("0xdeadbeef"), 9)).toEqual(
|
|
224
|
+
Buffer.from([0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0])
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)", async () => {
|
|
229
|
+
expect(toBufferLE(BigInt("0xbadc0ffee0ddf00d"), 8)).toEqual(
|
|
230
|
+
Buffer.from([0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn", async () => {
|
|
235
|
+
expect(toBufferLE(BigInt("0xbadc0ffee0ddf00ddeadbeef"), 12)).toEqual(
|
|
236
|
+
Buffer.from([
|
|
237
|
+
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba,
|
|
238
|
+
])
|
|
239
|
+
);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("long value should equal long val", async () => {
|
|
243
|
+
expect(
|
|
244
|
+
toBufferLE(
|
|
245
|
+
BigInt("0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef"),
|
|
246
|
+
24
|
|
247
|
+
)
|
|
248
|
+
).toEqual(
|
|
249
|
+
Buffer.from([
|
|
250
|
+
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba,
|
|
251
|
+
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba,
|
|
252
|
+
])
|
|
253
|
+
);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
describe("Try bigint conversion (big endian)", () => {
|
|
258
|
+
it("0 should equal 0n", () => {
|
|
259
|
+
expect(toBufferBE(BigInt("0"), 8)).toEqual(
|
|
260
|
+
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0])
|
|
261
|
+
);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("1 should equal 1n", async () => {
|
|
265
|
+
expect(toBufferBE(BigInt("1"), 8)).toEqual(
|
|
266
|
+
Buffer.from([0, 0, 0, 0, 0, 0, 0, 1])
|
|
267
|
+
);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("1 should equal 1n (32 byte)", async () => {
|
|
271
|
+
expect(toBufferBE(BigInt("1"), 32)).toEqual(
|
|
272
|
+
Buffer.from(
|
|
273
|
+
"0000000000000000000000000000000000000000000000000000000000000001",
|
|
274
|
+
"hex"
|
|
275
|
+
)
|
|
276
|
+
);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("0xdead should equal 0xdeadn (6 byte)", async () => {
|
|
280
|
+
expect(toBufferBE(BigInt("0xdead"), 6)).toEqual(
|
|
281
|
+
Buffer.from([0, 0, 0, 0, 0xde, 0xad])
|
|
282
|
+
);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)", async () => {
|
|
286
|
+
expect(toBufferBE(BigInt("0xdeadbeef"), 9)).toEqual(
|
|
287
|
+
Buffer.from([0, 0, 0, 0, 0, 0xde, 0xad, 0xbe, 0xef])
|
|
288
|
+
);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)", async () => {
|
|
292
|
+
expect(toBufferBE(BigInt("0xbadc0ffee0ddf00d"), 8)).toEqual(
|
|
293
|
+
Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d])
|
|
294
|
+
);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn", async () => {
|
|
298
|
+
expect(toBufferBE(BigInt("0xbadc0ffee0ddf00ddeadbeef"), 12)).toEqual(
|
|
299
|
+
Buffer.from([
|
|
300
|
+
0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef,
|
|
301
|
+
])
|
|
302
|
+
);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("long value should equal long val", async () => {
|
|
306
|
+
expect(
|
|
307
|
+
toBufferBE(
|
|
308
|
+
BigInt("0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef"),
|
|
309
|
+
24
|
|
310
|
+
)
|
|
311
|
+
).toEqual(
|
|
312
|
+
Buffer.from([
|
|
313
|
+
0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef,
|
|
314
|
+
0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef,
|
|
315
|
+
])
|
|
316
|
+
);
|
|
317
|
+
});
|
|
318
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// Export robust Buffer/BigInt methods at top level
|
|
2
|
+
import * as conversionUtils from "./conversion/src/ts/index";
|
|
3
|
+
export { conversionUtils };
|
|
4
|
+
|
|
5
|
+
interface ConverterInterface {
|
|
6
|
+
toBigInt(buf: Buffer, bigEndian?: boolean): bigint;
|
|
7
|
+
fromBigInt(num: bigint, buf: Buffer, bigEndian?: boolean): Buffer;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let converter: ConverterInterface;
|
|
11
|
+
export let isNative = false;
|
|
12
|
+
|
|
13
|
+
if (typeof window === "undefined") {
|
|
14
|
+
try {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
16
|
+
converter = require("bindings")("bigint_buffer");
|
|
17
|
+
isNative = converter !== undefined;
|
|
18
|
+
} catch (e) {
|
|
19
|
+
console.warn(
|
|
20
|
+
"bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)", e
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Convert a little-endian buffer into a BigInt.
|
|
27
|
+
* @param buf The little-endian buffer to convert
|
|
28
|
+
* @returns A BigInt with the little-endian representation of buf.
|
|
29
|
+
*/
|
|
30
|
+
export function toBigIntLE(buf: Buffer): bigint {
|
|
31
|
+
if (typeof window !== "undefined" || converter === undefined) {
|
|
32
|
+
const reversed = Buffer.from(buf);
|
|
33
|
+
reversed.reverse();
|
|
34
|
+
const hex = reversed.toString("hex");
|
|
35
|
+
if (hex.length === 0) {
|
|
36
|
+
return BigInt(0);
|
|
37
|
+
}
|
|
38
|
+
return BigInt(`0x${hex}`);
|
|
39
|
+
}
|
|
40
|
+
return converter.toBigInt(buf, false);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function validateBigIntBuffer(): boolean {
|
|
44
|
+
try {
|
|
45
|
+
const test = toBigIntLE(Buffer.from([0x01, 0x00]));
|
|
46
|
+
return test === BigInt(1);
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Convert a big-endian buffer into a BigInt
|
|
54
|
+
* @param buf The big-endian buffer to convert.
|
|
55
|
+
* @returns A BigInt with the big-endian representation of buf.
|
|
56
|
+
*/
|
|
57
|
+
export function toBigIntBE(buf: Buffer): bigint {
|
|
58
|
+
if (typeof window !== "undefined" || converter === undefined) {
|
|
59
|
+
const hex = buf.toString("hex");
|
|
60
|
+
if (hex.length === 0) {
|
|
61
|
+
return BigInt(0);
|
|
62
|
+
}
|
|
63
|
+
return BigInt(`0x${hex}`);
|
|
64
|
+
}
|
|
65
|
+
return converter.toBigInt(buf, true);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Convert a BigInt to a little-endian buffer.
|
|
70
|
+
* @param num The BigInt to convert.
|
|
71
|
+
* @param width The number of bytes that the resulting buffer should be.
|
|
72
|
+
* @returns A little-endian buffer representation of num.
|
|
73
|
+
*/
|
|
74
|
+
export function toBufferLE(num: bigint, width: number): Buffer {
|
|
75
|
+
if (typeof window !== "undefined" || converter === undefined) {
|
|
76
|
+
const hex = num.toString(16);
|
|
77
|
+
const buffer = Buffer.from(
|
|
78
|
+
hex.padStart(width * 2, "0").slice(0, width * 2),
|
|
79
|
+
"hex"
|
|
80
|
+
);
|
|
81
|
+
buffer.reverse();
|
|
82
|
+
return buffer;
|
|
83
|
+
}
|
|
84
|
+
// Allocation is done here, since it is slower using napi in C
|
|
85
|
+
return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Convert a BigInt to a big-endian buffer.
|
|
90
|
+
* @param num The BigInt to convert.
|
|
91
|
+
* @param width The number of bytes that the resulting buffer should be.
|
|
92
|
+
* @returns A big-endian buffer representation of num.
|
|
93
|
+
*/
|
|
94
|
+
export function toBufferBE(num: bigint, width: number): Buffer {
|
|
95
|
+
if (typeof window !== "undefined" || converter === undefined) {
|
|
96
|
+
const hex = num.toString(16);
|
|
97
|
+
return Buffer.from(hex.padStart(width * 2, "0").slice(0, width * 2), "hex");
|
|
98
|
+
}
|
|
99
|
+
return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ========== Conversion Utilities ==========
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Convert a bigint to a Buffer with automatic sizing.
|
|
106
|
+
* Uses big-endian encoding and calculates the minimum buffer size needed.
|
|
107
|
+
* @param num The bigint to convert
|
|
108
|
+
* @returns A big-endian Buffer representation
|
|
109
|
+
*/
|
|
110
|
+
export function bigintToBuf(num: bigint): Buffer {
|
|
111
|
+
if (num < BigInt(0)) {
|
|
112
|
+
throw new Error("bigintToBuf: negative bigint values are not supported");
|
|
113
|
+
}
|
|
114
|
+
if (num === BigInt(0)) {
|
|
115
|
+
return Buffer.from([0]);
|
|
116
|
+
}
|
|
117
|
+
// Calculate the number of bytes needed
|
|
118
|
+
const hex = num.toString(16);
|
|
119
|
+
const width = Math.ceil(hex.length / 2);
|
|
120
|
+
return toBufferBE(num, width);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Convert a Buffer to a bigint.
|
|
125
|
+
* Assumes big-endian encoding.
|
|
126
|
+
* @param buf The buffer to convert
|
|
127
|
+
* @returns A bigint representation of the buffer
|
|
128
|
+
*/
|
|
129
|
+
export function bufToBigint(buf: Buffer): bigint {
|
|
130
|
+
return toBigIntBE(buf);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Convert a bigint to a hexadecimal string.
|
|
135
|
+
* @param num The bigint to convert
|
|
136
|
+
* @returns A hexadecimal string (without '0x' prefix)
|
|
137
|
+
*/
|
|
138
|
+
export function bigintToHex(num: bigint): string {
|
|
139
|
+
if (num < BigInt(0)) {
|
|
140
|
+
throw new Error("bigintToHex: negative bigint values are not supported");
|
|
141
|
+
}
|
|
142
|
+
const hex = num.toString(16);
|
|
143
|
+
// Ensure even length for proper byte representation
|
|
144
|
+
return hex.length % 2 === 0 ? hex : "0" + hex;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Convert a hexadecimal string to a bigint.
|
|
149
|
+
* @param hex The hexadecimal string (with or without '0x' prefix)
|
|
150
|
+
* @returns A bigint representation
|
|
151
|
+
*/
|
|
152
|
+
export function hexToBigint(hex: string): bigint {
|
|
153
|
+
// Remove '0x' prefix if present
|
|
154
|
+
const cleanHex = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
155
|
+
if (cleanHex.length === 0) {
|
|
156
|
+
return BigInt(0);
|
|
157
|
+
}
|
|
158
|
+
return BigInt(`0x${cleanHex}`);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Convert a bigint to a decimal text string.
|
|
163
|
+
* @param num The bigint to convert
|
|
164
|
+
* @returns A decimal string representation
|
|
165
|
+
*/
|
|
166
|
+
export function bigintToText(num: bigint): string {
|
|
167
|
+
return num.toString(10);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Convert a decimal text string to a bigint.
|
|
172
|
+
* @param text The decimal string to convert
|
|
173
|
+
* @returns A bigint representation
|
|
174
|
+
*/
|
|
175
|
+
export function textToBigint(text: string): bigint {
|
|
176
|
+
if (!text?.trim()) {
|
|
177
|
+
throw new Error("textToBigint: input string cannot be empty");
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
return BigInt(text);
|
|
181
|
+
} catch (e) {
|
|
182
|
+
throw new Error(`textToBigint: invalid decimal string "${text}" ${e instanceof Error ? e.message : String(e)}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Convert a bigint to a base64 string.
|
|
188
|
+
* @param num The bigint to convert
|
|
189
|
+
* @returns A base64 string representation
|
|
190
|
+
*/
|
|
191
|
+
export function bigintToBase64(num: bigint): string {
|
|
192
|
+
if (num < BigInt(0)) {
|
|
193
|
+
throw new Error("bigintToBase64: negative bigint values are not supported");
|
|
194
|
+
}
|
|
195
|
+
const buf = bigintToBuf(num);
|
|
196
|
+
return buf.toString("base64");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Convert a base64 string to a bigint.
|
|
201
|
+
* @param base64 The base64 string to convert
|
|
202
|
+
* @returns A bigint representation
|
|
203
|
+
*/
|
|
204
|
+
export function base64ToBigint(base64: string): bigint {
|
|
205
|
+
if (!base64?.trim()) {
|
|
206
|
+
throw new Error("base64ToBigint: input string cannot be empty");
|
|
207
|
+
}
|
|
208
|
+
// Trim whitespace and validate base64 format (allows padding)
|
|
209
|
+
const cleaned = base64.trim();
|
|
210
|
+
if (!/^[A-Za-z0-9+/]+=*$/.test(cleaned)) {
|
|
211
|
+
throw new Error("base64ToBigint: invalid base64 string format");
|
|
212
|
+
}
|
|
213
|
+
const buf = Buffer.from(cleaned, "base64");
|
|
214
|
+
return bufToBigint(buf);
|
|
215
|
+
}
|
package/.travis.yml
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
language: node_js
|
|
2
|
-
node_js:
|
|
3
|
-
- node
|
|
4
|
-
- '24'
|
|
5
|
-
sudo : false
|
|
6
|
-
env:
|
|
7
|
-
global:
|
|
8
|
-
- DISPLAY=:99.0
|
|
9
|
-
- secure: SQDlqZZTkrbRW/wYLPgEwFQpkSrs65MiF0JgO+cwSCAG1m0c7EuBTzvttAXzKK+coZXjd2+QJcAOBi6YUP84OdSf31PojIy4/SLuZxuFQa0sGey+8oSDqgMntpSthO9nom0s6u2vB61IVfcRVIUNv5L3J5i5ZIGlKGgC2MIBzsnhx0L//eyh/bLszR27ajCT7RG+pnqpk010eaDtRabOJTtO0C2kbBQoW2SvJIisDsRj5zqVyl03pZJQQAJeseiNlLokucrgitDWENE+tEJ2w1x1yl+901tgU+BIi4iLQTezcSAxnF149U/Nof7LpCba+1Y+PPh1SRpO4100CT6O46qW0O1keVfTmZL/R3ghBM6H0AtV1s48k/XCpHV9CGwxjBHoCN1kKvSO9B1BJuAdlmgobOEdze+X81nilSBhBmYMI1XPLbXqTo2ImfaFeDzveuPJH6+HMCc+8sDlhgLcrOiteAsY8RsI0ngBkutsWWv7QcRq29e4VX2eBy0j1VJZNf0FPAcKpWF8eIaPw7Z0CuWMs2hM0v0ns58jDcOL+1k4kKyJ10OOmANqNX+NZYRJJ05dd/VMrlh37teARI64PFSG6eIoCniW0GtrUcpX4KhqpaRSUDpsKvncXNdNydumMyMQjeQqyHqahvIE2ONVXXELD2T/EV6TpazDR5KSSo0=
|
|
10
|
-
- secure: ZwKAlaNZ8liLknkAfFN+TztrILi8Q06AknHUj0Zd+9PjzD5n6tpzp5g1nE0MEvF9bqkGkwf6vwWJVKcSXWM9J4a2pfoLe9wsVcjq+AkmWnZG0zV26ie3UPoa8d/PezRtxz+4vErvQcOqnESVPJAqeWCt0fH1WvnEcjaY3BYyyDbWwAPamWr/v9c4ZLKxJqJmgtczMCrRahd6Dz+HGGJ9BehhOXtse+rT5qFI6w2SMXaisDzk2twMu0fK1I66G2IsesIKErPigCeEPahsP2yu831+S18YvxJtao7tXpluMBf+RNhD9eI1evUu6IxpF419ANDJ7BpJ8uGfRVzIz9kQIAmcy/1s7g3otT633XngUfNYDisUeLJ39plclOHw+sVeLL49Xa9k+/U0CzlOThNlCj0hY2uJW3Ot9NjvKDjdR/UkgEBCl6nIDAnRQk9xSJ4ws1f9Yp5CgEXcXvu7077i6O8vG7+k08qHDSkxEEwQjB2PwjhcPfg9ieAwafblM3szYwzblL4UpROp/L/XmmzjR/cOVrS93RIpeLqDr2CCMKGd0GoOFtDse7u43SJMIAZ+O5DNtNpqIQ22V13p4P3L2C1RV3cjE55luEYVR1JZ7AzaxD+hZu2zcU2301MuSgFLTKMEu43HV1tPJ6QtCcSf+kH/LK/TPfTLveW23xkkZM0=
|
|
11
|
-
matrix:
|
|
12
|
-
- TEST_SUITE=test:node
|
|
13
|
-
addons:
|
|
14
|
-
chrome: stable
|
|
15
|
-
services:
|
|
16
|
-
- xvfb
|
|
17
|
-
cache:
|
|
18
|
-
directories:
|
|
19
|
-
- $HOME/.pnpm
|
|
20
|
-
matrix:
|
|
21
|
-
fast_finish: true
|
|
22
|
-
include:
|
|
23
|
-
- os: linux
|
|
24
|
-
node_js: '24'
|
|
25
|
-
env: TEST_SUITE=test:browser
|
|
26
|
-
- os: windows
|
|
27
|
-
node_js: '24'
|
|
28
|
-
env: TEST_SUITE=test:node
|
|
29
|
-
script: pnpm run $TEST_SUITE
|
|
30
|
-
before_deploy:
|
|
31
|
-
- pnpm pack
|
|
32
|
-
deploy:
|
|
33
|
-
- provider: releases
|
|
34
|
-
api_key: "$GITHUB_TOKEN"
|
|
35
|
-
file_glob: true
|
|
36
|
-
file: bigint-buffer-*.tgz
|
|
37
|
-
skip_cleanup: true
|
|
38
|
-
on:
|
|
39
|
-
branch: main
|
|
40
|
-
node: '10'
|
|
41
|
-
condition: "$TEST_SUITE = test:node"
|
|
42
|
-
tags: true
|
|
43
|
-
- provider: npm
|
|
44
|
-
api_key: "$NPM_TOKEN"
|
|
45
|
-
skip_cleanup: true
|
|
46
|
-
email: gsknnft@gmail.com
|
|
47
|
-
on:
|
|
48
|
-
branch: main
|
|
49
|
-
node: '24'
|
|
50
|
-
condition: "$TEST_SUITE = test:node"
|
|
51
|
-
tags: true
|
package/PR_TEMPLATE.md
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
---
|
|
3
|
-
|
|
4
|
-
## 📑 PR Template (PULL_REQUEST_TEMPLATE.md)
|
|
5
|
-
|
|
6
|
-
```markdown
|
|
7
|
-
# Pull Request for @diamondprotocol/bigint-buffer
|
|
8
|
-
|
|
9
|
-
Thank you for contributing to **@diamondprotocol/bigint-buffer**! Please fill out the details below.
|
|
10
|
-
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
## 📋 What does this PR do?
|
|
14
|
-
|
|
15
|
-
<!--
|
|
16
|
-
- Security fix? (describe vulnerability/CVE)
|
|
17
|
-
- Feature addition? (describe feature, reason, protocol impact)
|
|
18
|
-
- Bug fix? (link to test/issue)
|
|
19
|
-
- CI/test/benchmark improvements?
|
|
20
|
-
- Documentation or README changes?
|
|
21
|
-
-->
|
|
22
|
-
|
|
23
|
-
---
|
|
24
|
-
|
|
25
|
-
## 🚦 Security & Breaking Change Checklist
|
|
26
|
-
|
|
27
|
-
- [ ] I have **added or updated tests** for this change.
|
|
28
|
-
- [ ] I have **run all test suites** and verified that everything passes.
|
|
29
|
-
- [ ] If changing buffer size limits or any protocol-relevant logic:
|
|
30
|
-
- [ ] I have documented the reason and added a warning in README.
|
|
31
|
-
- [ ] I have included reference(s) to any relevant CVEs or advisories.
|
|
32
|
-
|
|
33
|
-
---
|
|
34
|
-
|
|
35
|
-
## 💬 Additional Notes
|
|
36
|
-
|
|
37
|
-
<!--
|
|
38
|
-
- Did you check compatibility with Solana, Jito, Vera, or protocol X?
|
|
39
|
-
- Is there any API break?
|
|
40
|
-
- Link to related issues, discussions, or upstream PRs if any.
|
|
41
|
-
-->
|
|
42
|
-
|
|
43
|
-
---
|
|
44
|
-
|
|
45
|
-
## 🔗 Related Links
|
|
46
|
-
|
|
47
|
-
- [bigint-buffer on npm](https://www.npmjs.com/package/bigint-buffer)
|
|
48
|
-
- [Original repo](https://github.com/no2chem/bigint-buffer)
|
|
49
|
-
- [Current security advisory](https://github.com/advisories/GHSA-3gc7-fjrx-p6mg)
|
|
50
|
-
|
|
51
|
-
---
|
|
52
|
-
|
|
53
|
-
*Thank you for helping keep supply-chain infra safe and strong!*
|