Skip to content

Commit

Permalink
Accept one more eth signature format
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Jun 23, 2020
1 parent bb13b6c commit 8ba40e8
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions core/models/src/node/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,13 @@ impl Serialize for EIP1271Signature {
///
/// Some notes on implementation of methods of this structure:
///
/// Ethereum signed messages expect v parameter to be 27 + recovery_id(0,1,2,3)
/// Ethereum signed message produced by most clients contains v where v = 27 + recovery_id(0,1,2,3),
/// but for some clients v = recovery_id(0,1,2,3).
/// Library that we use for signature verification (written for bitcoin) expects v = recovery_id
///
/// That is why:
/// 1) when we create this structure by deserialization of message produced by user
/// we subtract 27 from v in `ETHSignature` and store it in ETHSignature structure this way.
/// we subtract 27 from v in `ETHSignature` if necessary and store it in the `ETHSignature` structure this way.
/// 2) When we serialize/create this structure we add 27 to v in `ETHSignature`.
///
/// This way when we have methods that consumes &self we can be sure that ETHSignature::recover_signer works
Expand All @@ -863,8 +864,14 @@ impl PackedEthSignature {

pub fn deserialize_packed(bytes: &[u8]) -> Result<Self, failure::Error> {
ensure!(bytes.len() == 65, "eth signature length should be 65 bytes");
// assumes v = recover + 27
Ok(PackedEthSignature(ETHSignature::from_electrum(&bytes)))
let mut bytes_array = [0u8; 65];
bytes_array.copy_from_slice(&bytes);

if bytes_array[64] >= 27 {
bytes_array[64] -= 27;
}

Ok(PackedEthSignature(ETHSignature::from(bytes_array)))
}

/// Signs message using ethereum private key, results are identical to signature created
Expand Down Expand Up @@ -1168,6 +1175,8 @@ mod test {
("0x8a91dc2d28b689474298d91899f0c1baf62cb85b", "0x", "0xd98f51c2ee0fd589e421348002dffec5d1b38e5bef9a41a699030456dc39298d12698158dc2a814b5f9ac6d433009dec87484a4579107be3f8f33907e92938291b"),
// this example has v = 28, unlike others
("0x8a91dc2d28b689474298d91899f0c1baf62cb85b", "0x14", "0xd288b623af654c9d805e132812edf09ce244040376ca49112e91d437ecceed7c518690d4ae14149cd533f1ca4f081e6d2252c980fccc63de4d6bb818f1b668921c"),
// same as first, but v is just recovery id
("0x8a91dc2d28b689474298d91899f0c1baf62cb85b", "0xdead", "0x13c34c76ffb42d97da67ddc5d275e92d758d1b48b5ee4b3bacd800cbeec3baff043a5ee63fea55485e1ee5d6f8b088daabd095f2ebbdc80a33806528b44bfccc01"),
];

for (address, msg, signature) in examples {
Expand Down

0 comments on commit 8ba40e8

Please sign in to comment.