139 lines
3.7 KiB
Plaintext
139 lines
3.7 KiB
Plaintext
#pragma version ^0.4.1;
|
|
#include "../imports/stdlib.fc";
|
|
#include "../utils/Constants.fc";
|
|
#include "../utils/Math.fc";
|
|
#include "../utils/Security.fc";
|
|
|
|
;; Message operation codes
|
|
const int op_transfer = 1;
|
|
const int op_lock = 2;
|
|
const int op_unlock = 3;
|
|
const int op_verify = 4;
|
|
|
|
;; Error codes
|
|
const int error_unauthorized = 101;
|
|
const int error_invalid_signature = 102;
|
|
const int error_invalid_amount = 103;
|
|
const int error_invalid_chain = 104;
|
|
const int error_invalid_message = 105;
|
|
|
|
;; Storage variables
|
|
global slice bridge_addr;
|
|
global int min_validators;
|
|
global int cur_nonce;
|
|
global cell validators_dict;
|
|
global cell pending_transfers;
|
|
global cell verifications;
|
|
|
|
() load_data() impure {
|
|
slice ds = get_data().begin_parse();
|
|
bridge_addr = ds~load_msg_addr();
|
|
min_validators = ds~load_uint(8);
|
|
cur_nonce = ds~load_uint(64);
|
|
validators_dict = ds~load_dict();
|
|
pending_transfers = ds~load_dict();
|
|
verifications = ds~load_dict();
|
|
}
|
|
|
|
() save_data() impure {
|
|
set_data(begin_cell()
|
|
.store_slice(bridge_addr)
|
|
.store_uint(min_validators, 8)
|
|
.store_uint(cur_nonce, 64)
|
|
.store_dict(validators_dict)
|
|
.store_dict(pending_transfers)
|
|
.store_dict(verifications)
|
|
.end_cell());
|
|
}
|
|
|
|
() recv_internal(int msg_value, cell in_msg_full, slice in_msg_body) impure {
|
|
if (in_msg_body.slice_empty?()) {
|
|
return ();
|
|
}
|
|
|
|
slice cs = in_msg_full.begin_parse();
|
|
int flags = cs~load_uint(4);
|
|
if (flags & 1) {
|
|
return ();
|
|
}
|
|
|
|
slice sender = cs~load_msg_addr();
|
|
int op = in_msg_body~load_uint(32);
|
|
|
|
load_data();
|
|
|
|
if (op == op_transfer) {
|
|
handle_transfer(in_msg_body, sender);
|
|
} elseif (op == op_lock) {
|
|
handle_lock(in_msg_body, sender, msg_value);
|
|
} elseif (op == op_verify) {
|
|
handle_verification(in_msg_body, sender);
|
|
}
|
|
|
|
save_data();
|
|
}
|
|
|
|
() handle_transfer(slice msg, slice sender) impure {
|
|
throw_unless(error_unauthorized, equal_slices(sender, bridge_addr));
|
|
|
|
int source_chain = msg~load_uint(32);
|
|
int target_chain = msg~load_uint(32);
|
|
int amount = msg~load_coins();
|
|
slice recipient = msg~load_msg_addr();
|
|
|
|
throw_unless(error_invalid_chain, check_chain(source_chain));
|
|
throw_unless(error_invalid_chain, check_chain(target_chain));
|
|
|
|
cell transfer_data = begin_cell()
|
|
.store_uint(source_chain, 32)
|
|
.store_uint(target_chain, 32)
|
|
.store_coins(amount)
|
|
.store_slice(recipient)
|
|
.store_uint(cur_nonce, 64)
|
|
.end_cell();
|
|
|
|
pending_transfers~udict_set(64, cur_nonce, transfer_data);
|
|
cur_nonce += 1;
|
|
}
|
|
|
|
() handle_verification(slice msg, slice sender) impure {
|
|
throw_unless(error_unauthorized, check_validator(sender));
|
|
|
|
int nonce = msg~load_uint(64);
|
|
cell sig = msg~load_ref();
|
|
|
|
(cell transfer_data, int found) = pending_transfers.udict_get?(64, nonce);
|
|
throw_unless(error_invalid_message, found);
|
|
|
|
int verified_count = get_verified_count(nonce);
|
|
verified_count += 1;
|
|
verifications~udict_set(64, nonce, begin_cell().store_uint(verified_count, 8).end_cell());
|
|
|
|
if (verified_count >= min_validators) {
|
|
finalize_transfer(nonce, transfer_data);
|
|
}
|
|
}
|
|
|
|
int check_validator(slice addr) {
|
|
(_, int found) = validators_dict.udict_get?(267, slice_hash(addr));
|
|
return found;
|
|
}
|
|
|
|
int get_verified_count(int nonce) {
|
|
(cell data, int found) = verifications.udict_get?(64, nonce);
|
|
if (found) {
|
|
return data.begin_parse().preload_uint(8);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
;; Get methods
|
|
int get_pending_count() method_id {
|
|
load_data();
|
|
return cur_nonce;
|
|
}
|
|
|
|
int is_validator(slice addr) method_id {
|
|
load_data();
|
|
return check_validator(addr);
|
|
} |