Skip to content

Commit

Permalink
BFT-457: Add 27 to V
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Jun 11, 2024
1 parent 9587bad commit 6920c29
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions node/libs/crypto/src/secp256k1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ impl Hash for PublicKey {
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Signature {
sig: k256::ecdsa::Signature,
// TODO: Look up where we need to shift the recovery ID for Solidity
/// Standard Recover ID.
///
/// To verify signatures with Solidity, for example with the [OpenZeppelin](https://docs.openzeppelin.com/contracts/2.x/api/cryptography#ECDSA-recover-bytes32-bytes-)
/// library, we need to shift this by 27 when it's serailized to bytes. See [ECDSA.sol](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/de4154710bcc7c6ca5417097f34ce14e9205c3ac/contracts/utils/cryptography/ECDSA.sol#L128-L136).

Check warning on line 90 in node/libs/crypto/src/secp256k1/mod.rs

View workflow job for this annotation

GitHub Actions / typos

"serailized" should be "serialized".
recid: k256::ecdsa::RecoveryId,
}

Expand Down Expand Up @@ -149,7 +152,7 @@ impl ByteFmt for Signature {
let (r, s) = self.sig.split_bytes();
bz[..32].copy_from_slice(&r);
bz[32..64].copy_from_slice(&s);
bz[64] = self.recid.to_byte();
bz[64] = self.recid.to_byte() + 27;
bz
}
}
Expand Down
3 changes: 3 additions & 0 deletions node/libs/crypto/src/secp256k1/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,7 @@ fn test_ethereum_example() {
assert_eq!(hex::encode(sig.sig.r().to_bytes()), r, "r matches");
assert_eq!(hex::encode(sig.sig.s().to_bytes()), s, "s matches");
assert_eq!(sig.recid.to_byte(), 0x0, "v is not shifted");

let bz = sig.encode();
assert!(bz[64] == 27 || bz[64] == 28, "v is shifted when encoded");
}

0 comments on commit 6920c29

Please sign in to comment.