js util_sha512h

This commit is contained in:
Richard Holland
2024-05-06 12:20:00 +10:00
committed by RichardAH
parent 7e12b46169
commit 38a4b3c1b9
2 changed files with 34 additions and 4 deletions

View File

@@ -177,6 +177,12 @@ DECLARE_WASM_FUNCTION(
uint32_t write_len,
uint32_t read_ptr,
uint32_t read_len);
DECLARE_JS_FUNCTION(
JSValue,
util_sha512h,
JSValue data);
DECLARE_WASM_FUNCTION(
int64_t,
util_keylet,
@@ -999,8 +1005,8 @@ public:
ADD_JS_FUNCTION(util_raddr, ctx);
ADD_JS_FUNCTION(util_accid, ctx);
ADD_JS_FUNCTION(util_verify, ctx);
/*
ADD_JS_FUNCTION(util_sha512h, ctx);
/*
ADD_JS_FUNCTION(sto_validate, ctx);
ADD_JS_FUNCTION(sto_subfield, ctx);
ADD_JS_FUNCTION(sto_subarray, ctx);

View File

@@ -786,9 +786,9 @@ FromJSString(JSContext* ctx, JSValueConst& v, int max_len)
template <typename T>
inline
std::optional<JSValue>
ToJSIntArray(JSContext* ctx, std::vector<T>& vec)
ToJSIntArray(JSContext* ctx, T& vec)
{
if (vec.size() > 1024)
if (vec.size() > 65535)
return {};
JSValue out = JS_NewArray(ctx);
@@ -796,7 +796,7 @@ ToJSIntArray(JSContext* ctx, std::vector<T>& vec)
return {};
int i = 0;
for (T& x: vec)
for (auto& x: vec)
JS_DefinePropertyValueUint32(ctx, out, i++, JS_NewInt32(ctx, x), JS_PROP_C_W_E);
return out;
@@ -4101,6 +4101,30 @@ DEFINE_WASM_FUNCTION(
WASM_HOOK_TEARDOWN();
}
DEFINE_JS_FUNCTION(
JSValue,
util_sha512h,
JSValue data)
{
JS_HOOK_SETUP();
auto vec = FromJSIntArrayOrHexString(ctx, data, 65536);
if (!vec)
returnJS(INVALID_ARGUMENT);
auto hash = ripple::sha512Half(ripple::Slice{vec->data(), vec->size()});
auto ret = ToJSIntArray(ctx, hash);
if (!ret)
returnJS(INTERNAL_ERROR);
return *ret;
JS_HOOK_TEARDOWN();
}
// these are only used by get_stobject_length below
enum parse_error : int32_t {
pe_unexpected_end = -1,