You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use dsnp_graph_config::{ConnectionType,Environment,PrivacyType};use dsnp_graph_core::api::{
api::{GraphAPI,GraphState},
api_types::{Action,Connection},};fnmain(){// create graph state with chosen environmentletmut state = GraphState::new(Environment::Mainnet);// graph owner that we want to interact withlet my_dsnp_user_id = 1000;// get that desired graph schema idlet public_follow_graph_schema_id = Environment::Mainnet.get_config().get_schema_id_from_connection_type(ConnectionType::Follow(PrivacyType::Public)).unwrap();// add a new connection to the owner graphlet add_connection = Action::Connect{connection:Connection{dsnp_user_id:2000,schema_id: public_follow_graph_schema_id },dsnp_keys:None,owner_dsnp_user_id: my_dsnp_user_id,};// add new connection to graphlet apply_result = state.apply_actions(&vec![add_connection]);ifletErr(e) = apply_result {println!("{:?}", e);return}// get all connections including pending one that we just addedlet connections = state
.get_connections_for_user_graph(&my_dsnp_user_id,&public_follow_graph_schema_id,true).unwrap();println!("Connections {:?}", connections);let connections_user_ids:Vec<_> = connections.iter().map(|c| c.user_id).collect();assert!(connections_user_ids.contains(&2000));// export all updates to the graphmatch state.export_updates(){Ok(updates) => {println!("Updates {:?}", updates);// Update Blockchain using the updates},Err(e) => {println!("{:?}", e);},}}
Add a new Graph Key
use dsnp_graph_config::Environment;use dsnp_graph_core::api::{
api::{GraphAPI,GraphState},
api_types::{Action,Update},};fnmain(){// create graph state with chosen environmentletmut state = GraphState::new(Environment::Mainnet);// graph key ownerlet dsnp_key_owner = 1000;// new_keylet x25519_public_key = vec![15u8,234,44,175,171,220,131,117,43,227,111,165,52,150,64,218,44,130,138,221,10,41,13,241,60,210,216,23,62,178,73,111,];let new_key_action = Action::AddGraphKey{owner_dsnp_user_id: dsnp_key_owner,new_public_key: x25519_public_key,};// add new keylet key_result = state.apply_actions(&[new_key_action]);ifletErr(e) = key_result {println!("{:?}", e);return}// export newly add key to publish on chainmatch state.export_updates(){Ok(updates) => {println!("Updates {:?}", updates);let _add_key_updates:Vec<_> =
updates.iter().filter(|u| matches!(u,Update::AddKey{ .. })).collect();// publish added key},Err(e) => {println!("{:?}", e);},}}
Read and deserialize published graph keys
use dsnp_graph_core::api::{
api::{GraphAPI,GraphState},
api_types::{DsnpKeys,KeyData},};fnmain(){// graph key ownerlet dsnp_key_owner = 1000;// published keys blobs fetched from blockchainlet published_keys_blob = vec![64,15,234,44,175,171,220,131,117,43,227,111,165,52,150,64,218,44,130,138,221,10,41,13,241,60,210,216,23,62,178,73,111,];let dsnp_keys = DsnpKeys{keys:vec![KeyData{ content: published_keys_blob, index:0}],dsnp_user_id: dsnp_key_owner,keys_hash:2789,// should get this hash value from blockchain};// deserialize published keysmatchGraphState::deserialize_dsnp_keys(&dsnp_keys){Ok(keys) => {println!("Keys {:?}", keys);},Err(e) => {println!("{:?}", e);},}}
Update a Private Follow graph
use dsnp_graph_config::{ConnectionType,Environment,PrivacyType};use dsnp_graph_core::api::{
api::{GraphAPI,GraphState},
api_types::{Action,Connection,DsnpKeys,ImportBundle},};fnmain(){// create graph state with chosen environmentletmut state = GraphState::new(Environment::Mainnet);// graph owner that we want to interact withlet my_dsnp_user_id = 1000;// get that desired graph schema idlet private_follow_graph_schema_id = Environment::Mainnet.get_config().get_schema_id_from_connection_type(ConnectionType::Follow(PrivacyType::Private)).unwrap();// import existing published graph and keys fetched from blockchainlet import_bundle = ImportBundle{dsnp_keys:DsnpKeys{keys_hash:123,// get from blockchainkeys:vec![/* published keys got from blockchain */],dsnp_user_id: my_dsnp_user_id,},dsnp_user_id: my_dsnp_user_id,schema_id: private_follow_graph_schema_id,pages:vec![/* published graph pages got from blockchain */],key_pairs:vec![/* get key-pairs associated with the my_dsnp_user_id user from wallet */],};ifletErr(e) = state.import_users_data(&vec![import_bundle]){println!("{:?}", e);return}// add a new connection to the owner graphlet add_connection = Action::Connect{connection:Connection{dsnp_user_id:3000,schema_id: private_follow_graph_schema_id },owner_dsnp_user_id: my_dsnp_user_id,dsnp_keys:None,};// add new connection to graphlet apply_result = state.apply_actions(&vec![add_connection]);ifletErr(e) = apply_result {println!("{:?}", e);return}// export all updates to the graphmatch state.export_updates(){Ok(updates) => {println!("Updates {:?}", updates);// Update Blockchain using the updates},Err(e) => {println!("{:?}", e);},}}
Update a Private Friendship graph
use dsnp_graph_config::{ConnectionType,Environment,PrivacyType};use dsnp_graph_core::api::{
api::{GraphAPI,GraphState},
api_types::{Action,Connection,DsnpKeys,ImportBundle},};fnmain(){// create graph state with chosen environmentletmut state = GraphState::new(Environment::Mainnet);// graph owner that we want to interact withlet my_dsnp_user_id = 1000;// get that desired graph schema idlet private_friendship_graph_schema_id = Environment::Mainnet.get_config().get_schema_id_from_connection_type(ConnectionType::Friendship(PrivacyType::Private)).unwrap();// import existing published graph and keys fetched from blockchainlet import_bundle = ImportBundle{dsnp_keys:DsnpKeys{keys_hash:123,// get from blockchainkeys:vec![/* published keys got from blockchain */],dsnp_user_id: my_dsnp_user_id,},dsnp_user_id: my_dsnp_user_id,schema_id: private_friendship_graph_schema_id,pages:vec![/* published graph pages got from blockchain */],key_pairs:vec![/* get key-pairs associated with the my_dsnp_user_id user from wallet */],};ifletErr(e) = state.import_users_data(&vec![import_bundle]){println!("{:?}", e);return}// get all associated user without keys so we can fetch and import keys for themlet user_without_keys = state.get_connections_without_keys().unwrap();letmut users_import_bundles:Vec<ImportBundle> = vec![];for _user in user_without_keys {// let user_dsnp_keys = DsnpKeys {..} // fetch published DsnpKeys for user// let user_pages = .. // fetch published private friendship pages for the user// let user_import_bundle = {// dsnp_keys: user_dsnp_keys,// dsnp_user_id: user,// schema_id: private_friendship_graph_schema_id,// pages: user_pages,// key_pairs: vec![], // empty key pairs for user since we don't know and need their secret key// }// users_import_bundle.push(user_import_bundle);}// import these fetched keys and pages into stateifletErr(e) = state.import_users_data(&users_import_bundles){println!("{:?}", e);return}// add a new connection to the owner graphlet add_connection = Action::Connect{connection:Connection{dsnp_user_id:400,schema_id: private_friendship_graph_schema_id },owner_dsnp_user_id: my_dsnp_user_id,dsnp_keys:Some(DsnpKeys{dsnp_user_id:400,keys_hash:2982,// get keys hash from chainkeys:vec![/* get keys from chain for user 400 */],}),};// add new connection to graphlet apply_result = state.apply_actions(&vec![add_connection]);ifletErr(e) = apply_result {println!("{:?}", e);return}// export all updates to the graphmatch state.export_updates(){Ok(updates) => {println!("Updates {:?}", updates);// Update Blockchain using the updates},Err(e) => {println!("{:?}", e);},}}