Skip to content

Commit bec5a1a

Browse files
committed
fix: added bigint-buffer-guard to sdk-coin-sol
Ticket: DX-1549
1 parent fbdb32f commit bec5a1a

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
try {
2+
const mod = require('bigint-buffer');
3+
const le = typeof mod.toBigIntLE === 'function' ? mod.toBigIntLE : undefined;
4+
const be = typeof mod.toBigIntBE === 'function' ? mod.toBigIntBE : undefined;
5+
6+
const isBufferLike = (b: unknown) => (typeof Buffer !== 'undefined' && Buffer.isBuffer(b)) || b instanceof Uint8Array;
7+
8+
const byteLen = (b: any) =>
9+
typeof Buffer !== 'undefined' && Buffer.isBuffer(b) ? b.length : (b as Uint8Array).byteLength;
10+
11+
const assertBuf = (b: unknown) => {
12+
if (!isBufferLike(b)) throw new TypeError('toBigInt*: input must be Buffer/Uint8Array');
13+
if (byteLen(b as any) > 1_000_000) throw new RangeError('toBigInt*: buffer too large');
14+
};
15+
16+
if (le)
17+
mod.toBigIntLE = (buf: Buffer | Uint8Array) => {
18+
assertBuf(buf);
19+
return le(buf);
20+
};
21+
if (be)
22+
mod.toBigIntBE = (buf: Buffer | Uint8Array) => {
23+
assertBuf(buf);
24+
return be(buf);
25+
};
26+
} catch {
27+
/* noop */
28+
}

modules/sdk-coin-sol/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './bigint-buffer-guard';
12
export * from './lib';
23
export * from './sol';
34
export * from './solToken';

0 commit comments

Comments
 (0)