-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
285 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
export enum MessageType { | ||
RAW = 0, | ||
XOR = 1, | ||
} | ||
|
||
export class MessageEncoder { | ||
private xor_key = 'suia'; | ||
|
||
public encode(raw_message: string, type: MessageType): Uint8Array { | ||
let encoded = new Uint8Array(raw_message.length + 1); | ||
let uint8Array = new Uint8Array(raw_message.length); | ||
encoded[0] = type; | ||
if (type == MessageType.RAW) { | ||
uint8Array = this.encode_raw(raw_message); | ||
} else if (type == MessageType.XOR) { | ||
uint8Array = this.encode_xor(raw_message); | ||
} else { | ||
throw new Error('Unsupported message type'); | ||
} | ||
encoded.set(uint8Array, 1); | ||
return encoded; | ||
} | ||
|
||
public decode(encoded_message: Uint8Array): string { | ||
let type = encoded_message[0]; | ||
if (type == MessageType.RAW) { | ||
return this.decode_raw(encoded_message.slice(1)); | ||
} else if (type == MessageType.XOR) { | ||
return this.decode_xor(encoded_message.slice(1)); | ||
} else { | ||
throw new Error('Unsupported message type'); | ||
} | ||
} | ||
|
||
public encode_raw(raw_message: string): Uint8Array { | ||
const encoder = new TextEncoder(); | ||
const uint8Array = encoder.encode(raw_message); | ||
return uint8Array; | ||
} | ||
|
||
public decode_raw(encoded_message: Uint8Array): string { | ||
const decoder = new TextDecoder(); | ||
return decoder.decode(encoded_message); | ||
} | ||
|
||
public encode_xor(raw_message: string): Uint8Array { | ||
let raw = this.encode_raw(raw_message); | ||
let result = new Uint8Array(raw.length); | ||
for (let i = 0; i < raw.length; i++) { | ||
result[i] = raw[i] ^ this.xor_key.charCodeAt(i % this.xor_key.length); | ||
} | ||
return result; | ||
} | ||
|
||
public decode_xor(encoded_message: Uint8Array): string { | ||
let result = new Uint8Array(encoded_message.length); | ||
for (let i = 0; i < encoded_message.length; i++) { | ||
result[i] = encoded_message[i] ^ this.xor_key.charCodeAt(i % this.xor_key.length); | ||
} | ||
return this.decode_raw(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
module socialcoin::club { | ||
use std::vector; | ||
use sui::clock::{Clock, timestamp_ms}; | ||
use sui::table_vec; | ||
use socialcoin::socialcoin::is_holder; | ||
use socialcoin::socialcoin; | ||
use sui::transfer::share_object; | ||
use sui::table; | ||
use sui::tx_context; | ||
use sui::tx_context::TxContext; | ||
use sui::table::Table; | ||
use sui::table_vec::TableVec; | ||
use sui::object::{UID, new}; | ||
|
||
// errors | ||
const ERR_NOT_AUTHORIZED: u64 = 1; | ||
const ERR_MESSAGE_NOT_FOUND: u64 = 2; | ||
const ERR_MESSAGE_DELETED: u64 = 3; | ||
|
||
struct Global has key, store { | ||
id: UID, | ||
admin: address, | ||
clubs: Table<address, Club>, | ||
} | ||
|
||
struct Club has store { | ||
owner: address, | ||
messages: TableVec<Message> | ||
} | ||
|
||
struct Message has store { | ||
sender: address, | ||
content: vector<u8>, | ||
timestamp: u64, | ||
deleted: bool, | ||
} | ||
|
||
fun init(ctx: &mut TxContext) { | ||
let sender = tx_context::sender(ctx); | ||
let global = Global { | ||
id: new(ctx), | ||
admin: sender, | ||
clubs: table::new(ctx), | ||
}; | ||
share_object(global); | ||
} | ||
|
||
|
||
entry public fun new_message( | ||
clock: &Clock, | ||
social_coin_global: &socialcoin::Global, | ||
club_global: &mut Global, | ||
club_owner: address, | ||
content: vector<u8>, | ||
ctx: &mut TxContext, | ||
) { | ||
let sender = tx_context::sender(ctx); | ||
assert!(is_holder(social_coin_global, sender, club_owner), ERR_NOT_AUTHORIZED); | ||
if(!table::contains(&club_global.clubs, club_owner)) { | ||
let club = Club { | ||
owner: club_owner, | ||
messages: table_vec::empty(ctx), | ||
}; | ||
table::add(&mut club_global.clubs, club_owner, club); | ||
}; | ||
let club = table::borrow_mut(&mut club_global.clubs, club_owner); | ||
let message = Message { | ||
sender, | ||
content, | ||
timestamp: timestamp_ms(clock), | ||
deleted: false, | ||
}; | ||
table_vec::push_back(&mut club.messages, message); | ||
} | ||
|
||
entry public fun delete_message( | ||
club_global: &mut Global, | ||
club_owner: address, | ||
message_index: u64, | ||
ctx: &mut TxContext, | ||
) { | ||
let sender = tx_context::sender(ctx); | ||
let club = table::borrow_mut(&mut club_global.clubs, club_owner); | ||
assert!(message_index < table_vec::length(&club.messages), ERR_MESSAGE_NOT_FOUND); | ||
let message = table_vec::borrow_mut(&mut club.messages, message_index); | ||
assert!(message.sender == sender || club_global.admin == sender, ERR_NOT_AUTHORIZED); | ||
assert!(!message.deleted, ERR_MESSAGE_DELETED); | ||
message.content = vector::empty(); | ||
message.deleted = true; | ||
} | ||
} |