Skip to content

Commit 4adb2d5

Browse files
authored
@peter/encryption (#49)
* add encryption/decryption * add encryption/decryption * format code * use sdk git repo * add test cases
1 parent a08baaa commit 4adb2d5

File tree

11 files changed

+1132
-274
lines changed

11 files changed

+1132
-274
lines changed

Cargo.lock

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

node/src/config/convert.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ impl ZgsKVConfig {
3737
error!("{}", format!("stream ids is empty"))
3838
}
3939
let stream_set = HashSet::from_iter(stream_ids.iter().cloned());
40+
let encryption_key = if self.encryption_key.is_empty() {
41+
None
42+
} else {
43+
Some(parse_encryption_key(&self.encryption_key)?)
44+
};
4045
Ok(StreamConfig {
4146
stream_ids,
4247
stream_set,
48+
encryption_key,
4349
})
4450
}
4551

@@ -109,6 +115,22 @@ impl ZgsKVConfig {
109115
}
110116
}
111117

118+
fn parse_encryption_key(hex_str: &str) -> Result<[u8; 32], String> {
119+
let hex_str = hex_str.strip_prefix("0x").unwrap_or(hex_str);
120+
if hex_str.len() != 64 {
121+
return Err(format!(
122+
"encryption_key must be 64 hex chars (32 bytes), got {} chars",
123+
hex_str.len()
124+
));
125+
}
126+
let mut key = [0u8; 32];
127+
for i in 0..32 {
128+
key[i] = u8::from_str_radix(&hex_str[i * 2..i * 2 + 2], 16)
129+
.map_err(|e| format!("Invalid hex in encryption_key at position {}: {}", i * 2, e))?;
130+
}
131+
Ok(key)
132+
}
133+
112134
// zgs_node_urls can be used as a static node list; otherwise indexer_url is used.
113135
pub fn to_zgs_nodes(zgs_node_urls: String) -> Result<Vec<String>, String> {
114136
if zgs_node_urls.is_empty() {

node/src/config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ build_config! {
4040
(max_response_body_in_bytes, (u32), 1024 * 1024 * 30) // 30MB
4141
(zgs_rpc_timeout, (u64), 5) // 5 seconds
4242

43+
// encryption
44+
(encryption_key, (String), "".to_string())
45+
4346
// db
4447
(db_dir, (String), "db".to_string())
4548
(merkle_node_cache_capacity, (usize), 32 * 1024 * 1024)

node/stream/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ append_merkle = { workspace = true }
1111
async-trait = "0.1.56"
1212
ethereum-types = "0.14"
1313
futures = "0.3.21"
14-
jsonrpsee = { version = "0.14.0", features = ["full"] }
1514
shared_types = { workspace = true }
1615
kv_types = { path = "../kv_types" }
1716
task_executor = { workspace = true }
1817
tokio = "1.19.2"
1918
ethers = { version = "^2", features = ["ws"] }
20-
serde_json = "1.0.127"
2119
storage_with_stream = { path = "../storage_with_stream" }
2220
rpc = {path = "../rpc"}
23-
zgs_rpc = { git = "https://github.com/0gfoundation/0g-storage-node.git", branch = "main", package = "rpc" }
24-
zgs_storage = { git = "https://github.com/0gfoundation/0g-storage-node.git", branch = "main", package = "storage" }
21+
zg-storage-client = { git = "https://github.com/0gfoundation/0g-storage-sdk-rust.git", rev = "6101dc7714c0cfc71c9e902e351068e189fa9e15" }
2522
contract-interface = { workspace = true }
2623
rusqlite = { version = "0.28.0", features = ["bundled"] }
2724
tracing = "0.1.35"
2825
eth2_ssz = "0.4.0"
2926
eth2_ssz_derive = "0.3.0"
3027
thiserror = "1.0.37"
28+
29+
[dev-dependencies]
30+
tokio = { version = "1.19.2", features = ["rt", "macros"] }

node/stream/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ use ethereum_types::H256;
66
pub struct Config {
77
pub stream_ids: Vec<H256>,
88
pub stream_set: HashSet<H256>,
9+
pub encryption_key: Option<[u8; 32]>,
910
}

0 commit comments

Comments
 (0)