Skip to content
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

feat: Ownable2Step #352

Merged
merged 19 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions .github/workflows/test.yml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggonzalez94 we should have consistent formatting in our *.yml files. You can change it everywhere or revert this change in this file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It got automatically formatted, what plugin or extension are you using so that I can use the same for md? :)

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ permissions:
contents: read
on:
push:
branches: [ main, release/* ]
branches: [main, release/*]
paths-ignore:
- "**.md"
- "**.adoc"
Expand All @@ -32,7 +32,7 @@ jobs:
matrix:
# Run on stable and beta to ensure that tests won't break on the next
# version of the rust toolchain.
toolchain: [ stable, beta ]
toolchain: [stable, beta]
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -44,6 +44,11 @@ jobs:
toolchain: ${{ matrix.toolchain }}
rustflags: ""

- name: "Install nextest"
uses: taiki-e/install-action@v2
with:
tool: cargo-nextest

- name: Cargo generate-lockfile
# Enable this ci template to run regardless of whether the lockfile is
# checked in or not.
Expand All @@ -52,7 +57,7 @@ jobs:

# https://twitter.com/jonhoo/status/1571290371124260865
- name: Run unit tests
run: cargo test --locked --features std --all-targets
run: cargo nextest run --locked --features std --all-targets

# https://github.com/rust-lang/cargo/issues/6669
- name: Run doc tests
Expand All @@ -64,7 +69,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ macos-latest ]
os: [macos-latest]
# Windows fails because of `stylus-proc`.
# os: [macos-latest, windows-latest]
steps:
Expand All @@ -78,12 +83,17 @@ jobs:
toolchain: stable
rustflags: ""

- name: "Install nextest"
uses: taiki-e/install-action@v2
with:
tool: cargo-nextest

- name: Cargo generate-lockfile
if: hashFiles('Cargo.lock') == ''
run: cargo generate-lockfile

- name: Run unit tests
run: cargo test --locked --features std --all-targets
run: cargo nextest run --locked --features std --all-targets
coverage:
# Use llvm-cov to build and collect coverage and outputs in a format that
# is compatible with codecov.io.
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"examples/basic/token",
"examples/basic/script",
"examples/ecdsa",
"examples/ownable-two-step",
"examples/safe-erc20",
"benches",
]
Expand All @@ -36,6 +37,7 @@ default-members = [
"examples/safe-erc20",
"examples/merkle-proofs",
"examples/ownable",
"examples/ownable-two-step",
"examples/access-control",
"examples/basic/token",
"examples/ecdsa",
Expand Down
1 change: 1 addition & 0 deletions contracts/src/access/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Contracts implementing access control mechanisms.
pub mod control;
pub mod ownable;
pub mod ownable_two_step;
41 changes: 24 additions & 17 deletions contracts/src/access/ownable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use alloy_primitives::Address;
use alloy_sol_types::sol;
use stylus_sdk::{
call::MethodError,
evm, msg,
stylus_proc::{public, sol_storage, SolidityError},
};
Expand Down Expand Up @@ -45,6 +46,12 @@ pub enum Error {
InvalidOwner(OwnableInvalidOwner),
}

impl MethodError for Error {
fn encode(self) -> alloc::vec::Vec<u8> {
self.into()
}
}

sol_storage! {
/// State of an `Ownable` contract.
pub struct Ownable {
Expand All @@ -60,23 +67,6 @@ impl Ownable {
self._owner.get()
}

/// Checks if the [`msg::sender`] is set as the owner.
///
/// # Errors
///
/// If called by any account other than the owner, then the error
/// [`Error::UnauthorizedAccount`] is returned.
pub fn only_owner(&self) -> Result<(), Error> {
let account = msg::sender();
if self.owner() != account {
return Err(Error::UnauthorizedAccount(
OwnableUnauthorizedAccount { account },
));
}

Ok(())
}

/// Transfers ownership of the contract to a new account (`new_owner`). Can
/// only be called by the current owner.
///
Expand Down Expand Up @@ -124,6 +114,23 @@ impl Ownable {
}

impl Ownable {
/// Checks if the [`msg::sender`] is set as the owner.
bidzyyys marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Errors
///
/// If called by any account other than the owner, then the error
/// [`Error::UnauthorizedAccount`] is returned.
pub fn only_owner(&self) -> Result<(), Error> {
let account = msg::sender();
if self.owner() != account {
return Err(Error::UnauthorizedAccount(
OwnableUnauthorizedAccount { account },
));
}

Ok(())
}

/// Transfers ownership of the contract to a new account (`new_owner`).
/// Internal function without access restriction.
///
Expand Down
Loading
Loading