Skip to content

Commit

Permalink
Implement improvements in Vault and VC Issuance contracts (kommitters…
Browse files Browse the repository at this point in the history
…#156)

* Implement vault improvements

* Implement vc changes

* Refactor code

Co-authored-by: Juan Manuel Giraldo <[email protected]>

* Fix soroban-sdk version

* Adjust multi-contract structure

* Adjust CI and CD

* Update did_contract submodule

* Fix date format

---------

Co-authored-by: Miguel Nieto <[email protected]>
Co-authored-by: Juan Manuel Giraldo <[email protected]>
  • Loading branch information
3 people committed Dec 6, 2023
1 parent f3512d7 commit 74b5fb4
Show file tree
Hide file tree
Showing 22 changed files with 246 additions and 289 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,21 @@ jobs:
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: rustup target add wasm32-unknown-unknown
- name: Build required wasm
run: |
cargo build --target wasm32-unknown-unknown --release --package vault_contract
- name: Contract Build
- run: cargo install --locked --version 20.0.0-rc2 soroban-cli
- name: Build contracts
run: |
cargo build --target wasm32-unknown-unknown --release
soroban contract build
- name: Code Tests
run: |
cargo test
- run: cargo install --locked --version 20.0.0-rc2 soroban-cli
- name: Contract Deployment VC issuance contract to testnet
- name: Deploy VC issuance contract to testnet
run: |
soroban contract deploy \
--wasm target/wasm32-unknown-unknown/release/vc_issuance_contract.wasm \
--source ${{ secrets.DEPLOYER_SECRET }} \
--rpc-url https://soroban-testnet.stellar.org:443/ \
--network-passphrase 'Test SDF Network ; September 2015'
- name: Contract Deployment Vault contract to testnet
- name: Deploy Vault contract to testnet
run: |
soroban contract deploy \
--wasm target/wasm32-unknown-unknown/release/vault_contract.wasm \
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- run: rustup target add wasm32-unknown-unknown
- name: Build required wasm
run: |
cargo build --target wasm32-unknown-unknown --release --package vault_contract
cargo build --target wasm32-unknown-unknown --release --package vault-contract
- name: Contract Build
run: |
cargo build --target wasm32-unknown-unknown --release
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
tarpaulin-report.html
19 changes: 11 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ resolver = "2"

members = ["deployer_contract", "vault_contract", "vc_issuance_contract"]

[profile.release-with-logs]
inherits = "release"
debug-assertions = true
[workspace.package]
version = "0.0.0"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/kommitters/chaincerts-smart-contracts"

[workspace.dependencies]
soroban-sdk = { version = "=20.0.0-rc2.2" }

[profile.release]
opt-level = "z"
Expand All @@ -17,8 +22,6 @@ panic = "abort"
codegen-units = 1
lto = true

[workspace.dependencies.soroban-sdk]
version = "20.0.0-rc2"

[workspace.dependencies.soroban-auth]
version = "0.8.3"
[profile.release-with-logs]
inherits = "release"
debug-assertions = true
8 changes: 5 additions & 3 deletions deployer_contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[package]
name = "deployer_contract"
version = "0.11.1"
edition = "2021"
name = "deployer-contract"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
8 changes: 5 additions & 3 deletions vault_contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[package]
name = "vault_contract"
version = "0.1.0"
edition = "2021"
name = "vault-contract"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
148 changes: 65 additions & 83 deletions vault_contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::did;
use crate::did::{Did, DidWithVCs};
use crate::error::ContractError;
use crate::issuer;
use crate::issuer::Issuer;
use crate::storage;
use crate::verifiable_credential;
use crate::verifiable_credential::VerifiableCredential;

use crate::vault;
use crate::vault::Vault;
use crate::vault_trait::VaultTrait;
use crate::verifiable_credential;
use soroban_sdk::{
contract, contractimpl, contractmeta, panic_with_error, Address, Env, IntoVal, Map, String, Vec,
};
Expand All @@ -31,7 +29,7 @@ impl VaultTrait for VaultContract {
}
storage::write_admin(&e, &admin);

did::set_initial_dids(&e, &dids);
vault::set_initial_vaults(&e, &dids);

e.storage()
.instance()
Expand All @@ -40,14 +38,18 @@ impl VaultTrait for VaultContract {

fn authorize_issuer(e: Env, admin: Address, issuer: Address, did: String) {
validate_admin(&e, admin);
validate_did(&e, &did);

let vaults = storage::read_vaults(&e);
validate_vault(&e, &vaults, &did);

issuer::authorize_issuer(&e, &issuer, &did);
}

fn revoke_issuer(e: Env, admin: Address, issuer: Address, did: String) {
validate_admin(&e, admin);
validate_did(&e, &did);

let vaults = storage::read_vaults(&e);
validate_vault(&e, &vaults, &did);

issuer::revoke_issuer(&e, &issuer, &did)
}
Expand All @@ -57,96 +59,77 @@ impl VaultTrait for VaultContract {
vc_id: String,
vc_data: String,
recipient_did: String,
issuer_pk: Address,
issuance_contract_address: Address,
issuer: Address,
issuance_contract: Address,
) {
validate_did(&e, &recipient_did);
validate_issuer(
&e,
&issuer_pk,
&recipient_did,
&vc_data,
&issuance_contract_address,
);
let mut vaults = storage::read_vaults(&e);
validate_vault(&e, &vaults, &recipient_did);

validate_issuer(&e, &issuer, &recipient_did, &vc_data, &issuance_contract);

verifiable_credential::store_vc(
&e,
&vc_id,
&vc_data,
&issuance_contract_address,
&recipient_did,
&mut vaults,
vc_id,
vc_data,
issuance_contract,
recipient_did,
);
}

fn get_vc(e: Env, vc_id: String) -> VerifiableCredential {
let vcs = storage::read_vcs(&e);
fn register_vault(e: Env, admin: Address, did: String) {
validate_admin(&e, admin);
let mut vaults = storage::read_vaults(&e);

match vcs.get(vc_id) {
Some(vc) => vc,
None => panic_with_error!(&e, ContractError::VCNotFound),
if vault::is_registered(&vaults, &did) {
panic_with_error!(e, ContractError::VaultAlreadyRegistered)
}
}

fn list_vcs(e: Env) -> Map<String, DidWithVCs> {
let vcs = storage::read_vcs(&e);
let dids = storage::read_dids(&e);
let mut dids_with_vcs = Map::new(&e);

for (did, did_struct) in dids {
let mut did_vcs = Vec::new(&e);
for vc in did_struct.vcs {
did_vcs.push_front(vcs.get_unchecked(vc));
}

dids_with_vcs.set(
did.clone(),
DidWithVCs {
did: did.clone(),
is_revoked: did_struct.is_revoked,
vcs: did_vcs,
},
)
}
vaults.set(
did.clone(),
Vault {
did,
revoked: false,
vcs: Vec::new(&e),
},
);

dids_with_vcs
storage::write_vaults(&e, &vaults)
}

fn revoke_did(e: Env, admin: Address, did: String) {
fn revoke_vault(e: Env, admin: Address, did: String) {
validate_admin(&e, admin);
let mut dids = storage::read_dids(&e);
if !did::is_registered(&dids, &did) {
panic_with_error!(e, ContractError::DidNotFound)
let mut vaults = storage::read_vaults(&e);

if !vault::is_registered(&vaults, &did) {
panic_with_error!(e, ContractError::VaultNotFound)
}

let did_struct = dids.get_unchecked(did.clone());
dids.set(
let vault = vaults.get_unchecked(did.clone());

vaults.set(
did.clone(),
Did {
did,
is_revoked: true,
vcs: did_struct.vcs,
Vault {
revoked: true,
..vault
},
);
storage::write_dids(&e, &dids);

storage::write_vaults(&e, &vaults);
}

fn register_did(e: Env, admin: Address, did: String) {
validate_admin(&e, admin);
let mut dids = storage::read_dids(&e);
fn get_vault(e: Env, did: String) -> Vault {
let vaults = storage::read_vaults(&e);

if did::is_registered(&dids, &did) {
panic_with_error!(e, ContractError::DuplicatedDID)
match vaults.get(did) {
Some(vault) => vault,
None => panic_with_error!(&e, ContractError::VaultNotFound),
}
}

dids.set(
did.clone(),
Did {
did,
is_revoked: false,
vcs: Vec::new(&e),
},
);
storage::write_dids(&e, &dids)
fn list_vaults(e: Env) -> Vec<Vault> {
let vaults = storage::read_vaults(&e);
vaults.values()
}
}

Expand All @@ -158,14 +141,13 @@ fn validate_admin(e: &Env, admin: Address) {
admin.require_auth();
}

fn validate_did(e: &Env, did: &String) {
let dids = storage::read_dids(e);

if !did::is_registered(&dids, did) {
panic_with_error!(e, ContractError::DidNotFound)
fn validate_vault(e: &Env, vaults: &Map<String, Vault>, did: &String) {
if !vault::is_registered(vaults, did) {
panic_with_error!(e, ContractError::VaultNotFound)
}
if did::is_revoked(&dids, did) {
panic_with_error!(e, ContractError::DidRevoked)

if vault::is_revoked(vaults, did) {
panic_with_error!(e, ContractError::VaultRevoked)
}
}

Expand All @@ -174,7 +156,7 @@ fn validate_issuer(
issuer: &Address,
did: &String,
vc_data: &String,
issuance_contract_address: &Address,
issuance_contract: &Address,
) {
let issuers: Map<Address, Issuer> = storage::read_issuers(e, did);

Expand All @@ -190,7 +172,7 @@ fn validate_issuer(
vc_data.clone(),
did.clone(),
issuer.clone(),
issuance_contract_address.clone(),
issuance_contract.clone(),
)
.into_val(e),
);
Expand Down
49 changes: 0 additions & 49 deletions vault_contract/src/did.rs

This file was deleted.

9 changes: 4 additions & 5 deletions vault_contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ pub enum ContractError {
NotAuthorized = 2,
EmptyDIDs = 3,
IssuerNotFound = 4,
DidRevoked = 5,
DidNotFound = 6,
IssuerRevoked = 7,
VCNotFound = 8,
DuplicatedDID = 9,
IssuerRevoked = 5,
VaultNotFound = 6,
VaultRevoked = 7,
VaultAlreadyRegistered = 8,
}
Loading

0 comments on commit 74b5fb4

Please sign in to comment.