From 31824ff79e09fe8c9cebf80ad951b100da31b100 Mon Sep 17 00:00:00 2001 From: CristhianRodriguezMolina <50219561+CristhianRodriguezMolina@users.noreply.github.com> Date: Wed, 17 Apr 2024 11:34:51 -0500 Subject: [PATCH 1/2] Set new contract admin function (#38) * Set new admin * Fix format * Solved comment * Added improvement * Added verification methods to be updated when setting admin * Updated readme * Fixed readme --- README.md | 24 ++++++++++++++++++++ src/contract.rs | 33 +++++++++++++++++++++++---- src/did_trait.rs | 7 ++++++ src/test/contract.rs | 53 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 112 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e193198..e60b01b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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>); +``` + +#### 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. diff --git a/src/contract.rs b/src/contract.rs index a1b30f1..470677b 100644 --- a/src/contract.rs +++ b/src/contract.rs @@ -50,8 +50,7 @@ impl DIDTrait for DIDContract { verification_methods: Option>, services: Option>, ) -> DIDDocument { - let contract_admin = storage::read_admin(&e); - contract_admin.require_auth(); + validate_admin(&e); let mut did_document = storage::read_did_document(&e); @@ -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>, + ) -> 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); } @@ -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(); +} diff --git a/src/did_trait.rs b/src/did_trait.rs index 45ab158..2f982fc 100644 --- a/src/did_trait.rs +++ b/src/did_trait.rs @@ -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>, + ) -> DIDDocument; + /// Upgrades WASM code. fn upgrade(e: Env, new_wasm_hash: BytesN<32>); diff --git a/src/test/contract.rs b/src/test/contract.rs index 1f5a554..624719c 100644 --- a/src/test/contract.rs +++ b/src/test/contract.rs @@ -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; @@ -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 { From 23d016189c1debbffda2689bc2a66094d74671a4 Mon Sep 17 00:00:00 2001 From: CristhianRodriguezMolina <50219561+CristhianRodriguezMolina@users.noreply.github.com> Date: Wed, 17 Apr 2024 11:51:29 -0500 Subject: [PATCH 2/2] Prepare release v0.6.0 (#39) --- CHANGELOG.md | 3 +++ Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5f7ca2..62f4e0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Cargo.toml b/Cargo.toml index ff7dcb2..c2f6d76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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