Skip to content

Commit 0905bba

Browse files
authored
Fix: treat keccak256 string input as utf8 not hex (#32)
I did mistake and assumed that hex string input should be hex encoded bytes, but @noble which we are want to polyfill treats hex string as plain UTF8 string, not hex encoded bytes. Tests in separate PR: #33
1 parent c3a7a44 commit 0905bba

File tree

4 files changed

+8
-18
lines changed

4 files changed

+8
-18
lines changed

cpp/HybridNativeUtils.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,6 @@ static std::shared_ptr<ArrayBuffer> keccak256Hash(const uint8_t* dataBytes, size
133133
return result;
134134
}
135135

136-
std::shared_ptr<ArrayBuffer> HybridNativeUtils::keccak256(const std::string& data) {
137-
validateHexString(data);
138-
139-
size_t dataLen = data.length() / 2;
140-
141-
auto dataBuffer = ArrayBuffer::allocate(dataLen);
142-
uint8_t* dataBytes = static_cast<uint8_t*>(dataBuffer->data());
143-
144-
hexToBytes(data, dataBytes, dataLen);
145-
146-
return keccak256FromBytes(dataBuffer);
147-
}
148136
std::shared_ptr<ArrayBuffer> HybridNativeUtils::keccak256FromBytes(const std::shared_ptr<ArrayBuffer>& data) {
149137
// Get the data bytes
150138
const uint8_t* dataBytes = static_cast<const uint8_t*>(data->data());

cpp/HybridNativeUtils.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class HybridNativeUtils : public HybridNativeUtilsSpec {
1414
std::shared_ptr<ArrayBuffer> toPublicKeyFromBytes(const std::shared_ptr<ArrayBuffer>& privateKey, bool isCompressed) override;
1515
std::shared_ptr<ArrayBuffer> getPublicKeyEd25519(const std::string& privateKey) override;
1616
std::shared_ptr<ArrayBuffer> getPublicKeyEd25519FromBytes(const std::shared_ptr<ArrayBuffer>& privateKey) override;
17-
std::shared_ptr<ArrayBuffer> keccak256(const std::string& data) override;
1817
std::shared_ptr<ArrayBuffer> keccak256FromBytes(const std::shared_ptr<ArrayBuffer>& data) override;
1918
std::shared_ptr<ArrayBuffer> pubToAddress(const std::shared_ptr<ArrayBuffer>& pubKey, bool sanitize = false) override;
2019
std::shared_ptr<ArrayBuffer> hmacSha512(const std::shared_ptr<ArrayBuffer>& key, const std::shared_ptr<ArrayBuffer>& data) override;

src/NativeUtils.nitro.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export interface NativeUtils
1010
): ArrayBuffer;
1111
getPublicKeyEd25519(privateKey: string): ArrayBuffer;
1212
getPublicKeyEd25519FromBytes(privateKey: ArrayBuffer): ArrayBuffer;
13-
keccak256(data: string): ArrayBuffer;
1413
keccak256FromBytes(data: ArrayBuffer): ArrayBuffer;
1514
pubToAddress(pubKey: ArrayBuffer, sanitize: boolean): ArrayBuffer;
1615
hmacSha512(key: ArrayBuffer, data: ArrayBuffer): ArrayBuffer;

src/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ export function getPublicKey(
6666

6767
/**
6868
* Compute Keccak-256 hash using native implementation.
69+
* 100% compatible with @noble/hashes keccak_256 API.
6970
* Accepts multiple input types for maximum flexibility.
7071
*
71-
* @param data - The data to hash as string (hex), number[], ArrayBuffer, or Uint8Array
72+
* @param data - The data to hash as string (UTF-8), number[], ArrayBuffer, or Uint8Array
7273
* @returns Uint8Array containing the 32-byte Keccak-256 hash
7374
*/
7475
export function keccak256(
@@ -77,8 +78,11 @@ export function keccak256(
7778
let result: ArrayBuffer;
7879

7980
if (typeof data === 'string') {
80-
// Assume hex string, use the string version (C++ will handle hex validation)
81-
result = NativeUtilsHybridObject.keccak256(data);
81+
// Match noble's behavior: treat string as UTF-8 text, not hex
82+
const encoder = new TextEncoder();
83+
const bytes = encoder.encode(data);
84+
const buffer = uint8ArrayToArrayBuffer(bytes);
85+
result = NativeUtilsHybridObject.keccak256FromBytes(buffer);
8286
} else if (Array.isArray(data)) {
8387
// Convert number array to Uint8Array, then to ArrayBuffer
8488
const bytes = numberArrayToUint8Array(data);
@@ -93,7 +97,7 @@ export function keccak256(
9397
result = NativeUtilsHybridObject.keccak256FromBytes(buffer);
9498
} else {
9599
throw new Error(
96-
'Data must be a hex string, number[], ArrayBuffer, or Uint8Array',
100+
'Data must be a string, number[], ArrayBuffer, or Uint8Array',
97101
);
98102
}
99103

0 commit comments

Comments
 (0)