Skip to content

Commit

Permalink
Merge pull request #205 from kommitters/v0.20
Browse files Browse the repository at this point in the history
Release v0.20.0
  • Loading branch information
juliandsg0222 authored May 8, 2024
2 parents 6839223 + adcb0fa commit 3c47194
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 291 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
version: 0.22.0
# Due to a bug with cargo-tarpaulin crate with multi-line chain calls, the test coverage was reduced.
# https://github.com/xd009642/tarpaulin/issues/949
args: --all-features --fail-under 95 --out Lcov
args: --all-features --fail-under 90 --out Lcov
- name: Upload to Coveralls
uses: coverallsapp/github-action@3dfc5567390f6fa9267c0ee9c251e4c8c3f18949 # v2.2.3
with:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.20.0 (08.05.2024)
- Remove VCs amount limit. See [PR #200](https://github.com/kommitters/chaincerts-smart-contracts/pull/200)
- Migrate VCs storage in old contracts. See [PR #203](https://github.com/kommitters/chaincerts-smart-contracts/pull/203)

## 0.19.1 (25.04.2024)
- Add stale issues policy. See [PR #196](https://github.com/kommitters/chaincerts-smart-contracts/pull/196)

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"
members = ["deployer_contract", "did_contract", "vault_contract", "vc_issuance_contract"]

[workspace.package]
version = "0.19.1"
version = "0.20.0"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/kommitters/chaincerts-smart-contracts"
Expand Down
Binary file removed deployer_contract/did_contract.wasm
Binary file not shown.
2 changes: 0 additions & 2 deletions deployer_contract/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ fn test_deploy_from_address() {
fn vc_issuance_init_args(env: &Env) -> Vec<Val> {
let admin = Address::generate(env);
let issuer_did = String::from_str(env, "did:chaincerts:3mtjfbxad3wzh7qa4w5f7q4h");
let amount: Option<u32> = Some(10);
vec![
env,
Val::from_val(env, &admin),
Val::from_val(env, &issuer_did),
Val::from_val(env, &amount),
]
}
31 changes: 6 additions & 25 deletions vault_contract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ With this smart contract, you will be able to:
- Revoke an issuer for a specific vault.
- Store a verifiable credential in the recipient's vault.
- Revoke the vault.
- Retrieve the list of stored vcs in the vault.
- Migrate the VCs key for contracts older than version v0.20.0.
- Set the contract admin.
- Upgrade the contract.
- Get the contract version.
Expand All @@ -28,7 +28,7 @@ Represents a digitally signed statement made by an issuer about a DID subject.
| `id` | `String` | Unique identifier (e.g., `t5iwuct2njbbcdu2nfwr32ib`). |
| `data` | `String` | VC data encrypted utilizing a key agreement algorithm for heightened security. |
| `issuance_contract` | `Address` | Smart contract address responsible for verifiable credential issuance. |
| `issuer_did` | `String` | DID of the verifiable credential issuer. |
| `issuer_did` | `String` | DID of the verifiable credential issuer. |

#### Example

Expand Down Expand Up @@ -219,16 +219,13 @@ soroban contract invoke \
revoke_vault
```

### Get VCs
Retrieve the list of stored vcs in the vault.
### Migrate VCs
Migrates the VCs from being stored in a single vector to multiple vectors.

```rust
fn get_vcs(e: Env) -> Vec<VerifiableCredential>;
fn migrate(e: Env);
```

#### Output
Returns a list of vcs.

#### Example

```bash
Expand All @@ -238,23 +235,7 @@ soroban contract invoke \
--rpc-url https://soroban-testnet.stellar.org:443 \
--network-passphrase 'Test SDF Network ; September 2015' \
-- \
get_vcs

# Output: VCs
[
{
"id": "t5iwuct2njbbcdu2nfwr32ib",
"data": "gzLDVsdtPc6w8tOhyiaftVPu9gI8J+/8UKlIAmTVNkiV0QAAfahvqhgMY2ZNLHnksFA15XiLDiXb6Yam39rcif94XrsVnXZ7UKuhOFqgMew",
"issuance_contract": "CBCA3EDJOEHHVH3X2RGWQNUDWVHP2JZHFYVGSSCDWD3RI3IUYY4FKLD4",
"issuer_did": "did:chaincerts:5ppl9sm47frl0tpj7g3lp6eo"
},
{
"id": "wqzrxs3eq2v90i5un1ph7k8l",
"data": "Pc1hVUB2Mz8jXw9rEk7NxF4Lg5vmB3rYscAItJfRqiD0dVxkpwZqXlO2eau7YcDIoZaVlqSRF7sQ1B2YnmfIY",
"issuance_contract": "CBRM3HA7GLEI6QQ3O55RUKVRDSQASARUPKK6NXKXKKPWEYLE533GDYQD",
"issuer_did": "did:chaincerts:pe4t2r94dftr1n1gf6jikt6a"
}
]
migrate
```

### Set contract admin
Expand Down
34 changes: 25 additions & 9 deletions vault_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::issuer;
use crate::storage;
use crate::vault_trait::VaultTrait;
use crate::verifiable_credential;
use crate::verifiable_credential::VerifiableCredential;
use soroban_sdk::{
contract, contractimpl, contractmeta, panic_with_error, Address, BytesN, Env, IntoVal, String,
Symbol, Val, Vec,
Expand All @@ -17,6 +16,7 @@ contractmeta!(
val = "Smart contract for Chaincerts Vault",
);

#[allow(dead_code)]
#[contract]
pub struct VaultContract;

Expand All @@ -38,9 +38,9 @@ impl VaultTrait for VaultContract {

storage::write_admin(&e, &admin);
storage::write_did(&e, &did_uri);
storage::write_did_contract(&e, &did_contract_address);
storage::write_revoked(&e, &false);
storage::write_issuers(&e, &Vec::new(&e));
storage::write_vcs(&e, &Vec::new(&e));

(did_contract_address, did_document.into_val(&e))
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl VaultTrait for VaultContract {
issuance_contract: Address,
) {
validate_vault_revoked(&e);
validate_issuer(&e, &issuer, &vc_data, &issuance_contract);
validate_issuer(&e, &issuer);

verifiable_credential::store_vc(&e, vc_id, vc_data, issuance_contract, issuer_did);
}
Expand All @@ -87,8 +87,26 @@ impl VaultTrait for VaultContract {
storage::write_revoked(&e, &true);
}

fn get_vcs(e: Env) -> Vec<VerifiableCredential> {
storage::read_vcs(&e)
fn migrate(e: Env) {
validate_admin(&e);

let vcs = storage::read_old_vcs(&e);

if vcs.is_none() {
panic_with_error!(e, ContractError::VCSAlreadyMigrated)
}

for vc in vcs.unwrap().iter() {
verifiable_credential::store_vc(
&e,
vc.id.clone(),
vc.data.clone(),
vc.issuance_contract.clone(),
vc.issuer_did.clone(),
);
}

storage::remove_old_vcs(&e);
}

fn set_admin(e: Env, new_admin: Address) {
Expand All @@ -114,16 +132,14 @@ fn validate_admin(e: &Env) {
contract_admin.require_auth();
}

fn validate_issuer(e: &Env, issuer: &Address, vc_data: &String, issuance_contract: &Address) {
fn validate_issuer(e: &Env, issuer: &Address) {
let issuers: Vec<Address> = storage::read_issuers(e);

if !issuer::is_authorized(&issuers, issuer) {
panic_with_error!(e, ContractError::IssuerNotAuthorized)
}

issuer.require_auth_for_args(
(vc_data.clone(), issuer.clone(), issuance_contract.clone()).into_val(e),
);
issuer.require_auth();
}

fn validate_vault_revoked(e: &Env) {
Expand Down
1 change: 1 addition & 0 deletions vault_contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pub enum ContractError {
IssuerNotAuthorized = 2,
IssuerAlreadyAuthorized = 3,
VaultRevoked = 4,
VCSAlreadyMigrated = 5,
}
30 changes: 21 additions & 9 deletions vault_contract/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use soroban_sdk::{contracttype, Address, Env, String, Vec};
#[derive(Clone)]
#[contracttype]
pub enum DataKey {
Admin, // Address
Did, // String
Revoked, // Boolean
Issuers, // Vec<Address>
VCs, // Vec<VerifiableCredential>
Admin, // Address
Did, // String
DidContract, // Address
Revoked, // Boolean
Issuers, // Vec<Address>
VC(String), // VerifiableCredential
VCs, // Vec<VerifiableCredential>
}

pub fn has_admin(e: &Env) -> bool {
Expand All @@ -31,6 +33,11 @@ pub fn write_did(e: &Env, did: &String) {
e.storage().instance().set(&key, did);
}

pub fn write_did_contract(e: &Env, did_contract: &Address) {
let key = DataKey::DidContract;
e.storage().instance().set(&key, did_contract);
}

pub fn read_revoked(e: &Env) -> bool {
let key = DataKey::Revoked;
e.storage().instance().get(&key).unwrap()
Expand All @@ -51,12 +58,17 @@ pub fn write_issuers(e: &Env, issuers: &Vec<Address>) {
e.storage().persistent().set(&key, issuers)
}

pub fn read_vcs(e: &Env) -> Vec<VerifiableCredential> {
pub fn write_vc(e: &Env, vc_id: &String, vc: &VerifiableCredential) {
let key = DataKey::VC(vc_id.clone());
e.storage().persistent().set(&key, vc)
}

pub fn read_old_vcs(e: &Env) -> Option<Vec<VerifiableCredential>> {
let key = DataKey::VCs;
e.storage().persistent().get(&key).unwrap()
e.storage().persistent().get(&key)
}

pub fn write_vcs(e: &Env, vcs: &Vec<VerifiableCredential>) {
pub fn remove_old_vcs(e: &Env) {
let key = DataKey::VCs;
e.storage().persistent().set(&key, vcs)
e.storage().persistent().remove(&key);
}
52 changes: 5 additions & 47 deletions vault_contract/src/test/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::setup::{did_context, get_vc_setup, VCVaultContractTest};
use crate::did_contract;
use crate::test::setup::VaultContractTest;
use crate::verifiable_credential::VerifiableCredential;
use soroban_sdk::{testutils::Address as _, vec, Address, String};

#[test]
Expand Down Expand Up @@ -332,61 +331,20 @@ fn test_revoke_vault() {
}

#[test]
fn test_get_vcs() {
#[should_panic(expected = "HostError: Error(Contract, #5)")]
fn test_migrate_should_fail_without_vcs() {
let VaultContractTest {
env,
env: _,
admin,
issuer,
issuer: _,
did_init_args,
did_wasm_hash,
salt,
contract,
} = VaultContractTest::setup();

let VCVaultContractTest {
vc_id,
vc_data,
issuance_contract_address,
issuer_did,
} = get_vc_setup(&env);

let vc_id_2 = String::from_str(&env, "vc_id2");

let vc_1 = VerifiableCredential {
id: vc_id.clone(),
data: vc_data.clone(),
issuance_contract: issuance_contract_address.clone(),
issuer_did: issuer_did.clone(),
};

let vc_2 = VerifiableCredential {
id: vc_id_2.clone(),
data: vc_data.clone(),
issuance_contract: issuance_contract_address.clone(),
issuer_did: issuer_did.clone(),
};

contract.initialize(&admin, &did_wasm_hash, &did_init_args, &salt);
contract.authorize_issuer(&issuer);
contract.store_vc(
&vc_id,
&vc_data,
&issuer,
&issuer_did,
&issuance_contract_address,
);
contract.store_vc(
&vc_id_2,
&vc_data,
&issuer,
&issuer_did,
&issuance_contract_address,
);
let vcs = contract.get_vcs();

assert_eq!(vcs.len(), 2);
assert_eq!(vcs.get_unchecked(1), vc_1);
assert_eq!(vcs.get_unchecked(0), vc_2);
contract.migrate();
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions vault_contract/src/vault_trait.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::verifiable_credential::VerifiableCredential;
use soroban_sdk::{Address, BytesN, Env, String, Val, Vec};

#[allow(dead_code)]
pub trait VaultTrait {
/// Initializes the vault contract by setting the admin and deploying the DID.
fn initialize(
Expand Down Expand Up @@ -33,8 +33,8 @@ pub trait VaultTrait {
/// Revokes the vault.
fn revoke_vault(e: Env);

/// Retrieves the vcs.
fn get_vcs(e: Env) -> Vec<VerifiableCredential>;
/// Migrates the VCs from being stored in a single vector to multiple vectors.
fn migrate(e: Env);

/// Sets the new contract admin.
fn set_admin(e: Env, new_admin: Address);
Expand Down
8 changes: 3 additions & 5 deletions vault_contract/src/verifiable_credential.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::storage;
use soroban_sdk::{contracttype, Address, Env, String, Vec};
use soroban_sdk::{contracttype, Address, Env, String};

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -11,14 +11,12 @@ pub struct VerifiableCredential {
}

pub fn store_vc(e: &Env, id: String, data: String, issuance_contract: Address, issuer_did: String) {
let mut vcs: Vec<VerifiableCredential> = storage::read_vcs(e);
let new_vc: VerifiableCredential = VerifiableCredential {
id,
id: id.clone(),
data,
issuance_contract,
issuer_did,
};

vcs.push_front(new_vc);
storage::write_vcs(e, &vcs);
storage::write_vc(e, &id, &new_vc);
}
Loading

0 comments on commit 3c47194

Please sign in to comment.