Skip to content

Commit bd64036

Browse files
committed
Add plugins::package pluginVerify method
1 parent ae8750b commit bd64036

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/main.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ async fn main() -> Result<(), Error> {
184184
} else if cmd.starts_with("plugin") {
185185
match cmd {
186186
"pluginSign" => {
187-
let mut args = args.values_of("path").unwrap();
187+
let mut args = args.values_of("name").unwrap();
188188
let name = args.next().unwrap();
189189
let manifest = PluginManifest::load(name).await;
190190
let manifest_path = PathBuf::from(format!("plugins/{}/manifest.json", name));
@@ -199,20 +199,8 @@ async fn main() -> Result<(), Error> {
199199
"pluginVerify" => {
200200
let mut args = args.values_of("name").unwrap();
201201
let name = args.next().unwrap();
202-
let manifest_path = PathBuf::from(format!("plugins/{}/manifest.json", name));
203-
let plugin_file = PathBuf::from(format!("plugins/{name}/{name}.wasm", name = name));
204-
let manifest_str = tokio::fs::read_to_string(&manifest_path).await.unwrap();
205-
let manifest: PluginManifest = serde_json::from_str(&manifest_str).unwrap();
206-
let manifest_verified = manifest.verify().unwrap_or(false);
207-
let bytes = tokio::fs::read(plugin_file).await.unwrap();
208-
let sign = manifest.plugin_signature;
209-
let mut verified = manifest_verified;
210-
for key in manifest.signs.keys() {
211-
if !manifest_verified {
212-
continue;
213-
}
214-
verified = verified || zeronet_cryptography::verify(&*bytes, key, &sign).is_ok();
215-
}
202+
let manifest = PluginManifest::load(name).await.unwrap();
203+
let verified = manifest.verify_plugin().await.unwrap_or(false) ;
216204
println!("Plugin Verified : {}", verified);
217205
}
218206
_ => {

src/plugins/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn load_plugins() -> Vec<Plugin> {
7474
if has_manifest && has_plugin {
7575
let res = block_on(PluginManifest::load(name));
7676
if let Ok(manifest) = res {
77-
let verified = manifest.verify().unwrap();
77+
let verified = block_on(manifest.verify_plugin()).unwrap();
7878
if verified {
7979
return Some(plugin);
8080
}

src/plugins/package.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ impl PluginManifest {
3737
Ok(manifest)
3838
}
3939

40+
pub async fn verify_plugin(&self) -> Result<bool, Error> {
41+
self.verify()?;
42+
let plugin_file = PathBuf::from(format!(
43+
"plugins/{name}/{name}.wasm",
44+
name = self.plugin.name
45+
));
46+
let bytes = tokio::fs::read(plugin_file).await.unwrap();
47+
let signature = &self.plugin_signature;
48+
let mut verified = false;
49+
for key in self.signs.keys() {
50+
if verified {
51+
continue;
52+
}
53+
verified = zeronet_cryptography::verify(bytes.clone(), &key, &signature).is_ok();
54+
}
55+
Ok(verified)
56+
}
57+
4058
pub fn sign(&mut self, private_key: &str) -> Result<Self, Error> {
4159
let mut data = sort_json(serde_json::to_value(&self)?)?;
4260
let data = data.as_object_mut().unwrap();

0 commit comments

Comments
 (0)