Skip to content

Commit efbf6d6

Browse files
holgerd77ScottyPoi
andauthored
Add Real-World Util Blob Example (#4192)
* Expand util blob example * Fix getBlobs() in Util, remove size limitation in example * revert change to getBlob * fix blob example * Commented out blobs example file reading and make it optional again --------- Co-authored-by: ScottyPoi <[email protected]>
1 parent 7eead0b commit efbf6d6

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

packages/util/examples/blobs.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1-
import { bytesToHex, computeVersionedHash, getBlobs } from '@ethereumjs/util'
1+
//import * as fs from 'fs'
2+
import {
3+
type PrefixedHexString,
4+
blobsToCellProofs,
5+
blobsToProofs,
6+
computeVersionedHash,
7+
hexToBytes,
8+
} from '@ethereumjs/util'
9+
import { trustedSetup } from '@paulmillr/trusted-setups/fast-peerdas.js'
10+
import { KZG as microEthKZG } from 'micro-eth-signer/kzg.js'
211

3-
const blobs = getBlobs('test input')
12+
const kzg = new microEthKZG(trustedSetup)
413

5-
console.log('Created the following blobs:')
6-
console.log(blobs)
14+
/**
15+
* Uncomment for a more realistic example using a real blob, e.g. from https://blobscan.com/
16+
* Use with node ./examples/blobs.ts <file path>
17+
*/
18+
// const filePath = process.argv[2]
19+
//const blob: PrefixedHexString = `0x${fs.readFileSync(filePath, 'ascii')}`
20+
const blob: PrefixedHexString = `0x${'11'.repeat(131072)}` // 128 KiB
21+
console.log(blob)
22+
23+
const commitment = kzg.blobToKzgCommitment(blob)
724

8-
const commitment = bytesToHex(new Uint8Array([1, 2, 3]))
925
const blobCommitmentVersion = 0x01
10-
const versionedHash = computeVersionedHash(commitment, blobCommitmentVersion)
26+
const versionedHash = computeVersionedHash(commitment as PrefixedHexString, blobCommitmentVersion)
27+
28+
// EIP-4844 only
29+
const blobProof = blobsToProofs(kzg, [blob], [commitment as PrefixedHexString])
30+
const cellProofs = blobsToCellProofs(kzg, [blob])
1131

12-
console.log(`Versioned hash ${versionedHash} computed`)
32+
console.log(`Blob size : ${hexToBytes(blob).length / 1024}KiB`)
33+
console.log(`Commitment : ${commitment}`)
34+
console.log(`Versioned hash : ${versionedHash}`)
35+
console.log(`Blob proof (EIP-4844) : ${blobProof}`)
36+
console.log(`First cell proof (EIP-7594) : ${cellProofs[0]}`)
37+
console.log(`Num cell proofs (EIP-7594) : ${cellProofs.length}`)

packages/util/src/blobs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function getPadded(data: Uint8Array, blobs_len: number): Uint8Array {
3636
* @param data Input data (must be exactly BLOB_SIZE bytes)
3737
* @returns Hex-prefixed blob string
3838
*/
39-
function getBlob(data: Uint8Array): PrefixedHexString {
39+
export function getBlob(data: Uint8Array): PrefixedHexString {
4040
const blob = new Uint8Array(BLOB_SIZE)
4141
for (let i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) {
4242
const chunk = new Uint8Array(32)

0 commit comments

Comments
 (0)