Skip to content

Commit

Permalink
Merge pull request #60 from zama-ai/fix/eaddress
Browse files Browse the repository at this point in the history
fixture(eaddress): truncate hex string from decrypted bytes (leading 00)
  • Loading branch information
immortal-tofu authored Mar 18, 2024
2 parents aa13198 + 1c93456 commit a401885
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhevmjs",
"version": "0.4.0-8",
"version": "0.4.0-9",
"description": "fhEVM SDK for blockchain using TFHE",
"main": "lib/node.cjs",
"types": "lib/node.d.ts",
Expand Down
28 changes: 28 additions & 0 deletions src/sdk/decrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,32 @@ describe('decrypt', () => {
const cleartext = decryptAddress(keypair, ciphertext);
expect(cleartext).toBe(getAddress(value.toString(16)));
});

it('decrypts an address Uint8Array value bigger than 160 bits', async () => {
const keypair = sodium.crypto_box_keypair();
const address = '0x9b8a8ba1f109551bd432803012645ac136ddd64dba72'
// Must truncate to 40-digit
const expected = '0x8ba1f109551bd432803012645ac136ddd64dba72'
const value = BigInt(address);
const ciphertext = sodium.crypto_box_seal(
bigIntToBytes(value),
keypair.publicKey,
);
const cleartext = decryptAddress(keypair, ciphertext);
expect(cleartext).toBe(getAddress(expected));
});

it('decrypts an address Uint8Array value lower than 160 bits', async () => {
const keypair = sodium.crypto_box_keypair();
const address = '0x8ba1f109551bd432803012645ac136ddd64d'
// Must add padding until to 40-digit
const expected = '0x00008ba1f109551bd432803012645ac136ddd64d'
const value = BigInt(address);
const ciphertext = sodium.crypto_box_seal(
bigIntToBytes(value),
keypair.publicKey,
);
const cleartext = decryptAddress(keypair, ciphertext);
expect(cleartext).toBe(getAddress(expected));
});
});
11 changes: 10 additions & 1 deletion src/sdk/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.slice(2).padStart(40, '0');
}
return getAddress(hexString);
};
10 changes: 1 addition & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,17 @@ 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');

return result;
return `0x${buffer.toString('hex')}`;
};

export const bytesToBigInt = function (byteArray: Uint8Array): bigint {
if (!byteArray || byteArray?.length === 0) {
return BigInt(0);
}

const length = byteArray.length;

const buffer = Buffer.from(byteArray);
const result = toBigIntBE(buffer);

return result;
};

Expand Down

0 comments on commit a401885

Please sign in to comment.