Skip to content

Commit

Permalink
Merge pull request #40 from kommitters/v0.6
Browse files Browse the repository at this point in the history
Release v0.6.0
  • Loading branch information
CristhianRodriguezMolina authored Apr 17, 2024
2 parents 95dbd93 + 23d0161 commit 39988d9
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.6.0 (17.04.2024)
- Feature: [Set new admin function](https://github.com/kommitters/soroban-did-contract/issues/37).

## 0.5.0 (21.03.2024)
- Feature: [Upgradeable contract](https://github.com/kommitters/soroban-did-contract/issues/32).
- [Remove `admin` from `update_did` function](https://github.com/kommitters/soroban-did-contract/pull/34)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "soroban-did-contract"
version = "0.5.0"
version = "0.6.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The DID contract enables you to manage a Decentralized Identifier within the Sor
- Create a DID.
- Update the DID attributes.
- Retrieve the DID document.
- Set the contract admin.
- Upgrade the contract.
- Get the contract version.

Expand Down Expand Up @@ -385,6 +386,29 @@ soroban contract invoke \
}
```

### Set contract admin
Replaces the current contract admin with a new one. Also, you have the flexibility to update the `VerificationMethods`, if it is not intended to be updated, simply pass `None`.

Verification Methods must not be empty; otherwise, a contract error will be thrown.

```rust
fn set_admin(e: Env, new_admin: Address, new_verification_methods: Option<Vec<VerificationMethodEntry>>);
```

#### Example

```bash
soroban contract invoke \
--id CONTRACT_ID \
--source SOURCE_ACCOUNT_SECRET_KEY \
--rpc-url https://soroban-testnet.stellar.org:443 \
--network-passphrase 'Test SDF Network ; September 2015' \
-- \
set_admin \
--new_admin GCWZBFEKWUGQKYLCLI5ULI4DTXLEA7LPC5QVB55NZPC7FY2NGMLP4YMC \
--new_verification_methods '[{"id": "keys-1", "type_": "Ed25519VerificationKey2020", "controller": "", "public_key_multibase": "z6MkgpAN9rsVPXJ6DrrvxcsGzKwjdkVdvjNtbQsRiLfsqmuQ", "verification_relationships": ["Authentication", "AssertionMethod"]}]'
```

### Upgrade contract
Replaces the current contract code with a new one.

Expand Down
33 changes: 29 additions & 4 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ impl DIDTrait for DIDContract {
verification_methods: Option<Vec<VerificationMethodEntry>>,
services: Option<Vec<Service>>,
) -> DIDDocument {
let contract_admin = storage::read_admin(&e);
contract_admin.require_auth();
validate_admin(&e);

let mut did_document = storage::read_did_document(&e);

Expand All @@ -70,9 +69,30 @@ impl DIDTrait for DIDContract {
storage::read_did_document(&e)
}

fn set_admin(
e: Env,
new_admin: Address,
new_verification_methods: Option<Vec<VerificationMethodEntry>>,
) -> DIDDocument {
validate_admin(&e);

let mut did_document = storage::read_did_document(&e);

did_document::update_did_document(
&e,
&Option::None,
&new_verification_methods,
&Option::None,
&mut did_document,
);

storage::write_admin(&e, &new_admin);

did_document
}

fn upgrade(e: Env, new_wasm_hash: BytesN<32>) {
let admin = storage::read_admin(&e);
admin.require_auth();
validate_admin(&e);

e.deployer().update_current_contract_wasm(new_wasm_hash);
}
Expand All @@ -81,3 +101,8 @@ impl DIDTrait for DIDContract {
String::from_str(&e, VERSION)
}
}

fn validate_admin(e: &Env) {
let contract_admin = storage::read_admin(e);
contract_admin.require_auth();
}
7 changes: 7 additions & 0 deletions src/did_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub trait DIDTrait {
/// Returns the DID Document.
fn get_did(e: Env) -> DIDDocument;

/// Sets the new contract admin and updates the verification methods if provided.
fn set_admin(
e: Env,
new_admin: Address,
new_verification_methods: Option<Vec<VerificationMethodEntry>>,
) -> DIDDocument;

/// Upgrades WASM code.
fn upgrade(e: Env, new_wasm_hash: BytesN<32>);

Expand Down
53 changes: 52 additions & 1 deletion src/test/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::verification_method::{
format_verification_method, VerificationMethodEntry, VerificationMethodType,
VerificationRelationship,
};
use soroban_sdk::{vec, String, Vec};
use soroban_sdk::{testutils::Address as _, vec, Address, String, Vec};

// Length of the Method Specific ID (MSI) encoded in Base32
const ENCODED_MSI_LEN: usize = 24;
Expand Down Expand Up @@ -316,6 +316,57 @@ fn test_update_services() {
assert_eq!(did_document.service, new_services)
}

#[test]
fn test_set_admin() {
let DIDContractTest {
env,
admin,
did_method,
context,
verification_methods,
services,
contract,
} = DIDContractTest::setup();

let did_document = contract.initialize(
&admin,
&did_method,
&context,
&verification_methods,
&services,
);

let new_admin = Address::generate(&env);

let new_verification_methods = vec![
&env,
VerificationMethodEntry {
id: String::from_str(&env, "keys-1"),
type_: VerificationMethodType::Ed25519VerificationKey2020,
controller: String::from_str(&env, ""),
public_key_multibase: String::from_str(
&env,
"z6MkgpAN9rsVPXJ6DrrvxcsGzKwjdkVdvjNtbQsRiLfsqmuQ",
),
verification_relationships: vec![
&env,
VerificationRelationship::Authentication,
VerificationRelationship::AssertionMethod,
],
},
];

contract.set_admin(&new_admin, &Option::Some(new_verification_methods.clone()));

let formatted_verification_methods =
format_verification_method(&env, &new_verification_methods, &did_document.id);

assert_eq!(
formatted_verification_methods,
contract.get_did().verification_method
)
}

#[test]
fn test_version() {
let DIDContractTest {
Expand Down

0 comments on commit 39988d9

Please sign in to comment.