Skip to content

Commit

Permalink
feat: add capitalizeAddressERC55 function (#933)
Browse files Browse the repository at this point in the history
* feat: add capitalizeAddressERC55 function

* chore: code style
  • Loading branch information
Cafe137 authored Jul 17, 2024
1 parent dae70ce commit 8e06014
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/utils/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ export type HexEthAddress = HexString<40>
const ETH_ADDR_BYTES_LENGTH = 20
const ETH_ADDR_HEX_LENGTH = 40

export function capitalizeAddressERC55(address: string): string {
if (address.startsWith('0x')) {
address = address.slice(2)
}
const addressHash = keccak256(address.toLowerCase())
let result = '0x'
for (let i = 0; i < address.length; i++) {
if (parseInt(addressHash[i], 16) > 7) {
result += address[i].toUpperCase()
} else {
result += address[i].toLowerCase()
}
}

return result
}

export function makeEthAddress(address: EthAddress | Uint8Array | string | unknown): EthAddress {
if (typeof address === 'string') {
const hexAddr = makeHexString(address, ETH_ADDR_HEX_LENGTH)
Expand Down
1 change: 1 addition & 0 deletions src/utils/expose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {

export {
EthAddress,
capitalizeAddressERC55,
ethToSwarmAddress,
fromLittleEndian,
isHexEthAddress,
Expand Down
41 changes: 37 additions & 4 deletions test/unit/utils/eth.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
/* eslint @typescript-eslint/no-empty-function: 0 */
import { expect } from 'chai'
import sinon from 'sinon'
import { wrapBytesWithHelpers } from '../../../src/utils/bytes'
import {
makeEthereumWalletSigner,
capitalizeAddressERC55,
ethToSwarmAddress,
fromLittleEndian,
isHexEthAddress,
JsonRPC,
makeEthereumWalletSigner,
toLittleEndian,
} from '../../../src/utils/eth'
import { HexString, hexToBytes } from '../../../src/utils/hex'
import { wrapBytesWithHelpers } from '../../../src/utils/bytes'
import { expect } from 'chai'
import sinon from 'sinon'

describe('eth', () => {
describe('capitalizeAddressERC55', () => {
it('should calculate checksum for address', () => {
// All caps
expect(capitalizeAddressERC55('0x52908400098527886E0F7030069857D2E4169EE7')).to.be(
'0x52908400098527886E0F7030069857D2E4169EE7',
)
expect(capitalizeAddressERC55('0x8617E340B3D01FA5F11F306F4090FD50E238070D')).to.be(
'0x8617E340B3D01FA5F11F306F4090FD50E238070D',
)
// All Lower
expect(capitalizeAddressERC55('0xde709f2102306220921060314715629080e2fb77')).to.be(
'0xde709f2102306220921060314715629080e2fb77',
)
expect(capitalizeAddressERC55('0x27b1fdb04752bbc536007a920d24acb045561c26')).to.be(
'0x27b1fdb04752bbc536007a920d24acb045561c26',
)
// Normal
expect(capitalizeAddressERC55('0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed')).to.be(
'0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
)
expect(capitalizeAddressERC55('0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359')).to.be(
'0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
)
expect(capitalizeAddressERC55('0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB')).to.be(
'0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
)
expect(capitalizeAddressERC55('0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb')).to.be(
'0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
)
})
})

describe('isEthAddress', () => {
const testValues = [
{ value: () => {}, result: false },
Expand Down

0 comments on commit 8e06014

Please sign in to comment.