63 releases (10 stable)

new 1.5.1 Nov 16, 2024
1.4.0 Apr 10, 2024
1.3.0 Oct 23, 2023
1.2.3 Jul 1, 2023
0.1.14 Nov 28, 2018

#136 in Magic Beans

Download history 415/week @ 2024-07-29 347/week @ 2024-08-05 264/week @ 2024-08-12 228/week @ 2024-08-19 408/week @ 2024-08-26 497/week @ 2024-09-02 427/week @ 2024-09-09 356/week @ 2024-09-16 362/week @ 2024-09-23 264/week @ 2024-09-30 297/week @ 2024-10-07 296/week @ 2024-10-14 217/week @ 2024-10-21 383/week @ 2024-10-28 283/week @ 2024-11-04 300/week @ 2024-11-11

1,193 downloads per month
Used in 14 crates (13 directly)

Apache-2.0

165KB
3.5K SLoC

Introduction

Clarity is a low-level Ethereum transaction library written in pure Rust.

Features

  • Any-endian, 32/64-bit support
  • Public/private key handling
  • Transaction signing and verification
  • ABI enconding for common data types (see abi::Token variants)

Getting started

Here's an example lifetime of an Alice-to-Bob Ethereum transaction made with Clarity:

extern crate clarity;
use web30::client::Web3;

use clarity::{Address, Signature, Transaction, PrivateKey};
use std::time::Duration;


// A helper for filling the keys
let mut key_buf: [u8; 32] = rand::random();

let alices_key = PrivateKey::from_bytes(key_buf).unwrap();

key_buf = rand::random();
let bobs_key = PrivateKey::from_bytes(key_buf).unwrap();

// Create a new transaction
let tx = Transaction::Legacy {
    nonce: 0u32.into(),
    gas_price: 1_000_000_000u32.into(),
    gas_limit: 21_000u32.into(),
    to: bobs_key.to_address(),
    value: 100u32.into(),
    data: Vec::new(),
    signature: None, // Not signed. Yet.
};

let tx_signed: Transaction = tx.sign(&alices_key, None);
assert!(tx_signed.is_valid());

// You can always derive the sender from a signed transaction
let sender: Address = tx_signed.sender().unwrap();

// Send the locally assembled raw transaction over web3 (no need to trust another
// machine with your wallet or host a node locally).
const TIMEOUT: Duration = Duration::from_secs(1);
let web3 = Web3::new("http://localhost:8545", TIMEOUT);
let res = web3
    .eth_send_raw_transaction(tx_signed.to_bytes());
// res.await.unwrap()

Dependencies

~8MB
~96K SLoC