25 releases (6 breaking)

new 0.6.2 Nov 7, 2024
0.5.4 Oct 23, 2024
0.2.0 Jul 16, 2024
0.0.0-reserved Nov 23, 2023

#277 in Magic Beans

Download history 11829/week @ 2024-07-18 12622/week @ 2024-07-25 15002/week @ 2024-08-01 18236/week @ 2024-08-08 16341/week @ 2024-08-15 17053/week @ 2024-08-22 19833/week @ 2024-08-29 23385/week @ 2024-09-05 25267/week @ 2024-09-12 25353/week @ 2024-09-19 26534/week @ 2024-09-26 35681/week @ 2024-10-03 38663/week @ 2024-10-10 40250/week @ 2024-10-17 40289/week @ 2024-10-24 36054/week @ 2024-10-31

162,807 downloads per month
Used in 102 crates (18 directly)

MIT/Apache

23KB
325 lines

alloy-signer

Ethereum signer abstraction.

You can implement the Signer trait to extend functionality to other signers such as Hardware Security Modules, KMS etc. See its documentation for more.

Signer implementations in Alloy:

Examples

Sign an Ethereum prefixed message (EIP-712):

use alloy_signer::{Signer, SignerSync};
use alloy_signer_local::PrivateKeySigner;

// Instantiate a signer.
let signer = PrivateKeySigner::random();

// Sign a message.
let message = "Some data";
let signature = signer.sign_message_sync(message.as_bytes())?;

// Recover the signer from the message.
let recovered = signature.recover_address_from_msg(message)?;
assert_eq!(recovered, signer.address());
# Ok::<_, Box<dyn std::error::Error>>(())

Sign a transaction:

use alloy_consensus::TxLegacy;
use alloy_primitives::{U256, address, bytes};
use alloy_signer::{Signer, SignerSync};
use alloy_signer_local::PrivateKeySigner;
use alloy_network::{TxSignerSync};

// Instantiate a signer.
let signer = "dcf2cbdd171a21c480aa7f53d77f31bb102282b3ff099c78e3118b37348c72f7"
    .parse::<PrivateKeySigner>()?;

// Create a transaction.
let mut tx = TxLegacy {
    to: address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into(),
    value: U256::from(1_000_000_000),
    gas_limit: 2_000_000,
    nonce: 0,
    gas_price: 21_000_000_000,
    input: bytes!(),
    chain_id: Some(1),
};

// Sign it.
let signature = signer.sign_transaction_sync(&mut tx)?;
# Ok::<_, Box<dyn std::error::Error>>(())

Dependencies

~17MB
~363K SLoC