Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix: signature fields should be U256 instead of H256 (#379)
Browse files Browse the repository at this point in the history
* fix: signature fields should be U256 instead of H256

* fix: relevant changes for aws and ledger signer
  • Loading branch information
roynalnaruto authored Aug 15, 2021
1 parent 60ff466 commit 3c89c73
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
26 changes: 17 additions & 9 deletions ethers-core/src/types/signature.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code adapted from: https://github.com/tomusdrw/rust-web3/blob/master/src/api/accounts.rs
use crate::{
types::{Address, H256},
types::{Address, H256, U256},
utils::hash_message,
};

Expand Down Expand Up @@ -55,9 +55,9 @@ pub enum RecoveryMessage {
/// An ECDSA signature
pub struct Signature {
/// R value
pub r: H256,
pub r: U256,
/// S Value
pub s: H256,
pub s: U256,
/// V value in 'Electrum' notation.
pub v: u64,
}
Expand Down Expand Up @@ -118,8 +118,12 @@ impl Signature {
fn as_signature(&self) -> Result<(RecoverableSignature, RecoveryId), SignatureError> {
let recovery_id = self.recovery_id()?;
let signature = {
let gar: &GenericArray<u8, U32> = GenericArray::from_slice(self.r.as_bytes());
let gas: &GenericArray<u8, U32> = GenericArray::from_slice(self.s.as_bytes());
let mut r_bytes = [0u8; 32];
let mut s_bytes = [0u8; 32];
self.r.to_big_endian(&mut r_bytes);
self.s.to_big_endian(&mut s_bytes);
let gar: &GenericArray<u8, U32> = GenericArray::from_slice(&r_bytes);
let gas: &GenericArray<u8, U32> = GenericArray::from_slice(&s_bytes);
let sig = K256Signature::from_scalars(*gar, *gas)?;
RecoverableSignature::new(&sig, recovery_id)?
};
Expand Down Expand Up @@ -163,8 +167,8 @@ impl<'a> TryFrom<&'a [u8]> for Signature {
}

let v = bytes[64];
let r = H256::from_slice(&bytes[0..32]);
let s = H256::from_slice(&bytes[32..64]);
let r = U256::from_big_endian(&bytes[0..32]);
let s = U256::from_big_endian(&bytes[32..64]);

Ok(Signature { r, s, v: v.into() })
}
Expand All @@ -183,8 +187,12 @@ impl FromStr for Signature {
impl From<&Signature> for [u8; 65] {
fn from(src: &Signature) -> [u8; 65] {
let mut sig = [0u8; 65];
sig[..32].copy_from_slice(src.r.as_bytes());
sig[32..64].copy_from_slice(src.s.as_bytes());
let mut r_bytes = [0u8; 32];
let mut s_bytes = [0u8; 32];
src.r.to_big_endian(&mut r_bytes);
src.s.to_big_endian(&mut s_bytes);
sig[..32].copy_from_slice(&r_bytes);
sig[32..64].copy_from_slice(&s_bytes);
// TODO: What if we try to serialize a signature where
// the `v` is not normalized?
sig[64] = src.v as u8;
Expand Down
6 changes: 3 additions & 3 deletions ethers-signers/src/aws/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ethers_core::{
elliptic_curve::sec1::ToEncodedPoint,
FieldBytes,
},
types::{Address, Signature as EthSig, H256},
types::{Address, Signature as EthSig, U256},
utils::keccak256,
};
use rusoto_kms::{GetPublicKeyResponse, SignResponse};
Expand All @@ -26,8 +26,8 @@ pub(super) fn rsig_to_ethsig(sig: &RSig) -> EthSig {
let v = (v + 27) as u64;
let r_bytes: FieldBytes = sig.r().into();
let s_bytes: FieldBytes = sig.s().into();
let r = H256::from_slice(&r_bytes.as_slice());
let s = H256::from_slice(&s_bytes.as_slice());
let r = U256::from_big_endian(&r_bytes.as_slice());
let s = U256::from_big_endian(&s_bytes.as_slice());
EthSig { r, s, v }
}

Expand Down
4 changes: 2 additions & 2 deletions ethers-signers/src/ledger/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ impl LedgerEthereum {
}

let v = result[0] as u64;
let r = H256::from_slice(&result[1..33]);
let s = H256::from_slice(&result[33..]);
let r = U256::from_big_endian(&result[1..33]);
let s = U256::from_big_endian(&result[33..]);
Ok(Signature { r, s, v })
}

Expand Down
6 changes: 3 additions & 3 deletions ethers-signers/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ethers_core::{
elliptic_curve::FieldBytes,
Secp256k1,
},
types::{transaction::eip2718::TypedTransaction, Address, Signature, H256},
types::{transaction::eip2718::TypedTransaction, Address, Signature, H256, U256},
utils::hash_message,
};
use hash::Sha256Proxy;
Expand Down Expand Up @@ -116,8 +116,8 @@ impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> Wallet<D> {

let r_bytes: FieldBytes<Secp256k1> = recoverable_sig.r().into();
let s_bytes: FieldBytes<Secp256k1> = recoverable_sig.s().into();
let r = H256::from_slice(r_bytes.as_slice());
let s = H256::from_slice(s_bytes.as_slice());
let r = U256::from_big_endian(r_bytes.as_slice());
let s = U256::from_big_endian(s_bytes.as_slice());

Signature { r, s, v }
}
Expand Down

0 comments on commit 3c89c73

Please sign in to comment.