Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixture(eaddress): truncate hex string from decrypted bytes (leading 00) #60

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading