Skip to content

Commit

Permalink
Merge pull request #2037 from oasisprotocol/lw/jest-buffer
Browse files Browse the repository at this point in the history
Fix privateToEthAddress in jest
  • Loading branch information
lukaw3d authored Aug 13, 2024
2 parents aa802fd + 78c7d22 commit 7fe40c6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
1 change: 1 addition & 0 deletions .changelog/2037.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix privateToEthAddress in jest
4 changes: 1 addition & 3 deletions src/app/lib/__tests__/privateToEthAddress.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { privateToEthAddress } from '../eth-helpers'

// TODO: enable when jest fixes `instanceof Uint8Array`. This throws "TypeError: Expected valid private key".
// https://github.com/facebook/jest/issues/4422
test.skip('privateToEthAddress', () => {
test('privateToEthAddress', () => {
expect(privateToEthAddress('414bba7f242e9054f9fc119fe32d322a7d9bbe0cb8c75173a6826cc8b1af1370')).toEqual(
'0xFed5547859F9948d2C85F0516E3944377F88046f',
)
Expand Down
6 changes: 3 additions & 3 deletions src/app/lib/eth-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as oasis from '@oasisprotocol/client'
import * as oasisRT from '@oasisprotocol/client-rt'
import { bytesToHex, isValidPrivate, privateToAddress, toChecksumAddress } from '@ethereumjs/util'
import { hex2uint } from './helpers'
export { isValidAddress as isValidEthAddress, stripHexPrefix } from '@ethereumjs/util'

export const hexToBuffer = (value: string): Buffer => Buffer.from(value, 'hex')
export const isValidEthPrivateKey = (ethPrivateKey: string): boolean => {
try {
return isValidPrivate(hexToBuffer(ethPrivateKey))
return isValidPrivate(hex2uint(ethPrivateKey))
} catch {
return false
}
}
export const isValidEthPrivateKeyLength = (ethPrivateKey: string) => ethPrivateKey.length === 64
export const privateToEthAddress = (ethPrivateKey: string): string =>
toChecksumAddress(bytesToHex(privateToAddress(hexToBuffer(ethPrivateKey))))
toChecksumAddress(bytesToHex(privateToAddress(hex2uint(ethPrivateKey))))

export async function getEvmBech32Address(evmAddress: string) {
const evmBytes = oasis.misc.fromHex(evmAddress.replace('0x', ''))
Expand Down

0 comments on commit 7fe40c6

Please sign in to comment.