Starknet 백서

Starknet

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 73쪽 중 9쪽

ll} fn new() -> HashState ``` #### Examples ```rust theme={null} use core::poseidon::PoseidonTrait; let mut state = PoseidonTrait::new(); ``` # core::poseidon::PoseidonTrait Source: https://docs.starknet.io/build/corelib/core-poseidon-PoseidonTrait ## Signature ```rust theme={null} pub trait PoseidonTrait ``` ## Trait functions ### new Creates an initial state with all fields set to 0. #### Signature ```rust theme={null} fn new() -> HashState ``` #### Examples ```rust theme={null} use core::poseidon::PoseidonTrait; let mut state = PoseidonTrait::new(); ``` # core::poseidon::hades_permutation Source: https://docs.starknet.io/build/corelib/core-poseidon-hades_permutation ## Signature ```rust theme={null} pub extern fn hades_permutation(s0: felt252, s1: felt252, s2: felt252) -> (felt252, felt252, felt252) implicits(Poseidon) nopanic; ``` # core::poseidon::poseidon_hash_span Source: https://docs.starknet.io/build/corelib/core-poseidon-poseidon_hash_span Computes the Poseidon hash on the given span input. Applies the sponge construction to digest many elements. To distinguish between use cases, the capacity element is initialized to 0. To distinguish between different input sizes always pads with 1, and possibly with another 0 to complete to an even-sized input. ## Signature ```rust theme={null} pub fn poseidon_hash_span(mut span: Span) -> felt252 ``` ## Examples ```rust theme={null} let span = [1, 2].span(); let hash = poseidon_hash_span(span); assert!(hash == 0x0371cb6995ea5e7effcd2e174de264b5b407027a75a231a70c2c8d196107f0e7); ``` # core::qm31::QM31Trait Source: https://docs.starknet.io/build/corelib/core-qm31-QM31Trait ## Signature ```rust theme={null} pub trait QM31Trait ``` ## Trait functions ### new Returns a new `qm31` composed of the given parts. #### Signature ```rust theme={null} fn new( w0: BoundedInt, w1: BoundedInt, w2: BoundedInt, w3: BoundedInt, ) -> qm31 ``` ### unpack Returns the parts of the given `qm31` as `m31`s. #### Signature ```rust theme={null} fn unpack(self: qm31) -> [BoundedInt; 4] ``` # core::qm31::m31_ops Source: https://docs.starknet.io/build/corelib/core-qm31-m31_ops Additional `m31` actions for specific implementations based on `qm31` opcode. ## [Extern functions](./core-qm31-m31_ops-extern_functions) | | | | :- | :- | | [m31\_add](./core-qm31-m31_ops-m31_add) | Addition of `m31` s in field. | | [m31\_div](./core-qm31-m31_ops-m31_div) | Division of `m31` s in field. | | [m31\_mul](./core-qm31-m31_ops-m31_mul) | Multiplication of `m31` s in field. | | [m31\_sub](./core-qm31-m31_ops-m31_sub) | Subtraction of `m31` s in field. | *** # Re-exports: * ### Extern functions | | | | :- | :- | | [m31\_add](./core-qm31-m31_ops-m31_add) | Addition of `m31` s in field. | | [m31\_div](./core-qm31-m31_ops-m31_div) | Division of `m31` s in field. | | [m31\_mul](./core-qm31-m31_ops-m31_mul) | Multiplication of `m31` s in field. | | [m31\_sub](./core-qm31-m31_ops-m31_sub) | Subtraction of `m31` s in field. | # core::qm31::m31_ops::m31_add Source: https://docs.starknet.io/build/corelib/core-qm31-m31_ops-m31_add Addition of `m31`s in field. ## Signature ```rust theme={null} extern fn m31_add(a: BoundedInt, b: BoundedInt) -> BoundedInt nopanic; ``` # core::qm31::m31_ops::m31_div Source: https://docs.starknet.io/build/corelib/core-qm31-m31_ops-m31_div Division of `m31`s in field. ## Signature ```rust theme={null} extern fn m31_div(a: BoundedInt, b: NonZero>) -> BoundedInt nopanic; ``` # core::qm31::m31_ops::m31_mul Source: https://docs.starknet.io/build/corelib/core-qm31-m31_ops-m31_mul Multiplication of `m31`s in field. ## Signature ```rust theme={null} extern fn m31_mul(a: BoundedInt, b: BoundedInt) -> BoundedInt nopanic; ``` # core::qm31::m31_ops::m31_sub Source: https://docs.starknet.io/build/corelib/core-qm31-m31_ops-m31_sub Subtraction of `m31`s in field. ## Signature ```rust theme={null} extern fn m31_sub(a: BoundedInt, b: BoundedInt) -> BoundedInt nopanic; ``` # core::qm31::qm31 Source: https://docs.starknet.io/build/corelib/core-qm31-qm31 The `qm31` type, defining an extension field over 4 `m31`s. ## Signature ```rust theme={null} pub extern type qm31; ``` # core::qm31::qm31_const Source: https://docs.starknet.io/build/corelib/core-qm31-qm31_const Returns a `qm31` given its values as constants. ## Signature ```rust theme={null} pub extern fn qm31_const() -> qm31 nopanic; ``` # core::serde::Serde Source: https://docs.starknet.io/build/corelib/core-serde-Serde A trait that allows for serializing and deserializing values of any type. The `Serde` trait defines two core operations: * `serialize`: Converts a value into a sequence of `felt252`s * `deserialize`: Reconstructs a value from a sequence of `felt252`s ## Signature ```rust theme={null} pub trait Serde ``` ## Examples ## Simple Types (u8, u16, u32, u64, u128) Simple types are serialized into a single `felt252`: ```rust theme={null} let value: u8 = 42; let mut output: Array = array![]; value.serialize(ref output); assert!(output == array![42]); // Single value ``` ## Compound Types (u256) Compound types may be serialized into multiple `felt252` values: ```rust theme={null} let value: u256 = u256 { low: 1, high: 2 }; let mut output: Array = array![]; value.serialize(ref output); assert!(output == array![1, 2]); // Two `felt252`s: low and high ``` # Implementing `Serde` ## Using the `Derive` Macro In most cases, you can use the `#[derive(Serde)]` attribute to automatically generate the implementation for your type: ```rust theme={null} #[derive(Serde)] struct Point { x: u32, y: u32 } ``` ## Manual Implementation Should you need to customize the serialization behavior for a type in a way that derive does not support, you can implement the `Serde` yourself: ```rust theme={null} impl PointSerde of Serde { fn serialize(self: @Point, ref output: Array) { output.append((*self.x).into()); output.append((*self.y).into()); } fn deserialize(ref serialized: Span) -> Option { let x = (*serialized.pop_front()?).try_into()?; let y = (*serialized.pop_front()?).try_into()?; Some(Point { x, y }) } } ``` ## Trait functions ### serialize Serializes a value into a sequence of `felt252`s. #### Signature ```rust theme={null} fn serialize(self: @T, ref output: Array) ``` #### Examples ```rust theme={null} let value: u256 = 1; let mut serialized: Array = array![]; value.serialize(ref serialized); assert!(serialized == array![1, 0]); // `serialized` contains the [low, high] parts of the `u256` value ``` ### deserialize Deserializes a value from a sequence of `felt252`s. If the value cannot be deserialized, returns `None`. #### Signature ```rust theme={null} fn deserialize(ref serialized: Span) -> Option ``` #### Examples ```rust theme={null} let mut serialized: Span = array![1, 0].span(); let value: u256 = Serde::deserialize(ref serialized).unwrap(); assert!(value == 1); ``` # core::serde::into_felt252_based Source: https://docs.starknet.io/build/corelib/core-serde-into_felt252_based # core::sha256::compute_sha256_byte_array Source: https://docs.starknet.io/build/corelib/core-sha256-compute_sha256_byte_array Computes the SHA-256 hash of the input `ByteArray`. ## Signature ```rust theme={null} pub fn compute_sha256_byte_array(arr: ByteArray) -> [u32; 8] ``` ## Examples ```rust theme={null} use core::sha256::compute_sha256_byte_array; ``` # core::sha256::compute_sha256_u32_array Source: https://docs.starknet.io/build/corelib/core-sha256-compute_sha256_u32_array Computes the SHA-256 hash of an array of 32-bit words. ## Signature ```rust theme={null} pub fn compute_sha256_u32_array( mut input: Array, last_input_word: u32, last_input_num_bytes: u32, ) -> [u32; 8] ``` #### Arguments * `input` - An array of `u32` values to hash * `last_input_word` - The final word when input is not word-aligned * `last_input_num_bytes` - Number of bytes in the last input word (must be less than 4) #### Returns * The SHA-256 hash of the `input array` + `last_input_word` as big endian ## Examples ```rust theme={null} use core::sha256::compute_sha256_u32_array; let hash = compute_sha256_u32_array(array![0x68656c6c], 0x6f, 1); assert!(hash == [0x2cf24dba, 0x5fb0a30e, 0x26e83b2a, 0xc5b9e29e, 0x1b161e5c, 0x1fa7425e, 0x73043362, 0x938b9824]); ``` # core::starknet::SyscallResult Source: https://docs.starknet.io/build/corelib/core-starknet-SyscallResult The `Result` type for a syscall. ## Signature ```rust theme={null} pub type SyscallResult = Result>; ``` # core::starknet::SyscallResultTrait Source: https://docs.starknet.io/build/corelib/core-starknet-SyscallResultTrait Trait for handling syscall results. ## Signature ```rust theme={null} pub trait SyscallResultTrait ``` ## Trait functions ### unwrap\_syscall Unwraps a syscall result, yielding the content of an `Ok`. #### Panics Panics with the syscall error message if the value is an `Err`. #### Examples ```rust theme={null} let result = starknet::syscalls::get_execution_info_v2_syscall(); let info = result.unwrap_syscall(); ``` #### Signature ```rust theme={null} fn unwrap_syscall(self: SyscallResult) -> T ``` # core::starknet::System Source: https://docs.starknet.io/build/corelib/core-starknet-System ## Signature ```rust theme={null} pub extern type System; ``` # core::starknet::VALIDATED Source: https://docs.starknet.io/build/corelib/core-starknet-VALIDATED The expected return value of the `__validate__` function in account contracts. This constant is used to indicate that a transaction validation was successful. Account contracts must return this value from their `__validate__` function to signal that the transaction should proceed. ## Signature ```rust theme={null} pub const VALIDATED: felt252 = 370462705988; ``` # core::starknet::account::AccountContract Source: https://docs.starknet.io/build/corelib/core-starknet-account-AccountContract A trait for account contracts that support class declarations (only `__validate__` and `__execute__` are mandatory for an account). This trait assumes that the calldata for invoke transactions is `Array`. This is the network standard following SNIP6. It is not enforced by Starknet, but deviating from the standard interface may lead to incompatibility with standard tooling. ## Signature ```rust theme={null} pub trait AccountContract ``` ## Trait functions ### **validate\_declare** An entry point that is called to check if the account is willing to pay for the declaration of the class with the given hash. The entry point should return `starknet::VALIDATED` if the account is willing to pay for the declaration. #### Signature ```rust theme={null} fn __validate_declare__( self: @TContractState, class_hash: felt252, ) -> felt252 ``` ### **validate** An entry point that is called to check if the account is willing to pay for executing a given set of calls. The entry point should return `starknet::VALIDATED` if the account is willing to pay for the execution, in which case `__execute__` will be called on the same set of calls. #### Signature ```rust theme={null} fn __validate__( ref self: TContractState, calls: Array, ) -> felt252 ``` ### **execute** An entry point that is called to execute a given set of calls. This entry point should block the deprecated v0 invoke transactions as they do not go through the `__validate__` entry point. #### Signature ```rust theme={null} fn __execute__( ref self: TContractState, calls: Array, ) -> Array> ``` # core::starknet::account::AccountContractDispatcher Source: https://docs.starknet.io/build/corelib/core-starknet-account-AccountContractDispatcher ## Signature ```rust theme={null} #[derive(Copy, Drop, Serde)] pub struct AccountContractDispatcher { pub contract_address: ContractAddress, } ``` ## Members ### contract\_address #### Signature ```rust theme={null} pub contract_address: ContractAddress ``` # core::starknet::account::AccountContractDispatcherTrait Source: https://docs.starknet.io/build/corelib/core-starknet-account-AccountContractDispatcherTrait ## Signature ```rust theme={null} pub trait AccountContractDispatcherTrait ``` ## Trait functions ### **validate\_declare** An entry point that is called to check if the account is willing to pay for the declaration of the class with the given hash. The entry point should return `starknet::VALIDATED` if the account is willing to pay for the declaration. #### Signature ```rust theme={null} fn __validate_declare__(self: T, class_hash: felt252) -> felt252 ``` ### **validate** An entry point that is called to check if the account is willing to pay for executing a given set of calls. The entry point should return `starknet::VALIDATED` if the account is willing to pay for the execution, in which case `__execute__` will be called on the same set of calls. #### Signature ```rust theme={null} fn __validate__(self: T, calls: Array) -> felt252 ``` ### **execute** An entry point that is called to execute a given set of calls. This entry point should block the deprecated v0 invoke transactions as they do not go through the `__validate__` entry point. #### Signature ```rust theme={null} fn __execute__(self: T, calls: Array) -> Array> ``` # core::starknet::account::AccountContractLibraryDispatcher Source: https://docs.starknet.io/build/corelib/core-starknet-account-AccountContractLibraryDispatcher ## Signature ```rust theme={null} #[derive(Copy, Drop, Serde)] pub struct AccountContractLibraryDispatcher { pub class_hash: ClassHash, } ``` ## Members ### class\_hash #### Signature ```rust theme={null} pub class_hash: ClassHash ``` # core::starknet::account::AccountContractSafeDispatcher Source: https://docs.starknet.io/build/corelib/core-starknet-account-AccountContractSafeDispatcher ## Signature ```rust theme={null} #[derive(Copy, Drop, Serde)] pub struct AccountContractSafeDispatcher { pub contract_address: ContractAddress, } ``` ## Members ### contract\_address #### Signature ```rust theme={null} pub contract_address: ContractAddress ``` # core::starknet::account::AccountContractSafeDispatcherTrait Source: https://docs.starknet.io/build/corelib/core-starknet-account-AccountContractSafeDispatcherTrait ## Signature ```rust theme={null} pub trait AccountContractSafeDispatcherTrait ``` ## Trait functions ### **validate\_declare** An entry point that is called to check if the account is willing to pay for the declaration of the class with the given hash. The entry point should return `starknet::VALIDATED` if the account is willing to pay for the declaration. #### Signature ```rust theme={null} fn __validate_declare__(self: T, class_hash: felt252) -> Result> ``` ### **validate** An entry point that is called to check if the account is willing to pay for executing a given set of calls. The entry point should return `starknet::VALIDATED` if the account is willing to pay for the execution, in which case `__execute__` will be called on the same set of calls. #### Signature ```rust theme={null} fn __validate__(self: T, calls: Array) -> Result> ``` ### **execute** An entry point that is called to execute a given set of calls. This entry point should block the deprecated v0 invoke transactions as they do not go through the `__validate__` entry point. #### Signature ```rust theme={null} fn __execute__(self: T, calls: Array) -> Result>, Array> ``` # core::starknet::account::AccountContractSafeLibraryDispatcher Source: https://docs.starknet.io/build/corelib/core-starknet-account-AccountContractSafeLibraryDispatcher ## Signature ```rust theme={null} #[derive(Copy, Drop, Serde)] pub struct AccountContractSafeLibraryDispatcher { pub class_hash: ClassHash, } ``` ## Members ### class\_hash #### Signature ```rust theme={null} pub class_hash: ClassHash ``` # core::starknet::account::Call Source: https://docs.starknet.io/build/corelib/core-starknet-account-Call A struct representing a call to a contract. ## Signature ```rust theme={null} #[derive(Drop, Copy, Serde, Debug)] pub struct Call { pub to: ContractAddress, pub selector: felt252, pub calldata: Span, } ``` ## Members ### to The address of the contract to call. #### Signature ```rust theme={null} pub to: ContractAddress ``` ### selector The entry point selector in the called contract. #### Signature ```rust theme={null} pub selector: felt252 ``` ### calldata The calldata to pass to entry point. #### Signature ```rust theme={null} pub calldata: Span ``` # core::starknet::class_hash::ClassHash Source: https://docs.starknet.io/build/corelib/core-starknet-class_hash-ClassHash Represents a Starknet contract class hash. The value range of this type is `[0, 2**251)`. ## Signature ```rust theme={null} pub extern type ClassHash; ``` # core::starknet::class_hash::class_hash_const Source: https://docs.starknet.io/build/corelib/core-starknet-class_hash-class_hash_const Returns a `ClassHash` given a `felt252` value. ## Signature ```rust theme={null} pub extern fn class_hash_const() -> ClassHash nopanic; ``` ## Examples ```rust theme={null} use starknet::class_hash::class_hash_const; let class_hash = class_hash_const::(); ``` # core::starknet::contract_address::ContractAddress Source: https://docs.starknet.io/build/corelib/core-starknet-contract_address-ContractAddress Represents a Starknet contract address. The value range of this type is `[0, 2**251)`. ## Signature ```rust theme={null} pub extern type ContractAddress; ``` # core::starknet::eth_address::EthAddress Source: https://docs.starknet.io/build/corelib/core-starknet-eth_address-EthAddress An Ethereum address, 20 bytes in length. ## Signature ```rust theme={null} #[derive(Copy, Drop, Hash, PartialEq)] pub struct EthAddress { address: felt252, } ``` # core::starknet::eth_signature::is_eth_signature_valid Source: https://docs.starknet.io/build/corelib/core-starknet-eth_signature-is_eth_signature_valid Validates an Ethereum signature against a message hash and Ethereum address. Similar to `verify_eth_signature` but returns a `Result` instead of panicking. Also verifies that `r` and `s` components of the signature are in the range `[1, N)`, where N is the size of the curve. ## Signature ```rust theme={null} pub fn is_eth_signature_valid( msg_hash: u256, signature: Signature, eth_address: EthAddress, ) -> Result ``` #### Arguments * `msg_hash` - The 32-byte hash of the message that was signed * `signature` - The Ethereum signature containing `r`, `s` components and `y_parity` * `eth_address` - The expected Ethereum address of the signer #### Returns Returns `Ok(())` if the signature is valid, or `Err(felt252)` containing an error message if invalid. ## Examples ```rust theme={null} use starknet::eth_address::EthAddress; use starknet::eth_signature::is_eth_signature_valid; use starknet::secp256_trait::Signature; let msg_hash = 0xe888fbb4cf9ae6254f19ba12e6d9af54788f195a6f509ca3e934f78d7a71dd85; let r = 0x4c8e4fbc1fbb1dece52185e532812c4f7a5f81cf3ee10044320a0d03b62d3e9a; let s = 0x4ac5e5c0c0e8a4871583cc131f35fb49c2b7f60e6a8b84965830658f08f7410c; let y_parity = true; let eth_address: EthAddress = 0x767410c1bb448978bd42b984d7de5970bcaf5c43_u256 .try_into() .unwrap(); assert!(is_eth_signature_valid(msg_hash, Signature { r, s, y_parity }, eth_address).is_ok()); ``` # core::starknet::eth_signature::public_key_point_to_eth_address Source: https://docs.starknet.io/build/corelib/core-starknet-eth_signature-public_key_point_to_eth_address Converts a public key point to its corresponding Ethereum address. The Ethereum address is calculated by taking the Keccak-256 hash of the public key coordinates and taking the last 20 big-endian bytes. ## Signature ```rust theme={null} pub fn public_key_point_to_eth_address, +Secp256Trait, +Secp256PointTrait, >( public_key_point: Secp256Point, ) -> EthAddress ``` #### Arguments * `public_key_point` - A point on a secp256 curve representing a publ
프로젝트로 돌아가기