Skip to content

Commit

Permalink
feat: add all code
Browse files Browse the repository at this point in the history
  • Loading branch information
hsinghgrewal committed Dec 4, 2024
1 parent c43966a commit 64d3d06
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 79 deletions.
28 changes: 14 additions & 14 deletions packages/message_transmitter/sources/attester.move
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module message_transmitter::attester {
const ENOT_OWNER: u64 = 1;
const EATTESTER_ALREADY_ENABLED: u64 = 2;
const ETOO_FEW_ENABLED_ATTESTERS: u64 = 3;
const ELOW_SIGNATURE_THRESHOLD: u64 = 4;
const ENUM_ATTESTERS_SHOULD_BE_GREATER_THAN_SIGNATURE_THRESHOLD: u64 = 4;
const ENOT_ATTESTER_MANAGER: u64 = 5;
const EINVALID_ATTESTER_MANAGER: u64 = 6;
const EINVALID_SIGNATURE_THRESHOLD: u64 = 7;
Expand Down Expand Up @@ -146,7 +146,7 @@ module message_transmitter::attester {
assert!(vector::length<address>(&enabled_attesters) > 1, error::invalid_state(ETOO_FEW_ENABLED_ATTESTERS));
assert!(
vector::length<address>(&enabled_attesters) > state::get_signature_threshold(),
error::invalid_state(ELOW_SIGNATURE_THRESHOLD)
error::invalid_state(ENUM_ATTESTERS_SHOULD_BE_GREATER_THAN_SIGNATURE_THRESHOLD)
);
state::remove_attester(attester);
event::emit(AttesterDisabled { attester });
Expand Down Expand Up @@ -186,21 +186,13 @@ module message_transmitter::attester {
event::emit(SignatureThresholdUpdated { old_signature_threshold, new_signature_threshold });
}

// -----------------------------
// ----- Friend Functions ------
// -----------------------------

public(friend) fun init_attester(caller: &signer, attester: address) {
enable_attester(caller, attester);
}

/// Validates the attestation for the given message
/// Aborts if:
/// - length of attestation != ATTESTATION_SIGNATURE_LENGTH * signature_threshold
/// - there are duplicate signers
/// - signer is not one of the enabled attesters
/// - addresses recovered are not in increasing order
public(friend) fun verify_attestation_signature(message: &vector<u8>, attestation: &vector<u8>) {
public fun verify_attestation_signature(message: &vector<u8>, attestation: &vector<u8>) {
// Validate Attestation Size
let signature_threshold = state::get_signature_threshold();
assert!(
Expand Down Expand Up @@ -240,6 +232,14 @@ module message_transmitter::attester {
}
}

// -----------------------------
// ----- Friend Functions ------
// -----------------------------

public(friend) fun init_attester(caller: &signer, attester: address) {
enable_attester(caller, attester);
}

// -----------------------------
// ----- Private Functions -----
// -----------------------------
Expand All @@ -253,7 +253,7 @@ module message_transmitter::attester {
// Retrieve and validate signature id
let recovery_id = *vector::borrow(signature, SIGNATURE_LENGTH - 1) - 27;
assert!(
vector::contains(&vector[0, 1, 2, 3], &recovery_id),
vector::contains(&vector[0, 1], &recovery_id),
error::invalid_argument(EINVALID_SIGNATURE)
);

Expand Down Expand Up @@ -286,15 +286,15 @@ module message_transmitter::attester {
let address_bytes = keccak256(*public_key);

// EVM address is the made of last 20 bytes of the hash
let address = vector::slice(
let address_without_prefix = vector::slice(
&address_bytes,
vector::length(&address_bytes) - 20,
vector::length(&address_bytes)
);

// Add 0x0 prefix to make the address 32 bytes
let address_with_prefix = x"000000000000000000000000";
vector::append(&mut address_with_prefix, address);
vector::append(&mut address_with_prefix, address_without_prefix);
from_bcs::to_address(address_with_prefix)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ module message_transmitter::message_transmitter {
// -----------------------------

/// Generate signer from the `ExtendRef`
public(friend) fun get_signer(): signer acquires ObjectController {
fun get_signer(): signer acquires ObjectController {
let object_address = state::get_object_address();
let object_controller = borrow_global<ObjectController>(object_address);
let object_signer = object::generate_signer_for_extending(
Expand Down Expand Up @@ -495,6 +495,11 @@ module message_transmitter::message_transmitter {
(message, attestation)
}

#[test_only]
public fun get_message_from_event(message_sent_event: &MessageSent): vector<u8> {
message_sent_event.message
}

// Message Transmitter Initialization Tests

#[test(owner = @deployer)]
Expand Down
23 changes: 8 additions & 15 deletions packages/message_transmitter/sources/state.move
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ module message_transmitter::state {

public(friend) fun init_state(
owner: &signer,
admin: &signer,
object_signer: &signer,
local_domain: u32,
version: u32,
max_message_body_size: u64
) {
move_to(
admin,
object_signer,
State {
local_domain,
version,
Expand Down Expand Up @@ -132,10 +132,6 @@ module message_transmitter::state {
// ---------- Setters ----------
// -----------------------------

public(friend) fun set_version(version: u32) acquires State {
borrow_global_mut<State>(get_object_address()).version = version
}

public(friend) fun set_max_message_body_size(max_message_body_size: u64) acquires State {
borrow_global_mut<State>(get_object_address()).max_message_body_size = max_message_body_size
}
Expand Down Expand Up @@ -184,7 +180,7 @@ module message_transmitter::state {
use aptos_extensions::pausable::{Self, PauseState};

#[test_only]
public(friend) fun init_test_state(caller: &signer) {
public fun init_test_state(caller: &signer) {
let resource_account_address = account::create_resource_address(&@deployer, b"test_seed_mt");
let resource_account_signer = create_signer_for_test(resource_account_address);
let constructor_ref = object::create_named_object(&resource_account_signer, SEED_NAME);
Expand All @@ -204,6 +200,11 @@ module message_transmitter::state {
ownable::set_owner_for_testing(get_object_address(), owner_address);
}

#[test_only]
public fun set_version(version: u32) acquires State {
borrow_global_mut<State>(get_object_address()).version = version
}

// -----------------------------
// ---------- Getters ----------
// -----------------------------
Expand Down Expand Up @@ -281,14 +282,6 @@ module message_transmitter::state {
// ---------- Setters ----------
// -----------------------------

#[test(owner = @message_transmitter)]
fun test_set_version(owner: &signer) acquires State {
init_test_state(owner);
let version = 10;
set_version(version);
assert!(get_version() == version, 0);
}

#[test(owner = @message_transmitter)]
fun test_set_max_message_body_size(owner: &signer) acquires State {
init_test_state(owner);
Expand Down
33 changes: 20 additions & 13 deletions packages/token_messenger_minter/sources/state.move
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ module token_messenger_minter::state {
smart_table::length(&borrow_global<State>(get_object_address()).remote_token_messengers)
}

public(friend) fun get_num_linked_tokens(): u64 acquires State {
smart_table::length(&borrow_global<State>(get_object_address()).remote_tokens_to_local_tokens)
}

public(friend) fun get_object_address(): address {
object::create_object_address(&@token_messenger_minter, SEED_NAME)
}
Expand All @@ -138,10 +142,6 @@ module token_messenger_minter::state {
// ---------- Setters ----------
// -----------------------------

public(friend) fun set_message_body_version(message_body_version: u32) acquires State {
borrow_global_mut<State>(get_object_address()).message_body_version = message_body_version;
}

public(friend) fun add_remote_token_messenger(domain: u32, token_messenger: address) acquires State {
smart_table::add(
&mut borrow_global_mut<State>(get_object_address()).remote_token_messengers,
Expand Down Expand Up @@ -210,7 +210,7 @@ module token_messenger_minter::state {
// -----------------------------

#[test_only]
public(friend) fun init_test_state(caller: &signer) {
public fun init_test_state(caller: &signer) {
let resource_account_address = account::create_resource_address(&@deployer, b"test_seed_tmm");
let resource_account_signer = create_signer_for_test(resource_account_address);
let constructor_ref = object::create_named_object(&resource_account_signer, SEED_NAME);
Expand All @@ -221,10 +221,15 @@ module token_messenger_minter::state {
}

#[test_only]
public(friend) fun set_paused(pauser: &signer) {
public fun set_paused(pauser: &signer) {
pausable::test_pause(pauser, object::address_to_object<PauseState>(get_object_address()));
}

#[test_only]
public fun set_message_body_version(message_body_version: u32) acquires State {
borrow_global_mut<State>(get_object_address()).message_body_version = message_body_version;
}

// -----------------------------
// ---------- Getters ----------
// -----------------------------
Expand Down Expand Up @@ -342,18 +347,20 @@ module token_messenger_minter::state {
assert!(hash == @0x8ae28a8d79ea231bafe63fb643c45dce060b33a1c2e122d161c4d8b75997436, 0);
}

#[test(owner = @token_messenger_minter)]
fun test_get_num_linked_tokens(owner: &signer) acquires State {
init_test_state(owner);
smart_table::add(
&mut borrow_global_mut<State>(get_object_address()).remote_tokens_to_local_tokens, @0x100, @0x101
);
assert!(get_num_linked_tokens() == 1, 0)
}


// -----------------------------
// ---------- Setters ----------
// -----------------------------

#[test(owner = @token_messenger_minter)]
fun test_set_message_body_version(owner: &signer) acquires State {
init_test_state(owner);
set_message_body_version(5);
assert!(get_message_body_version() == 5, 0);
}

#[test(owner = @token_messenger_minter)]
fun test_add_remote_token_messenger(owner: &signer) acquires State {
init_test_state(owner);
Expand Down
Loading

0 comments on commit 64d3d06

Please sign in to comment.