|
| 1 | +use bech32::{Bech32, Hrp}; |
| 2 | +use move_core_types::gas_algebra::NumBytes; |
| 3 | +use move_vm_runtime::native_functions::NativeFunction; |
| 4 | +use move_vm_types::{ |
| 5 | + loaded_data::runtime_types::Type, |
| 6 | + values::{Struct, Value}, |
| 7 | +}; |
| 8 | +use smallvec::{smallvec, SmallVec}; |
| 9 | +use std::collections::VecDeque; |
| 10 | + |
| 11 | +use crate::{ |
| 12 | + helpers::get_string, |
| 13 | + interface::{ |
| 14 | + RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeError, SafeNativeResult, |
| 15 | + }, |
| 16 | + safely_pop_arg, |
| 17 | +}; |
| 18 | + |
| 19 | +// See stdlib/error.move |
| 20 | +const ECATEGORY_INVALID_ARGUMENT: u64 = 0x1; |
| 21 | + |
| 22 | +// native errors always start from 100 |
| 23 | +const EUNABLE_TO_ENCODE: u64 = (ECATEGORY_INVALID_ARGUMENT << 16) + 100; |
| 24 | +const EUNABLE_TO_DECODE: u64 = (ECATEGORY_INVALID_ARGUMENT << 16) + 101; |
| 25 | +const EINVALID_PREFIX: u64 = (ECATEGORY_INVALID_ARGUMENT << 16) + 102; |
| 26 | +const EINVALID_ADDRESS: u64 = (ECATEGORY_INVALID_ARGUMENT << 16) + 103; |
| 27 | + |
| 28 | +/* |
| 29 | + native public fun encode(prefix: String, data: vector<u8>): String; |
| 30 | + native public fun decode(addr: String): (String, vector<u8>); |
| 31 | +*/ |
| 32 | + |
| 33 | +/*************************************************************************************************** |
| 34 | + * native fun encode |
| 35 | + * |
| 36 | + * gas cost: base_cost + unit_cost * (prefix_len + data_len) |
| 37 | + * |
| 38 | + **************************************************************************************************/ |
| 39 | +fn native_encode( |
| 40 | + context: &mut SafeNativeContext, |
| 41 | + ty_args: Vec<Type>, |
| 42 | + mut arguments: VecDeque<Value>, |
| 43 | +) -> SafeNativeResult<SmallVec<[Value; 1]>> { |
| 44 | + let gas_params = &context.native_gas_params.initia_stdlib; |
| 45 | + |
| 46 | + debug_assert!(ty_args.is_empty()); |
| 47 | + debug_assert_eq!(arguments.len(), 2); |
| 48 | + |
| 49 | + let data = safely_pop_arg!(arguments, Vec<u8>); |
| 50 | + let raw_prefix = get_string(safely_pop_arg!(arguments, Struct))?; |
| 51 | + let prefix = String::from_utf8(raw_prefix).map_err(|_| SafeNativeError::Abort { |
| 52 | + abort_code: EINVALID_PREFIX, |
| 53 | + })?; |
| 54 | + context.charge( |
| 55 | + gas_params.bech32_encode_base |
| 56 | + + gas_params.bech32_encode_unit * NumBytes::new((prefix.len() + data.len()) as u64), |
| 57 | + )?; |
| 58 | + |
| 59 | + let encoded_string = bech32::encode::<Bech32>( |
| 60 | + Hrp::parse(prefix.as_str()).map_err(|_| SafeNativeError::Abort { |
| 61 | + abort_code: EINVALID_PREFIX, |
| 62 | + })?, |
| 63 | + data.as_slice(), |
| 64 | + ) |
| 65 | + .map_err(|_| SafeNativeError::Abort { |
| 66 | + abort_code: EUNABLE_TO_ENCODE, |
| 67 | + })?; |
| 68 | + |
| 69 | + Ok(smallvec![Value::struct_(Struct::pack(vec![ |
| 70 | + Value::vector_u8(encoded_string.as_bytes().to_vec()), |
| 71 | + ]))]) |
| 72 | +} |
| 73 | + |
| 74 | +/*************************************************************************************************** |
| 75 | + * native fun decode |
| 76 | + * |
| 77 | + * gas cost: base_cost + unit_cost * address_len |
| 78 | + * |
| 79 | + **************************************************************************************************/ |
| 80 | +fn native_decode( |
| 81 | + context: &mut SafeNativeContext, |
| 82 | + ty_args: Vec<Type>, |
| 83 | + mut arguments: VecDeque<Value>, |
| 84 | +) -> SafeNativeResult<SmallVec<[Value; 1]>> { |
| 85 | + let gas_params = &context.native_gas_params.initia_stdlib; |
| 86 | + |
| 87 | + debug_assert!(ty_args.is_empty()); |
| 88 | + debug_assert_eq!(arguments.len(), 1); |
| 89 | + |
| 90 | + let raw_addr = get_string(safely_pop_arg!(arguments, Struct))?; |
| 91 | + let addr = String::from_utf8(raw_addr).map_err(|_| SafeNativeError::Abort { |
| 92 | + abort_code: EINVALID_ADDRESS, |
| 93 | + })?; |
| 94 | + |
| 95 | + context.charge( |
| 96 | + gas_params.bech32_decode_base |
| 97 | + + gas_params.bech32_decode_unit * NumBytes::new(addr.len() as u64), |
| 98 | + )?; |
| 99 | + |
| 100 | + let (prefix, words) = bech32::decode(addr.as_str()).map_err(|_| SafeNativeError::Abort { |
| 101 | + abort_code: EUNABLE_TO_DECODE, |
| 102 | + })?; |
| 103 | + |
| 104 | + Ok(smallvec![ |
| 105 | + Value::struct_(Struct::pack(vec![Value::vector_u8( |
| 106 | + prefix.as_bytes().to_vec() |
| 107 | + )])), |
| 108 | + Value::vector_u8(words) |
| 109 | + ]) |
| 110 | +} |
| 111 | + |
| 112 | +/*************************************************************************************************** |
| 113 | + * module |
| 114 | + * |
| 115 | + **************************************************************************************************/ |
| 116 | +pub fn make_all( |
| 117 | + builder: &SafeNativeBuilder, |
| 118 | +) -> impl Iterator<Item = (String, NativeFunction)> + '_ { |
| 119 | + let natives = [ |
| 120 | + ("encode", native_encode as RawSafeNative), |
| 121 | + ("decode", native_decode), |
| 122 | + ]; |
| 123 | + |
| 124 | + builder.make_named_natives(natives) |
| 125 | +} |
0 commit comments