Skip to content

Commit 95e6cfc

Browse files
authored
refactor: refactor type_id for variable length support (#135)
update ckb-x64-simulator version
1 parent 9b5e8f0 commit 95e6cfc

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ckb-hash = { version = "0.200.0", default-features = false, features = [
4747
], optional = true }
4848

4949
buddy-alloc = { version = "0.6", optional = true }
50-
ckb-x64-simulator = { version = "0.11", optional = true }
50+
ckb-x64-simulator = { version = "0.11.1", optional = true }
5151
gcd = "2.3"
5252
log = { version = "0.4", optional = true, default-features = false }
5353

contracts/exec-callee/exec-callee-dbg/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/type_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ entry!(main);
88
default_alloc!();
99

1010
fn main() -> i8 {
11-
match check_type_id(0) {
11+
match check_type_id(0, 32) {
1212
Ok(_) => 0,
1313
Err(_) => -10,
1414
}

src/type_id.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
high_level::{QueryIter, load_cell_type_hash, load_input, load_script, load_script_hash},
1515
syscalls::load_cell,
1616
};
17+
use alloc::vec::Vec;
1718
use ckb_hash::new_blake2b;
1819
use ckb_types::prelude::Entity;
1920

@@ -44,7 +45,7 @@ fn locate_index() -> Result<usize, SysError> {
4445
///
4546
/// # Arguments
4647
///
47-
/// * `type_id` - A 32-byte array representing the Type ID to validate.
48+
/// * `type_id` - A slice representing the Type ID to validate.
4849
///
4950
/// # Returns
5051
///
@@ -62,9 +63,9 @@ fn locate_index() -> Result<usize, SysError> {
6263
/// use ckb_std::type_id::validate_type_id;
6364
///
6465
/// let type_id = [0u8; 32];
65-
/// validate_type_id(type_id)?;
66+
/// validate_type_id(&type_id)?;
6667
/// ```
67-
pub fn validate_type_id(type_id: [u8; 32]) -> Result<(), SysError> {
68+
pub fn validate_type_id(type_id: &[u8]) -> Result<(), SysError> {
6869
// after this checking, there are 3 cases:
6970
// 1. 0 input cell and 1 output cell, it's minting operation
7071
// 2. 1 input cell and 1 output cell, it's transfer operation
@@ -82,37 +83,38 @@ pub fn validate_type_id(type_id: [u8; 32]) -> Result<(), SysError> {
8283
blake2b.update(&index.to_le_bytes());
8384
let mut ret = [0; 32];
8485
blake2b.finalize(&mut ret);
85-
86-
if ret != type_id {
86+
if type_id.len() > ret.len() {
87+
return Err(SysError::TypeIDError);
88+
}
89+
if &ret[..type_id.len()] != type_id {
8790
return Err(SysError::TypeIDError);
8891
}
8992
}
9093
// case 2 & 3: for the `else` part, it's transfer operation or burning operation
9194
Ok(())
9295
}
9396

94-
fn load_id_from_args(offset: usize) -> Result<[u8; 32], SysError> {
97+
fn load_id_from_args(offset: usize, length: usize) -> Result<Vec<u8>, SysError> {
9598
let script = load_script()?;
9699
let args = script.as_reader().args();
97100
let args_data = args.raw_data();
98101

99-
args_data
100-
.get(offset..offset + 32)
102+
Ok(args_data
103+
.get(offset..offset + length)
101104
.ok_or(SysError::TypeIDError)?
102-
.try_into()
103-
.map_err(|_| SysError::TypeIDError)
105+
.to_vec())
104106
}
105107

106108
///
107109
/// Validates that the script follows the Type ID rule.
108110
///
109-
/// This function checks if the Type ID (a 32-byte value) stored in the script's `args`
111+
/// This function checks if the Type ID (variable length) stored in the script's `args`
110112
/// at the specified offset is valid according to the Type ID rules.
111113
///
112114
/// # Arguments
113115
///
114116
/// * `offset` - The byte offset in the script's `args` where the Type ID starts.
115-
///
117+
/// * `length` - The length of Type ID
116118
/// # Returns
117119
///
118120
/// * `Ok(())` if the Type ID is valid.
@@ -125,7 +127,7 @@ fn load_id_from_args(offset: usize) -> Result<[u8; 32], SysError> {
125127
///
126128
/// fn main() -> Result<(), ckb_std::error::SysError> {
127129
/// // Check the Type ID stored at the beginning of the script args
128-
/// check_type_id(0)?;
130+
/// check_type_id(0, 32)?;
129131
/// Ok(())
130132
/// }
131133
/// ```
@@ -134,8 +136,8 @@ fn load_id_from_args(offset: usize) -> Result<[u8; 32], SysError> {
134136
///
135137
/// This function internally calls `load_id_from_args` to retrieve the Type ID
136138
/// and then `validate_type_id` to perform the actual validation.
137-
pub fn check_type_id(offset: usize) -> Result<(), SysError> {
138-
let type_id = load_id_from_args(offset)?;
139-
validate_type_id(type_id)?;
139+
pub fn check_type_id(offset: usize, length: usize) -> Result<(), SysError> {
140+
let type_id = load_id_from_args(offset, length)?;
141+
validate_type_id(&type_id)?;
140142
Ok(())
141143
}

test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2024"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
ckb-x64-simulator = "0.11"
10+
ckb-x64-simulator = "0.11.1"
1111
ckb-testtool = "0.15"
1212
serde_json = "1.0"
1313
ckb-mock-tx-types = "0.200.0"

0 commit comments

Comments
 (0)