Skip to content

Commit b63c49a

Browse files
committed
Refactor notarization examples and update scripts
1 parent 9874486 commit b63c49a

File tree

11 files changed

+129
-142
lines changed

11 files changed

+129
-142
lines changed

examples/01_create_locked_notarization.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::time::{SystemTime, UNIX_EPOCH};
55

66
use anyhow::Result;
77
use examples::get_funded_client;
8-
98
use notarization::core::state::State;
109
use notarization::core::timelock::TimeLock;
1110
use notarization::core::NotarizationMethod;

examples/02_create_dynamic_notarization.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
use std::time::{SystemTime, UNIX_EPOCH};
55

66
use anyhow::Result;
7+
use examples::get_funded_client;
78
use notarization::core::state::State;
89
use notarization::core::timelock::TimeLock;
910
use notarization::core::NotarizationMethod;
10-
use examples::get_funded_client;
1111

1212
#[tokio::main]
1313
async fn main() -> Result<()> {
@@ -44,10 +44,7 @@ async fn main() -> Result<()> {
4444
// Create a dynamic notarization with transfer lock
4545
let locked_transfer_notarization = notarization_client
4646
.create_dynamic_notarization()
47-
.with_state(State::from_string(
48-
"Transfer-locked content".to_string(),
49-
None,
50-
))
47+
.with_state(State::from_string("Transfer-locked content".to_string(), None))
5148
.with_immutable_description("Transfer-locked document".to_string())
5249
.with_transfer_lock(TimeLock::UnlockAt(unlock_at as u32))
5350
.finish()
@@ -74,4 +71,4 @@ async fn main() -> Result<()> {
7471
println!("📝 State and metadata can be modified over time");
7572

7673
Ok(())
77-
}
74+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2020-2025 IOTA Stiftung
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use anyhow::Result;
5+
use examples::get_funded_client;
6+
use notarization::core::state::State;
7+
8+
#[tokio::main]
9+
async fn main() -> Result<()> {
10+
println!("Demonstrating update on dynamic notarization");
11+
12+
let notarization_client = get_funded_client().await?;
13+
14+
println!("Creating a dynamic notarization...");
15+
16+
// Create a dynamic notarization
17+
let dynamic_notarization_id = notarization_client
18+
.create_dynamic_notarization()
19+
.with_state(State::from_string(
20+
"Original content".to_string(),
21+
Some("Original metadata".to_string()),
22+
))
23+
.with_immutable_description("Dynamic document for update test".to_string())
24+
.with_updateable_metadata("Initial updateable metadata".to_string())
25+
.finish()
26+
.build_and_execute(&notarization_client)
27+
.await?
28+
.output
29+
.id;
30+
31+
println!("✅ Dynamic notarization created with ID: {:?}", dynamic_notarization_id);
32+
33+
let current_state = notarization_client.state(*dynamic_notarization_id.object_id()).await?;
34+
println!("Initial state: {}", current_state.data.as_text()?);
35+
println!("Initial state metadata: {:?}", current_state.metadata);
36+
37+
println!("\n🔄 Updating state on dynamic notarization...");
38+
let new_state = State::from_string("Updated content".to_string(), Some("New metadata".to_string()));
39+
40+
let state_update_result = notarization_client
41+
.update_state(new_state, *dynamic_notarization_id.object_id())
42+
.build_and_execute(&notarization_client)
43+
.await;
44+
45+
match state_update_result {
46+
Ok(_) => println!("✅ State update succeeded"),
47+
Err(e) => println!("❌ State update failed - {}", e),
48+
}
49+
50+
println!("\n📝 Updating metadata on dynamic notarization...");
51+
let new_metadata = Some("Updated metadata".to_string());
52+
53+
let metadata_update_result = notarization_client
54+
.update_metadata(new_metadata, *dynamic_notarization_id.object_id())
55+
.build_and_execute(&notarization_client)
56+
.await;
57+
58+
match metadata_update_result {
59+
Ok(_) => println!("✅ Metadata update succeeded"),
60+
Err(e) => println!("❌ Metadata update failed - {}", e),
61+
}
62+
63+
let current_state = notarization_client.state(*dynamic_notarization_id.object_id()).await?;
64+
println!("Updated state: {}", current_state.data.as_text()?);
65+
println!("Updated state metadata: {:?}", current_state.metadata);
66+
67+
println!("\n🔒 Dynamic notarizations are mutable - state and metadata can be changed");
68+
69+
Ok(())
70+
}

examples/03_update_locked_notarization.rs

Lines changed: 0 additions & 97 deletions
This file was deleted.

examples/05_update_state.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,38 +75,16 @@ async fn main() -> Result<()> {
7575
assert_eq!(version_count, i as u64);
7676
}
7777

78-
// Demonstrate working with binary data
79-
println!("\n🔢 Demonstrating binary state updates...");
80-
81-
let binary_data = vec![0x48, 0x65, 0x6C, 0x6C, 0x6F]; // "Hello" in bytes
82-
let binary_state = State::from_bytes(binary_data.clone(), Some("Binary data example".to_string()));
83-
84-
// Update with binary state
85-
notarization_client
86-
.update_state(binary_state, *notarization_id.object_id())
87-
.build_and_execute(&notarization_client)
88-
.await?;
89-
90-
println!("✅ Binary state update completed");
91-
92-
// Retrieve and verify binary state
93-
let binary_retrieved = notarization_client.state(*notarization_id.object_id()).await?;
94-
95-
let retrieved_bytes = binary_retrieved.data.as_bytes()?;
96-
println!("Retrieved binary data: {:?}", retrieved_bytes);
97-
println!("As text: {}", String::from_utf8_lossy(&retrieved_bytes));
98-
99-
// Verify binary data matches
100-
assert_eq!(retrieved_bytes, binary_data);
101-
10278
// Show final version count
10379
let final_version_count = notarization_client
10480
.state_version_count(*notarization_id.object_id())
10581
.await?;
10682

83+
let final_state = notarization_client.state(*notarization_id.object_id()).await?;
84+
10785
println!("\n📊 Final Statistics:");
10886
println!("Total updates performed: {}", final_version_count);
109-
println!("Final metadata: {:?}", binary_retrieved.metadata);
87+
println!("Final metadata: {:?}", final_state.metadata);
11088

11189
// Get last state change timestamp
11290
let last_change = notarization_client
@@ -118,7 +96,7 @@ async fn main() -> Result<()> {
11896
println!("\n🎯 Key Points:");
11997
println!("✓ Dynamic notarizations support state updates");
12098
println!("✓ Each update increments the version count");
121-
println!("✓ Both text and binary data are supported");
99+
println!("✓ Both text and bytes data are supported");
122100
println!("✓ State metadata can be updated alongside content");
123101
println!("✓ Timestamps track when changes were made");
124102

examples/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ path = "02_create_dynamic_notarization.rs"
3737
name = "02_create_dynamic_notarization"
3838

3939
[[example]]
40-
path = "03_update_locked_notarization.rs"
41-
name = "03_update_locked_notarization"
40+
path = "03_update_dynamic_notarization.rs"
41+
name = "03_update_dynamic_notarization"
4242

4343
[[example]]
4444
path = "04_destroy_notarization.rs"

examples/run.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
# Script to run all notarization examples
4+
# Usage: ./run.sh
5+
# Make sure to set NOTARIZATION_PKG_ID environment variable
6+
7+
if [ -z "$IOTA_NOTARIZATION_PKG_ID" ]; then
8+
echo "Error: NOTARIZATION_PKG_ID environment variable is not set"
9+
echo "Usage: NOTARIZATION_PKG_ID=0x... ./run.sh"
10+
exit 1
11+
fi
12+
13+
echo "Running all notarization examples..."
14+
echo "Package ID: $IOTA_NOTARIZATION_PKG_ID"
15+
echo "================================"
16+
17+
examples=(
18+
"01_create_locked_notarization"
19+
"02_create_dynamic_notarization"
20+
"03_update_dynamic_notarization"
21+
"04_destroy_notarization"
22+
"05_update_state"
23+
"06_update_metadata"
24+
"07_transfer_dynamic_notarization"
25+
"08_access_read_only_methods"
26+
)
27+
28+
for example in "${examples[@]}"; do
29+
echo ""
30+
echo "Running: $example"
31+
echo "------------------------"
32+
cargo run --release --example "$example"
33+
if [ $? -ne 0 ]; then
34+
echo "Error: Failed to run $example"
35+
exit 1
36+
fi
37+
done
38+
39+
echo ""
40+
echo "All examples completed successfully!"

notarization-move/Move.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ flavor = "iota"
2929
[env]
3030

3131
[env.localnet]
32-
chain-id = "11d8ffd3"
33-
original-published-id = "0x1a00d75b8c5cc4086b469afccf3abbdcca55b7e57ac7a1f24d3c0d1f71fe4de4"
34-
latest-published-id = "0x1a00d75b8c5cc4086b469afccf3abbdcca55b7e57ac7a1f24d3c0d1f71fe4de4"
32+
chain-id = "f34938a6"
33+
original-published-id = "0x4d3fb7928aa352e7ad65710fa06a27848f99d428bbc8d99938557f65f7753623"
34+
latest-published-id = "0x4d3fb7928aa352e7ad65710fa06a27848f99d428bbc8d99938557f65f7753623"
3535
published-version = "1"
3636

3737
[env.devnet]

notarization-move/sources/notarization.move

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public struct NotarizationDestroyed has copy, drop {
101101

102102
// ===== Constructor Functions =====
103103
/// Create a new state from a vector<u8> data
104-
public fun new_state_from_vector(data: vector<u8>, metadata: Option<String>): State<vector<u8>> {
104+
public fun new_state_from_bytes(data: vector<u8>, metadata: Option<String>): State<vector<u8>> {
105105
State { data, metadata }
106106
}
107107

notarization-move/tests/dynamic_notarization_tests.move

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public fun test_create_dynamic_notarization_with_vector_data() {
7777
vector::push_back(&mut data, 2u8);
7878
vector::push_back(&mut data, 3u8);
7979
let metadata = std::option::some(string::utf8(b"Test Metadata"));
80-
let state = notarization::new_state_from_vector(data, metadata);
80+
let state = notarization::new_state_from_bytes(data, metadata);
8181

8282
// Create a dynamic notarization with no transfer lock
8383
dynamic_notarization::create(

0 commit comments

Comments
 (0)