From df8c1038b941f79cafac3588e3f3ebc2df24b720 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 17 Jul 2019 23:19:25 +0200 Subject: [PATCH] Add address check function (#443) * Add address check function * Add tests for checkAddress --- package.json | 3 +- .../util-crypto/src/address/check.spec.ts | 37 +++++++++++++++++++ packages/util-crypto/src/address/check.ts | 35 ++++++++++++++++++ .../util-crypto/src/address/decode.spec.ts | 1 - .../util-crypto/src/address/encode.spec.ts | 1 - packages/util-crypto/src/address/index.ts | 2 + .../util-rlp/src/decoder/safeParseInt.spec.ts | 1 - 7 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 packages/util-crypto/src/address/check.spec.ts create mode 100644 packages/util-crypto/src/address/check.ts diff --git a/package.json b/package.json index 7683c814a..fdcee138f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/util-crypto/src/address/check.spec.ts b/packages/util-crypto/src/address/check.spec.ts new file mode 100644 index 000000000..2a0e48a16 --- /dev/null +++ b/packages/util-crypto/src/address/check.spec.ts @@ -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']); + }); +}); diff --git a/packages/util-crypto/src/address/check.ts b/packages/util-crypto/src/address/check.ts new file mode 100644 index 000000000..ffbd103a3 --- /dev/null +++ b/packages/util-crypto/src/address/check.ts @@ -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]; +} diff --git a/packages/util-crypto/src/address/decode.spec.ts b/packages/util-crypto/src/address/decode.spec.ts index 3c340fb6f..2d9e7701c 100644 --- a/packages/util-crypto/src/address/decode.spec.ts +++ b/packages/util-crypto/src/address/decode.spec.ts @@ -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'; diff --git a/packages/util-crypto/src/address/encode.spec.ts b/packages/util-crypto/src/address/encode.spec.ts index 2b3e0b608..cf4480dc2 100644 --- a/packages/util-crypto/src/address/encode.spec.ts +++ b/packages/util-crypto/src/address/encode.spec.ts @@ -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'; diff --git a/packages/util-crypto/src/address/index.ts b/packages/util-crypto/src/address/index.ts index 25cbce0bc..538f271c3 100644 --- a/packages/util-crypto/src/address/index.ts +++ b/packages/util-crypto/src/address/index.ts @@ -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 diff --git a/packages/util-rlp/src/decoder/safeParseInt.spec.ts b/packages/util-rlp/src/decoder/safeParseInt.spec.ts index abfaa4cb5..e9da5082e 100644 --- a/packages/util-rlp/src/decoder/safeParseInt.spec.ts +++ b/packages/util-rlp/src/decoder/safeParseInt.spec.ts @@ -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';