Skip to content

Commit 88dfe96

Browse files
committed
Added utility method to split EIP-7594 BLOb proofs into its cells.
1 parent 7c5faf8 commit 88dfe96

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src.ts/transaction/transaction.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ const BN_MAX_UINT = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffff
3030
const inspect = Symbol.for("nodejs.util.inspect.custom");
3131

3232
const BLOB_SIZE = 4096 * 32;
33+
const CELL_COUNT = 128;
34+
35+
36+
/**
37+
* Returns a BLOb proof as its cells for [[link-eip-7594]] BLOb.
38+
*
39+
* The default %%cellCount%% is 128.
40+
*/
41+
export function splitBlobCells(_proof: BytesLike, cellCount?: number): Array<string> {
42+
if (cellCount == null) { cellCount = CELL_COUNT; }
43+
44+
const cellProofs: Array<string> = [ ];
45+
const proof = getBytes(_proof);
46+
47+
const cellSize = proof.length / cellCount;
48+
for (let i = 0; i < proof.length; i += cellSize) {
49+
cellProofs.push(hexlify(proof.subarray(i, i + cellSize)));
50+
}
51+
52+
return cellProofs;
53+
}
3354

3455
// The BLS Modulo; each field within a BLOb must be less than this
3556
//const BLOB_BLS_MODULO = BigInt("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001");
@@ -613,9 +634,6 @@ function _parseEip4844(data: Uint8Array): TransactionLike {
613634
} else if (fields.length === 5 && Array.isArray(fields[0])) {
614635
// EIP-7594 format with sidecar
615636

616-
// The number of cells per proof
617-
const cellCount = 128;
618-
619637
typeName = "3 (EIP-7594 network format)";
620638

621639
blobWrapperVersion = getNumber(fields[1]);
@@ -626,13 +644,13 @@ function _parseEip4844(data: Uint8Array): TransactionLike {
626644
assertArgument(Array.isArray(fCommits), "invalid EIP-7594 network format: commitments not an array", "fields[3]", fCommits);
627645
assertArgument(Array.isArray(fProofs), "invalid EIP-7594 network format: proofs not an array", "fields[4]", fProofs);
628646
assertArgument(fBlobs.length === fCommits.length, "invalid network format: blobs/commitments length mismatch", "fields", fields);
629-
assertArgument(fBlobs.length * cellCount === fProofs.length, "invalid network format: blobs/proofs length mismatch", "fields", fields);
647+
assertArgument(fBlobs.length * CELL_COUNT === fProofs.length, "invalid network format: blobs/proofs length mismatch", "fields", fields);
630648

631649
blobs = [ ];
632650
for (let i = 0; i < fBlobs.length; i++) {
633651
const proof = [ ];
634-
for (let j = 0; j < cellCount; j++) {
635-
proof.push(fProofs[(i * cellCount) + j]);
652+
for (let j = 0; j < CELL_COUNT; j++) {
653+
proof.push(fProofs[(i * CELL_COUNT) + j]);
636654
}
637655

638656
blobs.push({
@@ -713,13 +731,10 @@ function _serializeEip4844(tx: Transaction, sig: null | Signature, blobs: null |
713731
if (tx.blobWrapperVersion != null) {
714732
const wrapperVersion = toBeArray(tx.blobWrapperVersion);
715733

716-
// The number of cells per blob
717-
const cellCount = 128;
718-
719734
const cellProofs: Array<Uint8Array> = [ ];
720735
for (const { proof } of blobs) {
721736
const p = getBytes(proof);
722-
const cellSize = p.length / cellCount;
737+
const cellSize = p.length / CELL_COUNT;
723738
for (let i = 0; i < p.length; i += cellSize) {
724739
cellProofs.push(p.subarray(i, i + cellSize));
725740
}

0 commit comments

Comments
 (0)