Skip to content

Commit

Permalink
Set controller, alsoKnownAs fields from Account (#658)
Browse files Browse the repository at this point in the history
* poc `set_controller`

* implement `set_also_known_as`

* format

* add tests for `set_controller` and `also_known_as`

* remote redundant `set_also_known_as(..)`

* fix set controller
  • Loading branch information
abdulmth authored Feb 18, 2022
1 parent c51071a commit 94c7019
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
61 changes: 61 additions & 0 deletions identity-account/src/tests/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use std::sync::Arc;

use identity_core::common::OneOrSet;
use identity_core::common::OrderedSet;
use identity_core::common::Timestamp;
use identity_core::common::Url;
use identity_core::crypto::KeyCollection;
Expand Down Expand Up @@ -698,3 +700,62 @@ async fn test_remove_service() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn test_set_controller() -> Result<()> {
let mut account = Account::create_identity(account_setup(Network::Mainnet).await, IdentitySetup::default()).await?;

let keypair1: KeyPair = KeyPair::new_ed25519().unwrap();
let iota_did1: IotaDID = IotaDID::new(keypair1.public().as_ref()).unwrap();

let keypair2: KeyPair = KeyPair::new_ed25519().unwrap();
let iota_did2: IotaDID = IotaDID::new(keypair2.public().as_ref()).unwrap();

// Set one controller.
let update: Update = Update::SetController {
controllers: Some(OneOrSet::new_one(iota_did1.clone())),
};
account.process_update(update).await.unwrap();
assert_eq!(account.document().controller().unwrap().len(), 1);

// Set two controllers.
let set: OrderedSet<IotaDID> = OrderedSet::from_iter(vec![iota_did1, iota_did2]);
let update: Update = Update::SetController {
controllers: Some(OneOrSet::new_set(set).unwrap()),
};
account.process_update(update).await.unwrap();
assert_eq!(account.document().controller().unwrap().len(), 2);

// Remove all controllers.
let update: Update = Update::SetController { controllers: None };
account.process_update(update).await.unwrap();
assert_eq!(account.document().controller(), None);

Ok(())
}

#[tokio::test]
async fn test_set_also_known_as() -> Result<()> {
let mut account = Account::create_identity(account_setup(Network::Mainnet).await, IdentitySetup::default()).await?;

// No elements by default.
assert_eq!(account.document().also_known_as().len(), 0);

// Set two Urls.
let urls: OrderedSet<Url> = OrderedSet::from_iter(vec![
Url::parse("did:iota:xyz").unwrap(),
Url::parse("did:iota:abc").unwrap(),
]);
let update: Update = Update::SetAlsoKnownAs { urls };
account.process_update(update).await.unwrap();
assert_eq!(account.document().also_known_as().len(), 2);

// Remove all Urls.
let update: Update = Update::SetAlsoKnownAs {
urls: OrderedSet::new(),
};
account.process_update(update).await.unwrap();
assert_eq!(account.document().also_known_as().len(), 0);

Ok(())
}
25 changes: 25 additions & 0 deletions identity-account/src/updates/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use crypto::signatures::ed25519;

use identity_core::common::Fragment;
use identity_core::common::Object;
use identity_core::common::OneOrSet;
use identity_core::common::OrderedSet;
use identity_core::common::Timestamp;
use identity_core::common::Url;
use identity_core::crypto::KeyPair;
use identity_core::crypto::KeyType;
use identity_core::crypto::PublicKey;
Expand Down Expand Up @@ -133,6 +135,12 @@ pub(crate) enum Update {
DeleteService {
fragment: String,
},
SetController {
controllers: Option<OneOrSet<IotaDID>>,
},
SetAlsoKnownAs {
urls: OrderedSet<Url>,
},
}

impl Update {
Expand Down Expand Up @@ -271,6 +279,13 @@ impl Update {

state.document_mut().remove_service(&service_url)?;
}
Self::SetController { controllers } => {
*state.document_mut().controller_mut() = controllers;
}

Self::SetAlsoKnownAs { urls } => {
*state.document_mut().also_known_as_mut() = urls;
}
}

state.document_mut().metadata.updated = Timestamp::now_utc();
Expand Down Expand Up @@ -408,3 +423,13 @@ impl_update_builder!(
DeleteService {
@required fragment String,
});

impl_update_builder!(
SetController {
@required controllers Option<OneOrSet<IotaDID>>,
});

impl_update_builder!(
SetAlsoKnownAs {
@required urls OrderedSet<Url>,
});

0 comments on commit 94c7019

Please sign in to comment.