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

Added example for hashing and signature verification #46

Merged
merged 7 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ jobs:
'predicate',
'script',
'big_number',
'hashing',
'verify_signature',
]

name: Forc build [ ${{ matrix.dir }} ]
Expand Down
3 changes: 2 additions & 1 deletion docs/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ cheatsheet
enums
Forc
toml
bytecode
bytecode
Keccak
2 changes: 2 additions & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@
- [Script](./script.md)
- [Big Numbers](./big-numbers.md)
- [SRC20](./src20.md)
- [Hashing](./hashing.md)
- [Verify-Signature](./verify-signature.md)
7 changes: 7 additions & 0 deletions docs/src/hashing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Hashing with Keccak256

Example of how to compute hash in Sway using Keccak256

```sway
{{#include ../examples/hashing/src/main.sw}}
```
7 changes: 7 additions & 0 deletions docs/src/verify-signature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Verifying Signatures in Sway

Example of how to verify signatures in Sway

```sway
{{#include ../examples/verify_signature/src/main.sw}}
```
2 changes: 2 additions & 0 deletions examples/hashing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
13 changes: 13 additions & 0 deletions examples/hashing/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "core"
source = "path+from-root-CC22FE9C5CBB0084"

[[package]]
name = "hashing"
source = "member"
dependencies = ["std"]

[[package]]
name = "std"
source = "git+https://github.com/fuellabs/sway?tag=v0.61.2#e1b1c2bee73e0ba825e07736cefa6c0abd079595"
dependencies = ["core"]
7 changes: 7 additions & 0 deletions examples/hashing/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Laisha Wadhwa"]
entry = "main.sw"
license = "Apache-2.0"
name = "hashing"

[dependencies]
42 changes: 42 additions & 0 deletions examples/hashing/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
contract;

use std::hash::*;
use std::bytes::Bytes;

abi HashFunction {
fn hash(_text: str, _num: u64, _addr: b256) -> b256;
fn collision(_text: str, _anotherText: str) -> b256;
fn guess(_word: str) -> bool;
}

impl HashFunction for Contract {
fn hash(_text: str, _num: u64, _addr: b256) -> b256 {
keccak256({
let mut bytes = Bytes::new();
bytes.append(Bytes::from(core::codec::encode(_text)));
bytes.append(Bytes::from(core::codec::encode(_num)));
bytes.append(Bytes::from(core::codec::encode(_addr)));
bytes
})
}

// The collision function is not strictly necessary unless you have a specific use case that requires hashing two strings together.
// If your primary goal is to hash individual strings you can remove the collision function.
fn collision(_text: str, _anotherText: str) -> b256 {
keccak256({
let mut bytes = Bytes::new();
bytes.append(Bytes::from(core::codec::encode(_text)));
bytes.append(Bytes::from(core::codec::encode(_anotherText)));
bytes
})
}

fn guess(_word: str) -> bool {
let answer: b256 = 0x60298f78cc0b47170ba79c10aa3851d7648bd96f2f8e46a19dbc777c36fb0c00;
keccak256({
let mut bytes = Bytes::new();
bytes.append(Bytes::from(core::codec::encode(_word)));
bytes
}) == answer
}
}
2 changes: 2 additions & 0 deletions examples/verify_signature/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
13 changes: 13 additions & 0 deletions examples/verify_signature/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "core"
source = "path+from-root-CC22FE9C5CBB0084"

[[package]]
name = "std"
source = "git+https://github.com/fuellabs/sway?tag=v0.61.2#e1b1c2bee73e0ba825e07736cefa6c0abd079595"
dependencies = ["core"]

[[package]]
name = "verify_signature"
source = "member"
dependencies = ["std"]
7 changes: 7 additions & 0 deletions examples/verify_signature/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Laisha Wadhwa"]
entry = "main.sw"
license = "Apache-2.0"
name = "verify_signature"

[dependencies]
65 changes: 65 additions & 0 deletions examples/verify_signature/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
contract;

use std::hash::*;
use std::ecr::{ec_recover_address, EcRecoverError};
use std::bytes::Bytes;
use std::b512::B512;
use std::constants::ZERO_B256;

abi VerifySignature {
fn get_message_hash(_to: b256, _amount: u64, _message: str, _nonce: u64) -> b256;
fn get_eth_signed_message_hash(_message_hash: b256) -> b256;
fn recover_signer(_eth_signed_message_hash: b256, _signature: B512) -> b256;
fn verify(_signer: b256, _to: b256, _amount: u64, _message: str, _nonce: u64, _signature: B512) -> bool;

}

fn get_message_hash(_to: b256, _amount: u64, _message: str, _nonce: u64) -> b256 {
keccak256({
let mut bytes = Bytes::new();
bytes.append(Bytes::from(core::codec::encode(_to)));
bytes.append(Bytes::from(core::codec::encode(_amount)));
bytes.append(Bytes::from(core::codec::encode(_message)));
bytes.append(Bytes::from(core::codec::encode(_nonce)));
bytes
})
}

fn get_eth_signed_message_hash(_message_hash: b256) -> b256 {
keccak256({
let mut bytes = Bytes::new();
bytes.append(Bytes::from(core::codec::encode("\x19Fuel Signed Message:\n32")));
bytes.append(Bytes::from(core::codec::encode(_message_hash)));
bytes
})
}

fn recover_signer(_eth_signed_message_hash: b256, _signature: B512) -> b256 {
match ec_recover_address(_signature, _eth_signed_message_hash) {
Ok(address) => address.bits(),
Err(_) => ZERO_B256,
}
}

impl VerifySignature for Contract {


fn get_message_hash(_to: b256, _amount: u64, _message: str, _nonce: u64) -> b256{
::get_message_hash(_to, _amount, _message, _nonce)
}

fn get_eth_signed_message_hash(_message_hash: b256) -> b256 {
::get_eth_signed_message_hash(_message_hash)
}

fn recover_signer(_eth_signed_message_hash: b256, _signature: B512) -> b256{
::recover_signer(_eth_signed_message_hash, _signature)
}

fn verify(_signer: b256, _to: b256, _amount: u64, _message: str, _nonce: u64, _signature: B512) -> bool {
let message_hash = get_message_hash(_to, _amount, _message, _nonce);
let eth_signed_message_hash = get_eth_signed_message_hash(message_hash);
let recovered_signer = recover_signer(eth_signed_message_hash, _signature);
recovered_signer == _signer
}
}
Loading