Key derivation and cryptographic signing functionality for Ethereum applications (ethers-rs)

Overview

ethers-signer-factory

ethers-signer-factory is a Rust crate that provides functions for key derivation and signing of Ethereum transactions and messages.

Features

  • Key Derivation: Compute derived keys from a master key using HKDF-SHA512.
  • Address Generation: Calculate the Ethereum address associated with a derived key.
  • Transaction Signing: Sign Ethereum transactions using a derived key and obtain the generated signature.
  • Message Signing: Sign Ethereum messages using a derived key and obtain the generated signature.
  • Typed Data Signing (EIP-712): Sign EIP-712 typed data using a derived key and obtain the generated signature.

Getting Started

To use ethers-signer-factory in your Rust project, add it as a dependency in your Cargo.toml file:

[dependencies]
ethers-signer-factory = "2.0.0"

Usage

use ethers_signer_factory::{get_derived_key, get_wallet_address, sign_transaction, sign_message};
use ethers_contract_derive::{Eip712, EthAbiType};
use ethers_core::types::transaction::eip2718::TypedTransaction;
use ethers_core::types::{TransactionRequest, Address};

#[tokio::main]
async fn main() {
    let key: [u8; 64] = [0u8; 64];
    let salt: [u8; 64] = [0u8; 64];

    let info = "some_key_id";

    // Example 1: Derive a key.
    let derived_key = get_derived_key(&key, &salt, info).expect("Key derivation failed");
    assert_eq!(
        hex::encode(derived_key).as_str(),
        "de61002dc1676e6b59b2dd27da3f32280defe3bc384f77de6eee372f70ceaae7"
    );

    // Example 2: Get an Ethereum address from the derived key.
    let wallet_address = get_wallet_address(&key, &salt, info).expect("Wallet creation failed");
    assert_eq!(
        hex::encode(wallet_address.as_bytes()).as_str(),
        "ea4d3e00f3d283cdb0e4d4fb783f208a5095fcb7"
    );

    // Example 3. Get a Signer Wallet from the derived key.
    let chain_id = 1;
    let _wallet = get_signer(&key, &salt, info, chain_id).expect("Signer creation failed");

    // Example 4: Sign a transaction.
    let tx = TypedTransaction::Legacy(TransactionRequest::default().from(wallet_address));
    let signed_tx =
        sign_transaction(&key, &salt, info, &tx).expect("Transaction signing failed");

    assert_eq!(
        hex::encode(signed_tx.to_vec().as_slice()).as_str(),
        "815dd9b736c52fed571b2d5d985c52d78593a6d4a602f8455e884371270760132c8cd1f163af8303dc83aaaf48d2a03bff3697f6c922951809c19c0593841b3426"
    );

    // Example 5: Sign a message.
    let message = "Some message to sign";
    let signature = sign_message(&key, &salt, info, message).expect("Message signing failed");

    assert_eq!(
        hex::encode(signature.to_vec().as_slice()).as_str(),
        "35867e62e5a2c4dd52947c0cbb4af6afc95564d5abb0584bec2f9669046f81aa0d22dba8e6844fb125b93fec9a3e3684916dc4541e45bdc824cf18b5ab2409c81c"
    );

    // Example 6: Sign Typed data.
    #[derive(Clone, Default, EthAbiType, Eip712)]
    struct TestTypedData {
        value: u32,
    }

    let test_payload = TestTypedData { value: 1 };
    let signature = sign_typed_data(&key, &salt, info, &test_payload)
        .await
        .expect("Sign typed data failed");

    assert_eq!(
        hex::encode(signature.to_vec().as_slice()).as_str(),
        "cdd86e133a50b7d2c4f5bff092dbfe4086ed559bb0718e9c1c9bce31171f0ff44df3cc0dcd20388ad99be26b6eca0c104bff48eb6ad8135bec9d76aee2c6930f1b"
    );
}

License

This code is licensed under the MIT License.

You might also like...
Freelance payment protocol written in Rust.. (with multi-sig signing for dispute settling)

Freelance Escrow payment protocol 📝 About The freelance protocol is a protocol built on decentralized and open systems such as blockchain and decentr

Ethernaut solutions with ethers-rs

the full repository is using Ethers-rs for the finding of CTF unless requirement of smart contract like in case of re-entrancy. best way get good at e

ethers-rs signer using GCP KMS

ethers-gcp-kms-signer Installation Cargo cargo add ethers-gcp-kms-signer Usage Signer use ethers::prelude::*; use ethers_gcp_kms_signer::{GcpKeyRingRe

An MEV back-running template for ethers-rs

MEV price prediction I show how to predict ChainLink price updates from the mempool. For the sake of illustration I work with AAVE V2 price oracles. E

Ethers-rs CCIP-Read Middleware

Ethers-rs CCIP-Read Middleware Ready to dive into the world of cross-chain data access? Look no further! This Rust library provides an Ethers middlewa

An open source, high performance limit order book for the Seaport smart contracts. Implemented in Rust using ethers-rs, this offers a turnkey option for digital asset marketplaces.

Quay Quay is an open source, high performance backend for the Seaport smart contracts. The project is implemented in Rust, using Postgres as a storage

ethers-rs scripting boilerplate

ethers-rs script boilerplate ethers-rs is a great blockchain scripting framework! This repo contains the essentials I find are useful when reading/wri

An API and test-app that exposes zcash functionality for app consumption

Zingolib This repo provides both a library for zingoproxyclient and zingo-mobile, as well as an included cli application to interact with zcashd via l

Cross-platform atomic wait and wake (aka futex) functionality for Rust.

Cross platform atomic wait and wake (aka futex) functionality. This crate only supports functionality that's available on all of Linux, Windows, and m

Releases(v2.0.0)
  • v2.0.0(Oct 16, 2023)

    Changed sign_transaction to return Signature object, introduced sign_typed_data for EIP-712 typed data signing, and added get_signer method.

    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Sep 14, 2023)

    The v1.0.1 release provides a comprehensive set of functions for key derivation and signing operations tailored for Ethereum applications.

    Source code(tar.gz)
    Source code(zip)
Owner
Ilia
Full-stack developer (Node.JS / Vue.js / Rust / Solidity / DeFi / Real-time / High-Load / FinTech)
Ilia
HD wallet BIP-32 related key derivation utilities.

HDWallet Docs HD wallet(BIP-32) key derivation utilities. This crate is build upon secp256k1 crate, this crate only provides BIP-32 related features,

jjy 23 Nov 27, 2022
Ethereum (and Ethereum like) indexer using P2P message to fetch blocks and transactions

Ethereum P2P indexer This project is an indexer for Ethereum and Ethereum forks. It takes advantage of the ETH (Ethereum Wire Protocol) to fetch block

null 5 Nov 10, 2023
Substreams development kit for Ethereum chains, contains Firehose Block model and helpers as well as utilities for Ethereum ABI encoding/decoding.

Substreams Ethereum Substreams development kit for Ethereum chains, contains Rust Firehose Block model and helpers as well as utilities for Ethereum A

StreamingFast 15 Oct 25, 2022
Fast and efficient ed25519 signing and verification in Rust.

ed25519-dalek Fast and efficient Rust implementation of ed25519 key generation, signing, and verification in Rust. Documentation Documentation is avai

dalek cryptography 563 Dec 26, 2022
An extensible and practical demonstration of constructing evm-based sandwich attacks built with ethers-rs and Huff language.

subway-rs • Construct evm-based sandwich attacks using Rust and Huff. Getting Started subway-rs is a port of libevm's original subway, implemented wit

refcell.eth 230 Apr 25, 2023
Ethereum key tool - Lightweight CLI tool to deal with ETH keys written in rust

ekt - Etherum Key Tool ekt is a lightweight tool to generate ethereum keys and addresses. Installation Either clone it and run it with cargo or instal

null 5 May 8, 2023
A library facilitating the signing and broadcasting of transactions on Cosmos SDK-based blockchains

txf Transaction factory - a library facilitating the signing and broadcasting of transactions (txs) on Cosmos SDK-based blockchains. How to use Exampl

larry 5 Jun 29, 2023
Like a thread pool, but for signing and broadcasting transactions

ethers-signer-pool This library provides a utility built on top of the ethers crate. The purpose of the utility is to provide a pool of signers (walle

Shaurya Arora 3 Aug 14, 2023
An Ethereum 2.0 Emulator for Local Testing of Eth2 Applications

Mousse is an Ethereum 2.0 emulator for local testing of Eth2 applications (mainly Rollups). HTTP Server The REST API definition can be found in the ht

Mousse 46 Sep 10, 2022
Example worker for ethereum-based applications

Ethsig-rs Example worker for ethereum-based applications. Features: Verify arbitrary messages and their signature from an Ethereum Address Verify EIP-

odyslam.eth 28 Sep 26, 2022