Xahau / xahaud

Codebase for Xahaud - The consensus, RPC & blockchain app for the Xahau network.
https://xahau.network
ISC License
22 stars 11 forks source link

Include JS TextEncoder for string conversion #334

Open WietseWind opened 1 month ago

WietseWind commented 1 month ago

For easy (cheaper) encoding from uint8 to UTF8 (and then to hex if needed) and vice versa, the TextEncoder would be used in node (eliminates the need for the larger Buffer, eliminates the need for doing all these things in added JS functions which is less efficient).

There's a third party module for the QuickJS engine adding TextEncoder support: https://github.com/rsenn/qjs-modules/blob/main/quickjs-textcode.h https://github.com/rsenn/qjs-modules/blob/main/quickjs-textcode.c

I propose we include it, so we can (under the hood, in helper lib, or directly by devs) allow for:

function utf8ToHex(str) {
    // Create a new TextEncoder instance
    const encoder = new TextEncoder();

    // Encode the string into a Uint8Array
    const bytes = encoder.encode(str);

    // Convert each byte to a hexadecimal string
    const hexArray = Array.from(bytes, byte => {
        return byte.toString(16).padStart(2, '0');
    });

    // Join the hexadecimal strings together
    return hexArray.join('');
}

// Example usage
const utf8String = "Hello, World!";
const hexString = utf8ToHex(utf8String);
console.log(hexString);  // Output: "48656c6c6f2c20576f726c6421"

There are also more interesting fns to consider adding here, e.g. the atob / btoa / buffer methods. It's worth considering adding some of these to the C of the JS Hook side instead of more expensive JS implementations.

https://github.com/rsenn/qjs-modules/blob/main/quickjs-misc.c