Skip to content

Commit

Permalink
add protection
Browse files Browse the repository at this point in the history
  • Loading branch information
dragos-rebegea committed Oct 18, 2024
1 parent f65df11 commit a1e1f79
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
44 changes: 41 additions & 3 deletions contracts/faucet/src/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
#[allow(unused_imports)]
use multiversx_sc::{derive_imports::*, imports::*};

pub type Signature<M> = ManagedByteArray<M, ED25519_SIGNATURE_BYTE_LEN>;
pub const ED25519_SIGNATURE_BYTE_LEN: usize = 64;

/// An empty contract. To be used as a template when starting a new contract from scratch.
#[multiversx_sc::contract]
pub trait Faucet {
#[init]
fn init(&self) {}

#[upgrade]
fn upgrade(&self) {}

#[only_owner]
#[payable("*")]
#[endpoint(deposit)]
Expand All @@ -21,7 +27,14 @@ pub trait Faucet {
}

#[endpoint(claim)]
fn claim(&self) {
fn claim(&self, signature: &Signature<Self::Api>) {
let caller = self.blockchain().get_caller();
let claimers_mapper = self.claimers();
require!(claimers_mapper.contains(&caller) == false, "Caller already claimed");

Check warning on line 33 in contracts/faucet/src/faucet.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/faucet/src/faucet.rs#L33

warning: equality checks against false can be replaced by a negation --> contracts/faucet/src/faucet.rs:33:18 | 33 | require!(claimers_mapper.contains(&caller) == false, "Caller already claimed"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!claimers_mapper.contains(&caller)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison = note: `#[warn(clippy::bool_comparison)]` on by default
Raw output
contracts/faucet/src/faucet.rs:33:18:w:warning: equality checks against false can be replaced by a negation
  --> contracts/faucet/src/faucet.rs:33:18
   |
33 |         require!(claimers_mapper.contains(&caller) == false, "Caller already claimed");
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!claimers_mapper.contains(&caller)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
   = note: `#[warn(clippy::bool_comparison)]` on by default


__END__

Check warning on line 33 in contracts/faucet/src/faucet.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/faucet/src/faucet.rs#L33

warning: equality checks against false can be replaced by a negation --> contracts/faucet/src/faucet.rs:33:18 | 33 | require!(claimers_mapper.contains(&caller) == false, "Caller already claimed"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!claimers_mapper.contains(&caller)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison = note: `#[warn(clippy::bool_comparison)]` on by default
Raw output
contracts/faucet/src/faucet.rs:33:18:w:warning: equality checks against false can be replaced by a negation
  --> contracts/faucet/src/faucet.rs:33:18
   |
33 |         require!(claimers_mapper.contains(&caller) == false, "Caller already claimed");
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!claimers_mapper.contains(&caller)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
   = note: `#[warn(clippy::bool_comparison)]` on by default


__END__

self.verify_signature(&caller, signature);
self.claimers().add(&caller);

let mut payments = ManagedVec::new();
for payment in self.payments().iter() {
payments.push(payment);
Expand All @@ -30,9 +43,34 @@ pub trait Faucet {
self.tx().to(ToCaller).with_multi_token_transfer(payments).transfer();
}

fn verify_signature(
&self,
caller: &ManagedAddress<Self::Api>,
signature: &Signature<Self::Api>,
) {
let mut data = ManagedBuffer::new();
let _ = caller.dep_encode(&mut data);

let signer = self.signer().get();
self.crypto().verify_ed25519(
signer.as_managed_buffer(),
&data,
signature.as_managed_buffer(),
);
}

#[only_owner]
#[endpoint(setSigner)]
fn set_signer(&self, signer: ManagedAddress<Self::Api>) {
self.signer().set(&signer);
}

#[storage_mapper("payments")]
fn payments(&self) -> VecMapper<EsdtTokenPayment<Self::Api>>;

#[upgrade]
fn upgrade(&self) {}
#[storage_mapper("signer")]
fn signer(&self) -> SingleValueMapper<ManagedAddress<Self::Api>>;

#[storage_mapper("claimers")]
fn claimers(&self) -> WhitelistMapper<ManagedAddress<Self::Api>>;
}
34 changes: 34 additions & 0 deletions contracts/faucet/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Code generated by the multiversx-sc build system. DO NOT EDIT.

# ##########################################
# ############## AUTO-GENERATED #############
# ##########################################

[package]
name = "faucet-wasm"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]

[profile.release]
codegen-units = 1
opt-level = "z"
lto = true
debug = false
panic = "abort"
overflow-checks = false

[profile.dev]
panic = "abort"

[dependencies.faucet]
path = ".."

[dependencies.multiversx-sc-wasm-adapter]
version = "0.53.2"

[workspace]
members = ["."]
29 changes: 29 additions & 0 deletions contracts/faucet/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Code generated by the multiversx-sc build system. DO NOT EDIT.

////////////////////////////////////////////////////
////////////////// AUTO-GENERATED //////////////////
////////////////////////////////////////////////////

// Init: 1
// Upgrade: 1
// Endpoints: 3
// Async Callback (empty): 1
// Total number of exported functions: 6

#![no_std]

multiversx_sc_wasm_adapter::allocator!();
multiversx_sc_wasm_adapter::panic_handler!();

multiversx_sc_wasm_adapter::endpoints! {
faucet
(
init => init
upgrade => upgrade
deposit => deposit
claim => claim
setSigner => set_signer
)
}

multiversx_sc_wasm_adapter::async_callback_empty! {}

0 comments on commit a1e1f79

Please sign in to comment.