forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Identity
type to Sway Book (FuelLabs#2041)
* Add Identity type to Sway Book * Move Identity code to examples directory * Add Forc.lock to gitignore * Update docs/src/basics/blockchain_types.md Co-authored-by: John Adler <[email protected]> * Typo * Ran forc fmt * Update Cargo.toml * Update examples/identity/src/main.sw Co-authored-by: Mohammad Fawaz <[email protected]> * Delete Cargo.toml * Update Identity docs to use anchor * Fix CI * Add path to std in Forc.toml and update Forc.lock std source * Run forc fmt again Co-authored-by: John Adler <[email protected]> Co-authored-by: Mohammad Fawaz <[email protected]>
- Loading branch information
1 parent
32b48c2
commit a8f8bb4
Showing
7 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,14 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-C38E96E529F27BA9' | ||
dependencies = [] | ||
|
||
[[package]] | ||
name = 'identity' | ||
source = 'root' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-C38E96E529F27BA9' | ||
dependencies = ['core'] |
This file contains 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,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "identity" | ||
|
||
[dependencies] | ||
std = { path = "../../sway-lib-std" } |
This file contains 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,10 @@ | ||
library abi; | ||
|
||
use std::identity::Identity; | ||
|
||
abi IdentityExample { | ||
#[storage(read)]fn access_control_with_identity(); | ||
fn cast_to_identity(); | ||
fn different_executions(my_identity: Identity); | ||
fn identity_to_contract_id(my_identity: Identity); | ||
} |
This file contains 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,5 @@ | ||
library errors; | ||
|
||
pub enum MyError { | ||
UnauthorizedUser: (), | ||
} |
This file contains 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,68 @@ | ||
contract; | ||
|
||
dep abi; | ||
dep errors; | ||
|
||
use abi::IdentityExample; | ||
use errors::MyError; | ||
|
||
use std::{ | ||
address::Address, | ||
assert::require, | ||
chain::auth::{AuthError, msg_sender}, | ||
constants::BASE_ASSET_ID, | ||
contract_id::ContractId, | ||
identity::*, | ||
result::*, | ||
revert::revert, | ||
token::{force_transfer_to_contract, transfer_to_output} | ||
}; | ||
|
||
storage { | ||
owner: Identity, | ||
} | ||
|
||
impl IdentityExample for Contract { | ||
fn cast_to_identity() { | ||
// ANCHOR: cast_to_identity | ||
let my_address: Address = ~Address::from(BASE_ASSET_ID); | ||
let my_identity: Identity = Identity::Address(my_address); | ||
// ANCHOR_END: cast_to_identity | ||
} | ||
|
||
fn identity_to_contract_id(my_identity: Identity) { | ||
// ANCHOR: identity_to_contract_id | ||
let my_contract_id: ContractId = match my_identity { | ||
Identity::ContractId(identity) => { | ||
identity | ||
}, | ||
_ => { | ||
revert(0); | ||
} | ||
}; | ||
// ANCHOR_END: identity_to_contract_ids | ||
} | ||
|
||
fn different_executions(my_identity: Identity) { | ||
let amount = 1; | ||
let token_id = ~ContractId::from(BASE_ASSET_ID); | ||
|
||
// ANCHOR: different_executions | ||
match my_identity { | ||
Identity::Address(identity) => { | ||
transfer_to_output(amount, token_id, identity); | ||
}, | ||
Identity::ContractId(identity) => { | ||
force_transfer_to_contract(amount, token_id, identity); | ||
}, | ||
}; | ||
// ANCHOR_END: different_executions | ||
} | ||
|
||
#[storage(read)]fn access_control_with_identity() { | ||
// ANCHOR: access_control_with_identity | ||
let sender: Result<Identity, AuthError> = msg_sender(); | ||
require(sender.unwrap() == storage.owner, MyError::UnauthorizedUser); | ||
// ANCHOR_END: access_control_with_identity | ||
} | ||
} |
This file contains 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