Skip to content

Commit

Permalink
fixture(eaddress): truncate hex string from decrypted bytes (leading 00)
Browse files Browse the repository at this point in the history
The decrypted value could contain leading 0 bytes  (e.g. KMS returns 32 bytes serialized big int).
This behaviour triggers error with getAddress method from ethers which takes
hex string and return eth plain address (if hexstring > 40 digits)
  • Loading branch information
leventdem committed Mar 18, 2024
1 parent aa13198 commit 26248fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
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.padStart(40, '0');

Check warning on line 38 in src/sdk/decrypt.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 39 in src/sdk/decrypt.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return getAddress(hexString);
};
8 changes: 1 addition & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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);

Expand Down

0 comments on commit 26248fb

Please sign in to comment.