Skip to content

Commit

Permalink
Added to msg_sender to prelude and removed from swayfiles (FuelLabs#4483
Browse files Browse the repository at this point in the history
)

## Description
Fixes FuelLabs#4478

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Kin Chan <[email protected]>
  • Loading branch information
calldelegation and Kin Chan authored Apr 27, 2023
1 parent e99b788 commit af5f536
Show file tree
Hide file tree
Showing 32 changed files with 25 additions and 67 deletions.
1 change: 1 addition & 0 deletions docs/book/src/introduction/standard_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ The current version of the prelude lives in [`std::prelude`](https://github.com/
- [`std::revert::require`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw), a function that reverts the VM and logs a given value if the condition provided to it is `false`.
- [`std::revert::revert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw), a function that reverts the VM.
- [`std::logging::log`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/logging.sw), a function that logs arbitrary stack types.
- [`std::auth::msg_sender`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/auth.sw), a function that gets the `Identity` from which a call was made.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
contract;

// ANCHOR: identity
use std::auth::msg_sender;

storage {
owner: Option<Identity> = Option::None,
}

// ANCHOR_END: identity
// ANCHOR: abi
abi Ownership {
Expand Down
1 change: 0 additions & 1 deletion docs/reference/src/code/examples/wallet/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ abi Wallet {
// ANCHOR_END: abi
// ANCHOR: implementation
use std::{
auth::msg_sender,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
Expand Down
4 changes: 0 additions & 4 deletions docs/reference/src/code/operations/call_data/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
library;

// ANCHOR: import_sender
use std::auth::msg_sender;
// ANCHOR_END: import_sender

// ANCHOR: import_asset
use std::{call_frames::msg_asset_id, constants::BASE_ASSET_ID};
// ANCHOR_END: import_asset
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
contract;

// ANCHOR: initialization
use std::chain::auth::msg_sender;

storage {
// k = Identity, v = u64
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contract;

// ANCHOR: initialization
use std::{chain::auth::msg_sender, storage::StorageVec};
use std::storage::StorageVec;

storage {
// T = u64
Expand Down
12 changes: 8 additions & 4 deletions docs/reference/src/documentation/misc/prelude.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

The [prelude](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/prelude.sw) is a list of commonly used features from the [standard library](https://github.com/FuelLabs/sway/tree/master/sway-lib-std) which is automatically imported into every Sway program.

The prelude contains the following modules:
The prelude contains the following:

- [`Address`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/address.sw): A struct containing a `b256` value which represents the wallet address
- [`assert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw): A function that reverts if the condition provided is `false`
- [`ContractId`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/contract_id.sw) A struct containing a `b256` value which represents the ID of a contract
- [`Identity`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/identity.sw): An enum containing `Address` & `ContractID` structs
- [`Vec`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/vec.sw): A growable, heap-allocated vector
- [`StorageMap`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/storage.sw): A key-value mapping in contract storage
- [`Option`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/option.sw): An enum containing either some generic value `<T>` or an absence of that value
- [`Result`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/result.sw): An enum used to represent either a success or failure of an operation
- [`assert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw): A module containing
- `assert`: A function that reverts the VM if the condition provided to it is false
- `assert_eq`: A function that reverts the VM and logs its two inputs v1 and v2 if the condition v1 == v2 is false
- [`revert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw): A module containing
- `require`: A function that reverts and logs a given value if the condition is `false`
- `revert`: A function that reverts
- [`StorageMap`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/storage.sw): A key-value mapping in contract storage
- [`Vec`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/vec.sw): A growable, heap-allocated vector
- [`log`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/logging.sw): A function that logs arbitrary stack types
- [`msg_sender`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/auth.sw): A function that gets the Identity from which a call was made
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# Message Sender

The [standard library](https://github.com/FuelLabs/sway/tree/master/sway-lib-std) provides a function [`msg_sender()`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/auth.sw) which retrieves the [Identity](../namespace/identity.md) of the caller.
The standard [prelude](https://github.com/FuelLabs/sway/blob/master/docs/reference/src/documentation/misc/prelude.md) imports a function [`msg_sender()`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/auth.sw) automatically, which retrieves the [Identity](../namespace/identity.md) of the caller.

The identity can be used for a variety of reasons however a common application is access control i.e. restricting functionality for non-privileged users (non-admins).

## Example

To use `msg_sender()` we must import it from the standard library.

```sway
{{#include ../../../code/operations/call_data/src/lib.sw:import_sender}}
```

We can implement access control by specifying that only the owner can call a function.

In the following snippet we accomplish this by comparing the caller `msg_sender()` to the `OWNER`. If a regular user calls the function then it will revert otherwise it will continue to run when called by the `OWNER`.
Expand Down
2 changes: 0 additions & 2 deletions examples/cei_analysis/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ mod other_contract;

use other_contract::*;

use std::auth::msg_sender;

abi MyContract {
#[storage(read, write)]
fn withdraw(external_contract_id: ContractId);
Expand Down
1 change: 0 additions & 1 deletion examples/identity/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use abi::IdentityExample;
use errors::MyError;

use std::{
auth::msg_sender,
constants::{
BASE_ASSET_ID,
ZERO_B256,
Expand Down
2 changes: 0 additions & 2 deletions examples/msg_sender/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
contract;

use std::auth::msg_sender;

abi MyOwnedContract {
fn receive(field_1: u64) -> bool;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/subcurrency/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ANCHOR: body
contract;

use std::{auth::msg_sender, hash::sha256};
use std::hash::sha256;

////////////////////////////////////////
// Event declarations
Expand Down
1 change: 0 additions & 1 deletion examples/wallet_smart_contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
contract;

use std::{
auth::msg_sender,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
Expand Down
3 changes: 3 additions & 0 deletions sway-lib-std/src/prelude.sw
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ use ::convert::From;

// Logging
use ::logging::log;

// Auth
use ::auth::msg_sender;
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
script;

use std::auth::*;
use std::identity::*;
//use std::b512::*;

fn bogus() {
let value = B512 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ mod other_contract;

use other_contract::*;

use std::auth::msg_sender;

abi MyContract {
#[storage(read, write)]
fn withdraw(external_contract_id: ContractId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ contract;
mod wallet_abi;

use std::{
auth::{
AuthError,
msg_sender,
},
auth::AuthError,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ contract;
mod wallet_abi;

use std::{
auth::{
AuthError,
msg_sender,
},
auth::AuthError,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ contract;
mod wallet_abi;

use std::{
auth::{
AuthError,
msg_sender,
},
auth::AuthError,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait Ownable : StorageHelpers {

#[storage(read)]
fn only_owner() {
assert(std::auth::msg_sender().unwrap() == Identity::Address(Address::from(Self::get_owner())));
assert(msg_sender().unwrap() == Identity::Address(Address::from(Self::get_owner())));
}

#[storage(write)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait Ownable : StorageHelpers {

#[storage(read)]
fn only_owner() {
assert(std::auth::msg_sender().unwrap() == Identity::Address(Address::from(Self::get_owner())));
assert(msg_sender().unwrap() == Identity::Address(Address::from(Self::get_owner())));
}

#[storage(write)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ contract;
mod wallet_abi;

use std::{
auth::{
AuthError,
msg_sender,
},
auth::AuthError,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ contract;
mod wallet_abi;

use std::{
auth::{
AuthError,
msg_sender,
},
auth::AuthError,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ contract;
mod wallet_abi;

use std::{
auth::{
AuthError,
msg_sender,
},
auth::AuthError,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ abi NFT {

use std::{
block::height,
auth::msg_sender,
call_frames::{
contract_id,
msg_asset_id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
contract;

use std::auth::msg_sender;
use std::storage::storage_vec::*;

abi MyContract {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
contract;

use std::auth::msg_sender;
use std::storage::storage_vec::StorageVec;

abi MyContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait Ownable : StorageHelpers {

#[storage(read)]
fn only_owner() {
assert(std::auth::msg_sender().unwrap() == Identity::Address(Address::from(Self::get_owner())));
assert(msg_sender().unwrap() == Identity::Address(Address::from(Self::get_owner())));
}

#[storage(write)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
contract;

use auth_testing_abi::*;
use std::auth::*;

abi AuthCaller {
fn call_auth_contract(auth_id: ContractId, expected_id: ContractId) -> bool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
script;

use auth_testing_abi::AuthTesting;
use std::auth::*;

fn main() -> u64 {
// TODO: ContractId for auth_testing_contract should ideally be passed to script as an arg when possible.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
library;

use std::auth::*;

abi AuthTesting {
fn is_caller_external() -> bool;
fn returns_msg_sender(expected_id: ContractId) -> bool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ contract;

use methods_abi::MethodsContract;

use std::auth::*;

fn bogus() -> Identity {
let sender = msg_sender();
sender.unwrap()
Expand Down

0 comments on commit af5f536

Please sign in to comment.