Skip to content

Commit

Permalink
Merge pull request #68 from kommitters/v0.4
Browse files Browse the repository at this point in the history
Release v0.4.0
  • Loading branch information
Jenietoc committed Apr 14, 2023
2 parents 3bddcf8 + e433825 commit bb8c22c
Show file tree
Hide file tree
Showing 19 changed files with 761 additions and 311 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.4.0 (14.04.2023)

- [Refactor](https://github.com/kommitters/chaincerts_governance/milestone/4)

## 0.3.0 (04.04.2023)

- [Error handling](https://github.com/kommitters/chaincerts_governance/milestone/3)
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ codegen-units = 1
lto = true

[workspace.dependencies.soroban-sdk]
version = "0.6.0"
version = "0.7.0"

[workspace.dependencies.soroban-auth]
version = "0.6.0"
version = "0.7.0"
316 changes: 315 additions & 1 deletion README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion certs_governance/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "certs_governance"
version = "0.3.0"
version = "0.4.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Binary file modified certs_governance/certs_wallet.wasm
Binary file not shown.
112 changes: 56 additions & 56 deletions certs_governance/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Module Contract
//!
//! Module containing the main contract logic.
use crate::certs_wallet::{self, OptU64};
use crate::certs_wallet::{self, OptionU64};
use crate::error::ContractError;
use crate::governance_trait::GovernanceTrait;
use crate::metadata::{
Expand All @@ -19,13 +19,14 @@ pub struct CertGovernance;

#[contractimpl]
impl GovernanceTrait for CertGovernance {
fn init_w_r(
/// Initialize the contract a list of receivers or with the limit of Chaincerts that can be distributed.
fn initialize(
e: Env,
file_storage: Bytes,
name: Bytes,
revocable: bool,
expiration_time: Option<u64>,
receivers: Vec<Address>,
receivers: Option<Vec<Address>>,
distribution_limit: Option<u32>,
governance_rules: (bool, OptionU64), // (revocable, expiration_time)
organization: Organization,
) {
if has_organization(&e) {
Expand All @@ -34,35 +35,26 @@ impl GovernanceTrait for CertGovernance {
write_organization(&e, organization);
write_file_storage(&e, file_storage);
write_name(&e, name);
write_revocable(&e, revocable);
write_expiration_time(&e, expiration_time);
write_distribution_limit(&e, receivers.len());
create_receivers(&e, receivers);
write_supply(&e, 0);
}

fn init_w_l(
e: Env,
file_storage: Bytes,
name: Bytes,
revocable: bool,
expiration_time: Option<u64>,
distribution_limit: u32,
organization: Organization,
) {
if has_organization(&e) {
panic_with_error!(&e, ContractError::AlreadyInit);
}
write_organization(&e, organization);
write_file_storage(&e, file_storage);
write_name(&e, name);
write_revocable(&e, revocable);
write_expiration_time(&e, expiration_time);
write_distribution_limit(&e, distribution_limit);
write_receivers(&e, Map::<Address, CertData>::new(&e));
write_revocable(&e, governance_rules.0);
write_expiration_time(&e, governance_rules.1);
write_supply(&e, 0);
match receivers {
Some(receivers) => {
write_distribution_limit(&e, receivers.len());
create_receivers(&e, receivers);
}
None => {
if let Some(distribution_limit) = distribution_limit {
write_distribution_limit(&e, distribution_limit);
} else {
write_distribution_limit(&e, 10);
}
write_receivers(&e, Map::<Address, CertData>::new(&e));
}
};
}

/// Distribute a Chaincert to a receiver.
fn distribute(
e: Env,
admin: Address,
Expand All @@ -85,66 +77,74 @@ impl GovernanceTrait for CertGovernance {
};
}

fn revoke(e: Env, admin: Address, receiver: Address, wallet_contract_id: BytesN<32>) {
/// Revoke a Chaincert from a holder.
fn revoke(e: Env, admin: Address, holder: Address, wallet_contract_id: BytesN<32>) {
check_revocable(&e);
check_admin(&e, &admin);
admin.require_auth();
let mut receivers: Map<Address, CertData> = read_receivers(&e);
let mut cert_data: CertData = receivers.get(receiver.clone()).unwrap().unwrap();
let mut cert_data: CertData = receivers.get(holder.clone()).unwrap().unwrap();
check_receiver_status_for_revoke(&e, &cert_data);

revoke_from_wallet(&e, wallet_contract_id, &cert_data.id_cert);
revoke_from_wallet(&e, wallet_contract_id, &cert_data.id);
cert_data.status = Status::Revoked;
receivers.set(receiver, cert_data);
receivers.set(holder, cert_data);
write_receivers(&e, receivers);
}

/// Get the Chaincert name.
fn name(e: Env) -> Bytes {
read_name(&e)
}

fn revocable(e: Env) -> bool {
/// Get if the Chaincert can be revoked or not.
fn is_revocable(e: Env) -> bool {
read_revocable(&e)
}

fn exp_time(e: Env) -> Option<u64> {
/// Get the Chaincert expiration time (Epoch time).
fn expiration_time(e: Env) -> OptionU64 {
read_expiration_time(&e)
}

fn dist_limit(e: Env) -> u32 {
/// Get the maximum number of Chaincerts that can be distributed by this contract.
fn distribution_limit(e: Env) -> u32 {
read_distribution_limit(&e)
}

fn f_storage(e: Env) -> Bytes {
read_file_storage(&e)
}

/// Get number of Chaincerts that have been distributed.
fn supply(e: Env) -> u32 {
read_supply(&e)
}

/// Get the type of decentralized storage service.
fn file_storage(e: Env) -> Bytes {
read_file_storage(&e)
}

/// Get the receivers data in the contract.
fn receivers(e: Env) -> Map<Address, CertData> {
read_receivers(&e)
}

/// Get all relevant contract data.
fn info(e: Env) -> Info {
let exp_time = match read_expiration_time(&e) {
Some(value) => OptU64::Some(value),
None => OptU64::None,
};
Info {
name: read_name(&e),
revocable: read_revocable(&e),
exp_time,
dist_limit: read_distribution_limit(&e),
expiration_time: read_expiration_time(&e),
distribution_limit: read_distribution_limit(&e),
supply: read_supply(&e),
}
}
}

/// Calculates the expiration date of a distributed Chaincert (using Unix time).
fn expiration_date(e: &Env, distribution_date: u64) -> Option<u64> {
read_expiration_time(e).map(|exp_time| distribution_date + exp_time)
/// Calculates the expiration date of a distributed Chaincert (using Epoch Unix Timestamp, and Epoch time).
fn expiration_date(e: &Env, distribution_date: u64) -> OptionU64 {
match read_expiration_time(e) {
OptionU64::Some(value) => OptionU64::Some(distribution_date + value),
OptionU64::None => OptionU64::None,
}
}

/// Checks that no more chain certificates are issued than allowed by the distribution limit.
Expand Down Expand Up @@ -189,13 +189,13 @@ fn distribute_receiver(
deposit_to_wallet(
e,
wallet_contract_id,
cert_data.id_cert.clone(),
cert_data.id.clone(),
cid,
distribution_date,
);

cert_data.status = Status::Distribute;
cert_data.dist_date = OptU64::Some(distribution_date);
cert_data.distribution_date = OptionU64::Some(distribution_date);
receivers.set(address.clone(), cert_data);
write_receivers(e, receivers);
increment_supply(e);
Expand All @@ -211,9 +211,9 @@ fn deposit_to_wallet(
) {
let wallet_client = certs_wallet::Client::new(e, &wallet_contract_id);
let distributor_contract = e.current_contract_address();
let expiration_date: Option<u64> = expiration_date(e, distribution_date);
let expiration_date: OptionU64 = expiration_date(e, distribution_date);
let org_id = read_organization_id(e);
wallet_client.deposit_cc(
wallet_client.deposit_chaincert(
&chaincert_id,
&cid,
&distributor_contract,
Expand All @@ -228,5 +228,5 @@ fn revoke_from_wallet(e: &Env, wallet_contract_id: BytesN<32>, chaincert_id: &By
let wallet_client = certs_wallet::Client::new(e, &wallet_contract_id);
let distributor_contract = e.current_contract_address();
let org_id = read_organization_id(e);
wallet_client.revoke_cc(chaincert_id, &distributor_contract, &org_id);
wallet_client.revoke_chaincert(chaincert_id, &distributor_contract, &org_id);
}
34 changes: 13 additions & 21 deletions certs_governance/src/governance_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,19 @@
//! Interface that defines the behavior of a Governance contract.
use soroban_sdk::{Address, Bytes, BytesN, Env, Map, Vec};

use crate::storage_types::{CertData, Info, Organization};
use crate::{
certs_wallet::OptionU64,
storage_types::{CertData, Info, Organization},
};
pub trait GovernanceTrait {
/// Initialize the contract with a list of receivers.
fn init_w_r(
/// Initialize the contract a list of receivers or with the limit of Chaincerts that can be distributed.
fn initialize(
e: Env,
file_storage: Bytes,
name: Bytes,
revocable: bool,
expiration_time: Option<u64>,
receivers: Vec<Address>,
organization: Organization,
);

/// Initialize the contract with the limit of Chaincerts that can be distributed.
fn init_w_l(
e: Env,
file_storage: Bytes,
name: Bytes,
revocable: bool,
expiration_time: Option<u64>,
distribution_limit: u32,
receivers: Option<Vec<Address>>,
distribution_limit: Option<u32>,
governance_rules: (bool, OptionU64),
organization: Organization,
);

Expand All @@ -44,19 +36,19 @@ pub trait GovernanceTrait {
fn name(e: Env) -> Bytes;

/// Get if the Chaincert can be revoked or not.
fn revocable(e: Env) -> bool;
fn is_revocable(e: Env) -> bool;

/// Get the Chaincert expiration time (Unix time).
fn exp_time(e: Env) -> Option<u64>;
fn expiration_time(e: Env) -> OptionU64;

/// Get the maximum number of Chaincerts that can be distributed by this contract.
fn dist_limit(e: Env) -> u32;
fn distribution_limit(e: Env) -> u32;

/// Get number of Chaincerts that have been distributed.
fn supply(e: Env) -> u32;

/// Get the type of decentralized storage service.
fn f_storage(e: Env) -> Bytes;
fn file_storage(e: Env) -> Bytes;

/// Get the receivers data in the contract.
fn receivers(e: Env) -> Map<Address, CertData>;
Expand Down
21 changes: 12 additions & 9 deletions certs_governance/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
//! Module MetaData
//!
//! Module for obtaining and modifying the metadata fields.
use crate::storage_types::{CertData, DataKey};
use crate::{
certs_wallet::OptionU64,
storage_types::{CertData, DataKey},
};
use soroban_sdk::{Address, Bytes, Env, Map};

pub fn read_file_storage(e: &Env) -> Bytes {
let key = DataKey::FStorage;
let key = DataKey::FileStorage;
e.storage().get_unchecked(&key).unwrap()
}

pub fn write_file_storage(e: &Env, file_storage: Bytes) {
let key = DataKey::FStorage;
let key = DataKey::FileStorage;
e.storage().set(&key, &file_storage);
}

Expand All @@ -34,13 +37,13 @@ pub fn write_revocable(e: &Env, revocable: bool) {
e.storage().set(&key, &revocable)
}

pub fn read_expiration_time(e: &Env) -> Option<u64> {
let key = DataKey::ExpTime;
pub fn read_expiration_time(e: &Env) -> OptionU64 {
let key = DataKey::ExpirationTime;
e.storage().get_unchecked(&key).unwrap()
}

pub fn write_expiration_time(e: &Env, expiration_time: Option<u64>) {
let key = DataKey::ExpTime;
pub fn write_expiration_time(e: &Env, expiration_time: OptionU64) {
let key = DataKey::ExpirationTime;
e.storage().set(&key, &expiration_time)
}

Expand All @@ -50,12 +53,12 @@ pub fn write_receivers(e: &Env, receivers: Map<Address, CertData>) {
}

pub fn read_distribution_limit(e: &Env) -> u32 {
let key = DataKey::DistLimit;
let key = DataKey::DistributionLimit;
e.storage().get_unchecked(&key).unwrap()
}

pub fn write_distribution_limit(e: &Env, distribution_limit: u32) {
let key = DataKey::DistLimit;
let key = DataKey::DistributionLimit;
e.storage().set(&key, &distribution_limit)
}

Expand Down
10 changes: 5 additions & 5 deletions certs_governance/src/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ use crate::{
use soroban_sdk::{panic_with_error, Address, Bytes, Env};

pub fn has_organization(e: &Env) -> bool {
let key = DataKey::Org;
let key = DataKey::Organization;
e.storage().has(&key)
}

pub fn write_organization(e: &Env, org: Organization) {
let key = DataKey::Org;
let key = DataKey::Organization;
e.storage().set(&key, &org);
}

pub fn read_organization_id(e: &Env) -> Bytes {
let key = DataKey::Org;
let key = DataKey::Organization;
let organization: Organization = e.storage().get_unchecked(&key).unwrap();
organization.id_org
organization.id
}

fn read_organization_admin(e: &Env) -> Address {
let key = DataKey::Org;
let key = DataKey::Organization;
let organization: Organization = e.storage().get_unchecked(&key).unwrap();
organization.admin
}
Expand Down

0 comments on commit bb8c22c

Please sign in to comment.