-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set up blob decoder package and add starknet decoding logic
- Loading branch information
Showing
10 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@blobscan/blob-decoder": minor | ||
--- | ||
|
||
Added starknet blob decoder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@blobscan/blob-decoder", | ||
"version": "0.0.1", | ||
"private": true, | ||
"types": "./src/index.ts", | ||
"exports": { | ||
".": { | ||
"import": "./src/index.ts", | ||
"require": "./src/index.ts" | ||
} | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"clean": "rm -rf .turbo node_modules", | ||
"lint": "eslint .", | ||
"lint:fix": "pnpm lint --fix", | ||
"type-check": "tsc --noEmit", | ||
"with-env:test": "dotenv -e ../../.env.test --", | ||
"test": "pnpm with-env:test vitest", | ||
"test:ui": "pnpm with-env:test vitest --ui" | ||
}, | ||
"dependencies": { | ||
"@blobscan/logger": "^0.0.6", | ||
"@blobscan/majin-blob-wasm": "^0.0.1", | ||
"@blobscan/open-telemetry": "^0.0.6", | ||
"@blobscan/zod": "^0.0.4" | ||
}, | ||
"eslintConfig": { | ||
"root": true, | ||
"extends": [ | ||
"@blobscan/eslint-config/base" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { StarknetStateDiff, decodeStarknetBlob } from "./starknet"; | ||
import { DecodableRollup } from "./types"; | ||
|
||
export function isDecodableRollup(rollup: string): rollup is DecodableRollup { | ||
return rollup === "STARKNET"; | ||
} | ||
|
||
export async function decodeBlob( | ||
rollup: "STARKNET", | ||
blobData: string | ||
): Promise<StarknetStateDiff[]>; | ||
export async function decodeBlob(rollup: DecodableRollup, blobData: string) { | ||
switch (rollup) { | ||
case "STARKNET": | ||
return decodeStarknetBlob(blobData); | ||
default: | ||
throw new Error(`Unsupported rollup: ${rollup}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./blob-decoder"; | ||
export * from "./types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { blob_recover } from "@blobscan/majin-blob-wasm"; | ||
import { z } from "@blobscan/zod"; | ||
|
||
import { BlobDecoderFn } from "./types"; | ||
import { bigIntToHex } from "./utils"; | ||
|
||
export const starknetStateDiffSchema = z | ||
.object({ | ||
address: z.string(), | ||
nonce: z.number(), | ||
number_of_storage_updates: z.number(), | ||
new_class_hash: z.string(), | ||
storage_updates: z.array( | ||
z.object({ | ||
key: z.string(), | ||
value: z.string(), | ||
}) | ||
), | ||
}) | ||
.transform((data) => ({ | ||
contractAddress: data.address, | ||
nonce: data.nonce, | ||
numberOfStorageUpdates: data.number_of_storage_updates, | ||
newClassHash: data.new_class_hash, | ||
storageUpdates: data.storage_updates, | ||
})); | ||
|
||
export type StarknetStateDiff = z.infer<typeof starknetStateDiffSchema>; | ||
|
||
function normalizeBlobData(blobData: string) { | ||
return blobData.startsWith("0x") ? blobData.slice(2) : blobData; | ||
} | ||
|
||
export const decodeStarknetBlob: BlobDecoderFn<StarknetStateDiff[]> = function ( | ||
blobData | ||
) { | ||
const normalizedBlobData = normalizeBlobData(blobData); | ||
const result = blob_recover(normalizedBlobData); | ||
const decodedBlob = JSON.parse(result); | ||
|
||
const starknetStateDiffs = starknetStateDiffSchema.array().parse(decodedBlob); | ||
|
||
return starknetStateDiffs.map<StarknetStateDiff>((entry) => { | ||
return { | ||
contractAddress: bigIntToHex(entry.contractAddress), | ||
nonce: entry.nonce, | ||
numberOfStorageUpdates: entry.numberOfStorageUpdates, | ||
newClassHash: bigIntToHex(entry.newClassHash), | ||
storageUpdates: entry.storageUpdates.map((storageUpdage) => ({ | ||
key: bigIntToHex(storageUpdage.key), | ||
value: bigIntToHex(storageUpdage.value), | ||
})), | ||
}; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type DecodableRollup = "STARKNET"; | ||
|
||
export type BlobDecoderFn<R> = (blobData: string) => R; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function bigIntToHex(value: bigint | string): string { | ||
const value_ = typeof value === "string" ? BigInt(value) : value; | ||
|
||
return `0x${value_.toString(16)}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "@blobscan/tsconfig/base.production.json", | ||
"include": ["src/**/*.ts", "test/**/*.ts", "vitest.config.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { defineProject } from "vitest/config"; | ||
|
||
import { sharedProjectConfig } from "../../vitest.shared"; | ||
|
||
export default defineProject(sharedProjectConfig); |