Skip to content

Commit

Permalink
fix identity creation lint
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed Dec 2, 2024
1 parent 395a950 commit 9c314c4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 74 deletions.
70 changes: 38 additions & 32 deletions identity_iota_core/packages/iota_identity/sources/identity.move
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ module iota_identity::identity {
version: u64,
}

/// Creates a new DID Document with a single controller.
/// Creates a [`Identity`] with a single controller.
public fun new(
doc: vector<u8>,
clock: &Clock,
ctx: &mut TxContext
): Identity {
): ID {
new_with_controller(doc, ctx.sender(), false, clock, ctx)
}

Expand All @@ -79,12 +79,20 @@ module iota_identity::identity {
creation_timestamp: u64,
clock: &Clock,
ctx: &mut TxContext
): Identity {
let mut identity = new_with_controller(doc, ctx.sender(), false, clock, ctx);
assert!(identity.updated >= creation_timestamp, EInvalidTimestamp);
identity.created = creation_timestamp;
): ID {
let now = clock.timestamp_ms();
assert!(now >= creation_timestamp, EInvalidTimestamp);
let identity = Identity {
id: object::new(ctx),
did_doc: multicontroller::new_with_controller(doc, ctx.sender(), false, ctx),
created: creation_timestamp,
updated: now,
version: PACKAGE_VERSION,
};
let id = object::id(&identity);
transfer::share_object(identity);

identity
id
}

/// Creates a new `Identity` wrapping DID DOC `doc` and controller by
Expand All @@ -95,15 +103,19 @@ module iota_identity::identity {
can_delegate: bool,
clock: &Clock,
ctx: &mut TxContext,
): Identity {
): ID {
let now = clock.timestamp_ms();
Identity {
let identity = Identity {
id: object::new(ctx),
did_doc: multicontroller::new_with_controller(doc, controller, can_delegate, ctx),
created: now,
updated: now,
version: PACKAGE_VERSION,
}
};
let id = object::id(&identity);
transfer::share_object(identity);

id
}

/// Creates a new DID Document controlled by multiple controllers.
Expand All @@ -116,19 +128,23 @@ module iota_identity::identity {
threshold: u64,
clock: &Clock,
ctx: &mut TxContext,
): Identity {
): ID {
assert!(is_did_output(&doc), ENotADidDocument);
assert!(threshold >= 1, EInvalidThreshold);
assert!(controllers.size() > 0, EInvalidControllersList);

let now = clock.timestamp_ms();
Identity {
let identity = Identity {
id: object::new(ctx),
did_doc: multicontroller::new_with_controllers(doc, controllers, controllers_that_can_delegate, threshold, ctx),
created: now,
updated: now,
version: PACKAGE_VERSION,
}
};
let id = object::id(&identity);

transfer::share_object(identity);
id
}

/// Returns a reference to the `UID` of an `Identity`.
Expand Down Expand Up @@ -564,8 +580,7 @@ module iota_identity::identity_tests {

// Create a DID document with no funds and 1 controller with a weight of 1 and a threshold of 1.
// Share the document and send the controller capability to `controller1`.
let identity = new(b"DID", &clock, scenario.ctx());
transfer::public_share_object(identity);
let _identity_id = new(b"DID", &clock, scenario.ctx());

scenario.next_tx(controller1);

Expand Down Expand Up @@ -607,15 +622,14 @@ module iota_identity::identity_tests {
controllers.insert(controller3, 1);

// Create an identity shared by `controller1`, `controller2`, `controller3`.
let identity = new_with_controllers(
let _identity_id = new_with_controllers(
b"DID",
controllers,
vec_map::empty(),
2,
&clock,
scenario.ctx(),
);
transfer::public_share_object(identity);

scenario.next_tx(controller1);

Expand Down Expand Up @@ -682,15 +696,14 @@ module iota_identity::identity_tests {
// === First transaction ===
// Controller A can execute config changes
{
let identity = new_with_controllers(
let _ = new_with_controllers(
b"DID",
controllers,
vec_map::empty(),
10,
&clock,
scenario.ctx(),
);
transfer::public_share_object(identity);
scenario.next_tx(controller_a);

// Controller A alone should be able to do anything.
Expand Down Expand Up @@ -718,15 +731,14 @@ module iota_identity::identity_tests {

// Controller B alone should not be able to make changes.
{
let identity = new_with_controllers(
let _ = new_with_controllers(
b"DID",
controllers,
vec_map::empty(),
10,
&clock,
scenario.ctx(),
);
transfer::public_share_object(identity);
scenario.next_tx(controller_a);

let mut identity = scenario.take_shared<Identity>();
Expand Down Expand Up @@ -770,15 +782,14 @@ module iota_identity::identity_tests {

// === First transaction ===
// Controller B & C can execute config changes
let identity = new_with_controllers(
let _ = new_with_controllers(
b"DID",
controllers,
vec_map::empty(),
10,
&clock,
scenario.ctx(),
);
transfer::public_share_object(identity);
scenario.next_tx(controller_b);

let mut identity = scenario.take_shared<Identity>();
Expand Down Expand Up @@ -820,8 +831,7 @@ module iota_identity::identity_tests {
let mut scenario = test_scenario::begin(controller_a);
let clock = clock::create_for_testing(scenario.ctx());

let first_identity = new(b"DID", &clock, scenario.ctx());
transfer::public_share_object(first_identity);
let _ = new(b"DID", &clock, scenario.ctx());

scenario.next_tx(controller_a);
let first_identity = scenario.take_shared<Identity>();
Expand All @@ -830,7 +840,7 @@ module iota_identity::identity_tests {
controllers.insert(first_identity.to_address(), 10);

// Create a second identity.
let second_identity = new_with_controllers(
let _ = new_with_controllers(
b"DID",
controllers,
vec_map::empty(),
Expand All @@ -839,8 +849,6 @@ module iota_identity::identity_tests {
scenario.ctx(),
);

transfer::public_share_object(second_identity);

scenario.next_tx(first_identity.to_address());
let mut first_identity_cap = scenario.take_from_address<ControllerCap>(first_identity.to_address());
let (token, borrow) = first_identity_cap.borrow();
Expand Down Expand Up @@ -874,8 +882,7 @@ module iota_identity::identity_tests {
let mut scenario = test_scenario::begin(controller);
let clock = clock::create_for_testing(scenario.ctx());

let identity = new(b"DID", &clock, scenario.ctx());
transfer::public_share_object(identity);
let _ = new(b"DID", &clock, scenario.ctx());

scenario.next_tx(controller);

Expand Down Expand Up @@ -907,8 +914,7 @@ module iota_identity::identity_tests {
controllers.insert(controller_a, 1);
controllers.insert(controller_b, 1);

let identity = new_with_controllers(b"DID", controllers, vec_map::empty(), 2, &clock, scenario.ctx());
transfer::public_share_object(identity);
let _ = new_with_controllers(b"DID", controllers, vec_map::empty(), 2, &clock, scenario.ctx());

scenario.next_tx(controller_a);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module iota_identity::migration {

const ENotADidOutput: u64 = 1;

#[allow(lint(share_owned))]
public fun migrate_alias(
alias: Alias,
migration_registry: &mut MigrationRegistry,
Expand All @@ -24,19 +25,17 @@ module iota_identity::migration {
// Check if `state_metadata` contains a DID document.
assert!(state_metadata.is_some() && identity::is_did_output(state_metadata.borrow()), ENotADidOutput);

let identity = identity::new_with_creation_timestamp(
let identity_id = identity::new_with_creation_timestamp(
state_metadata.extract(),
creation_timestamp,
clock,
ctx
);
let identity_addr = identity.id().to_address();

// Add a migration record.
migration_registry.add(alias_id, identity.id().to_inner());
transfer::public_share_object(identity);
migration_registry.add(alias_id, identity_id);

identity_addr
identity_id.to_address()
}

/// Creates a new `Identity` from an Iota 1.0 legacy `AliasOutput` containing a DID Document.
Expand Down
51 changes: 14 additions & 37 deletions identity_iota_core/src/rebased/iota/move_calls/identity/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
use iota_sdk::types::base_types::IotaAddress;
use iota_sdk::types::base_types::ObjectID;
use iota_sdk::types::programmable_transaction_builder::ProgrammableTransactionBuilder;
use iota_sdk::types::transaction::Command;
use iota_sdk::types::transaction::ProgrammableMoveCall;
use iota_sdk::types::transaction::ProgrammableTransaction;
use iota_sdk::types::TypeTag;
use iota_sdk::types::IOTA_FRAMEWORK_PACKAGE_ID;
use move_core_types::ident_str;

use crate::rebased::iota::move_calls::utils;
use crate::rebased::migration::OnChainIdentity;

use crate::rebased::utils::MoveType;
use crate::rebased::Error;

/// Build a transaction that creates a new on-chain Identity containing `did_doc`.
Expand All @@ -24,22 +19,13 @@ pub(crate) fn new(did_doc: &[u8], package_id: ObjectID) -> Result<ProgrammableTr
let clock = utils::get_clock_ref(&mut ptb);

// Create a new identity, sending its capability to the tx's sender.
let identity_res = ptb.command(Command::MoveCall(Box::new(ProgrammableMoveCall {
package: package_id,
module: ident_str!("identity").into(),
function: ident_str!("new").into(),
type_arguments: vec![],
arguments: vec![doc_arg, clock],
})));

// Share the resulting identity.
ptb.command(Command::MoveCall(Box::new(ProgrammableMoveCall {
package: IOTA_FRAMEWORK_PACKAGE_ID,
module: ident_str!("transfer").into(),
function: ident_str!("public_share_object").into(),
type_arguments: vec![OnChainIdentity::move_type(package_id)],
arguments: vec![identity_res],
})));
let _identity_id = ptb.programmable_move_call(
package_id,
ident_str!("identity").into(),
ident_str!("new").into(),
vec![],
vec![doc_arg, clock],
);

Ok(ptb.finish())
}
Expand Down Expand Up @@ -80,28 +66,19 @@ where
let clock = utils::get_clock_ref(&mut ptb);

// Create a new identity, sending its capabilities to the specified controllers.
let identity_res = ptb.command(Command::MoveCall(Box::new(ProgrammableMoveCall {
package: package_id,
module: ident_str!("identity").into(),
function: ident_str!("new_with_controllers").into(),
type_arguments: vec![],
arguments: vec![
let _identity_id = ptb.programmable_move_call(
package_id,
ident_str!("identity").into(),
ident_str!("new_with_controllers").into(),
vec![],
vec![
doc_arg,
controllers,
controllers_that_can_delegate,
threshold_arg,
clock,
],
})));

// Share the resulting identity.
ptb.command(Command::MoveCall(Box::new(ProgrammableMoveCall {
package: IOTA_FRAMEWORK_PACKAGE_ID,
module: ident_str!("transfer").into(),
function: ident_str!("public_share_object").into(),
type_arguments: vec![OnChainIdentity::move_type(package_id)],
arguments: vec![identity_res],
})));
);

Ok(ptb.finish())
}

0 comments on commit 9c314c4

Please sign in to comment.