FrankSzendzielarz / AlgorandVisualStudio

Visual Studio extensions for C# TEAL compilation and Algorand Smart Contract development
MIT License
21 stars 2 forks source link

Feature Request: Add Functions for Byte String Manipulation and Casting to and from the Appropriate Int Type #14

Closed mangoplane closed 1 year ago

mangoplane commented 1 year ago

In the process of implementing smart contracts, it was observed that there's a need for additional functions for byte string manipulation and casting. These additional functions would facilitate easier extraction and casting of byte arrays to the appropriate data types, in this case, UINT64/int.

Current implementations, such as BigIntegerFromByteArray(byte[] bytes) and UBigIntegerFromByteArray(byte[] bytes), do not completely meet the requirements for this use case.

The following is an example of a potential use case where these functions would be beneficial:

byte[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int start = 4; // starting index
int length = 4; // number of bytes to copy

byte[] destination = new byte[length];
Array.Copy(source, start, destination, 0, length);

In the above example, the goal is to extract a segment of a byte array and cast it to UINT64 if supported. As of now, this requires external splitting and internal combining for hashing and checking if it has been signed with the provided signatures.

A proposed function bytesToUInt64(bytes[] b) could be added. This function would assume a big endian unsigned byte array and if b.length != 8, an error could be thrown during compilation.

Furthermore, functions like btoi(bytes) in PyTeal could be referenced for implementation inspiration.

However, it's important to note that the addition of these functions should maintain the high-level language nature of the implementation, and the ability to handle exceptions gracefully, unlike TEAL which just fails.

E.g. https://pyteal.readthedocs.io/en/stable/arithmetic_expression.html https://pyteal.readthedocs.io/en/stable/byte_expression.html#extract https://pyteal.readthedocs.io/en/stable/data_type.html

Kind Regards & thanks

Disclaimer: ChatGPT4 wrote this issue from a Discord chat dialogue. TLDR: Add Functions for Byte String Manipulation and Casting to and from the Appropriate Int Type, similar to PyTeal.

FrankSzendzielarz commented 1 year ago

Just a quick note - Byte[] extensions supported by the tooling include :


       public static byte[] Concat(this byte[] a, byte[] b) 
        public static AccountReference ToAccountReference(this byte[] a) 
        public static byte[] Part(this byte[] a,uint start, uint end) 
        public static void Init(this byte[] a, [Literal]byte b, [Literal] uint l) 
        /// <param name="a"></param>
        /// <param name="b">Position to replace from</param>
        /// <param name="c">Bytes to replace with</param>
        /// <exception cref="IntentionallyNotImplementedException"></exception>
        public static void Replace(this byte[] a,  byte b, byte[] c) 
        public static string ToString(this byte[] a) 
        public static int BitLen(this byte[] a) 
             public static int GetBit(this byte[] a, ulong b) 

There are some others like the UbigInteger stuff on the SmartContract/Sig base class too.

Will add the btoi shortly (to ToTealBytes() is already defined on ulong)

FrankSzendzielarz commented 1 year ago

In addition to above note , byte[] now has ToTealUlong()

:-) Thanks!

FrankSzendzielarz commented 1 year ago

PS: Updated https://github.com/FrankSzendzielarz/AlgorandVisualStudio/blob/main/ContractDevelopment/PredefinedFunctions.md