Ethereum address utils (#659)
* Ethereum address utils * export in base
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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}`;
|
||||
}
|
||||
@@ -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';
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user