@trezoa/buffer-layout-utils 0.3.0 → 0.3.1
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/lib/cjs/base.js +9 -0
- package/lib/cjs/bigint.js +48 -0
- package/lib/cjs/decimal.js +25 -0
- package/lib/cjs/index.js +21 -0
- package/lib/cjs/native.js +20 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/web3.js +21 -0
- package/lib/esm/base.mjs +5 -0
- package/lib/esm/bigint.mjs +45 -0
- package/lib/esm/decimal.mjs +18 -0
- package/lib/esm/index.mjs +5 -0
- package/lib/esm/native.mjs +16 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/web3.mjs +17 -0
- package/lib/types/base.d.ts +6 -0
- package/lib/types/bigint.d.ts +11 -0
- package/lib/types/decimal.d.ts +4 -0
- package/lib/types/index.d.ts +5 -0
- package/lib/types/native.d.ts +2 -0
- package/lib/types/web3.d.ts +3 -0
- package/package.json +1 -1
package/lib/cjs/base.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeDecode = void 0;
|
|
4
|
+
const encodeDecode = (layout) => {
|
|
5
|
+
const decode = layout.decode.bind(layout);
|
|
6
|
+
const encode = layout.encode.bind(layout);
|
|
7
|
+
return { decode, encode };
|
|
8
|
+
};
|
|
9
|
+
exports.encodeDecode = encodeDecode;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.u256be = exports.u256 = exports.u192be = exports.u192 = exports.u128be = exports.u128 = exports.u64be = exports.u64 = exports.bigIntBE = exports.bigInt = void 0;
|
|
4
|
+
const buffer_1 = require("buffer");
|
|
5
|
+
const buffer_layout_1 = require("@trezoa/buffer-layout");
|
|
6
|
+
const bigint_buffer_1 = require("bigint-buffer");
|
|
7
|
+
const base_1 = require("./base");
|
|
8
|
+
// https://github.com/no2chem/bigint-buffer/issues/59
|
|
9
|
+
function assertValidBigInteger(untrustedInput) {
|
|
10
|
+
if (typeof untrustedInput !== 'bigint') {
|
|
11
|
+
throw new Error('Expected a `BigInt`');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function bigInt_IMPL(littleEndian, length) {
|
|
15
|
+
return (property) => {
|
|
16
|
+
const layout = (0, buffer_layout_1.blob)(length, property);
|
|
17
|
+
const { encode, decode } = (0, base_1.encodeDecode)(layout);
|
|
18
|
+
const bigIntLayout = layout;
|
|
19
|
+
bigIntLayout.decode = (buffer, offset) => {
|
|
20
|
+
const src = decode(buffer, offset);
|
|
21
|
+
return littleEndian ? (0, bigint_buffer_1.toBigIntLE)(buffer_1.Buffer.from(src)) : (0, bigint_buffer_1.toBigIntBE)(buffer_1.Buffer.from(src));
|
|
22
|
+
};
|
|
23
|
+
bigIntLayout.encode = (bigInt, buffer, offset) => {
|
|
24
|
+
assertValidBigInteger(bigInt);
|
|
25
|
+
let src;
|
|
26
|
+
if (length === 0) {
|
|
27
|
+
// https://github.com/no2chem/bigint-buffer/issues/40
|
|
28
|
+
// `toBuffer{BE|LE}` crashes when passed zero for the `length` argument.
|
|
29
|
+
src = buffer_1.Buffer.alloc(0);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
src = littleEndian ? (0, bigint_buffer_1.toBufferLE)(bigInt, length) : (0, bigint_buffer_1.toBufferBE)(bigInt, length);
|
|
33
|
+
}
|
|
34
|
+
return encode(src, buffer, offset);
|
|
35
|
+
};
|
|
36
|
+
return bigIntLayout;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
exports.bigInt = bigInt_IMPL.bind(null, /* littleEndian */ true);
|
|
40
|
+
exports.bigIntBE = bigInt_IMPL.bind(null, /* littleEndian */ false);
|
|
41
|
+
exports.u64 = (0, exports.bigInt)(8);
|
|
42
|
+
exports.u64be = (0, exports.bigIntBE)(8);
|
|
43
|
+
exports.u128 = (0, exports.bigInt)(16);
|
|
44
|
+
exports.u128be = (0, exports.bigIntBE)(16);
|
|
45
|
+
exports.u192 = (0, exports.bigInt)(24);
|
|
46
|
+
exports.u192be = (0, exports.bigIntBE)(24);
|
|
47
|
+
exports.u256 = (0, exports.bigInt)(32);
|
|
48
|
+
exports.u256be = (0, exports.bigIntBE)(32);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.decimal = exports.WAD = void 0;
|
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
8
|
+
const base_1 = require("./base");
|
|
9
|
+
const bigint_1 = require("./bigint");
|
|
10
|
+
exports.WAD = new bignumber_js_1.default('1e+18');
|
|
11
|
+
const decimal = (property) => {
|
|
12
|
+
const layout = (0, bigint_1.u128)(property);
|
|
13
|
+
const { encode, decode } = (0, base_1.encodeDecode)(layout);
|
|
14
|
+
const decimalLayout = layout;
|
|
15
|
+
decimalLayout.decode = (buffer, offset) => {
|
|
16
|
+
const src = decode(buffer, offset).toString();
|
|
17
|
+
return new bignumber_js_1.default(src).div(exports.WAD);
|
|
18
|
+
};
|
|
19
|
+
decimalLayout.encode = (decimal, buffer, offset) => {
|
|
20
|
+
const src = BigInt(decimal.times(exports.WAD).integerValue().toString());
|
|
21
|
+
return encode(src, buffer, offset);
|
|
22
|
+
};
|
|
23
|
+
return decimalLayout;
|
|
24
|
+
};
|
|
25
|
+
exports.decimal = decimal;
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./base"), exports);
|
|
18
|
+
__exportStar(require("./bigint"), exports);
|
|
19
|
+
__exportStar(require("./decimal"), exports);
|
|
20
|
+
__exportStar(require("./native"), exports);
|
|
21
|
+
__exportStar(require("./web3"), exports);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bool = void 0;
|
|
4
|
+
const buffer_layout_1 = require("@trezoa/buffer-layout");
|
|
5
|
+
const base_1 = require("./base");
|
|
6
|
+
const bool = (property) => {
|
|
7
|
+
const layout = (0, buffer_layout_1.u8)(property);
|
|
8
|
+
const { encode, decode } = (0, base_1.encodeDecode)(layout);
|
|
9
|
+
const boolLayout = layout;
|
|
10
|
+
boolLayout.decode = (buffer, offset) => {
|
|
11
|
+
const src = decode(buffer, offset);
|
|
12
|
+
return !!src;
|
|
13
|
+
};
|
|
14
|
+
boolLayout.encode = (bool, buffer, offset) => {
|
|
15
|
+
const src = Number(bool);
|
|
16
|
+
return encode(src, buffer, offset);
|
|
17
|
+
};
|
|
18
|
+
return boolLayout;
|
|
19
|
+
};
|
|
20
|
+
exports.bool = bool;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/lib/cjs/web3.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.publicKey = void 0;
|
|
4
|
+
const buffer_layout_1 = require("@trezoa/buffer-layout");
|
|
5
|
+
const web3_js_1 = require("@trezoa/web3.js");
|
|
6
|
+
const base_1 = require("./base");
|
|
7
|
+
const publicKey = (property) => {
|
|
8
|
+
const layout = (0, buffer_layout_1.blob)(32, property);
|
|
9
|
+
const { encode, decode } = (0, base_1.encodeDecode)(layout);
|
|
10
|
+
const publicKeyLayout = layout;
|
|
11
|
+
publicKeyLayout.decode = (buffer, offset) => {
|
|
12
|
+
const src = decode(buffer, offset);
|
|
13
|
+
return new web3_js_1.PublicKey(src);
|
|
14
|
+
};
|
|
15
|
+
publicKeyLayout.encode = (publicKey, buffer, offset) => {
|
|
16
|
+
const src = publicKey.toBuffer();
|
|
17
|
+
return encode(src, buffer, offset);
|
|
18
|
+
};
|
|
19
|
+
return publicKeyLayout;
|
|
20
|
+
};
|
|
21
|
+
exports.publicKey = publicKey;
|
package/lib/esm/base.mjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
import { blob } from '@trezoa/buffer-layout';
|
|
3
|
+
import { toBigIntBE, toBigIntLE, toBufferBE, toBufferLE } from 'bigint-buffer';
|
|
4
|
+
import { encodeDecode } from './base.mjs';
|
|
5
|
+
// https://github.com/no2chem/bigint-buffer/issues/59
|
|
6
|
+
function assertValidBigInteger(untrustedInput) {
|
|
7
|
+
if (typeof untrustedInput !== 'bigint') {
|
|
8
|
+
throw new Error('Expected a `BigInt`');
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function bigInt_IMPL(littleEndian, length) {
|
|
12
|
+
return (property) => {
|
|
13
|
+
const layout = blob(length, property);
|
|
14
|
+
const { encode, decode } = encodeDecode(layout);
|
|
15
|
+
const bigIntLayout = layout;
|
|
16
|
+
bigIntLayout.decode = (buffer, offset) => {
|
|
17
|
+
const src = decode(buffer, offset);
|
|
18
|
+
return littleEndian ? toBigIntLE(Buffer.from(src)) : toBigIntBE(Buffer.from(src));
|
|
19
|
+
};
|
|
20
|
+
bigIntLayout.encode = (bigInt, buffer, offset) => {
|
|
21
|
+
assertValidBigInteger(bigInt);
|
|
22
|
+
let src;
|
|
23
|
+
if (length === 0) {
|
|
24
|
+
// https://github.com/no2chem/bigint-buffer/issues/40
|
|
25
|
+
// `toBuffer{BE|LE}` crashes when passed zero for the `length` argument.
|
|
26
|
+
src = Buffer.alloc(0);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
src = littleEndian ? toBufferLE(bigInt, length) : toBufferBE(bigInt, length);
|
|
30
|
+
}
|
|
31
|
+
return encode(src, buffer, offset);
|
|
32
|
+
};
|
|
33
|
+
return bigIntLayout;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export const bigInt = /* @__PURE__ */ bigInt_IMPL.bind(null, /* littleEndian */ true);
|
|
37
|
+
export const bigIntBE = /* @__PURE__ */ bigInt_IMPL.bind(null, /* littleEndian */ false);
|
|
38
|
+
export const u64 = /* @__PURE__ */ bigInt(8);
|
|
39
|
+
export const u64be = /* @__PURE__ */ bigIntBE(8);
|
|
40
|
+
export const u128 = /* @__PURE__ */ bigInt(16);
|
|
41
|
+
export const u128be = /* @__PURE__ */ bigIntBE(16);
|
|
42
|
+
export const u192 = /* @__PURE__ */ bigInt(24);
|
|
43
|
+
export const u192be = /* @__PURE__ */ bigIntBE(24);
|
|
44
|
+
export const u256 = /* @__PURE__ */ bigInt(32);
|
|
45
|
+
export const u256be = /* @__PURE__ */ bigIntBE(32);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { encodeDecode } from './base.mjs';
|
|
3
|
+
import { u128 } from './bigint.mjs';
|
|
4
|
+
export const WAD = new BigNumber('1e+18');
|
|
5
|
+
export const decimal = (property) => {
|
|
6
|
+
const layout = u128(property);
|
|
7
|
+
const { encode, decode } = encodeDecode(layout);
|
|
8
|
+
const decimalLayout = layout;
|
|
9
|
+
decimalLayout.decode = (buffer, offset) => {
|
|
10
|
+
const src = decode(buffer, offset).toString();
|
|
11
|
+
return new BigNumber(src).div(WAD);
|
|
12
|
+
};
|
|
13
|
+
decimalLayout.encode = (decimal, buffer, offset) => {
|
|
14
|
+
const src = BigInt(decimal.times(WAD).integerValue().toString());
|
|
15
|
+
return encode(src, buffer, offset);
|
|
16
|
+
};
|
|
17
|
+
return decimalLayout;
|
|
18
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { u8 } from '@trezoa/buffer-layout';
|
|
2
|
+
import { encodeDecode } from './base.mjs';
|
|
3
|
+
export const bool = (property) => {
|
|
4
|
+
const layout = u8(property);
|
|
5
|
+
const { encode, decode } = encodeDecode(layout);
|
|
6
|
+
const boolLayout = layout;
|
|
7
|
+
boolLayout.decode = (buffer, offset) => {
|
|
8
|
+
const src = decode(buffer, offset);
|
|
9
|
+
return !!src;
|
|
10
|
+
};
|
|
11
|
+
boolLayout.encode = (bool, buffer, offset) => {
|
|
12
|
+
const src = Number(bool);
|
|
13
|
+
return encode(src, buffer, offset);
|
|
14
|
+
};
|
|
15
|
+
return boolLayout;
|
|
16
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
package/lib/esm/web3.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { blob } from '@trezoa/buffer-layout';
|
|
2
|
+
import { PublicKey } from '@trezoa/web3.js';
|
|
3
|
+
import { encodeDecode } from './base.mjs';
|
|
4
|
+
export const publicKey = (property) => {
|
|
5
|
+
const layout = blob(32, property);
|
|
6
|
+
const { encode, decode } = encodeDecode(layout);
|
|
7
|
+
const publicKeyLayout = layout;
|
|
8
|
+
publicKeyLayout.decode = (buffer, offset) => {
|
|
9
|
+
const src = decode(buffer, offset);
|
|
10
|
+
return new PublicKey(src);
|
|
11
|
+
};
|
|
12
|
+
publicKeyLayout.encode = (publicKey, buffer, offset) => {
|
|
13
|
+
const src = publicKey.toBuffer();
|
|
14
|
+
return encode(src, buffer, offset);
|
|
15
|
+
};
|
|
16
|
+
return publicKeyLayout;
|
|
17
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Layout } from '@trezoa/buffer-layout';
|
|
2
|
+
export interface EncodeDecode<T> {
|
|
3
|
+
decode(buffer: Buffer, offset?: number): T;
|
|
4
|
+
encode(src: T, buffer: Buffer, offset?: number): number;
|
|
5
|
+
}
|
|
6
|
+
export declare const encodeDecode: <T>(layout: Layout<T>) => EncodeDecode<T>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Layout } from '@trezoa/buffer-layout';
|
|
2
|
+
export declare const bigInt: (length: number) => (property?: string) => Layout<bigint>;
|
|
3
|
+
export declare const bigIntBE: (length: number) => (property?: string) => Layout<bigint>;
|
|
4
|
+
export declare const u64: (property?: string) => Layout<bigint>;
|
|
5
|
+
export declare const u64be: (property?: string) => Layout<bigint>;
|
|
6
|
+
export declare const u128: (property?: string) => Layout<bigint>;
|
|
7
|
+
export declare const u128be: (property?: string) => Layout<bigint>;
|
|
8
|
+
export declare const u192: (property?: string) => Layout<bigint>;
|
|
9
|
+
export declare const u192be: (property?: string) => Layout<bigint>;
|
|
10
|
+
export declare const u256: (property?: string) => Layout<bigint>;
|
|
11
|
+
export declare const u256be: (property?: string) => Layout<bigint>;
|
package/package.json
CHANGED