pub const SIGHASH_DEFAULT: u8 = 0;
pub const SIGHASH_ALL: u8 = 1;
pub const SIGHASH_NONE: u8 = 2;
pub const SIGHASH_SINGLE: u8 = 3;
pub const SIGHASH_ANYONECANPAY: u8 = 128;
pub const SIG_HASH_TYPES: [u8; 6] = [
SIGHASH_ALL,
SIGHASH_NONE,
SIGHASH_SINGLE,
SIGHASH_ALL | SIGHASH_ANYONECANPAY,
SIGHASH_NONE | SIGHASH_ANYONECANPAY,
SIGHASH_SINGLE | SIGHASH_ANYONECANPAY,
];
pub enum PubKeyCheckResult {
Invalid,
Valid { compressed: bool },
}
pub fn check_pub_key(pub_key: &[u8]) -> PubKeyCheckResult {
if pub_key.len() == 33 && (pub_key[0] == 0x02 || pub_key[0] == 0x03) {
PubKeyCheckResult::Valid { compressed: true }
} else if pub_key.len() == 65 && pub_key[0] == 0x04 {
PubKeyCheckResult::Valid { compressed: false }
} else {
PubKeyCheckResult::Invalid
}
}
pub fn is_valid_signature_encoding(sig: &[u8]) -> bool {
if sig.len() < 9 {
return false;
}
if sig.len() > 73 {
return false;
}
if sig[0] != 0x30 {
return false;
}
if sig[1] != sig.len() as u8 - 3 {
return false;
}
let len_r = sig[3] as usize;
if 5 + len_r >= sig.len() {
return false;
}
let len_s = sig[5 + len_r] as usize;
if len_r + len_s + 7 != sig.len() {
return false;
}
if sig[2] != 0x02 {
return false;
}
if len_r == 0 {
return false;
}
if (sig[4] & 0x80) != 0 {
return false;
}
if len_r > 1 && sig[4] == 0x00 && (sig[5] & 0x80) == 0 {
return false;
}
if sig[len_r + 4] != 0x02 {
return false;
}
if len_s == 0 {
return false;
}
if (sig[len_r + 6] & 0x80) != 0 {
return false;
}
if len_s > 1 && sig[len_r + 6] == 0x00 && (sig[len_r + 7] & 0x80) == 0 {
return false;
}
true
}