Skip to content

Commit

Permalink
Merge pull request #865 from dcSpark/agallardol/fix-identity-created-…
Browse files Browse the repository at this point in the history
…quest

fix: identity created quest
  • Loading branch information
nicarq authored Feb 12, 2025
2 parents de70ba6 + 339fb35 commit e62ebe7
Show file tree
Hide file tree
Showing 19 changed files with 273 additions and 236 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.9.9"
version = "0.9.10"
edition = "2021"
authors = ["Nico Arqueros <[email protected]>"]

Expand Down
112 changes: 54 additions & 58 deletions shinkai-bin/shinkai-node/src/managers/galxe_quests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use blake3::{self, Hasher};
use chrono::{DateTime, Utc};
use ed25519_dalek::Signer;
use ed25519_dalek::{Signer, VerifyingKey};
use reqwest;
use rusqlite::params;
use serde::{Deserialize, Serialize};
Expand All @@ -9,8 +9,8 @@ use shinkai_crypto_identities::ShinkaiRegistry;
use shinkai_message_primitives::schemas::shinkai_name::ShinkaiName;
use shinkai_message_primitives::shinkai_utils::signatures::unsafe_deterministic_signature_keypair;
use shinkai_sqlite::SqliteManager;
use std::collections::HashMap;
use std::sync::Arc;
use x25519_dalek::PublicKey as EncryptionPublicKey;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum QuestStatus {
Expand Down Expand Up @@ -58,6 +58,8 @@ pub struct QuestInfo {
pub async fn compute_quests(
db: Arc<SqliteManager>,
node_name: ShinkaiName,
encryption_public_key: EncryptionPublicKey,
identity_public_key: VerifyingKey,
) -> Result<Vec<(QuestType, QuestInfo)>, String> {
let mut quests = Vec::new();

Expand All @@ -77,7 +79,14 @@ pub async fn compute_quests(
QuestType::CreateIdentity,
QuestInfo {
name: "CreateIdentity".to_string(),
status: match compute_create_identity_quest(db.clone(), node_name.clone()).await {
status: match compute_create_identity_quest(
db.clone(),
node_name.clone(),
encryption_public_key.clone(),
identity_public_key.clone(),
)
.await
{
Ok(status) => status,
Err(e) => {
eprintln!("Error computing create identity quest: {}", e);
Expand Down Expand Up @@ -294,70 +303,57 @@ pub async fn compute_quests(
Ok(quests)
}

pub async fn compute_create_identity_quest(db: Arc<SqliteManager>, node_name: ShinkaiName) -> Result<bool, String> {
pub async fn compute_create_identity_quest(
db: Arc<SqliteManager>,
node_name: ShinkaiName,
encryption_public_key: EncryptionPublicKey,
identity_public_key: VerifyingKey,
) -> Result<bool, String> {
// First check if the node name is localhost
if node_name.to_string() == "@@localhost.sep-shinkai" {
println!("Identity is localhost, quest not completed");
return Ok(false);
}

let node_info = db.query_row(
"SELECT node_name, node_encryption_public_key, node_signature_public_key FROM local_node_keys LIMIT 1",
params![],
|row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, Vec<u8>>(1)?,
row.get::<_, Vec<u8>>(2)?,
))
},
);

let has_identity = match node_info {
Ok((name, local_enc_key, local_sig_key)) => {
// Get registry data
let registry = ShinkaiRegistry::new(
"https://sepolia.base.org",
"0x425fb20ba3874e887336aaa7f3fab32d08135ba9",
None,
)
.await
.map_err(|e| format!("Failed to create registry: {}", e))?;

let onchain_identity = match registry.get_identity_record(node_name.to_string()).await {
Ok(identity) => identity,
Err(e) => {
println!("Identity not found in registry: {}", e);
return Ok(false);
}
};

// Hash our local keys
let local_enc_hash = blake3::hash(&local_enc_key).to_hex().to_string();
let local_sig_hash = blake3::hash(&local_sig_key).to_hex().to_string();

// The registry keys are already hex strings of the hashes, no need to decode and hash again
let registry_enc_hash = onchain_identity.encryption_key;
let registry_sig_hash = onchain_identity.signature_key;

println!(
"Node Identity Found:\nName: {}\nLocal Encryption Key Hash: {}\nLocal Signature Key Hash: {}\nRegistry Encryption Key Hash: {}\nRegistry Signature Key Hash: {}",
name,
local_enc_hash,
local_sig_hash,
registry_enc_hash,
registry_sig_hash
);

// Compare the hex strings directly
let keys_match = local_enc_hash == registry_enc_hash && local_sig_hash == registry_sig_hash;
keys_match
// Get registry data
let registry = ShinkaiRegistry::new(
"https://sepolia.base.org",
"0x425fb20ba3874e887336aaa7f3fab32d08135ba9",
None,
)
.await
.map_err(|e| format!("Failed to create registry: {}", e))?;

let onchain_identity = match registry.get_identity_record(node_name.to_string(), Some(true)).await {
Ok(identity) => identity,
Err(e) => {
println!("Identity not found in registry: {}", e);
return Ok(false);
}
Err(_) => false,
};

println!("Has valid identity: {}", has_identity);
Ok(has_identity)
// Hash our local keys
let local_enc_hash = hex::encode(encryption_public_key.to_bytes());
let local_sig_hash = hex::encode(identity_public_key.to_bytes());

// The registry keys are already hex strings of the hashes, no need to decode and hash again
let registry_enc_hash = onchain_identity.encryption_key;
let registry_sig_hash = onchain_identity.signature_key;

println!(
"Node Identity Found:\nName: {}\nLocal Encryption Key Hash: {}\nLocal Signature Key Hash: {}\nRegistry Encryption Key Hash: {}\nRegistry Signature Key Hash: {}",
node_name,
local_enc_hash,
local_sig_hash,
registry_enc_hash,
registry_sig_hash
);

// Compare the hex strings directly
let keys_match = local_enc_hash == registry_enc_hash && local_sig_hash == registry_sig_hash;

println!("Has valid identity: {}", keys_match);
Ok(keys_match)
}

pub async fn compute_download_store_quest(db: Arc<SqliteManager>) -> Result<bool, String> {
Expand Down
22 changes: 17 additions & 5 deletions shinkai-bin/shinkai-node/src/managers/identity_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ pub trait IdentityManagerTrait {
fn find_by_identity_name(&self, full_profile_name: ShinkaiName) -> Option<&Identity>;
async fn search_identity(&self, full_identity_name: &str) -> Option<Identity>;
fn clone_box(&self) -> Box<dyn IdentityManagerTrait + Send>;
async fn external_profile_to_global_identity(&self, full_profile_name: &str) -> Result<StandardIdentity, String>;
async fn external_profile_to_global_identity(
&self,
full_profile_name: &str,
force_refresh: Option<bool>,
) -> Result<StandardIdentity, String>;
}

impl Clone for Box<dyn IdentityManagerTrait + Send> {
Expand Down Expand Up @@ -280,7 +284,11 @@ impl IdentityManagerTrait for IdentityManager {
match identity {
Identity::Standard(identity) => identity.full_identity_name == full_profile_name,
Identity::Device(device) => device.full_identity_name == full_profile_name,
Identity::LLMProvider(agent) => agent.full_identity_name == full_profile_name, // Assuming the 'name' field of Agent struct can be considered as the profile name
Identity::LLMProvider(agent) => agent.full_identity_name == full_profile_name, /* Assuming the 'name'
* field of Agent
* struct can be
* considered as the
* profile name */
}
})
}
Expand All @@ -296,7 +304,7 @@ impl IdentityManagerTrait for IdentityManager {
// If not, query the identity network manager
let external_im = self.external_identity_manager.lock().await;
match external_im
.external_identity_to_profile_data(full_identity_name.to_string())
.external_identity_to_profile_data(full_identity_name.to_string(), None)
.await
{
Ok(identity_network_manager) => match identity_network_manager.first_address().await {
Expand Down Expand Up @@ -332,7 +340,11 @@ impl IdentityManagerTrait for IdentityManager {
Box::new(self.clone())
}

async fn external_profile_to_global_identity(&self, full_profile_name: &str) -> Result<StandardIdentity, String> {
async fn external_profile_to_global_identity(
&self,
full_profile_name: &str,
force_refresh: Option<bool>,
) -> Result<StandardIdentity, String> {
shinkai_log(
ShinkaiLogOption::Identity,
ShinkaiLogLevel::Debug,
Expand Down Expand Up @@ -362,7 +374,7 @@ impl IdentityManagerTrait for IdentityManager {
let external_im = self.external_identity_manager.lock().await;

match external_im
.external_identity_to_profile_data(node_name.to_string())
.external_identity_to_profile_data(node_name.to_string(), force_refresh)
.await
{
Ok(identity_network_manager) => match identity_network_manager.first_address().await {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ impl IdentityNetworkManager {
pub async fn external_identity_to_profile_data(
&self,
global_identity: String,
force_refresh: Option<bool>,
) -> Result<OnchainIdentity, &'static str> {
let record = {
let identity = global_identity.trim_start_matches("@@");
let registry = self.registry.lock().await;
match registry.get_identity_record(identity.to_string()).await {
match registry.get_identity_record(identity.to_string(), force_refresh).await {
Ok(record) => record,
Err(_) => return Err("Unrecognized global identity"),
}
Expand All @@ -53,7 +54,10 @@ impl IdentityNetworkManager {
let proxy_identity = record.address_or_proxy_nodes.clone();
let proxy_record = {
let registry = self.registry.lock().await;
match registry.get_identity_record(proxy_identity.join(",")).await {
match registry
.get_identity_record(proxy_identity.join(","), force_refresh)
.await
{
Ok(record) => record,
Err(_) => return Err("Failed to fetch proxy node data"),
}
Expand Down
Loading

0 comments on commit e62ebe7

Please sign in to comment.