Add address check function (#443)

* Add address check function

* Add tests for checkAddress
This commit is contained in:
Jaco Greeff
2019-07-17 23:19:25 +02:00
committed by GitHub
parent edf681b134
commit df8c1038b9
7 changed files with 76 additions and 4 deletions
+2 -1
View File
@@ -17,7 +17,8 @@
"lint": "eslint . --ext .js,.jsx,.ts,.tsx . && tsc --noEmit --pretty",
"clean": "polkadot-dev-clean-build",
"postinstall": "polkadot-dev-yarn-only",
"test": "jest --coverage"
"test": "jest --coverage",
"test:one": "jest"
},
"devDependencies": {
"@babel/core": "^7.5.4",
@@ -0,0 +1,37 @@
// Copyright 2017-2019 @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 checkAddress from './check';
describe('checkAddress', (): void => {
it('returns [true, null] for Kusama', (): void => {
expect(
checkAddress('FJaco77EJ99VtBmVFibuBJR3x5Qq9KQrgQJvWjqScCcCCae', 2)
).toEqual([true, null]);
});
it('returns [true, null] for Substrate', (): void => {
expect(
checkAddress('5EnxxUmEbw8DkENKiYuZ1DwQuMoB2UWEQJZZXrTsxoz7SpgG', 42)
).toEqual([true, null]);
});
it('fails with invalid prefix when checking Substrate against Kusama prefix', (): void => {
expect(
checkAddress('5EnxxUmEbw8DkENKiYuZ1DwQuMoB2UWEQJZZXrTsxoz7SpgG', 2)
).toEqual([false, 'Prefix mismatch, expected 2, found 42']);
});
it('fails with invalid length when some bytes are missing', (): void => {
expect(
checkAddress('y9EMHt34JJo4rWLSaxoLGdYXvjgSXEd4zHUnQgfNzwES8b', 42)
).toEqual([false, 'Invalid decoded address length']);
});
it('fails with invalid length on checksum mismatch', (): void => {
expect(
checkAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU', 42)
).toEqual([false, 'Invalid decoded address checksum']);
});
});
+35
View File
@@ -0,0 +1,35 @@
// Copyright 2017-2019 @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 { Prefix } from './types';
import bs58 from 'bs58';
import defaults from './defaults';
import sshash from './sshash';
export default function check (address: string, prefix: Prefix): [boolean, string | null] {
const decoded = bs58.decode(address);
if (decoded[0] as Prefix !== prefix) {
return [false, `Prefix mismatch, expected ${prefix}, found ${decoded[0]}`];
} else if (!defaults.allowedEncodedLengths.includes(decoded.length)) {
return [false, 'Invalid decoded address length'];
}
const isPublicKey = decoded.length === 35;
// non-publicKeys has 1 byte checksums, else default to 2
const endPos = decoded.length - (isPublicKey ? 2 : 1);
// calculate the hash and do the checksum byte checks
const hash = sshash(decoded.subarray(0, endPos));
const checks = isPublicKey
? decoded[decoded.length - 2] === hash[0] && decoded[decoded.length - 1] === hash[1]
: decoded[decoded.length - 1] === hash[0];
return !checks
? [false, 'Invalid decoded address checksum']
: [true, null];
}
@@ -1,7 +1,6 @@
// Copyright 2017-2019 @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.
// @flow
import testingPairs, { TestKeyringMap } from '../../../keyring/src/testingPairs';
import decode from './decode';
@@ -1,7 +1,6 @@
// Copyright 2017-2019 @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.
// @flow
import testingPairs from '../../../keyring/src/testingPairs';
import encode from './encode';
@@ -2,11 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import checkAddress from './check';
import decodeAddress from './decode';
import encodeAddress from './encode';
import setAddressPrefix from './setPrefix';
export {
checkAddress,
decodeAddress,
encodeAddress,
setAddressPrefix
@@ -1,7 +1,6 @@
// Copyright 2017-2019 @polkadot/util-rlp authors & contributors
// This software may be modified and distributed under the terms
// of the MPL-2.0 license. See the LICENSE file for details.
// @flow
import safeParseInt from './safeParseInt';