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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#[cfg(target_arch = "wasm32")]
use std::convert::TryInto;
use crate::types::Address;
#[cfg(target_arch = "wasm32")]
use crate::types::Raw;
use crate::utils::sha3_256;
#[cfg(target_arch = "wasm32")]
use ewasm_api::types::{StorageKey, StorageValue};
#[cfg(not(target_arch = "wasm32"))]
pub struct StorageValue {}
pub fn calculate_approval_hash(sender: &[u8; 20], spender: &[u8; 20]) -> Vec<u8> {
let mut allowance: Vec<u8> = "approval".as_bytes().into();
allowance.extend_from_slice(sender);
allowance.extend_from_slice(spender);
sha3_256(&allowance).to_vec()
}
pub fn calculate_token_approval_hash(token_id: &[u8; 32]) -> Vec<u8> {
let mut token_approval: Vec<u8> = "token approval".as_bytes().into();
token_approval.extend_from_slice(token_id);
sha3_256(&token_approval).to_vec()
}
pub fn calculate_allowance_hash(sender: &[u8; 20], spender: &[u8; 20]) -> Vec<u8> {
let mut allowance: Vec<u8> = "allowance".as_bytes().into();
allowance.extend_from_slice(sender);
allowance.extend_from_slice(spender);
sha3_256(&allowance).to_vec()
}
pub fn calculate_balance_hash(address: &[u8; 20]) -> Vec<u8> {
let mut balance_of: Vec<u8> = "balanceOf".as_bytes().into();
balance_of.extend_from_slice(address);
sha3_256(&balance_of).to_vec()
}
pub fn calculate_token_hash(token_id: &[u8; 32]) -> Vec<u8> {
let mut token: Vec<u8> = "token_id".as_bytes().into();
token.extend_from_slice(token_id);
sha3_256(&token).to_vec()
}
pub fn calculate_token_balance_hash(address: &[u8; 20], token_id: &[u8; 32]) -> Vec<u8> {
let mut balance_of: Vec<u8> = "balanceOf".as_bytes().into();
balance_of.extend_from_slice(address);
balance_of.extend_from_slice(token_id);
sha3_256(&balance_of).to_vec()
}
#[cfg(not(target_arch = "wasm32"))]
pub fn get_balance(_address: &Address) -> StorageValue {
StorageValue {}
}
#[cfg(target_arch = "wasm32")]
pub fn get_balance(address: &Address) -> StorageValue {
let hash = calculate_balance_hash(&address.inner.bytes);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
ewasm_api::storage_load(&storage_key)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn set_balance(_address: &Address, _value: &StorageValue) {}
#[cfg(target_arch = "wasm32")]
pub fn set_balance(address: &Address, value: &StorageValue) {
let hash = calculate_balance_hash(&address.inner.bytes);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
ewasm_api::storage_store(&storage_key, &value);
}
#[cfg(not(target_arch = "wasm32"))]
pub fn get_token_balance(_address: &Address, _token_id: &[u8; 32]) -> StorageValue {
StorageValue {}
}
#[cfg(target_arch = "wasm32")]
pub fn get_token_balance(address: &Address, token_id: &[u8; 32]) -> StorageValue {
let hash = calculate_token_balance_hash(&address.inner.bytes, token_id);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
ewasm_api::storage_load(&storage_key)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn set_token_balance(_address: &Address, _token_id: &[u8; 32], _value: &StorageValue) {}
#[cfg(target_arch = "wasm32")]
pub fn set_token_balance(address: &Address, token_id: &[u8; 32], value: &StorageValue) {
let hash = calculate_token_balance_hash(&address.inner.bytes, token_id);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
ewasm_api::storage_store(&storage_key, &value);
}
#[cfg(not(target_arch = "wasm32"))]
pub fn get_allowance(_sender: &Address, _spender: &Address) -> StorageValue {
StorageValue {}
}
#[cfg(target_arch = "wasm32")]
pub fn get_allowance(sender: &Address, spender: &Address) -> StorageValue {
let hash = calculate_allowance_hash(&sender.inner.bytes, &spender.inner.bytes);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
ewasm_api::storage_load(&storage_key)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn set_allowance(_sender: &Address, _spender: &Address, _value: &StorageValue) {}
#[cfg(target_arch = "wasm32")]
pub fn set_allowance(sender: &Address, spender: &Address, value: &StorageValue) {
let hash = calculate_allowance_hash(&sender.inner.bytes, &spender.inner.bytes);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
ewasm_api::storage_store(&storage_key, &value);
}
#[cfg(not(target_arch = "wasm32"))]
pub fn get_token_approval(_token_id: &[u8; 32]) -> Address {
Address {
..Default::default()
}
}
#[cfg(target_arch = "wasm32")]
pub fn get_token_approval(token_id: &[u8; 32]) -> Address {
let hash = calculate_token_approval_hash(token_id);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
let buf: [u8; 20] = ewasm_api::storage_load(&storage_key).bytes[12..32]
.try_into()
.expect("token approval from hash should be here");
buf.into()
}
#[cfg(not(target_arch = "wasm32"))]
pub fn set_token_approval(_token_id: &[u8; 32], _spender: &Address) {}
#[cfg(target_arch = "wasm32")]
pub fn set_token_approval(token_id: &[u8; 32], spender: &Address) {
let hash = calculate_token_approval_hash(token_id);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
ewasm_api::storage_store(&storage_key, &Raw::from(spender).to_bytes32().into());
}
#[cfg(not(target_arch = "wasm32"))]
pub fn get_approval(_sender: &Address, _spender: &Address) -> bool {
true
}
#[cfg(target_arch = "wasm32")]
pub fn get_approval(sender: &Address, spender: &Address) -> bool {
let hash = calculate_approval_hash(&sender.inner.bytes, &spender.inner.bytes);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
ewasm_api::storage_load(&storage_key).bytes[31] == 1
}
#[cfg(not(target_arch = "wasm32"))]
pub fn set_approval(_sender: &Address, _spender: &Address, _is_approved: bool) {}
#[cfg(target_arch = "wasm32")]
pub fn set_approval(sender: &Address, spender: &Address, is_approved: bool) {
let hash = calculate_approval_hash(&sender.inner.bytes, &spender.inner.bytes);
let mut storage_key = StorageKey::default();
storage_key.bytes.copy_from_slice(&hash[0..32]);
let mut storage_value = StorageKey::default();
storage_value.bytes[31] = if is_approved { 1 } else { 0 };
ewasm_api::storage_store(&storage_key, &storage_value);
}
#[cfg(not(target_arch = "wasm32"))]
pub fn copy_into_storage_value(_slice: &[u8]) -> StorageValue {
StorageValue {}
}
#[cfg(target_arch = "wasm32")]
pub fn copy_into_storage_value(slice: &[u8]) -> StorageValue {
let mut sk = StorageKey::default();
sk.bytes[0..32].copy_from_slice(slice);
sk
}
#[cfg(not(target_arch = "wasm32"))]
pub fn copy_into_address(_slice: &[u8]) -> Address {
Address::default()
}
#[cfg(target_arch = "wasm32")]
pub fn copy_into_address(slice: &[u8]) -> Address {
let bytes20: [u8; 20] = slice.try_into().expect("the length of slice should be 20");
bytes20.into()
}
#[cfg(not(target_arch = "wasm32"))]
pub fn set_token_owner(_token_id: &[u8; 32], _owner: &Address) {}
#[cfg(target_arch = "wasm32")]
pub fn set_token_owner(token_id: &[u8; 32], owner: &Address) {
let storage_key = copy_into_storage_value(&calculate_token_hash(token_id));
let value: StorageValue = Raw::from(owner).to_bytes32().into();
ewasm_api::storage_store(&storage_key, &value);
}
#[cfg(not(target_arch = "wasm32"))]
pub fn get_token_owner(_token_id: &[u8; 32]) -> Address {
Address::default()
}
#[cfg(target_arch = "wasm32")]
pub fn get_token_owner(token_id: &[u8; 32]) -> Address {
let storage_key = copy_into_storage_value(&calculate_token_hash(token_id));
let storage_value = ewasm_api::storage_load(&storage_key);
let bytes20: [u8; 20] = storage_value.bytes[12..32]
.try_into()
.expect("address should be bytes20");
bytes20.into()
}