Skip to content

Commit

Permalink
Add first test
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Jul 17, 2024
1 parent ce4856a commit 03c08b6
Show file tree
Hide file tree
Showing 8 changed files with 1,269 additions and 286 deletions.
5 changes: 4 additions & 1 deletion contracts/ccc-btc-lock/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::Error;
use alloc::{string::String, vec::Vec};
use ckb_lock_helper::{
constant::{BTC_PREFIX, PREFIX, SUFFIX},
generate_sighash_all,
generate_sighash_all, println_hex,
};
use ckb_std::{
ckb_constants::Source,
Expand Down Expand Up @@ -56,9 +56,12 @@ pub fn entry() -> Result<(), Error> {
.map_err(|_| Error::CanNotRecover)?;
// TODO: double check its format
let recovered_key_bytes = recovered_key.to_sec1_bytes();
assert!(recovered_key_bytes.len() == 33);
// RIPEMD160 over SHA-256 for pubkey hashing
let pubkey_hash_result: [u8; 20] =
Ripemd160::digest(&Sha256::digest(&recovered_key_bytes)).into();
println_hex("pubkey result", pubkey_hash_result.as_ref());
println_hex("pubkey expect", pubkey_hash.as_ref());
if pubkey_hash_result.as_ref() != pubkey_hash.as_ref() {
return Err(Error::PubkeyHashMismatched);
}
Expand Down
11 changes: 11 additions & 0 deletions crates/ckb-lock-helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ use ckb_std::debug;
use ckb_std::high_level::{load_tx_hash, load_witness, load_witness_args};
use ckb_std::syscalls::{load_input_by_field, SysError};

pub fn println_hex(name: &str, data: &[u8]) {
debug!(
"{}(len={}): {}",
name,
data.len(),
hex::encode(data)
);
}


pub fn generate_sighash_all() -> Result<[u8; 32], Error> {
let mut blake2b_ctx = new_blake2b_stat();
let tx_hash = load_tx_hash()?;
Expand Down Expand Up @@ -60,6 +70,7 @@ pub fn generate_sighash_all() -> Result<[u8; 32], Error> {
let mut msg = [0u8; 32];
debug!("Hashed {} bytes in sighash_all", blake2b_ctx.count());
blake2b_ctx.finalize(&mut msg);
println_hex("sighash_all_msg", &msg);
Ok(msg)
}

Expand Down
Loading

0 comments on commit 03c08b6

Please sign in to comment.