diff --git a/src/sdk/decrypt.ts b/src/sdk/decrypt.ts index f663c4c..3ea9530 100644 --- a/src/sdk/decrypt.ts +++ b/src/sdk/decrypt.ts @@ -28,5 +28,14 @@ export const decryptAddress = ( keypair.publicKey, keypair.privateKey, ); - return getAddress(bytesToHex(decrypted)); + +let hexString = bytesToHex(decrypted); +// Ensure hexString forms a valid 40-digit Ethereum address. +// Truncate or pad with leading zeros as necessary to correct length issues. +if (hexString.length > 40) { + hexString = hexString.substring(hexString.length - 40); +} else { + hexString = hexString.padStart(40, '0'); +} + return getAddress(hexString); }; diff --git a/src/utils.ts b/src/utils.ts index 9a8dde9..7283a85 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -18,12 +18,8 @@ export const bytesToHex = function (byteArray: Uint8Array): string { if (!byteArray || byteArray?.length === 0) { return '0x0'; } - - const length = byteArray.length; - const buffer = Buffer.from(byteArray); - const result = buffer.toString('hex'); - + const result = '0x' + buffer.toString('hex'); return result; }; @@ -32,8 +28,6 @@ export const bytesToBigInt = function (byteArray: Uint8Array): bigint { return BigInt(0); } - const length = byteArray.length; - const buffer = Buffer.from(byteArray); const result = toBigIntBE(buffer);