pub struct LocalSigner<C> { /* private fields */ }
Expand description
An Ethereum private-public key pair which can be used for signing messages.
§Examples
§Signing and Verifying a message
The signer can be used to produce ECDSA Signature
objects, which can be
then verified. Note that this uses
eip191_hash_message
under the hood which will
prefix the message being hashed with the Ethereum Signed Message
domain separator.
use alloy_signer::{Signer, SignerSync};
use alloy_signer_local::PrivateKeySigner;
let signer = PrivateKeySigner::random();
// Optionally, the signer's chain id can be set, in order to use EIP-155
// replay protection with different chains
let signer = signer.with_chain_id(Some(1337));
// The signer can be used to sign messages
let message = b"hello";
let signature = signer.sign_message_sync(message)?;
assert_eq!(signature.recover_address_from_msg(&message[..]).unwrap(), signer.address());
// LocalSigner is cloneable:
let signer_clone = signer.clone();
let signature2 = signer_clone.sign_message_sync(message)?;
assert_eq!(signature, signature2);
Implementations§
source§impl LocalSigner<SigningKey>
impl LocalSigner<SigningKey>
sourcepub fn from_signing_key(credential: SigningKey) -> Self
pub fn from_signing_key(credential: SigningKey) -> Self
Creates a new LocalSigner
instance from a SigningKey
.
This can also be used to create a LocalSigner
from a SecretKey
.
See also the From
implementations.
sourcepub fn from_bytes(bytes: &B256) -> Result<Self, Error>
pub fn from_bytes(bytes: &B256) -> Result<Self, Error>
Creates a new LocalSigner
instance from a raw scalar serialized as a B256
byte
array.
This is identical to from_field_bytes
.
sourcepub fn from_field_bytes(bytes: &FieldBytes) -> Result<Self, Error>
pub fn from_field_bytes(bytes: &FieldBytes) -> Result<Self, Error>
Creates a new LocalSigner
instance from a raw scalar serialized as a FieldBytes
byte
array.
sourcepub fn from_slice(bytes: &[u8]) -> Result<Self, Error>
pub fn from_slice(bytes: &[u8]) -> Result<Self, Error>
Creates a new LocalSigner
instance from a raw scalar serialized as a byte slice.
Byte slices shorter than the field size (32 bytes) are handled by zero padding the input.
sourcepub fn random() -> Self
pub fn random() -> Self
Creates a new random keypair seeded with rand::thread_rng()
.
sourcepub fn random_with<R: Rng + CryptoRng>(rng: &mut R) -> Self
pub fn random_with<R: Rng + CryptoRng>(rng: &mut R) -> Self
Creates a new random keypair seeded with the provided RNG.
sourcepub fn as_nonzero_scalar(&self) -> &NonZeroScalar
pub fn as_nonzero_scalar(&self) -> &NonZeroScalar
Borrow the secret NonZeroScalar
value for this key.
§⚠️ Warning
This value is key material.
Please treat it with the care it deserves!
sourcepub fn to_bytes(&self) -> B256
pub fn to_bytes(&self) -> B256
Serialize this LocalSigner
’s SigningKey
as a B256
byte array.
sourcepub fn to_field_bytes(&self) -> FieldBytes
pub fn to_field_bytes(&self) -> FieldBytes
Serialize this LocalSigner
’s SigningKey
as a FieldBytes
byte array.
source§impl LocalSigner<SigningKey>
impl LocalSigner<SigningKey>
sourcepub fn new_keystore<P, R, S>(
dir: P,
rng: &mut R,
password: S,
name: Option<&str>,
) -> Result<(Self, String), LocalSignerError>
Available on crate feature keystore
only.
pub fn new_keystore<P, R, S>( dir: P, rng: &mut R, password: S, name: Option<&str>, ) -> Result<(Self, String), LocalSignerError>
keystore
only.Creates a new random encrypted JSON with the provided password and stores it in the
provided directory. Returns a tuple (LocalSigner, String) of the signer instance for the
keystore with its random UUID. Accepts an optional name for the keystore file. If None
,
the keystore is stored as the stringified UUID.
sourcepub fn decrypt_keystore<P, S>(
keypath: P,
password: S,
) -> Result<Self, LocalSignerError>
Available on crate feature keystore
only.
pub fn decrypt_keystore<P, S>( keypath: P, password: S, ) -> Result<Self, LocalSignerError>
keystore
only.Decrypts an encrypted JSON from the provided path to construct a LocalSigner
instance
sourcepub fn encrypt_keystore<P, R, B, S>(
keypath: P,
rng: &mut R,
pk: B,
password: S,
name: Option<&str>,
) -> Result<(Self, String), LocalSignerError>
Available on crate feature keystore
only.
pub fn encrypt_keystore<P, R, B, S>( keypath: P, rng: &mut R, pk: B, password: S, name: Option<&str>, ) -> Result<(Self, String), LocalSignerError>
keystore
only.Creates a new encrypted JSON with the provided private key and password and stores it in the
provided directory. Returns a tuple (LocalSigner, String) of the signer instance for the
keystore with its random UUID. Accepts an optional name for the keystore file. If None
,
the keystore is stored as the stringified UUID.
source§impl LocalSigner<Signer<Secp256k1>>
impl LocalSigner<Signer<Secp256k1>>
sourcepub fn connect(connector: Connector, credentials: Credentials, id: Id) -> Self
Available on crate feature yubihsm
only.
pub fn connect(connector: Connector, credentials: Credentials, id: Id) -> Self
yubihsm
only.Connects to a yubi key’s ECDSA account at the provided id
source§impl<C: PrehashSigner<(Signature, RecoveryId)>> LocalSigner<C>
impl<C: PrehashSigner<(Signature, RecoveryId)>> LocalSigner<C>
sourcepub const fn new_with_credential(
credential: C,
address: Address,
chain_id: Option<ChainId>,
) -> Self
pub const fn new_with_credential( credential: C, address: Address, chain_id: Option<ChainId>, ) -> Self
Construct a new credential with an external PrehashSigner
.
sourcepub const fn credential(&self) -> &C
pub const fn credential(&self) -> &C
Returns this signer’s credential.
sourcepub fn into_credential(self) -> C
pub fn into_credential(self) -> C
Consumes this signer and returns its credential.
Trait Implementations§
source§impl<C: Clone> Clone for LocalSigner<C>
impl<C: Clone> Clone for LocalSigner<C>
source§fn clone(&self) -> LocalSigner<C>
fn clone(&self) -> LocalSigner<C>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<C: PrehashSigner<(Signature, RecoveryId)>> Debug for LocalSigner<C>
impl<C: PrehashSigner<(Signature, RecoveryId)>> Debug for LocalSigner<C>
source§impl From<SecretKey<Secp256k1>> for LocalSigner<SigningKey>
impl From<SecretKey<Secp256k1>> for LocalSigner<SigningKey>
source§fn from(value: K256SecretKey) -> Self
fn from(value: K256SecretKey) -> Self
source§impl From<Signer<Secp256k1>> for LocalSigner<Signer<Secp256k1>>
Available on crate feature yubihsm
only.
impl From<Signer<Secp256k1>> for LocalSigner<Signer<Secp256k1>>
yubihsm
only.source§fn from(credential: YubiSigner<Secp256k1>) -> Self
fn from(credential: YubiSigner<Secp256k1>) -> Self
source§impl From<SigningKey<Secp256k1>> for LocalSigner<SigningKey>
impl From<SigningKey<Secp256k1>> for LocalSigner<SigningKey>
source§fn from(value: SigningKey) -> Self
fn from(value: SigningKey) -> Self
source§impl FromStr for LocalSigner<SigningKey>
impl FromStr for LocalSigner<SigningKey>
source§impl PartialEq for LocalSigner<SigningKey>
impl PartialEq for LocalSigner<SigningKey>
source§impl<C: PrehashSigner<(Signature, RecoveryId)> + Send + Sync> Signer for LocalSigner<C>
impl<C: PrehashSigner<(Signature, RecoveryId)> + Send + Sync> Signer for LocalSigner<C>
source§fn sign_hash<'life0, 'life1, 'async_trait>(
&'life0 self,
hash: &'life1 B256,
) -> Pin<Box<dyn Future<Output = Result<Signature>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn sign_hash<'life0, 'life1, 'async_trait>(
&'life0 self,
hash: &'life1 B256,
) -> Pin<Box<dyn Future<Output = Result<Signature>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
source§fn set_chain_id(&mut self, chain_id: Option<ChainId>)
fn set_chain_id(&mut self, chain_id: Option<ChainId>)
source§fn sign_message<'life0, 'life1, 'async_trait>(
&'life0 self,
message: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = Result<Sig, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: Sync + 'async_trait,
fn sign_message<'life0, 'life1, 'async_trait>(
&'life0 self,
message: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = Result<Sig, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: Sync + 'async_trait,
source§fn sign_typed_data<'life0, 'life1, 'life2, 'async_trait, T>(
&'life0 self,
payload: &'life1 T,
domain: &'life2 Eip712Domain,
) -> Pin<Box<dyn Future<Output = Result<Sig, Error>> + Send + 'async_trait>>
fn sign_typed_data<'life0, 'life1, 'life2, 'async_trait, T>( &'life0 self, payload: &'life1 T, domain: &'life2 Eip712Domain, ) -> Pin<Box<dyn Future<Output = Result<Sig, Error>> + Send + 'async_trait>>
eip712
only.source§fn sign_dynamic_typed_data<'life0, 'life1, 'async_trait>(
&'life0 self,
payload: &'life1 TypedData,
) -> Pin<Box<dyn Future<Output = Result<Sig, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: Sync + 'async_trait,
fn sign_dynamic_typed_data<'life0, 'life1, 'async_trait>(
&'life0 self,
payload: &'life1 TypedData,
) -> Pin<Box<dyn Future<Output = Result<Sig, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: Sync + 'async_trait,
eip712
only.source§impl<C: PrehashSigner<(Signature, RecoveryId)>> SignerSync for LocalSigner<C>
impl<C: PrehashSigner<(Signature, RecoveryId)>> SignerSync for LocalSigner<C>
source§fn chain_id_sync(&self) -> Option<ChainId>
fn chain_id_sync(&self) -> Option<ChainId>
source§fn sign_message_sync(&self, message: &[u8]) -> Result<Sig, Error>
fn sign_message_sync(&self, message: &[u8]) -> Result<Sig, Error>
source§fn sign_typed_data_sync<T>(
&self,
payload: &T,
domain: &Eip712Domain,
) -> Result<Sig, Error>
fn sign_typed_data_sync<T>( &self, payload: &T, domain: &Eip712Domain, ) -> Result<Sig, Error>
eip712
only.source§impl<C> TxSigner<Signature> for LocalSigner<C>
impl<C> TxSigner<Signature> for LocalSigner<C>
source§fn sign_transaction<'life0, 'life1, 'async_trait>(
&'life0 self,
tx: &'life1 mut dyn SignableTransaction<Signature>,
) -> Pin<Box<dyn Future<Output = Result<Signature>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn sign_transaction<'life0, 'life1, 'async_trait>(
&'life0 self,
tx: &'life1 mut dyn SignableTransaction<Signature>,
) -> Pin<Box<dyn Future<Output = Result<Signature>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
source§impl<C> TxSignerSync<Signature> for LocalSigner<C>
impl<C> TxSignerSync<Signature> for LocalSigner<C>
Auto Trait Implementations§
impl<C> Freeze for LocalSigner<C>where
C: Freeze,
impl<C> RefUnwindSafe for LocalSigner<C>where
C: RefUnwindSafe,
impl<C> Send for LocalSigner<C>where
C: Send,
impl<C> Sync for LocalSigner<C>where
C: Sync,
impl<C> Unpin for LocalSigner<C>where
C: Unpin,
impl<C> UnwindSafe for LocalSigner<C>where
C: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.