Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kinode/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rayon::prelude::*;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::{
collections::HashSet,
fs::{self, File},
Expand Down
12 changes: 6 additions & 6 deletions kinode/src/fakenet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::fakenet::helpers::RegisterHelpers::{
ipCall, multicallCall, ownerOfCall, registerCall, setAllIpCall, setKeyCall,
};
use crate::{keygen, KNS_ADDRESS};
use alloy::network::{eip2718::Encodable2718, EthereumWallet, TransactionBuilder};
use alloy::providers::{Provider, ProviderBuilder, RootProvider};
use alloy::pubsub::PubSubFrontend;
Expand All @@ -11,10 +15,6 @@ use std::str::FromStr;

pub mod helpers;

use crate::{keygen, KNS_ADDRESS};
pub use helpers::RegisterHelpers::*;
pub use helpers::*;

const FAKE_DOTDEV: &str = "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9";

/// Attempts to connect to a local anvil fakechain,
Expand Down Expand Up @@ -42,8 +42,8 @@ pub async fn register_local(

let provider: RootProvider<PubSubFrontend> = ProviderBuilder::default().on_ws(ws).await?;

let fqdn = dns_encode_fqdn(name);
let namehash = encode_namehash(name);
let fqdn = helpers::dns_encode_fqdn(name);
let namehash = helpers::encode_namehash(name);
// todo: find a better way?
let namehash_bint: B256 = namehash.into();
let namehash_uint: U256 = namehash_bint.into();
Expand Down
10 changes: 4 additions & 6 deletions kinode/src/kernel/process.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::KERNEL_PROCESS_ID;
use anyhow::Result;
use lib::types::core as t;
pub use lib::v0::ProcessV0;
pub use lib::Process;
Expand All @@ -9,8 +8,7 @@ use std::sync::Arc;
use tokio::fs;
use tokio::task::JoinHandle;
use wasi_common::sync::Dir;
use wasmtime::component::ResourceTable as Table;
use wasmtime::component::*;
use wasmtime::component::{Component, Linker, ResourceTable as Table};
use wasmtime::{Engine, Store};
use wasmtime_wasi::{
pipe::MemoryOutputPipe, DirPerms, FilePerms, WasiCtx, WasiCtxBuilder, WasiView,
Expand Down Expand Up @@ -90,7 +88,7 @@ async fn make_component(
wasm_bytes: &[u8],
home_directory_path: String,
process_state: ProcessState,
) -> Result<(Process, Store<ProcessWasi>, MemoryOutputPipe)> {
) -> anyhow::Result<(Process, Store<ProcessWasi>, MemoryOutputPipe)> {
let component = Component::new(&engine, wasm_bytes.to_vec())
.expect("make_process_loop: couldn't read file");

Expand Down Expand Up @@ -170,7 +168,7 @@ async fn make_component_v0(
wasm_bytes: &[u8],
home_directory_path: String,
process_state: ProcessState,
) -> Result<(ProcessV0, Store<ProcessWasiV0>, MemoryOutputPipe)> {
) -> anyhow::Result<(ProcessV0, Store<ProcessWasiV0>, MemoryOutputPipe)> {
let component = Component::new(&engine, wasm_bytes.to_vec())
.expect("make_process_loop: couldn't read file");

Expand Down Expand Up @@ -257,7 +255,7 @@ pub async fn make_process_loop(
caps_oracle: t::CapMessageSender,
engine: Engine,
home_directory_path: String,
) -> Result<()> {
) -> anyhow::Result<()> {
// before process can be instantiated, need to await 'run' message from kernel
let mut pre_boot_queue = Vec::<Result<t::KernelMessage, t::WrappedSendError>>::new();
while let Some(message) = recv_in_process.recv().await {
Expand Down
3 changes: 1 addition & 2 deletions kinode/src/kernel/standard_host.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::kernel::process;
use crate::KERNEL_PROCESS_ID;
use crate::VFS_PROCESS_ID;
use anyhow::Result;
use lib::core::{KERNEL_PROCESS_ID, VFS_PROCESS_ID};
use lib::types::core::{self as t, STATE_PROCESS_ID};
pub use lib::wit;
pub use lib::wit::Host as StandardHost;
Expand Down
3 changes: 1 addition & 2 deletions kinode/src/kernel/standard_host_v0.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::kernel::process;
use crate::KERNEL_PROCESS_ID;
use crate::VFS_PROCESS_ID;
use anyhow::Result;
use lib::core::{KERNEL_PROCESS_ID, VFS_PROCESS_ID};
use lib::types::core::{self as t, STATE_PROCESS_ID};
pub use lib::v0::wit;
pub use lib::v0::wit::Host as StandardHost;
Expand Down
26 changes: 14 additions & 12 deletions kinode/src/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ use aes_gcm::{
aead::{Aead, AeadCore, KeyInit, OsRng},
Aes256Gcm, Key,
};
use alloy_primitives::keccak256;
use anyhow::Result;
use generic_array::GenericArray;
use hmac::Hmac;
use jwt::SignWithKey;
use lib::types::core::Keyfile;
use ring::pbkdf2;
use ring::pkcs8::Document;
use ring::rand::SystemRandom;
use ring::signature::{self, KeyPair};
use ring::{digest as ring_digest, rand::SecureRandom};
use sha2::Sha256;
use std::num::NonZeroU32;

type DiskKey = [u8; CREDENTIAL_LEN];

pub const CREDENTIAL_LEN: usize = ring_digest::SHA256_OUTPUT_LEN;
pub const CREDENTIAL_LEN: usize = ring::digest::SHA256_OUTPUT_LEN;
pub const ITERATIONS: u32 = 1_000_000;
pub static PBKDF2_ALG: pbkdf2::Algorithm = pbkdf2::PBKDF2_HMAC_SHA256; // TODO maybe look into Argon2

Expand All @@ -30,8 +22,9 @@ pub fn encode_keyfile(
jwt: &[u8],
file_key: &[u8],
) -> Vec<u8> {
let mut disk_key: DiskKey = [0u8; CREDENTIAL_LEN];
use ring::rand::SecureRandom;

let mut disk_key: DiskKey = [0u8; CREDENTIAL_LEN];
let rng = SystemRandom::new();
let mut salt = [0u8; 32]; // generate a unique salt
rng.fill(&mut salt).unwrap();
Expand Down Expand Up @@ -67,6 +60,8 @@ pub fn encode_keyfile(
}

pub fn decode_keyfile(keyfile: &[u8], password: &str) -> Result<Keyfile, &'static str> {
use generic_array::GenericArray;

let (username, routers, salt, key_enc, jwt_enc, file_enc) =
bincode::deserialize::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>(keyfile)
.map_err(|_| "failed to deserialize keyfile")?;
Expand Down Expand Up @@ -117,8 +112,11 @@ pub fn generate_jwt(
username: &str,
subdomain: &Option<String>,
) -> Option<String> {
let jwt_secret: Hmac<Sha256> = Hmac::new_from_slice(jwt_secret_bytes).ok()?;
use hmac::Hmac;
use jwt::SignWithKey;
use sha2::Sha256;

let jwt_secret: Hmac<Sha256> = Hmac::new_from_slice(jwt_secret_bytes).ok()?;
let subdomain = match subdomain.clone().unwrap_or_default().as_str() {
"" => None,
subdomain => Some(subdomain.to_string()),
Expand All @@ -143,6 +141,8 @@ pub fn get_username_and_routers(keyfile: &[u8]) -> Result<(String, Vec<String>),
}

pub fn namehash(name: &str) -> Vec<u8> {
use alloy_primitives::keccak256;

let mut node = vec![0u8; 32];
if name.is_empty() {
return node;
Expand All @@ -158,6 +158,8 @@ pub fn namehash(name: &str) -> Vec<u8> {

/// randomly generated key to encrypt file chunks,
pub fn generate_file_key() -> Vec<u8> {
use ring::rand::SecureRandom;

let mut key = [0u8; 32];
let rng = SystemRandom::new();
rng.fill(&mut key).unwrap();
Expand All @@ -166,7 +168,7 @@ pub fn generate_file_key() -> Vec<u8> {

/// # Returns
/// a pair of (public key (encoded as a hex string), serialized key as a pkcs8 Document)
pub fn generate_networking_key() -> (String, Document) {
pub fn generate_networking_key() -> (String, ring::pkcs8::Document) {
let seed = SystemRandom::new();
let doc = signature::Ed25519KeyPair::generate_pkcs8(&seed).unwrap();
let keys = signature::Ed25519KeyPair::from_pkcs8(doc.as_ref()).unwrap();
Expand Down
Loading