Skip to content

Commit f1a572c

Browse files
committed
Pull out get_recovery_param
1 parent 77860be commit f1a572c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

contracts/crypto-verify/src/contract.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub fn query_verify_ethereum(
9999
Some(pair) => pair,
100100
None => return Err(StdError::generic_err("Signature must not be empty")),
101101
};
102-
let recovery = v - 27;
102+
let recovery = get_recovery_param(*v)?;
103103

104104
// Verification
105105
let calculated_pubkey = deps.api.secp256k1_recover_pubkey(&hash, rs, recovery)?;
@@ -151,6 +151,16 @@ fn ethereum_address(pubkey: &[u8]) -> StdResult<String> {
151151
Ok(out)
152152
}
153153

154+
fn get_recovery_param(v: u8) -> StdResult<u8> {
155+
// See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
156+
// for how `v` is composed.
157+
match v {
158+
27 => Ok(0),
159+
28 => Ok(1),
160+
_ => Err(StdError::generic_err("Values of v other than 27 and 28 not supported. Replay protection (EIP-155) cannot be used here."))
161+
}
162+
}
163+
154164
#[cfg(test)]
155165
mod tests {
156166
use super::*;

0 commit comments

Comments
 (0)