Skip to content

Commit

Permalink
Use recover_from_prehash
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Jul 16, 2024
1 parent 25f493b commit a9fcaf3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions contracts/ccc-btc-lock/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ pub fn entry() -> Result<(), Error> {
let script = load_script()?;
let pubkey_hash = script.args().raw_data();
if pubkey_hash.len() != 20 {
return Err(Error::WrongPubkey);
return Err(Error::WrongPubkeyHash);
}

let mut to_be_hashed: Vec<u8> = Default::default();
to_be_hashed.push(BTC_PREFIX.len() as u8); // fixed value 24
assert_eq!(BTC_PREFIX.len(), 24);
to_be_hashed.extend(String::from(BTC_PREFIX).into_bytes());
to_be_hashed.push(BTC_PREFIX.len() as u8);
to_be_hashed.extend(BTC_PREFIX.as_bytes());

let sighash_all = generate_sighash_all()?;
let sighash_all_hex = hex::encode(&sighash_all);
Expand All @@ -32,7 +32,7 @@ pub fn entry() -> Result<(), Error> {
to_be_hashed.push(message1.len() as u8);
to_be_hashed.extend(message1.into_bytes());

// double SHA-256 from bitcoin
// Double SHA-256 from bitcoin
let msg = Sha256::digest(&Sha256::digest(&to_be_hashed));

let witness_args = load_witness_args(0, Source::GroupInput)?;
Expand All @@ -48,7 +48,7 @@ pub fn entry() -> Result<(), Error> {
let rec_id = sig[0];
let rec_id = RecoveryId::try_from(rec_id).map_err(|_| Error::InvalidRecoverId)?;
let signature = Signature::from_slice(&sig[1..]).map_err(|_| Error::WrongSignatureFormat)?;
let recovered_key = VerifyingKey::recover_from_msg(&msg, &signature, rec_id)
let recovered_key = VerifyingKey::recover_from_prehash(&msg, &signature, rec_id)
.map_err(|_| Error::CanNotRecover)?;
// TODO: double check its format
let recovered_key_bytes = recovered_key.to_sec1_bytes();
Expand Down
2 changes: 1 addition & 1 deletion contracts/ccc-btc-lock/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub enum Error {
Encoding,
Unknown = 30,
WrongWitnessArgs,
WrongPubkey,
WrongPubkeyHash,
PubkeyHashMismatched,
WrongSignatureFormat,
InvalidRecoverId,
Expand Down

0 comments on commit a9fcaf3

Please sign in to comment.