Struct safe_network::client::client_api::Client [−][src]
pub struct Client { /* fields omitted */ }
Expand description
Client object
Implementations
Read the contents of a blob from the network. The contents might be spread across different chunks in the network. This function invokes the self-encryptor and returns the data that was initially stored.
Takes position
and len
arguments which specify the start position
and the length of bytes to be read. Passing None
to position reads the data from the beginning.
Passing None
to length reads the full length of the data.
Examples
Get data
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::ChunkAddress; use xor_name::XorName; let head_chunk = ChunkAddress::Public(XorName::random()); let mut client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; // grab the random head of the blob from the network let _data = client.read_blob(head_chunk, None, None).await?;
Store data in public chunks on the network.
This performs self encrypt on the data itself and returns a single address pointing to the head chunk of the blob, and with which the data can be read. It performs data storage as well as all necessary payment validation and checks against the client’s AT2 actor.
Examples
Store data
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::Token; use std::str::FromStr; // Let's use an existing client, with a pre-existing balance to be used for write payments. let mut client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let data = b"some data".to_vec(); // grab the random head of the blob from the network let _address = client.store_public_blob(&data).await?;
Store data in private chunks on the network.
This performs self encrypt on the data itself and returns a single address pointing to the head chunk of the blob, and with which the data can be read. It performs data storage as well as all necessary payment validation and checks against the client’s AT2 actor.
Examples
Store data
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::Token; use std::str::FromStr; // Let's use an existing client, with a pre-existing balance to be used for write payments. let mut client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let data = b"some data".to_vec(); // grab the random head of the blob from the network let fetched_data = client.store_private_blob(&data).await?; println!( "{:?}", fetched_data ); // prints "some data"
Clear the client’s blob cache
Delete blob can only be performed on private chunks. But on those private chunks this will remove the data from the network.
Examples
Remove data
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::Token; use std::str::FromStr; // Let's use an existing client, with a pre-existing balance to be used for write payments. let mut client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let data = b"some private data".to_vec(); let address = client.store_private_blob(&data).await?; let _ = client.delete_blob(address).await?; // Now when we attempt to retrieve the blob, we should get an error match client.read_blob(address, None, None).await { Err(error) => eprintln!("Expected error getting blob {:?}", error), _ => return Err(anyhow::anyhow!("Should not have been able to retrieve this blob")) };
pub async fn store_seq_map(
&self,
name: XorName,
tag: u64,
owner: PublicKey,
entries: Option<MapSeqEntries>,
permissions: Option<BTreeMap<PublicKey, MapPermissionSet>>
) -> Result<MapAddress, Error>
pub async fn store_seq_map(
&self,
name: XorName,
tag: u64,
owner: PublicKey,
entries: Option<MapSeqEntries>,
permissions: Option<BTreeMap<PublicKey, MapPermissionSet>>
) -> Result<MapAddress, Error>
Store a new sequenced Map
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapSeqValue, MapSeqEntries}; use rand::rngs::OsRng; use std::collections::BTreeMap; use xor_name::XorName; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 15001; let mut entries = MapSeqEntries::default(); let mut permissions = BTreeMap::default(); let permission_set = MapPermissionSet::new().allow(MapAction::Read); let _ = permissions.insert(client.public_key(), permission_set); let _ = entries.insert(b"key".to_vec(), MapSeqValue { data: b"value".to_vec(), version: 0 }); let owner = client.public_key(); let _ = client.store_seq_map(name, tag, owner, Some(entries), Some(permissions)).await?;
pub async fn store_unseq_map(
&self,
name: XorName,
tag: u64,
owner: PublicKey,
entries: Option<MapUnseqEntries>,
permissions: Option<BTreeMap<PublicKey, MapPermissionSet>>
) -> Result<MapAddress, Error>
pub async fn store_unseq_map(
&self,
name: XorName,
tag: u64,
owner: PublicKey,
entries: Option<MapUnseqEntries>,
permissions: Option<BTreeMap<PublicKey, MapPermissionSet>>
) -> Result<MapAddress, Error>
Store a new unsequenced Map
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapUnseqEntries}; use rand::rngs::OsRng; use std::collections::BTreeMap; use xor_name::XorName; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 15001; let mut entries = MapUnseqEntries::default(); let mut permissions = BTreeMap::default(); let permission_set = MapPermissionSet::new().allow(MapAction::Read); let _ = permissions.insert(client.public_key(), permission_set); let _ = entries.insert(b"key".to_vec(), b"value".to_vec()); let owner = client.public_key(); let _ = client.store_unseq_map(name, tag, owner, Some(entries), Some(permissions)).await?;
Delete Map
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapUnseqEntries}; use rand::rngs::OsRng; use std::collections::BTreeMap; use xor_name::XorName; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 15001; let mut entries = MapUnseqEntries::default(); let mut permissions = BTreeMap::default(); let permission_set = MapPermissionSet::new().allow(MapAction::Read); let _ = permissions.insert(client.public_key(), permission_set); let _ = entries.insert(b"key".to_vec(), b"value".to_vec()); let owner = client.public_key(); let address = client.store_unseq_map(name, tag, owner, Some(entries.clone()), Some(permissions)).await?; let _ = client.delete_map(address).await?;
pub async fn delete_map_user_perms(
&self,
address: MapAddress,
user: PublicKey,
version: u64
) -> Result<(), Error>
pub async fn delete_map_user_perms(
&self,
address: MapAddress,
user: PublicKey,
version: u64
) -> Result<(), Error>
Delete map user permission
pub async fn set_map_user_perms(
&self,
address: MapAddress,
user: PublicKey,
permissions: MapPermissionSet,
version: u64
) -> Result<(), Error>
pub async fn set_map_user_perms(
&self,
address: MapAddress,
user: PublicKey,
permissions: MapPermissionSet,
version: u64
) -> Result<(), Error>
Set map user permissions
pub async fn edit_map_entries(
&self,
address: MapAddress,
changes: MapEntryActions
) -> Result<(), Error>
pub async fn edit_map_entries(
&self,
address: MapAddress,
changes: MapEntryActions
) -> Result<(), Error>
Mutate map user entries
Fetch map data from the network
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapUnseqEntries}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 15001; let mut entries = MapUnseqEntries::default(); let mut permissions = BTreeMap::default(); let permission_set = MapPermissionSet::new().allow(MapAction::Read); let _ = permissions.insert(client.public_key(), permission_set); let _ = entries.insert(b"key".to_vec(), b"value".to_vec()); let owner = client.public_key(); let address = client.store_unseq_map(name, tag, owner, Some(entries.clone()), Some(permissions)).await?; let _ = client.get_map(address).await?;
pub async fn get_map_value(
&self,
address: MapAddress,
key: Vec<u8>
) -> Result<MapValue, Error> where
Self: Sized,
pub async fn get_map_value(
&self,
address: MapAddress,
key: Vec<u8>
) -> Result<MapValue, Error> where
Self: Sized,
Fetch the value for a given key in a map
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{ Keypair, Token, MapAction, MapValue, MapPermissionSet, MapUnseqEntries}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 15001; let mut entries = MapUnseqEntries::default(); let _ = entries.insert(b"beep".to_vec(), b"boop".to_vec() ); let mut permissions = BTreeMap::default(); let permission_set = MapPermissionSet::new().allow(MapAction::Read); let _ = permissions.insert(client.public_key(), permission_set); let _ = entries.insert(b"key".to_vec(), b"value".to_vec()); let owner = client.public_key(); let address = client.store_unseq_map(name, tag, owner, Some(entries.clone()), Some(permissions)).await?; let received_value = match client.get_map_value(address, b"beep".to_vec()).await? { MapValue::Unseq(value) => value, _ => panic!("Exptected an unsequenced map") }; assert_eq!(received_value, b"boop".to_vec());
pub async fn get_map_shell(&self, address: MapAddress) -> Result<Map, Error> where
Self: Sized,
pub async fn get_map_shell(&self, address: MapAddress) -> Result<Map, Error> where
Self: Sized,
Get a shell (bare bones) version of Map
from the network.
pub async fn get_map_version(&self, address: MapAddress) -> Result<u64, Error> where
Self: Sized,
pub async fn get_map_version(&self, address: MapAddress) -> Result<u64, Error> where
Self: Sized,
Get a current version of Map
from the network.
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapUnseqEntries}; use rand::rngs::OsRng; use std::collections::BTreeMap; use xor_name::XorName; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 15001; let mut entries = MapUnseqEntries::default(); let _ = entries.insert(b"beep".to_vec(), b"boop".to_vec() ); let mut permissions = BTreeMap::default(); let permission_set = MapPermissionSet::new().allow(MapAction::Read); let _ = permissions.insert(client.public_key(), permission_set); let _ = entries.insert(b"key".to_vec(), b"value".to_vec()); let owner = client.public_key(); let address = client.store_unseq_map(name, tag, owner, Some(entries.clone()), Some(permissions)).await?; let version = client.get_map_version(address).await?; assert_eq!(version, 0);
pub async fn mutate_seq_map_entries(
&self,
name: XorName,
tag: u64,
actions: MapSeqEntryActions
) -> Result<(), Error> where
Self: Sized,
pub async fn mutate_seq_map_entries(
&self,
name: XorName,
tag: u64,
actions: MapSeqEntryActions
) -> Result<(), Error> where
Self: Sized,
Mutates sequenced Map
entries in bulk
pub async fn mutate_unseq_map_entries(
&self,
name: XorName,
tag: u64,
actions: MapUnseqEntryActions
) -> Result<(), Error> where
Self: Sized,
pub async fn mutate_unseq_map_entries(
&self,
name: XorName,
tag: u64,
actions: MapUnseqEntryActions
) -> Result<(), Error> where
Self: Sized,
Mutates unsequenced Map
entries in bulk
Return a complete list of entries in Map
.
pub async fn list_seq_map_entries(
&self,
name: XorName,
tag: u64
) -> Result<MapSeqEntries, Error> where
Self: Sized,
pub async fn list_seq_map_entries(
&self,
name: XorName,
tag: u64
) -> Result<MapSeqEntries, Error> where
Self: Sized,
Return a complete list of entries in Map
.
pub async fn list_map_keys(
&self,
address: MapAddress
) -> Result<BTreeSet<Vec<u8>>, Error> where
Self: Sized,
pub async fn list_map_keys(
&self,
address: MapAddress
) -> Result<BTreeSet<Vec<u8>>, Error> where
Self: Sized,
Return a list of keys in Map
stored on the network.
pub async fn list_seq_map_values(
&self,
name: XorName,
tag: u64
) -> Result<Vec<MapSeqValue>, Error> where
Self: Sized,
pub async fn list_seq_map_values(
&self,
name: XorName,
tag: u64
) -> Result<Vec<MapSeqValue>, Error> where
Self: Sized,
Return a list of values in a Sequenced Mutable Data
Returns a list of values in an Unsequenced Mutable Data
pub async fn list_map_user_permissions(
&self,
address: MapAddress,
user: PublicKey
) -> Result<MapPermissionSet, Error> where
Self: Sized,
pub async fn list_map_user_permissions(
&self,
address: MapAddress,
user: PublicKey
) -> Result<MapPermissionSet, Error> where
Self: Sized,
Return the permissions set for a particular user
pub async fn list_map_permissions(
&self,
address: MapAddress
) -> Result<BTreeMap<PublicKey, MapPermissionSet>, Error> where
Self: Sized,
pub async fn list_map_permissions(
&self,
address: MapAddress
) -> Result<BTreeMap<PublicKey, MapPermissionSet>, Error> where
Self: Sized,
Return a list of permissions in Map
stored on the network.
pub async fn set_map_user_permissions(
&self,
address: MapAddress,
user: PublicKey,
permissions: MapPermissionSet,
version: u64
) -> Result<(), Error> where
Self: Sized,
pub async fn set_map_user_permissions(
&self,
address: MapAddress,
user: PublicKey,
permissions: MapPermissionSet,
version: u64
) -> Result<(), Error> where
Self: Sized,
Updates or inserts a permissions set for a user
pub async fn del_map_user_permissions(
&self,
address: MapAddress,
user: PublicKey,
version: u64
) -> Result<(), Error> where
Self: Sized,
pub async fn del_map_user_permissions(
&self,
address: MapAddress,
user: PublicKey,
version: u64
) -> Result<(), Error> where
Self: Sized,
Updates or inserts a permissions set for a user
Create a Private Register onto the Network
Creates a private Register on the network which can then be written to. Private data can be removed from the network at a later date.
A tag must be supplied. A xorname must be supplied, this can be random or deterministic as per your apps needs.
Create a Public Register onto the Network
Creates a public Register on the network which can then be written to. Public data can not be removed from the network at a later date.
A tag must be supplied. A xorname must be supplied, this can be random or deterministic as per your apps needs.
Delete Register
You’re only able to delete a PrivateRegister. Public data can no be removed from the network.
Write to Register
Public or private isn’t important for writing, though the data you write will be Public or Private according to the type of the targeted Register.
Get a Register from the Network
Get the last data entry from a Register data.
Get an entry from a Register on the Network by its hash
Get the owner of a Register.
pub async fn get_register_permissions_for_user(
&self,
address: Address,
user: PublicKey
) -> Result<Permissions, Error>
pub async fn get_register_permissions_for_user(
&self,
address: Address,
user: PublicKey
) -> Result<Permissions, Error>
Get the set of Permissions in a Register for a specific user.
pub async fn store_private_sequence(
&self,
sequence: Option<SequenceEntries>,
name: XorName,
tag: u64,
owner: PublicKey,
permissions: BTreeMap<PublicKey, SequencePrivatePermissions>
) -> Result<SequenceAddress, Error>
pub async fn store_private_sequence(
&self,
sequence: Option<SequenceEntries>,
name: XorName,
tag: u64,
owner: PublicKey,
permissions: BTreeMap<PublicKey, SequencePrivatePermissions>
) -> Result<SequenceAddress, Error>
Create Private Sequence Data on to the Network
Creates a private sequence on the network which can be appended to. Private data can be removed from the network at a later date.
A tag must be supplied. A xorname must be supplied, this can be random or deterministic as per your apps needs.
Examples
Store data
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let _address = client.store_private_sequence(None, name, tag, owner, perms).await?;
pub async fn store_public_sequence(
&self,
sequence: Option<SequenceEntries>,
name: XorName,
tag: u64,
owner: PublicKey,
permissions: BTreeMap<SequenceUser, SequencePublicPermissions>
) -> Result<SequenceAddress, Error>
pub async fn store_public_sequence(
&self,
sequence: Option<SequenceEntries>,
name: XorName,
tag: u64,
owner: PublicKey,
permissions: BTreeMap<SequenceUser, SequencePublicPermissions>
) -> Result<SequenceAddress, Error>
Create Public Sequence Data into the Network
Creates a public sequence on the network which can be appended to. Public data can not be removed from the network at a later date.
A tag must be supplied. A xorname must be supplied, this can be random or deterministic as per your apps needs.
Examples
Store data
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, SequenceUser, Token, SequencePublicPermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<SequenceUser, SequencePublicPermissions>::new(); // Set the access permissions let _ = perms.insert( SequenceUser::Key(owner), SequencePublicPermissions::new(true), ); // The returned address can then be used to `append` data to. let _address = client.store_public_sequence(None, name, tag, owner, perms).await?;
Delete sequence
You’re only able to delete a PrivateSequence. Public data can no be removed from the network.
Examples
Delete data
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let address = client.store_private_sequence(None, name, tag, owner, perms).await?; client.delete_sequence(address).await?;
pub async fn append_to_sequence(
&self,
address: SequenceAddress,
entry: SequenceEntry
) -> Result<(), Error>
pub async fn append_to_sequence(
&self,
address: SequenceAddress,
entry: SequenceEntry
) -> Result<(), Error>
Append to Sequence
Public or private isn’t important for append. You can append to either (though the data you append will be Public or Private).
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let address = client.store_private_sequence(None, name, tag, owner, perms).await?; client.append_to_sequence(address, b"New Entry Value".to_vec()).await?;
Get Sequence Data from the Network
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let address = client.store_private_sequence(None, name, tag, owner, perms).await?; let _data = client.get_sequence(address).await?;
pub async fn get_sequence_last_entry(
&self,
address: SequenceAddress
) -> Result<(u64, SequenceEntry), Error>
pub async fn get_sequence_last_entry(
&self,
address: SequenceAddress
) -> Result<(u64, SequenceEntry), Error>
Get the last data entry from a Sequence Data.
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let address = client.store_private_sequence(None, name, tag, owner, perms).await?; client.append_to_sequence(address, b"New Entry Value".to_vec()).await?; client.append_to_sequence(address, b"Another New Entry Value".to_vec()).await?; // Now we can retrieve the alst entry in the sequence: let (_position, last_entry) = client.get_sequence_last_entry(address).await?; assert_eq!(last_entry, b"Another New Entry Value".to_vec());
pub async fn get_sequence_entry(
&self,
address: SequenceAddress,
index_from_start: u64
) -> Result<SequenceEntry, Error>
pub async fn get_sequence_entry(
&self,
address: SequenceAddress,
index_from_start: u64
) -> Result<SequenceEntry, Error>
Get Sequence Data from the Network at a specific version
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let address = client.store_private_sequence(None, name, tag, owner, perms).await?; client.append_to_sequence(address, b"New Entry Value".to_vec()).await?; client.append_to_sequence(address, b"Another New Entry Value".to_vec()).await?; // Now we can retrieve the alst entry in the sequence: let entry_v1 = client.get_sequence_entry(address, 1).await?; assert_eq!(entry_v1, b"Another New Entry Value".to_vec());
pub async fn get_sequence_range(
&self,
address: SequenceAddress,
range: (SequenceIndex, SequenceIndex)
) -> Result<SequenceEntries, Error>
pub async fn get_sequence_range(
&self,
address: SequenceAddress,
range: (SequenceIndex, SequenceIndex)
) -> Result<SequenceEntries, Error>
Get a set of Entries for the requested range from a Sequence.
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions, SequenceIndex}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let address = client.store_private_sequence(None, name, tag, owner, perms).await?; client.append_to_sequence(address, b"New Entry Value".to_vec()).await?; client.append_to_sequence(address, b"Another New Entry Value".to_vec()).await?; client.append_to_sequence(address, b"Third Entry Value".to_vec()).await?; // Now we can retrieve the alst entry in the sequence: let entries = client.get_sequence_range(address, (SequenceIndex::FromStart(1), SequenceIndex::FromEnd(0) )).await?; assert_eq!(entries[0], b"Another New Entry Value".to_vec()); assert_eq!(entries[1], b"Third Entry Value".to_vec());
Get the owner of a Sequence.
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let address = client.store_private_sequence(None, name, tag, owner, perms).await?; let seq_owner = client.get_sequence_owner(address).await?; assert_eq!(seq_owner, owner);
pub async fn get_sequence_public_permissions_for_user(
&self,
address: SequenceAddress,
user: PublicKey
) -> Result<SequencePublicPermissions, Error>
pub async fn get_sequence_public_permissions_for_user(
&self,
address: SequenceAddress,
user: PublicKey
) -> Result<SequencePublicPermissions, Error>
Get the set of Permissions of a Public Sequence.
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, Token, SequenceUser,SequencePublicPermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<SequenceUser, SequencePublicPermissions>::new(); // Set the access permissions let _ = perms.insert( SequenceUser::Key(owner), SequencePublicPermissions::new(true), ); // The returned address can then be used to `append` data to. let address = client.store_public_sequence(None, name, tag, owner, perms).await?; let _permissions = client.get_sequence_public_permissions_for_user(address, owner).await?;
pub async fn get_sequence_private_permissions_for_user(
&self,
address: SequenceAddress,
user: PublicKey
) -> Result<SequencePrivatePermissions, Error>
pub async fn get_sequence_private_permissions_for_user(
&self,
address: SequenceAddress,
user: PublicKey
) -> Result<SequencePrivatePermissions, Error>
Get the set of Permissions of a Private Sequence.
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let address = client.store_private_sequence(None, name, tag, owner, perms).await?; let _permissions = client.get_sequence_private_permissions_for_user(address, owner).await?;
pub async fn get_sequence_permissions(
&self,
address: SequenceAddress,
user: SequenceUser
) -> Result<SequencePermissions, Error>
pub async fn get_sequence_permissions(
&self,
address: SequenceAddress,
user: SequenceUser
) -> Result<SequencePermissions, Error>
Get the set of Permissions for a specific user in a Sequence.
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions}; use std::collections::BTreeMap; use xor_name::XorName; use rand::rngs::OsRng; // Let's use an existing client, with a pre-existing balance to be used for write payments. let id = Keypair::new_ed25519(&mut OsRng); let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let name = XorName::random(); let tag = 10; let owner = client.public_key(); let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new(); // Set the access permissions let _ = perms.insert( owner, SequencePrivatePermissions::new(true, true), ); // The returned address can then be used to `append` data to. let address = client.store_private_sequence(None, name, tag, owner, perms).await?; let _permissions = client.get_sequence_public_permissions_for_user(address, owner).await?;
Handle all token transfers and Write API requests for a given ClientId.
Get the current known account balance from the local actor. (ie. Without querying the network)
Examples
Create a random client
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use std::str::FromStr; use safe_network::types::Token; let client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; // now we check the local balance let some_balance = client.get_local_balance().await; assert_eq!(some_balance, Token::from_str("0")?);
Send token to another PublicKey.
If the PublicKey does not exist as a balance on the network it will be created with the send amount.
Examples
Send token to a PublickKey. (This test uses “simulated payouts” to generate test token. This of course would not be avaiable on a live network.)
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{PublicKey, Token}; use std::str::FromStr; // A random sk, to send token to let sk = bls::SecretKey::random(); let pk = PublicKey::from(sk.public_key()); // Next we create a random client. let mut client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let target_balance = Token::from_str("100")?; // And trigger a simulated payout to our client's PublicKey, so we have token to send. let _ = client.trigger_simulated_farming_payout(target_balance).await?; // Now we have 100 token at our balance, we can send it elsewhere: let (count, sending_pk) = client.send_tokens( pk, target_balance ).await?; // Finally, we can see that the token has arrived: let received_balance = client.get_balance_for(pk).await?; assert_eq!(1, count); assert_ne!(pk, sending_pk); assert_eq!(received_balance, target_balance);
Handle all token transfers and Write API requests for a given ClientId.
Simulate a farming payout & add a balance to the client’s PublicKey.
Useful for testing to generate initial balances needed for sending transfer requests, which is in turn required for performing write operations.
This also keeps the client transfer actor up to date.
Examples
Add 100 token to a client
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, Token}; use std::str::FromStr; use rand::rngs::OsRng; let id = Keypair::new_ed25519(&mut OsRng); // Start our client let mut client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let target_balance = Token::from_str("100")?; let _ = client.trigger_simulated_farming_payout(target_balance).await?; let balance = client.get_balance().await?; assert_eq!(balance, target_balance);
Get the client’s current coin balance from the network
Examples
Retrieve an existing balance
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, Token}; use rand::rngs::OsRng; use std::str::FromStr; // Let's check the balance of a client with a random id. // (It should have 0 balance) let id = Keypair::new_ed25519(&mut OsRng); let client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let initial_balance = Token::from_str("0")?; let balance = client.get_balance().await?; assert_eq!(balance, initial_balance);
Get balance for a Public Key on the network.
Examples
Retrieve an existing balance
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::{Keypair, Token}; use std::str::FromStr; use rand::rngs::OsRng; // Let's check the balance of a client with a random id. let id = Keypair::new_ed25519(&mut OsRng); let pk = id.public_key(); // And we use a random client to do this let client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let initial_balance = Token::from_str("0")?; let balance = client.get_balance_for(pk).await?; assert_eq!(balance, initial_balance);
Retrieve the history of the account from the network and apply to our local client’s AT2 actor.
Examples
Retrieving an existing balance history
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; use safe_network::types::Keypair; use rand::rngs::OsRng; // Let's check the balance of a random client. // And we use a random client id to do this let id = Keypair::new_ed25519(&mut OsRng); let client = Client::new(Some(id), None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; // Upon calling, history is retrieved and applied to the local AT2 actor. let _ = client.get_history().await?;
Easily manage connections to/from The Safe Network with the client and its APIs. Use a random client for read-only or one-time operations. Supply an existing, SecretKey which holds a SafeCoin balance to be able to perform write operations.
Create a Safe Network client instance. Either for an existing SecretKey (in which case) the client will attempt to retrieve the history of the key’s balance in order to be ready for any token operations. Or if no SecreteKey is passed, a random keypair will be used, which provides a client that can only perform Read operations (at least until the client’s SecretKey receives some token).
Examples
Create a random client
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; let _: Result<()> = futures::executor::block_on( async { let client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; // Now for example you can perform read operations: let _some_balance = client.get_balance().await?;
pub fn handle_anti_entropy_errors(
client: Self,
transfer_err_receiver: Receiver<(SocketAddr, ErrorMessage)>
)
pub fn handle_anti_entropy_errors(
client: Self,
transfer_err_receiver: Receiver<(SocketAddr, ErrorMessage)>
)
Sets up a listener for Cmd Errors that may need transfer debits to be resent to elders
Return the client’s FullId.
Useful for retrieving the PublicKey or KeyPair in the event you need to sign something
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; let client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let _keypair = client.keypair();
Return the client’s PublicKey.
Examples
use safe_network::client::{Client, DEFAULT_QUERY_TIMEOUT}; let client = Client::new(None, None, bootstrap_contacts, DEFAULT_QUERY_TIMEOUT).await?; let _pk = client.public_key();
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Client
impl !UnwindSafe for Client
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V