Skip to content

Commit 4d5b408

Browse files
authored
docs: add docs for starknet-core (#636)
1 parent c2a1d43 commit 4d5b408

File tree

17 files changed

+584
-34
lines changed

17 files changed

+584
-34
lines changed

starknet-core/src/chain_id.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use starknet_types_core::felt::Felt;
22

3+
/// The chain identifier for Starknet Mainnet. A Cairo short string encoding of `SN_MAIN`.
34
pub const MAINNET: Felt = Felt::from_raw([
45
502562008147966918,
56
18446744073709551615,
67
18446744073709551615,
78
17696389056366564951,
89
]);
910

11+
/// The chain identifier for Starknet Goerli. A Cairo short string encoding of `SN_GOERLI`.
1012
#[deprecated = "The Goerli testnet has been shutdown"]
1113
pub const TESTNET: Felt = Felt::from_raw([
1214
398700013197595345,
@@ -15,6 +17,7 @@ pub const TESTNET: Felt = Felt::from_raw([
1517
3753493103916128178,
1618
]);
1719

20+
/// The chain identifier for Starknet Goerli 2. A Cairo short string encoding of `SN_GOERLI2`.
1821
#[deprecated = "The Goerli testnet has been shutdown"]
1922
pub const TESTNET2: Felt = Felt::from_raw([
2023
33650220878420990,
@@ -23,6 +26,7 @@ pub const TESTNET2: Felt = Felt::from_raw([
2326
1663542769632127759,
2427
]);
2528

29+
/// The chain identifier for Starknet Sepolia. A Cairo short string encoding of `SN_SEPOLIA`.
2630
pub const SEPOLIA: Felt = Felt::from_raw([
2731
507980251676163170,
2832
18446744073709551615,

starknet-core/src/crypto.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ use starknet_crypto::{rfc6979_generate_k, sign, verify, SignError, VerifyError};
66
mod errors {
77
use core::fmt::{Display, Formatter, Result};
88

9+
/// Errors when performing ECDSA [`sign`](fn.ecdsa_sign) operations.
910
#[derive(Debug)]
1011
pub enum EcdsaSignError {
12+
/// The message hash is not in the range of `[0, 2^251)`.
1113
MessageHashOutOfRange,
1214
}
1315

1416
#[derive(Debug)]
17+
/// Errors when performing ECDSA [`verify`](fn.ecdsa_verify) operations.
1518
pub enum EcdsaVerifyError {
19+
/// The message hash is not in the range of `[0, 2^251)`.
1620
MessageHashOutOfRange,
21+
/// The public key is not a valid point on the STARK curve.
1722
InvalidPublicKey,
23+
/// The `r` value is not in the range of `[0, 2^251)`.
1824
SignatureROutOfRange,
25+
/// The `s` value is not in the range of `[0, 2^251)`.
1926
SignatureSOutOfRange,
2027
}
2128

@@ -46,6 +53,16 @@ mod errors {
4653
}
4754
pub use errors::{EcdsaSignError, EcdsaVerifyError};
4855

56+
/// Computes the Pedersen hash of a list of [`Felt`].
57+
///
58+
/// The hash is computed by starting with `0`, hashing it recursively against all elements in
59+
/// the list, and finally also hashing against the length of the list.
60+
///
61+
/// For example, calling `compute_hash_on_elements([7, 8])` would return:
62+
///
63+
/// ```markdown
64+
/// pedersen_hash(pedersen_hash(pedersen_hash(0, 7)), 8), 2)
65+
/// ```
4966
pub fn compute_hash_on_elements<'a, ESI, II>(data: II) -> Felt
5067
where
5168
ESI: ExactSizeIterator<Item = &'a Felt>,
@@ -62,6 +79,8 @@ where
6279
pedersen_hash(&current_hash, &data_len)
6380
}
6481

82+
/// Signs a hash using deterministic ECDSA on the STARK curve. The signature returned can be used
83+
/// to recover the public key.
6584
pub fn ecdsa_sign(
6685
private_key: &Felt,
6786
message_hash: &Felt,
@@ -89,6 +108,7 @@ pub fn ecdsa_sign(
89108
}
90109
}
91110

111+
/// Verified an ECDSA signature on the STARK curve.
92112
pub fn ecdsa_verify(
93113
public_key: &Felt,
94114
message_hash: &Felt,

starknet-core/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
//! Core data types and utilities for Starknet.
2+
3+
#![deny(missing_docs)]
14
#![cfg_attr(not(feature = "std"), no_std)]
25
#![allow(clippy::comparison_chain)]
3-
#![doc = include_str!("../README.md")]
46

7+
/// Module containing custom serialization/deserialization implementations.
58
pub mod serde;
69

10+
/// Module containing core types for representing objects in Starknet.
711
pub mod types;
812

13+
/// High-level utilities for cryptographic operations used in Starknet.
914
pub mod crypto;
1015

16+
/// Utilities for performing commonly used algorithms in Starknet.
1117
pub mod utils;
1218

19+
/// Chain IDs for commonly used public Starknet networks.
1320
pub mod chain_id;
1421

1522
extern crate alloc;

starknet-core/src/serde/byte_array.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// Serializing and deserializing [`Vec<u8>`] with base64 encoding.
12
pub mod base64 {
23
use alloc::{fmt::Formatter, format, vec::*};
34

@@ -6,6 +7,7 @@ pub mod base64 {
67

78
struct Base64Visitor;
89

10+
/// Serializes [`Vec<u8>`] as base64 string.
911
pub fn serialize<S, T>(value: T, serializer: S) -> Result<S::Ok, S::Error>
1012
where
1113
S: Serializer,
@@ -14,6 +16,7 @@ pub mod base64 {
1416
serializer.serialize_str(&STANDARD.encode(value.as_ref()))
1517
}
1618

19+
/// Deserializes [`Vec<u8>`] from base64 string.
1720
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
1821
where
1922
D: Deserializer<'de>,

starknet-core/src/serde/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
/// Custom serialization/deserialization implementations for [`Vec<u8>`].
12
pub mod byte_array;
23

4+
/// Custom serialization/deserialization implementations for [`Felt`].
35
pub mod unsigned_field_element;
46

57
pub(crate) mod num_hex;

starknet-core/src/serde/unsigned_field_element.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ use starknet_types_core::felt::Felt;
1212
const PRIME: U256 =
1313
U256::from_be_hex("0800000000000011000000000000000000000000000000000000000000000001");
1414

15+
/// Serialize/deserialize [`Felt`] as hex strings. For use with `serde_with`.
1516
#[derive(Debug)]
1617
pub struct UfeHex;
1718

19+
/// Serialize/deserialize [`Option<Felt>`] as hex strings. For use with `serde_with`.
1820
#[derive(Debug)]
1921
pub struct UfeHexOption;
2022

23+
/// Serialize/deserialize [`Option<Felt>`] as hex strings in a pending block hash context. For use
24+
/// with `serde_with`.
2125
#[derive(Debug)]
2226
pub struct UfePendingBlockHash;
2327

starknet-core/src/types/codegen.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// https://github.com/xJonathanLEI/starknet-jsonrpc-codegen
44

55
// Code generated with version:
6-
// https://github.com/xJonathanLEI/starknet-jsonrpc-codegen#fbd3aed2a08d6b29328e87ee0bbfb7e80f7051b0
6+
// https://github.com/xJonathanLEI/starknet-jsonrpc-codegen#f1278dfb2ae57d319093421c038f6ec7a3dfba2f
77

88
// These types are ignored from code generation. Implement them manually:
99
// - `RECEIPT_BLOCK`
@@ -24,6 +24,7 @@
2424
// - `TXN`
2525
// - `TXN_RECEIPT`
2626

27+
#![allow(missing_docs)]
2728
#![allow(clippy::doc_markdown)]
2829
#![allow(clippy::missing_const_for_fn)]
2930

0 commit comments

Comments
 (0)