Struct sn_api::Safe[][src]

pub struct Safe {
    pub xorurl_base: XorUrlBase,
    // some fields omitted
}

Fields

xorurl_base: XorUrlBase

Implementations

impl Safe[src]

pub async fn auth_app(
    app_id: &str,
    app_name: &str,
    app_vendor: &str,
    endpoint: Option<&str>
) -> Result<Keypair>
[src]

Generate an authorisation request string and send it to a SAFE Authenticator. It returns the credentials necessary to connect to the network, encoded in a single string.

pub async fn connect(
    &mut self,
    app_keypair: Option<Keypair>,
    config_path: Option<&Path>,
    bootstrap_config: Option<BootstrapConfig>
) -> Result<()>
[src]

Connect to the SAFE Network using the provided auth credentials

impl Safe[src]

pub fn generate_random_ed_keypair(&self) -> Keypair[src]

pub async fn keys_create_and_preload(
    &self,
    preload_amount: &str
) -> Result<(String, Keypair)>
[src]

pub async fn keys_create_and_preload_from_sk_string(
    &self,
    from: &str,
    preload_amount: &str
) -> Result<(String, Keypair)>
[src]

pub async fn keys_create_preload_test_coins(
    &self,
    preload_amount: &str
) -> Result<(String, Keypair)>
[src]

pub async fn keys_balance_from_sk(
    &self,
    secret_key: SecretKey
) -> Result<String>
[src]

pub async fn keys_balance_from_url(
    &self,
    url: &str,
    secret_key: SecretKey
) -> Result<String>
[src]

pub async fn validate_sk_for_url(
    &self,
    secret_key: &SecretKey,
    url: &str
) -> Result<String>
[src]

pub async fn keys_transfer(
    &self,
    amount: &str,
    from_sk_str: Option<&str>,
    to: &str
) -> Result<u64>
[src]

Transfer safecoins from one SafeKey to another, to a Wallet, or to a PublicKey

Using a secret key you can send safecoins to a SafeKey, Wallet, or PublicKey.

Example

let mut safe = Safe::default();
    let (key1_xorurl, keypair1) = safe.keys_create_preload_test_coins("14").await.unwrap();
    let (key2_xorurl, keypair2) = safe.keys_create_preload_test_coins("1").await.unwrap();
    let current_balance = safe.keys_balance_from_sk(keypair1.clone().unwrap().sk).await.unwrap();
    assert_eq!("14.000000000", current_balance);

    safe.keys_transfer( "10", Some(&keypair1.clone().unwrap().sk), &key2_xorurl, None ).await.unwrap();
    let from_balance = safe.keys_balance_from_url( &key1_xorurl, &keypair1.unwrap().sk ).await.unwrap();
    assert_eq!("4.000000000", from_balance);
    let to_balance = safe.keys_balance_from_url( &key2_xorurl, &keypair2.unwrap().sk ).await.unwrap();
    assert_eq!("11.000000000", to_balance);

impl Safe[src]

pub async fn sequence_create(
    &mut self,
    data: &[u8],
    name: Option<XorName>,
    type_tag: u64,
    private: bool
) -> Result<XorUrl>
[src]

Create a Public Sequence on the network

Example

    let data = b"First in the sequence";
    let xorurl = safe.sequence_create(data, None, 20_000, false).await.unwrap();
    let received_data = safe.sequence_get(&xorurl).await.unwrap();
    assert_eq!(received_data, (0, data.to_vec()));

pub async fn sequence_get(&mut self, url: &str) -> Result<(u64, Vec<u8>)>[src]

Get data from a Public Sequence on the network

Example

    let data = b"First in the sequence";
    let xorurl = safe.sequence_create(data, None, 20_000, false).await.unwrap();
    let received_data = safe.sequence_get(&xorurl).await.unwrap();
    assert_eq!(received_data, (0, data.to_vec()));

pub async fn append_to_sequence(&mut self, url: &str, data: &[u8]) -> Result<()>[src]

Append data to a Public Sequence on the network

Example

    let data1 = b"First in the sequence";
    let xorurl = safe.sequence_create(data1, None, 20_000, false).await.unwrap();
    let data2 = b"Second in the sequence";
    safe.append_to_sequence(&xorurl, data2).await.unwrap();
    let received_data = safe.sequence_get(&xorurl).await.unwrap();
    assert_eq!(received_data, (1, data2.to_vec()));

impl Safe[src]

pub async fn fetch(&self, url: &str, range: Range) -> Result<SafeData>[src]

Retrieve data from a safe:// URL

Examples

Fetch FilesContainer relative path file

    let (xorurl, _, _) = safe.files_container_create(Some("../testdata/"), None, true, false, false).await.unwrap();

    let safe_data = safe.fetch( &format!( "{}/test.md", &xorurl.replace("?v=0", "") ), None ).await.unwrap();
    let data_string = match safe_data {
        SafeData::PublicBlob { data, .. } => {
            match String::from_utf8(data) {
                Ok(string) => string,
                Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
            }
        }
        other => panic!(
            "Content type '{:?}' should not have been found. This should be immutable data.",
            other
        )
    };

    assert!(data_string.starts_with("hello tests!"));

pub async fn inspect(&mut self, url: &str) -> Result<Vec<SafeData>>[src]

Inspect a safe:// URL and retrieve metadata information but the actual target content

As opposed to ‘fetch’ function, the actual target content won’t be fetched, and only

the URL will be inspected resolving it as necessary to find the target location.

This is helpful if you are interested in knowing about the target content rather than

trying to revieve the actual content.

Examples

Inspect FilesContainer relative path file

    let (container_xorurl, _, _) = safe.files_container_create(Some("../testdata/"), None, true, false, false).await.unwrap();

    let inspected_content = safe.inspect( &format!( "{}/test.md", &container_xorurl.replace("?v=0", "") ) ).await.unwrap();
    match &inspected_content[0] {
        SafeData::FilesContainer { xorurl, .. } => {
            assert_eq!(*xorurl, container_xorurl);
        }
        other => panic!(
            "Content type '{:?}' should not have been found. This should be a Files Container.",
            other
        )
    };
    match &inspected_content[1] {
        SafeData::PublicBlob { data, media_type, .. } => {
            assert_eq!(*media_type, Some("text/markdown".to_string()));
            assert!(data.is_empty());
        }
        other => panic!(
            "Content type '{:?}' should not have been found. This should be an Immutable Data.",
            other
        )
    };

impl Safe[src]

pub async fn files_container_create(
    &mut self,
    location: Option<&str>,
    dest: Option<&str>,
    recursive: bool,
    follow_links: bool,
    dry_run: bool
) -> Result<(XorUrl, ProcessedFiles, FilesMap)>
[src]

Create a FilesContainer.

Example

    safe.connect("", Some("fake-credentials")).await.unwrap();
    let (xorurl, _processed_files, _files_map) = safe.files_container_create(Some("../testdata"), None, true, true, false).await.unwrap();
    assert!(xorurl.contains("safe://"))

pub async fn files_container_get(
    &mut self,
    url: &str
) -> Result<(u64, FilesMap)>
[src]

Fetch an existing FilesContainer.

Example

    let (xorurl, _processed_files, _files_map) = safe.files_container_create(Some("../testdata"), None, true, true, false).await.unwrap();
    let (version, files_map) = safe.files_container_get(&xorurl).await.unwrap();
    println!("FilesContainer fetched is at version: {}", version);
    println!("FilesMap of fetched version is: {:?}", files_map);

pub async fn files_container_sync(
    &mut self,
    location: &str,
    url: &str,
    recursive: bool,
    follow_links: bool,
    delete: bool,
    update_nrs: bool,
    dry_run: bool
) -> Result<(u64, ProcessedFiles, FilesMap)>
[src]

Sync up local folder with the content on a FilesContainer.

Example

    let (xorurl, _processed_files, _files_map) = safe.files_container_create(Some("../testdata"), None, true, false, false).await.unwrap();
    let (version, new_processed_files, new_files_map) = safe.files_container_sync("../testdata", &xorurl, true, true, false, false, false).await.unwrap();
    println!("FilesContainer synced up is at version: {}", version);
    println!("The local files that were synced up are: {:?}", new_processed_files);
    println!("The FilesMap of the updated FilesContainer now is: {:?}", new_files_map);

pub async fn files_container_add(
    &mut self,
    source_file: &str,
    url: &str,
    force: bool,
    update_nrs: bool,
    follow_links: bool,
    dry_run: bool
) -> Result<(u64, ProcessedFiles, FilesMap)>
[src]

Add a file, either a local path or an already uploaded file, on an existing FilesContainer.

Example

    let (xorurl, _processed_files, _files_map) = safe.files_container_create(Some("../testdata"), None, true, true, false).await.unwrap();
    let new_file_name = format!("{}/new_name_test.md", xorurl);
    let (version, new_processed_files, new_files_map) = safe.files_container_add("../testdata/test.md", &new_file_name, false, false, true, false).await.unwrap();
    println!("FilesContainer is now at version: {}", version);
    println!("The local files that were synced up are: {:?}", new_processed_files);
    println!("The FilesMap of the updated FilesContainer now is: {:?}", new_files_map);

pub async fn files_container_add_from_raw(
    &mut self,
    data: &[u8],
    url: &str,
    force: bool,
    update_nrs: bool,
    dry_run: bool
) -> Result<(u64, ProcessedFiles, FilesMap)>
[src]

Add a file, from raw bytes, on an existing FilesContainer.

Example

    let (xorurl, _processed_files, _files_map) = safe.files_container_create(Some("../testdata"), None, true, true, false).await.unwrap();
    let new_file_name = format!("{}/new_name_test.md", xorurl);
    let (version, new_processed_files, new_files_map) = safe.files_container_add_from_raw(b"0123456789", &new_file_name, false, false, false).await.unwrap();
    println!("FilesContainer is now at version: {}", version);
    println!("The local files that were synced up are: {:?}", new_processed_files);
    println!("The FilesMap of the updated FilesContainer now is: {:?}", new_files_map);

pub async fn files_container_remove_path(
    &mut self,
    url: &str,
    recursive: bool,
    update_nrs: bool,
    dry_run: bool
) -> Result<(u64, ProcessedFiles, FilesMap)>
[src]

Remove a file from an existing FilesContainer.

Example

    let (xorurl, processed_files, files_map) = safe.files_container_create(Some("../testdata/"), None, true, true, false).await.unwrap();
    let remote_file_path = format!("{}/test.md", xorurl);
    let (version, new_processed_files, new_files_map) = safe.files_container_remove_path(&remote_file_path, false, false, false).await.unwrap();
    println!("FilesContainer is now at version: {}", version);
    println!("The files that were removed: {:?}", new_processed_files);
    println!("The FilesMap of the updated FilesContainer now is: {:?}", new_files_map);

pub async fn files_store_public_blob(
    &self,
    data: &[u8],
    media_type: Option<&str>,
    dry_run: bool
) -> Result<XorUrl>
[src]

Put a Public Blob

Put data blobs onto the network.

Example

    let data = b"Something super good";
    let xorurl = safe.files_store_public_blob(data, Some("text/plain"), false).await.unwrap();
    let received_data = safe.files_get_public_blob(&xorurl, None).await.unwrap();
    assert_eq!(received_data, data);

pub async fn files_get_public_blob(
    &mut self,
    url: &str,
    range: Range
) -> Result<Vec<u8>>
[src]

Get a Public Blob

Get blob from the network.

Example

    let data = b"Something super good";
    let xorurl = safe.files_store_public_blob(data, None, false).await.unwrap();
    let received_data = safe.files_get_public_blob(&xorurl, None).await.unwrap();
    assert_eq!(received_data, data);

impl Safe[src]

pub async fn multimap_create(
    &self,
    name: Option<XorName>,
    type_tag: u64,
    private: bool
) -> Result<XorUrl>
[src]

Create a Multimap on the network

pub async fn multimap_get_by_key(
    &self,
    url: &str,
    key: &[u8]
) -> Result<MultimapKeyValues>
[src]

Return the value of a Multimap on the network corresponding to the key provided

pub async fn multimap_get_by_hash(
    &self,
    url: &str,
    hash: EntryHash
) -> Result<Option<MultimapKeyValue>>
[src]

Return the value of a Multimap on the network corresponding to the hash provided

pub async fn multimap_insert(
    &self,
    url: &str,
    entry: MultimapKeyValue,
    replace: BTreeSet<EntryHash>
) -> Result<EntryHash>
[src]

Insert a key-value pair into a Multimap on the network

pub async fn multimap_remove(
    &self,
    url: &str,
    to_remove: BTreeSet<EntryHash>
) -> Result<EntryHash>
[src]

Remove a key from a Multimap on the network

impl Safe[src]

pub fn parse_url(url: &str) -> Result<SafeUrl>[src]

pub async fn nrs_map_container_add(
    &self,
    name: &str,
    link: &str,
    default: bool,
    hard_link: bool,
    dry_run: bool
) -> Result<(u64, XorUrl, ProcessedEntries, NrsMap)>
[src]

pub async fn nrs_map_container_create(
    &mut self,
    name: &str,
    link: &str,
    default: bool,
    hard_link: bool,
    dry_run: bool
) -> Result<(XorUrl, ProcessedEntries, NrsMap)>
[src]

Create a NrsMapContainer.

Example

    let rand_string: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();
    let file_xorurl = safe.files_store_public_blob(&vec![], None, false).await.unwrap();
    let (xorurl, _processed_entries, nrs_map_container) = safe.nrs_map_container_create(&rand_string, &file_xorurl, true, false, false).await.unwrap();
    assert!(xorurl.contains("safe://"))

pub async fn nrs_map_container_remove(
    &self,
    name: &str,
    dry_run: bool
) -> Result<(u64, XorUrl, ProcessedEntries, NrsMap)>
[src]

pub async fn nrs_map_container_get(&self, url: &str) -> Result<(u64, NrsMap)>[src]

Fetch an existing NrsMapContainer.

Example

    let rand_string: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect();
    let file_xorurl = safe.files_store_public_blob(&vec![], Some("text/plain"), false).await.unwrap();
    let (xorurl, _processed_entries, _nrs_map) = safe.nrs_map_container_create(&rand_string, &file_xorurl, true, false, false).await.unwrap();
    let (version, nrs_map_container) = safe.nrs_map_container_get(&xorurl).await.unwrap();
    assert_eq!(version, 0);
    assert_eq!(nrs_map_container.get_default_link().unwrap(), file_xorurl);

impl Safe[src]

pub async fn register_create(
    &self,
    name: Option<XorName>,
    type_tag: u64,
    private: bool
) -> Result<XorUrl>
[src]

Create a Register on the network

pub async fn register_read(
    &self,
    url: &str
) -> Result<BTreeSet<(EntryHash, Entry)>>
[src]

Read value from a Register on the network

pub async fn register_read_entry(
    &self,
    url: &str,
    hash: EntryHash
) -> Result<Entry>
[src]

Read value from a Register on the network by its hash

pub async fn write_to_register(
    &self,
    url: &str,
    data: Vec<u8>,
    parents: BTreeSet<EntryHash>
) -> Result<EntryHash>
[src]

Write value to a Register on the network

impl Safe[src]

pub async fn wallet_create(&mut self) -> Result<XorUrl>[src]

pub async fn wallet_insert(
    &mut self,
    url: &str,
    name: Option<&str>,
    default: bool,
    sk: &str
) -> Result<String>
[src]

pub async fn wallet_balance(&mut self, url: &str) -> Result<String>[src]

pub async fn wallet_get_default_balance(
    &self,
    url: &str
) -> Result<(WalletSpendableBalance, u64)>
[src]

pub async fn wallet_transfer(
    &mut self,
    amount: &str,
    from_wallet_url: &str,
    to: &str
) -> Result<u64>
[src]

Transfer safecoins from one Wallet to another

Using established Wallet and SpendableBalances you can send safecoins between Wallets.

Example

let mut safe = Safe::default();
    let wallet_xorurl = safe.wallet_create().await.unwrap();
    let wallet_xorurl2 = safe.wallet_create().await.unwrap();
    let (key1_xorurl, keypair1) = safe.keys_create_preload_test_coins("14").await.unwrap();
    let (key2_xorurl, keypair2) = safe.keys_create_preload_test_coins("1").await.unwrap();
    safe.wallet_insert(
        &wallet_xorurl,
        Some("frombalance"),
        true,
        &keypair1.clone().unwrap().sk,
    ).await.unwrap();
    let current_balance = safe.wallet_balance(&wallet_xorurl).await.unwrap();
    assert_eq!("14.000000000", current_balance);

    safe.wallet_insert(
        &wallet_xorurl2,
        Some("tobalance"),
        true,
        &keypair2.clone().unwrap().sk,
    ).await.unwrap();


    safe.wallet_transfer( "10", Some(&wallet_xorurl), &wallet_xorurl2, None ).await.unwrap();
    let from_balance = safe.keys_balance_from_url( &key1_xorurl, &keypair1.unwrap().sk ).await.unwrap();
    assert_eq!("4.000000000", from_balance);
    let to_balance = safe.keys_balance_from_url( &key2_xorurl, &keypair2.unwrap().sk ).await.unwrap();
    assert_eq!("11.000000000", to_balance);

pub async fn wallet_get(&mut self, url: &str) -> Result<WalletSpendableBalances>[src]

impl Safe[src]

pub fn new(xorurl_base: Option<XorUrlBase>, timeout: Duration) -> Self[src]

pub fn keypair(&self) -> Keypair[src]

Generate a new random Ed25519 keypair

pub async fn get_my_keypair(&self) -> Result<Keypair>[src]

Retrieve the keypair this instance was instantiated with, i.e. the keypair this instance uses by default to sign each outgoing message

Trait Implementations

impl Clone for Safe[src]

fn clone(&self) -> Safe[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Default for Safe[src]

fn default() -> Self[src]

Returns the “default value” for a type. Read more

Auto Trait Implementations

impl !RefUnwindSafe for Safe

impl Send for Safe

impl Sync for Safe

impl Unpin for Safe

impl !UnwindSafe for Safe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V