-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_impl.rs
188 lines (175 loc) · 5.9 KB
/
core_impl.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::types::{Content, DstContract, Session};
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::UnorderedMap;
use near_sdk::json_types::U128;
use near_sdk::{env, ext_contract, AccountId, Balance, Gas, IntoStorageKey, Promise};
use std::collections::HashMap;
const GAS_FOR_SENT_MESSAGE: Gas = Gas(5_000_000_000_000);
const NO_DEPOSIT: Balance = 0;
#[ext_contract(ext_cross_contract)]
pub trait OmniChainContract {
fn send_message(
&mut self,
to_chain: String,
content: Content,
session: Option<Session>,
) -> u128;
}
#[derive(BorshDeserialize, BorshSerialize, Debug)]
pub struct OmniChain {
pub owner_id: AccountId,
pub omni_chain_contract_id: AccountId,
pub destination_contract: UnorderedMap<String, HashMap<String, DstContract>>,
pub permitted_contract: UnorderedMap<(String, Vec<u8>), Vec<String>>,
}
impl OmniChain {
pub fn new<S, T>(
owner_id: AccountId,
destination_contract_prefix: S,
permitted_contract_prefix: T,
omni_chain_contract_id: AccountId,
) -> Self
where
S: IntoStorageKey,
T: IntoStorageKey,
{
let this = Self {
owner_id,
omni_chain_contract_id,
destination_contract: UnorderedMap::new(destination_contract_prefix),
permitted_contract: UnorderedMap::new(permitted_contract_prefix),
};
this
}
pub fn internal_call_omni_chain(
&self,
to_chain: String,
content: Content,
session: Option<Session>,
) -> Promise {
ext_cross_contract::send_message(
to_chain,
content,
session,
self.omni_chain_contract_id.clone(),
NO_DEPOSIT,
GAS_FOR_SENT_MESSAGE,
)
}
pub fn call_cross(&self, to_chain: String, content: Content) -> Promise {
self.internal_call_omni_chain(to_chain, content, None)
}
pub fn call_cross_with_session(
&self,
to_chain: String,
content: Content,
callback: Vec<u8>,
) -> Promise {
self.internal_call_omni_chain(
to_chain,
content,
Some(Session {
id: U128(0),
session_type: 2,
callback: Some(callback),
commitment: None,
answer: None,
}),
)
}
pub fn send_response_message(&self, to_chain: String, content: Content, id: U128) {
self.internal_call_omni_chain(
to_chain,
content,
Some(Session {
id,
session_type: 1,
callback: None,
commitment: None,
answer: None,
}),
);
}
pub fn register_dst_contract(
&mut self,
chain_name: String,
action_name: String,
contract_address: String,
contract_action_name: String,
) {
assert_eq!(env::predecessor_account_id(), self.owner_id, "Unauthorize");
let contract_address = hex::decode(contract_address.strip_prefix("0x").unwrap()).unwrap();
let contract_action_name =
hex::decode(contract_action_name.strip_prefix("0x").unwrap()).unwrap();
match self.destination_contract.get(&chain_name) {
Some(mut map) => {
// if !map.contains_key(&action_name) {
// map.insert(
// action_name,
// DstContract {
// contract_address,
// action_name: contract_action_name,
// },
// );
// } else {
// env::panic_str("Already contains");
// }
map.insert(
action_name,
DstContract {
contract_address,
action_name: contract_action_name,
},
);
self.destination_contract.insert(&chain_name, &map);
}
_ => {
let mut ms = HashMap::new();
ms.insert(
action_name,
DstContract {
contract_address,
action_name: contract_action_name,
},
);
self.destination_contract.insert(&chain_name, &ms);
}
}
}
///////////////////////////////////////////////
/// Receive messages from other chains ///
///////////////////////////////////////////////
/**
* Authorize contracts of other chains to call the action of this contract
* @param chain_name - from chain name
* @param sender - sender of cross chain message
* @param action_name - action name which allowed to be invoked
*/
pub fn register_permitted_contract(
&mut self,
chain_name: String,
sender: String,
action_name: String,
) {
// assert_eq!(self.owner_id, env::predecessor_account_id(), "Unauthorize");
let sender = hex::decode(sender.strip_prefix("0x").unwrap()).unwrap();
let key = (chain_name, sender);
let mut actions = Vec::new();
if let Some(acts) = self.permitted_contract.get(&key) {
assert!(!actions.contains(&action_name), "Already exist");
actions.extend(acts.into_iter());
}
actions.push(action_name);
self.permitted_contract.insert(&key, &actions);
}
pub fn assert_register_permitted_contract(
&self,
chain_name: &String,
sender: &Vec<u8>,
action: &String,
) {
let key = (chain_name.clone(), sender.clone());
let actions = self.permitted_contract.get(&key).unwrap_or(Vec::new());
assert!(actions.contains(action), "{} not register", action);
}
}