Fix eth_sign signature encoding.

This commit is contained in:
Tomasz Drwięga
2017-05-11 12:18:20 +02:00
parent 28b66d1b58
commit fc3e1a4f18
6 changed files with 13 additions and 17 deletions

View File

@@ -45,27 +45,23 @@ impl Signature {
self.0[64]
}
/// Encode the signature into VRS array (V altered to be in "Electrum" notation).
pub fn into_vrs(self) -> [u8; 65] {
let mut vrs = [0u8; 65];
vrs[0] = self.v() + 27;
vrs[1..33].copy_from_slice(self.r());
vrs[33..65].copy_from_slice(self.s());
vrs
/// Encode the signature into RSV array (V altered to be in "Electrum" notation).
pub fn into_electrum(mut self) -> [u8; 65] {
self.0[64] += 27;
self.0
}
/// Parse bytes as a signature encoded as VRS (V in "Electrum" notation).
/// Parse bytes as a signature encoded as RSV (V in "Electrum" notation).
/// May return empty (invalid) signature if given data has invalid length.
pub fn from_vrs(data: &[u8]) -> Self {
pub fn from_electrum(data: &[u8]) -> Self {
if data.len() != 65 || data[0] < 27 {
// fallback to empty (invalid) signature
return Signature::default();
}
let mut sig = [0u8; 65];
sig[0..32].copy_from_slice(&data[1..33]);
sig[32..64].copy_from_slice(&data[33..65]);
sig[64] = data[0] - 27;
sig.copy_from_slice(data);
sig[64] -= 27;
Signature(sig)
}