Skip to content

Commit

Permalink
formatting (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
leecchh authored Nov 18, 2024
1 parent d1bf67a commit f5bb391
Show file tree
Hide file tree
Showing 15 changed files with 3,368 additions and 2,683 deletions.
140 changes: 76 additions & 64 deletions packages/suins/sources/actions/update_image.move
Original file line number Diff line number Diff line change
@@ -1,82 +1,94 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module suins::update_image {
use std::string::{utf8, String};
use sui::{clock::Clock, bcs, ecdsa_k1};
module suins::update_image;

use suins::{registry::Registry, suins::SuiNS, config::Config, suins_registration::SuinsRegistration};
use std::string::{utf8, String};
use sui::bcs;
use sui::clock::Clock;
use sui::ecdsa_k1;
use suins::config::Config;
use suins::registry::Registry;
use suins::suins::SuiNS;
use suins::suins_registration::SuinsRegistration;

/// Message data cannot be parsed.
const EInvalidData: u64 = 0;
/// The parsed name does not match the expected domain.
const EInvalidDomainData: u64 = 1;
/// Invalid signature for the message.
const ESignatureNotMatch: u64 = 2;
/// Message data cannot be parsed.
const EInvalidData: u64 = 0;
/// The parsed name does not match the expected domain.
const EInvalidDomainData: u64 = 1;
/// Invalid signature for the message.
const ESignatureNotMatch: u64 = 2;

/// Authorization token for the app.
public struct UpdateImage has drop {}
/// Authorization token for the app.
public struct UpdateImage has drop {}

/// Updates the image attached to a `SuinsRegistration`.
entry fun update_image_url(
suins: &SuiNS,
nft: &mut SuinsRegistration,
raw_msg: vector<u8>,
signature: vector<u8>,
clock: &Clock,
) {
suins.assert_app_is_authorized<UpdateImage>();
let registry = suins.registry<Registry>();
registry.assert_nft_is_authorized(nft, clock);
/// Updates the image attached to a `SuinsRegistration`.
entry fun update_image_url(
suins: &SuiNS,
nft: &mut SuinsRegistration,
raw_msg: vector<u8>,
signature: vector<u8>,
clock: &Clock,
) {
suins.assert_app_is_authorized<UpdateImage>();
let registry = suins.registry<Registry>();
registry.assert_nft_is_authorized(nft, clock);

let config = suins.get_config<Config>();
let config = suins.get_config<Config>();

assert!(
ecdsa_k1::secp256k1_verify(&signature, config.public_key(), &raw_msg, 1),
ESignatureNotMatch
);
assert!(
ecdsa_k1::secp256k1_verify(
&signature,
config.public_key(),
&raw_msg,
1,
),
ESignatureNotMatch,
);

let (ipfs_hash, domain_name, expiration_timestamp_ms, _data) = image_data_from_bcs(raw_msg);
let (
ipfs_hash,
domain_name,
expiration_timestamp_ms,
_data,
) = image_data_from_bcs(raw_msg);

assert!(nft.expiration_timestamp_ms() == expiration_timestamp_ms, EInvalidData);
assert!(nft.domain().to_string() == domain_name, EInvalidDomainData);
assert!(
nft.expiration_timestamp_ms() == expiration_timestamp_ms,
EInvalidData,
);
assert!(nft.domain().to_string() == domain_name, EInvalidDomainData);

nft.update_image_url(ipfs_hash);
nft.update_image_url(ipfs_hash);

// TODO emit an event
// event::emit(ImageUpdatedEvent {
// sender: tx_context::sender(ctx),
// domain_name: nft.name,
// new_image: nft.url,
// data: additional_data,
// })
}
// TODO emit an event
// event::emit(ImageUpdatedEvent {
// sender: tx_context::sender(ctx),
// domain_name: nft.name,
// new_image: nft.url,
// data: additional_data,
// })
}

/// Parses the message bytes into the image data.
/// ```
/// struct MessageData {
/// ipfs_hash: String,
/// domain_name: String,
/// expiration_timestamp_ms: u64,
/// data: String
/// }
/// ```
fun image_data_from_bcs(msg_bytes: vector<u8>): (String, String, u64, String) {
let mut bcs = bcs::new(msg_bytes);
/// Parses the message bytes into the image data.
/// ```
/// struct MessageData {
/// ipfs_hash: String,
/// domain_name: String,
/// expiration_timestamp_ms: u64,
/// data: String
/// }
/// ```
fun image_data_from_bcs(msg_bytes: vector<u8>): (String, String, u64, String) {
let mut bcs = bcs::new(msg_bytes);

let ipfs_hash = utf8(bcs.peel_vec_u8());
let domain_name = utf8(bcs.peel_vec_u8());
let expiration_timestamp_ms = bcs.peel_u64();
let data = utf8(bcs.peel_vec_u8());
let ipfs_hash = utf8(bcs.peel_vec_u8());
let domain_name = utf8(bcs.peel_vec_u8());
let expiration_timestamp_ms = bcs.peel_u64();
let data = utf8(bcs.peel_vec_u8());

let remainder = bcs.into_remainder_bytes();
remainder.destroy_empty();
let remainder = bcs.into_remainder_bytes();
remainder.destroy_empty();

(
ipfs_hash,
domain_name,
expiration_timestamp_ms,
data,
)
}
(ipfs_hash, domain_name, expiration_timestamp_ms, data)
}
2 changes: 1 addition & 1 deletion packages/suins/sources/domain.move
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public fun parent(domain: &Domain): Domain {

/// Checks if `parent` domain is a valid parent for `child`.
public fun is_parent_of(parent: &Domain, child: &Domain): bool {
number_of_levels(parent) < number_of_levels(child) &&
number_of_levels(parent) < number_of_levels(child) &&
&parent(child).labels == &parent.labels
}

Expand Down
Loading

0 comments on commit f5bb391

Please sign in to comment.