@feelyourprotocol/util 8141.0.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/LICENSE +373 -0
- package/README.md +297 -0
- package/dist/cjs/account.d.ts +165 -0
- package/dist/cjs/account.d.ts.map +1 -0
- package/dist/cjs/account.js +530 -0
- package/dist/cjs/account.js.map +1 -0
- package/dist/cjs/address.d.ts +67 -0
- package/dist/cjs/address.d.ts.map +1 -0
- package/dist/cjs/address.js +136 -0
- package/dist/cjs/address.js.map +1 -0
- package/dist/cjs/authorization.d.ts +41 -0
- package/dist/cjs/authorization.d.ts.map +1 -0
- package/dist/cjs/authorization.js +135 -0
- package/dist/cjs/authorization.js.map +1 -0
- package/dist/cjs/bal.d.ts +129 -0
- package/dist/cjs/bal.d.ts.map +1 -0
- package/dist/cjs/bal.js +529 -0
- package/dist/cjs/bal.js.map +1 -0
- package/dist/cjs/binaryTree.d.ts +148 -0
- package/dist/cjs/binaryTree.d.ts.map +1 -0
- package/dist/cjs/binaryTree.js +240 -0
- package/dist/cjs/binaryTree.js.map +1 -0
- package/dist/cjs/blobs.d.ts +76 -0
- package/dist/cjs/blobs.d.ts.map +1 -0
- package/dist/cjs/blobs.js +175 -0
- package/dist/cjs/blobs.js.map +1 -0
- package/dist/cjs/bytes.d.ts +291 -0
- package/dist/cjs/bytes.d.ts.map +1 -0
- package/dist/cjs/bytes.js +606 -0
- package/dist/cjs/bytes.js.map +1 -0
- package/dist/cjs/constants.d.ts +91 -0
- package/dist/cjs/constants.d.ts.map +1 -0
- package/dist/cjs/constants.js +97 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/db.d.ts +65 -0
- package/dist/cjs/db.d.ts.map +1 -0
- package/dist/cjs/db.js +14 -0
- package/dist/cjs/db.js.map +1 -0
- package/dist/cjs/env.d.ts +9 -0
- package/dist/cjs/env.d.ts.map +1 -0
- package/dist/cjs/env.js +13 -0
- package/dist/cjs/env.js.map +1 -0
- package/dist/cjs/errors.d.ts +3 -0
- package/dist/cjs/errors.d.ts.map +1 -0
- package/dist/cjs/errors.js +19 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/helpers.d.ts +21 -0
- package/dist/cjs/helpers.d.ts.map +1 -0
- package/dist/cjs/helpers.js +50 -0
- package/dist/cjs/helpers.js.map +1 -0
- package/dist/cjs/index.d.ts +67 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +93 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/internal.d.ts +72 -0
- package/dist/cjs/internal.d.ts.map +1 -0
- package/dist/cjs/internal.js +182 -0
- package/dist/cjs/internal.js.map +1 -0
- package/dist/cjs/kzg.d.ts +14 -0
- package/dist/cjs/kzg.d.ts.map +1 -0
- package/dist/cjs/kzg.js +3 -0
- package/dist/cjs/kzg.js.map +1 -0
- package/dist/cjs/lock.d.ts +15 -0
- package/dist/cjs/lock.d.ts.map +1 -0
- package/dist/cjs/lock.js +45 -0
- package/dist/cjs/lock.js.map +1 -0
- package/dist/cjs/mapDB.d.ts +17 -0
- package/dist/cjs/mapDB.d.ts.map +1 -0
- package/dist/cjs/mapDB.js +46 -0
- package/dist/cjs/mapDB.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/provider.d.ts +46 -0
- package/dist/cjs/provider.d.ts.map +1 -0
- package/dist/cjs/provider.js +84 -0
- package/dist/cjs/provider.js.map +1 -0
- package/dist/cjs/request.d.ts +20 -0
- package/dist/cjs/request.d.ts.map +1 -0
- package/dist/cjs/request.js +35 -0
- package/dist/cjs/request.js.map +1 -0
- package/dist/cjs/signature.d.ts +47 -0
- package/dist/cjs/signature.d.ts.map +1 -0
- package/dist/cjs/signature.js +147 -0
- package/dist/cjs/signature.js.map +1 -0
- package/dist/cjs/tasks.d.ts +32 -0
- package/dist/cjs/tasks.d.ts.map +1 -0
- package/dist/cjs/tasks.js +51 -0
- package/dist/cjs/tasks.js.map +1 -0
- package/dist/cjs/types.d.ts +64 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +78 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/units.d.ts +22 -0
- package/dist/cjs/units.d.ts.map +1 -0
- package/dist/cjs/units.js +51 -0
- package/dist/cjs/units.js.map +1 -0
- package/dist/cjs/withdrawal.d.ts +72 -0
- package/dist/cjs/withdrawal.d.ts.map +1 -0
- package/dist/cjs/withdrawal.js +93 -0
- package/dist/cjs/withdrawal.js.map +1 -0
- package/dist/esm/account.d.ts +165 -0
- package/dist/esm/account.d.ts.map +1 -0
- package/dist/esm/account.js +505 -0
- package/dist/esm/account.js.map +1 -0
- package/dist/esm/address.d.ts +67 -0
- package/dist/esm/address.d.ts.map +1 -0
- package/dist/esm/address.js +125 -0
- package/dist/esm/address.js.map +1 -0
- package/dist/esm/authorization.d.ts +41 -0
- package/dist/esm/authorization.d.ts.map +1 -0
- package/dist/esm/authorization.js +126 -0
- package/dist/esm/authorization.js.map +1 -0
- package/dist/esm/bal.d.ts +129 -0
- package/dist/esm/bal.d.ts.map +1 -0
- package/dist/esm/bal.js +522 -0
- package/dist/esm/bal.js.map +1 -0
- package/dist/esm/binaryTree.d.ts +148 -0
- package/dist/esm/binaryTree.d.ts.map +1 -0
- package/dist/esm/binaryTree.js +226 -0
- package/dist/esm/binaryTree.js.map +1 -0
- package/dist/esm/blobs.d.ts +76 -0
- package/dist/esm/blobs.d.ts.map +1 -0
- package/dist/esm/blobs.js +163 -0
- package/dist/esm/blobs.js.map +1 -0
- package/dist/esm/bytes.d.ts +291 -0
- package/dist/esm/bytes.d.ts.map +1 -0
- package/dist/esm/bytes.js +562 -0
- package/dist/esm/bytes.js.map +1 -0
- package/dist/esm/constants.d.ts +91 -0
- package/dist/esm/constants.d.ts.map +1 -0
- package/dist/esm/constants.js +94 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/db.d.ts +65 -0
- package/dist/esm/db.d.ts.map +1 -0
- package/dist/esm/db.js +11 -0
- package/dist/esm/db.js.map +1 -0
- package/dist/esm/env.d.ts +9 -0
- package/dist/esm/env.d.ts.map +1 -0
- package/dist/esm/env.js +9 -0
- package/dist/esm/env.js.map +1 -0
- package/dist/esm/errors.d.ts +3 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +14 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/helpers.d.ts +21 -0
- package/dist/esm/helpers.d.ts.map +1 -0
- package/dist/esm/helpers.js +43 -0
- package/dist/esm/helpers.js.map +1 -0
- package/dist/esm/index.d.ts +67 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +67 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal.d.ts +72 -0
- package/dist/esm/internal.d.ts.map +1 -0
- package/dist/esm/internal.js +170 -0
- package/dist/esm/internal.js.map +1 -0
- package/dist/esm/kzg.d.ts +14 -0
- package/dist/esm/kzg.d.ts.map +1 -0
- package/dist/esm/kzg.js +2 -0
- package/dist/esm/kzg.js.map +1 -0
- package/dist/esm/lock.d.ts +15 -0
- package/dist/esm/lock.d.ts.map +1 -0
- package/dist/esm/lock.js +41 -0
- package/dist/esm/lock.js.map +1 -0
- package/dist/esm/mapDB.d.ts +17 -0
- package/dist/esm/mapDB.d.ts.map +1 -0
- package/dist/esm/mapDB.js +42 -0
- package/dist/esm/mapDB.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/provider.d.ts +46 -0
- package/dist/esm/provider.d.ts.map +1 -0
- package/dist/esm/provider.js +79 -0
- package/dist/esm/provider.js.map +1 -0
- package/dist/esm/request.d.ts +20 -0
- package/dist/esm/request.d.ts.map +1 -0
- package/dist/esm/request.js +30 -0
- package/dist/esm/request.js.map +1 -0
- package/dist/esm/signature.d.ts +47 -0
- package/dist/esm/signature.d.ts.map +1 -0
- package/dist/esm/signature.js +137 -0
- package/dist/esm/signature.js.map +1 -0
- package/dist/esm/tasks.d.ts +32 -0
- package/dist/esm/tasks.d.ts.map +1 -0
- package/dist/esm/tasks.js +47 -0
- package/dist/esm/tasks.js.map +1 -0
- package/dist/esm/types.d.ts +64 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +71 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/units.d.ts +22 -0
- package/dist/esm/units.d.ts.map +1 -0
- package/dist/esm/units.js +46 -0
- package/dist/esm/units.js.map +1 -0
- package/dist/esm/withdrawal.d.ts +72 -0
- package/dist/esm/withdrawal.d.ts.map +1 -0
- package/dist/esm/withdrawal.js +86 -0
- package/dist/esm/withdrawal.js.map +1 -0
- package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.prod.esm.tsbuildinfo +1 -0
- package/package.json +116 -0
- package/src/account.ts +630 -0
- package/src/address.ts +158 -0
- package/src/authorization.ts +180 -0
- package/src/bal.ts +761 -0
- package/src/binaryTree.ts +353 -0
- package/src/blobs.ts +209 -0
- package/src/bytes.ts +659 -0
- package/src/constants.ts +125 -0
- package/src/db.ts +86 -0
- package/src/env.ts +9 -0
- package/src/errors.ts +28 -0
- package/src/helpers.ts +46 -0
- package/src/index.ts +88 -0
- package/src/internal.ts +212 -0
- package/src/kzg.ts +24 -0
- package/src/lock.ts +42 -0
- package/src/mapDB.ts +57 -0
- package/src/provider.ts +109 -0
- package/src/request.ts +48 -0
- package/src/signature.ts +202 -0
- package/src/tasks.ts +59 -0
- package/src/types.ts +177 -0
- package/src/units.ts +56 -0
- package/src/withdrawal.ts +133 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { secp256k1 } from '@noble/curves/secp256k1.js';
|
|
2
|
+
import { keccak_256 } from '@noble/hashes/sha3.js';
|
|
3
|
+
import { bigIntToBytes, bytesToBigInt, bytesToHex, bytesToInt, concatBytes, hexToBytes, setLengthLeft, utf8ToBytes, } from "./bytes.js";
|
|
4
|
+
import { BIGINT_0, BIGINT_1, BIGINT_2, BIGINT_27, SECP256K1_ORDER, SECP256K1_ORDER_DIV_2, } from "./constants.js";
|
|
5
|
+
import { EthereumJSErrorWithoutCode } from "./errors.js";
|
|
6
|
+
import { assertIsBytes } from "./helpers.js";
|
|
7
|
+
export function calculateSigRecovery(v, chainId) {
|
|
8
|
+
if (v === BIGINT_0 || v === BIGINT_1)
|
|
9
|
+
return v;
|
|
10
|
+
if (chainId === undefined) {
|
|
11
|
+
return v - BIGINT_27;
|
|
12
|
+
}
|
|
13
|
+
return v - (chainId * BIGINT_2 + BigInt(35));
|
|
14
|
+
}
|
|
15
|
+
function isValidSigRecovery(recovery) {
|
|
16
|
+
return recovery === BIGINT_0 || recovery === BIGINT_1;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* ECDSA public key recovery from signature.
|
|
20
|
+
* NOTE: Accepts `v === 0 | v === 1` for EIP1559 transactions
|
|
21
|
+
* @returns Recovered public key
|
|
22
|
+
*/
|
|
23
|
+
export const ecrecover = function (msgHash, v, r, s, chainId) {
|
|
24
|
+
const signature = concatBytes(setLengthLeft(r, 32), setLengthLeft(s, 32));
|
|
25
|
+
const recovery = calculateSigRecovery(v, chainId);
|
|
26
|
+
if (!isValidSigRecovery(recovery)) {
|
|
27
|
+
throw EthereumJSErrorWithoutCode('Invalid signature v value');
|
|
28
|
+
}
|
|
29
|
+
const sig = secp256k1.Signature.fromBytes(signature).addRecoveryBit(Number(recovery));
|
|
30
|
+
const senderPubKey = sig.recoverPublicKey(msgHash);
|
|
31
|
+
return senderPubKey.toBytes(false).slice(1);
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Convert signature parameters into the format of `eth_sign` RPC method.
|
|
35
|
+
* NOTE: Accepts `v === 0 | v === 1` for EIP1559 transactions
|
|
36
|
+
* @returns Signature
|
|
37
|
+
*/
|
|
38
|
+
export const toRPCSig = function (v, r, s, chainId) {
|
|
39
|
+
const recovery = calculateSigRecovery(v, chainId);
|
|
40
|
+
if (!isValidSigRecovery(recovery)) {
|
|
41
|
+
throw EthereumJSErrorWithoutCode('Invalid signature v value');
|
|
42
|
+
}
|
|
43
|
+
// geth (and the RPC eth_sign method) uses the 65 byte format used by Bitcoin
|
|
44
|
+
return bytesToHex(concatBytes(setLengthLeft(r, 32), setLengthLeft(s, 32), bigIntToBytes(v)));
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Convert signature parameters into the format of Compact Signature Representation (EIP-2098).
|
|
48
|
+
* NOTE: Accepts `v === 0 | v === 1` for EIP1559 transactions
|
|
49
|
+
* @returns Signature
|
|
50
|
+
*/
|
|
51
|
+
export const toCompactSig = function (v, r, s, chainId) {
|
|
52
|
+
const recovery = calculateSigRecovery(v, chainId);
|
|
53
|
+
if (!isValidSigRecovery(recovery)) {
|
|
54
|
+
throw EthereumJSErrorWithoutCode('Invalid signature v value');
|
|
55
|
+
}
|
|
56
|
+
const ss = Uint8Array.from([...s]);
|
|
57
|
+
if ((v > BigInt(28) && v % BIGINT_2 === BIGINT_1) || v === BIGINT_1 || v === BigInt(28)) {
|
|
58
|
+
ss[0] |= 0x80;
|
|
59
|
+
}
|
|
60
|
+
return bytesToHex(concatBytes(setLengthLeft(r, 32), setLengthLeft(ss, 32)));
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Convert signature format of the `eth_sign` RPC method to signature parameters
|
|
64
|
+
*
|
|
65
|
+
* NOTE: For an extracted `v` value < 27 (see Geth bug https://github.com/ethereum/go-ethereum/issues/2053)
|
|
66
|
+
* `v + 27` is returned for the `v` value
|
|
67
|
+
* NOTE: After EIP1559, `v` could be `0` or `1` but this function assumes
|
|
68
|
+
* it's a signed message (EIP-191 or EIP-712) adding `27` at the end. Remove if needed.
|
|
69
|
+
*/
|
|
70
|
+
export const fromRPCSig = function (sig) {
|
|
71
|
+
const bytes = hexToBytes(sig);
|
|
72
|
+
let r;
|
|
73
|
+
let s;
|
|
74
|
+
let v;
|
|
75
|
+
if (bytes.length >= 65) {
|
|
76
|
+
r = bytes.subarray(0, 32);
|
|
77
|
+
s = bytes.subarray(32, 64);
|
|
78
|
+
v = bytesToBigInt(bytes.subarray(64));
|
|
79
|
+
}
|
|
80
|
+
else if (bytes.length === 64) {
|
|
81
|
+
// Compact Signature Representation (https://eips.ethereum.org/EIPS/eip-2098)
|
|
82
|
+
r = bytes.subarray(0, 32);
|
|
83
|
+
s = bytes.subarray(32, 64);
|
|
84
|
+
v = BigInt(bytesToInt(bytes.subarray(32, 33)) >> 7);
|
|
85
|
+
s[0] &= 0x7f;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
throw EthereumJSErrorWithoutCode('Invalid signature length');
|
|
89
|
+
}
|
|
90
|
+
// support both versions of `eth_sign` responses
|
|
91
|
+
if (v < 27) {
|
|
92
|
+
// TODO: verify this behavior, and verify in which context this method (`fromRPCSig`) is used
|
|
93
|
+
v = v + BIGINT_27;
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
v,
|
|
97
|
+
r,
|
|
98
|
+
s,
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Validate a ECDSA signature.
|
|
103
|
+
* NOTE: Accepts `v === 0 | v === 1` for EIP1559 transactions
|
|
104
|
+
* @param homesteadOrLater Indicates whether this is being used on either the homestead hardfork or a later one
|
|
105
|
+
*/
|
|
106
|
+
export const isValidSignature = function (v, r, s, homesteadOrLater = true, chainId) {
|
|
107
|
+
if (r.length !== 32 || s.length !== 32) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (!isValidSigRecovery(calculateSigRecovery(v, chainId))) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const rBigInt = bytesToBigInt(r);
|
|
114
|
+
const sBigInt = bytesToBigInt(s);
|
|
115
|
+
if (rBigInt === BIGINT_0 ||
|
|
116
|
+
rBigInt >= SECP256K1_ORDER ||
|
|
117
|
+
sBigInt === BIGINT_0 ||
|
|
118
|
+
sBigInt >= SECP256K1_ORDER) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
if (homesteadOrLater && sBigInt >= SECP256K1_ORDER_DIV_2) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
return true;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Returns the keccak-256 hash of `message`, prefixed with the header used by the `eth_sign` RPC call.
|
|
128
|
+
* The output of this function can be fed into `ecsign` to produce the same signature as the `eth_sign`
|
|
129
|
+
* call for a given `message`, or fed to `ecrecover` along with a signature to recover the public key
|
|
130
|
+
* used to produce the signature.
|
|
131
|
+
*/
|
|
132
|
+
export const hashPersonalMessage = function (message) {
|
|
133
|
+
assertIsBytes(message);
|
|
134
|
+
const prefix = utf8ToBytes(`\u0019Ethereum Signed Message:\n${message.length}`);
|
|
135
|
+
return keccak_256(concatBytes(prefix, message));
|
|
136
|
+
};
|
|
137
|
+
//# sourceMappingURL=signature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signature.js","sourceRoot":"","sources":["../../src/signature.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAElD,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,UAAU,EACV,WAAW,EACX,UAAU,EACV,aAAa,EACb,WAAW,GACZ,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,eAAe,EACf,qBAAqB,GACtB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAI5C,MAAM,UAAU,oBAAoB,CAAC,CAAS,EAAE,OAAgB;IAC9D,IAAI,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,QAAQ;QAAE,OAAO,CAAC,CAAA;IA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AoB,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC;QAC1D,OAAO,KAAK,CAAA;IACd,CAAC;IAED,MAAM,OAAO,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;IAChC,MAAM,OAAO,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;IAEhC,IACE,OAAO,KAAK,QAAQ;QACpB,OAAO,IAAI,eAAe;QAC1B,OAAO,KAAK,QAAQ;QACpB,OAAO,IAAI,eAAe,EAC1B,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,gBAAgB,IAAI,OAAO,IAAI,qBAAqB,EAAE,CAAC;QACzD,OAAO,KAAK,CAAA;IACd,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,UAAU,OAAmB;IAC9D,aAAa,CAAC,OAAO,CAAC,CAAA;IACtB,MAAM,MAAM,GAAG,WAAW,CAAC,mCAAmC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;IAC/E,OAAO,UAAU,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;AACjD,CAAC,CAAA"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare class PrioritizedTaskExecutor {
|
|
2
|
+
/** The maximum size of the pool */
|
|
3
|
+
private maxPoolSize;
|
|
4
|
+
/** The current size of the pool */
|
|
5
|
+
private currentPoolSize;
|
|
6
|
+
/** The task queue */
|
|
7
|
+
private queue;
|
|
8
|
+
/**
|
|
9
|
+
* Executes tasks up to maxPoolSize at a time, other items are put in a priority queue.
|
|
10
|
+
* @class PrioritizedTaskExecutor
|
|
11
|
+
* @private
|
|
12
|
+
* @param maxPoolSize The maximum size of the pool
|
|
13
|
+
*/
|
|
14
|
+
constructor(maxPoolSize: number);
|
|
15
|
+
/**
|
|
16
|
+
* Executes the task or queues it if no spots are available.
|
|
17
|
+
* When a task is added, check if there are spots left in the pool.
|
|
18
|
+
* If a spot is available, claim that spot and give back the spot once the asynchronous task has been resolved.
|
|
19
|
+
* When no spots are available, add the task to the task queue. The task will be executed at some point when another task has been resolved.
|
|
20
|
+
* @private
|
|
21
|
+
* @param priority The priority of the task
|
|
22
|
+
* @param fn The function that accepts the callback, which must be called upon the task completion.
|
|
23
|
+
*/
|
|
24
|
+
executeOrQueue(priority: number, fn: Function): void;
|
|
25
|
+
/**
|
|
26
|
+
* Checks if the taskExecutor is finished.
|
|
27
|
+
* @private
|
|
28
|
+
* @returns Returns `true` if the taskExecutor is finished, otherwise returns `false`.
|
|
29
|
+
*/
|
|
30
|
+
finished(): boolean;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=tasks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tasks.d.ts","sourceRoot":"","sources":["../../src/tasks.ts"],"names":[],"mappings":"AAKA,qBAAa,uBAAuB;IAClC,mCAAmC;IACnC,OAAO,CAAC,WAAW,CAAQ;IAC3B,mCAAmC;IACnC,OAAO,CAAC,eAAe,CAAQ;IAC/B,qBAAqB;IACrB,OAAO,CAAC,KAAK,CAAQ;IAErB;;;;;OAKG;gBACS,WAAW,EAAE,MAAM;IAM/B;;;;;;;;OAQG;IACH,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ;IAgB7C;;;;OAIG;IACH,QAAQ,IAAI,OAAO;CAGpB"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export class PrioritizedTaskExecutor {
|
|
2
|
+
/**
|
|
3
|
+
* Executes tasks up to maxPoolSize at a time, other items are put in a priority queue.
|
|
4
|
+
* @class PrioritizedTaskExecutor
|
|
5
|
+
* @private
|
|
6
|
+
* @param maxPoolSize The maximum size of the pool
|
|
7
|
+
*/
|
|
8
|
+
constructor(maxPoolSize) {
|
|
9
|
+
this.maxPoolSize = maxPoolSize;
|
|
10
|
+
this.currentPoolSize = 0;
|
|
11
|
+
this.queue = [];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Executes the task or queues it if no spots are available.
|
|
15
|
+
* When a task is added, check if there are spots left in the pool.
|
|
16
|
+
* If a spot is available, claim that spot and give back the spot once the asynchronous task has been resolved.
|
|
17
|
+
* When no spots are available, add the task to the task queue. The task will be executed at some point when another task has been resolved.
|
|
18
|
+
* @private
|
|
19
|
+
* @param priority The priority of the task
|
|
20
|
+
* @param fn The function that accepts the callback, which must be called upon the task completion.
|
|
21
|
+
*/
|
|
22
|
+
executeOrQueue(priority, fn) {
|
|
23
|
+
if (this.currentPoolSize < this.maxPoolSize) {
|
|
24
|
+
this.currentPoolSize++;
|
|
25
|
+
fn(() => {
|
|
26
|
+
this.currentPoolSize--;
|
|
27
|
+
if (this.queue.length > 0) {
|
|
28
|
+
this.queue.sort((a, b) => b.priority - a.priority);
|
|
29
|
+
const item = this.queue.shift();
|
|
30
|
+
this.executeOrQueue(item.priority, item.fn);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.queue.push({ priority, fn });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Checks if the taskExecutor is finished.
|
|
40
|
+
* @private
|
|
41
|
+
* @returns Returns `true` if the taskExecutor is finished, otherwise returns `false`.
|
|
42
|
+
*/
|
|
43
|
+
finished() {
|
|
44
|
+
return this.currentPoolSize === 0;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=tasks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tasks.js","sourceRoot":"","sources":["../../src/tasks.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,uBAAuB;IAQlC;;;;;OAKG;IACH,YAAY,WAAmB;QAC7B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAC9B,IAAI,CAAC,eAAe,GAAG,CAAC,CAAA;QACxB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAA;IACjB,CAAC;IAED;;;;;;;;OAQG;IACH,cAAc,CAAC,QAAgB,EAAE,EAAY;QAC3C,IAAI,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;YAC5C,IAAI,CAAC,eAAe,EAAE,CAAA;YACtB,EAAE,CAAC,GAAG,EAAE;gBACN,IAAI,CAAC,eAAe,EAAE,CAAA;gBACtB,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBAC1B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAA;oBAClD,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;oBAC/B,IAAI,CAAC,cAAc,CAAC,IAAK,CAAC,QAAQ,EAAE,IAAK,CAAC,EAAE,CAAC,CAAA;gBAC/C,CAAC;YACH,CAAC,CAAC,CAAA;QACJ,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAA;QACnC,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACN,OAAO,IAAI,CAAC,eAAe,KAAK,CAAC,CAAA;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Address } from './address.ts';
|
|
2
|
+
import type { ToBytesInputTypes } from './bytes.ts';
|
|
3
|
+
export type BigIntLike = bigint | PrefixedHexString | number | Uint8Array;
|
|
4
|
+
export type BytesLike = Uint8Array | number[] | number | bigint | TransformableToBytes | PrefixedHexString;
|
|
5
|
+
export type NumericString = `${number}`;
|
|
6
|
+
export type PrefixedHexString = `0x${string}`;
|
|
7
|
+
/**
|
|
8
|
+
* A type that represents an input that can be converted to an Address.
|
|
9
|
+
*/
|
|
10
|
+
export type AddressLike = Address | Uint8Array | PrefixedHexString;
|
|
11
|
+
export interface TransformableToBytes {
|
|
12
|
+
toBytes?(): Uint8Array;
|
|
13
|
+
}
|
|
14
|
+
export type NestedUint8Array = Array<Uint8Array | NestedUint8Array>;
|
|
15
|
+
export declare function isNestedUint8Array(value: unknown): value is NestedUint8Array;
|
|
16
|
+
export type TypeOutput = (typeof TypeOutput)[keyof typeof TypeOutput];
|
|
17
|
+
export declare const TypeOutput: {
|
|
18
|
+
readonly Number: 0;
|
|
19
|
+
readonly BigInt: 1;
|
|
20
|
+
readonly Uint8Array: 2;
|
|
21
|
+
readonly PrefixedHexString: 3;
|
|
22
|
+
};
|
|
23
|
+
export type TypeOutputReturnType = {
|
|
24
|
+
[TypeOutput.Number]: number;
|
|
25
|
+
[TypeOutput.BigInt]: bigint;
|
|
26
|
+
[TypeOutput.Uint8Array]: Uint8Array;
|
|
27
|
+
[TypeOutput.PrefixedHexString]: PrefixedHexString;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Convert an input to a specified type.
|
|
31
|
+
* Input of null/undefined returns null/undefined regardless of the output type.
|
|
32
|
+
* @param input value to convert
|
|
33
|
+
* @param outputType type to output
|
|
34
|
+
*/
|
|
35
|
+
export declare function toType<T extends TypeOutput>(input: null, outputType: T): null;
|
|
36
|
+
export declare function toType<T extends TypeOutput>(input: undefined, outputType: T): undefined;
|
|
37
|
+
export declare function toType<T extends TypeOutput>(input: ToBytesInputTypes, outputType: T): TypeOutputReturnType[T];
|
|
38
|
+
/**
|
|
39
|
+
* EIP-7702 Authorization list types
|
|
40
|
+
*/
|
|
41
|
+
export type EOACode7702AuthorizationListItemUnsigned = {
|
|
42
|
+
chainId: PrefixedHexString;
|
|
43
|
+
address: PrefixedHexString;
|
|
44
|
+
nonce: PrefixedHexString;
|
|
45
|
+
};
|
|
46
|
+
export type EOACode7702AuthorizationListItem = {
|
|
47
|
+
yParity: PrefixedHexString;
|
|
48
|
+
r: PrefixedHexString;
|
|
49
|
+
s: PrefixedHexString;
|
|
50
|
+
} & EOACode7702AuthorizationListItemUnsigned;
|
|
51
|
+
export type EOACode7702AuthorizationListBytesItem = [
|
|
52
|
+
Uint8Array,
|
|
53
|
+
Uint8Array,
|
|
54
|
+
Uint8Array,
|
|
55
|
+
Uint8Array,
|
|
56
|
+
Uint8Array,
|
|
57
|
+
Uint8Array
|
|
58
|
+
];
|
|
59
|
+
export type EOACode7702AuthorizationListBytes = EOACode7702AuthorizationListBytesItem[];
|
|
60
|
+
export type EOACode7702AuthorizationList = EOACode7702AuthorizationListItem[];
|
|
61
|
+
export type EOACode7702AuthorizationListBytesItemUnsigned = [Uint8Array, Uint8Array, Uint8Array];
|
|
62
|
+
export declare function isEOACode7702AuthorizationListBytes(input: EOACode7702AuthorizationListBytes | EOACode7702AuthorizationList): input is EOACode7702AuthorizationListBytes;
|
|
63
|
+
export declare function isEOACode7702AuthorizationList(input: EOACode7702AuthorizationListBytes | EOACode7702AuthorizationList): input is EOACode7702AuthorizationList;
|
|
64
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAKnD,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,iBAAiB,GAAG,MAAM,GAAG,UAAU,CAAA;AAKzE,MAAM,MAAM,SAAS,GACjB,UAAU,GACV,MAAM,EAAE,GACR,MAAM,GACN,MAAM,GACN,oBAAoB,GACpB,iBAAiB,CAAA;AAKrB,MAAM,MAAM,aAAa,GAAG,GAAG,MAAM,EAAE,CAAA;AAKvC,MAAM,MAAM,iBAAiB,GAAG,KAAK,MAAM,EAAE,CAAA;AAE7C;;GAEG;AACH,MAAM,MAAM,WAAW,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C7C,OAAO,EAAE,iBAAiB,CAAA;IAC1B,CAAC,EAAE,iBAAiB,CAAA;IACpB,CAAC,EAAE,iBAAiB,CAAA;CACrB,GAAG,wCAAwC,CAAA;AAG5C,MAAM,MAAM,qCAAqC,GAAG;IAClD,UAAU;IACV,UAAU;IACV,UAAU;IACV,UAAU;IACV,UAAU;IACV,UAAU;CACX,CAAA;AACD,MAAM,MAAM,iCAAiC,GAAG,qCAAqC,EAAE,CAAA;AACvF,MAAM,MAAM,4BAA4B,GAAG,gCAAgC,EAAE,CAAA;AAE7E,MAAM,MAAM,6CAA6C,GAAG,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,CAAC,CAAA;AAEhG,wBAAgB,mCAAmC,CACjD,KAAK,EAAE,iCAAiC,GAAG,4BAA4B,GACtE,KAAK,IAAI,iCAAiC,CAS5C;AAED,wBAAgB,8BAA8B,CAC5C,KAAK,EAAE,iCAAiC,GAAG,4BAA4B,GACtE,KAAK,IAAI,4BAA4B,CAEvC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { bytesToBigInt, bytesToHex, toBytes } from "./bytes.js";
|
|
2
|
+
import { EthereumJSErrorWithoutCode } from "./errors.js";
|
|
3
|
+
import { isHexString } from "./internal.js";
|
|
4
|
+
export function isNestedUint8Array(value) {
|
|
5
|
+
if (!Array.isArray(value)) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
for (const item of value) {
|
|
9
|
+
if (Array.isArray(item)) {
|
|
10
|
+
if (!isNestedUint8Array(item)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
else if (!(item instanceof Uint8Array)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
export const TypeOutput = {
|
|
21
|
+
Number: 0,
|
|
22
|
+
BigInt: 1,
|
|
23
|
+
Uint8Array: 2,
|
|
24
|
+
PrefixedHexString: 3,
|
|
25
|
+
};
|
|
26
|
+
export function toType(input, outputType) {
|
|
27
|
+
if (input === null) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
if (input === undefined) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
if (typeof input === 'string' && !isHexString(input)) {
|
|
34
|
+
throw EthereumJSErrorWithoutCode(`A string must be provided with a 0x-prefix, given: ${input}`);
|
|
35
|
+
}
|
|
36
|
+
else if (typeof input === 'number' && !Number.isSafeInteger(input)) {
|
|
37
|
+
throw EthereumJSErrorWithoutCode('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)');
|
|
38
|
+
}
|
|
39
|
+
const output = toBytes(input);
|
|
40
|
+
switch (outputType) {
|
|
41
|
+
case TypeOutput.Uint8Array:
|
|
42
|
+
return output;
|
|
43
|
+
case TypeOutput.BigInt:
|
|
44
|
+
return bytesToBigInt(output);
|
|
45
|
+
case TypeOutput.Number: {
|
|
46
|
+
const bigInt = bytesToBigInt(output);
|
|
47
|
+
if (bigInt > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
48
|
+
throw EthereumJSErrorWithoutCode('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative output type)');
|
|
49
|
+
}
|
|
50
|
+
return Number(bigInt);
|
|
51
|
+
}
|
|
52
|
+
case TypeOutput.PrefixedHexString:
|
|
53
|
+
return bytesToHex(output);
|
|
54
|
+
default:
|
|
55
|
+
throw EthereumJSErrorWithoutCode('unknown outputType');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export function isEOACode7702AuthorizationListBytes(input) {
|
|
59
|
+
if (input.length === 0) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const firstItem = input[0];
|
|
63
|
+
if (Array.isArray(firstItem)) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
export function isEOACode7702AuthorizationList(input) {
|
|
69
|
+
return !isEOACode7702AuthorizationListBytes(input); // This is exactly the same method, except the output is negated.
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAC/D,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AA0C3C,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAC/C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAA;IACd,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACxB,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC9B,OAAO,KAAK,CAAA;YACd,CAAC;QACH,CAAC;aAAM,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC,EAAE,CAAC;YACzC,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC;AAID,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,MAAM,EAAE,CAAC;IACT,MAAM,EAAE,CAAC;IACT,UAAU,EAAE,CAAC;IACb,iBAAiB,EAAE,CAAC;CACZ,CAAA;AAqBV,MAAM,UAAU,MAAM,CACpB,KAAwB,EACxB,UAAa;IAEb,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,OAAO,IAAI,CAAA;IACb,CAAC;IACD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CA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;AACd,CAAC;AAED,MAAM,UAAU,8BAA8B,CAC5C,KAAuE;IAEvE,OAAO,CAAC,mCAAmC,CAAC,KAAK,CAAC,CAAA,CAAC,iEAAiE;AACtH,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Conversion constants to wei */
|
|
2
|
+
export declare const GWEI_TO_WEI: bigint;
|
|
3
|
+
export declare const ETHER_TO_WEI: bigint;
|
|
4
|
+
export declare function formatBigDecimal(numerator: bigint, denominator: bigint, maxDecimalFactor: bigint): string;
|
|
5
|
+
export declare class Units {
|
|
6
|
+
static validateInput(amount: number | bigint): void;
|
|
7
|
+
/**
|
|
8
|
+
* Convert a number or bigint input of ether to wei
|
|
9
|
+
*
|
|
10
|
+
* @param {number | bigint} amount amount of units of ether to convert to wei
|
|
11
|
+
* @returns {bigint} amount of units in wei
|
|
12
|
+
*/
|
|
13
|
+
static ether(amount: number | bigint): bigint;
|
|
14
|
+
/**
|
|
15
|
+
* Convert a number or bigint input of gwei to wei
|
|
16
|
+
*
|
|
17
|
+
* @param amount amount of units of gwei to convert to wei
|
|
18
|
+
* @returns {bigint} amount of units in wei
|
|
19
|
+
*/
|
|
20
|
+
static gwei(amount: number | bigint): bigint;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=units.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"units.d.ts","sourceRoot":"","sources":["../../src/units.ts"],"names":[],"mappings":"AAGA,kCAAkC;AAClC,eAAO,MAAM,WAAW,QAAkB,CAAA;AAC1C,eAAO,MAAM,YAAY,QAAmB,CAAA;AAE5C,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,EACnB,gBAAgB,EAAE,MAAM,GACvB,MAAM,CAWR;AAED,qBAAa,KAAK;IAChB,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IASnD;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAK7C;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;CAI7C"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { BIGINT_0, BIGINT_1 } from "./constants.js";
|
|
2
|
+
import { EthereumJSErrorWithoutCode } from "./errors.js";
|
|
3
|
+
/** Conversion constants to wei */
|
|
4
|
+
export const GWEI_TO_WEI = BigInt(10 ** 9); // Multiplier to convert from Gwei to Wei
|
|
5
|
+
export const ETHER_TO_WEI = BigInt(10 ** 18); // Multiplier to convert from Ether to Wei
|
|
6
|
+
export function formatBigDecimal(numerator, denominator, maxDecimalFactor) {
|
|
7
|
+
if (denominator === BIGINT_0) {
|
|
8
|
+
denominator = BIGINT_1;
|
|
9
|
+
}
|
|
10
|
+
const full = numerator / denominator;
|
|
11
|
+
const fraction = ((numerator - full * denominator) * maxDecimalFactor) / denominator;
|
|
12
|
+
// zeros to be added post decimal are number of zeros in maxDecimalFactor - number of digits in fraction
|
|
13
|
+
const zerosPostDecimal = String(maxDecimalFactor).length - 1 - String(fraction).length;
|
|
14
|
+
return `${full}.${'0'.repeat(zerosPostDecimal)}${fraction}`;
|
|
15
|
+
}
|
|
16
|
+
export class Units {
|
|
17
|
+
static validateInput(amount) {
|
|
18
|
+
if (typeof amount === 'number' && !Number.isInteger(amount)) {
|
|
19
|
+
throw EthereumJSErrorWithoutCode('Input must be an integer number');
|
|
20
|
+
}
|
|
21
|
+
if (BigInt(amount) < 0) {
|
|
22
|
+
throw EthereumJSErrorWithoutCode('Input must be a positive number');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Convert a number or bigint input of ether to wei
|
|
27
|
+
*
|
|
28
|
+
* @param {number | bigint} amount amount of units of ether to convert to wei
|
|
29
|
+
* @returns {bigint} amount of units in wei
|
|
30
|
+
*/
|
|
31
|
+
static ether(amount) {
|
|
32
|
+
Units.validateInput(amount);
|
|
33
|
+
return BigInt(amount) * ETHER_TO_WEI;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Convert a number or bigint input of gwei to wei
|
|
37
|
+
*
|
|
38
|
+
* @param amount amount of units of gwei to convert to wei
|
|
39
|
+
* @returns {bigint} amount of units in wei
|
|
40
|
+
*/
|
|
41
|
+
static gwei(amount) {
|
|
42
|
+
Units.validateInput(amount);
|
|
43
|
+
return BigInt(amount) * GWEI_TO_WEI;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=units.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"units.js","sourceRoot":"","sources":["../../src/units.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AACnD,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAA;AAExD,kCAAkC;AAClC,MAAM,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC,CAAA,CAAC,yCAAyC;AACpF,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAA,CAAC,0CAA0C;AAEvF,MAAM,UAAU,gBAAgB,CAC9B,SAAiB,EACjB,WAAmB,EACnB,gBAAwB;IAExB,IAAI,WAAW,KAAK,QAAQ,EAAE,CAAC;QAC7B,WAAW,GAAG,QAAQ,CAAA;IACxB,CAAC;IAED,MAAM,IAAI,GAAG,SAAS,GAAG,WAAW,CAAA;IACpC,MAAM,QAAQ,GAAG,CAAC,CAAC,SAAS,GAAG,IAAI,GAAG,WAAW,CAAC,GAAG,gBAAgB,CAAC,GAAG,WAAW,CAAA;IAEpF,wGAAwG;IACxG,MAAM,gBAAgB,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAA;IACtF,OAAO,GAAG,IAAI,IAAI,GAAG,CAAC,MAAM,CAAC,gBAAgB,CAAC,GAAG,QAAQ,EAAE,CAAA;AAC7D,CAAC;AAED,MAAM,OAAO,KAAK;IAChB,MAAM,CAAC,aAAa,CAAC,MAAuB;QAC1C,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;YAC5D,MAAM,0BAA0B,CAAC,iCAAiC,CAAC,CAAA;QACrE,CAAC;QACD,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACvB,MAAM,0BAA0B,CAAC,iCAAiC,CAAC,CAAA;QACrE,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,MAAuB;QAClC,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QAC3B,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,YAAY,CAAA;IACtC,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAAC,MAAuB;QACjC,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QAC3B,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,WAAW,CAAA;IACrC,CAAC;CACF"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Address } from './address.ts';
|
|
2
|
+
import type { AddressLike, BigIntLike, PrefixedHexString } from './types.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Flexible input data type for EIP-4895 withdrawal data with amount in Gwei to
|
|
5
|
+
* match CL representation and for eventual ssz withdrawalsRoot
|
|
6
|
+
*/
|
|
7
|
+
export type WithdrawalData = {
|
|
8
|
+
index: BigIntLike;
|
|
9
|
+
validatorIndex: BigIntLike;
|
|
10
|
+
address: AddressLike;
|
|
11
|
+
amount: BigIntLike;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* JSON RPC interface for EIP-4895 withdrawal data with amount in Gwei to
|
|
15
|
+
* match CL representation and for eventual ssz withdrawalsRoot
|
|
16
|
+
*/
|
|
17
|
+
export interface JSONRPCWithdrawal {
|
|
18
|
+
index: PrefixedHexString;
|
|
19
|
+
validatorIndex: PrefixedHexString;
|
|
20
|
+
address: PrefixedHexString;
|
|
21
|
+
amount: PrefixedHexString;
|
|
22
|
+
}
|
|
23
|
+
export type WithdrawalBytes = [Uint8Array, Uint8Array, Uint8Array, Uint8Array];
|
|
24
|
+
/**
|
|
25
|
+
* Convert a withdrawal to a byte array
|
|
26
|
+
* @param withdrawal the withdrawal to convert
|
|
27
|
+
* @returns byte array of the withdrawal
|
|
28
|
+
*/
|
|
29
|
+
export declare function withdrawalToBytesArray(withdrawal: Withdrawal | WithdrawalData): WithdrawalBytes;
|
|
30
|
+
/**
|
|
31
|
+
* Representation of EIP-4895 withdrawal data
|
|
32
|
+
*/
|
|
33
|
+
export declare class Withdrawal {
|
|
34
|
+
readonly index: bigint;
|
|
35
|
+
readonly validatorIndex: bigint;
|
|
36
|
+
readonly address: Address;
|
|
37
|
+
readonly amount: bigint;
|
|
38
|
+
/**
|
|
39
|
+
* This constructor assigns and validates the values.
|
|
40
|
+
* Use the static factory methods to assist in creating a Withdrawal object from varying data types.
|
|
41
|
+
* Its amount is in Gwei to match CL representation and for eventual ssz withdrawalsRoot
|
|
42
|
+
*/
|
|
43
|
+
constructor(index: bigint, validatorIndex: bigint, address: Address, amount: bigint);
|
|
44
|
+
raw(): WithdrawalBytes;
|
|
45
|
+
toValue(): {
|
|
46
|
+
index: bigint;
|
|
47
|
+
validatorIndex: bigint;
|
|
48
|
+
address: Uint8Array<ArrayBufferLike>;
|
|
49
|
+
amount: bigint;
|
|
50
|
+
};
|
|
51
|
+
toJSON(): {
|
|
52
|
+
index: `0x${string}`;
|
|
53
|
+
validatorIndex: `0x${string}`;
|
|
54
|
+
address: `0x${string}`;
|
|
55
|
+
amount: `0x${string}`;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a validator withdrawal request to be submitted to the consensus layer
|
|
60
|
+
* @param withdrawalData the consensus layer index and validator index values for the
|
|
61
|
+
* validator requesting the withdrawal and the address and withdrawal amount of the request
|
|
62
|
+
* @returns a {@link Withdrawal} object
|
|
63
|
+
*/
|
|
64
|
+
export declare function createWithdrawal(withdrawalData: WithdrawalData): Withdrawal;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a validator withdrawal request to be submitted to the consensus layer from
|
|
67
|
+
* an RLP list
|
|
68
|
+
* @param withdrawalArray decoded RLP list of withdrawal data elements
|
|
69
|
+
* @returns a {@link Withdrawal} object
|
|
70
|
+
*/
|
|
71
|
+
export declare function createWithdrawalFromBytesArray(withdrawalArray: WithdrawalBytes): Withdrawal;
|
|
72
|
+
//# sourceMappingURL=withdrawal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withdrawal.d.ts","sourceRoot":"","sources":["../../src/withdrawal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAKtC,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAE5E;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,UAAU,CAAA;IACjB,cAAc,EAAE,UAAU,CAAA;IAC1B,OAAO,EAAE,WAAW,CAAA;IACpB,MAAM,EAAE,UAAU,CAAA;CACnB,CAAA;AAED;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,iBAAiB,CAAA;IACxB,cAAc,EAAE,iBAAiB,CAAA;IACjC,OAAO,EAAE,iBAAiB,CAAA;IAC1B,MAAM,EAAE,iBAAiB,CAAA;CAC1B;AAED,MAAM,MAAM,eAAe,GAAG,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,CAAC,CAAA;AAC9E;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,UAAU,GAAG,cAAc,GAAG,eAAe,CAmB/F;AACD;;GAEG;AACH,qBAAa,UAAU;IACrB,SAAgB,KAAK,EAAE,MAAM,CAAA;IAC7B,SAAgB,cAAc,EAAE,MAAM,CAAA;IACtC,SAAgB,OAAO,EAAE,OAAO,CAAA;IAChC,SAAgB,MAAM,EAAE,MAAM,CAAA;IAE9B;;;;OAIG;gBACS,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM;IAOnF,GAAG;IAIH,OAAO;;;;;;IASP,MAAM;;;;;;CAQP;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,cAAc,EAAE,cAAc,cAa9D;AAED;;;;;GAKG;AACH,wBAAgB,8BAA8B,CAAC,eAAe,EAAE,eAAe,cAM9E"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Address } from "./address.js";
|
|
2
|
+
import { bigIntToHex, bytesToHex, toBytes } from "./bytes.js";
|
|
3
|
+
import { BIGINT_0 } from "./constants.js";
|
|
4
|
+
import { TypeOutput, toType } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Convert a withdrawal to a byte array
|
|
7
|
+
* @param withdrawal the withdrawal to convert
|
|
8
|
+
* @returns byte array of the withdrawal
|
|
9
|
+
*/
|
|
10
|
+
export function withdrawalToBytesArray(withdrawal) {
|
|
11
|
+
const { index, validatorIndex, address, amount } = withdrawal;
|
|
12
|
+
const indexBytes = toType(index, TypeOutput.BigInt) === BIGINT_0
|
|
13
|
+
? new Uint8Array()
|
|
14
|
+
: toType(index, TypeOutput.Uint8Array);
|
|
15
|
+
const validatorIndexBytes = toType(validatorIndex, TypeOutput.BigInt) === BIGINT_0
|
|
16
|
+
? new Uint8Array()
|
|
17
|
+
: toType(validatorIndex, TypeOutput.Uint8Array);
|
|
18
|
+
const addressBytes = address instanceof Address ? address.bytes : toType(address, TypeOutput.Uint8Array);
|
|
19
|
+
const amountBytes = toType(amount, TypeOutput.BigInt) === BIGINT_0
|
|
20
|
+
? new Uint8Array()
|
|
21
|
+
: toType(amount, TypeOutput.Uint8Array);
|
|
22
|
+
return [indexBytes, validatorIndexBytes, addressBytes, amountBytes];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Representation of EIP-4895 withdrawal data
|
|
26
|
+
*/
|
|
27
|
+
export class Withdrawal {
|
|
28
|
+
/**
|
|
29
|
+
* This constructor assigns and validates the values.
|
|
30
|
+
* Use the static factory methods to assist in creating a Withdrawal object from varying data types.
|
|
31
|
+
* Its amount is in Gwei to match CL representation and for eventual ssz withdrawalsRoot
|
|
32
|
+
*/
|
|
33
|
+
constructor(index, validatorIndex, address, amount) {
|
|
34
|
+
this.index = index;
|
|
35
|
+
this.validatorIndex = validatorIndex;
|
|
36
|
+
this.address = address;
|
|
37
|
+
this.amount = amount;
|
|
38
|
+
}
|
|
39
|
+
raw() {
|
|
40
|
+
return withdrawalToBytesArray(this);
|
|
41
|
+
}
|
|
42
|
+
toValue() {
|
|
43
|
+
return {
|
|
44
|
+
index: this.index,
|
|
45
|
+
validatorIndex: this.validatorIndex,
|
|
46
|
+
address: this.address.bytes,
|
|
47
|
+
amount: this.amount,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
toJSON() {
|
|
51
|
+
return {
|
|
52
|
+
index: bigIntToHex(this.index),
|
|
53
|
+
validatorIndex: bigIntToHex(this.validatorIndex),
|
|
54
|
+
address: bytesToHex(this.address.bytes),
|
|
55
|
+
amount: bigIntToHex(this.amount),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates a validator withdrawal request to be submitted to the consensus layer
|
|
61
|
+
* @param withdrawalData the consensus layer index and validator index values for the
|
|
62
|
+
* validator requesting the withdrawal and the address and withdrawal amount of the request
|
|
63
|
+
* @returns a {@link Withdrawal} object
|
|
64
|
+
*/
|
|
65
|
+
export function createWithdrawal(withdrawalData) {
|
|
66
|
+
const { index: indexData, validatorIndex: validatorIndexData, address: addressData, amount: amountData, } = withdrawalData;
|
|
67
|
+
const index = toType(indexData, TypeOutput.BigInt);
|
|
68
|
+
const validatorIndex = toType(validatorIndexData, TypeOutput.BigInt);
|
|
69
|
+
const address = addressData instanceof Address ? addressData : new Address(toBytes(addressData));
|
|
70
|
+
const amount = toType(amountData, TypeOutput.BigInt);
|
|
71
|
+
return new Withdrawal(index, validatorIndex, address, amount);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Creates a validator withdrawal request to be submitted to the consensus layer from
|
|
75
|
+
* an RLP list
|
|
76
|
+
* @param withdrawalArray decoded RLP list of withdrawal data elements
|
|
77
|
+
* @returns a {@link Withdrawal} object
|
|
78
|
+
*/
|
|
79
|
+
export function createWithdrawalFromBytesArray(withdrawalArray) {
|
|
80
|
+
if (withdrawalArray.length !== 4) {
|
|
81
|
+
throw Error(`Invalid withdrawalArray length expected=4 actual=${withdrawalArray.length}`);
|
|
82
|
+
}
|
|
83
|
+
const [index, validatorIndex, address, amount] = withdrawalArray;
|
|
84
|
+
return createWithdrawal({ index, validatorIndex, address, amount });
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=withdrawal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withdrawal.js","sourceRoot":"","sources":["../../src/withdrawal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AA2B/C;;;;GAIG;AACH,MAAM,UAAU,sBAAsB,CAAC,UAAuC;IAC5E,MA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