fix: signature fields should be U256 instead of H256 (#379)

* fix: signature fields should be U256 instead of H256

* fix: relevant changes for aws and ledger signer
This commit is contained in:
Rohit Narurkar 2021-08-15 17:00:44 +05:30 committed by GitHub
parent 60ff4660df
commit 3c89c732b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 17 deletions

View File

@ -1,6 +1,6 @@
// Code adapted from: https://github.com/tomusdrw/rust-web3/blob/master/src/api/accounts.rs // Code adapted from: https://github.com/tomusdrw/rust-web3/blob/master/src/api/accounts.rs
use crate::{ use crate::{
types::{Address, H256}, types::{Address, H256, U256},
utils::hash_message, utils::hash_message,
}; };
@ -55,9 +55,9 @@ pub enum RecoveryMessage {
/// An ECDSA signature /// An ECDSA signature
pub struct Signature { pub struct Signature {
/// R value /// R value
pub r: H256, pub r: U256,
/// S Value /// S Value
pub s: H256, pub s: U256,
/// V value in 'Electrum' notation. /// V value in 'Electrum' notation.
pub v: u64, pub v: u64,
} }
@ -118,8 +118,12 @@ impl Signature {
fn as_signature(&self) -> Result<(RecoverableSignature, RecoveryId), SignatureError> { fn as_signature(&self) -> Result<(RecoverableSignature, RecoveryId), SignatureError> {
let recovery_id = self.recovery_id()?; let recovery_id = self.recovery_id()?;
let signature = { let signature = {
let gar: &GenericArray<u8, U32> = GenericArray::from_slice(self.r.as_bytes()); let mut r_bytes = [0u8; 32];
let gas: &GenericArray<u8, U32> = GenericArray::from_slice(self.s.as_bytes()); 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)?; let sig = K256Signature::from_scalars(*gar, *gas)?;
RecoverableSignature::new(&sig, recovery_id)? RecoverableSignature::new(&sig, recovery_id)?
}; };
@ -163,8 +167,8 @@ impl<'a> TryFrom<&'a [u8]> for Signature {
} }
let v = bytes[64]; let v = bytes[64];
let r = H256::from_slice(&bytes[0..32]); let r = U256::from_big_endian(&bytes[0..32]);
let s = H256::from_slice(&bytes[32..64]); let s = U256::from_big_endian(&bytes[32..64]);
Ok(Signature { r, s, v: v.into() }) Ok(Signature { r, s, v: v.into() })
} }
@ -183,8 +187,12 @@ impl FromStr for Signature {
impl From<&Signature> for [u8; 65] { impl From<&Signature> for [u8; 65] {
fn from(src: &Signature) -> [u8; 65] { fn from(src: &Signature) -> [u8; 65] {
let mut sig = [0u8; 65]; let mut sig = [0u8; 65];
sig[..32].copy_from_slice(src.r.as_bytes()); let mut r_bytes = [0u8; 32];
sig[32..64].copy_from_slice(src.s.as_bytes()); 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 // TODO: What if we try to serialize a signature where
// the `v` is not normalized? // the `v` is not normalized?
sig[64] = src.v as u8; sig[64] = src.v as u8;

View File

@ -13,7 +13,7 @@ use ethers_core::{
elliptic_curve::sec1::ToEncodedPoint, elliptic_curve::sec1::ToEncodedPoint,
FieldBytes, FieldBytes,
}, },
types::{Address, Signature as EthSig, H256}, types::{Address, Signature as EthSig, U256},
utils::keccak256, utils::keccak256,
}; };
use rusoto_kms::{GetPublicKeyResponse, SignResponse}; use rusoto_kms::{GetPublicKeyResponse, SignResponse};
@ -26,8 +26,8 @@ pub(super) fn rsig_to_ethsig(sig: &RSig) -> EthSig {
let v = (v + 27) as u64; let v = (v + 27) as u64;
let r_bytes: FieldBytes = sig.r().into(); let r_bytes: FieldBytes = sig.r().into();
let s_bytes: FieldBytes = sig.s().into(); let s_bytes: FieldBytes = sig.s().into();
let r = H256::from_slice(&r_bytes.as_slice()); let r = U256::from_big_endian(&r_bytes.as_slice());
let s = H256::from_slice(&s_bytes.as_slice()); let s = U256::from_big_endian(&s_bytes.as_slice());
EthSig { r, s, v } EthSig { r, s, v }
} }

View File

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

View File

@ -16,7 +16,7 @@ use ethers_core::{
elliptic_curve::FieldBytes, elliptic_curve::FieldBytes,
Secp256k1, Secp256k1,
}, },
types::{transaction::eip2718::TypedTransaction, Address, Signature, H256}, types::{transaction::eip2718::TypedTransaction, Address, Signature, H256, U256},
utils::hash_message, utils::hash_message,
}; };
use hash::Sha256Proxy; use hash::Sha256Proxy;
@ -116,8 +116,8 @@ impl<D: DigestSigner<Sha256Proxy, RecoverableSignature>> Wallet<D> {
let r_bytes: FieldBytes<Secp256k1> = recoverable_sig.r().into(); let r_bytes: FieldBytes<Secp256k1> = recoverable_sig.r().into();
let s_bytes: FieldBytes<Secp256k1> = recoverable_sig.s().into(); let s_bytes: FieldBytes<Secp256k1> = recoverable_sig.s().into();
let r = H256::from_slice(r_bytes.as_slice()); let r = U256::from_big_endian(r_bytes.as_slice());
let s = H256::from_slice(s_bytes.as_slice()); let s = U256::from_big_endian(s_bytes.as_slice());
Signature { r, s, v } Signature { r, s, v }
} }