Skip to content

Commit

Permalink
docs: update signer documentation (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Feb 2, 2024
1 parent 2d23d57 commit 2be6214
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 39 deletions.
19 changes: 19 additions & 0 deletions crates/network/src/transaction/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ pub enum TxKind {
Call(Address),
}

impl From<Option<Address>> for TxKind {
/// Creates a `TxKind::Call` with the `Some` address, `None` otherwise.
#[inline]
fn from(value: Option<Address>) -> Self {
match value {
None => TxKind::Create,
Some(addr) => TxKind::Call(addr),
}
}
}

impl From<Address> for TxKind {
/// Creates a `TxKind::Call` with the given address.
#[inline]
fn from(value: Address) -> Self {
TxKind::Call(value)
}
}

impl TxKind {
/// Returns the address of the contract that will be called or will receive the transfer.
pub const fn to(self) -> Option<Address> {
Expand Down
78 changes: 43 additions & 35 deletions crates/signer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,65 @@

Ethereum signer abstraction.

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

Supported signers:
- [Private key](./src/wallet)
- [Ledger](./src/ledger)
- [Trezor](./src/trezor)
Signer implementations in this crate:
- [Private key](./src/wallet/private_key.rs)
- [YubiHSM2](./src/wallet/yubi.rs)
- [AWS KMS](./src/aws)

## Examples

<!-- TODO
```rust,no_run
# use ethers_signers::{LocalWallet, Signer};
# use ethers_core::{k256::ecdsa::SigningKey, types::TransactionRequest};
# async fn foo() -> Result<(), Box<dyn std::error::Error>> {
// instantiate the wallet
let wallet = "dcf2cbdd171a21c480aa7f53d77f31bb102282b3ff099c78e3118b37348c72f7"
.parse::<LocalWallet>()?;
// create a transaction
let tx = TransactionRequest::new()
.to("vitalik.eth") // this will use ENS
.value(10000).into();
Additional signer implementation in Alloy:
- [Ledger](../signer-ledger/)
- [Trezor](../signer-trezor/)
- [AWS KMS](../signer-aws/)
- [GCP KMS](../signer-gcp/)

// sign it
let signature = wallet.sign_transaction(&tx).await?;
<!-- TODO: docs.rs -->
[Signer]: https://alloy-rs.github.io/alloy/alloy_signer/trait.Signer.html

// can also sign a message
let signature = wallet.sign_message("hello world").await?;
signature.verify("hello world", wallet.address()).unwrap();
# Ok(())
# }
```
-->
## Examples

Sign an Ethereum prefixed message ([EIP-712](https://eips.ethereum.org/EIPS/eip-712)):

```rust
use alloy_signer::{LocalWallet, Signer, SignerSync};

let message = "Some data";
// Instantiate the wallet.
let wallet = LocalWallet::random();

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

// Recover the signer from the message
// Recover the signer from the message.
let recovered = signature.recover_address_from_msg(message)?;

assert_eq!(recovered, wallet.address());
# Ok::<_, Box<dyn std::error::Error>>(())
```

Sign a transaction:

```rust
use alloy_consensus::TxLegacy;
use alloy_primitives::{U256, address, bytes};
use alloy_signer::{LocalWallet, Signer, SignerSync};

// Instantiate the wallet.
let wallet = "dcf2cbdd171a21c480aa7f53d77f31bb102282b3ff099c78e3118b37348c72f7"
.parse::<LocalWallet>()?;

// 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 = wallet.sign_transaction_sync(&mut tx)?;
# Ok::<_, Box<dyn std::error::Error>>(())
```
2 changes: 1 addition & 1 deletion crates/signer/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloy_primitives::hex;
use k256::ecdsa;
use thiserror::Error;

/// Error thrown by the Wallet module.
/// Error thrown by [`Wallet`](crate::Wallet).
#[derive(Debug, Error)]
pub enum WalletError {
/// [`ecdsa`] error.
Expand Down
4 changes: 3 additions & 1 deletion crates/signer/src/wallet/mnemonic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Specific helper functions for creating/loading a mnemonic private key following BIP-39
//! Specific helper functions for creating/loading a mnemonic private key following [BIP-39]
//! specifications.
//!
//! [BIP-39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki

use crate::{utils::secret_key_to_address, Wallet, WalletError};
use coins_bip32::path::DerivationPath;
Expand Down
2 changes: 2 additions & 0 deletions crates/signer/src/wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! In-memory (local) private key signer.

use crate::{Result, Signer, SignerSync};
use alloy_primitives::{Address, ChainId, Signature, B256};
use async_trait::async_trait;
Expand Down
2 changes: 1 addition & 1 deletion crates/signer/src/wallet/private_key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Specific helper functions for loading an offline K256 Private Key stored on disk
//! [`k256`] wallet implementation.

use super::{Wallet, WalletError};
use crate::utils::secret_key_to_address;
Expand Down
2 changes: 1 addition & 1 deletion crates/signer/src/wallet/yubi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Helpers for creating wallets for YubiHSM2.
//! [YubiHSM2](yubihsm) wallet implementation.

use super::Wallet;
use crate::utils::raw_public_key_to_address;
Expand Down

0 comments on commit 2be6214

Please sign in to comment.