From 61b57687af2f2b076736037d690e47b526f85a3d Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 11 Aug 2020 16:46:01 +0200 Subject: [PATCH] Ethereum address utils (#659) * Ethereum address utils * export in base --- CHANGELOG.md | 1 + .../util-crypto/src/ethereum/encode.spec.ts | 29 ++++++++++++++++ packages/util-crypto/src/ethereum/encode.ts | 29 ++++++++++++++++ packages/util-crypto/src/ethereum/index.ts | 7 ++++ .../src/ethereum/isAddress.spec.ts | 33 +++++++++++++++++++ .../util-crypto/src/ethereum/isAddress.ts | 19 +++++++++++ .../util-crypto/src/ethereum/isChecksum.ts | 23 +++++++++++++ .../src/ethereum/isCheksum.spec.ts | 29 ++++++++++++++++ packages/util-crypto/src/index.ts | 1 + 9 files changed, 171 insertions(+) create mode 100644 packages/util-crypto/src/ethereum/encode.spec.ts create mode 100644 packages/util-crypto/src/ethereum/encode.ts create mode 100644 packages/util-crypto/src/ethereum/index.ts create mode 100644 packages/util-crypto/src/ethereum/isAddress.spec.ts create mode 100644 packages/util-crypto/src/ethereum/isAddress.ts create mode 100644 packages/util-crypto/src/ethereum/isChecksum.ts create mode 100644 packages/util-crypto/src/ethereum/isCheksum.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index cb6b3d46f..c274710cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 3.2.0-beta.x - Allow for (optional, default blake2) hasher to secp256k1 sign/verify functions +- Add basic Ethereum address utils (encode with checksum & checksum verification) ## 3.1.1 Aug 10, 2020 diff --git a/packages/util-crypto/src/ethereum/encode.spec.ts b/packages/util-crypto/src/ethereum/encode.spec.ts new file mode 100644 index 000000000..b43ea29d0 --- /dev/null +++ b/packages/util-crypto/src/ethereum/encode.spec.ts @@ -0,0 +1,29 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { ethereumEncode } from './'; + +const ADDRESS = '0x00a329c0648769A73afAc7F9381E08FB43dBEA72'; + +describe('formatAddress', () => { + it('returns 0x00..00 for no address', () => { + expect(ethereumEncode()).toBe('0x'); + }); + + it('returns 0x00..00 on invalid address', () => { + expect(ethereumEncode('0xnotaddress')).toBe('0x'); + }); + + it('converts lowercase to the checksummed address', () => { + expect(ethereumEncode(ADDRESS.toLowerCase())).toBe(ADDRESS); + }); + + it('converts uppercase to the checksummed address', () => { + expect(ethereumEncode(ADDRESS.toUpperCase().replace('0X', '0x'))).toBe(ADDRESS); + }); + + it('returns formatted address on checksum input', () => { + expect(ethereumEncode(ADDRESS)).toBe(ADDRESS); + }); +}); diff --git a/packages/util-crypto/src/ethereum/encode.ts b/packages/util-crypto/src/ethereum/encode.ts new file mode 100644 index 000000000..522c05de4 --- /dev/null +++ b/packages/util-crypto/src/ethereum/encode.ts @@ -0,0 +1,29 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { u8aToHex, u8aToU8a } from '@polkadot/util'; + +import { keccakAsU8a } from '../keccak'; +import isEthereumAddress from './isAddress'; + +export default function ethereumEncode (_address?: string | Uint8Array): string { + if (!_address) { + return '0x'; + } + + const address = u8aToHex(u8aToU8a(_address), -1, false).toLowerCase(); + + if (!isEthereumAddress(`0x${address}`)) { + return '0x'; + } + + const hash = u8aToHex(keccakAsU8a(address.toLowerCase()), -1, false); + let result = ''; + + for (let index = 0; index < 40; index++) { + result = `${result}${parseInt(hash[index], 16) > 7 ? address[index].toUpperCase() : address[index]}`; + } + + return `0x${result}`; +} diff --git a/packages/util-crypto/src/ethereum/index.ts b/packages/util-crypto/src/ethereum/index.ts new file mode 100644 index 000000000..ab5f54015 --- /dev/null +++ b/packages/util-crypto/src/ethereum/index.ts @@ -0,0 +1,7 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +export { default as ethereumEncode } from './encode'; +export { default as isEthereumAddress } from './isAddress'; +export { default as isEthereumChecksum } from './isChecksum'; diff --git a/packages/util-crypto/src/ethereum/isAddress.spec.ts b/packages/util-crypto/src/ethereum/isAddress.spec.ts new file mode 100644 index 000000000..e9b5e5bbf --- /dev/null +++ b/packages/util-crypto/src/ethereum/isAddress.spec.ts @@ -0,0 +1,33 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { isEthereumAddress } from './'; + +const ADDRESS = '0x00a329c0648769A73afAc7F9381E08FB43dBEA72'; + +describe('isEthereumAddress', () => { + it('returns true when fully lowercase', () => { + expect(isEthereumAddress(ADDRESS.toLowerCase())).toBe(true); + }); + + it('returns true when fully uppercase', () => { + expect(isEthereumAddress(ADDRESS.toUpperCase().replace('0X', '0x'))).toBe(true); + }); + + it('returns true when checksummed', () => { + expect(isEthereumAddress(ADDRESS)).toBe(true); + }); + + it('returns false when empty address', () => { + expect(isEthereumAddress()).toBe(false); + }); + + it('returns false when invalid address', () => { + expect(isEthereumAddress('0xinvalid')).toBe(false); + }); + + it('returns false when invalid address of correct length', () => { + expect(isEthereumAddress('0xinvalid000123456789012345678901234567890')).toBe(false); + }); +}); diff --git a/packages/util-crypto/src/ethereum/isAddress.ts b/packages/util-crypto/src/ethereum/isAddress.ts new file mode 100644 index 000000000..ab4e2fe1c --- /dev/null +++ b/packages/util-crypto/src/ethereum/isAddress.ts @@ -0,0 +1,19 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { isHex } from '@polkadot/util'; + +import isEthereumChecksum from './isChecksum'; + +export default function isEthereumAddress (address?: string): boolean { + if (!address || address.length !== 42 || !isHex(address)) { + return false; + } + + if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) { + return true; + } + + return isEthereumChecksum(address); +} diff --git a/packages/util-crypto/src/ethereum/isChecksum.ts b/packages/util-crypto/src/ethereum/isChecksum.ts new file mode 100644 index 000000000..e3a66b7d8 --- /dev/null +++ b/packages/util-crypto/src/ethereum/isChecksum.ts @@ -0,0 +1,23 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { u8aToHex } from '@polkadot/util'; + +import { keccakAsU8a } from '../keccak'; + +export default function isEthereumChecksum (_address: string): boolean { + const address = _address.replace('0x', ''); + const hash = u8aToHex(keccakAsU8a(address.toLowerCase()), -1, false); + + for (let index = 0; index < 40; index++) { + const char = address[index]; + const hashval = parseInt(hash[index], 16); + + if ((hashval > 7 && char !== char.toUpperCase()) || (hashval <= 7 && char !== char.toLowerCase())) { + return false; + } + } + + return true; +} diff --git a/packages/util-crypto/src/ethereum/isCheksum.spec.ts b/packages/util-crypto/src/ethereum/isCheksum.spec.ts new file mode 100644 index 000000000..c8484b91d --- /dev/null +++ b/packages/util-crypto/src/ethereum/isCheksum.spec.ts @@ -0,0 +1,29 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { isEthereumChecksum } from './'; + +const ADDRESS = '0x00a329c0648769A73afAc7F9381E08FB43dBEA72'; + +describe('isEthereumChecksum', () => { + it('returns false on invalid address', () => { + expect(isEthereumChecksum('0xinvalid')).toBe(false); + }); + + it('returns false on non-checksum address', () => { + expect(isEthereumChecksum('0x1234567890abcdeedcba1234567890abcdeedcba')).toBe(false); + }); + + it('returns false when fully lowercase', () => { + expect(isEthereumChecksum(ADDRESS.toLowerCase())).toBe(false); + }); + + it('returns false when fully uppercase', () => { + expect(isEthereumChecksum(ADDRESS.toUpperCase().replace('0X', '0x'))).toBe(false); + }); + + it('returns true on a checksummed address', () => { + expect(isEthereumChecksum(ADDRESS)).toBe(true); + }); +}); diff --git a/packages/util-crypto/src/index.ts b/packages/util-crypto/src/index.ts index 0cac66b19..1e5afcfe8 100644 --- a/packages/util-crypto/src/index.ts +++ b/packages/util-crypto/src/index.ts @@ -14,6 +14,7 @@ export * from './base32'; export * from './base58'; export * from './base64'; export * from './blake2'; +export * from './ethereum'; export * from './keccak'; export * from './key'; export * from './mnemonic';