Skip to content

Commit

Permalink
fix: private key without trailing 0x (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjlevesque committed Apr 12, 2021
1 parent 63071ec commit 4d71cae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/utils/src/crypto/ec-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default {
*/
function getAddressFromPrivateKey(privateKey: string): string {
try {
if (!privateKey.match(/^0x/)) {
privateKey = `0x` + privateKey;
}
return ethers.utils.computeAddress(ethers.utils.hexlify(privateKey));
} catch (e) {
if (
Expand Down
7 changes: 7 additions & 0 deletions packages/utils/test/crypto/ec-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ describe('Utils.ecUtils', () => {
'The private key must be a string representing 32 bytes',
);
});
it('can get an address from a private key without 0x', () => {
expect(
ecUtils.getAddressFromPrivateKey(
'af16c10a33bd8c2a0d55551080c3eb248ab727e5ff17d052c95f9d92b7e6528e',
),
).toBe('0xe011e28aBAa005223a2d4AEfFD5c2fF8D7B5291c');
});
});

describe('getAddressFromPublicKey', () => {
Expand Down

0 comments on commit 4d71cae

Please sign in to comment.