alexandria_bytes 0.1.0

An implementation similar to Solidity bytes written in Cairo 1

Readme

bytes

bytes is an implementation similar to Solidity bytes written in Cairo 1 by zkLink. It is notable for its built-in implementation of Keccak and Sha256 hash functions, offering convenience for migrating EVM Contracts written in Solidity to the Starknet ecosystem.

Example

bytes implements read and write operations for all Cairo built-in types, as well as keccak/sha256 operations. For example, in Solidity, if there is a Token type, and we want to calculate the keccak256 hash of an instance of this structure.

struct Token {
    uint16 tokenId;
    address tokenAddress;
    uint8 decimals;
}

In Solidity, we can achieve this by doing the following:

keccak256(abi.encodePacked(tokenId, tokenAddress, decimals));

In Cairo, we can effortlessly achieve the same using Bytes:

use alexandria_bytes::Bytes;
use alexandria_bytes::BytesTrait;
use debug::PrintTrait;
use starknet::contract_address_const;

fn main() {
    let mut bytes: Bytes = BytesTrait::new(0, array![]);
    bytes.append_u16(1);
    bytes.append_address(contract_address_const::<0x123>());
    bytes.append_u8(16);
    let keccak_hash = bytes.keccak();
    keccak_hash.print();
}

Metadata

Version 0.1.0

Uploaded 1 month ago

Size 10.5 KB

Installation

Run the following command in your project dir

scarb add alexandria_bytes@0.1.0

Or add the following line to your Scarb.toml

alexandria_bytes = "0.1.0"

Monthly downloads

Links

Homepage github.com/keep-starknet-strange/alexandria/tree/main/src/bytes

Owners