diff --git a/c/auth.c b/c/auth.c index 48c389f..a4d6016 100644 --- a/c/auth.c +++ b/c/auth.c @@ -219,8 +219,8 @@ static int _recover_secp256k1_pubkey_btc(const uint8_t *sig, size_t sig_len, if (err) return err; out_pubkey[0] = 0; - out_pubkey[1] = BLAKE160_SIZE; - memcpy(out_pubkey + 2, temp, BLAKE160_SIZE); + out_pubkey[1] = AUTH160_SIZE; + memcpy(out_pubkey + 2, temp, AUTH160_SIZE); *out_pubkey_size = 22; } } else { @@ -234,7 +234,7 @@ int validate_signature_ckb(void *prefilled_data, const uint8_t *sig, size_t sig_len, const uint8_t *msg, size_t msg_len, uint8_t *output, size_t *output_len) { int ret = 0; - if (*output_len < BLAKE160_SIZE) { + if (*output_len < AUTH160_SIZE) { return ERROR_INVALID_ARG; } uint8_t out_pubkey[SECP256K1_PUBKEY_SIZE]; @@ -248,8 +248,8 @@ int validate_signature_ckb(void *prefilled_data, const uint8_t *sig, blake2b_update(&ctx, out_pubkey, out_pubkey_size); blake2b_final(&ctx, out_pubkey, BLAKE2B_BLOCK_SIZE); - memcpy(output, out_pubkey, BLAKE160_SIZE); - *output_len = BLAKE160_SIZE; + memcpy(output, out_pubkey, AUTH160_SIZE); + *output_len = AUTH160_SIZE; return ret; } @@ -258,8 +258,8 @@ int validate_signature_eth(void *prefilled_data, const uint8_t *sig, size_t sig_len, const uint8_t *msg, size_t msg_len, uint8_t *output, size_t *output_len) { int ret = 0; - if (*output_len < BLAKE160_SIZE) { - return SECP256K1_PUBKEY_SIZE; + if (*output_len < AUTH160_SIZE) { + return ERROR_INVALID_ARG; } uint8_t out_pubkey[UNCOMPRESSED_SECP256K1_PUBKEY_SIZE]; size_t out_pubkey_size = UNCOMPRESSED_SECP256K1_PUBKEY_SIZE; @@ -291,8 +291,8 @@ int validate_signature_eth(void *prefilled_data, const uint8_t *sig, keccak_update(&sha3_ctx, &out_pubkey[1], out_pubkey_size - 1); keccak_final(&sha3_ctx, out_pubkey); - memcpy(output, &out_pubkey[12], BLAKE160_SIZE); - *output_len = BLAKE160_SIZE; + memcpy(output, &out_pubkey[12], AUTH160_SIZE); + *output_len = AUTH160_SIZE; return ret; } @@ -300,8 +300,8 @@ int validate_signature_eth(void *prefilled_data, const uint8_t *sig, int validate_signature_eos(void *prefilled_data, const uint8_t *sig, size_t sig_len, const uint8_t *msg, size_t msg_len, uint8_t *output, size_t *output_len) { int err = 0; - if (*output_len < BLAKE160_SIZE) { - return SECP256K1_PUBKEY_SIZE; + if (*output_len < AUTH160_SIZE) { + return ERROR_INVALID_ARG; } uint8_t out_pubkey[UNCOMPRESSED_SECP256K1_PUBKEY_SIZE]; size_t out_pubkey_size = UNCOMPRESSED_SECP256K1_PUBKEY_SIZE; @@ -313,8 +313,8 @@ int validate_signature_eos(void *prefilled_data, const uint8_t *sig, size_t sig_ blake2b_update(&ctx, out_pubkey, out_pubkey_size); blake2b_final(&ctx, out_pubkey, BLAKE2B_BLOCK_SIZE); - memcpy(output, out_pubkey, BLAKE160_SIZE); - *output_len = BLAKE160_SIZE; + memcpy(output, out_pubkey, AUTH160_SIZE); + *output_len = AUTH160_SIZE; exit: return err; } @@ -323,8 +323,8 @@ int validate_signature_btc(void *prefilled_data, const uint8_t *sig, size_t sig_len, const uint8_t *msg, size_t msg_len, uint8_t *output, size_t *output_len) { int err = 0; - if (*output_len < BLAKE160_SIZE) { - return SECP256K1_PUBKEY_SIZE; + if (*output_len < AUTH160_SIZE) { + return ERROR_INVALID_ARG; } uint8_t out_pubkey[UNCOMPRESSED_SECP256K1_PUBKEY_SIZE]; size_t out_pubkey_size = UNCOMPRESSED_SECP256K1_PUBKEY_SIZE; @@ -342,8 +342,8 @@ int validate_signature_btc(void *prefilled_data, const uint8_t *sig, err = md_string(md_info, temp, SHA256_SIZE, temp); CHECK(err); - memcpy(output, temp, BLAKE160_SIZE); - *output_len = BLAKE160_SIZE; + memcpy(output, temp, AUTH160_SIZE); + *output_len = AUTH160_SIZE; exit: return err; @@ -356,8 +356,8 @@ int validate_signature_schnorr(void *prefilled_data, const uint8_t *sig, int err = 0; int success = 0; - if (*output_len < BLAKE160_SIZE) { - return SECP256K1_PUBKEY_SIZE; + if (*output_len < AUTH160_SIZE) { + return ERROR_INVALID_ARG; } if (sig_len != SCHNORR_SIGNATURE_SIZE || msg_len != 32) { return ERROR_INVALID_ARG; @@ -380,8 +380,8 @@ int validate_signature_schnorr(void *prefilled_data, const uint8_t *sig, blake2b_update(&blake2b_ctx, sig, SCHNORR_PUBKEY_SIZE); blake2b_final(&blake2b_ctx, temp, BLAKE2B_BLOCK_SIZE); - memcpy(output, temp, BLAKE160_SIZE); - *output_len = BLAKE160_SIZE; + memcpy(output, temp, AUTH160_SIZE); + *output_len = AUTH160_SIZE; return 0; } @@ -392,8 +392,8 @@ int validate_signature_cardano(void *prefilled_data, const uint8_t *sig, size_t *output_len) { int err = 0; - if (*output_len < BLAKE160_SIZE) { - return SECP256K1_PUBKEY_SIZE; + if (*output_len < AUTH160_SIZE) { + return ERROR_INVALID_ARG; } CardanoSignatureData cardano_data; @@ -415,8 +415,8 @@ int validate_signature_cardano(void *prefilled_data, const uint8_t *sig, sizeof(cardano_data.public_key)); blake2b_final(&ctx, pubkey_hash, sizeof(pubkey_hash)); - memcpy(output, pubkey_hash, BLAKE160_SIZE); - *output_len = BLAKE160_SIZE; + memcpy(output, pubkey_hash, AUTH160_SIZE); + *output_len = AUTH160_SIZE; exit: return err; } @@ -426,8 +426,8 @@ int validate_signature_ripple(void *prefilled_data, const uint8_t *sig, size_t msg_len, uint8_t *output, size_t *output_len) { int err = 0; - if (*output_len < BLAKE160_SIZE) { - return SECP256K1_PUBKEY_SIZE; + if (*output_len < AUTH160_SIZE) { + return ERROR_INVALID_ARG; } uint8_t out_sign_msg_buf[sig_len]; @@ -442,7 +442,7 @@ int validate_signature_ripple(void *prefilled_data, const uint8_t *sig, CHECK(verify_ripple(&sign_data)); get_ripple_pubkey_hash(sign_data.public_key, output); - *output_len = BLAKE160_SIZE; + *output_len = AUTH160_SIZE; exit: return err; } @@ -603,8 +603,8 @@ int validate_signature_monero(void *prefilled_data, const uint8_t *sig, blake2b_update(&ctx, mode_ptr, 1 + MONERO_PUBKEY_SIZE * 2); blake2b_final(&ctx, pubkey_hash, sizeof(pubkey_hash)); - memcpy(output, pubkey_hash, BLAKE160_SIZE); - *output_len = BLAKE160_SIZE; + memcpy(output, pubkey_hash, AUTH160_SIZE); + *output_len = AUTH160_SIZE; exit: return err; } @@ -663,8 +663,8 @@ int validate_signature_solana(void *prefilled_data, const uint8_t *sig, blake2b_update(&ctx, pub_key_ptr, SOLANA_PUBKEY_SIZE); blake2b_final(&ctx, pubkey_hash, sizeof(pubkey_hash)); - memcpy(output, pubkey_hash, BLAKE160_SIZE); - *output_len = BLAKE160_SIZE; + memcpy(output, pubkey_hash, AUTH160_SIZE); + *output_len = AUTH160_SIZE; exit: return err; } @@ -823,7 +823,7 @@ bool is_lock_script_hash_present(uint8_t *lock_script_hash) { break; } - if (memcmp(lock_script_hash, buff, BLAKE160_SIZE) == 0) { + if (memcmp(lock_script_hash, buff, AUTH160_SIZE) == 0) { return true; } i += 1; @@ -844,13 +844,13 @@ static int verify(uint8_t *pubkey_hash, const uint8_t *sig, uint32_t sig_len, err = convert(msg, msg_len, new_msg, sizeof(new_msg)); CHECK(err); - uint8_t output_pubkey_hash[BLAKE160_SIZE]; - size_t output_len = BLAKE160_SIZE; + uint8_t output_pubkey_hash[AUTH160_SIZE]; + size_t output_len = AUTH160_SIZE; err = func(NULL, sig, sig_len, new_msg, sizeof(new_msg), output_pubkey_hash, &output_len); CHECK(err); - int same = memcmp(pubkey_hash, output_pubkey_hash, BLAKE160_SIZE); + int same = memcmp(pubkey_hash, output_pubkey_hash, AUTH160_SIZE); CHECK2(same == 0, ERROR_MISMATCHED); exit: @@ -903,7 +903,7 @@ int verify_multisig(const uint8_t *lock_bytes, size_t lock_bytes_len, } // Based on the number of public keys and thresholds, we can calculate // the required length of the lock field. - size_t multisig_script_len = FLAGS_SIZE + BLAKE160_SIZE * pubkeys_cnt; + size_t multisig_script_len = FLAGS_SIZE + AUTH160_SIZE * pubkeys_cnt; size_t signatures_len = SIGNATURE_SIZE * threshold; size_t required_lock_len = multisig_script_len + signatures_len; if (lock_bytes_len != required_lock_len) { @@ -917,7 +917,7 @@ int verify_multisig(const uint8_t *lock_bytes, size_t lock_bytes_len, blake2b_update(&blake2b_ctx, lock_bytes, multisig_script_len); blake2b_final(&blake2b_ctx, temp, BLAKE2B_BLOCK_SIZE); - if (memcmp(hash, temp, BLAKE160_SIZE) != 0) { + if (memcmp(hash, temp, AUTH160_SIZE) != 0) { return ERROR_MULTSIG_SCRIPT_HASH; } @@ -977,8 +977,8 @@ int verify_multisig(const uint8_t *lock_bytes, size_t lock_bytes_len, if (used_signatures[i] == 1) { continue; } - if (memcmp(&lock_bytes[FLAGS_SIZE + i * BLAKE160_SIZE], - calculated_pubkey_hash, BLAKE160_SIZE) != 0) { + if (memcmp(&lock_bytes[FLAGS_SIZE + i * AUTH160_SIZE], + calculated_pubkey_hash, AUTH160_SIZE) != 0) { continue; } matched = 1; @@ -1017,7 +1017,7 @@ __attribute__((visibility("default"))) int ckb_auth_validate( CHECK2(signature != NULL, ERROR_INVALID_ARG); CHECK2(message != NULL, ERROR_INVALID_ARG); CHECK2(message_size > 0, ERROR_INVALID_ARG); - CHECK2(pubkey_hash_size == BLAKE160_SIZE, ERROR_INVALID_ARG); + CHECK2(pubkey_hash_size == AUTH160_SIZE, ERROR_INVALID_ARG); if (auth_algorithm_id == AuthAlgorithmIdCkb) { CHECK2(signature_size == SECP256K1_SIGNATURE_SIZE, ERROR_INVALID_ARG); diff --git a/c/auth_libecc.c b/c/auth_libecc.c index a7d673e..3cdfee8 100644 --- a/c/auth_libecc.c +++ b/c/auth_libecc.c @@ -23,7 +23,7 @@ int validate_signature_secp256r1(void *prefilled_data, const uint8_t *sig, size_t *output_len) { int err = 0; - if (*output_len < BLAKE160_SIZE) { + if (*output_len < AUTH160_SIZE) { return ERROR_INVALID_ARG; } CHECK2(msg_len == BLAKE2B_BLOCK_SIZE, ERROR_INVALID_ARG); @@ -39,8 +39,8 @@ int validate_signature_secp256r1(void *prefilled_data, const uint8_t *sig, blake2b_update(&ctx, pub_key_ptr, SECP256R1_PUBKEY_SIZE); blake2b_final(&ctx, pubkey_hash, sizeof(pubkey_hash)); - memcpy(output, pubkey_hash, BLAKE160_SIZE); - *output_len = BLAKE160_SIZE; + memcpy(output, pubkey_hash, AUTH160_SIZE); + *output_len = AUTH160_SIZE; exit: return err; } @@ -62,13 +62,13 @@ static int verify(uint8_t *pubkey_hash, const uint8_t *sig, uint32_t sig_len, err = convert(msg, msg_len, new_msg, sizeof(new_msg)); CHECK(err); - uint8_t output_pubkey_hash[BLAKE160_SIZE]; - size_t output_len = BLAKE160_SIZE; + uint8_t output_pubkey_hash[AUTH160_SIZE]; + size_t output_len = AUTH160_SIZE; err = func(NULL, sig, sig_len, new_msg, sizeof(new_msg), output_pubkey_hash, &output_len); CHECK(err); - int same = memcmp(pubkey_hash, output_pubkey_hash, BLAKE160_SIZE); + int same = memcmp(pubkey_hash, output_pubkey_hash, AUTH160_SIZE); CHECK2(same == 0, ERROR_MISMATCHED); exit: @@ -85,7 +85,7 @@ __attribute__((visibility("default"))) int ckb_auth_validate( CHECK2(signature != NULL, ERROR_INVALID_ARG); CHECK2(message != NULL, ERROR_INVALID_ARG); CHECK2(message_size > 0, ERROR_INVALID_ARG); - CHECK2(pubkey_hash_size == BLAKE160_SIZE, ERROR_INVALID_ARG); + CHECK2(pubkey_hash_size == AUTH160_SIZE, ERROR_INVALID_ARG); if (auth_algorithm_id == AuthAlgorithmIdSecp256R1) { err = verify(pubkey_hash, signature, signature_size, message, diff --git a/c/ckb_auth.h b/c/ckb_auth.h index 1fe8d70..10a1598 100644 --- a/c/ckb_auth.h +++ b/c/ckb_auth.h @@ -28,7 +28,7 @@ } while (0) #define CKB_AUTH_LEN 21 -#define BLAKE160_SIZE 20 +#define AUTH160_SIZE 20 #define BLAKE2B_BLOCK_SIZE 32 #define OFFSETOF(TYPE, ELEMENT) ((size_t) & (((TYPE *)0)->ELEMENT)) @@ -295,7 +295,7 @@ static int ckb_auth_validate_with_func(int argc, char *argv[], ckb_auth_validate if (algorithm_id_len != 2 || signature_len % 2 != 0 || message_len != BLAKE2B_BLOCK_SIZE * 2 || - pubkey_hash_len != BLAKE160_SIZE * 2) { + pubkey_hash_len != AUTH160_SIZE * 2) { return ERROR_SPAWN_INVALID_LENGTH; } @@ -307,7 +307,7 @@ static int ckb_auth_validate_with_func(int argc, char *argv[], ckb_auth_validate uint8_t algorithm_id = 0; uint8_t signature[signature_len / 2]; uint8_t message[BLAKE2B_BLOCK_SIZE]; - uint8_t pubkey_hash[BLAKE160_SIZE]; + uint8_t pubkey_hash[AUTH160_SIZE]; // auth algorithm id CHECK2( @@ -328,7 +328,7 @@ static int ckb_auth_validate_with_func(int argc, char *argv[], ckb_auth_validate // public key hash CHECK2(!ckb_hex2bin(ARGV_PUBKEY_HASH, pubkey_hash, pubkey_hash_len, &pubkey_hash_len) && - pubkey_hash_len == BLAKE160_SIZE, + pubkey_hash_len == AUTH160_SIZE, ERROR_SPAWN_INVALID_PUBKEY); err = validate_func(algorithm_id, signature, signature_len, message, diff --git a/ckb-auth-rs/Cargo.toml b/ckb-auth-rs/Cargo.toml index 09d0a56..d431681 100644 --- a/ckb-auth-rs/Cargo.toml +++ b/ckb-auth-rs/Cargo.toml @@ -13,5 +13,5 @@ ckb2023 = ["ckb-std/ckb2023"] ckb-std = "0.14.3" [target.'cfg(target_arch = "riscv64")'.dependencies] -log = { version = "0.4.17", default-features = false} hex = { version = "0.4.3", default-features = false, features = ["alloc"]} +blake2b-rs = "0.2.0" diff --git a/ckb-auth-rs/src/ckb_auth.rs b/ckb-auth-rs/src/ckb_auth.rs index 4e57c74..b14591c 100644 --- a/ckb-auth-rs/src/ckb_auth.rs +++ b/ckb-auth-rs/src/ckb_auth.rs @@ -1,62 +1,18 @@ extern crate alloc; -use crate::{CkbAuthType, CkbAuthTypesError, EntryCategoryType}; +use crate::{CkbAuthError, CkbAuthType, CkbEntryType, EntryCategoryType}; use alloc::collections::BTreeMap; use alloc::ffi::CString; -use alloc::ffi::NulError; use alloc::format; use alloc::vec::Vec; +#[cfg(feature = "ckb2023")] +use ckb_std::high_level::spawn_cell; use ckb_std::{ ckb_types::core::ScriptHashType, dynamic_loading_c_impl::{CKBDLContext, Library, Symbol}, - syscalls::SysError, }; -#[cfg(feature = "ckb2023")] -use ckb_std::high_level::spawn_cell; use core::mem::size_of_val; use hex::encode; -use log::info; - -#[derive(Debug)] -pub enum CkbAuthError { - UnknowAlgorithmID, - DynamicLinkingUninit, - LoadDLError, - LoadDLFuncError, - RunDLError, - ExecError(SysError), - SignatureMissing, - EncodeArgs, -} - -impl From for CkbAuthError { - fn from(err: SysError) -> Self { - info!("Exec error: {:?}", err); - Self::ExecError(err) - } -} - -impl From for CkbAuthError { - fn from(err: NulError) -> Self { - info!("Exec encode args failed: {:?}", err); - Self::EncodeArgs - } -} - -impl From for CkbAuthError { - fn from(err: CkbAuthTypesError) -> Self { - match err { - CkbAuthTypesError::UnknowAlgorithmID => Self::UnknowAlgorithmID, - CkbAuthTypesError::EncodeArgs => Self::EncodeArgs, - } - } -} - -pub struct CkbEntryType { - pub code_hash: [u8; 32], - pub hash_type: ScriptHashType, - pub entry_category: EntryCategoryType, -} pub fn ckb_auth( entry: &CkbEntryType, @@ -151,7 +107,6 @@ impl CKBDLLoader { }; if !has_lib { - info!("loading library"); let size = size_of_val(&self.context); let lib = self .context @@ -206,9 +161,6 @@ fn ckb_auth_dl( match rc_code { 0 => Ok(()), - _ => { - info!("run auth error({}) in dynamic linking", rc_code); - Err(CkbAuthError::RunDLError) - } + _ => Err(CkbAuthError::RunDLError), } } diff --git a/examples/auth-rust-demo/contracts/auth-rust-demo/src/utils/generate_sighash_all.rs b/ckb-auth-rs/src/generate_sighash_all.rs similarity index 79% rename from examples/auth-rust-demo/contracts/auth-rust-demo/src/utils/generate_sighash_all.rs rename to ckb-auth-rs/src/generate_sighash_all.rs index bc77b4e..b56b281 100644 --- a/examples/auth-rust-demo/contracts/auth-rust-demo/src/utils/generate_sighash_all.rs +++ b/ckb-auth-rs/src/generate_sighash_all.rs @@ -1,15 +1,13 @@ -use crate::error::Error; -use crate::utils::new_blake2b; -use blake2b_rs::Blake2b; +use crate::CkbAuthError; +use blake2b_rs::{Blake2b, Blake2bBuilder}; use ckb_std::ckb_constants::{InputField, Source}; use ckb_std::high_level::load_tx_hash; -use ckb_std::syscalls::{load_cell, load_input_by_field, load_witness, SysError}; +use ckb_std::syscalls::{load_input_by_field, load_witness, SysError}; pub const MAX_WITNESS_SIZE: usize = 32768; pub const ONE_BATCH_SIZE: usize = 32768; -#[allow(dead_code)] -pub fn generate_sighash_all() -> Result<[u8; 32], Error> { +pub fn generate_sighash_all() -> Result<[u8; 32], CkbAuthError> { let mut temp = [0u8; MAX_WITNESS_SIZE]; // Load witness of first input. @@ -21,11 +19,11 @@ pub fn generate_sighash_all() -> Result<[u8; 32], Error> { // Load signature. if read_len < 20 { - return Err(Error::Encoding); + return Err(CkbAuthError::GenerateSigHash); } let lock_length = u32::from_le_bytes(temp[16..20].try_into().unwrap()) as usize; if read_len < 20 + lock_length { - return Err(Error::Encoding); + return Err(CkbAuthError::GenerateSigHash); } // Clear lock field to zero, then digest the first witness // lock_bytes_seg.ptr actually points to the memory in temp buffer. @@ -103,7 +101,7 @@ fn load_and_hash_witness( Ok(()) } -fn calculate_inputs_len() -> Result { +fn calculate_inputs_len() -> Result { let mut temp = [0u8; 8]; let mut i = 0; loop { @@ -117,18 +115,9 @@ fn calculate_inputs_len() -> Result { Ok(i) } -#[allow(dead_code)] -fn calculate_outputs_len() -> Result { - let mut temp = [0u8; 8]; - let mut i = 0; - loop { - let sysret = load_cell(&mut temp, 0, i, Source::Output); - match sysret { - Err(SysError::IndexOutOfBound) => break, - Err(SysError::LengthNotEnough(_)) => i += 1, - Err(x) => return Err(x.into()), - Ok(_) => i += 1, - } - } - Ok(i) +pub const CKB_PERSONALIZATION: &[u8] = b"ckb-default-hash"; +pub fn new_blake2b() -> Blake2b { + Blake2bBuilder::new(32) + .personal(CKB_PERSONALIZATION) + .build() } diff --git a/ckb-auth-rs/src/lib.rs b/ckb-auth-rs/src/lib.rs index dfb5f8e..87571b7 100644 --- a/ckb-auth-rs/src/lib.rs +++ b/ckb-auth-rs/src/lib.rs @@ -3,7 +3,25 @@ extern crate alloc; use core::mem::transmute; #[cfg(target_arch = "riscv64")] -pub mod ckb_auth; +mod ckb_auth; + +#[cfg(target_arch = "riscv64")] +pub use ckb_auth::ckb_auth; + +#[cfg(target_arch = "riscv64")] +mod generate_sighash_all; + +#[cfg(target_arch = "riscv64")] +pub use crate::generate_sighash_all::generate_sighash_all; + +#[cfg(target_arch = "riscv64")] +use alloc::ffi::NulError; +#[cfg(target_arch = "riscv64")] +use ckb_std::{ckb_types::core::ScriptHashType, error::SysError}; +#[cfg(not(target_arch = "riscv64"))] +type SysError = u64; +#[cfg(not(target_arch = "riscv64"))] +type ScriptHashType = u8; #[derive(Clone)] pub enum AuthAlgorithmIdType { @@ -26,11 +44,6 @@ pub enum AuthAlgorithmIdType { OwnerLock = 0xFC, } -pub enum CkbAuthTypesError { - UnknowAlgorithmID, - EncodeArgs, -} - impl Into for AuthAlgorithmIdType { fn into(self) -> u8 { self as u8 @@ -38,7 +51,7 @@ impl Into for AuthAlgorithmIdType { } impl TryFrom for AuthAlgorithmIdType { - type Error = CkbAuthTypesError; + type Error = CkbAuthError; fn try_from(value: u8) -> Result { if (value >= AuthAlgorithmIdType::Ckb.into() && value <= AuthAlgorithmIdType::Iso97962.into()) @@ -46,11 +59,38 @@ impl TryFrom for AuthAlgorithmIdType { { Ok(unsafe { transmute(value) }) } else { - Err(CkbAuthTypesError::UnknowAlgorithmID) + Err(CkbAuthError::UnknownAlgorithmID) } } } +#[derive(Debug)] +pub enum CkbAuthError { + UnknownAlgorithmID, + DynamicLinkingUninit, + LoadDLError, + LoadDLFuncError, + RunDLError, + ExecError(SysError), + SignatureMissing, + EncodeArgs, + GenerateSigHash, +} + +#[cfg(target_arch = "riscv64")] +impl From for CkbAuthError { + fn from(err: SysError) -> Self { + Self::ExecError(err) + } +} + +#[cfg(target_arch = "riscv64")] +impl From for CkbAuthError { + fn from(_err: NulError) -> Self { + Self::EncodeArgs + } +} + #[derive(Clone)] pub enum EntryCategoryType { // Exec = 0, @@ -60,14 +100,14 @@ pub enum EntryCategoryType { } impl TryFrom for EntryCategoryType { - type Error = CkbAuthTypesError; + type Error = CkbAuthError; fn try_from(value: u8) -> Result { match value { // 0 => Ok(Self::Exec), 1 => Ok(Self::DynamicLinking), #[cfg(feature = "ckb2023")] 2 => Ok(Self::Spawn), - _ => Err(CkbAuthTypesError::EncodeArgs), + _ => Err(CkbAuthError::EncodeArgs), } } } @@ -76,3 +116,9 @@ pub struct CkbAuthType { pub algorithm_id: AuthAlgorithmIdType, pub pubkey_hash: [u8; 20], } + +pub struct CkbEntryType { + pub code_hash: [u8; 32], + pub hash_type: ScriptHashType, + pub entry_category: EntryCategoryType, +} diff --git a/examples/auth-rust-demo/Cargo.lock b/examples/auth-rust-demo/Cargo.lock index 4957e4a..075d44c 100644 --- a/examples/auth-rust-demo/Cargo.lock +++ b/examples/auth-rust-demo/Cargo.lock @@ -6,7 +6,6 @@ version = 3 name = "auth-rust-demo" version = "0.1.0" dependencies = [ - "blake2b-rs", "ckb-auth-rs", "ckb-std", "hex", @@ -54,9 +53,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "ckb-auth-rs" version = "0.1.0" dependencies = [ + "blake2b-rs", "ckb-std", "hex", - "log", ] [[package]] diff --git a/examples/auth-rust-demo/contracts/auth-rust-demo/Cargo.toml b/examples/auth-rust-demo/contracts/auth-rust-demo/Cargo.toml index ca0f2a5..65d4a26 100644 --- a/examples/auth-rust-demo/contracts/auth-rust-demo/Cargo.toml +++ b/examples/auth-rust-demo/contracts/auth-rust-demo/Cargo.toml @@ -10,4 +10,3 @@ ckb-std = "0.14.3" ckb-auth-rs = { path = "../../../../ckb-auth-rs", features = ["ckb2023"] } log = { version = "0.4.17", default-features = false } hex = { version = "0.4.3", default-features = false, features = ["alloc"]} -blake2b-rs = "0.2.0" diff --git a/examples/auth-rust-demo/contracts/auth-rust-demo/src/entry.rs b/examples/auth-rust-demo/contracts/auth-rust-demo/src/entry.rs index ffa0bb4..46d5283 100644 --- a/examples/auth-rust-demo/contracts/auth-rust-demo/src/entry.rs +++ b/examples/auth-rust-demo/contracts/auth-rust-demo/src/entry.rs @@ -1,5 +1,3 @@ -extern crate alloc; -use log::info; // Import from `core` instead of from `std` since we are in no-std mode use core::result::Result; @@ -12,10 +10,9 @@ use core::result::Result; use crate::error::Error; -use crate::utils::generate_sighash_all::generate_sighash_all; use ckb_auth_rs::{ - ckb_auth::{ckb_auth, CkbAuthError, CkbEntryType}, - AuthAlgorithmIdType, CkbAuthType, EntryCategoryType, + ckb_auth, generate_sighash_all, AuthAlgorithmIdType, CkbAuthError, CkbAuthType, CkbEntryType, + EntryCategoryType, }; use ckb_std::{ ckb_constants::Source, @@ -26,7 +23,6 @@ use ckb_std::{ // use ckb_std::debug; pub fn main() -> Result<(), Error> { - info!("auth-script-test entry"); let mut pubkey_hash = [0u8; 20]; let auth_id: u8; let entry_type: u8; @@ -36,8 +32,6 @@ pub fn main() -> Result<(), Error> { // get message let message = generate_sighash_all().map_err(|_| Error::GeneratedMsgError)?; let signature = { - info!("run as standalone script"); - let script = load_script()?; let args: Bytes = script.args().unpack(); if args.len() != 55 { diff --git a/examples/auth-rust-demo/contracts/auth-rust-demo/src/error.rs b/examples/auth-rust-demo/contracts/auth-rust-demo/src/error.rs index b35ff0a..6b812ab 100644 --- a/examples/auth-rust-demo/contracts/auth-rust-demo/src/error.rs +++ b/examples/auth-rust-demo/contracts/auth-rust-demo/src/error.rs @@ -1,4 +1,4 @@ -use ckb_auth_rs::ckb_auth::CkbAuthError; +use ckb_auth_rs::CkbAuthError; use ckb_std::error::SysError; /// Error @@ -39,7 +39,7 @@ impl From for Error { fn from(value: CkbAuthError) -> Self { use CkbAuthError::*; match value { - UnknowAlgorithmID => Self::Encoding, + UnknownAlgorithmID => Self::Encoding, LoadDLError => Self::LoadDLError, LoadDLFuncError => Self::LoadDLError, RunDLError => Self::RunAuthError, diff --git a/examples/auth-rust-demo/contracts/auth-rust-demo/src/main.rs b/examples/auth-rust-demo/contracts/auth-rust-demo/src/main.rs index 0f7e860..4f6f9ab 100644 --- a/examples/auth-rust-demo/contracts/auth-rust-demo/src/main.rs +++ b/examples/auth-rust-demo/contracts/auth-rust-demo/src/main.rs @@ -10,7 +10,6 @@ // define modules mod entry; mod error; -mod utils; #[cfg(test)] extern crate alloc; diff --git a/examples/auth-rust-demo/contracts/auth-rust-demo/src/utils/blake2b.rs b/examples/auth-rust-demo/contracts/auth-rust-demo/src/utils/blake2b.rs deleted file mode 100644 index e69de29..0000000 diff --git a/examples/auth-rust-demo/contracts/auth-rust-demo/src/utils/mod.rs b/examples/auth-rust-demo/contracts/auth-rust-demo/src/utils/mod.rs deleted file mode 100644 index 2d3e531..0000000 --- a/examples/auth-rust-demo/contracts/auth-rust-demo/src/utils/mod.rs +++ /dev/null @@ -1,10 +0,0 @@ -pub mod generate_sighash_all; - -pub use blake2b_rs::{Blake2b, Blake2bBuilder}; - -pub const CKB_PERSONALIZATION: &[u8] = b"ckb-default-hash"; -pub fn new_blake2b() -> Blake2b { - Blake2bBuilder::new(32) - .personal(CKB_PERSONALIZATION) - .build() -} diff --git a/tests/auth-c-tests/Cargo.lock b/tests/auth-c-tests/Cargo.lock index 2d6c846..e45465d 100644 --- a/tests/auth-c-tests/Cargo.lock +++ b/tests/auth-c-tests/Cargo.lock @@ -812,9 +812,9 @@ dependencies = [ name = "ckb-auth-rs" version = "0.1.0" dependencies = [ + "blake2b-rs", "ckb-std", "hex", - "log", ] [[package]] diff --git a/tests/auth-spawn-tests/Cargo.lock b/tests/auth-spawn-tests/Cargo.lock index 701191f..6590a92 100644 --- a/tests/auth-spawn-tests/Cargo.lock +++ b/tests/auth-spawn-tests/Cargo.lock @@ -358,9 +358,9 @@ dependencies = [ name = "ckb-auth-rs" version = "0.1.0" dependencies = [ + "blake2b-rs", "ckb-std", "hex", - "log 0.4.20", ] [[package]] diff --git a/tools/ckb-auth-cli/Cargo.lock b/tools/ckb-auth-cli/Cargo.lock index 10528fb..3160d57 100644 --- a/tools/ckb-auth-cli/Cargo.lock +++ b/tools/ckb-auth-cli/Cargo.lock @@ -323,9 +323,9 @@ dependencies = [ name = "ckb-auth-rs" version = "0.1.0" dependencies = [ + "blake2b-rs", "ckb-std", "hex", - "log", ] [[package]]