Skip to content

Commit

Permalink
[+] v0.1.4
Browse files Browse the repository at this point in the history
- Added `chunk32` and `chunks32` to kwl32
- Added `PartialEq` to `Bytes` and `Word`
- Added `remove` and `__remove` to `View`
- Added `len` wrapper and `partialEq` to `Calldata`
- Added tests to cover base types
  • Loading branch information
0xMaka authored Sep 30, 2024
2 parents 5aaf963 + 833838b commit 849bf42
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 65 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
# ------------------------------------------------------------------------------
[package] #|
name = "kawala"
version = "0.1.3"
version = "0.1.4"
authors = ["Maka <makagucci@gmx.com>"]
edition = "2021"
license = "MIT"

repository = "https://github.com/0xmaka/kawala"
description = "A tool to view, manipulate and craft calldata."
keywords = ["blockchain", "rust-lang", "evm", "blockchain-tool", "calldata"]
keywords = ["blockchain", "evm", "blockchain-tool", "calldata"]

[lib]
name = "kawala"
Expand All @@ -22,4 +22,4 @@ path = "src/lib.rs"
[features]
simd = []
##|
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ See tests and examples for inspiration.
/* Run in dir: */ use kawala::{ View, Calldata, WithSig };
cargo add kawala //
// or add to Cargo.toml: //* init *///------------------------------------------------------->
kawala = "0.1.3" let mut view = View::new(Calldata::from_hex("ff"), WithSig::False);
kawala = "0.1.4" let mut view = View::new(Calldata::from_hex("ff"), WithSig::False);
```
</td>
</tr>
Expand Down
1 change: 1 addition & 0 deletions misc/doc/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `kawala` crate."><title>kawala - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-492a78a4a87dcc01.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="kawala" data-themes="" data-resource-suffix="" data-rustdoc-version="1.82.0-nightly (a7399ba69 2024-08-31)" data-channel="nightly" data-search-js="search-a99f1315e7cc5121.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-921df33f47b8780c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-3b12f09e550e0385.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../kawala/index.html">kawala</a><span class="version">0.1.3</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li></ul></section></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">kawala</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../src/kawala/lib.rs.html#12-545">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="mod" href="bai/index.html" title="mod kawala::bai">bai</a></div></li><li><div class="item-name"><a class="mod" href="kwl32/index.html" title="mod kawala::kwl32">kwl32</a></div></li></ul><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Calldata.html" title="struct kawala::Calldata">Calldata</a></div></li><li><div class="item-name"><a class="struct" href="struct.Signature.html" title="struct kawala::Signature">Signature</a></div></li><li><div class="item-name"><a class="struct" href="struct.View.html" title="struct kawala::View">View</a></div></li><li><div class="item-name"><a class="struct" href="struct.Word.html" title="struct kawala::Word">Word</a></div></li></ul><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="enum" href="enum.Bytes.html" title="enum kawala::Bytes">Bytes</a></div></li><li><div class="item-name"><a class="enum" href="enum.WithSig.html" title="enum kawala::WithSig">WithSig</a></div></li></ul></section></div></main></body></html>
7 changes: 4 additions & 3 deletions misc/kawalas_log.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
## Version logs

##### v0.1.4-pre
##### v0.1.4
- Added `chunk32` and `chunks32` to kwl32
- Added `PartialEq` to `Bytes` and `Word`
- Added `remove` and `__remove` to `View`
- Added `len` wrapper and `partialEq` to `Calldata`
- Added tests to cover base types

##### v0.1.3
- Added edge case handlers to `pad32l` and `pad32r`
- - Functions needed to be more robust as the ones accepting arbitrary length
- Tests added to cover most of bai and kwl32
- Added tests to cover most of bai and kwl32
- Modules are now public

##### v0.1.2
Expand All @@ -26,4 +28,3 @@
- Initial commit
- - Porting over some of the python tools I wrote, as a single module
- - Data dump of initial working library { lib.rs, kwl32.rs, bai.rs }

20 changes: 14 additions & 6 deletions misc/kawalas_todo.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
## Current to do
## Current to do

- [ ] Tests => Complete `View` coverage
- Founder mods are minimally to mostly covered, but lib is undercovered
- - Don't wanna play catch up or keep adding without coverage

- [ ] Print => Refactor `summary` for cleaner default look when printing large pages
- Probably have dedicated method that takes column input and uses a std lib chunk method
- - Not worth a dependancy in termion and not about to botch a wrapper for ioctl over it
- - A heavier client could be a seperate package

- [ ] Masks => View methods for simplified masking
- We can already build a mask, but can make the process feel more intuitive
Expand All @@ -7,13 +16,12 @@
- There is already replace, and long winded ways to do above, just add an abstraction

- [ ] Deque => View method to pop from the top of the page
- Not an issue just undecided on how I want to handle empty, if at all
- Not an issue just undecided on how I want to handle it, if at all

- [x] Remove => View method to remove an element from within the middle of the page
- Not something I'd use over clearing, but very reasonable to expect to have the option

- [x] Chunk => kwl32 dedicated chunk function
- [x] Chunk => kwl32 dedicated chunk function
- Unlikley to be utilised by Kawala
- - Kawalas implementation is more forgiving for its less performance concerned `View`.
- - Gives kwl32 viability as its own module, though Kawala will remain dependancy free.

- - Kawalas implementation is more forgiving for its less performance concerned `View`
- - Gives kwl32 viability as its own module, though Kawala will remain dependancy free
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl Calldata {
self.data.hex()
}

pub fn len(&self) -> usize {
self.data.len()
}

pub fn from_bytes(bytes : &[u8]) -> Self {
Self::new(Bytes::Array(bytes.to_vec()))
}
Expand All @@ -90,6 +94,12 @@ impl Calldata {
}
}

impl PartialEq for Calldata {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}

/* ----------------------------------------------------------------------------
Signature structure
-----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -136,6 +146,12 @@ impl Signature {
}
}

impl PartialEq for Signature {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}

/*----------------------------------------------------------------------------
Word structure
-----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -366,6 +382,10 @@ View cont.. destructive functions that mutate state
pub fn pop(&mut self) -> String {
self.__pop().hex()
}
// removes a word - wrapper for vec method, unwraps to hex
pub fn remove(&mut self, index : usize) -> String {
self.__remove(index).hex()
}
// replace word with left padded equivalent
pub fn left_pad(&mut self, index : usize) -> () {
let word = self.__word(index);
Expand Down
95 changes: 69 additions & 26 deletions tests/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,126 @@ mod bytes {

// ----------------------------------- CREATION AND ACCESS --------------------------------------//

#[test]
fn bytes4_creation_and_access() {
#[test]
fn bytes4_creation_and_access() {
let bytes = Bytes::Bytes4([0x01,0x02,0x03,0x04]);
assert_eq!(bytes.bytes(), [0x01,0x02,0x03,0x04]);
assert_eq!(bytes.len(), 4);
assert_eq!(bytes[0], 1);
}
}

#[test]
fn bytes32_creation_and_access() {
#[test]
fn bytes32_creation_and_access() {
let bytes = Bytes::Bytes32([0; 32]);
assert_eq!(bytes.bytes().len(), 32);
assert_eq!(bytes[0], 0);
}
}

#[test]
fn array_creation_and_access() {
#[test]
fn array_creation_and_access() {
let bytes = Bytes::Array(vec![0x01,0x02,0x03]);
assert_eq!(bytes.bytes(), [1, 2, 3]);
assert_eq!(bytes.len(), 3);
assert_eq!(bytes[1], 2);
}
}

#[test]
fn array_empty() {
#[test]
fn array_empty() {
let bytes = Bytes::Array(vec![]);
assert_eq!(bytes.bytes().len(), 0);
}
}

// ---------------------------------------- LEN & HEX -------------------------------------------//

#[test]
fn hex_conversion() {
#[test]
fn hex_conversion() {
let bytes = Bytes::Bytes32([0xFF; 32]);
assert_eq!(bytes.hex(), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
}
}

#[test]
fn len_wrapper() {
#[test]
fn len_wrapper() {
let bytes1 = Bytes::Bytes4([1, 2, 3, 4]);
let bytes2 = Bytes::Bytes32([0; 32]);
let bytes3 = Bytes::Array(vec![1, 2, 3, 4, 5]);

assert_eq!(bytes1.len(), 4);
assert_eq!(bytes2.len(), 32);
assert_eq!(bytes3.len(), 5);
}
}

// --------------------------------------- COMPARISONS ------------------------------------------//

#[test]
fn bytes4_equality_match() {
let a = Bytes::Bytes4([0u8;4]);
let b = Bytes::Bytes4([0u8;4]);
assert_eq!(a, b);
}

#[test]
fn bytes4_equality_no_match() {
let a = Bytes::Bytes4([0u8;4]);
let b = Bytes::Bytes4([1u8;4]);
assert_ne!(a, b);
}

#[test]
fn bytes32_equality_match() {
let a = Bytes::Bytes32([0u8;32]);
let b = Bytes::Bytes32([0u8;32]);
assert_eq!(a, b);
}

#[test]
fn bytes32_equality_no_match() {
let a = Bytes::Bytes32([0u8;32]);
let b = Bytes::Bytes32([1u8;32]);
assert_ne!(a, b);
}

#[test]
fn array_equality_match() {
let a = Bytes::Array(vec![0u8;4]);
let b = Bytes::Array(vec![0u8;4]);
assert_eq!(a, b);
}

#[test]
fn array_equality_no_match() {
let a = Bytes::Array(vec![0u8;4]);
let b = Bytes::Array(vec![1u8;4]);
assert_ne!(a, b);
}


// --------------------------------------- GENERAL USE ------------------------------------------//

#[test]
fn bytes32() -> () {
/* initialize a bytes32 */
fn bytes32() {
let bytes = Bytes::Bytes32([0u8;32]);
/* Run some sanity checks */
assert_eq!(bytes.len(), 32);
assert_eq!(bytes.hex() . chars() . count(), 64);
assert_eq!(bytes.bytes() . len(), 32);
}

#[test]
fn byte_array() -> () {
fn byte_array() {
/* initialize a byte array */
let bytes = Bytes::Array([0u8;64].to_vec());
let bytes = Bytes::Array(vec![0u8;64]);
assert_eq!(bytes.len(), 64);
assert_ne!(bytes.hex() . chars() . count(), 64);
}

#[test]
fn small_array() -> () {
fn small_array() {
/* initialize a byte array */
let bytes = Bytes::Array([0u8;2].to_vec());
let bytes = Bytes::Array(vec![0u8;2]);
assert_eq!(bytes.len(), 2);
}

#[test]
fn bytes4() -> () {
fn bytes4() {
/* initialize a bytes32 */
let bytes = Bytes::Bytes4([0u8;4]);
assert_eq!(bytes.len(), 4);
Expand Down
71 changes: 71 additions & 0 deletions tests/calldata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//--------------------------------------- CALLDATA TYPE -----------------------------------------//
// ----------------------------------------------------------------------------------------------//
#[cfg(test)]
mod calldata {
use kawala::{ Calldata };
// ----------------------------------- CREATION AND ACCESS --------------------------------------//

#[test]
fn call_creation_and_access_from_bytes() {
let call = Calldata::from_bytes(&[0x01,0x02,0x03,0x04]);
assert_eq!(call.bytes(), [0x01,0x02,0x03,0x04]);
assert_eq!(call.len(), 4);
assert_eq!(call.bytes()[0], 1);
}

#[test]
fn call_creation_and_access_from_hex() {
let call = Calldata::from_hex("01020304");
assert_eq!(call.bytes(), [0x01,0x02,0x03,0x04]);
assert_eq!(call.len(), 4);
assert_eq!(call.bytes()[0], 1);
}

// ---------------------------------------- LEN & HEX -------------------------------------------//

#[test]
fn hex_conversion() {
let call = Calldata::from_bytes(&[0xFF; 32]);
assert_eq!(call.hex(), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
}

#[test]
fn len_wrapper() {
let call = Calldata::from_bytes(&[1, 2, 3, 4]);
assert_eq!(call.len(), 4);
}

// --------------------------------------- COMPARISONS ------------------------------------------//

#[test]
fn call_equality_match() {
let a = Calldata::from_bytes(&[0u8;4]);
let b = Calldata::from_bytes(&[0u8;4]);
assert_eq!(a, b);
}

#[test]
fn call_equality_no_match() {
let a = Calldata::from_bytes(&[0u8;4]);
let b = Calldata::from_bytes(&[1u8;4]);
assert_ne!(a, b);
}

// --------------------------------------- GENERAL ------------------------------------------//

#[test]
fn call() -> () {
let call = Calldata::from_bytes(&[0u8;32]);
assert_eq!(call.len(), 32);
let call = Calldata::from_hex(&"0".repeat(64));
assert_eq!(call.len(), 32);
}

#[test]
fn more_than_32() {
let call = Calldata::from_bytes(&[0u8;64]);
assert_eq!(call.len(), 64);
}

}

Loading

0 comments on commit 849bf42

Please sign in to comment.