@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,148 @@
|
|
|
1
|
+
import type { Account } from './account.ts';
|
|
2
|
+
import type { Address } from './address.ts';
|
|
3
|
+
import type { PrefixedHexString } from './types.ts';
|
|
4
|
+
/**
|
|
5
|
+
* @dev Returns the 31-bytes binary tree stem for a given address and tree index.
|
|
6
|
+
* @param hashFunction The hashFunction for the binary tree
|
|
7
|
+
* @param {Address} address The address to generate the tree key for.
|
|
8
|
+
* @param treeIndex The index of the tree to generate the key for. Defaults to 0.
|
|
9
|
+
* @return The 31-bytes binary tree stem as a Uint8Array.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getBinaryTreeStem(hashFunction: (value: Uint8Array) => Uint8Array, address: Address, treeIndex?: number | bigint): Uint8Array;
|
|
12
|
+
export interface BinaryTreeStateDiff {
|
|
13
|
+
stem: PrefixedHexString;
|
|
14
|
+
suffixDiffs: {
|
|
15
|
+
currentValue: PrefixedHexString | null;
|
|
16
|
+
newValue: PrefixedHexString | null;
|
|
17
|
+
suffix: number | string;
|
|
18
|
+
}[];
|
|
19
|
+
}
|
|
20
|
+
export type BinaryTreeProof = any;
|
|
21
|
+
/**
|
|
22
|
+
* Experimental, object format could eventual change.
|
|
23
|
+
* An object that provides the state and proof necessary for binary tree stateless execution
|
|
24
|
+
* */
|
|
25
|
+
export interface BinaryTreeExecutionWitness {
|
|
26
|
+
/**
|
|
27
|
+
* The stateRoot of the parent block
|
|
28
|
+
*/
|
|
29
|
+
parentStateRoot: PrefixedHexString;
|
|
30
|
+
/**
|
|
31
|
+
* An array of state diffs.
|
|
32
|
+
* Each item corresponding to state accesses or state modifications of the block.
|
|
33
|
+
* In the current design, it also contains the resulting state of the block execution (post-state).
|
|
34
|
+
*/
|
|
35
|
+
stateDiff: BinaryTreeStateDiff[];
|
|
36
|
+
/**
|
|
37
|
+
* The proof for the block.
|
|
38
|
+
* Proves that the provided stateDiff belongs to the canonical binary tree.
|
|
39
|
+
*/
|
|
40
|
+
proof: BinaryTreeProof;
|
|
41
|
+
}
|
|
42
|
+
export type BinaryTreeLeafType = (typeof BinaryTreeLeafType)[keyof typeof BinaryTreeLeafType];
|
|
43
|
+
export declare const BinaryTreeLeafType: {
|
|
44
|
+
readonly BasicData: 0;
|
|
45
|
+
readonly CodeHash: 1;
|
|
46
|
+
};
|
|
47
|
+
export type BinaryTreeLeafBasicData = {
|
|
48
|
+
version: number;
|
|
49
|
+
nonce: bigint;
|
|
50
|
+
balance: bigint;
|
|
51
|
+
codeSize: number;
|
|
52
|
+
};
|
|
53
|
+
export declare const BINARY_TREE_VERSION_OFFSET = 0;
|
|
54
|
+
export declare const BINARY_TREE_CODE_SIZE_OFFSET = 5;
|
|
55
|
+
export declare const BINARY_TREE_NONCE_OFFSET = 8;
|
|
56
|
+
export declare const BINARY_TREE_BALANCE_OFFSET = 16;
|
|
57
|
+
export declare const BINARY_TREE_VERSION_BYTES_LENGTH = 1;
|
|
58
|
+
export declare const BINARY_TREE_CODE_SIZE_BYTES_LENGTH = 3;
|
|
59
|
+
export declare const BINARY_TREE_NONCE_BYTES_LENGTH = 8;
|
|
60
|
+
export declare const BINARY_TREE_BALANCE_BYTES_LENGTH = 16;
|
|
61
|
+
export declare const BINARY_TREE_BASIC_DATA_LEAF_KEY: Uint8Array<ArrayBufferLike>;
|
|
62
|
+
export declare const BINARY_TREE_CODE_HASH_LEAF_KEY: Uint8Array<ArrayBufferLike>;
|
|
63
|
+
export declare const BINARY_TREE_CODE_CHUNK_SIZE = 31;
|
|
64
|
+
export declare const BINARY_TREE_HEADER_STORAGE_OFFSET = 64;
|
|
65
|
+
export declare const BINARY_TREE_CODE_OFFSET = 128;
|
|
66
|
+
export declare const BINARY_TREE_NODE_WIDTH = 256;
|
|
67
|
+
export declare const BINARY_TREE_MAIN_STORAGE_OFFSET: bigint;
|
|
68
|
+
/**
|
|
69
|
+
* @dev Returns the tree key for a given binary tree stem, and sub index.
|
|
70
|
+
* @dev Assumes that the tree node width = 256
|
|
71
|
+
* @param stem The 31-bytes binary tree stem as a Uint8Array.
|
|
72
|
+
* @param subIndex The sub index of the tree to generate the key for as a Uint8Array.
|
|
73
|
+
* @return The tree key as a Uint8Array.
|
|
74
|
+
*/
|
|
75
|
+
export declare const getBinaryTreeKey: (stem: Uint8Array, leaf: BinaryTreeLeafType | Uint8Array) => Uint8Array<ArrayBuffer>;
|
|
76
|
+
/**
|
|
77
|
+
* Calculates the position of the storage key in the BinaryTree tree, determining
|
|
78
|
+
* both the tree index (the node in the tree) and the subindex (the position within the node).
|
|
79
|
+
* @param {bigint} storageKey - The key representing a specific storage slot.
|
|
80
|
+
* @returns {Object} - An object containing the tree index and subindex
|
|
81
|
+
*/
|
|
82
|
+
export declare function getBinaryTreeIndicesForStorageSlot(storageKey: bigint): {
|
|
83
|
+
treeIndex: bigint;
|
|
84
|
+
subIndex: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Calculates the position of the code chunks in the BinaryTree tree, determining
|
|
88
|
+
* both the tree index (the node in the tree) and the subindex (the position within the node).
|
|
89
|
+
* @param {bigint} chunkId - The ID representing a specific chunk.
|
|
90
|
+
* @returns {Object} - An object containing the tree index and subindex
|
|
91
|
+
*/
|
|
92
|
+
export declare function getBinaryTreeIndicesForCodeChunk(chunkId: number): {
|
|
93
|
+
treeIndex: number;
|
|
94
|
+
subIndex: number;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Asynchronously calculates the BinaryTree tree key for the specified code chunk ID.
|
|
98
|
+
* @param {Address} address - The account address to access code for.
|
|
99
|
+
* @param {number} chunkId - The ID of the code chunk to retrieve.
|
|
100
|
+
* @param hashFunction - The hash function used for BinaryTree-related operations.
|
|
101
|
+
* @returns {Uint8Array} - The BinaryTree tree key as a byte array.
|
|
102
|
+
*/
|
|
103
|
+
export declare const getBinaryTreeKeyForCodeChunk: (address: Address, chunkId: number, hashFunction: (input: Uint8Array) => Uint8Array) => Uint8Array<ArrayBuffer>;
|
|
104
|
+
export declare const chunkifyBinaryTreeCode: (code: Uint8Array) => Uint8Array<ArrayBufferLike>[];
|
|
105
|
+
/**
|
|
106
|
+
* Asynchronously calculates the BinaryTree tree key for the specified storage slot.
|
|
107
|
+
* @param {Address} address - The account address to access code for.
|
|
108
|
+
* @param {bigint} storageKey - The storage slot key to retrieve the key for.
|
|
109
|
+
* @param hashFunction - The hash function used in the Binary Tree.
|
|
110
|
+
* @returns {Uint8Array} - The BinaryTree tree key as a byte array.
|
|
111
|
+
*/
|
|
112
|
+
export declare const getBinaryTreeKeyForStorageSlot: (address: Address, storageKey: bigint, hashFunction: (input: Uint8Array) => Uint8Array) => Uint8Array<ArrayBuffer>;
|
|
113
|
+
/**
|
|
114
|
+
* This function extracts and decodes account header elements (version, nonce, code size, and balance)
|
|
115
|
+
* from an encoded `Uint8Array` representation of raw BinaryTree leaf-node basic data. Each component is sliced
|
|
116
|
+
* from the `encodedBasicData` array based on predefined offsets and lengths, and then converted
|
|
117
|
+
* to its appropriate type (integer or BigInt).
|
|
118
|
+
* @param {Uint8Array} encodedBasicData - The encoded BinaryTree leaf basic data containing the version, nonce,
|
|
119
|
+
* code size, and balance in a compact Uint8Array format.
|
|
120
|
+
* @returns {BinaryTreeLeafBasicData} - An object containing the decoded version, nonce, code size, and balance.
|
|
121
|
+
*/
|
|
122
|
+
export declare function decodeBinaryTreeLeafBasicData(encodedBasicData: Uint8Array): BinaryTreeLeafBasicData;
|
|
123
|
+
/**
|
|
124
|
+
* This function takes a `BinaryTreeLeafBasicData` object and encodes its properties
|
|
125
|
+
* (version, nonce, code size, and balance) into a compact `Uint8Array` format. Each
|
|
126
|
+
* property is serialized and padded to match the required byte lengths defined by
|
|
127
|
+
* EIP-7864. Additionally, 4 bytes are reserved for future use as specified
|
|
128
|
+
* in EIP-7864.
|
|
129
|
+
* @param {Account} account - An object containing the version, nonce,
|
|
130
|
+
* code size, and balance to be encoded.
|
|
131
|
+
* @returns {Uint8Array} - A compact bytes representation of the account header basic data.
|
|
132
|
+
*/
|
|
133
|
+
export declare function encodeBinaryTreeLeafBasicData(account: Account): Uint8Array;
|
|
134
|
+
/**
|
|
135
|
+
* Helper method to generate the suffixes for code chunks for putting code
|
|
136
|
+
* @param numChunks number of chunks to generate suffixes for
|
|
137
|
+
* @returns number[] - an array of numbers corresponding to the code chunks being put
|
|
138
|
+
*/
|
|
139
|
+
export declare const generateBinaryTreeChunkSuffixes: (numChunks: number) => number[];
|
|
140
|
+
/**
|
|
141
|
+
* Helper method for generating the code stems necessary for putting code
|
|
142
|
+
* @param numChunks the number of code chunks to be put
|
|
143
|
+
* @param address the address of the account getting the code
|
|
144
|
+
* @param hashFunction an initialized {@link BinaryTreeCrypto} object
|
|
145
|
+
* @returns an array of stems for putting code
|
|
146
|
+
*/
|
|
147
|
+
export declare function generateBinaryTreeCodeStems(numChunks: number, address: Address, hashFunction: (input: Uint8Array) => Uint8Array): Uint8Array[];
|
|
148
|
+
//# sourceMappingURL=binaryTree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binaryTree.d.ts","sourceRoot":"","sources":["../../src/binaryTree.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEnD;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,YAAY,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,UAAU,EAC/C,OAAO,EAAE,OAAO,EAChB,SAAS,GAAE,MAAM,GAAG,MAAU,GAC7B,UAAU,CAaZ;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,iBAAiB,CAAA;IACvB,WAAW,EAAE;QACX,YAAY,EAAE,iBAAiB,GAAG,IAAI,CAAA;QACtC,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAA;QAClC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;KACxB,EAAE,CAAA;CACJ;AAGD,MAAM,MAAM,eAAe,GAAG,GAAG,CAAA;AAEjC;;;KAGK;AACL,MAAM,WAAW,0BAA0B;IACzC;;OAEG;IACH,eAAe,EAAE,iBAAiB,CAAA;IAClC;;;;OAIG;IACH,SAAS,EAAE,mBAAmB,EAAE,CAAA;IAChC;;;OAGG;IACH,KAAK,EAAE,eAAe,CAAA;CACvB;AAED,MAAM,MAAM,kBAA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GACvC,SAAS,OAAO,EAChB,SAAS,MAAM,EACf,cAAc,CAAC,KAAK,EAAE,UAAU,KAAK,UAAU,4BAIhD,CAAA;AAGD,eAAO,MAAM,sBAAsB,GAAI,MAAM,UAAU,kCAsCtD,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,8BAA8B,GACzC,SAAS,OAAO,EAChB,YAAY,MAAM,EAClB,cAAc,CAAC,KAAK,EAAE,UAAU,KAAK,UAAU,4BAKhD,CAAA;AAED;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAC3C,gBAAgB,EAAE,UAAU,GAC3B,uBAAuB,CAqBzB;AAED;;;;;;;;;GASG;AACH,wBAAgB,6BAA6B,CAAC,OAAO,EAAE,OAAO,GAAG,UAAU,CAiB1E;AAED;;;;GAIG;AACH,eAAO,MAAM,+BAA+B,GAAI,WAAW,MAAM,aAYhE,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,OAAO,EAChB,YAAY,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,UAAU,GAC9C,UAAU,EAAE,CAwBd"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { bigIntToBytes, bytesToBigInt, bytesToInt32, concatBytes, int32ToBytes, intToBytes, setLengthLeft, setLengthRight, } from "./bytes.js";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Returns the 31-bytes binary tree stem for a given address and tree index.
|
|
4
|
+
* @param hashFunction The hashFunction for the binary tree
|
|
5
|
+
* @param {Address} address The address to generate the tree key for.
|
|
6
|
+
* @param treeIndex The index of the tree to generate the key for. Defaults to 0.
|
|
7
|
+
* @return The 31-bytes binary tree stem as a Uint8Array.
|
|
8
|
+
*/
|
|
9
|
+
export function getBinaryTreeStem(hashFunction, address, treeIndex = 0) {
|
|
10
|
+
const address32 = setLengthLeft(address.toBytes(), 32);
|
|
11
|
+
let treeIndexBytes;
|
|
12
|
+
if (typeof treeIndex === 'number') {
|
|
13
|
+
treeIndexBytes = setLengthRight(int32ToBytes(Number(treeIndex), true), 32);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
treeIndexBytes = setLengthRight(bigIntToBytes(BigInt(treeIndex), true).slice(0, 32), 32);
|
|
17
|
+
}
|
|
18
|
+
const treeStem = hashFunction(concatBytes(address32, treeIndexBytes)).slice(0, 31);
|
|
19
|
+
return treeStem;
|
|
20
|
+
}
|
|
21
|
+
export const BinaryTreeLeafType = {
|
|
22
|
+
BasicData: 0,
|
|
23
|
+
CodeHash: 1,
|
|
24
|
+
};
|
|
25
|
+
export const BINARY_TREE_VERSION_OFFSET = 0;
|
|
26
|
+
export const BINARY_TREE_CODE_SIZE_OFFSET = 5;
|
|
27
|
+
export const BINARY_TREE_NONCE_OFFSET = 8;
|
|
28
|
+
export const BINARY_TREE_BALANCE_OFFSET = 16;
|
|
29
|
+
export const BINARY_TREE_VERSION_BYTES_LENGTH = 1;
|
|
30
|
+
export const BINARY_TREE_CODE_SIZE_BYTES_LENGTH = 3;
|
|
31
|
+
export const BINARY_TREE_NONCE_BYTES_LENGTH = 8;
|
|
32
|
+
export const BINARY_TREE_BALANCE_BYTES_LENGTH = 16;
|
|
33
|
+
export const BINARY_TREE_BASIC_DATA_LEAF_KEY = intToBytes(BinaryTreeLeafType.BasicData);
|
|
34
|
+
export const BINARY_TREE_CODE_HASH_LEAF_KEY = intToBytes(BinaryTreeLeafType.CodeHash);
|
|
35
|
+
export const BINARY_TREE_CODE_CHUNK_SIZE = 31;
|
|
36
|
+
export const BINARY_TREE_HEADER_STORAGE_OFFSET = 64;
|
|
37
|
+
export const BINARY_TREE_CODE_OFFSET = 128;
|
|
38
|
+
export const BINARY_TREE_NODE_WIDTH = 256;
|
|
39
|
+
export const BINARY_TREE_MAIN_STORAGE_OFFSET = BigInt(256) ** BigInt(BINARY_TREE_CODE_CHUNK_SIZE);
|
|
40
|
+
/**
|
|
41
|
+
* @dev Returns the tree key for a given binary tree stem, and sub index.
|
|
42
|
+
* @dev Assumes that the tree node width = 256
|
|
43
|
+
* @param stem The 31-bytes binary tree stem as a Uint8Array.
|
|
44
|
+
* @param subIndex The sub index of the tree to generate the key for as a Uint8Array.
|
|
45
|
+
* @return The tree key as a Uint8Array.
|
|
46
|
+
*/
|
|
47
|
+
export const getBinaryTreeKey = (stem, leaf) => {
|
|
48
|
+
switch (leaf) {
|
|
49
|
+
case BinaryTreeLeafType.BasicData:
|
|
50
|
+
return concatBytes(stem, BINARY_TREE_BASIC_DATA_LEAF_KEY);
|
|
51
|
+
case BinaryTreeLeafType.CodeHash:
|
|
52
|
+
return concatBytes(stem, BINARY_TREE_CODE_HASH_LEAF_KEY);
|
|
53
|
+
default:
|
|
54
|
+
return concatBytes(stem, leaf);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Calculates the position of the storage key in the BinaryTree tree, determining
|
|
59
|
+
* both the tree index (the node in the tree) and the subindex (the position within the node).
|
|
60
|
+
* @param {bigint} storageKey - The key representing a specific storage slot.
|
|
61
|
+
* @returns {Object} - An object containing the tree index and subindex
|
|
62
|
+
*/
|
|
63
|
+
export function getBinaryTreeIndicesForStorageSlot(storageKey) {
|
|
64
|
+
let position;
|
|
65
|
+
if (storageKey < BINARY_TREE_CODE_OFFSET - BINARY_TREE_HEADER_STORAGE_OFFSET) {
|
|
66
|
+
position = BigInt(BINARY_TREE_HEADER_STORAGE_OFFSET) + storageKey;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
position = BINARY_TREE_MAIN_STORAGE_OFFSET + storageKey;
|
|
70
|
+
}
|
|
71
|
+
const treeIndex = position / BigInt(BINARY_TREE_NODE_WIDTH);
|
|
72
|
+
const subIndex = Number(position % BigInt(BINARY_TREE_NODE_WIDTH));
|
|
73
|
+
return { treeIndex, subIndex };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Calculates the position of the code chunks in the BinaryTree tree, determining
|
|
77
|
+
* both the tree index (the node in the tree) and the subindex (the position within the node).
|
|
78
|
+
* @param {bigint} chunkId - The ID representing a specific chunk.
|
|
79
|
+
* @returns {Object} - An object containing the tree index and subindex
|
|
80
|
+
*/
|
|
81
|
+
export function getBinaryTreeIndicesForCodeChunk(chunkId) {
|
|
82
|
+
const treeIndex = Math.floor((BINARY_TREE_CODE_OFFSET + chunkId) / BINARY_TREE_NODE_WIDTH);
|
|
83
|
+
const subIndex = (BINARY_TREE_CODE_OFFSET + chunkId) % BINARY_TREE_NODE_WIDTH;
|
|
84
|
+
return { treeIndex, subIndex };
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Asynchronously calculates the BinaryTree tree key for the specified code chunk ID.
|
|
88
|
+
* @param {Address} address - The account address to access code for.
|
|
89
|
+
* @param {number} chunkId - The ID of the code chunk to retrieve.
|
|
90
|
+
* @param hashFunction - The hash function used for BinaryTree-related operations.
|
|
91
|
+
* @returns {Uint8Array} - The BinaryTree tree key as a byte array.
|
|
92
|
+
*/
|
|
93
|
+
export const getBinaryTreeKeyForCodeChunk = (address, chunkId, hashFunction) => {
|
|
94
|
+
const { treeIndex, subIndex } = getBinaryTreeIndicesForCodeChunk(chunkId);
|
|
95
|
+
return concatBytes(getBinaryTreeStem(hashFunction, address, treeIndex), intToBytes(subIndex));
|
|
96
|
+
};
|
|
97
|
+
// This code was written by robots based on the reference implementation in EIP-7864
|
|
98
|
+
export const chunkifyBinaryTreeCode = (code) => {
|
|
99
|
+
const PUSH1 = 0x60; // Assuming PUSH1 is defined as 0x60
|
|
100
|
+
const PUSH32 = 0x7f; // Assuming PUSH32 is defined as 0x7f
|
|
101
|
+
const PUSH_OFFSET = 0x5f; // Assuming PUSH_OFFSET is defined as 0x5f
|
|
102
|
+
// Calculate padding length
|
|
103
|
+
const paddingLength = (31 - (code.length % 31)) % 31;
|
|
104
|
+
const paddedCode = new Uint8Array(code.length + paddingLength);
|
|
105
|
+
paddedCode.set(code);
|
|
106
|
+
// Pre-allocate the bytesToExecData array
|
|
107
|
+
const bytesToExecData = new Uint8Array(paddedCode.length + 32);
|
|
108
|
+
let pos = 0;
|
|
109
|
+
while (pos < paddedCode.length) {
|
|
110
|
+
let pushdataBytes = 0;
|
|
111
|
+
if (PUSH1 <= paddedCode[pos] && paddedCode[pos] <= PUSH32) {
|
|
112
|
+
pushdataBytes = paddedCode[pos] - PUSH_OFFSET;
|
|
113
|
+
}
|
|
114
|
+
pos += 1;
|
|
115
|
+
for (let x = 0; x < pushdataBytes; x++) {
|
|
116
|
+
bytesToExecData[pos + x] = pushdataBytes - x;
|
|
117
|
+
}
|
|
118
|
+
pos += pushdataBytes;
|
|
119
|
+
}
|
|
120
|
+
// Pre-allocate the chunks array
|
|
121
|
+
const numChunks = Math.ceil(paddedCode.length / 31);
|
|
122
|
+
const chunks = new Array(numChunks);
|
|
123
|
+
for (let i = 0, pos = 0; i < numChunks; i++, pos += 31) {
|
|
124
|
+
const chunk = new Uint8Array(32);
|
|
125
|
+
chunk[0] = Math.min(bytesToExecData[pos], 31);
|
|
126
|
+
chunk.set(paddedCode.subarray(pos, pos + 31), 1);
|
|
127
|
+
chunks[i] = chunk;
|
|
128
|
+
}
|
|
129
|
+
return chunks;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Asynchronously calculates the BinaryTree tree key for the specified storage slot.
|
|
133
|
+
* @param {Address} address - The account address to access code for.
|
|
134
|
+
* @param {bigint} storageKey - The storage slot key to retrieve the key for.
|
|
135
|
+
* @param hashFunction - The hash function used in the Binary Tree.
|
|
136
|
+
* @returns {Uint8Array} - The BinaryTree tree key as a byte array.
|
|
137
|
+
*/
|
|
138
|
+
export const getBinaryTreeKeyForStorageSlot = (address, storageKey, hashFunction) => {
|
|
139
|
+
const { treeIndex, subIndex } = getBinaryTreeIndicesForStorageSlot(storageKey);
|
|
140
|
+
return concatBytes(getBinaryTreeStem(hashFunction, address, treeIndex), intToBytes(subIndex));
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* This function extracts and decodes account header elements (version, nonce, code size, and balance)
|
|
144
|
+
* from an encoded `Uint8Array` representation of raw BinaryTree leaf-node basic data. Each component is sliced
|
|
145
|
+
* from the `encodedBasicData` array based on predefined offsets and lengths, and then converted
|
|
146
|
+
* to its appropriate type (integer or BigInt).
|
|
147
|
+
* @param {Uint8Array} encodedBasicData - The encoded BinaryTree leaf basic data containing the version, nonce,
|
|
148
|
+
* code size, and balance in a compact Uint8Array format.
|
|
149
|
+
* @returns {BinaryTreeLeafBasicData} - An object containing the decoded version, nonce, code size, and balance.
|
|
150
|
+
*/
|
|
151
|
+
export function decodeBinaryTreeLeafBasicData(encodedBasicData) {
|
|
152
|
+
const versionBytes = encodedBasicData.slice(0, BINARY_TREE_VERSION_BYTES_LENGTH);
|
|
153
|
+
const nonceBytes = encodedBasicData.slice(BINARY_TREE_NONCE_OFFSET, BINARY_TREE_NONCE_OFFSET + BINARY_TREE_NONCE_BYTES_LENGTH);
|
|
154
|
+
const codeSizeBytes = encodedBasicData.slice(BINARY_TREE_CODE_SIZE_OFFSET, BINARY_TREE_CODE_SIZE_OFFSET + BINARY_TREE_CODE_SIZE_BYTES_LENGTH);
|
|
155
|
+
const balanceBytes = encodedBasicData.slice(BINARY_TREE_BALANCE_OFFSET, BINARY_TREE_BALANCE_OFFSET + BINARY_TREE_BALANCE_BYTES_LENGTH);
|
|
156
|
+
const version = bytesToInt32(versionBytes);
|
|
157
|
+
const nonce = bytesToBigInt(nonceBytes);
|
|
158
|
+
const codeSize = bytesToInt32(codeSizeBytes);
|
|
159
|
+
const balance = bytesToBigInt(balanceBytes);
|
|
160
|
+
return { version, nonce, codeSize, balance };
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* This function takes a `BinaryTreeLeafBasicData` object and encodes its properties
|
|
164
|
+
* (version, nonce, code size, and balance) into a compact `Uint8Array` format. Each
|
|
165
|
+
* property is serialized and padded to match the required byte lengths defined by
|
|
166
|
+
* EIP-7864. Additionally, 4 bytes are reserved for future use as specified
|
|
167
|
+
* in EIP-7864.
|
|
168
|
+
* @param {Account} account - An object containing the version, nonce,
|
|
169
|
+
* code size, and balance to be encoded.
|
|
170
|
+
* @returns {Uint8Array} - A compact bytes representation of the account header basic data.
|
|
171
|
+
*/
|
|
172
|
+
export function encodeBinaryTreeLeafBasicData(account) {
|
|
173
|
+
const encodedVersion = setLengthLeft(intToBytes(account.version), BINARY_TREE_VERSION_BYTES_LENGTH);
|
|
174
|
+
// Per EIP-7864, bytes 1-4 are reserved for future use
|
|
175
|
+
const reservedBytes = new Uint8Array([0, 0, 0, 0]);
|
|
176
|
+
const encodedNonce = setLengthLeft(bigIntToBytes(account.nonce), BINARY_TREE_NONCE_BYTES_LENGTH);
|
|
177
|
+
const encodedCodeSize = setLengthLeft(intToBytes(account.codeSize), BINARY_TREE_CODE_SIZE_BYTES_LENGTH);
|
|
178
|
+
const encodedBalance = setLengthLeft(bigIntToBytes(account.balance), BINARY_TREE_BALANCE_BYTES_LENGTH);
|
|
179
|
+
return concatBytes(encodedVersion, reservedBytes, encodedCodeSize, encodedNonce, encodedBalance);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Helper method to generate the suffixes for code chunks for putting code
|
|
183
|
+
* @param numChunks number of chunks to generate suffixes for
|
|
184
|
+
* @returns number[] - an array of numbers corresponding to the code chunks being put
|
|
185
|
+
*/
|
|
186
|
+
export const generateBinaryTreeChunkSuffixes = (numChunks) => {
|
|
187
|
+
if (numChunks === 0)
|
|
188
|
+
return [];
|
|
189
|
+
const chunkSuffixes = new Array(numChunks);
|
|
190
|
+
let currentSuffix = BINARY_TREE_CODE_OFFSET;
|
|
191
|
+
for (let x = 0; x < numChunks; x++) {
|
|
192
|
+
chunkSuffixes[x] = currentSuffix;
|
|
193
|
+
currentSuffix++;
|
|
194
|
+
// Reset suffix to 0 if exceeds BINARY_TREE_NODE_WIDTH
|
|
195
|
+
if (currentSuffix >= BINARY_TREE_NODE_WIDTH)
|
|
196
|
+
currentSuffix = 0;
|
|
197
|
+
}
|
|
198
|
+
return chunkSuffixes;
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* Helper method for generating the code stems necessary for putting code
|
|
202
|
+
* @param numChunks the number of code chunks to be put
|
|
203
|
+
* @param address the address of the account getting the code
|
|
204
|
+
* @param hashFunction an initialized {@link BinaryTreeCrypto} object
|
|
205
|
+
* @returns an array of stems for putting code
|
|
206
|
+
*/
|
|
207
|
+
export function generateBinaryTreeCodeStems(numChunks, address, hashFunction) {
|
|
208
|
+
// The maximum number of chunks is 793 (maxCodeSize - 24576) / (bytes per chunk 31) + (round up - 1)
|
|
209
|
+
// Code is stored in chunks starting at leaf index 128 of the leaf node corresponding to the stem of the code's address
|
|
210
|
+
// Code chunks beyond the initial 128 are stored in additional leaf nodes in batches up of up to 256 chunks per leaf node
|
|
211
|
+
// so the maximum number of leaf nodes that can hold contract code for a specific address is 4 leaf nodes (128 chunks in
|
|
212
|
+
// the first leaf node and 256 chunks in up to 3 additional leaf nodes)
|
|
213
|
+
// So, instead of computing every single leaf key (which is a heavy operation), we just compute the stem for the first
|
|
214
|
+
// chunk in each leaf node and can then know that the chunks in between have tree keys in monotonically increasing order
|
|
215
|
+
const numStems = numChunks > BINARY_TREE_CODE_OFFSET ? Math.ceil(numChunks / BINARY_TREE_NODE_WIDTH) + 1 : 1;
|
|
216
|
+
const chunkStems = new Array(numStems);
|
|
217
|
+
// Compute the stem for the initial set of code chunks
|
|
218
|
+
chunkStems[0] = getBinaryTreeKeyForCodeChunk(address, 0, hashFunction).slice(0, 31);
|
|
219
|
+
for (let stemNum = 0; stemNum < numStems - 1; stemNum++) {
|
|
220
|
+
// Generate additional stems
|
|
221
|
+
const firstChunkKey = getBinaryTreeKeyForCodeChunk(address, BINARY_TREE_CODE_OFFSET + stemNum * BINARY_TREE_NODE_WIDTH, hashFunction);
|
|
222
|
+
chunkStems[stemNum + 1] = firstChunkKey.slice(0, 31);
|
|
223
|
+
}
|
|
224
|
+
return chunkStems;
|
|
225
|
+
}
|
|
226
|
+
//# sourceMappingURL=binaryTree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binaryTree.js","sourceRoot":"","sources":["../../src/binaryTree.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,aAAa,EACb,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,UAAU,EACV,aAAa,EACb,cAAc,GACf,MAAM,YAAY,CAAA;AAMnB;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,YAA+C,EAC/C,OAAgB,EAChB,YAA6B,CAAC;IAE9B,MAAM,SAAS,GAAG,aAAa,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,EAAE,CAAC,CAAA;IAEtD,IAAI,cAA0B,CAAA;IAC9B,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;QAClC,cAAc,GAAG,cAAc,CAAC,YAAY,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAA;IAC5E,CAAC;SAAM,CAAC;QACN,cAAc,GAAG,cAAc,CAAC,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA;IAC1F,CAAC;IAED,MAAM,QAAQ,GAAG,YAAY,CAAC,WAAW,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IAElF,OAAO,QAAQ,CAAA;AACjB,CAAC;AAsCD,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,SAAS,EAAE,CAAC;IACZ,QAAQ,EAAE,CAAC;CACH,CAAA;AASV,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,CAAA;AAC3C,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,CAAA;AAC7C,MAAM,CAAC,MAA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
|
+
import type { PrefixedHexString } from './types.ts';
|
|
3
|
+
export declare const CELLS_PER_EXT_BLOB = 128;
|
|
4
|
+
/**
|
|
5
|
+
* Converts arbitrary byte data into EIP-4844 blob format.
|
|
6
|
+
* Splits data into 4096 field elements of 32 bytes each, with proper alignment.
|
|
7
|
+
* @param data Input data (must be exactly BLOB_SIZE bytes)
|
|
8
|
+
* @returns Hex-prefixed blob string
|
|
9
|
+
*/
|
|
10
|
+
export declare function getBlob(data: Uint8Array): PrefixedHexString;
|
|
11
|
+
/**
|
|
12
|
+
* EIP-4844: Converts UTF-8 string(s) into EIP-4844 blob format.
|
|
13
|
+
*
|
|
14
|
+
* Each input string is converted to UTF-8 bytes, padded with 0x80 followed by zeros
|
|
15
|
+
* to align with blob boundaries, and encoded as one or more blobs depending on size.
|
|
16
|
+
* Multiple inputs are processed sequentially, with each input contributing its own blob(s).
|
|
17
|
+
*
|
|
18
|
+
* @param input Single UTF-8 string or array of UTF-8 strings to encode
|
|
19
|
+
* @throws Error with message 'invalid blob data' if any input string is empty
|
|
20
|
+
* @throws Error with message 'blob data is too large' if any single input exceeds MAX_USEFUL_BYTES_PER_TX
|
|
21
|
+
* @returns Array of hex-prefixed blob strings (0x...), one blob per 131,071 useful bytes per input
|
|
22
|
+
*/
|
|
23
|
+
export declare const getBlobs: (input: string | string[]) => `0x${string}`[];
|
|
24
|
+
/**
|
|
25
|
+
* EIP-4844: Computes KZG commitments for a set of blobs.
|
|
26
|
+
* @param kzg KZG implementation used to compute commitments
|
|
27
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
28
|
+
* @returns Array of lowercase hex-prefixed KZG commitments (one per blob)
|
|
29
|
+
*/
|
|
30
|
+
export declare const blobsToCommitments: (kzg: KZG, blobs: PrefixedHexString[]) => `0x${string}`[];
|
|
31
|
+
/**
|
|
32
|
+
* EIP-4844: Computes KZG proofs for each blob/commitment pair.
|
|
33
|
+
* @param kzg KZG implementation used to compute proofs
|
|
34
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
35
|
+
* @param commitments Array of corresponding blob commitments
|
|
36
|
+
* @returns Array of lowercase hex-prefixed proofs (aligned with input order)
|
|
37
|
+
*/
|
|
38
|
+
export declare const blobsToProofs: (kzg: KZG, blobs: PrefixedHexString[], commitments: PrefixedHexString[]) => `0x${string}`[];
|
|
39
|
+
/**
|
|
40
|
+
* EIP-4844: Converts a vector commitment for a given data blob to its versioned hash. For 4844, this version
|
|
41
|
+
* number will be 0x01 for KZG vector commitments but could be different if future vector commitment
|
|
42
|
+
* types are introduced
|
|
43
|
+
* @param commitment a vector commitment to a blob
|
|
44
|
+
* @param blobCommitmentVersion the version number corresponding to the type of vector commitment
|
|
45
|
+
* @returns a versioned hash corresponding to a given blob vector commitment
|
|
46
|
+
*/
|
|
47
|
+
export declare const computeVersionedHash: (commitment: PrefixedHexString, blobCommitmentVersion: number) => `0x${string}`;
|
|
48
|
+
/**
|
|
49
|
+
* EIP-4844: Generate an array of versioned hashes from corresponding kzg commitments
|
|
50
|
+
* @param commitments array of kzg commitments
|
|
51
|
+
* @returns array of versioned hashes
|
|
52
|
+
* Note: assumes KZG commitments (version 1 version hashes)
|
|
53
|
+
*/
|
|
54
|
+
export declare const commitmentsToVersionedHashes: (commitments: PrefixedHexString[]) => `0x${string}`[];
|
|
55
|
+
/**
|
|
56
|
+
* EIP-7594: Expands blobs into their extended cells using the provided KZG implementation.
|
|
57
|
+
* @param kzg KZG implementation capable of computing cells
|
|
58
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
59
|
+
* @returns Tuple of [cells, indices], where cells are hex strings and indices are 0..127
|
|
60
|
+
*/
|
|
61
|
+
export declare const blobsToCells: (kzg: KZG, blobs: PrefixedHexString[]) => [PrefixedHexString[], number[]];
|
|
62
|
+
/**
|
|
63
|
+
* EIP-7594: Computes extended cells and corresponding proofs for the given blobs.
|
|
64
|
+
* @param kzg KZG implementation capable of computing cells and proofs
|
|
65
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
66
|
+
* @returns Tuple of [cells, proofs, indices]; indices are 0..127
|
|
67
|
+
*/
|
|
68
|
+
export declare const blobsToCellsAndProofs: (kzg: KZG, blobs: PrefixedHexString[]) => [PrefixedHexString[], PrefixedHexString[], number[]];
|
|
69
|
+
/**
|
|
70
|
+
* EIP-7594: Computes cell proofs for the given blobs.
|
|
71
|
+
* @param kzg KZG implementation capable of computing cell proofs
|
|
72
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
73
|
+
* @returns Array of lowercase hex-prefixed cell proofs (aligned with input order)
|
|
74
|
+
*/
|
|
75
|
+
export declare const blobsToCellProofs: (kzg: KZG, blobs: PrefixedHexString[]) => PrefixedHexString[];
|
|
76
|
+
//# sourceMappingURL=blobs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blobs.d.ts","sourceRoot":"","sources":["../../src/blobs.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AACnC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAYnD,eAAO,MAAM,kBAAkB,MAAM,CAAA;AAerC;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,UAAU,GAAG,iBAAiB,CAS3D;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,MAAM,GAAG,MAAM,EAAE,oBA0BhD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,KAAK,GAAG,EAAE,OAAO,iBAAiB,EAAE,oBAMtE,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,GACxB,KAAK,GAAG,EACR,OAAO,iBAAiB,EAAE,EAC1B,aAAa,iBAAiB,EAAE,oBAOjC,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,GAC/B,YAAY,iBAAiB,EAC7B,uBAAuB,MAAM,kBAM9B,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,GAAI,aAAa,iBAAiB,EAAE,oBAM5E,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GACvB,KAAK,GAAG,EACR,OAAO,iBAAiB,EAAE,KACzB,CAAC,iBAAiB,EAAE,EAAE,MAAM,EAAE,CAOhC,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAChC,KAAK,GAAG,EACR,OAAO,iBAAiB,EAAE,KACzB,CAAC,iBAAiB,EAAE,EAAE,iBAAiB,EAAE,EAAE,MAAM,EAAE,CAiBrD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAAI,KAAK,GAAG,EAAE,OAAO,iBAAiB,EAAE,KAAG,iBAAiB,EAEzF,CAAA"}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
2
|
+
import { bytesToHex, hexToBytes, utf8ToBytes } from "./bytes.js";
|
|
3
|
+
/**
|
|
4
|
+
* These utilities for constructing blobs are borrowed from https://github.com/Inphi/eip4844-interop.git
|
|
5
|
+
*/
|
|
6
|
+
const BYTES_PER_FIELD_ELEMENT = 32; // EIP-4844
|
|
7
|
+
const FIELD_ELEMENTS_PER_BLOB = 4096; // EIP-4844
|
|
8
|
+
const BLOB_SIZE = BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB;
|
|
9
|
+
const MAX_BLOBS_PER_TX = 6; // EIP-7691: Blob throughput increase, Pectra HF
|
|
10
|
+
const MAX_BLOB_BYTES_PER_TX = BLOB_SIZE * MAX_BLOBS_PER_TX - 1;
|
|
11
|
+
export const CELLS_PER_EXT_BLOB = 128; // EIP-4844, Consensus Spec, 2 * FIELD_ELEMENTS_PER_BLOB // 64 (FIELD_ELEMENTS_PER_CELL)
|
|
12
|
+
/**
|
|
13
|
+
* Pads input data to blob boundaries with 0x80 marker and zeros.
|
|
14
|
+
* @param data Input data to pad
|
|
15
|
+
* @param blobs_len Number of blobs the data should span
|
|
16
|
+
* @returns Padded data aligned to blob boundaries
|
|
17
|
+
*/
|
|
18
|
+
function getPadded(data, blobs_len) {
|
|
19
|
+
const pData = new Uint8Array(blobs_len * BLOB_SIZE);
|
|
20
|
+
pData.set(data);
|
|
21
|
+
pData[data.byteLength] = 0x80;
|
|
22
|
+
return pData;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Converts arbitrary byte data into EIP-4844 blob format.
|
|
26
|
+
* Splits data into 4096 field elements of 32 bytes each, with proper alignment.
|
|
27
|
+
* @param data Input data (must be exactly BLOB_SIZE bytes)
|
|
28
|
+
* @returns Hex-prefixed blob string
|
|
29
|
+
*/
|
|
30
|
+
export function getBlob(data) {
|
|
31
|
+
const blob = new Uint8Array(BLOB_SIZE);
|
|
32
|
+
for (let i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) {
|
|
33
|
+
const chunk = new Uint8Array(32);
|
|
34
|
+
chunk.set(data.subarray(i * 31, (i + 1) * 31), 0);
|
|
35
|
+
blob.set(chunk, i * 32);
|
|
36
|
+
}
|
|
37
|
+
return bytesToHex(blob);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* EIP-4844: Converts UTF-8 string(s) into EIP-4844 blob format.
|
|
41
|
+
*
|
|
42
|
+
* Each input string is converted to UTF-8 bytes, padded with 0x80 followed by zeros
|
|
43
|
+
* to align with blob boundaries, and encoded as one or more blobs depending on size.
|
|
44
|
+
* Multiple inputs are processed sequentially, with each input contributing its own blob(s).
|
|
45
|
+
*
|
|
46
|
+
* @param input Single UTF-8 string or array of UTF-8 strings to encode
|
|
47
|
+
* @throws Error with message 'invalid blob data' if any input string is empty
|
|
48
|
+
* @throws Error with message 'blob data is too large' if any single input exceeds MAX_USEFUL_BYTES_PER_TX
|
|
49
|
+
* @returns Array of hex-prefixed blob strings (0x...), one blob per 131,071 useful bytes per input
|
|
50
|
+
*/
|
|
51
|
+
export const getBlobs = (input) => {
|
|
52
|
+
const inputArray = Array.isArray(input) ? input : [input];
|
|
53
|
+
const blobs = [];
|
|
54
|
+
for (const input of inputArray) {
|
|
55
|
+
const data = utf8ToBytes(input);
|
|
56
|
+
const len = data.byteLength;
|
|
57
|
+
if (len === 0) {
|
|
58
|
+
throw Error('invalid blob data (0 bytes)');
|
|
59
|
+
}
|
|
60
|
+
if (len > MAX_BLOB_BYTES_PER_TX) {
|
|
61
|
+
throw Error(`blob data is too large (${len} bytes > ${MAX_BLOB_BYTES_PER_TX} bytes)`);
|
|
62
|
+
}
|
|
63
|
+
const blobs_len = Math.ceil(len / BLOB_SIZE);
|
|
64
|
+
const pData = getPadded(data, blobs_len);
|
|
65
|
+
for (let i = 0; i < blobs_len; i++) {
|
|
66
|
+
const chunk = pData.subarray(i * BLOB_SIZE, (i + 1) * BLOB_SIZE);
|
|
67
|
+
const blob = getBlob(chunk);
|
|
68
|
+
blobs.push(blob);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return blobs;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* EIP-4844: Computes KZG commitments for a set of blobs.
|
|
75
|
+
* @param kzg KZG implementation used to compute commitments
|
|
76
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
77
|
+
* @returns Array of lowercase hex-prefixed KZG commitments (one per blob)
|
|
78
|
+
*/
|
|
79
|
+
export const blobsToCommitments = (kzg, blobs) => {
|
|
80
|
+
const commitments = [];
|
|
81
|
+
for (const blob of blobs) {
|
|
82
|
+
commitments.push(kzg.blobToKzgCommitment(blob).toLowerCase());
|
|
83
|
+
}
|
|
84
|
+
return commitments;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* EIP-4844: Computes KZG proofs for each blob/commitment pair.
|
|
88
|
+
* @param kzg KZG implementation used to compute proofs
|
|
89
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
90
|
+
* @param commitments Array of corresponding blob commitments
|
|
91
|
+
* @returns Array of lowercase hex-prefixed proofs (aligned with input order)
|
|
92
|
+
*/
|
|
93
|
+
export const blobsToProofs = (kzg, blobs, commitments) => {
|
|
94
|
+
const proofs = blobs.map((blob, ctx) => kzg.computeBlobProof(blob, commitments[ctx]).toLowerCase());
|
|
95
|
+
return proofs;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* EIP-4844: Converts a vector commitment for a given data blob to its versioned hash. For 4844, this version
|
|
99
|
+
* number will be 0x01 for KZG vector commitments but could be different if future vector commitment
|
|
100
|
+
* types are introduced
|
|
101
|
+
* @param commitment a vector commitment to a blob
|
|
102
|
+
* @param blobCommitmentVersion the version number corresponding to the type of vector commitment
|
|
103
|
+
* @returns a versioned hash corresponding to a given blob vector commitment
|
|
104
|
+
*/
|
|
105
|
+
export const computeVersionedHash = (commitment, blobCommitmentVersion) => {
|
|
106
|
+
const computedVersionedHash = new Uint8Array(32);
|
|
107
|
+
computedVersionedHash.set([blobCommitmentVersion], 0);
|
|
108
|
+
computedVersionedHash.set(sha256(hexToBytes(commitment)).subarray(1), 1);
|
|
109
|
+
return bytesToHex(computedVersionedHash);
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* EIP-4844: Generate an array of versioned hashes from corresponding kzg commitments
|
|
113
|
+
* @param commitments array of kzg commitments
|
|
114
|
+
* @returns array of versioned hashes
|
|
115
|
+
* Note: assumes KZG commitments (version 1 version hashes)
|
|
116
|
+
*/
|
|
117
|
+
export const commitmentsToVersionedHashes = (commitments) => {
|
|
118
|
+
const hashes = [];
|
|
119
|
+
for (const commitment of commitments) {
|
|
120
|
+
hashes.push(computeVersionedHash(commitment, 0x01));
|
|
121
|
+
}
|
|
122
|
+
return hashes;
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* EIP-7594: Expands blobs into their extended cells using the provided KZG implementation.
|
|
126
|
+
* @param kzg KZG implementation capable of computing cells
|
|
127
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
128
|
+
* @returns Tuple of [cells, indices], where cells are hex strings and indices are 0..127
|
|
129
|
+
*/
|
|
130
|
+
export const blobsToCells = (kzg, blobs) => {
|
|
131
|
+
const cells = blobs.reduce((acc, elem) => {
|
|
132
|
+
return [...acc, ...kzg.computeCells(elem)];
|
|
133
|
+
}, []);
|
|
134
|
+
const indices = Array.from({ length: CELLS_PER_EXT_BLOB }, (_, i) => i);
|
|
135
|
+
return [cells, indices];
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* EIP-7594: Computes extended cells and corresponding proofs for the given blobs.
|
|
139
|
+
* @param kzg KZG implementation capable of computing cells and proofs
|
|
140
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
141
|
+
* @returns Tuple of [cells, proofs, indices]; indices are 0..127
|
|
142
|
+
*/
|
|
143
|
+
export const blobsToCellsAndProofs = (kzg, blobs) => {
|
|
144
|
+
const blobsAndCells = blobs.reduce(([cellsAcc, proofsAcc], elem) => {
|
|
145
|
+
const blobCellsAndProofs = kzg.computeCellsAndProofs(elem);
|
|
146
|
+
return [
|
|
147
|
+
[...cellsAcc, ...blobCellsAndProofs[0]],
|
|
148
|
+
[...proofsAcc, ...blobCellsAndProofs[1]],
|
|
149
|
+
];
|
|
150
|
+
}, [[], []]);
|
|
151
|
+
const indices = Array.from({ length: CELLS_PER_EXT_BLOB }, (_, i) => i);
|
|
152
|
+
return [...blobsAndCells, indices];
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* EIP-7594: Computes cell proofs for the given blobs.
|
|
156
|
+
* @param kzg KZG implementation capable of computing cell proofs
|
|
157
|
+
* @param blobs Array of blob data as hex-prefixed strings
|
|
158
|
+
* @returns Array of lowercase hex-prefixed cell proofs (aligned with input order)
|
|
159
|
+
*/
|
|
160
|
+
export const blobsToCellProofs = (kzg, blobs) => {
|
|
161
|
+
return blobsToCellsAndProofs(kzg, blobs)[1];
|
|
162
|
+
};
|
|
163
|
+
//# sourceMappingURL=blobs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|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