|
| 1 | +// Copyright 2020-2025 IOTA Stiftung |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::time::{SystemTime, UNIX_EPOCH}; |
| 5 | + |
| 6 | +use anyhow::Result; |
| 7 | +use examples::get_funded_client; |
| 8 | +use notarization::core::state::State; |
| 9 | +use notarization::core::timelock::TimeLock; |
| 10 | + |
| 11 | +#[tokio::main] |
| 12 | +async fn main() -> Result<()> { |
| 13 | + println!("Demonstrating notarization destruction scenarios"); |
| 14 | + |
| 15 | + // Create a notarization client |
| 16 | + let notarization_client = get_funded_client().await?; |
| 17 | + |
| 18 | + // Scenario 1: Destroy an unlocked dynamic notarization (should succeed) |
| 19 | + println!("📝 Scenario 1: Creating and destroying an unlocked dynamic notarization..."); |
| 20 | + |
| 21 | + let unlocked_dynamic_id = notarization_client |
| 22 | + .create_dynamic_notarization() |
| 23 | + .with_state(State::from_string("Unlocked content".to_string(), None)) |
| 24 | + .with_immutable_description("Unlocked dynamic document".to_string()) |
| 25 | + .finish() |
| 26 | + .build_and_execute(¬arization_client) |
| 27 | + .await? |
| 28 | + .output |
| 29 | + .id; |
| 30 | + |
| 31 | + println!("✅ Created unlocked dynamic notarization: {:?}", unlocked_dynamic_id); |
| 32 | + |
| 33 | + // Check if destroy is allowed |
| 34 | + let is_destroy_allowed = notarization_client |
| 35 | + .is_destroy_allowed(*unlocked_dynamic_id.object_id()) |
| 36 | + .await?; |
| 37 | + |
| 38 | + println!("🔍 Destroy allowed: {}", is_destroy_allowed); |
| 39 | + |
| 40 | + // Destroy the unlocked notarization |
| 41 | + let destroy_result = notarization_client |
| 42 | + .destroy(*unlocked_dynamic_id.object_id()) |
| 43 | + .build_and_execute(¬arization_client) |
| 44 | + .await; |
| 45 | + |
| 46 | + match destroy_result { |
| 47 | + Ok(_) => println!("✅ Successfully destroyed unlocked dynamic notarization"), |
| 48 | + Err(e) => println!("❌ Failed to destroy: {}", e), |
| 49 | + } |
| 50 | + |
| 51 | + // Scenario 2: Try to destroy a transfer-locked dynamic notarization (should fail) |
| 52 | + println!("\n📝 Scenario 2: Creating a transfer-locked dynamic notarization..."); |
| 53 | + |
| 54 | + let now_ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); |
| 55 | + let unlock_at = now_ts + 86400; // 24 hours |
| 56 | + |
| 57 | + let transfer_locked_id = notarization_client |
| 58 | + .create_dynamic_notarization() |
| 59 | + .with_state(State::from_string("Transfer-locked content".to_string(), None)) |
| 60 | + .with_immutable_description("Transfer-locked document".to_string()) |
| 61 | + .with_transfer_lock(TimeLock::UnlockAt(unlock_at as u32)) |
| 62 | + .finish() |
| 63 | + .build_and_execute(¬arization_client) |
| 64 | + .await? |
| 65 | + .output |
| 66 | + .id; |
| 67 | + |
| 68 | + println!( |
| 69 | + "✅ Created transfer-locked dynamic notarization: {:?}", |
| 70 | + transfer_locked_id |
| 71 | + ); |
| 72 | + |
| 73 | + let is_destroy_allowed = notarization_client |
| 74 | + .is_destroy_allowed(*transfer_locked_id.object_id()) |
| 75 | + .await?; |
| 76 | + |
| 77 | + println!("🔍 Destroy allowed: {}", is_destroy_allowed); |
| 78 | + |
| 79 | + // Try to destroy the transfer-locked notarization |
| 80 | + let destroy_result = notarization_client |
| 81 | + .destroy(*transfer_locked_id.object_id()) |
| 82 | + .build_and_execute(¬arization_client) |
| 83 | + .await; |
| 84 | + |
| 85 | + match destroy_result { |
| 86 | + Ok(_) => println!("❌ Unexpected: Destruction succeeded (should have failed)"), |
| 87 | + Err(e) => println!("✅ Expected: Destruction failed - {}", e), |
| 88 | + } |
| 89 | + |
| 90 | + // Scenario 3: Create and try to destroy a time-locked locked notarization (should fail) |
| 91 | + println!("\n📝 Scenario 3: Creating a time-locked locked notarization..."); |
| 92 | + |
| 93 | + let delete_locked_id = notarization_client |
| 94 | + .create_locked_notarization() |
| 95 | + .with_state(State::from_string("Delete-locked content".to_string(), None)) |
| 96 | + .with_immutable_description("Delete-locked document".to_string()) |
| 97 | + .with_delete_at(TimeLock::UnlockAt(unlock_at as u32)) |
| 98 | + .finish()? |
| 99 | + .build_and_execute(¬arization_client) |
| 100 | + .await? |
| 101 | + .output |
| 102 | + .id; |
| 103 | + |
| 104 | + println!("✅ Created delete-locked locked notarization: {:?}", delete_locked_id); |
| 105 | + |
| 106 | + let is_destroy_allowed = notarization_client |
| 107 | + .is_destroy_allowed(*delete_locked_id.object_id()) |
| 108 | + .await?; |
| 109 | + |
| 110 | + println!("🔍 Destroy allowed: {}", is_destroy_allowed); |
| 111 | + |
| 112 | + // Try to destroy the delete-locked notarization |
| 113 | + let destroy_result = notarization_client |
| 114 | + .destroy(*delete_locked_id.object_id()) |
| 115 | + .build_and_execute(¬arization_client) |
| 116 | + .await; |
| 117 | + |
| 118 | + match destroy_result { |
| 119 | + Ok(_) => println!("❌ Unexpected: Destruction succeeded (should have failed)"), |
| 120 | + Err(e) => println!("✅ Expected: Destruction failed - {}", e), |
| 121 | + } |
| 122 | + |
| 123 | + // Scenario 4: Create and destroy a locked notarization with no delete lock (should succeed) |
| 124 | + println!("\n📝 Scenario 4: Creating a locked notarization with no delete lock..."); |
| 125 | + |
| 126 | + let no_delete_lock_id = notarization_client |
| 127 | + .create_locked_notarization() |
| 128 | + .with_state(State::from_string("No delete lock content".to_string(), None)) |
| 129 | + .with_immutable_description("No delete lock document".to_string()) |
| 130 | + .with_delete_at(TimeLock::None) |
| 131 | + .finish()? |
| 132 | + .build_and_execute(¬arization_client) |
| 133 | + .await? |
| 134 | + .output |
| 135 | + .id; |
| 136 | + |
| 137 | + println!( |
| 138 | + "✅ Created locked notarization with no delete lock: {:?}", |
| 139 | + no_delete_lock_id |
| 140 | + ); |
| 141 | + |
| 142 | + let is_destroy_allowed = notarization_client |
| 143 | + .is_destroy_allowed(*no_delete_lock_id.object_id()) |
| 144 | + .await?; |
| 145 | + |
| 146 | + println!("🔍 Destroy allowed: {}", is_destroy_allowed); |
| 147 | + |
| 148 | + // Destroy the notarization with no delete lock |
| 149 | + let destroy_result = notarization_client |
| 150 | + .destroy(*no_delete_lock_id.object_id()) |
| 151 | + .build_and_execute(¬arization_client) |
| 152 | + .await; |
| 153 | + |
| 154 | + match destroy_result { |
| 155 | + Ok(_) => println!("✅ Successfully destroyed locked notarization with no delete lock"), |
| 156 | + Err(e) => println!("❌ Failed to destroy: {}", e), |
| 157 | + } |
| 158 | + |
| 159 | + println!("\n📋 Summary:"); |
| 160 | + println!("🔓 Unlocked notarizations can be destroyed immediately"); |
| 161 | + println!("🔒 Transfer-locked dynamic notarizations cannot be destroyed"); |
| 162 | + println!("⏰ Time-locked locked notarizations cannot be destroyed before lock expires"); |
| 163 | + println!("🆓 Locked notarizations with TimeLock::None can be destroyed"); |
| 164 | + |
| 165 | + Ok(()) |
| 166 | +} |
0 commit comments