@gsknnft/bigint-buffer 1.2.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.
@@ -0,0 +1,287 @@
1
+ import 'mocha';
2
+ declare var process: {browser: boolean;};
3
+
4
+ import * as chai from 'chai';
5
+ import * as path from 'path';
6
+
7
+ const lib = process.browser ? require('../../dist/browser') :
8
+ require(path.join(__dirname, '../dist/node'));
9
+ const toBigIntBE = lib.toBigIntBE;
10
+ const toBigIntLE = lib.toBigIntLE;
11
+ const toBufferBE = lib.toBufferBE;
12
+ const toBufferLE = lib.toBufferLE;
13
+
14
+ // Needed for should.not.be.undefined.
15
+ /* tslint:disable:no-unused-expression */
16
+
17
+ chai.should();
18
+
19
+ const assertEquals = (n0: bigint, n1: bigint) => {
20
+ chai.expect(n0.toString(16)).to.equal(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
+ it('0xbadc0ffee0 should equal 0xbadc0ffee0n', async () => {
43
+ assertEquals(
44
+ toBigIntLE(Buffer.from([0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
45
+ BigInt(`0xbadc0ffee0`));
46
+ });
47
+
48
+ it('0xbadc0ffee0dd should equal 0xbadc0ffee0ddn', async () => {
49
+ assertEquals(
50
+ toBigIntLE(Buffer.from([0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
51
+ BigInt(`0xbadc0ffee0dd`));
52
+ });
53
+
54
+ it('0xbadc0ffee0ddf0 should equal 0xbadc0ffee0ddf0n', async () => {
55
+ assertEquals(
56
+ toBigIntLE(Buffer.from([0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
57
+ BigInt(`0xbadc0ffee0ddf0`));
58
+ });
59
+
60
+ it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn', async () => {
61
+ assertEquals(
62
+ toBigIntLE(
63
+ Buffer.from([0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba])),
64
+ BigInt(`0xbadc0ffee0ddf00d`));
65
+ });
66
+
67
+ it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
68
+ async () => {
69
+ assertEquals(
70
+ toBigIntLE(Buffer.from([
71
+ 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc,
72
+ 0xba
73
+ ])),
74
+ BigInt(`0xbadc0ffee0ddf00ddeadbeef`));
75
+ });
76
+ it('0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefn',
77
+ async () => {
78
+ assertEquals(
79
+ toBigIntLE(Buffer.from([
80
+ 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0,
81
+ 0xfe, 0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde,
82
+ 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
83
+ ])),
84
+ BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`));
85
+ });
86
+ it('0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbeef should equal 0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbeefn',
87
+ async () => {
88
+ assertEquals(
89
+ toBigIntLE(Buffer.from([
90
+ 0xef, 0xbe, 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd,
91
+ 0xe0, 0xfe, 0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde,
92
+ 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
93
+ ])),
94
+ BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbeef`));
95
+ });
96
+ });
97
+
98
+ describe('Try buffer conversion (big endian)', () => {
99
+ it('0 should equal 0n', () => {
100
+ assertEquals(toBigIntBE(Buffer.from([0])), BigInt(`0`));
101
+ });
102
+
103
+ it('1 should equal 1n', async () => {
104
+ assertEquals(toBigIntBE(Buffer.from([1])), BigInt(`1`));
105
+ });
106
+
107
+ it('0xdead should equal 0xdeadn', async () => {
108
+ assertEquals(toBigIntBE(Buffer.from([0xde, 0xad])), BigInt(`0xdead`));
109
+ });
110
+
111
+ it('0xdeadbeef should equal 0xdeadbeefn', async () => {
112
+ assertEquals(
113
+ toBigIntBE(Buffer.from([0xde, 0xad, 0xbe, 0xef])),
114
+ BigInt(`0xdeadbeef`));
115
+ });
116
+
117
+ it('0xbadc0ffee0 should equal 0xbadc0ffee0n', async () => {
118
+ assertEquals(
119
+ toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0])),
120
+ BigInt(`0xbadc0ffee0`));
121
+ });
122
+
123
+ it('0xbadc0ffee0dd should equal 0xbadc0ffee0ddn', async () => {
124
+ assertEquals(
125
+ toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd])),
126
+ BigInt(`0xbadc0ffee0dd`));
127
+ });
128
+
129
+ it('0xbadc0ffee0ddf0 should equal 0xbadc0ffee0ddf0n', async () => {
130
+ assertEquals(
131
+ toBigIntBE(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0])),
132
+ BigInt(`0xbadc0ffee0ddf0`));
133
+ });
134
+
135
+ it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn', async () => {
136
+ assertEquals(
137
+ toBigIntBE(
138
+ Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d])),
139
+ BigInt(`0xbadc0ffee0ddf00d`));
140
+ });
141
+
142
+ it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
143
+ async () => {
144
+ assertEquals(
145
+ toBigIntBE(Buffer.from([
146
+ 0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe,
147
+ 0xef
148
+ ])),
149
+ BigInt(`0xbadc0ffee0ddf00ddeadbeef`));
150
+ });
151
+
152
+ it('long value should equal long val', async () => {
153
+ assertEquals(
154
+ toBigIntBE(Buffer.from(
155
+ 'badc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef',
156
+ 'hex')),
157
+ BigInt(
158
+ `0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`));
159
+ });
160
+
161
+ it('other long value should equal long val', async () => {
162
+ assertEquals(
163
+ toBigIntBE(Buffer.from(
164
+ 'd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544',
165
+ 'hex')),
166
+ BigInt(
167
+ `0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544`));
168
+ });
169
+ });
170
+
171
+ describe('Try bigint conversion (little endian)', () => {
172
+ it('0 should equal 0n', () => {
173
+ toBufferLE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
174
+ 0, 0, 0, 0, 0, 0, 0, 0
175
+ ]));
176
+ });
177
+
178
+ it('1 should equal 1n', async () => {
179
+ toBufferLE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
180
+ 1, 0, 0, 0, 0, 0, 0, 0
181
+ ]));
182
+ });
183
+
184
+
185
+ it('1 should equal 1n (32 byte)', async () => {
186
+ toBufferLE(BigInt(`1`), 32)
187
+ .should.deep.equal(Buffer.from(
188
+ '0100000000000000000000000000000000000000000000000000000000000000',
189
+ 'hex'));
190
+ });
191
+
192
+ it('0xdead should equal 0xdeadn (6 byte)', async () => {
193
+ toBufferLE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
194
+ 0xad, 0xde, 0, 0, 0, 0
195
+ ]));
196
+ });
197
+
198
+ it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
199
+ toBufferLE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
200
+ 0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0
201
+ ]));
202
+ });
203
+
204
+ it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
205
+ async () => {
206
+ toBufferLE(BigInt(`0xbadc0ffee0ddf00d`), 8)
207
+ .should.deep.equal(
208
+ Buffer.from([0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba]));
209
+ });
210
+
211
+ it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
212
+ async () => {
213
+ toBufferLE(BigInt(`0xbadc0ffee0ddf00ddeadbeef`), 12)
214
+ .should.deep.equal(Buffer.from([
215
+ 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc,
216
+ 0xba
217
+ ]));
218
+ });
219
+
220
+ it('long value should equal long val', async () => {
221
+ toBufferLE(BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`), 24)
222
+ .should.deep.equal(Buffer.from([
223
+ 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0,
224
+ 0xfe, 0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde,
225
+ 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
226
+ ]));
227
+ });
228
+ });
229
+
230
+
231
+ describe('Try bigint conversion (big endian)', () => {
232
+ it('0 should equal 0n', () => {
233
+ toBufferBE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
234
+ 0, 0, 0, 0, 0, 0, 0, 0
235
+ ]));
236
+ });
237
+
238
+ it('1 should equal 1n', async () => {
239
+ toBufferBE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
240
+ 0, 0, 0, 0, 0, 0, 0, 1
241
+ ]));
242
+ });
243
+
244
+ it('1 should equal 1n (32 byte)', async () => {
245
+ toBufferBE(BigInt(`1`), 32)
246
+ .should.deep.equal(Buffer.from(
247
+ '0000000000000000000000000000000000000000000000000000000000000001',
248
+ 'hex'));
249
+ });
250
+
251
+ it('0xdead should equal 0xdeadn (6 byte)', async () => {
252
+ toBufferBE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
253
+ 0, 0, 0, 0, 0xde, 0xad
254
+ ]));
255
+ });
256
+
257
+ it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
258
+ toBufferBE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
259
+ 0, 0, 0, 0, 0, 0xde, 0xad, 0xbe, 0xef
260
+ ]));
261
+ });
262
+
263
+ it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
264
+ async () => {
265
+ toBufferBE(BigInt(`0xbadc0ffee0ddf00d`), 8)
266
+ .should.deep.equal(
267
+ Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d]));
268
+ });
269
+
270
+ it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
271
+ async () => {
272
+ toBufferBE(BigInt(`0xbadc0ffee0ddf00ddeadbeef`), 12)
273
+ .should.deep.equal(Buffer.from([
274
+ 0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe,
275
+ 0xef
276
+ ]));
277
+ });
278
+
279
+ it('long value should equal long val', async () => {
280
+ toBufferBE(BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`), 24)
281
+ .should.deep.equal(Buffer.from([
282
+ 0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d,
283
+ 0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe,
284
+ 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef
285
+ ]));
286
+ });
287
+ });
package/src/index.ts ADDED
@@ -0,0 +1,95 @@
1
+
2
+ interface ConverterInterface {
3
+ toBigInt(buf: Buffer, bigEndian?: boolean): bigint;
4
+ fromBigInt(num: BigInt, buf: Buffer, bigEndian?: boolean): Buffer;
5
+ }
6
+
7
+ declare var process: {browser: boolean;};
8
+
9
+ let converter: ConverterInterface;
10
+ export let isNative = false;
11
+
12
+ if (!process.browser) {
13
+ try {
14
+ converter = require('bindings')('bigint_buffer');
15
+ isNative = !process.browser && converter !== undefined;
16
+ } catch (e) {
17
+ console.warn(
18
+ 'bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)');
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Convert a little-endian buffer into a BigInt.
24
+ * @param buf The little-endian buffer to convert
25
+ * @returns A BigInt with the little-endian representation of buf.
26
+ */
27
+ export function toBigIntLE(buf: Buffer): bigint {
28
+ if (process.browser || converter === undefined) {
29
+ const reversed = Buffer.from(buf);
30
+ reversed.reverse();
31
+ const hex = reversed.toString('hex');
32
+ if (hex.length === 0) {
33
+ return BigInt(0);
34
+ }
35
+ return BigInt(`0x${hex}`);
36
+ }
37
+ return converter.toBigInt(buf, false);
38
+ }
39
+
40
+ export function validateBigIntBuffer(): boolean {
41
+ try {
42
+ const test = toBigIntLE(Buffer.from([0x01, 0x00]));
43
+ return test === BigInt(1);
44
+ } catch {
45
+ return false;
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Convert a big-endian buffer into a BigInt
51
+ * @param buf The big-endian buffer to convert.
52
+ * @returns A BigInt with the big-endian representation of buf.
53
+ */
54
+ export function toBigIntBE(buf: Buffer): bigint {
55
+ if (process.browser || converter === undefined) {
56
+ const hex = buf.toString('hex');
57
+ if (hex.length === 0) {
58
+ return BigInt(0);
59
+ }
60
+ return BigInt(`0x${hex}`);
61
+ }
62
+ return converter.toBigInt(buf, true);
63
+ }
64
+
65
+ /**
66
+ * Convert a BigInt to a little-endian buffer.
67
+ * @param num The BigInt to convert.
68
+ * @param width The number of bytes that the resulting buffer should be.
69
+ * @returns A little-endian buffer representation of num.
70
+ */
71
+ export function toBufferLE(num: bigint, width: number): Buffer {
72
+ if (process.browser || converter === undefined) {
73
+ const hex = num.toString(16);
74
+ const buffer =
75
+ Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
76
+ buffer.reverse();
77
+ return buffer;
78
+ }
79
+ // Allocation is done here, since it is slower using napi in C
80
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
81
+ }
82
+
83
+ /**
84
+ * Convert a BigInt to a big-endian buffer.
85
+ * @param num The BigInt to convert.
86
+ * @param width The number of bytes that the resulting buffer should be.
87
+ * @returns A big-endian buffer representation of num.
88
+ */
89
+ export function toBufferBE(num: bigint, width: number): Buffer {
90
+ if (process.browser || converter === undefined) {
91
+ const hex = num.toString(16);
92
+ return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
93
+ }
94
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
95
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "extends": "./node_modules/gts/tsconfig-google.json",
3
+ "compilerOptions": {
4
+ "composite": true,
5
+ "rootDir": ".",
6
+ "outDir": "build",
7
+ "target" : "esnext",
8
+ "lib" : [ "esnext" ],
9
+ "sourceMap": true
10
+ },
11
+ "include": [
12
+ "src/*.ts",
13
+ "src/**/*.ts",
14
+ "test/*.ts",
15
+ "test/**/*.ts"
16
+ ],
17
+ "exclude": [
18
+ "node_modules"
19
+ ]
20
+ }