1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Functions for managing accounts.

use alloc::vec::Vec;
use core::convert::TryFrom;

use casperlabs_types::{
    account::{
        ActionType, AddKeyFailure, PublicKey, RemoveKeyFailure, SetThresholdFailure,
        UpdateKeyFailure, Weight,
    },
    bytesrepr, URef, UREF_SERIALIZED_LENGTH,
};

use super::to_ptr;
use crate::{contract_api, ext_ffi, unwrap_or_revert::UnwrapOrRevert};

/// Retrieves the ID of the account's main purse.
pub fn get_main_purse() -> URef {
    let dest_ptr = contract_api::alloc_bytes(UREF_SERIALIZED_LENGTH);
    let bytes = unsafe {
        ext_ffi::get_main_purse(dest_ptr);
        Vec::from_raw_parts(dest_ptr, UREF_SERIALIZED_LENGTH, UREF_SERIALIZED_LENGTH)
    };
    bytesrepr::deserialize(bytes).unwrap_or_revert()
}

/// Sets the given [`ActionType`]'s threshold to the provided value.
pub fn set_action_threshold(
    action_type: ActionType,
    threshold: Weight,
) -> Result<(), SetThresholdFailure> {
    let action_type = action_type as u32;
    let threshold = threshold.value().into();
    let result = unsafe { ext_ffi::set_action_threshold(action_type, threshold) };
    if result == 0 {
        Ok(())
    } else {
        Err(SetThresholdFailure::try_from(result).unwrap_or_revert())
    }
}

/// Adds the given [`PublicKey`] with associated [`Weight`] to the account's associated keys.
pub fn add_associated_key(public_key: PublicKey, weight: Weight) -> Result<(), AddKeyFailure> {
    let (public_key_ptr, public_key_size, _bytes) = to_ptr(public_key);
    // Cast of u8 (weight) into i32 is assumed to be always safe
    let result = unsafe {
        ext_ffi::add_associated_key(public_key_ptr, public_key_size, weight.value().into())
    };
    if result == 0 {
        Ok(())
    } else {
        Err(AddKeyFailure::try_from(result).unwrap_or_revert())
    }
}

/// Removes the given [`PublicKey`] from the account's associated keys.
pub fn remove_associated_key(public_key: PublicKey) -> Result<(), RemoveKeyFailure> {
    let (public_key_ptr, public_key_size, _bytes) = to_ptr(public_key);
    let result = unsafe { ext_ffi::remove_associated_key(public_key_ptr, public_key_size) };
    if result == 0 {
        Ok(())
    } else {
        Err(RemoveKeyFailure::try_from(result).unwrap_or_revert())
    }
}

/// Updates the [`Weight`] of the given [`PublicKey`] in the account's associated keys.
pub fn update_associated_key(
    public_key: PublicKey,
    weight: Weight,
) -> Result<(), UpdateKeyFailure> {
    let (public_key_ptr, public_key_size, _bytes) = to_ptr(public_key);
    // Cast of u8 (weight) into i32 is assumed to be always safe
    let result = unsafe {
        ext_ffi::update_associated_key(public_key_ptr, public_key_size, weight.value().into())
    };
    if result == 0 {
        Ok(())
    } else {
        Err(UpdateKeyFailure::try_from(result).unwrap_or_revert())
    }
}