forked from use-ink/ink
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathext.rs
More file actions
768 lines (700 loc) · 22.8 KB
/
ext.rs
File metadata and controls
768 lines (700 loc) · 22.8 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// Copyright 2018-2022 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! External C API to communicate with substrate contracts runtime module.
//!
//! Refer to substrate FRAME contract module for more documentation.
use crate::ReturnFlags;
use core::marker::PhantomData;
macro_rules! define_error_codes {
(
$(
$( #[$attr:meta] )*
$name:ident = $discr:literal,
)*
) => {
/// Every error that can be returned to a contract when it calls any of the host functions.
#[repr(u32)]
pub enum Error {
$(
$( #[$attr] )*
$name = $discr,
)*
/// Returns if an unknown error was received from the host module.
Unknown,
}
impl From<ReturnCode> for Result {
#[inline]
fn from(return_code: ReturnCode) -> Self {
match return_code.0 {
0 => Ok(()),
$(
$discr => Err(Error::$name),
)*
_ => Err(Error::Unknown),
}
}
}
};
}
define_error_codes! {
/// The called function trapped and has its state changes reverted.
/// In this case no output buffer is returned.
/// Can only be returned from `seal_call` and `seal_instantiate`.
CalleeTrapped = 1,
/// The called function ran to completion but decided to revert its state.
/// An output buffer is returned when one was supplied.
/// Can only be returned from `seal_call` and `seal_instantiate`.
CalleeReverted = 2,
/// The passed key does not exist in storage.
KeyNotFound = 3,
/// Deprecated and no longer returned: There is only the minimum balance.
_BelowSubsistenceThreshold = 4,
/// Transfer failed for other not further specified reason. Most probably
/// reserved or locked balance of the sender that was preventing the transfer.
TransferFailed = 5,
/// Deprecated and no longer returned: Endowment is no longer required.
_EndowmentTooLow = 6,
/// No code could be found at the supplied code hash.
CodeNotFound = 7,
/// The account that was called is no contract.
NotCallable = 8,
/// The call to `seal_debug_message` had no effect because debug message
/// recording was disabled.
LoggingDisabled = 9,
/// ECDSA public key recovery failed. Most probably wrong recovery id or signature.
EcdsaRecoveryFailed = 11,
}
/// Thin-wrapper around a `u32` representing a pointer for Wasm32.
///
/// Only for shared references.
///
/// # Note
///
/// Can only be constructed from shared reference types and encapsulates the
/// conversion from reference to raw `u32`.
/// Does not allow accessing the internal `u32` value.
#[derive(Debug)]
#[repr(transparent)]
pub struct Ptr32<'a, T>
where
T: ?Sized,
{
/// The internal Wasm32 raw pointer value.
///
/// Must not be readable or directly usable by any safe Rust code.
_value: u32,
/// We handle types like these as if the associated lifetime was exclusive.
marker: PhantomData<fn() -> &'a T>,
}
impl<'a, T> Ptr32<'a, T>
where
T: ?Sized,
{
/// Creates a new Wasm32 pointer for the given raw pointer value.
fn new(value: u32) -> Self {
Self {
_value: value,
marker: Default::default(),
}
}
}
impl<'a, T> Ptr32<'a, [T]> {
/// Creates a new Wasm32 pointer from the given shared slice.
pub fn from_slice(slice: &'a [T]) -> Self {
Self::new(slice.as_ptr() as u32)
}
}
/// Thin-wrapper around a `u32` representing a pointer for Wasm32.
///
/// Only for exclusive references.
///
/// # Note
///
/// Can only be constructed from exclusive reference types and encapsulates the
/// conversion from reference to raw `u32`.
/// Does not allow accessing the internal `u32` value.
#[derive(Debug)]
#[repr(transparent)]
pub struct Ptr32Mut<'a, T>
where
T: ?Sized,
{
/// The internal Wasm32 raw pointer value.
///
/// Must not be readable or directly usable by any safe Rust code.
_value: u32,
/// We handle types like these as if the associated lifetime was exclusive.
marker: PhantomData<fn() -> &'a mut T>,
}
impl<'a, T> Ptr32Mut<'a, T>
where
T: ?Sized,
{
/// Creates a new Wasm32 pointer for the given raw pointer value.
fn new(value: u32) -> Self {
Self {
_value: value,
marker: Default::default(),
}
}
}
impl<'a, T> Ptr32Mut<'a, [T]> {
/// Creates a new Wasm32 pointer from the given exclusive slice.
pub fn from_slice(slice: &'a mut [T]) -> Self {
Self::new(slice.as_ptr() as u32)
}
}
impl<'a, T> Ptr32Mut<'a, T>
where
T: Sized,
{
/// Creates a new Wasm32 pointer from the given exclusive reference.
pub fn from_ref(a_ref: &'a mut T) -> Self {
let a_ptr: *mut T = a_ref;
Self::new(a_ptr as u32)
}
}
/// Used as a sentinel value when reading and writing contract memory.
///
/// We use this value to signal `None` to a contract when only a primitive is allowed
/// and we don't want to go through encoding a full Rust type. Using `u32::Max` is a safe
/// sentinel because contracts are never allowed to use such a large amount of resources.
/// So this value doesn't make sense for a memory location or length.
const SENTINEL: u32 = u32::MAX;
/// The raw return code returned by the host side.
#[repr(transparent)]
pub struct ReturnCode(u32);
impl From<ReturnCode> for Option<u32> {
fn from(code: ReturnCode) -> Self {
(code.0 < SENTINEL).then(|| code.0)
}
}
impl ReturnCode {
/// Returns the raw underlying `u32` representation.
pub fn into_u32(self) -> u32 {
self.0
}
/// Returns the underlying `u32` converted into `bool`.
pub fn into_bool(self) -> bool {
self.0.ne(&0)
}
}
type Result = core::result::Result<(), Error>;
mod sys {
use super::{
Ptr32,
Ptr32Mut,
ReturnCode,
};
#[link(wasm_import_module = "seal0")]
extern "C" {
pub fn seal_transfer(
account_id_ptr: Ptr32<[u8]>,
account_id_len: u32,
transferred_value_ptr: Ptr32<[u8]>,
transferred_value_len: u32,
) -> ReturnCode;
pub fn seal_deposit_event(
topics_ptr: Ptr32<[u8]>,
topics_len: u32,
data_ptr: Ptr32<[u8]>,
data_len: u32,
);
pub fn seal_get_storage(
key_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;
pub fn seal_contains_storage(key_ptr: Ptr32<[u8]>) -> ReturnCode;
pub fn seal_clear_storage(key_ptr: Ptr32<[u8]>);
pub fn seal_call_chain_extension(
func_id: u32,
input_ptr: Ptr32<[u8]>,
input_len: u32,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;
pub fn seal_input(buf_ptr: Ptr32Mut<[u8]>, buf_len_ptr: Ptr32Mut<u32>);
pub fn seal_return(flags: u32, data_ptr: Ptr32<[u8]>, data_len: u32) -> !;
pub fn seal_caller(output_ptr: Ptr32Mut<[u8]>, output_len_ptr: Ptr32Mut<u32>);
pub fn seal_block_number(
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);
pub fn seal_address(output_ptr: Ptr32Mut<[u8]>, output_len_ptr: Ptr32Mut<u32>);
pub fn seal_balance(output_ptr: Ptr32Mut<[u8]>, output_len_ptr: Ptr32Mut<u32>);
pub fn seal_weight_to_fee(
gas: u64,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);
pub fn seal_gas_left(output_ptr: Ptr32Mut<[u8]>, output_len_ptr: Ptr32Mut<u32>);
pub fn seal_value_transferred(
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);
pub fn seal_now(output_ptr: Ptr32Mut<[u8]>, output_len_ptr: Ptr32Mut<u32>);
pub fn seal_minimum_balance(
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);
pub fn seal_hash_keccak_256(
input_ptr: Ptr32<[u8]>,
input_len: u32,
output_ptr: Ptr32Mut<[u8]>,
);
pub fn seal_hash_blake2_256(
input_ptr: Ptr32<[u8]>,
input_len: u32,
output_ptr: Ptr32Mut<[u8]>,
);
pub fn seal_hash_blake2_128(
input_ptr: Ptr32<[u8]>,
input_len: u32,
output_ptr: Ptr32Mut<[u8]>,
);
pub fn seal_hash_sha2_256(
input_ptr: Ptr32<[u8]>,
input_len: u32,
output_ptr: Ptr32Mut<[u8]>,
);
pub fn seal_is_contract(account_id_ptr: Ptr32<[u8]>) -> ReturnCode;
pub fn seal_caller_is_origin() -> ReturnCode;
pub fn seal_set_code_hash(code_hash_ptr: Ptr32<[u8]>) -> ReturnCode;
pub fn seal_code_hash(
account_id_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;
pub fn seal_own_code_hash(
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);
#[cfg(feature = "ink-debug")]
pub fn seal_debug_message(str_ptr: Ptr32<[u8]>, str_len: u32) -> ReturnCode;
pub fn seal_delegate_call(
flags: u32,
code_hash_ptr: Ptr32<[u8]>,
input_data_ptr: Ptr32<[u8]>,
input_data_len: u32,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;
}
#[link(wasm_import_module = "seal1")]
extern "C" {
pub fn seal_instantiate(
init_code_ptr: Ptr32<[u8]>,
gas: u64,
endowment_ptr: Ptr32<[u8]>,
input_ptr: Ptr32<[u8]>,
input_len: u32,
address_ptr: Ptr32Mut<[u8]>,
address_len_ptr: Ptr32Mut<u32>,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
salt_ptr: Ptr32<[u8]>,
salt_len: u32,
) -> ReturnCode;
pub fn seal_terminate(beneficiary_ptr: Ptr32<[u8]>) -> !;
pub fn seal_random(
subject_ptr: Ptr32<[u8]>,
subject_len: u32,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);
pub fn seal_call(
flags: u32,
callee_ptr: Ptr32<[u8]>,
gas: u64,
transferred_value_ptr: Ptr32<[u8]>,
input_data_ptr: Ptr32<[u8]>,
input_data_len: u32,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;
pub fn seal_set_storage(
key_ptr: Ptr32<[u8]>,
value_ptr: Ptr32<[u8]>,
value_len: u32,
) -> ReturnCode;
}
#[link(wasm_import_module = "__unstable__")]
extern "C" {
pub fn seal_ecdsa_recover(
// 65 bytes of ecdsa signature
signature_ptr: Ptr32<[u8]>,
// 32 bytes hash of the message
message_hash_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
) -> ReturnCode;
pub fn seal_ecdsa_to_eth_address(
public_key_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
) -> ReturnCode;
}
}
#[inline(always)]
fn extract_from_slice(output: &mut &mut [u8], new_len: usize) {
debug_assert!(new_len <= output.len());
let tmp = core::mem::take(output);
*output = &mut tmp[..new_len];
}
#[inline(always)]
pub fn instantiate(
code_hash: &[u8],
gas_limit: u64,
endowment: &[u8],
input: &[u8],
out_address: &mut &mut [u8],
out_return_value: &mut &mut [u8],
salt: &[u8],
) -> Result {
let mut address_len = out_address.len() as u32;
let mut return_value_len = out_return_value.len() as u32;
let ret_code = {
unsafe {
sys::seal_instantiate(
Ptr32::from_slice(code_hash),
gas_limit,
Ptr32::from_slice(endowment),
Ptr32::from_slice(input),
input.len() as u32,
Ptr32Mut::from_slice(out_address),
Ptr32Mut::from_ref(&mut address_len),
Ptr32Mut::from_slice(out_return_value),
Ptr32Mut::from_ref(&mut return_value_len),
Ptr32::from_slice(salt),
salt.len() as u32,
)
}
};
extract_from_slice(out_address, address_len as usize);
extract_from_slice(out_return_value, return_value_len as usize);
ret_code.into()
}
#[inline(always)]
pub fn call(
flags: u32,
callee: &[u8],
gas_limit: u64,
value: &[u8],
input: &[u8],
output: &mut &mut [u8],
) -> Result {
let mut output_len = output.len() as u32;
let ret_code = {
unsafe {
sys::seal_call(
flags,
Ptr32::from_slice(callee),
gas_limit,
Ptr32::from_slice(value),
Ptr32::from_slice(input),
input.len() as u32,
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
}
};
extract_from_slice(output, output_len as usize);
ret_code.into()
}
#[inline(always)]
pub fn delegate_call(
flags: u32,
code_hash: &[u8],
input: &[u8],
output: &mut &mut [u8],
) -> Result {
let mut output_len = output.len() as u32;
let ret_code = {
unsafe {
sys::seal_delegate_call(
flags,
Ptr32::from_slice(code_hash),
Ptr32::from_slice(input),
input.len() as u32,
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
}
};
extract_from_slice(output, output_len as usize);
ret_code.into()
}
pub fn transfer(account_id: &[u8], value: &[u8]) -> Result {
let ret_code = unsafe {
sys::seal_transfer(
Ptr32::from_slice(account_id),
account_id.len() as u32,
Ptr32::from_slice(value),
value.len() as u32,
)
};
ret_code.into()
}
pub fn deposit_event(topics: &[u8], data: &[u8]) {
unsafe {
sys::seal_deposit_event(
Ptr32::from_slice(topics),
topics.len() as u32,
Ptr32::from_slice(data),
data.len() as u32,
)
}
}
pub fn set_storage(key: &[u8], encoded_value: &[u8]) -> Option<u32> {
let ret_code = unsafe {
sys::seal_set_storage(
Ptr32::from_slice(key),
Ptr32::from_slice(encoded_value),
encoded_value.len() as u32,
)
};
ret_code.into()
}
pub fn clear_storage(key: &[u8]) {
unsafe { sys::seal_clear_storage(Ptr32::from_slice(key)) }
}
#[inline(always)]
pub fn get_storage(key: &[u8], output: &mut &mut [u8]) -> Result {
let mut output_len = output.len() as u32;
let ret_code = {
unsafe {
sys::seal_get_storage(
Ptr32::from_slice(key),
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
}
};
extract_from_slice(output, output_len as usize);
ret_code.into()
}
pub fn storage_contains(key: &[u8]) -> Option<u32> {
let ret_code = unsafe { sys::seal_contains_storage(Ptr32::from_slice(key)) };
ret_code.into()
}
pub fn terminate(beneficiary: &[u8]) -> ! {
unsafe { sys::seal_terminate(Ptr32::from_slice(beneficiary)) }
}
#[inline(always)]
pub fn call_chain_extension(func_id: u32, input: &[u8], output: &mut &mut [u8]) -> u32 {
let mut output_len = output.len() as u32;
let ret_code = {
unsafe {
sys::seal_call_chain_extension(
func_id,
Ptr32::from_slice(input),
input.len() as u32,
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
}
};
extract_from_slice(output, output_len as usize);
ret_code.into_u32()
}
#[inline(always)]
pub fn input(output: &mut &mut [u8]) {
let mut output_len = output.len() as u32;
{
unsafe {
sys::seal_input(
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
};
}
extract_from_slice(output, output_len as usize);
}
pub fn return_value(flags: ReturnFlags, return_value: &[u8]) -> ! {
unsafe {
sys::seal_return(
flags.into_u32(),
Ptr32::from_slice(return_value),
return_value.len() as u32,
)
}
}
macro_rules! impl_seal_wrapper_for {
( $( ($name:ident => $seal_name:ident), )* ) => {
$(
#[inline(always)]
pub fn $name(output: &mut &mut [u8]) {
let mut output_len = output.len() as u32;
{
unsafe {
sys::$seal_name(
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
};
}
// We don't need `extract_from_slice` here because it is stored in the own array
// in `get_property_little_endian` and `get_property_inplace`
}
)*
}
}
impl_seal_wrapper_for! {
(caller => seal_caller),
(block_number => seal_block_number),
(address => seal_address),
(balance => seal_balance),
(gas_left => seal_gas_left),
(value_transferred => seal_value_transferred),
(now => seal_now),
(minimum_balance => seal_minimum_balance),
}
#[inline(always)]
pub fn weight_to_fee(gas: u64, output: &mut &mut [u8]) {
let mut output_len = output.len() as u32;
{
unsafe {
sys::seal_weight_to_fee(
gas,
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
};
}
extract_from_slice(output, output_len as usize);
}
#[inline(always)]
pub fn random(subject: &[u8], output: &mut &mut [u8]) {
let mut output_len = output.len() as u32;
{
unsafe {
sys::seal_random(
Ptr32::from_slice(subject),
subject.len() as u32,
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
};
}
extract_from_slice(output, output_len as usize);
}
#[cfg(feature = "ink-debug")]
/// Call `seal_debug_message` with the supplied UTF-8 encoded message.
///
/// If debug message recording is disabled in the contracts pallet, the first call will
/// return a `LoggingDisabled` error, and further calls will be a no-op to avoid the cost
/// of calling into the supervisor.
///
/// # Note
///
/// This depends on the `seal_debug_message` interface which requires the
/// `"pallet-contracts/unstable-interface"` feature to be enabled in the target runtime.
pub fn debug_message(message: &str) {
static mut DEBUG_ENABLED: bool = false;
static mut FIRST_RUN: bool = true;
// SAFETY: safe because executing in a single threaded context
// We need those two variables in order to make sure that the assignment is performed
// in the "logging enabled" case. This is because during RPC execution logging might
// be enabled while it is disabled during the actual execution as part of a transaction.
// The gas estimation takes place during RPC execution. We want to overestimate instead
// of underestimate gas usage. Otherwise using this estimate could lead to a out of gas error.
if unsafe { DEBUG_ENABLED || FIRST_RUN } {
let bytes = message.as_bytes();
let ret_code = unsafe {
sys::seal_debug_message(Ptr32::from_slice(bytes), bytes.len() as u32)
};
if !matches!(ret_code.into(), Err(Error::LoggingDisabled)) {
// SAFETY: safe because executing in a single threaded context
unsafe { DEBUG_ENABLED = true }
}
// SAFETY: safe because executing in a single threaded context
unsafe { FIRST_RUN = false }
}
}
#[cfg(not(feature = "ink-debug"))]
/// A no-op. Enable the `ink-debug` feature for debug messages.
pub fn debug_message(_message: &str) {}
macro_rules! impl_hash_fn {
( $name:ident, $bytes_result:literal ) => {
paste::item! {
pub fn [<hash_ $name>](input: &[u8], output: &mut [u8; $bytes_result]) {
unsafe {
sys::[<seal_hash_ $name>](
Ptr32::from_slice(input),
input.len() as u32,
Ptr32Mut::from_slice(output),
)
}
}
}
};
}
impl_hash_fn!(sha2_256, 32);
impl_hash_fn!(keccak_256, 32);
impl_hash_fn!(blake2_256, 32);
impl_hash_fn!(blake2_128, 16);
pub fn ecdsa_recover(
signature: &[u8; 65],
message_hash: &[u8; 32],
output: &mut [u8; 33],
) -> Result {
let ret_code = unsafe {
sys::seal_ecdsa_recover(
Ptr32::from_slice(signature),
Ptr32::from_slice(message_hash),
Ptr32Mut::from_slice(output),
)
};
ret_code.into()
}
pub fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result {
let ret_code = unsafe {
sys::seal_ecdsa_to_eth_address(
Ptr32::from_slice(pubkey),
Ptr32Mut::from_slice(output),
)
};
ret_code.into()
}
pub fn is_contract(account_id: &[u8]) -> bool {
let ret_val = unsafe { sys::seal_is_contract(Ptr32::from_slice(account_id)) };
ret_val.into_bool()
}
pub fn caller_is_origin() -> bool {
let ret_val = unsafe { sys::seal_caller_is_origin() };
ret_val.into_bool()
}
pub fn set_code_hash(code_hash: &[u8]) -> Result {
let ret_val = unsafe { sys::seal_set_code_hash(Ptr32::from_slice(code_hash)) };
ret_val.into()
}
pub fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result {
let mut output_len = output.len() as u32;
let ret_val = unsafe {
sys::seal_code_hash(
Ptr32::from_slice(account_id),
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
};
ret_val.into()
}
pub fn own_code_hash(output: &mut [u8]) {
let mut output_len = output.len() as u32;
unsafe {
sys::seal_own_code_hash(
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
}
}