From 75aa7f930b11383b9daa168ae91009e53926b6c1 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Thu, 24 Dec 2020 12:38:27 +0200 Subject: [PATCH] feat: expose to_eip_155 (#111) --- ethers-signers/src/lib.rs | 12 ++++++++++++ ethers-signers/src/wallet/mod.rs | 19 ++----------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/ethers-signers/src/lib.rs b/ethers-signers/src/lib.rs index 0e7e9465..7c70ab34 100644 --- a/ethers-signers/src/lib.rs +++ b/ethers-signers/src/lib.rs @@ -60,6 +60,18 @@ use async_trait::async_trait; use ethers_core::types::{Address, Signature, TransactionRequest}; use std::error::Error; +/// Applies [EIP155](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md) +pub fn to_eip155_v>(recovery_id: T, chain_id: Option) -> u64 { + let standard_v: u8 = recovery_id.into(); + if let Some(chain_id) = chain_id { + // When signing with a chain ID, add chain replay protection. + (standard_v as u64) + 35 + chain_id * 2 + } else { + // Otherwise, convert to 'Electrum' notation. + (standard_v as u64) + 27 + } +} + /// Trait for signing transactions and messages /// /// Implement this trait to support different signing modes, e.g. Ledger, hosted etc. diff --git a/ethers-signers/src/wallet/mod.rs b/ethers-signers/src/wallet/mod.rs index 97cfbd26..2441aa37 100644 --- a/ethers-signers/src/wallet/mod.rs +++ b/ethers-signers/src/wallet/mod.rs @@ -4,13 +4,10 @@ mod private_key; #[cfg(feature = "yubihsm")] mod yubi; -use crate::Signer; +use crate::{to_eip155_v, Signer}; use ethers_core::{ k256::{ - ecdsa::{ - recoverable::{Id as RecoveryId, Signature as RecoverableSignature}, - signature::DigestSigner, - }, + ecdsa::{recoverable::Signature as RecoverableSignature, signature::DigestSigner}, elliptic_curve::FieldBytes, Secp256k1, }, @@ -123,18 +120,6 @@ impl> Wallet { } } -/// Applies [EIP155](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md) -fn to_eip155_v(recovery_id: RecoveryId, chain_id: Option) -> u64 { - let standard_v: u8 = recovery_id.into(); - if let Some(chain_id) = chain_id { - // When signing with a chain ID, add chain replay protection. - (standard_v as u64) + 35 + chain_id * 2 - } else { - // Otherwise, convert to 'Electrum' notation. - (standard_v as u64) + 27 - } -} - // do not log the signer impl> fmt::Debug for Wallet { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {