-
Notifications
You must be signed in to change notification settings - Fork 60
feat: add Precompiles trait #689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xNeshi
wants to merge
12
commits into
main
Choose a base branch
from
precompiles-trait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
94b3534
feat: add Precompiles trait
0xNeshi 44e42be
docs: simplify comment
0xNeshi d7c03d7
Merge branch 'main' into precompiles-trait
0xNeshi e6e1a8f
docs: fix
0xNeshi 969e443
feat: add primitives module containing types
0xNeshi a79d783
docs: fix
0xNeshi a04420a
docs: fix
0xNeshi 8f4067d
Merge branch 'main' into precompiles-trait
bidzyyys 8158f82
Merge branch 'main' into precompiles-trait
bidzyyys cb2542e
docs: wording
0xNeshi 9da43e0
test: update ecdsa
0xNeshi 9fe3374
Merge branch 'main' into precompiles-trait
bidzyyys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,111 @@ | ||
//! `ArbOS` precompiles wrapper enabling easier invocation. | ||
use alloy_primitives::{Address, B256}; | ||
use primitives::ecrecover::Error; | ||
use stylus_sdk::prelude::*; | ||
|
||
use crate::utils::cryptography::ecdsa::recover; | ||
|
||
/// Precompile primitives. | ||
pub mod primitives { | ||
/// The ecrecover precompile primitives. | ||
/// | ||
/// This module provides the cryptographic primitives needed for the | ||
/// `ecrecover` precompile, which recovers the signer address from an | ||
/// ECDSA signature and message hash. | ||
/// | ||
/// Re-exports selected ECDSA types and constants specifically relevant | ||
/// to the ecrecover operation. | ||
pub mod ecrecover { | ||
pub use crate::utils::cryptography::ecdsa::{ | ||
ECDSAInvalidSignature, ECDSAInvalidSignatureS, EcRecoverData, | ||
Error, ECRECOVER_ADDR, SIGNATURE_S_UPPER_BOUND, | ||
}; | ||
} | ||
} | ||
|
||
/// Trait providing access to Arbitrum precompiles for Stylus contracts. | ||
/// | ||
/// This trait wraps complex precompile invocations to provide a clean, | ||
/// ergonomic interface for calling Arbitrum's built-in cryptographic functions | ||
/// and utilities from within Stylus smart contracts. | ||
/// | ||
/// Precompiles are pre-deployed contracts at fixed addresses that implement | ||
/// commonly used cryptographic operations and other utilities. They execute | ||
/// natively in the Arbitrum runtime for better performance compared to | ||
/// implementing these operations in contract code. | ||
/// | ||
/// See: <https://docs.arbitrum.io/build-decentralized-apps/precompiles/overview> | ||
/// | ||
/// # Usage | ||
/// | ||
/// Implement this trait for your contract storage type to gain access to | ||
/// precompile functionality: | ||
/// | ||
/// ```rust,ignore | ||
/// use openzeppelin_stylus::utils::cryptography::Precompiles; | ||
/// | ||
/// #[storage] | ||
/// #[entrypoint] | ||
/// struct MyContract { | ||
/// // your fields... | ||
/// } | ||
/// | ||
/// // The `Precompiles` trait is automatically implemented for all | ||
/// // contracts annotated with `#[entrypoint]` or that implement | ||
/// // `stylus_sdk::prelude::TopLevelStorage`. | ||
/// | ||
/// #[public] | ||
/// impl MyContract { | ||
/// fn verify_signature(&mut self, msg_hash: B256, sig: (u8, B256, B256)) -> Result<Address, Error> { | ||
/// let (v, r, s) = sig; | ||
/// self.ecrecover(msg_hash, v, r, s) | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// # Error Handling | ||
/// | ||
/// Precompile methods return `Result` types to handle both invalid inputs and | ||
/// precompile execution failures. Always handle these errors appropriately | ||
/// in your contract logic. | ||
pub trait Precompiles: TopLevelStorage { | ||
/// Returns the address that signed a hashed message (`hash`). | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `&mut self` - Write access to the contract's state. given address. | ||
/// * `hash` - Hash of the message. | ||
/// * `v` - `v` value from the signature. | ||
/// * `r` - `r` value from the signature. | ||
/// * `s` - `s` value from the signature. | ||
/// | ||
/// # Errors | ||
/// | ||
/// * [`Error::InvalidSignatureS`] - If the `s` value is grater than | ||
/// [`primitives::ecrecover::SIGNATURE_S_UPPER_BOUND`]. | ||
/// * [`Error::InvalidSignature`] - If the recovered address is | ||
/// [`Address::ZERO`]. | ||
/// | ||
/// # Panics | ||
/// | ||
/// * If the `ecrecover` precompile fails to execute. | ||
fn ecrecover( | ||
&mut self, | ||
hash: B256, | ||
v: u8, | ||
r: B256, | ||
s: B256, | ||
) -> Result<Address, Error>; | ||
} | ||
|
||
impl<T: TopLevelStorage> Precompiles for T { | ||
fn ecrecover( | ||
&mut self, | ||
hash: B256, | ||
v: u8, | ||
r: B256, | ||
s: B256, | ||
) -> Result<Address, Error> { | ||
recover(self, hash, v, r, s) | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't remove
utils::cryptography::ecdsa
for two reasons:The
mod primitives
module is used to make it clear to devs where to find relevant precompile-related types, constants, enums etc.In this specific case, the reason for this reexport is that it should be easier for the average Stylus developer to understand that the relevant ecrecover primitives are in the same module, i.e.
precompiles::primitives::ecrecover
, than that they should actually import them from a completely different moduleutils::cryptography::ecdsa
.