Part 3: Encryption Logic
Overview
Before implementing encryption logic, let's first modify the Note
struct. Our policy will be that only the user (owner) can decrypt the note content.
We need to store information about the owner in the Note
object.
Note: You cannot update an object attribute in an existing contract. You will need to re-publish the contract after making these changes.
We will also implement the Data
struct for content storage.
Updated Note Struct
The Note
struct should now look like this:
// Import the data module and give it an alias `dt`.
// Also import the `Data` struct for type safety.
use suinotes::data::{Self as dt, Data};
public struct Note has key, store {
id: UID,
title: String,
content: Data, // content type changed to `Data`
timestamp: u64,
owner: address, // newly added attribute
}
Updated Functions
Because of the changes made to the struct, we need to update the functions as well:
public fun create(title: String, text: String, clock: &Clock, ctx: &mut TxContext): Note {
let data = dt::create(dt::text(), text);
let note = Note {
id: object::new(ctx),
title,
content: data,
timestamp: clock.timestamp_ms(),
owner: ctx.sender(),
};
note
}
public fun edit(note: &mut Note, text: String) {
note.content.edit(text);
}
public fun view(note: &Note): (ID, String, Data, u64) {
(object::id(note), note.title, note.content, note.timestamp)
}
public fun delete(note: Note) {
let Note { id, .. } = note;
id.delete();
}
Owner Verification Function
Now, let's create a function that verifies the requester is the owner of the note:
public fun seal_approve(note: &Note, ctx: &mut TxContext): bool {
assert!(note.owner == ctx.sender(), ENotAuthorized);
note.owner == ctx.sender()
}
Now you are ready to implement the Sui SDK.