diff --git a/frameworks/moveos-stdlib/doc/bcs.md b/frameworks/moveos-stdlib/doc/bcs.md index c904cffb07..d2e2fd7704 100644 --- a/frameworks/moveos-stdlib/doc/bcs.md +++ b/frameworks/moveos-stdlib/doc/bcs.md @@ -16,6 +16,32 @@ Note we provie a generic public from_bytes function and protected i - [Function `to_u64`](#0x2_bcs_to_u64) - [Function `to_u128`](#0x2_bcs_to_u128) - [Function `to_address`](#0x2_bcs_to_address) +- [Function `peel_address`](#0x2_bcs_peel_address) +- [Function `peel_bool`](#0x2_bcs_peel_bool) +- [Function `peel_u8`](#0x2_bcs_peel_u8) +- [Function `peel_u16`](#0x2_bcs_peel_u16) +- [Function `peel_u32`](#0x2_bcs_peel_u32) +- [Function `peel_u64`](#0x2_bcs_peel_u64) +- [Function `peel_u128`](#0x2_bcs_peel_u128) +- [Function `peel_u256`](#0x2_bcs_peel_u256) +- [Function `peel_vec_length`](#0x2_bcs_peel_vec_length) +- [Function `peel_vec_address`](#0x2_bcs_peel_vec_address) +- [Function `peel_vec_bool`](#0x2_bcs_peel_vec_bool) +- [Function `peel_vec_u8`](#0x2_bcs_peel_vec_u8) +- [Function `peel_vec_vec_u8`](#0x2_bcs_peel_vec_vec_u8) +- [Function `peel_vec_u16`](#0x2_bcs_peel_vec_u16) +- [Function `peel_vec_u32`](#0x2_bcs_peel_vec_u32) +- [Function `peel_vec_u64`](#0x2_bcs_peel_vec_u64) +- [Function `peel_vec_u128`](#0x2_bcs_peel_vec_u128) +- [Function `peel_vec_u256`](#0x2_bcs_peel_vec_u256) +- [Function `peel_option_address`](#0x2_bcs_peel_option_address) +- [Function `peel_option_bool`](#0x2_bcs_peel_option_bool) +- [Function `peel_option_u8`](#0x2_bcs_peel_option_u8) +- [Function `peel_option_u16`](#0x2_bcs_peel_option_u16) +- [Function `peel_option_u32`](#0x2_bcs_peel_option_u32) +- [Function `peel_option_u64`](#0x2_bcs_peel_option_u64) +- [Function `peel_option_u128`](#0x2_bcs_peel_option_u128) +- [Function `peel_option_u256`](#0x2_bcs_peel_option_u256) - [Function `from_bytes`](#0x2_bcs_from_bytes) - [Function `from_bytes_option`](#0x2_bcs_from_bytes_option) - [Function `native_from_bytes`](#0x2_bcs_native_from_bytes) @@ -32,6 +58,15 @@ Note we provie a generic public from_bytes function and protected i ## Constants + + + + +
const ErrorInvalidBool: u64 = 4;
+
+ + + @@ -41,6 +76,33 @@ Note we provie a generic public from_bytes function and protected i + + + + +
const ErrorInvalidLength: u64 = 3;
+
+ + + + + + + +
const ErrorLengthOutOfRange: u64 = 6;
+
+ + + + + + + +
const ErrorOutOfRange: u64 = 5;
+
+ + + The request Move type is not match with input Move type. @@ -117,6 +179,322 @@ The request Move type is not match with input Move type. + + +## Function `peel_address` + +Read address value from the bcs-serialized bytes. + + +
public fun peel_address(v: vector<u8>): address
+
+ + + + + +## Function `peel_bool` + +Read a bool value from bcs-serialized bytes. + + +
public fun peel_bool(v: vector<u8>): bool
+
+ + + + + +## Function `peel_u8` + +Read u8 value from bcs-serialized bytes. + + +
public fun peel_u8(v: vector<u8>): u8
+
+ + + + + +## Function `peel_u16` + +Read u16 value from bcs-serialized bytes. + + +
public fun peel_u16(v: vector<u8>): u16
+
+ + + + + +## Function `peel_u32` + +Read u32 value from bcs-serialized bytes. + + +
public fun peel_u32(v: vector<u8>): u32
+
+ + + + + +## Function `peel_u64` + +Read u64 value from bcs-serialized bytes. + + +
public fun peel_u64(v: vector<u8>): u64
+
+ + + + + +## Function `peel_u128` + +Read u128 value from bcs-serialized bytes. + + +
public fun peel_u128(v: vector<u8>): u128
+
+ + + + + +## Function `peel_u256` + +Read u256 value from bcs-serialized bytes. + + +
public fun peel_u256(v: vector<u8>): u256
+
+ + + + + +## Function `peel_vec_length` + +Read ULEB bytes expecting a vector length. Result should +then be used to perform peel_* operation LEN times. + +In BCS vector length is implemented with ULEB128; +See more here: https://en.wikipedia.org/wiki/LEB128 + + +
public fun peel_vec_length(v: vector<u8>): u64
+
+ + + + + +## Function `peel_vec_address` + +Peel a vector of address from serialized bytes. + + +
public fun peel_vec_address(v: vector<u8>): vector<address>
+
+ + + + + +## Function `peel_vec_bool` + +Peel a vector of address from serialized bytes. + + +
public fun peel_vec_bool(v: vector<u8>): vector<bool>
+
+ + + + + +## Function `peel_vec_u8` + +Peel a vector of u8 (eg string) from serialized bytes. + + +
public fun peel_vec_u8(v: vector<u8>): vector<u8>
+
+ + + + + +## Function `peel_vec_vec_u8` + +Peel a vector<vector<u8>> (eg vec of string) from serialized bytes. + + +
public fun peel_vec_vec_u8(v: vector<u8>): vector<vector<u8>>
+
+ + + + + +## Function `peel_vec_u16` + +Peel a vector of u16 from serialized bytes. + + +
public fun peel_vec_u16(v: vector<u8>): vector<u16>
+
+ + + + + +## Function `peel_vec_u32` + +Peel a vector of u32 from serialized bytes. + + +
public fun peel_vec_u32(v: vector<u8>): vector<u32>
+
+ + + + + +## Function `peel_vec_u64` + +Peel a vector of u64 from serialized bytes. + + +
public fun peel_vec_u64(v: vector<u8>): vector<u64>
+
+ + + + + +## Function `peel_vec_u128` + +Peel a vector of u128 from serialized bytes. + + +
public fun peel_vec_u128(v: vector<u8>): vector<u128>
+
+ + + + + +## Function `peel_vec_u256` + +Peel a vector of u256 from serialized bytes. + + +
public fun peel_vec_u256(v: vector<u8>): vector<u256>
+
+ + + + + +## Function `peel_option_address` + +Peel Option<address> from serialized bytes. + + +
public fun peel_option_address(v: vector<u8>): option::Option<address>
+
+ + + + + +## Function `peel_option_bool` + +Peel Option<bool> from serialized bytes. + + +
public fun peel_option_bool(v: vector<u8>): option::Option<bool>
+
+ + + + + +## Function `peel_option_u8` + +Peel Option<u8> from serialized bytes. + + +
public fun peel_option_u8(v: vector<u8>): option::Option<u8>
+
+ + + + + +## Function `peel_option_u16` + +Peel Option<u16> from serialized bytes. + + +
public fun peel_option_u16(v: vector<u8>): option::Option<u16>
+
+ + + + + +## Function `peel_option_u32` + +Peel Option<u32> from serialized bytes. + + +
public fun peel_option_u32(v: vector<u8>): option::Option<u32>
+
+ + + + + +## Function `peel_option_u64` + +Peel Option<u64> from serialized bytes. + + +
public fun peel_option_u64(v: vector<u8>): option::Option<u64>
+
+ + + + + +## Function `peel_option_u128` + +Peel Option<u128> from serialized bytes. + + +
public fun peel_option_u128(v: vector<u8>): option::Option<u128>
+
+ + + + + +## Function `peel_option_u256` + +Peel Option<u256> from serialized bytes. + + +
public fun peel_option_u256(v: vector<u8>): option::Option<u256>
+
+ + + ## Function `from_bytes` diff --git a/frameworks/moveos-stdlib/sources/bcs.move b/frameworks/moveos-stdlib/sources/bcs.move index 2e54319fea..0fe9f05f1b 100644 --- a/frameworks/moveos-stdlib/sources/bcs.move +++ b/frameworks/moveos-stdlib/sources/bcs.move @@ -9,11 +9,11 @@ module moveos_std::bcs{ use std::option::{Self, Option}; + use std::vector; friend moveos_std::any; friend moveos_std::copyable_any; - friend moveos_std::address; - + /// The request Move type is not match with input Move type. const ErrorTypeNotMatch: u64 = 1; const ErrorInvalidBytes: u64 = 2; @@ -48,10 +48,10 @@ module moveos_std::bcs{ /// Read `address` value from the bcs-serialized bytes. public fun peel_address(v: vector): address { - assert!(vector::length(&v) >= address::length(), ErrorInvalidLength); + assert!(vector::length(&v) >= 32, ErrorInvalidLength); let i = 0; let addr_bytes = vector::empty(); - while (i < address::length()) { + while (i < 32) { let byte = vector::pop_back(&mut v); vector::push_back(&mut addr_bytes, byte); i = i + 1; @@ -84,7 +84,7 @@ module moveos_std::bcs{ let i = 0; let bits = 16u8; while (i < bits) { - let byte = vector::pop_back(&mut v); + let byte = (vector::pop_back(&mut v) as u16); value = value + (byte << (i as u8)); i = i + 8; }; @@ -99,7 +99,7 @@ module moveos_std::bcs{ let i = 0; let bits = 32u8; while (i < bits) { - let byte = vector::pop_back(&mut v); + let byte = (vector::pop_back(&mut v) as u32); value = value + (byte << (i as u8)); i = i + 8; }; @@ -114,7 +114,7 @@ module moveos_std::bcs{ let i = 0; let bits = 64u8; while (i < bits) { - let byte = vector::pop_back(&mut v); + let byte = (vector::pop_back(&mut v) as u64); value = value + (byte << (i as u8)); i = i + 8; }; @@ -129,7 +129,7 @@ module moveos_std::bcs{ let i = 0; let bits = 128u8; while (i < bits) { - let byte = vector::pop_back(&mut v); + let byte = (vector::pop_back(&mut v) as u128); value = value + (byte << (i as u8)); i = i + 8; }; @@ -144,7 +144,7 @@ module moveos_std::bcs{ let i = 0; let bits = 256u16; while (i < bits) { - let byte = vector::pop_back(&mut v); + let byte = (vector::pop_back(&mut v) as u256); value = value + (byte << (i as u8)); i = i + 8; }; @@ -165,12 +165,12 @@ module moveos_std::bcs{ let len = 0; loop { assert!(len <= 4, ErrorLengthOutOfRange); - let byte = vector::pop_back(&mut v) as u64; + let byte = (vector::pop_back(&mut v) as u64); len = len + 1; total = total | ((byte & 0x7f) << shift); if ((byte & 0x80) == 0) { - break; - } + break + }; shift = shift + 7; }; total