add util_raddr, util_accid

This commit is contained in:
tequ
2025-09-29 13:55:23 +09:00
parent 4ff8d688e2
commit cf59180660
3 changed files with 35 additions and 13 deletions

View File

@@ -31,8 +31,11 @@ public:
// rollback // rollback
/// util APIs /// util APIs
// util_raddr Expected<std::string, HookReturnCode>
// util_accid util_raddr(Bytes const& accountID) const;
Expected<Bytes, HookReturnCode>
util_accid(std::string raddress) const;
Expected<bool, HookReturnCode> Expected<bool, HookReturnCode>
util_verify(Slice const& data, Slice const& sig, Slice const& key) const; util_verify(Slice const& data, Slice const& sig, Slice const& key) const;

View File

@@ -305,8 +305,24 @@ using namespace hook_float;
// rollback // rollback
/// util APIs /// util APIs
// util_raddr Expected<std::string, HookReturnCode>
// util_accid HookAPI::util_raddr(Bytes const& accountID) const
{
if (accountID.size() != 20)
return Unexpected(INVALID_ARGUMENT);
return encodeBase58Token(
TokenType::AccountID, accountID.data(), accountID.size());
}
Expected<Bytes, HookReturnCode>
HookAPI::util_accid(std::string raddress) const
{
auto const result = decodeBase58Token(raddress, TokenType::AccountID);
if (result.empty())
return Unexpected(INVALID_ARGUMENT);
return Bytes(result.data(), result.data() + result.size());
}
Expected<bool, HookReturnCode> Expected<bool, HookReturnCode>
HookAPI::util_verify(Slice const& data, Slice const& sig, Slice const& key) HookAPI::util_verify(Slice const& data, Slice const& sig, Slice const& key)

View File

@@ -3064,11 +3064,12 @@ DEFINE_HOOK_FUNCTION(
if (NOT_IN_BOUNDS(read_ptr, read_len, memory_length)) if (NOT_IN_BOUNDS(read_ptr, read_len, memory_length))
return OUT_OF_BOUNDS; return OUT_OF_BOUNDS;
if (read_len != 20) hook::HookAPI api(hookCtx);
return INVALID_ARGUMENT; auto const result =
api.util_raddr(Bytes{memory + read_ptr, memory + read_ptr + read_len});
std::string raddr = if (!result)
encodeBase58Token(TokenType::AccountID, memory + read_ptr, read_len); return result.error();
auto const& raddr = result.value();
if (write_len < raddr.size()) if (write_len < raddr.size())
return TOO_SMALL; return TOO_SMALL;
@@ -3119,12 +3120,14 @@ DEFINE_HOOK_FUNCTION(
std::string raddr{buffer}; std::string raddr{buffer};
auto const result = decodeBase58Token(raddr, TokenType::AccountID); hook::HookAPI api(hookCtx);
if (result.empty()) auto const result = api.util_accid(raddr);
return INVALID_ARGUMENT; if (!result)
return result.error();
auto const& accountID = result.value();
WRITE_WASM_MEMORY_AND_RETURN( WRITE_WASM_MEMORY_AND_RETURN(
write_ptr, write_len, result.data(), 20, memory, memory_length); write_ptr, write_len, accountID.data(), 20, memory, memory_length);
HOOK_TEARDOWN(); HOOK_TEARDOWN();
} }