Skip to content

Commit

Permalink
feat: hashing and declaring jsonrpc-formatted classes
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Aug 20, 2024
1 parent 0e9b706 commit 56e044b
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 206 deletions.
17 changes: 4 additions & 13 deletions book/src/declaring-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,15 @@ To come up with the _CASM hash_, Starkli compiles the Sierra class provided unde

While the normal process of declaring a class involves getting the compiled contract artifact from the compiler and following the steps documented above, it's sometimes helpful to _redeclare_ a class you found from another network.

Currently, Starkli does _not_ support directly declaring the class file fetched from a network. So **this would fail**:
To do so, simply run `starkli declare` on any class fetched from `class-at` or `class-by-hash` commands. For example:

```console
starkli class-by-hash --network sepolia SOME_CLASS_HASH_HERE > class.json
starkli declare --network mainnet ./class.json
```

The above would fail as the JSON-RPC class format is different from compiler output. The fix is simple: just add `--parse` to the first command to instruct Starkli to recover the JSON-RPC-formatted class back into the original compiler output format:

```console
starkli class-by-hash --network sepolia SOME_CLASS_HASH_HERE --parse > class.json
starkli declare --network mainnet ./class.json
```

Now the commands should execute successfully.

> ℹ️ **Note**
> 💡 **Tips**
>
> _Technically_, Starkli could support declaring JSON-RPC-formatted class files. It's just that the current Starkli implementation does not come with that capability. This might change in the future. For now you'll have to use the `--parse` flag when fetching the class.
> While Starkli is capable of handling its declaration, the class format retrieved from JSON-RPC is different from the original compiler output. To obtain the format identical to what comes out of the compiler, use the `--parse` flag when retrieving the class (e.g. in a `class-by-hash` command).
>
> While `--parse` should work just fine most of the time, unfortunately, certain exotic classes might not be parsable. In these rare cases, Starkli cannot redeclare them until the aformentioned capability is implemented, which would remove the need of parsing.
> Note that while `--parse` should work just fine most of the time, unfortunately, certain exotic classes might not be parsable. Nevertheless, Starkli would still be able to redeclare the format without parsing.
17 changes: 14 additions & 3 deletions src/subcommands/class_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use std::path::PathBuf;

use anyhow::Result;
use clap::Parser;
use starknet::core::types::contract::{legacy::LegacyContractClass, CompiledClass, SierraClass};
use starknet::core::types::{
contract::{legacy::LegacyContractClass, CompiledClass, SierraClass},
CompressedLegacyContractClass, FlattenedSierraClass,
};

use crate::path::ExpandedPathbufParser;
use crate::{path::ExpandedPathbufParser, utils::parse_compressed_legacy_class};

#[derive(Debug, Parser)]
pub struct ClassHash {
Expand All @@ -29,9 +32,17 @@ impl ClassHash {
{
class.class_hash()?
} else if let Ok(class) =
serde_json::from_reader::<_, LegacyContractClass>(std::fs::File::open(self.file)?)
serde_json::from_reader::<_, LegacyContractClass>(std::fs::File::open(&self.file)?)
{
class.class_hash()?
} else if let Ok(class) =
serde_json::from_reader::<_, FlattenedSierraClass>(std::fs::File::open(&self.file)?)
{
class.class_hash()
} else if let Ok(class) = serde_json::from_reader::<_, CompressedLegacyContractClass>(
std::fs::File::open(self.file)?,
) {
parse_compressed_legacy_class(class)?.class_hash()?
} else {
anyhow::bail!("failed to parse contract artifact");
};
Expand Down
Loading

0 comments on commit 56e044b

Please sign in to comment.