Skip to content

Commit

Permalink
bugfix! fix bip137 sig (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanKung authored Apr 28, 2024
1 parent 318ac4a commit e85f00f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"
members = ["crates/*", "examples/native", "examples/snark"]

[workspace.package]
version = "0.6.1"
version = "0.7.0"
edition = "2021"
license = "GPL-3.0"
authors = ["RND <[email protected]>"]
Expand Down
20 changes: 19 additions & 1 deletion crates/core/src/ecc/signers/bip137.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,34 @@ use sha2::Sha256;

use crate::ecc::PublicKey;
use crate::ecc::PublicKeyAddress;
use crate::error::Error;
use crate::error::Result;

/// recover pubkey according to signature.
/// | y-parity | x-order | compression | recovery id | v |
/// |----------|---------------|-------------|-------------|----|
/// | even | less than n | false | 0 | 27 |
/// | odd | less than n | false | 1 | 28 |
/// | even | more than n | false | 2 | 29 |
/// | odd | more than n | false | 3 | 30 |
/// | even | less than n | true | 0 | 31 |
/// | odd | less than n | true | 1 | 32 |
/// | even | more than n | true | 2 | 33 |
/// | odd | more than n | true | 3 | 34 |
pub fn recover(msg: &[u8], sig: impl AsRef<[u8]>) -> Result<PublicKey> {
let mut sig = sig.as_ref().to_vec();
sig.rotate_left(1);
let sig = sig.as_mut_slice();
let sig_byte = array_mut_ref![sig, 0, 65];
let hash = self::magic_hash(msg);
sig_byte[64] -= 27;

if sig_byte[64] >= 27 && sig_byte[64] <= 30 {
sig_byte[64] -= 27;
} else if sig_byte[64] >= 31 && sig_byte[64] <= 34 {
sig_byte[64] -= 31;
} else {
return Err(Error::InvalidRecoverId(sig_byte[64]));
}
crate::ecc::recover_hash(&hash, sig_byte)
}

Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ pub enum Error {
#[error("Failed on verify message signature")]
VerifySignatureFailed,

#[error("ECDSA Invalid recover Id {0}")]
InvalidRecoverId(u8),

#[error("Gzip encode error.")]
GzipEncode,

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"RND <[email protected]>"
],
"description": "Rings is a structured peer-to-peer network implementation using WebRTC, Chord algorithm, and full WebAssembly (WASM) support.\n",
"version": "0.6.1",
"version": "0.7.0",
"license": "GPL-3.0",
"repository": {
"type": "git",
Expand Down

0 comments on commit e85f00f

Please sign in to comment.