From f034d4cffa420a646239a2e61ba85fb9153fd7b7 Mon Sep 17 00:00:00 2001 From: 0xZensh Date: Sat, 17 Aug 2024 13:19:30 +0800 Subject: [PATCH] chore: replace ByteN with ByteArray --- src/ic_oss/src/bucket.rs | 8 ++++---- src/ic_oss/src/cluster.rs | 8 ++++---- src/ic_oss_bucket/src/api_query.rs | 6 +++--- src/ic_oss_bucket/src/lib.rs | 4 ++-- src/ic_oss_bucket/src/store.rs | 26 +++++++++++++------------- src/ic_oss_can/src/types.rs | 5 +++-- src/ic_oss_cli/src/file.rs | 5 +++-- src/ic_oss_cli/src/main.rs | 25 ++++++++++++------------- src/ic_oss_cluster/src/api_admin.rs | 15 +++++++-------- src/ic_oss_cluster/src/api_query.rs | 5 +++-- src/ic_oss_cluster/src/lib.rs | 3 +-- src/ic_oss_cluster/src/store.rs | 23 +++++++++++------------ src/ic_oss_cose/src/lib.rs | 8 ++++---- src/ic_oss_types/src/bucket.rs | 8 ++++---- src/ic_oss_types/src/cluster.rs | 12 +++++------- src/ic_oss_types/src/file.rs | 17 ++++++++++------- 16 files changed, 89 insertions(+), 89 deletions(-) diff --git a/src/ic_oss/src/bucket.rs b/src/ic_oss/src/bucket.rs index ed85560..41096d8 100644 --- a/src/ic_oss/src/bucket.rs +++ b/src/ic_oss/src/bucket.rs @@ -1,9 +1,9 @@ use bytes::{Bytes, BytesMut}; use candid::{CandidType, Principal}; use ic_agent::Agent; -use ic_oss_types::{bucket::*, crc32, file::*, folder::*, format_error, ByteN}; +use ic_oss_types::{bucket::*, crc32, file::*, folder::*, format_error}; use serde::{Deserialize, Serialize}; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use sha3::{Digest, Sha3_256}; use std::{collections::BTreeSet, sync::Arc}; use tokio::io::AsyncRead; @@ -94,7 +94,7 @@ impl Client { .await? } - pub async fn get_file_info_by_hash(&self, hash: ByteN<32>) -> Result { + pub async fn get_file_info_by_hash(&self, hash: ByteArray<32>) -> Result { query_call( &self.agent, &self.bucket, @@ -350,7 +350,7 @@ impl Client { stream: T, id: u32, size: Option, - hash: Option>, + hash: Option>, exclude_chunks: &BTreeSet, on_progress: F, ) -> UploadFileChunksResult diff --git a/src/ic_oss/src/cluster.rs b/src/ic_oss/src/cluster.rs index 4256d7a..788bcfa 100644 --- a/src/ic_oss/src/cluster.rs +++ b/src/ic_oss/src/cluster.rs @@ -1,7 +1,7 @@ use candid::{Nat, Principal}; use ic_agent::Agent; -use ic_oss_types::{bucket::Token, cluster::*, ByteN}; -use serde_bytes::ByteBuf; +use ic_oss_types::{bucket::Token, cluster::*}; +use serde_bytes::{ByteArray, ByteBuf}; use std::{ collections::{BTreeMap, BTreeSet}, sync::Arc, @@ -54,7 +54,7 @@ impl Client { query_call(&self.agent, &self.cluster, "get_cluster_info", ()).await? } - pub async fn get_bucket_wasm(&self, hash: ByteN<32>) -> Result { + pub async fn get_bucket_wasm(&self, hash: ByteArray<32>) -> Result { query_call(&self.agent, &self.cluster, "get_bucket_wasm", (hash,)).await? } @@ -92,7 +92,7 @@ impl Client { pub async fn admin_add_wasm( &self, args: AddWasmInput, - force_prev_hash: Option>, + force_prev_hash: Option>, ) -> Result<(), String> { update_call( &self.agent, diff --git a/src/ic_oss_bucket/src/api_query.rs b/src/ic_oss_bucket/src/api_query.rs index b60a784..3d7afd0 100644 --- a/src/ic_oss_bucket/src/api_query.rs +++ b/src/ic_oss_bucket/src/api_query.rs @@ -5,9 +5,9 @@ use ic_oss_types::{ bucket::BucketInfo, file::{FileChunk, FileInfo}, folder::{FolderInfo, FolderName}, - format_error, ByteN, + format_error, }; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use crate::{permission, store, SECONDS}; @@ -99,7 +99,7 @@ fn get_file_info(id: u32, access_token: Option) -> Result, + hash: ByteArray<32>, access_token: Option, ) -> Result { let id = store::fs::get_file_id(&hash).ok_or("file not found")?; diff --git a/src/ic_oss_bucket/src/lib.rs b/src/ic_oss_bucket/src/lib.rs index 64dd9e8..6b5f848 100644 --- a/src/ic_oss_bucket/src/lib.rs +++ b/src/ic_oss_bucket/src/lib.rs @@ -1,6 +1,6 @@ use candid::Principal; use ic_cdk::api::management_canister::main::CanisterStatusResponse; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use std::collections::BTreeSet; mod api_admin; @@ -12,7 +12,7 @@ mod permission; mod store; use api_init::CanisterArgs; -use ic_oss_types::{bucket::*, file::*, folder::*, ByteN}; +use ic_oss_types::{bucket::*, file::*, folder::*}; const MILLISECONDS: u64 = 1_000_000; const SECONDS: u64 = 1_000_000_000; diff --git a/src/ic_oss_bucket/src/store.rs b/src/ic_oss_bucket/src/store.rs index ec87242..fc93a6d 100644 --- a/src/ic_oss_bucket/src/store.rs +++ b/src/ic_oss_bucket/src/store.rs @@ -11,7 +11,7 @@ use ic_oss_types::{ }, folder::{FolderInfo, FolderName, UpdateFolderInput}, permission::Policies, - ByteN, MapValue, + MapValue, }; use ic_stable_structures::{ memory_manager::{MemoryId, MemoryManager, VirtualMemory}, @@ -51,7 +51,7 @@ pub struct Bucket { // used to verify the request token signed with SECP256K1 pub trusted_ecdsa_pub_keys: Vec, // used to verify the request token signed with ED25519 - pub trusted_eddsa_pub_keys: Vec>, + pub trusted_eddsa_pub_keys: Vec>, } impl Default for Bucket { @@ -232,8 +232,8 @@ pub struct FileMetadata { pub created_at: u64, // unix timestamp in milliseconds pub updated_at: u64, // unix timestamp in milliseconds pub chunks: u32, - pub status: i8, // -1: archived; 0: readable and writable; 1: readonly - pub hash: Option>, // recommend sha3 256 + pub status: i8, // -1: archived; 0: readable and writable; 1: readonly + pub hash: Option>, // recommend sha3 256 pub dek: Option, // // Data Encryption Key that encrypted by BYOK or vetKey in COSE_Encrypt0 pub custom: Option, // custom metadata pub ex: Option, // External Resource, ER indicates that the file is an external resource. @@ -947,7 +947,7 @@ pub mod fs { Err(format!("file hash conflict, {}", prev))?; } - m.insert(hash.0, id); + m.insert(hash, id); Ok::<(), String>(()) })?; } @@ -1094,13 +1094,13 @@ pub mod fs { HASHS.with(|r| { let mut hm = r.borrow_mut(); if let Some(ref hash) = file.hash { - if let Some(prev) = hm.get(&hash.0) { + if let Some(prev) = hm.get(hash) { Err(format!("file hash conflict, {}", prev))?; } - hm.insert(hash.0, change.id); + hm.insert(*hash, change.id); } if let Some(prev_hash) = prev_hash { - hm.remove(&prev_hash.0); + hm.remove(&prev_hash); } Ok::<(), String>(()) })?; @@ -1283,7 +1283,7 @@ pub mod fs { m.remove(&id); if let Some(hash) = file.hash { - HASHS.with(|r| r.borrow_mut().remove(&hash.0)); + HASHS.with(|r| r.borrow_mut().remove(&hash)); } FS_CHUNKS_STORE.with(|r| { let mut fs_data = r.borrow_mut(); @@ -1321,7 +1321,7 @@ pub mod fs { removed.push(id); folder.files.remove(&id); if let Some(hash) = file.hash { - HASHS.with(|r| r.borrow_mut().remove(&hash.0)); + HASHS.with(|r| r.borrow_mut().remove(&hash)); } for i in 0..file.chunks { @@ -1379,7 +1379,7 @@ mod test { let f1 = fs::add_file(FileMetadata { name: "f1.bin".to_string(), - hash: Some(ByteN::from([1u8; 32])), + hash: Some(ByteArray::from([1u8; 32])), ..Default::default() }) .unwrap(); @@ -1404,14 +1404,14 @@ mod test { assert!(fs::add_file(FileMetadata { name: "f2.bin".to_string(), - hash: Some(ByteN::from([1u8; 32])), + hash: Some(ByteArray::from([1u8; 32])), ..Default::default() }) .is_err()); let f2 = fs::add_file(FileMetadata { name: "f2.bin".to_string(), - hash: Some(ByteN::from([2u8; 32])), + hash: Some(ByteArray::from([2u8; 32])), ..Default::default() }) .unwrap(); diff --git a/src/ic_oss_can/src/types.rs b/src/ic_oss_can/src/types.rs index 412dcf0..5b164b6 100644 --- a/src/ic_oss_can/src/types.rs +++ b/src/ic_oss_can/src/types.rs @@ -1,8 +1,9 @@ use candid::Principal; use ciborium::{from_reader, into_writer}; -use ic_oss_types::{file::*, ByteN}; +use ic_oss_types::file::*; use ic_stable_structures::{storable::Bound, Storable}; use serde::{Deserialize, Serialize}; +use serde_bytes::ByteArray; use std::{ borrow::Cow, collections::{BTreeMap, BTreeSet}, @@ -94,7 +95,7 @@ pub struct FileMetadata { pub created_at: u64, // unix timestamp in milliseconds pub updated_at: u64, // unix timestamp in milliseconds pub chunks: u32, - pub hash: Option>, // recommend sha3 256 + pub hash: Option>, // recommend sha3 256 } impl Storable for FileMetadata { diff --git a/src/ic_oss_cli/src/file.rs b/src/ic_oss_cli/src/file.rs index 81e75d5..0acc793 100644 --- a/src/ic_oss_cli/src/file.rs +++ b/src/ic_oss_cli/src/file.rs @@ -1,5 +1,6 @@ use chrono::prelude::*; -use ic_oss_types::{file::*, format_error, ByteN}; +use ic_oss_types::{file::*, format_error}; +use serde_bytes::ByteArray; use sha3::{Digest, Sha3_256}; use tokio::io::AsyncReadExt; use tokio::{time, time::Duration}; @@ -28,7 +29,7 @@ pub async fn upload_file( mime_db::lookup(file).unwrap_or("application/octet-stream") }; - let hash: Option> = if enable_hash_index { + let hash: Option> = if enable_hash_index { let fs = tokio::fs::File::open(&file_path) .await .map_err(format_error)?; diff --git a/src/ic_oss_cli/src/main.rs b/src/ic_oss_cli/src/main.rs index d29e81b..2da58dc 100644 --- a/src/ic_oss_cli/src/main.rs +++ b/src/ic_oss_cli/src/main.rs @@ -6,10 +6,10 @@ use ic_oss_types::{ cluster::AddWasmInput, file::{MoveInput, CHUNK_SIZE}, folder::CreateFolderInput, - format_error, ByteN, + format_error, }; use ring::{rand, signature::Ed25519KeyPair}; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use sha3::{Digest, Sha3_256}; use std::{ io::SeekFrom, @@ -306,16 +306,15 @@ async fn main() -> Result<(), String> { let cli = cli.cluster(identity, ic, cluster).await?; let wasm = std::fs::read(path).map_err(format_error)?; let prev_hash = prev_hash.as_ref().map(|s| parse_file_hash(s)).transpose()?; - cli - .admin_add_wasm( - AddWasmInput { - wasm: ByteBuf::from(wasm), - description: description.to_owned(), - }, - prev_hash, - ) - .await - .map_err(format_error)?; + cli.admin_add_wasm( + AddWasmInput { + wasm: ByteBuf::from(wasm), + description: description.to_owned(), + }, + prev_hash, + ) + .await + .map_err(format_error)?; return Ok(()); } @@ -584,7 +583,7 @@ where Ok(()) } -fn parse_file_hash(s: &str) -> Result, String> { +fn parse_file_hash(s: &str) -> Result, String> { let s = s.replace("\\", ""); let data = hex::decode(s.strip_prefix("0x").unwrap_or(&s)).map_err(format_error)?; let hash: [u8; 32] = data.try_into().map_err(format_error)?; diff --git a/src/ic_oss_cluster/src/api_admin.rs b/src/ic_oss_cluster/src/api_admin.rs index e9d6a03..d877b57 100644 --- a/src/ic_oss_cluster/src/api_admin.rs +++ b/src/ic_oss_cluster/src/api_admin.rs @@ -9,9 +9,8 @@ use ic_oss_types::{ cluster::{AddWasmInput, DeployWasmInput}, format_error, permission::Policies, - ByteN, }; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use std::collections::BTreeSet; use std::time::Duration; @@ -82,7 +81,7 @@ async fn admin_detach_policies(args: Token) -> Result<(), String> { #[ic_cdk::update(guard = "is_controller_or_manager")] async fn admin_add_wasm( args: AddWasmInput, - force_prev_hash: Option>, + force_prev_hash: Option>, ) -> Result<(), String> { store::wasm::add_wasm( ic_cdk::caller(), @@ -96,7 +95,7 @@ async fn admin_add_wasm( #[ic_cdk::update] async fn validate_admin_add_wasm( args: AddWasmInput, - force_prev_hash: Option>, + force_prev_hash: Option>, ) -> Result<(), String> { store::wasm::add_wasm( ic_cdk::caller(), @@ -110,7 +109,7 @@ async fn validate_admin_add_wasm( #[ic_cdk::update(guard = "is_controller")] async fn admin_deploy_bucket( args: DeployWasmInput, - ignore_prev_hash: Option>, + ignore_prev_hash: Option>, ) -> Result<(), String> { let (info,) = canister_info(CanisterInfoRequest { canister_id: args.canister, @@ -138,7 +137,7 @@ async fn admin_deploy_bucket( } else { Default::default() }; - let prev_hash = ByteN::from(prev_hash); + let prev_hash = ByteArray::from(prev_hash); let (hash, wasm) = if let Some(ignore_prev_hash) = ignore_prev_hash { if ignore_prev_hash != prev_hash { Err(format!( @@ -187,7 +186,7 @@ async fn admin_deploy_bucket( #[ic_cdk::update] async fn validate_admin_deploy_bucket( args: DeployWasmInput, - ignore_prev_hash: Option>, + ignore_prev_hash: Option>, ) -> Result<(), String> { let (info,) = canister_info(CanisterInfoRequest { canister_id: args.canister, @@ -209,7 +208,7 @@ async fn validate_admin_deploy_bucket( } else { Default::default() }; - let prev_hash = ByteN::from(prev_hash); + let prev_hash = ByteArray::from(prev_hash); if let Some(ignore_prev_hash) = ignore_prev_hash { if ignore_prev_hash != prev_hash { Err(format!( diff --git a/src/ic_oss_cluster/src/api_query.rs b/src/ic_oss_cluster/src/api_query.rs index dd8be5c..a0b3859 100644 --- a/src/ic_oss_cluster/src/api_query.rs +++ b/src/ic_oss_cluster/src/api_query.rs @@ -1,8 +1,9 @@ use candid::{Nat, Principal}; use ic_oss_types::{ cluster::{BucketDeploymentInfo, ClusterInfo, WasmInfo}, - nat_to_u64, ByteN, + nat_to_u64, }; +use serde_bytes::ByteArray; use std::collections::BTreeMap; use crate::{is_controller_or_manager, store}; @@ -13,7 +14,7 @@ fn get_cluster_info() -> Result { } #[ic_cdk::query] -fn get_bucket_wasm(hash: ByteN<32>) -> Result { +fn get_bucket_wasm(hash: ByteArray<32>) -> Result { store::wasm::get_wasm(&hash) .map(|w| WasmInfo { created_at: w.created_at, diff --git a/src/ic_oss_cluster/src/lib.rs b/src/ic_oss_cluster/src/lib.rs index 765e308..50bb017 100644 --- a/src/ic_oss_cluster/src/lib.rs +++ b/src/ic_oss_cluster/src/lib.rs @@ -2,9 +2,8 @@ use candid::{Nat, Principal}; use ic_oss_types::{ bucket::Token, cluster::{AddWasmInput, BucketDeploymentInfo, ClusterInfo, DeployWasmInput, WasmInfo}, - ByteN, }; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use std::collections::{BTreeMap, BTreeSet}; mod api_admin; diff --git a/src/ic_oss_cluster/src/store.rs b/src/ic_oss_cluster/src/store.rs index aa2dc9c..382633b 100644 --- a/src/ic_oss_cluster/src/store.rs +++ b/src/ic_oss_cluster/src/store.rs @@ -4,7 +4,6 @@ use ic_oss_cose::{sha256, CLUSTER_TOKEN_AAD}; use ic_oss_types::{ cluster::{AddWasmInput, BucketDeploymentInfo, ClusterInfo}, permission::Policies, - ByteN, }; use ic_stable_structures::{ memory_manager::{MemoryId, MemoryManager, VirtualMemory}, @@ -12,7 +11,7 @@ use ic_stable_structures::{ DefaultMemoryImpl, StableBTreeMap, StableCell, StableLog, Storable, }; use serde::{Deserialize, Serialize}; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use std::{ borrow::Cow, cell::RefCell, @@ -32,11 +31,11 @@ pub struct State { pub token_expiration: u64, // in seconds pub managers: BTreeSet, #[serde(default)] - pub bucket_latest_version: ByteN<32>, + pub bucket_latest_version: ByteArray<32>, #[serde(default)] - pub bucket_upgrade_path: HashMap, ByteN<32>>, + pub bucket_upgrade_path: HashMap, ByteArray<32>>, #[serde(default)] - pub bucket_deployed_list: BTreeMap)>, + pub bucket_deployed_list: BTreeMap)>, #[serde(default)] pub bucket_upgrade_process: Option, #[serde(default)] @@ -123,8 +122,8 @@ impl Storable for Wasm { pub struct DeployLog { pub deploy_at: u64, // in milliseconds pub canister: Principal, - pub prev_hash: ByteN<32>, - pub wasm_hash: ByteN<32>, + pub prev_hash: ByteArray<32>, + pub wasm_hash: ByteArray<32>, pub args: ByteBuf, pub error: Option, } @@ -293,13 +292,13 @@ pub mod wasm { caller: Principal, now_ms: u64, args: AddWasmInput, - force_prev_hash: Option>, + force_prev_hash: Option>, dry_run: bool, ) -> Result<(), String> { WASM_STORE.with(|r| { if dry_run { let m = r.borrow(); - let hash: ByteN<32> = sha256(&args.wasm).into(); + let hash: ByteArray<32> = sha256(&args.wasm).into(); if m.contains_key(&hash) { return Err("wasm already exists".to_string()); } @@ -316,7 +315,7 @@ pub mod wasm { } let mut m = r.borrow_mut(); - let hash: ByteN<32> = sha256(&args.wasm).into(); + let hash: ByteArray<32> = sha256(&args.wasm).into(); if m.contains_key(&hash) { return Err("wasm already exists".to_string()); } @@ -347,11 +346,11 @@ pub mod wasm { }) } - pub fn get_wasm(hash: &ByteN<32>) -> Option { + pub fn get_wasm(hash: &ByteArray<32>) -> Option { WASM_STORE.with(|r| r.borrow().get(hash)) } - pub fn next_version(prev_hash: ByteN<32>) -> Result<(ByteN<32>, Wasm), String> { + pub fn next_version(prev_hash: ByteArray<32>) -> Result<(ByteArray<32>, Wasm), String> { state::with(|s| { let h = s .bucket_upgrade_path diff --git a/src/ic_oss_cose/src/lib.rs b/src/ic_oss_cose/src/lib.rs index 9f7dbe9..7949c8d 100644 --- a/src/ic_oss_cose/src/lib.rs +++ b/src/ic_oss_cose/src/lib.rs @@ -4,11 +4,11 @@ use coset::{ iana, Algorithm, CborSerializable, CoseSign1, CoseSign1Builder, HeaderBuilder, }; use ed25519_dalek::{Signature, VerifyingKey}; -use ic_oss_types::{bucket, ByteN}; +use ic_oss_types::bucket; use k256::{ecdsa, ecdsa::signature::hazmat::PrehashVerifier}; use num_traits::ToPrimitive; use serde::{Deserialize, Serialize}; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use sha2::Digest; pub use coset; @@ -44,7 +44,7 @@ impl Token { pub fn from_sign1( sign1_token: &[u8], secp256k1_pub_keys: &[ByteBuf], - ed25519_pub_keys: &[ByteN<32>], + ed25519_pub_keys: &[ByteArray<32>], aad: &[u8], now_sec: i64, ) -> Result { @@ -103,7 +103,7 @@ impl Token { } fn ed25519_verify( - pub_keys: &[ByteN<32>], + pub_keys: &[ByteArray<32>], tbs_data: &[u8], signature: &[u8], ) -> Result<(), String> { diff --git a/src/ic_oss_types/src/bucket.rs b/src/ic_oss_types/src/bucket.rs index 502e9ab..b89ed95 100644 --- a/src/ic_oss_types/src/bucket.rs +++ b/src/ic_oss_types/src/bucket.rs @@ -1,9 +1,9 @@ use candid::{CandidType, Principal}; use serde::{Deserialize, Serialize}; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use std::collections::BTreeSet; -use crate::{file::MAX_FILE_SIZE, ByteN}; +use crate::file::MAX_FILE_SIZE; #[derive(CandidType, Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] pub struct BucketInfo { @@ -26,7 +26,7 @@ pub struct BucketInfo { // used to verify the request token signed with SECP256K1 pub trusted_ecdsa_pub_keys: Vec, // used to verify the request token signed with ED25519 - pub trusted_eddsa_pub_keys: Vec>, + pub trusted_eddsa_pub_keys: Vec>, } #[derive(CandidType, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] @@ -47,7 +47,7 @@ pub struct UpdateBucketInput { pub status: Option, // -1: archived; 0: readable and writable; 1: readonly pub visibility: Option, // 0: private; 1: public pub trusted_ecdsa_pub_keys: Option>, - pub trusted_eddsa_pub_keys: Option>>, + pub trusted_eddsa_pub_keys: Option>>, } impl UpdateBucketInput { diff --git a/src/ic_oss_types/src/cluster.rs b/src/ic_oss_types/src/cluster.rs index afd71a7..f22c8a9 100644 --- a/src/ic_oss_types/src/cluster.rs +++ b/src/ic_oss_types/src/cluster.rs @@ -1,10 +1,8 @@ use candid::{CandidType, Principal}; use serde::{Deserialize, Serialize}; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use std::collections::BTreeSet; -use crate::ByteN; - #[derive(CandidType, Clone, Debug, Default, Deserialize, Serialize)] pub struct ClusterInfo { pub name: String, @@ -13,7 +11,7 @@ pub struct ClusterInfo { pub token_expiration: u64, // in seconds pub managers: BTreeSet, pub subject_authz_total: u64, - pub bucket_latest_version: ByteN<32>, + pub bucket_latest_version: ByteArray<32>, pub bucket_wasm_total: u64, pub bucket_deployed_total: u64, pub bucket_deployment_logs: u64, @@ -25,7 +23,7 @@ pub struct WasmInfo { pub created_by: Principal, pub description: String, pub wasm: ByteBuf, - pub hash: ByteN<32>, // sha256 hash of the wasm data + pub hash: ByteArray<32>, // sha256 hash of the wasm data } #[derive(CandidType, Clone, Debug, Deserialize, Serialize)] @@ -44,8 +42,8 @@ pub struct DeployWasmInput { pub struct BucketDeploymentInfo { pub deploy_at: u64, // in milliseconds pub canister: Principal, - pub prev_hash: ByteN<32>, - pub wasm_hash: ByteN<32>, + pub prev_hash: ByteArray<32>, + pub wasm_hash: ByteArray<32>, pub args: Option, pub error: Option, } diff --git a/src/ic_oss_types/src/file.rs b/src/ic_oss_types/src/file.rs index 49597fc..9e77e75 100644 --- a/src/ic_oss_types/src/file.rs +++ b/src/ic_oss_types/src/file.rs @@ -1,11 +1,11 @@ use base64::{engine::general_purpose, Engine}; use candid::CandidType; use serde::{Deserialize, Serialize}; -use serde_bytes::ByteBuf; +use serde_bytes::{ByteArray, ByteBuf}; use std::path::Path; use url::Url; -use crate::{ByteN, MapValue}; +use crate::{format_error, MapValue}; pub const CHUNK_SIZE: u32 = 256 * 1024; pub const MAX_FILE_SIZE: u64 = 384 * 1024 * 1024 * 1024; // 384GB @@ -23,7 +23,7 @@ pub struct FileInfo { pub updated_at: u64, // unix timestamp in milliseconds pub chunks: u32, pub status: i8, // -1: archived; 0: readable and writable; 1: readonly - pub hash: Option>, + pub hash: Option>, pub dek: Option, // // Data Encryption Key that encrypted by BYOK or vetKey in COSE_Encrypt0 pub custom: Option, // custom metadata pub ex: Option, // External Resource info @@ -37,7 +37,7 @@ pub struct CreateFileInput { pub size: Option, // if provided, can be used to detect the file is fully filled pub content: Option, // should <= 1024 * 1024 * 2 - 1024 pub status: Option, // when set to 1, the file must be fully filled, and hash must be provided - pub hash: Option>, // recommend sha3 256 + pub hash: Option>, // recommend sha3 256 pub dek: Option, pub custom: Option, pub crc32: Option, @@ -106,7 +106,7 @@ pub struct UpdateFileInput { pub name: Option, pub content_type: Option, pub status: Option, // when set to 1, the file must be fully filled, and hash must be provided - pub hash: Option>, + pub hash: Option>, pub custom: Option, } @@ -162,7 +162,7 @@ pub struct MoveInput { pub struct UrlFileParam { pub file: u32, - pub hash: Option>, + pub hash: Option>, pub token: Option, pub name: Option, pub inline: bool, @@ -193,7 +193,10 @@ impl UrlFileParam { inline: false, }, Some("h") => { - let hash = ByteN::from_hex(path_segments.next().unwrap_or_default())?; + let val = path_segments.next().unwrap_or_default(); + let data = hex::decode(val).map_err(format_error)?; + let hash: [u8; 32] = data.try_into().map_err(format_error)?; + let hash = ByteArray::from(hash); Self { file: 0, hash: Some(hash),