essential-eth 0.13.0 → 1.1.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/dist/{chunk-C4VOKUKQ.js → chunk-4V2QY6OK.js} +42 -18
- package/dist/chunk-GFWRB7PT.js +96 -0
- package/dist/conversions.d.ts +5 -17
- package/dist/conversions.js +1 -1
- package/dist/{index-utils-D40THakY.d.ts → index-utils-e8a8adc8.d.ts} +2 -2
- package/dist/index.cjs +486 -282
- package/dist/index.d.ts +72 -38
- package/dist/index.js +343 -66
- package/dist/utils.d.ts +1 -2
- package/dist/utils.js +2 -2
- package/package.json +5 -6
- package/readme.md +739 -456
- package/dist/chunk-UXC5R4JE.js +0 -210
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { validateType
|
|
1
|
+
import { validateType } from './chunk-GFWRB7PT.js';
|
|
2
2
|
import { keccak_256 } from '@noble/hashes/sha3.js';
|
|
3
3
|
import { bytesToHex } from '@noble/hashes/utils.js';
|
|
4
4
|
import { Point } from '@noble/secp256k1';
|
|
@@ -26,7 +26,7 @@ function toChecksumAddress(address) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// src/logger/package-version.ts
|
|
29
|
-
var version = "
|
|
29
|
+
var version = "1.1.0";
|
|
30
30
|
|
|
31
31
|
// src/logger/logger.ts
|
|
32
32
|
var Logger = class {
|
|
@@ -116,6 +116,15 @@ function arrayify(value, options) {
|
|
|
116
116
|
}
|
|
117
117
|
return new Uint8Array(result);
|
|
118
118
|
}
|
|
119
|
+
if (typeof value === "bigint") {
|
|
120
|
+
const hex = value.toString(16).padStart(2, "0");
|
|
121
|
+
const padded = hex.length % 2 ? "0" + hex : hex;
|
|
122
|
+
const result = new Uint8Array(padded.length / 2);
|
|
123
|
+
for (let i = 0; i < result.length; i++) {
|
|
124
|
+
result[i] = parseInt(padded.substring(i * 2, i * 2 + 2), 16);
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
119
128
|
if (options.allowMissingPrefix && typeof value === "string" && value.substring(0, 2) !== "0x") {
|
|
120
129
|
value = "0x" + value;
|
|
121
130
|
}
|
|
@@ -303,10 +312,6 @@ function hexZeroPad(value, length) {
|
|
|
303
312
|
}
|
|
304
313
|
return value;
|
|
305
314
|
}
|
|
306
|
-
function computePublicKey(privKey) {
|
|
307
|
-
privKey = hexlify(privKey).slice(2);
|
|
308
|
-
return "0x" + Point.fromPrivateKey(privKey).toHex();
|
|
309
|
-
}
|
|
310
315
|
function keccak256(data) {
|
|
311
316
|
let bytes;
|
|
312
317
|
if (typeof data === "string") {
|
|
@@ -321,6 +326,15 @@ function keccak256(data) {
|
|
|
321
326
|
return "0x" + bytesToHex(keccak_256(bytes));
|
|
322
327
|
}
|
|
323
328
|
|
|
329
|
+
// src/utils/to-utf8-bytes.ts
|
|
330
|
+
function toUtf8Bytes(data) {
|
|
331
|
+
return new TextEncoder().encode(data);
|
|
332
|
+
}
|
|
333
|
+
function computePublicKey(privKey) {
|
|
334
|
+
privKey = hexlify(privKey).slice(2);
|
|
335
|
+
return "0x" + Point.fromPrivateKey(privKey).toHex();
|
|
336
|
+
}
|
|
337
|
+
|
|
324
338
|
// src/utils/compute-address.ts
|
|
325
339
|
function computeAddress(key) {
|
|
326
340
|
if (!key.startsWith("0x04") && !key.startsWith("0x03") && !key.startsWith("0x02")) {
|
|
@@ -329,11 +343,6 @@ function computeAddress(key) {
|
|
|
329
343
|
return toChecksumAddress(hexDataSlice(keccak256(hexDataSlice(key, 1)), 12));
|
|
330
344
|
}
|
|
331
345
|
|
|
332
|
-
// src/utils/to-utf8-bytes.ts
|
|
333
|
-
function toUtf8Bytes(data) {
|
|
334
|
-
return new TextEncoder().encode(data);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
346
|
// src/utils/hash-message.ts
|
|
338
347
|
var messagePrefix = "Ethereum Signed Message:\n";
|
|
339
348
|
function hashMessage(message) {
|
|
@@ -359,6 +368,13 @@ function isAddress(address) {
|
|
|
359
368
|
return false;
|
|
360
369
|
}
|
|
361
370
|
}
|
|
371
|
+
|
|
372
|
+
// src/classes/utils/hex-to-decimal.ts
|
|
373
|
+
function hexToDecimal(hex) {
|
|
374
|
+
return BigInt(hex).toString();
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// src/classes/utils/encode-decode-transaction.ts
|
|
362
378
|
var hexFalse = "0".repeat(64);
|
|
363
379
|
var hexTrue = "0".repeat(63) + "1";
|
|
364
380
|
function hexToUtf8(hex) {
|
|
@@ -370,7 +386,8 @@ function hexToUtf8(hex) {
|
|
|
370
386
|
}
|
|
371
387
|
for (; i < l; i += 2) {
|
|
372
388
|
const code = parseInt(hex.substr(i, 2), 16);
|
|
373
|
-
if (code === 0)
|
|
389
|
+
if (code === 0)
|
|
390
|
+
continue;
|
|
374
391
|
str += String.fromCharCode(code);
|
|
375
392
|
}
|
|
376
393
|
try {
|
|
@@ -484,7 +501,7 @@ function decodeRPCResponse(jsonABIArgument, nodeResponse) {
|
|
|
484
501
|
if (rawOutputs?.length === 1 && rawOutputs[0].type === "uint256[]") {
|
|
485
502
|
const outputs2 = encodedOutputs.slice(2);
|
|
486
503
|
return outputs2.map((output) => {
|
|
487
|
-
return
|
|
504
|
+
return BigInt(hexToDecimal(`0x${output}`));
|
|
488
505
|
});
|
|
489
506
|
}
|
|
490
507
|
const outputs = encodedOutputs.map((output, i) => {
|
|
@@ -494,14 +511,17 @@ function decodeRPCResponse(jsonABIArgument, nodeResponse) {
|
|
|
494
511
|
return output === hexTrue;
|
|
495
512
|
case "address":
|
|
496
513
|
return toChecksumAddress(`0x${output.slice(24)}`);
|
|
497
|
-
case "uint256":
|
|
498
|
-
case "uint120":
|
|
499
|
-
return tinyBig(hexToDecimal(`0x${output}`));
|
|
500
514
|
case "bytes32":
|
|
501
515
|
return `0x${output}`;
|
|
502
516
|
case "uint8":
|
|
503
517
|
return Number(hexToDecimal(`0x${output}`));
|
|
504
518
|
default:
|
|
519
|
+
if (outputType.startsWith("uint")) {
|
|
520
|
+
return BigInt(hexToDecimal(`0x${output}`));
|
|
521
|
+
}
|
|
522
|
+
if (outputType.startsWith("int")) {
|
|
523
|
+
return BigInt(hexToDecimal(`0x${output}`));
|
|
524
|
+
}
|
|
505
525
|
throw new Error(
|
|
506
526
|
`essential-eth does not yet support "${outputType}" outputs. Make a PR today!"`
|
|
507
527
|
);
|
|
@@ -541,7 +561,11 @@ function _pack(type, value, isArray) {
|
|
|
541
561
|
if (isArray) {
|
|
542
562
|
size = 256;
|
|
543
563
|
}
|
|
544
|
-
|
|
564
|
+
let bigVal = BigInt(value);
|
|
565
|
+
if (bigVal < 0n) {
|
|
566
|
+
bigVal = bigVal + (1n << BigInt(size));
|
|
567
|
+
}
|
|
568
|
+
value = Number(bigVal);
|
|
545
569
|
const hexValue2 = hexlify(value);
|
|
546
570
|
return zeroPad(hexValue2, size / 8);
|
|
547
571
|
}
|
|
@@ -753,4 +777,4 @@ function splitSignature(signature) {
|
|
|
753
777
|
return result;
|
|
754
778
|
}
|
|
755
779
|
|
|
756
|
-
export { arrayify, computeAddress, computePublicKey, concat, decodeRPCResponse, encodeData, hashMessage, hexConcat, hexDataLength, hexDataSlice, hexStripZeros, hexValue, hexZeroPad, hexlify, isAddress, isBytes, isBytesLike, isHexString, keccak256, logger, pack, solidityKeccak256, splitSignature, stripZeros, toChecksumAddress, toUtf8Bytes, zeroPad };
|
|
780
|
+
export { arrayify, computeAddress, computePublicKey, concat, decodeRPCResponse, encodeData, hashMessage, hexConcat, hexDataLength, hexDataSlice, hexStripZeros, hexToDecimal, hexValue, hexZeroPad, hexlify, isAddress, isBytes, isBytesLike, isHexString, keccak256, logger, pack, solidityKeccak256, splitSignature, stripZeros, toChecksumAddress, toUtf8Bytes, zeroPad };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// src/shared/validate-type.ts
|
|
2
|
+
var validateType = (value, allowedTypes) => {
|
|
3
|
+
if (!allowedTypes.includes(typeof value)) {
|
|
4
|
+
throw new Error(
|
|
5
|
+
`${allowedTypes.join(" or ")} required. Received ${typeof value}`
|
|
6
|
+
);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// src/utils/fixed-point.ts
|
|
11
|
+
function parseFixed(value, decimals) {
|
|
12
|
+
let negative = false;
|
|
13
|
+
if (value.startsWith("-")) {
|
|
14
|
+
negative = true;
|
|
15
|
+
value = value.substring(1);
|
|
16
|
+
}
|
|
17
|
+
if (value === "") {
|
|
18
|
+
throw new Error("invalid decimal value");
|
|
19
|
+
}
|
|
20
|
+
const parts = value.split(".");
|
|
21
|
+
if (parts.length > 2) {
|
|
22
|
+
throw new Error("too many decimal points");
|
|
23
|
+
}
|
|
24
|
+
const integer = parts[0] || "0";
|
|
25
|
+
let fraction = parts[1] || "";
|
|
26
|
+
if (fraction.length > decimals) {
|
|
27
|
+
const extra = fraction.substring(decimals);
|
|
28
|
+
if (extra.match(/[^0]/)) {
|
|
29
|
+
throw new Error("fractional component exceeds decimals");
|
|
30
|
+
}
|
|
31
|
+
fraction = fraction.substring(0, decimals);
|
|
32
|
+
}
|
|
33
|
+
while (fraction.length < decimals) {
|
|
34
|
+
fraction += "0";
|
|
35
|
+
}
|
|
36
|
+
const result = BigInt(integer + fraction);
|
|
37
|
+
return negative ? -result : result;
|
|
38
|
+
}
|
|
39
|
+
function formatFixed(value, decimals) {
|
|
40
|
+
let negative = "";
|
|
41
|
+
if (value < 0n) {
|
|
42
|
+
negative = "-";
|
|
43
|
+
value = -value;
|
|
44
|
+
}
|
|
45
|
+
let str = value.toString();
|
|
46
|
+
while (str.length <= decimals) {
|
|
47
|
+
str = "0" + str;
|
|
48
|
+
}
|
|
49
|
+
const integerPart = str.substring(0, str.length - decimals);
|
|
50
|
+
const fractionPart = str.substring(str.length - decimals);
|
|
51
|
+
const trimmedFraction = fractionPart.replace(/0+$/, "");
|
|
52
|
+
if (trimmedFraction) {
|
|
53
|
+
return `${negative}${integerPart}.${trimmedFraction}`;
|
|
54
|
+
}
|
|
55
|
+
return `${negative}${integerPart}`;
|
|
56
|
+
}
|
|
57
|
+
function toBigInt(value) {
|
|
58
|
+
if (typeof value === "bigint")
|
|
59
|
+
return value;
|
|
60
|
+
if (typeof value === "string") {
|
|
61
|
+
if (value.startsWith("0x")) {
|
|
62
|
+
return BigInt(value);
|
|
63
|
+
}
|
|
64
|
+
if (value.includes(".")) {
|
|
65
|
+
return BigInt(value.split(".")[0] || "0");
|
|
66
|
+
}
|
|
67
|
+
return BigInt(value);
|
|
68
|
+
}
|
|
69
|
+
return BigInt(value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/utils/ether-to-gwei.ts
|
|
73
|
+
function etherToGwei(etherQuantity) {
|
|
74
|
+
validateType(etherQuantity, ["string", "number", "bigint"]);
|
|
75
|
+
return parseFixed(String(etherQuantity), 9);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/utils/ether-to-wei.ts
|
|
79
|
+
function etherToWei(etherQuantity) {
|
|
80
|
+
validateType(etherQuantity, ["string", "number", "bigint"]);
|
|
81
|
+
return parseFixed(String(etherQuantity), 18);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/utils/gwei-to-ether.ts
|
|
85
|
+
function gweiToEther(gweiQuantity) {
|
|
86
|
+
validateType(gweiQuantity, ["string", "number", "bigint"]);
|
|
87
|
+
return formatFixed(toBigInt(gweiQuantity), 9);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/utils/wei-to-ether.ts
|
|
91
|
+
function weiToEther(weiQuantity) {
|
|
92
|
+
validateType(weiQuantity, ["string", "number", "bigint"]);
|
|
93
|
+
return formatFixed(toBigInt(weiQuantity), 18);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { etherToGwei, etherToWei, formatFixed, gweiToEther, parseFixed, toBigInt, validateType, weiToEther };
|
package/dist/conversions.d.ts
CHANGED
|
@@ -1,21 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
declare function etherToGwei(etherQuantity: string | number | bigint): bigint;
|
|
2
2
|
|
|
3
|
-
declare
|
|
4
|
-
constructor(value: string | number | TinyBig | Big);
|
|
5
|
-
toHexString(): string;
|
|
6
|
-
toNumber(): number;
|
|
7
|
-
toString(): string;
|
|
8
|
-
private padAndChop;
|
|
9
|
-
toTwos(bitCount: number): Big;
|
|
10
|
-
}
|
|
11
|
-
declare function tinyBig(value: string | number | TinyBig | Big): TinyBig;
|
|
3
|
+
declare function etherToWei(etherQuantity: string | number | bigint): bigint;
|
|
12
4
|
|
|
13
|
-
declare function
|
|
5
|
+
declare function gweiToEther(gweiQuantity: string | number | bigint): string;
|
|
14
6
|
|
|
15
|
-
declare function
|
|
7
|
+
declare function weiToEther(weiQuantity: string | number | bigint): string;
|
|
16
8
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
declare function weiToEther(weiQuantity: string | number | TinyBig | Big): TinyBig;
|
|
20
|
-
|
|
21
|
-
export { TinyBig, etherToGwei, etherToWei, gweiToEther, tinyBig, weiToEther };
|
|
9
|
+
export { etherToGwei, etherToWei, gweiToEther, weiToEther };
|
package/dist/conversions.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { etherToGwei, etherToWei, gweiToEther, weiToEther } from './chunk-GFWRB7PT.js';
|
|
@@ -28,7 +28,7 @@ interface Signature {
|
|
|
28
28
|
}
|
|
29
29
|
declare function isBytesLike(value: any): value is BytesLike;
|
|
30
30
|
declare function isBytes(value: any): value is Bytes;
|
|
31
|
-
declare function arrayify(value: BytesLike | Hexable | number, options?: DataOptions): Uint8Array;
|
|
31
|
+
declare function arrayify(value: BytesLike | Hexable | number | bigint, options?: DataOptions): Uint8Array;
|
|
32
32
|
declare function concat(arrayOfBytesLike: ReadonlyArray<BytesLikeWithNumber>): Uint8Array;
|
|
33
33
|
declare function stripZeros(value: BytesLike): Uint8Array;
|
|
34
34
|
declare function zeroPad(value: BytesLike, length: number): Uint8Array;
|
|
@@ -60,4 +60,4 @@ declare function toChecksumAddress(address: string): string;
|
|
|
60
60
|
|
|
61
61
|
declare function toUtf8Bytes(data: string): Uint8Array;
|
|
62
62
|
|
|
63
|
-
export {
|
|
63
|
+
export { toUtf8Bytes as A, BytesLike as B, DataOptions as D, Hexable as H, Signature as S, arrayify as a, Bytes as b, BytesLikeWithNumber as c, concat as d, hexDataLength as e, hexDataSlice as f, hexlify as g, hexConcat as h, hexStripZeros as i, hexValue as j, hexZeroPad as k, isBytes as l, isBytesLike as m, isHexString as n, SignatureLike as o, computeAddress as p, computePublicKey as q, hashMessage as r, stripZeros as s, isAddress as t, keccak256 as u, pack as v, solidityKeccak256 as w, splitSignature as x, toChecksumAddress as y, zeroPad as z };
|