Skip to content

Commit

Permalink
feat: New commands to manage data in SocialDB (#38)
Browse files Browse the repository at this point in the history
Resolves #16 #17
  • Loading branch information
FroVolod committed May 28, 2023
1 parent 3530ce0 commit 552711c
Show file tree
Hide file tree
Showing 19 changed files with 915 additions and 125 deletions.
68 changes: 34 additions & 34 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Expand Up @@ -39,12 +39,12 @@ near-primitives = "0.16.1"
near-jsonrpc-client = "0.5.1"
near-jsonrpc-primitives = "0.16.1"

interactive-clap = "0.2.1"
interactive-clap-derive = "0.2.1"
interactive-clap = "0.2.2"
interactive-clap-derive = "0.2.2"

console = "0.15.5"

near-cli-rs = { version = "0.4.1", default-features = false }
near-cli-rs = { version = "0.4.2", default-features = false }

[target.'cfg(target_os = "macos")'.dependencies]
security-framework = "2.7.0"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -15,6 +15,12 @@ Currently, only two groups of commands are implemented:

### socialdb - SocialDb management

#### data - Data management: viewing, adding, updating, deleting information by a given key

- `view` allows you to view information by a given key.
- `set` allows you to add or update information by a given key.
- `delete` allows you to delete information by the specified key.

#### prepaid-storage - Storage management: deposit, withdrawal, balance review

- `view-balance` allows you to view the storage balance for an account.
Expand Down
95 changes: 95 additions & 0 deletions src/common.rs
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::str::FromStr;

use color_eyre::eyre::{ContextCompat, WrapErr};
use console::{style, Style};
Expand Down Expand Up @@ -195,6 +196,77 @@ pub fn get_access_key_permission(
Ok(permission)
}

pub fn get_deposit(
network_config: &near_cli_rs::config::NetworkConfig,
signer_account_id: &near_primitives::types::AccountId,
signer_public_key: &near_crypto::PublicKey,
deploy_to_account_id: &near_primitives::types::AccountId,
key: &str,
near_social_account_id: &near_primitives::types::AccountId,
required_deposit: near_cli_rs::common::NearBalance,
) -> color_eyre::eyre::Result<near_cli_rs::common::NearBalance> {
let signer_access_key_permission = crate::common::get_access_key_permission(
network_config,
signer_account_id,
signer_public_key,
)?;

let is_signer_access_key_full_access = matches!(
signer_access_key_permission,
near_primitives::views::AccessKeyPermissionView::FullAccess
);

let is_write_permission_granted_to_public_key = crate::common::is_write_permission_granted(
network_config,
near_social_account_id,
signer_public_key.clone(),
format!("{deploy_to_account_id}/{key}"),
)?;

let is_write_permission_granted_to_signer = crate::common::is_write_permission_granted(
network_config,
near_social_account_id,
signer_account_id.clone(),
format!("{deploy_to_account_id}/{key}"),
)?;

let deposit = if is_signer_access_key_full_access
|| crate::common::is_signer_access_key_function_call_access_can_call_set_on_social_db_account(
near_social_account_id,
&signer_access_key_permission
)?
{
if is_write_permission_granted_to_public_key || is_write_permission_granted_to_signer {
if required_deposit.is_zero()
{
near_cli_rs::common::NearBalance::from_str("0 NEAR").unwrap()
} else if is_signer_access_key_full_access {
required_deposit
} else {
color_eyre::eyre::bail!("ERROR: Social DB requires more storage deposit, but we cannot cover it when signing transaction with a Function Call only access key")
}
} else if signer_account_id == deploy_to_account_id {
if is_signer_access_key_full_access {
if required_deposit.is_zero()
{
near_cli_rs::common::NearBalance::from_str("1 yoctoNEAR").unwrap()
} else {
required_deposit
}
} else {
color_eyre::eyre::bail!("ERROR: Social DB requires more storage deposit, but we cannot cover it when signing transaction with a Function Call only access key")
}
} else {
color_eyre::eyre::bail!(
"ERROR: the signer is not allowed to modify the components of this account_id."
)
}
} else {
color_eyre::eyre::bail!("ERROR: signer access key cannot be used to sign a transaction to update components in Social DB.")
};
Ok(deposit)
}

pub fn required_deposit(
network_config: &near_cli_rs::config::NetworkConfig,
near_social_account_id: &near_primitives::types::AccountId,
Expand Down Expand Up @@ -306,3 +378,26 @@ fn estimate_data_size(data: &serde_json::Value, prev_data: Option<&serde_json::V
}
}
}

/// Helper function that marks SocialDB values to be deleted by setting `null` to the values
pub fn mark_leaf_values_as_null(data: &mut serde_json::Value) {
match data {
serde_json::Value::Object(object_data) => {
for value in object_data.values_mut() {
mark_leaf_values_as_null(value);
}
}
data => {
*data = serde_json::Value::Null;
}
}
}

pub fn social_db_data_from_key(full_key: &str, data_to_set: &mut serde_json::Value) {
if let Some((prefix, key)) = full_key.rsplit_once('/') {
*data_to_set = serde_json::json!({ key: data_to_set });
social_db_data_from_key(prefix, data_to_set)
} else {
*data_to_set = serde_json::json!({ full_key: data_to_set });
}
}

0 comments on commit 552711c

Please sign in to comment.